Main Content

Specify the Initialization, Output, and Termination

The setupImpl and stepImpl methods hook the C functions to the System object™. The initialization of a digital pin as output needs to be done only once at model initialization. Hence, the MW_gpioInit function is called in setupImpl. To update the logic state of the digital output pin, a call to MW_gpioRead is made from the stepImpl method. At termination, a call to MW_gpioTerminate is made from releaseImpl method to release the hardware resource. Follow these steps to update the initialization, output, and termination code sections of the DigitalRead System object you created in Select System Object Template.

  1. In the MATLAB® Editor, open DigitalRead.m class file.

  2. Update the setupImpl method using the following code.

    methods (Access=protected)
        function setupImpl(obj) %#ok<MANU>
            if isempty(coder.target)
                % Place simulation setup code here
            else
                % Call C-function implementing device initialization
                coder.cinlcude('MW_gpio.h');
                coder.ceval('MW_gpioInit', 9, 0);
            end
        end
        ...
    end

    The coder.ceval function executes calls to the C wrapper functions in MW_gpio.h. The second and third arguments of coder.ceval are the ARM® Cortex®–A hardware board pin number and value, respectively.

  3. Update the BuildInfo method using the following code.

    methods (Static)
        ...
        function updateBuildInfo(buildInfo, context)
            if context.isCodeGenTarget('rtw')
                % Update buildInfo
                srcDir = fullfile(fileparts(mfilename('fullpath')),'src'); %#ok     
                includeDir = fullfile(fileparts(mfilename('fullpath')),'include');                 
                addIncludePaths(buildInfo,includeDir);
                % Use the following API's to add include files, sources and linker flags
                addSourceFiles(buildInfo,'MW_gpio.c', srcDir);
            end
        end
        ...
    end
  4. Update the stepImpl method with the following code.

    methods(Access=protected)
        ...
        function y = stepImpl(obj) %#ok<MANU>
            y = double(0);
            if isempty(coder.target)
                % Place simulation output code here
            else
                % Call C-function implementing device output
                y = coder.ceval('MW_gpioRead', 9);
            end
        end
        ...
    end

    Unlike the DigitalWrite System object, the stepImpl method for the DigitalRead System object defines an output, y, which is the logical value of the chosen pin.

  5. Update the releaseImpl method with the following code.

    methods(Access=protected)
        ...
        function releaseImpl(obj) %#ok<MANU>
            if isempty(coder.target)
                % Place simulation termination code here
            else
                % Call C-function implementing device termination
                coder.ceval('MW_gpioTerminate', 9);
            end
        end
        ...
    end

In the next section, you will Set Output Port Properties of your System object.

See Also

| |