Specify the Initialization, Output, and Termination
ThesetupImpl
andstepImpl
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, theMW_gpioInit
function is called insetupImpl
. To update the logic state of the digital output pin, a call toMW_gpioRead
is made from thestepImpl
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 the DigitalRead System object you created inSelect System Object Template.
In the MATLAB®编辑器,打开
DigitalRead.m
class file.Update the
setupImpl
method using the following code.methods(Access=protected)functionsetupImpl(obj)%#ok
ifisempty(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 The
coder.ceval
function executes calls to the C wrapper functions inMW_gpio.h
. The second and third arguments ofcoder.ceval
are theARM®Cortex®–A hardware board pin number and value, respectively.Update the
BuildInfo
method 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
Update the
stepImpl
method with the following code.methods(Access=protected)...functiony = stepImpl(obj)%#ok
y = double(0);ifisempty(coder.target)% Place simulation output code hereelse% Call C-function implementing device outputy = coder.ceval('MW_gpioRead', 9);endend...end Unlike the
DigitalWrite
System object, thestepImpl
method for theDigitalRead
System object defines an output,y
, which is the logical value of the chosen pin.Update the
releaseImpl
method with the following code.methods(Access=protected)...functionreleaseImpl(obj)%#ok
ifisempty(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
Create a Digital Read Block|Select System Object Template|Set Output Port Properties