Skip to main content
Visitor II
March 23, 2025
Question

FreeRTOS STM32 LIS2DH12 and LIS3MDL data read problem

  • March 23, 2025
  • 0 replies
  • 519 views

Hello friends;
I aim to develop FreeRTOS STM32 based flight control card software. First of all, I am trying to read data from my LIS2DH12 and LIS3MDL sensors in separate tasks and see them on my serial screen at usart1 921600 baudrate for debug purposes. However, I encounter a few problems. I present my project file in the attachment and I will explain the problems I encounter in detail below. Thank you in advance for your interest.
These are ;

1-) When I run the UART in normal mode, I can only read data from LIS2DH12.LIS3MDL only ends with a not found sensor 0xFF warning at startup, but the data flow of LIS2DH12 continues.

2-)When I run the UART with DMA and semaphore, I can only read LIS3MDL data. This time I cannot see the LIS2DH12 sensor data on my serial screen.

3-) However, sometimes the sensors sometimes interrupt the serial screen data flow, as if the sensor is locked and becomes inactive, but every time I reset the stm32, 10-20 data comes and goes back. After a few reset attempts, it returns to normal continuous data flow.

4-) As a result of the data interruption of the sensors I mentioned in the 3rd problem, I encounter the same problem when I try to run the sensors in another project file that I read with the polling method in order to debug the sensors individually, and I can perform this single debug by making a few reset attempts or plugging and unplugging the stmin usb a few times.

5-)Although the LIS2DH12 and LIS3MDL sensors should be operated as SPI MODE 3 on the ST site and datasheets, I cannot read the correct whoiam addresses from the sensors when in mode 3, but I can only read in MODE 0. What could be the reasons for this?

I wonder if there is a problem in my SPI and UART settings, I would be very grateful if you can check. Have a good day.

/* USER CODE BEGIN Header */
/**
 ******************************************************************************
 * File Name : freertos.c
 * Description : Code for freertos applications
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2025 STMicroelectronics.
 * All rights reserved.
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 * If no LICENSE file comes with this software, it is provided AS-IS.
 *
 ******************************************************************************
 */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "task.h"
#include "timers.h"
#include "queue.h"
#include "semphr.h"
#include "lis2dh12_reg.h"
#include "lis3mdl_reg.h"

#include <stdio.h>
#include <string.h>
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
typedef struct {
 float acceleration_g[3];
 uint32_t timestamp_ms;
} lis2dh12_data_t;

typedef struct {
 float magnetic_mG[3];
 uint32_t timestamp_ms;
} lis3mdl_data_t;
/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
extern UART_HandleTypeDef huart1;
extern SPI_HandleTypeDef hspi1;
extern SPI_HandleTypeDef hspi2;
extern IWDG_HandleTypeDef hiwdg;
/* ----------------------------- RTOS OBJECTS ----------------------------- */
QueueHandle_t lis2dh12Queue;
QueueHandle_t lis3mdlQueue;
SemaphoreHandle_t uartTxMutex;
SemaphoreHandle_t uartTxSemaphore;

/* DMA transfer tamamlandı flag’i */
volatile uint8_t uart_dma_done = 1;
/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */

/* USER CODE END Variables */
osThreadId defaultTaskHandle;

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart);
void uart_send_dma(const char *data);

void lis2dh12_task(void *param);
void lis3mdl_task(void *param);
void uart_logger_task(void *params);

int32_t platform_write_lis2dh12(void *handle, uint8_t reg, const uint8_t *bufp, uint16_t len);
int32_t platform_read_lis2dh12(void *handle, uint8_t reg, uint8_t *bufp, uint16_t len);
int32_t platform_write_lis3mdl(void *handle, uint8_t reg, const uint8_t *bufp, uint16_t len);
int32_t platform_read_lis3mdl(void *handle, uint8_t reg, uint8_t *bufp, uint16_t len) ;

void platform_delay(uint32_t ms);
/* USER CODE END FunctionPrototypes */

void StartDefaultTask(void const * argument);

void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/* GetIdleTaskMemory prototype (linked to static allocation support) */
void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize );

/* USER CODE BEGIN GET_IDLE_TASK_MEMORY */
static StaticTask_t xIdleTaskTCBBuffer;
static StackType_t xIdleStack[configMINIMAL_STACK_SIZE];

void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize )
{
 *ppxIdleTaskTCBBuffer = &xIdleTaskTCBBuffer;
 *ppxIdleTaskStackBuffer = &xIdleStack[0];
 *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
 /* place for user code */
}
/* USER CODE END GET_IDLE_TASK_MEMORY */

/**
 * @brief FreeRTOS initialization
 * @PAram None
 * @retval None
 */
void MX_FREERTOS_Init(void) {
 /* USER CODE BEGIN Init */

 /* USER CODE END Init */

 /* USER CODE BEGIN RTOS_MUTEX */
 /* add mutexes, ... */
 uartTxSemaphore = xSemaphoreCreateBinary();
 xSemaphoreGive(uartTxSemaphore);
 /* USER CODE END RTOS_MUTEX */

 /* USER CODE BEGIN RTOS_SEMAPHORES */
 /* add semaphores, ... */
	uartTxMutex = xSemaphoreCreateMutex();
 /* USER CODE END RTOS_SEMAPHORES */

 /* USER CODE BEGIN RTOS_TIMERS */
 /* start timers, add new ones, ... */
 /* USER CODE END RTOS_TIMERS */

 /* USER CODE BEGIN RTOS_QUEUES */
 /* add queues, ... */
	 lis2dh12Queue = xQueueCreate(32, sizeof(lis2dh12_data_t));
	 lis3mdlQueue = xQueueCreate(32, sizeof(lis3mdl_data_t));
 /* USER CODE END RTOS_QUEUES */

 /* Create the thread(s) */
 /* definition and creation of defaultTask */
 osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 128);
 defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);

 /* USER CODE BEGIN RTOS_THREADS */
 /* add threads, ... */

 xTaskCreate(lis2dh12_task, "LIS2", 512, NULL, 3, NULL);
 xTaskCreate(lis3mdl_task, "LIS3", 512, NULL, 3, NULL);
 xTaskCreate(uart_logger_task, "UART_LOG", 768, NULL, 2, NULL);
 /* USER CODE END RTOS_THREADS */

}

/* USER CODE BEGIN Header_StartDefaultTask */
/**
 * @brief Function implementing the defaultTask thread.
 * @PAram argument: Not used
 * @retval None
 */
/* USER CODE END Header_StartDefaultTask */
void StartDefaultTask(void const * argument)
{
 /* USER CODE BEGIN StartDefaultTask */
 /* Infinite loop */
 for(;;)
 {
 osDelay(1);
 }
 /* USER CODE END StartDefaultTask */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) {
 if (huart == &huart1) {
 xSemaphoreGiveFromISR(uartTxSemaphore, NULL);
 }
}

void uart_send_dma(const char *data) {
 if (xSemaphoreTake(uartTxMutex, portMAX_DELAY) == pdTRUE) {
 HAL_UART_Transmit_DMA(&huart1, (uint8_t *)data, strlen(data));
 xSemaphoreTake(uartTxSemaphore, portMAX_DELAY);
 xSemaphoreGive(uartTxMutex);
 }
}


/* ----------------------------- LIS2DH12 TASK ----------------------------- */
void lis2dh12_task(void *param) {
 stmdev_ctx_t ctx = {
 .write_reg = platform_write_lis2dh12,
 .read_reg = platform_read_lis2dh12,
 .mdelay = platform_delay,
 .handle = &hspi1
 };

 uint8_t whoamI = 0;
 lis2dh12_device_id_get(&ctx, &whoamI);
 if (whoamI != LIS2DH12_ID) Error_Handler();

 lis2dh12_block_data_update_set(&ctx, PROPERTY_ENABLE);
 lis2dh12_data_rate_set(&ctx, LIS2DH12_ODR_400Hz);
 lis2dh12_full_scale_set(&ctx, LIS2DH12_2g);
 lis2dh12_operating_mode_set(&ctx, LIS2DH12_HR_12bit);

 TickType_t lastWakeTime = xTaskGetTickCount();

 for (;;) {
 lis2dh12_reg_t reg;
 lis2dh12_xl_data_ready_get(&ctx, &reg.byte);
 if (reg.byte) {
 int16_t raw[3] = {0};
 lis2dh12_acceleration_raw_get(&ctx, raw);
 lis2dh12_data_t data = {
 .acceleration_g[0] = lis2dh12_from_fs2_hr_to_mg(raw[0]) / 1000.0f,
 .acceleration_g[1] = lis2dh12_from_fs2_hr_to_mg(raw[1]) / 1000.0f,
 .acceleration_g[2] = lis2dh12_from_fs2_hr_to_mg(raw[2]) / 1000.0f,
 .timestamp_ms = xTaskGetTickCount() * portTICK_PERIOD_MS
 };
 if (xQueueSend(lis2dh12Queue, &data, 0) != pdPASS) {
 const char *warn = "[WARN] lis2dh12Queue FULL!\r\n";
 uart_send_dma(warn);
 }
 }
 HAL_IWDG_Refresh(&hiwdg);
 vTaskDelayUntil(&lastWakeTime, pdMS_TO_TICKS(2));
 }
}



/* ----------------------------- LIS3MDL TASK ----------------------------- */
void lis3mdl_task(void *param) {
 stmdev_ctx_t ctx = {
 .write_reg = platform_write_lis3mdl,
 .read_reg = platform_read_lis3mdl,
 .mdelay = platform_delay,
 .handle = &hspi2
 };

 uint8_t whoamI = 0;
 lis3mdl_device_id_get(&ctx, &whoamI);
 if (whoamI != LIS3MDL_ID) Error_Handler();

 lis3mdl_reset_set(&ctx, PROPERTY_ENABLE);
 vTaskDelay(pdMS_TO_TICKS(10));

 lis3mdl_block_data_update_set(&ctx, PROPERTY_ENABLE);
 lis3mdl_data_rate_set(&ctx, LIS3MDL_HP_300Hz);
 lis3mdl_full_scale_set(&ctx, LIS3MDL_16_GAUSS);
 lis3mdl_operating_mode_set(&ctx, LIS3MDL_CONTINUOUS_MODE);

 TickType_t lastWakeTime = xTaskGetTickCount();
 const TickType_t period = pdMS_TO_TICKS(3);

 for (;;) {
 uint8_t drdy;
 lis3mdl_mag_data_ready_get(&ctx, &drdy);
 if (drdy) {
 int16_t raw[3];
 lis3mdl_magnetic_raw_get(&ctx, raw);
 lis3mdl_data_t data = {
 .magnetic_mG[0] = 1000.0f * lis3mdl_from_fs16_to_gauss(raw[0]),
 .magnetic_mG[1] = 1000.0f * lis3mdl_from_fs16_to_gauss(raw[1]),
 .magnetic_mG[2] = 1000.0f * lis3mdl_from_fs16_to_gauss(raw[2]),
 .timestamp_ms = xTaskGetTickCount() * portTICK_PERIOD_MS
 };
 if (xQueueSend(lis3mdlQueue, &data, 0) != pdPASS) {
 const char *warn = "[WARN] lis3mdlQueue FULL!\r\n";
 uart_send_dma(warn);
 }
 }
 HAL_IWDG_Refresh(&hiwdg);
 vTaskDelayUntil(&lastWakeTime, period);
 }
}

/* ----------------------------- UART LOGGER TASK ----------------------------- */
void uart_logger_task(void *params) {
 lis2dh12_data_t acc;
 lis3mdl_data_t mag;
 char buffer_acc[128];
 char buffer_mag[128];

 for (;;) {
 if (xQueueReceive(lis2dh12Queue, &acc, pdMS_TO_TICKS(1)) == pdPASS) {
 snprintf(buffer_acc, sizeof(buffer_acc),
 "[%lu] ACC [G]: %.3f, %.3f, %.3f\r\n",
 acc.timestamp_ms,
 acc.acceleration_g[0],
 acc.acceleration_g[1],
 acc.acceleration_g[2]);
 uart_send_dma(buffer_acc);
 }

 if (xQueueReceive(lis3mdlQueue, &mag, pdMS_TO_TICKS(1)) == pdPASS) {
 snprintf(buffer_mag, sizeof(buffer_mag),
 "[%lu] MAG [mG]: %.2f, %.2f, %.2f\r\n",
 mag.timestamp_ms,
 mag.magnetic_mG[0],
 mag.magnetic_mG[1],
 mag.magnetic_mG[2]);
 uart_send_dma(buffer_mag);
 }

 HAL_IWDG_Refresh(&hiwdg);
 vTaskDelay(pdMS_TO_TICKS(1));
 }
}


/* ----------------------------- PLATFORM DRIVERS ----------------------------- */
int32_t platform_write_lis2dh12(void *handle, uint8_t reg, const uint8_t *bufp, uint16_t len) {
 reg |= 0x40;
 HAL_GPIO_WritePin(CS_LIS2_GPIO_Port, CS_LIS2_Pin, GPIO_PIN_RESET);
 HAL_SPI_Transmit(handle, &reg, 1, 100);
 HAL_SPI_Transmit(handle, (uint8_t*)bufp, len, 100);
 HAL_GPIO_WritePin(CS_LIS2_GPIO_Port, CS_LIS2_Pin, GPIO_PIN_SET);
 return 0;
}
int32_t platform_read_lis2dh12(void *handle, uint8_t reg, uint8_t *bufp, uint16_t len) {
 reg |= 0xC0;
 HAL_GPIO_WritePin(CS_LIS2_GPIO_Port, CS_LIS2_Pin, GPIO_PIN_RESET);
 HAL_SPI_Transmit(handle, &reg, 1, 100);
 HAL_SPI_Receive(handle, bufp, len, 100);
 HAL_GPIO_WritePin(CS_LIS2_GPIO_Port, CS_LIS2_Pin, GPIO_PIN_SET);
 return 0;
}
int32_t platform_write_lis3mdl(void *handle, uint8_t reg, const uint8_t *bufp, uint16_t len) {
 reg |= 0x40;
 HAL_GPIO_WritePin(CS_LIS3_GPIO_Port, CS_LIS3_Pin, GPIO_PIN_RESET);
 HAL_SPI_Transmit(handle, &reg, 1, 100);
 HAL_SPI_Transmit(handle, (uint8_t*)bufp, len, 100);
 HAL_GPIO_WritePin(CS_LIS3_GPIO_Port, CS_LIS3_Pin, GPIO_PIN_SET);
 return 0;
}
int32_t platform_read_lis3mdl(void *handle, uint8_t reg, uint8_t *bufp, uint16_t len) {
 reg |= 0xC0;
 HAL_GPIO_WritePin(CS_LIS3_GPIO_Port, CS_LIS3_Pin, GPIO_PIN_RESET);
 HAL_SPI_Transmit(handle, &reg, 1, 100);
 HAL_SPI_Receive(handle, bufp, len, 100);
 HAL_GPIO_WritePin(CS_LIS3_GPIO_Port, CS_LIS3_Pin, GPIO_PIN_SET);
 return 0;
}
void platform_delay(uint32_t ms) {
 vTaskDelay(pdMS_TO_TICKS(ms));
}
/* USER CODE END Application */
    This topic has been closed for replies.