ThesetupImpl
andstepImpl
方法钩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_gpioInit
function is called insetupImpl
. To update the logic state of the digital output pin, a call toMW_gpioWrite
is made fromstepImpl
method. At termination, a call toMW_gpioTerminate
is made fromreleaseImpl
method to release the hardware resource. Follow these steps to update the initialization, output, and termination code sections of theDigitalWrite
System object you created inSelect a System Object Template.
In the MATLAB®Editor, openDigitalWrite.m
class file.
Update thesetupImpl
method 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.ceval
function executes calls to the C wrapper functions indigitalio_arduino.h
. The second argument and third arguments ofcoder.ceval
are the hardware pin number and pin mode values, respectively.
Update theBuildInfo
method 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
Update thestepImpl
method 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
Update thereleaseImpl
method 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.
Create a Digital Write Block|Select a System Object Template|Test System Object on MATLAB Command Line