Ring buffer impl
Implemented ring buffer with inline push and pop functions Updated copyrights on main.c and ring_buffer files
This commit is contained in:
14
STM32/CM7/Core/Inc/ring_buffer.h
Normal file
14
STM32/CM7/Core/Inc/ring_buffer.h
Normal file
@@ -0,0 +1,14 @@
|
||||
// Copyright (C) 2026 Hector van der Aa <hector@h3cx.dev>
|
||||
// Copyright (C) 2026 Association Exergie <association.exergie@gmail.com>
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#include <stdint.h>
|
||||
typedef struct ring_buffer_t {
|
||||
uint32_t buffer[256];
|
||||
uint8_t w_head;
|
||||
uint8_t r_head;
|
||||
} ring_buffer_t;
|
||||
|
||||
inline void ringBufferPush(ring_buffer_t *rb, uint32_t value);
|
||||
|
||||
inline int ringBufferPop(ring_buffer_t *rb, uint32_t *output);
|
||||
@@ -1,20 +1,8 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.c
|
||||
* @brief : Main program body
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2026 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.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
// Copyright (C) 2026 Hector van der Aa <hector@h3cx.dev>
|
||||
// Copyright (C) 2026 Pierre Barbier <pierrebarbier741@gmail.com>
|
||||
// Copyright (C) 2026 Association Exergie <association.exergie@gmail.com>
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
/* USER CODE END Header */
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "main.h"
|
||||
@@ -64,11 +52,11 @@ osThreadId_t defaultTaskHandle;
|
||||
const osThreadAttr_t defaultTask_attributes = {
|
||||
.name = "defaultTask",
|
||||
.stack_size = 128 * 4,
|
||||
.priority = (osPriority_t) osPriorityNormal,
|
||||
.priority = (osPriority_t)osPriorityNormal,
|
||||
};
|
||||
/* Definitions for crankTask */
|
||||
osThreadId_t crankTaskHandle;
|
||||
uint32_t crankTaskBuffer[ 128 ];
|
||||
uint32_t crankTaskBuffer[128];
|
||||
osStaticThreadDef_t crankTaskControlBlock;
|
||||
const osThreadAttr_t crankTask_attributes = {
|
||||
.name = "crankTask",
|
||||
@@ -76,11 +64,11 @@ const osThreadAttr_t crankTask_attributes = {
|
||||
.cb_size = sizeof(crankTaskControlBlock),
|
||||
.stack_mem = &crankTaskBuffer[0],
|
||||
.stack_size = sizeof(crankTaskBuffer),
|
||||
.priority = (osPriority_t) osPriorityRealtime7,
|
||||
.priority = (osPriority_t)osPriorityRealtime7,
|
||||
};
|
||||
/* Definitions for camTask */
|
||||
osThreadId_t camTaskHandle;
|
||||
uint32_t camTaskBuffer[ 128 ];
|
||||
uint32_t camTaskBuffer[128];
|
||||
osStaticThreadDef_t camTaskControlBlock;
|
||||
const osThreadAttr_t camTask_attributes = {
|
||||
.name = "camTask",
|
||||
@@ -88,7 +76,7 @@ const osThreadAttr_t camTask_attributes = {
|
||||
.cb_size = sizeof(camTaskControlBlock),
|
||||
.stack_mem = &camTaskBuffer[0],
|
||||
.stack_size = sizeof(camTaskBuffer),
|
||||
.priority = (osPriority_t) osPriorityRealtime7,
|
||||
.priority = (osPriority_t)osPriorityRealtime7,
|
||||
};
|
||||
/* USER CODE BEGIN PV */
|
||||
|
||||
@@ -112,15 +100,15 @@ void camHandler(void *argument);
|
||||
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim) {
|
||||
if (htim->Instance == TIM2) {
|
||||
switch (htim->Channel) {
|
||||
//HAL_TIM_ACTIVE_CHANNEL_2 is the channel used for cam interupts
|
||||
// HAL_TIM_ACTIVE_CHANNEL_2 is the channel used for cam interupts
|
||||
case HAL_TIM_ACTIVE_CHANNEL_2:
|
||||
// TODO push timestamp to the cam ring buffer
|
||||
osThreadFlagsSet(camTaskHandle,0x01);
|
||||
osThreadFlagsSet(camTaskHandle, 0x01);
|
||||
break;
|
||||
//HAL_TIM_ACTIVE_CHANNEL_1 is the channel used for crank interupts
|
||||
// HAL_TIM_ACTIVE_CHANNEL_1 is the channel used for crank interupts
|
||||
case HAL_TIM_ACTIVE_CHANNEL_1:
|
||||
// TODO push timestamp to the crank ring buffer
|
||||
osThreadFlagsSet(crankTaskHandle,0x01);
|
||||
osThreadFlagsSet(crankTaskHandle, 0x01);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@@ -134,8 +122,7 @@ void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim) {
|
||||
* @brief The application entry point.
|
||||
* @retval int
|
||||
*/
|
||||
int main(void)
|
||||
{
|
||||
int main(void) {
|
||||
|
||||
/* USER CODE BEGIN 1 */
|
||||
|
||||
@@ -144,7 +131,7 @@ int main(void)
|
||||
#if defined(DUAL_CORE_BOOT_SYNC_SEQUENCE)
|
||||
int32_t timeout;
|
||||
#endif /* DUAL_CORE_BOOT_SYNC_SEQUENCE */
|
||||
/* USER CODE END Boot_Mode_Sequence_0 */
|
||||
/* USER CODE END Boot_Mode_Sequence_0 */
|
||||
|
||||
/* MPU Configuration--------------------------------------------------------*/
|
||||
MPU_Config();
|
||||
@@ -159,10 +146,11 @@ int main(void)
|
||||
Error_Handler();
|
||||
}
|
||||
#endif /* DUAL_CORE_BOOT_SYNC_SEQUENCE */
|
||||
/* USER CODE END Boot_Mode_Sequence_1 */
|
||||
/* USER CODE END Boot_Mode_Sequence_1 */
|
||||
/* MCU Configuration--------------------------------------------------------*/
|
||||
|
||||
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
|
||||
/* Reset of all peripherals, Initializes the Flash interface and the Systick.
|
||||
*/
|
||||
HAL_Init();
|
||||
|
||||
/* USER CODE BEGIN Init */
|
||||
@@ -189,7 +177,7 @@ int main(void)
|
||||
Error_Handler();
|
||||
}
|
||||
#endif /* DUAL_CORE_BOOT_SYNC_SEQUENCE */
|
||||
/* USER CODE END Boot_Mode_Sequence_2 */
|
||||
/* USER CODE END Boot_Mode_Sequence_2 */
|
||||
|
||||
/* USER CODE BEGIN SysInit */
|
||||
|
||||
@@ -223,7 +211,8 @@ int main(void)
|
||||
|
||||
/* Create the thread(s) */
|
||||
/* creation of defaultTask */
|
||||
defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);
|
||||
defaultTaskHandle =
|
||||
osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);
|
||||
|
||||
/* creation of crankTask */
|
||||
crankTaskHandle = osThreadNew(crankHandler, NULL, &crankTask_attributes);
|
||||
@@ -258,8 +247,7 @@ int main(void)
|
||||
* @brief System Clock Configuration
|
||||
* @retval None
|
||||
*/
|
||||
void SystemClock_Config(void)
|
||||
{
|
||||
void SystemClock_Config(void) {
|
||||
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
|
||||
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
|
||||
|
||||
@@ -271,7 +259,8 @@ void SystemClock_Config(void)
|
||||
*/
|
||||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE0);
|
||||
|
||||
while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}
|
||||
while (!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {
|
||||
}
|
||||
|
||||
/** Initializes the RCC Oscillators according to the specified parameters
|
||||
* in the RCC_OscInitTypeDef structure.
|
||||
@@ -288,16 +277,15 @@ void SystemClock_Config(void)
|
||||
RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_3;
|
||||
RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE;
|
||||
RCC_OscInitStruct.PLL.PLLFRACN = 0;
|
||||
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
|
||||
{
|
||||
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
/** Initializes the CPU, AHB and APB buses clocks
|
||||
*/
|
||||
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|
||||
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
|
||||
|RCC_CLOCKTYPE_D3PCLK1|RCC_CLOCKTYPE_D1PCLK1;
|
||||
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK |
|
||||
RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2 |
|
||||
RCC_CLOCKTYPE_D3PCLK1 | RCC_CLOCKTYPE_D1PCLK1;
|
||||
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
|
||||
RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
|
||||
RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV2;
|
||||
@@ -306,8 +294,7 @@ void SystemClock_Config(void)
|
||||
RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV2;
|
||||
RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV2;
|
||||
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
|
||||
{
|
||||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) {
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
@@ -317,8 +304,7 @@ void SystemClock_Config(void)
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_TIM2_Init(void)
|
||||
{
|
||||
static void MX_TIM2_Init(void) {
|
||||
|
||||
/* USER CODE BEGIN TIM2_Init 0 */
|
||||
|
||||
@@ -337,41 +323,34 @@ static void MX_TIM2_Init(void)
|
||||
htim2.Init.Period = 4294967295;
|
||||
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV4;
|
||||
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
||||
if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
|
||||
{
|
||||
if (HAL_TIM_Base_Init(&htim2) != HAL_OK) {
|
||||
Error_Handler();
|
||||
}
|
||||
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
|
||||
if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
|
||||
{
|
||||
if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK) {
|
||||
Error_Handler();
|
||||
}
|
||||
if (HAL_TIM_IC_Init(&htim2) != HAL_OK)
|
||||
{
|
||||
if (HAL_TIM_IC_Init(&htim2) != HAL_OK) {
|
||||
Error_Handler();
|
||||
}
|
||||
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
||||
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
||||
if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
|
||||
{
|
||||
if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK) {
|
||||
Error_Handler();
|
||||
}
|
||||
sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING;
|
||||
sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI;
|
||||
sConfigIC.ICPrescaler = TIM_ICPSC_DIV1;
|
||||
sConfigIC.ICFilter = 0;
|
||||
if (HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_1) != HAL_OK)
|
||||
{
|
||||
if (HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_1) != HAL_OK) {
|
||||
Error_Handler();
|
||||
}
|
||||
if (HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_2) != HAL_OK)
|
||||
{
|
||||
if (HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_2) != HAL_OK) {
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN TIM2_Init 2 */
|
||||
|
||||
/* USER CODE END TIM2_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -379,8 +358,7 @@ static void MX_TIM2_Init(void)
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_GPIO_Init(void)
|
||||
{
|
||||
static void MX_GPIO_Init(void) {
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
/* USER CODE BEGIN MX_GPIO_Init_1 */
|
||||
|
||||
@@ -416,8 +394,7 @@ static void MX_GPIO_Init(void)
|
||||
* @retval None
|
||||
*/
|
||||
/* USER CODE END Header_StartDefaultTask */
|
||||
void StartDefaultTask(void *argument)
|
||||
{
|
||||
void StartDefaultTask(void *argument) {
|
||||
/* USER CODE BEGIN 5 */
|
||||
/* Infinite loop */
|
||||
for (;;) {
|
||||
@@ -428,46 +405,41 @@ void StartDefaultTask(void *argument)
|
||||
|
||||
/* USER CODE BEGIN Header_crankHandler */
|
||||
/**
|
||||
* @brief Function implementing the crankTask thread.
|
||||
* @param argument: Not used
|
||||
* @retval None
|
||||
*/
|
||||
* @brief Function implementing the crankTask thread.
|
||||
* @param argument: Not used
|
||||
* @retval None
|
||||
*/
|
||||
/* USER CODE END Header_crankHandler */
|
||||
void crankHandler(void *argument)
|
||||
{
|
||||
void crankHandler(void *argument) {
|
||||
/* USER CODE BEGIN crankHandler */
|
||||
/* Infinite loop */
|
||||
for(;;)
|
||||
{
|
||||
osThreadFlagsWait(0x01,osFlagsWaitAny,osWaitForever);
|
||||
//TODO Handle the call
|
||||
for (;;) {
|
||||
osThreadFlagsWait(0x01, osFlagsWaitAny, osWaitForever);
|
||||
// TODO Handle the call
|
||||
}
|
||||
/* USER CODE END crankHandler */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN Header_camHandler */
|
||||
/**
|
||||
* @brief Function implementing the camTask thread.
|
||||
* @param argument: Not used
|
||||
* @retval None
|
||||
*/
|
||||
* @brief Function implementing the camTask thread.
|
||||
* @param argument: Not used
|
||||
* @retval None
|
||||
*/
|
||||
/* USER CODE END Header_camHandler */
|
||||
void camHandler(void *argument)
|
||||
{
|
||||
void camHandler(void *argument) {
|
||||
/* USER CODE BEGIN camHandler */
|
||||
/* Infinite loop */
|
||||
for(;;)
|
||||
{
|
||||
osThreadFlagsWait(0x01,osFlagsWaitAny,osWaitForever);
|
||||
//TODO Handle the call
|
||||
for (;;) {
|
||||
osThreadFlagsWait(0x01, osFlagsWaitAny, osWaitForever);
|
||||
// TODO Handle the call
|
||||
}
|
||||
/* USER CODE END camHandler */
|
||||
}
|
||||
|
||||
/* MPU Configuration */
|
||||
/* MPU Configuration */
|
||||
|
||||
void MPU_Config(void)
|
||||
{
|
||||
void MPU_Config(void) {
|
||||
MPU_Region_InitTypeDef MPU_InitStruct = {0};
|
||||
|
||||
/* Disables the MPU */
|
||||
@@ -490,15 +462,13 @@ void MPU_Config(void)
|
||||
HAL_MPU_ConfigRegion(&MPU_InitStruct);
|
||||
/* Enables the MPU */
|
||||
HAL_MPU_Enable(MPU_PRIVILEGED_DEFAULT);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function is executed in case of error occurrence.
|
||||
* @retval None
|
||||
*/
|
||||
void Error_Handler(void)
|
||||
{
|
||||
void Error_Handler(void) {
|
||||
/* USER CODE BEGIN Error_Handler_Debug */
|
||||
/* User can add his own implementation to report the HAL error return state */
|
||||
__disable_irq();
|
||||
@@ -514,8 +484,7 @@ void Error_Handler(void)
|
||||
* @param line: assert_param error line source number
|
||||
* @retval None
|
||||
*/
|
||||
void assert_failed(uint8_t *file, uint32_t line)
|
||||
{
|
||||
void assert_failed(uint8_t *file, uint32_t line) {
|
||||
/* USER CODE BEGIN 6 */
|
||||
/* User can add his own implementation to report the file name and line
|
||||
number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file,
|
||||
|
||||
28
STM32/CM7/Core/Src/ring_buffer.c
Normal file
28
STM32/CM7/Core/Src/ring_buffer.c
Normal file
@@ -0,0 +1,28 @@
|
||||
// Copyright (C) 2026 Hector van der Aa <hector@h3cx.dev>
|
||||
// Copyright (C) 2026 Association Exergie <association.exergie@gmail.com>
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#include <assert.h>
|
||||
#include <ring_buffer.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
inline void ringBufferPush(ring_buffer_t *rb, uint32_t value) {
|
||||
rb->buffer[rb->w_head] = value;
|
||||
rb->w_head++;
|
||||
if (rb->w_head == rb->r_head) {
|
||||
// if write head catches up to read head, crash program as consumer task
|
||||
// should keep up with write head
|
||||
assert(false);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
inline int ringBufferPop(ring_buffer_t *rb, uint32_t *output) {
|
||||
if (rb->w_head == rb->r_head) {
|
||||
// if read head is at write head, buffer is empty so nothing to pop
|
||||
return 1;
|
||||
}
|
||||
*output = rb->buffer[rb->r_head];
|
||||
rb->r_head++;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user