6 years, 1 month ago.

Nucleo_Sensor_Demo - MEMS


I have used the Program Nucleo_Sensor_Demo by Team America and implemented SWO in order to read my values instead of using the serial connection. But this does not seems to work. Can anyone help on this matter..?

Here is the code:

include the mbed library with this snippet

#include "mbed.h"
#include "x_cube_mems.h"
#include "SWO.h"

SWO_Channel SWO;
DigitalOut myled(LED1);
//Serial pc(SERIAL_TX, SERIAL_RX);
volatile float TEMPERATURE_Value_C;
volatile float TEMPERATURE_Value_F;
volatile float HUMIDITY_Value;
volatile float PRESSURE_Value;
volatile AxesRaw_TypeDef MAG_Value;
volatile AxesRaw_TypeDef ACC_Value;
volatile AxesRaw_TypeDef GYR_Value;

int main() {
    /* Create sensor board object */
    static X_CUBE_MEMS *mems_expansion_board = X_CUBE_MEMS::Instance();
    while(1) {
        myled = 1; // LED is ON
        wait(0.2); // 200 ms
        myled = 0; // LED is OFF
        wait(1.0); // 1 sec
        /* Get instantaneous data from all sensors */
        mems_expansion_board->hts221.GetTemperature((float *)&TEMPERATURE_Value_C);
        mems_expansion_board->hts221.GetHumidity((float *)&HUMIDITY_Value);
        mems_expansion_board->lps25h.GetPressure((float *)&PRESSURE_Value);
        mems_expansion_board->lis3mdl.GetAxes((AxesRaw_TypeDef *)&MAG_Value);
        mems_expansion_board->lsm6ds0.Acc_GetAxes((AxesRaw_TypeDef *)&ACC_Value);
        mems_expansion_board->lsm6ds0.Gyro_GetAxes((AxesRaw_TypeDef *)&GYR_Value);
        TEMPERATURE_Value_F = (TEMPERATURE_Value_C * 1.8f) + 32.0f;
        SWO.printf("Temperature:\t\t %f C / %f F\r\n", TEMPERATURE_Value_C, TEMPERATURE_Value_F);
        pc.printf("Humidity:\t\t %f%%\r\n", HUMIDITY_Value);
        pc.printf("Pressure:\t\t %f hPa\r\n", PRESSURE_Value); 
        pc.printf("Magnetometer (mGauss):\t X: %d, Y: %d, Z: %d\r\n", MAG_Value.AXIS_X, MAG_Value.AXIS_Y, MAG_Value.AXIS_Z);
        pc.printf("Accelerometer (mg):\t X: %d, Y: %d, Z: %d\r\n", ACC_Value.AXIS_X, ACC_Value.AXIS_Y, ACC_Value.AXIS_Z);
        pc.printf("Gyroscope (mdps):\t X: %d, Y: %d, Z: %d\r\n", GYR_Value.AXIS_X, GYR_Value.AXIS_Y, GYR_Value.AXIS_Z);

1 Answer

6 years, 1 month ago.

I am assuming you tested the code on the F446. Please note that in order to use SWO you must run the ST-Link utility on your PC and open the Serial Wire Viewer window. There is a setting in that window to select the core clock of the CPU. That setting must match the F446 clock and should be 180000000 (180 MHz). With those settings my updated SWO testcode runs as expected on the F446. See here for the new version. See here for more information.

Hi Wim,

Yes I'm using the Nucleo-F446 with clock 180 Mhz..

I'm aware of STLink V2 must be installed and the setup of it.. that being said, prior to the testing of the MEMS shield, I'd made a "Hello world SWO test" which worked quite fine.. And therefore I began wondering if there exist some special option that I wasn't aware when using the MEMS-shield with SWO...?

posted by Hut The Nut 03 Apr 2016

I have run the SWO testcode with the MEMS IKS01A1 shield installed and it works fine. I have not tested it with your MEMS code above. Note that the SWO, SWDIO and SWCLK pins may not be used for anything else or the SWO viewer wont work.

// SWO is on pin PB_3
// SWDIO is on pin PA_13
// SWCLK is on pin PA_14
//I2C i2c(PB_9, PB_8); //I2C, OK
//DigitalIn in(PB_3);  //SWO, breaks SWO viewer
//DigitalIn in(PA_10); //RX UART1, OK
//DigitalIn in(PA_13); //SWDIO, breaks SWO viewer
//DigitalIn in(PA_14); //SWCLK, breaks SWO viewer

However, it looks like the MEMS shield only uses I2C on (PB_9, PB_8) and that still works fine on my board with the SWO demo. I guess you need to do some step-by-step testing of the components in the MEMS lib to figure out where it goes wrong.

Edit: Imported the MEMS lib from here and did some more testing. It works fine on my board. See here:


Make sure you update all libs and check to see that the SWO pin is not somehow shorted. I also noticed that sometimes after downloading a new bin the SWO viewer window is stuck and you need to reset the board or restart the SWO window.

posted by Wim Huiskamp 03 Apr 2016