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.

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:

  1. 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'))
  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__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
  3. Save a copy of the fileMW_gpio.hinto the include folder,include, of your device driver project folder, seeCreate a Project Folder.

  4. 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”))
  5. Save a copy of the fileMW_gpio.cinto the source folder,src, of your device driver project folder, seeCreate a Project Folder.

Warning

Do not modify theMW_gpio.handMW_gpio.cfiles 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.cfunction we includeMW_gpio.hfile that defines the initialize, read, write, and terminate functions of the GPIO pins. Note that we are using Simulink data types forgpioanddirectionvariables. For this reason, we includertwtypes.hfile inMW_gpio.h. You must include this file whenever you reference to Simulink data types. Sincegpiois a number between 0 and 53 we use theuint8_Tdata type to represent this variable. The in variable is the value to be written to the digital output pin and is represented byboolean_Tdata type.

In the next section, you willSelect System Object Templatefor the System object.

See Also

||