Write the Hardware Specific C/C++ Code
In most cases, to integrate device driver code into a Simulink® block, you need to write a wrapper function around the API provided by the hardware vendor.
All ARM® Cortex®-A processor derived support packages use a common set of C/C++ files for their GPIO read and write operations.
Follow these steps to access the C/C++ code required to implement digital read and write functionality:
Open the C header file,
MW_gpio.h
, for the ARM Cortex-A processors.edit(fullfile(codertarget.arm_cortex_a.internal.getSpPkgRootDir,'include','MW_gpio.h'))
The header provides the C function prototypes that get called in the System object.
// Copyright 2012-2015 The MathWorks, Inc. #ifndef _MW_GPIO_H_ #define _MW_GPIO_H_ #include "rtwtypes.h" #ifdef __cplusplus extern "C" { #endif // Common definitions #define GPIO_MAX_BUF (128) #define GPIO_DIRECTION_INPUT (1) // MATLAB numbering #define GPIO_DIRECTION_OUTPUT (2) extern void MW_gpioInit(int32_T gpio, boolean_T direction); extern void MW_gpioTerminate(int32_T gpio); extern boolean_T MW_gpioRead(int32_T gpio); extern void MW_gpioWrite(int32_T gpio, boolean_T value); #ifdef __cplusplus } #endif #endif
Save a copy of the file
MW_gpio.h
into the include folder,include
, of your device driver project folder, see Create a Project Folder.Open the C source file,
MW_gpio.c
, for the ARM Cortex-A processors.edit(fullfile(codertarget.arm_cortex_a.internal.getSpPkgRootDir,'src','MW_gpio.c'))
Save a copy of the file
MW_gpio.c
into the source folder,src
, of your device driver project folder, see Create a Project Folder.
Warning
Do not modify the MW_gpio.h
and MW_gpio.c
files
in the ARM
Cortex-A directory.
Many hardware devices either do not support or recommend using C++ compilers. In order to compile and link C++ functions with a C compiler, you need to add the extern "C" identifier in each function declaration to tell the compiler not to mangle function names so that they can be used with the C linker.
The MW_gpio.c
function includes the MW_gpio.h
file that
defines the initialize, read, write, and terminate functions of the GPIO pins. Note that
Simulink data types are used for gpio
and
direction
variables. For this reason, the
rtwtypes.h
file is included in MW_gpio.h
. You must
include this file whenever you reference to Simulink data types. Because gpio
is a number between 0 and 53, the
uint8_T
data type is used to represent this variable. The variable
in
is the value to be written to the digital output pin and is
represented by boolean_T
data type.
In the next section, you will Select System Object Template for the System object.
See Also
Create a Digital Read Block | Create a Project Folder | Select System Object Template