Specify the Initialization, Output, and Termination Behavior
The setupImpl and stepImpl methods
hook the C/C++ functions to the System object™. See Write the Hardware-Specific C/C++ Code for
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, the MW_gpioInit function is called in setupImpl.
To update the logic state of the digital output pin, a call to MW_gpioWrite is
made from 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 DigitalWrite System object you
created in Select a System Object Template.
- In the MATLAB® Editor, open - DigitalWrite.mclass file.
- Update the - setupImplmethod 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.cinclude('MW_gpio.h'); coder.ceval('MW_gpioInit', 9, 1); end end ... end - The - coder.cevalfunction executes calls to the C wrapper functions in- digitalio_arduino.h. The second argument and third arguments of- coder.cevalare the hardware pin number and pin mode values, respectively.
- Update the - BuildInfomethod 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 
- Update the - stepImplmethod with the following code.- methods(Access=protected) ... function stepImpl(obj,u) %#ok<INUSD> if isempty(coder.target) % Place simulation setup code here else % Call C-function implementing device output coder.ceval('MW_gpioWrite', 9, u); end end ... end 
- Update the - releaseImplmethod 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 Test System Object on MATLAB Command Line.
See Also
Create a Digital Write Block | Select a System Object Template | Test System Object on MATLAB Command Line