AnsweredAssumed Answered

STM32F4 Accelerometer issues using an RTOS (HAL SPI problem?)

Question asked by Stuart.Michael on Dec 6, 2016
Latest reply on Dec 7, 2016 by Stuart.Michael



I have managed to create a program (using Keil), that can turn LED's on and off based on the orientation of the MCU. However when i try to implement the program as an RTOS with threads, the program doesn't appear to run correctly.


The code that i have currently within the RTOS thread currently does the following;


  1. Initialize Setup (i.e. initialize SPI1, GPIOA, GPIOD, GPIOE)
  2. Configure control register on LIS3DSH to output required gyro/accelerometer data for given axis
  3. Continuous loop that reads the gyro/accelerometer data from the register, and will enable or disable LED's respectively.


Obviously i know it's not efficient to have all the code within the thread, but for this purpose it's okay.


This program runs perfectly as a standalone (i.e. not as RTOS / threads), however as an RTOS thread it appears to hang after a certain point. It will reach the point where it turns on an LED, but that LED will remain turned on despite the orientation changing.


From the testing and debugging that i have done, the problem seems to lie with the SPI communication with the register (I use SPI_HAL_Transmit / SPI_HAL_Receive functions to read/write data).


For example, i changed my code such that the control register was still configured, but instead i made the continuous loop a simple LED enable / disable timed loop. A snippet of the testing code is below;



// Communication procedure to configure control register

address = 0x20;
address = 0x14;

// Main program loop

       for(ii=0; ii<10000000; ii++){}

       GPIOD->BSRR = 1<<14;
       GPIOD->BSRR = 1<<(15+16);
       for(ii=0; ii<10000000; ii++){}

       GPIOD->BSRR = 1<<15;
       GPIOD->BSRR = 1<<(14+16);


When the communication procedure code was enabled, the program would hang before any LEDs were enabled.

When the communication procedure code was disabled, the program would blink the LED's correctly.


Any ideas as to how i go about resolving the communication issues i am having using threads in an RTOS? I am pretty confused why it would work as a standalone program but not from within a thread as an RTOS.


Thanks for any help or advise.