Added print on interrupt via task flag

This commit is contained in:
2026-06-02 19:46:05 +02:00
parent 0e99868078
commit 8d1debd69a
4 changed files with 46 additions and 27 deletions

View File

@@ -59,10 +59,10 @@ void Error_Handler(void);
/* Private defines -----------------------------------------------------------*/
#define LED_RED_Pin GPIO_PIN_12
#define LED_RED_GPIO_Port GPIOI
#define TIM2_CRANK_Pin GPIO_PIN_0
#define TIM2_CRANK_GPIO_Port GPIOA
#define TIM2_CAM_Pin GPIO_PIN_1
#define TIM2_CAM_GPIO_Port GPIOA
#define TIM2_CRANK_Pin GPIO_PIN_0
#define TIM2_CRANK_GPIO_Port GPIOA
/* USER CODE BEGIN Private defines */

View File

@@ -8,12 +8,14 @@
#include "main.h"
#include "FreeRTOS.h"
#include "cmsis_os2.h"
#include "stm32h7xx_hal_tim.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "global_state.h"
#include "ring_buffer.h"
#ifdef DEBUG
#include "SEGGER_RTT.h"
#endif
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
@@ -80,8 +82,7 @@ const osThreadAttr_t camTask_attributes = {
.priority = (osPriority_t)osPriorityRealtime7,
};
/* USER CODE BEGIN PV */
global_state_t state_g ={0};
static volatile global_state_t state_g = {0};
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
@@ -104,12 +105,14 @@ void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim) {
switch (htim->Channel) {
// HAL_TIM_ACTIVE_CHANNEL_2 is the channel used for cam interupts
case HAL_TIM_ACTIVE_CHANNEL_2:
ringBufferPush(&state_g.cam_RB,HAL_TIM_ReadCapturedValue(htim,HAL_TIM_ACTIVE_CHANNEL_2));
ringBufferPush(&state_g.cam_RB,
HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2));
osThreadFlagsSet(camTaskHandle, 0x01);
break;
// HAL_TIM_ACTIVE_CHANNEL_1 is the channel used for crank interupts
case HAL_TIM_ACTIVE_CHANNEL_1:
ringBufferPush(&state_g.crank_RB,HAL_TIM_ReadCapturedValue(htim,HAL_TIM_ACTIVE_CHANNEL_1));
ringBufferPush(&state_g.crank_RB,
HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1));
osThreadFlagsSet(crankTaskHandle, 0x01);
break;
default:
@@ -156,7 +159,7 @@ int main(void) {
HAL_Init();
/* USER CODE BEGIN Init */
state_g.sync=SYNC_NOT_OK;
state_g.sync = SYNC_NOT_OK;
/* USER CODE END Init */
/* Configure the system clock */
@@ -190,6 +193,12 @@ int main(void) {
MX_TIM2_Init();
/* USER CODE BEGIN 2 */
#ifdef DEBUG
void SEGGER_RTT_Init(void);
SEGGER_RTT_ConfigUpBuffer(0, NULL, NULL, 0,
SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL);
#endif
/* USER CODE END 2 */
/* Init scheduler */
@@ -340,18 +349,20 @@ static void MX_TIM2_Init(void) {
if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK) {
Error_Handler();
}
sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING;
sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_FALLING;
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) {
Error_Handler();
}
sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING;
if (HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_2) != HAL_OK) {
Error_Handler();
}
/* USER CODE BEGIN TIM2_Init 2 */
HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_1);
HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_2);
/* USER CODE END TIM2_Init 2 */
}
@@ -400,7 +411,7 @@ void StartDefaultTask(void *argument) {
/* USER CODE BEGIN 5 */
/* Infinite loop */
for (;;) {
osDelay(1);
osDelay(1000);
}
/* USER CODE END 5 */
}
@@ -417,9 +428,12 @@ void crankHandler(void *argument) {
/* Infinite loop */
for (;;) {
osThreadFlagsWait(0x01, osFlagsWaitAny, osWaitForever);
#ifdef DEBUG
SEGGER_RTT_printf(0, "Crank pulse detected at: %lu\n\r",
ringBufferRead(&state_g.crank_RB, 0));
#endif /* ifdef DEBUG */
if (state_g.sync == SYNC_OK) {
//TODO complete algorithm for scheduling spark
// TODO complete algorithm for scheduling spark
}
}
/* USER CODE END crankHandler */
@@ -437,9 +451,12 @@ void camHandler(void *argument) {
/* Infinite loop */
for (;;) {
osThreadFlagsWait(0x01, osFlagsWaitAny, osWaitForever);
#ifdef DEBUG
SEGGER_RTT_printf(0, "Cam pulse detected at: %lu\n\r",
ringBufferRead(&state_g.cam_RB, 0));
#endif /* ifdef DEBUG */
if (state_g.sync == SYNC_OK) {
//TODO complete algorithm for scheduling spark
// TODO complete algorithm for scheduling spark
}
}
/* USER CODE END camHandler */

View File

@@ -96,10 +96,10 @@ void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base)
__HAL_RCC_GPIOA_CLK_ENABLE();
/**TIM2 GPIO Configuration
PA1 ------> TIM2_CH2
PA0 ------> TIM2_CH1
PA1_C ------> TIM2_CH2
*/
GPIO_InitStruct.Pin = TIM2_CRANK_Pin|TIM2_CAM_Pin;
GPIO_InitStruct.Pin = TIM2_CAM_Pin|TIM2_CRANK_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
@@ -134,10 +134,10 @@ void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* htim_base)
__HAL_RCC_TIM2_CLK_DISABLE();
/**TIM2 GPIO Configuration
PA1 ------> TIM2_CH2
PA0 ------> TIM2_CH1
PA1_C ------> TIM2_CH2
*/
HAL_GPIO_DeInit(GPIOA, TIM2_CRANK_Pin|TIM2_CAM_Pin);
HAL_GPIO_DeInit(GPIOA, TIM2_CAM_Pin|TIM2_CRANK_Pin);
/* TIM2 interrupt DeInit */
HAL_NVIC_DisableIRQ(TIM2_IRQn);