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. Follow these steps to develop the C/C++ code required to implement digital read and write functionality:

  1. Create a new empty file in the MATLAB® editor.

  2. Copy the following C++ code into the file.

    #include <Arduino.h>
    #include "digitalio_arduino.h" 
    
    extern "C" void digitalIOSetup(uint8_T pin, boolean_T mode)
    { 
       // mode = 0: Input 
       // mode = 1: Output
       if (mode) { 
          pinMode(pin, OUTPUT); 
       } 
       else 
       { 
          pinMode(pin, INPUT); 
       } 
    } 
    
    // Write a logic value to pin 
    extern "C" void writeDigitalPin(uint8_T pin, boolean_T val) 
    { 
       digitalWrite(pin, val); 
    }
    
    // Read a logic value from pin 
    extern "C" boolean_T readDigitalPin(uint8_T pin) 
    { 
       return digitalRead(pin); 
    }

    This code wraps the Arduino® C++ API to write to a digital I/O pin on the Arduino hardware board.

    Note

    While the C++ code shown here is specific to the Arduino hardware, the same principle can be extended to any hardware specific C/C++ API.

  3. Save the file as digitalio_arduino.cpp into the source folder, src.

  4. Create an empty header file and copy the following C++ code into the file.

    #ifndef _DIGITALIO_ARDUINO_H_
    #define _DIGITALIO_ARDUINO_H_
    #include "rtwtypes.h"
    
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    void digitalIOSetup(uint8_T pin, boolean_T mode);
    void writeDigitalPin(uint8_T pin, boolean_T val);
    boolean_T readDigitalPin(uint8_T pin);
    #ifdef __cplusplus
    }
    #endif
    #endif //_DIGITALIO_ARDUINO_H_
  5. Save the file as digitalio_arduino.h into the include folder.

    This header file defines the C prototypes of the functions implemented in the C++ file, digitalio_arduino.cpp.

Many hardware devices either do not support or recommend using C++ compilers, for example the Simulink Support Package for Arduino Hardware uses a C compiler called avr-gcc. 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 digitalio_arduino.cpp function includes an Arduino.h file that defines the pinMode and digitalRead functions. Simulink data types are used for pin and val variables. For this reason, the rtwtypes.h file is included in digitalio_arduino.h. You must include this file whenever you reference to Simulink data types. Because pin is a number between 0 and 53, the uint8_T data type is used to represent this variable. The value returned by the function is the digital value of the hardware pin and is represented by boolean_T data type.

In the next section, you will Select System Object Template and begin the populate the methods.

See Also

| |