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.

    edit(fullfile(codertarget.arm_cortex_a.internal.getSpPkgRootDir,'include','MW_gpio.h'))
  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"
    {
    #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
    
  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.

    edit(fullfile(codertarget.arm_cortex_a.internal.getSpPkgRootDir,'src','MW_gpio.c'))
  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.

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 rtwtypes.h file must be included whenever referencing a Simulink data types.

In the next section, you will Select a System Object Template for the System object.

See Also

| |