Main Content

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:

  1. Open the C header file, MW_gpio.h, for the ARM Cortex-A processors.

  2. 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"
    // 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
  3. 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.

  4. Open the C source file, MW_gpio.c, for the ARM Cortex-A processors.

  5. 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.


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.

In the MW_gpio.c function we include MW_gpio.h file that defines the initialize, read, write, and terminate functions of the GPIO pins. Note that we are using Simulink data types for gpio and direction variables. For this reason, we include rtwtypes.h file in MW_gpio.h. You must include this file whenever you reference to Simulink data types. Since gpio is a number between 0 and 53 we use the uint8_T data type to represent this variable. The in variable 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

| |