How modify code

The code proposed is just made to be used as example and propose an architecture. Any compatible library can be integrated, and included library can be reused in other projects.

Platform support

Framework:

  • Arduino

This code is made to be used with:

  • All

General configuration

To be able to read sensors on your platform, some configurations are needed, especially the GPIO assignments for the SPI bus. Everything is located in the src/common.hpp file.

GPIO

Adjust the GPIO assignments with directly modifuing the PIN_CONFIG structure. Depending of the wiring chosen, this fields should be modified:

struct PIN_CONFIG
{
    uint8_t pinSS    = SS;
    uint8_t pinMmcSS = 11U;
    uint8_t pinLpsSS = 9U;
    uint8_t pinMosi  = 12U;
    uint8_t pinMiso  = 13U;
    uint8_t pinClk   = 14U;
};

SPI configuration

The SPI configuration can also be found in src/common.hpp file. It is configured with the struct GENERAL_CONFIG:

struct GENERAL_CONFIG
{
    uint32_t icmSpiClock = 24000000U; // [Hz]
    uint32_t lpsSpiClock = 10000000U; // [Hz]
    uint32_t mmcSpiClock = 10000000U; // [Hz]
};

Tasks

There is only a single task located in the loop() function. It is used to read all three sensors at 500Hz frequency.

Sensors

A light mechanism is used to manage sensors. It is described by SENSOR_UNIT_CONFIG. Any sensor can be modified, removed or added with the same configuration as the existing ones.

The principle is the create a sensor object, set its activation state and indicate the frequency at which it should be read. Once inside the task that will be used to read the sensor, a call to SENSOR_UNIT_CONFIG::setRatio() with the current task period will complete the sensor configuration.

Once inside the task’s loop, a call to SENSOR_UNIT_CONFIG::checkRatio() will return true if the sensor should be read at this loop iteration.

An example could be:

// Create a sensor object which is activated and should be read at 100Hz (10ms period)
SENSOR_UNIT_CONFIG sensorTest = SENSOR_UNIT_CONFIG(true, 10);

// Inside task setup
sensorTest.setRatio(currentTask.period);

// Inside task loop
while(true)
{
    if(sensorTest.checkRatio())
    {
        // Read sensor
    }
}

Add a library to the code

Any library code can be cloned in the lib folder. It will be compiled and linked with the code thanks to platformio library dependency finder.

If a library is added, user just need to include its headers in the app_ins code and add include source code folders in platformio.ini file of app_ins folder:

build_flags =
    -I lib/newlib_folder/src/
    ...

Like explained in the Build code section, we can build the code with:

pio run -e your_platform

and build+flash with:

pio run -e your_platform --target upload