Main Content

Specify the Initialization, Output, and Termination

ThesetupImplandstepImplmethods 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, theMW_gpioInitfunction is called insetupImpl. To update the logic state of the digital output pin, a call toMW_gpioReadis made from thestepImplmethod. At termination, a call toMW_gpioTerminateis made fromreleaseImplmethod to release the hardware resource. Follow these steps to update the initialization, output, and termination code sections of the DigitalRead System object you created inSelect System Object Template.

  1. In the MATLAB®编辑器,打开DigitalRead.mclass file.

  2. Update thesetupImplmethod using the following code.

    methods(Access=protected)functionsetupImpl(obj)%#okifisempty(coder.target)% Place simulation setup code hereelse% Call C-function implementing device initializationcoder.cinlcude('MW_gpio.h'); coder.ceval('MW_gpioInit', 9, 0);endend...end

    Thecoder.cevalfunction executes calls to the C wrapper functions inMW_gpio.h. The second and third arguments ofcoder.cevalare theARM®Cortex®–A hardware board pin number and value, respectively.

  3. Update theBuildInfomethod using the following code.

    methods(Static)...functionupdateBuildInfo(buildInfo, context)ifcontext.isCodeGenTarget('rtw')%更新buildInfosrcDir = 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 flagsaddSourceFiles(buildInfo,'MW_gpio.c', srcDir);endend...end
  4. Update thestepImplmethod with the following code.

    methods(Access=protected)...functiony = stepImpl(obj)%#oky = double(0);ifisempty(coder.target)% Place simulation output code hereelse% Call C-function implementing device outputy = coder.ceval('MW_gpioRead', 9);endend...end

    Unlike theDigitalWriteSystem object, thestepImplmethod for theDigitalReadSystem object defines an output,y, which is the logical value of the chosen pin.

  5. Update thereleaseImplmethod with the following code.

    methods(Access=protected)...functionreleaseImpl(obj)%#okifisempty(coder.target)% Place simulation termination code hereelse% Call C-function implementing device terminationcoder.ceval('MW_gpioTerminate', 9);endend...end

In the next section, you willSet Output Port Propertiesof your System object.

See Also

||