Main Content

Specify the Initialization, Output, and Termination Behavior

ThesetupImplandstepImpl方法钩C / c++函数系统object™. SeeWrite the Hardware-Specific C/C++ Codefor more information on creating C/C++ device driver code. 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_gpioWriteis made fromstepImplmethod. 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 theDigitalWriteSystem object you created inSelect a System Object Template.

  1. In the MATLAB®Editor, openDigitalWrite.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.cinclude('MW_gpio.h'); coder.ceval('MW_gpioInit', 9, 1);endend...end

    Thecoder.cevalfunction executes calls to the C wrapper functions indigitalio_arduino.h. The second argument and third arguments ofcoder.cevalare the hardware pin number and pin mode values, respectively.

  3. Update theBuildInfomethod using the following code.

    methods(Static)...functionupdateBuildInfo(buildInfo, context)ifcontext.isCodeGenTarget('rtw')% Update 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)...functionstepImpl(obj,u)%#okifisempty(coder.target)% Place simulation setup code hereelse% Call C-function implementing device outputcoder.ceval('MW_gpioWrite', 9, u);endend...end
  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 willTest System Object on MATLAB Command Line.

See Also

||