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.
AllARM®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 theARM 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__cplusplusextern"C"{#endif// Common definitions#defineGPIO_MAX_BUF (128)#defineGPIO_DIRECTION_INPUT (1)// MATLAB numbering#defineGPIO_DIRECTION_OUTPUT (2)externvoidMW_gpioInit(int32_T gpio, boolean_T direction);externvoidMW_gpioTerminate(int32_T gpio);externboolean_T MW_gpioRead(int32_T gpio);externvoidMW_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, seeCreate a Project Folder.Open the C source file,
MW_gpio.c
, for theARM 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, seeCreate a Project Folder.
Warning
Do not modify theMW_gpio.h
andMW_gpio.c
files in theARM 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 theextern"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.
In theMW_gpio.c
function we includeMW_gpio.h
file that defines the initialize, read, write, and terminate functions of the GPIO pins. Note that we are using Simulink data types forgpio
anddirection
variables. For this reason, we includertwtypes.h
file inMW_gpio.h
. You must include this file whenever you reference to Simulink data types. Sincegpio
is a number between 0 and 53 we use theuint8_T
data type to represent this variable. The in variable is the value to be written to the digital output pin and is represented byboolean_T
data type.
In the next section, you willSelect System Object Templatefor the System object.
See Also
Create a Digital Read Block|Create a Project Folder|Select System Object Template