Implemented "full" ignition timing system
This is a first implementation of a "full" timing system, with proper handling for cases wher the ignition charge time overlaps with the compression pulse, the spark has a more complex state machine system with the option to defer charge from the intake pulse to the compression pulse, and further defferal of the spark release schedule to the ISR that triggers when the output compare triggers for the charge Preliminary error counters have been put in place, they still require handling error conditions that would force a soft shutdown on the ignition IC. Alongside this the code still needs to be verified so that all non critical errors (ie missed charge schedules) reset the state machine and increment a global spark miss counter to avoid having these errors breaking the state machine as they can easily be recovered from Cases where the coil would already be charging and an error occurs require as stated above a full recovery procedure with soft shutdown of the ignition IC and a clean cycle of the state machine to ensure a full recovery
This commit is contained in:
15 files changed
+444
-195
No files matched your search
+123
-108
@@ -16,6 +16,7 @@
|
||||
#include "ring_buffer.h"
|
||||
#include "stm32h7xx_hal_tim.h"
|
||||
#include "tasks.h"
|
||||
#include "timer_utils.h"
|
||||
#include <assert.h>
|
||||
#include <stdbool.h>
|
||||
#ifdef DEBUG
|
||||
@@ -58,33 +59,33 @@ TIM_HandleTypeDef htim2;
|
||||
/* Definitions for defaultTask */
|
||||
osThreadId_t defaultTaskHandle;
|
||||
const osThreadAttr_t defaultTask_attributes = {
|
||||
.name = "defaultTask",
|
||||
.stack_size = 128 * 4,
|
||||
.priority = (osPriority_t) osPriorityNormal,
|
||||
.name = "defaultTask",
|
||||
.stack_size = 128 * 4,
|
||||
.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",
|
||||
.cb_mem = &crankTaskControlBlock,
|
||||
.cb_size = sizeof(crankTaskControlBlock),
|
||||
.stack_mem = &crankTaskBuffer[0],
|
||||
.stack_size = sizeof(crankTaskBuffer),
|
||||
.priority = (osPriority_t) osPriorityRealtime7,
|
||||
.name = "crankTask",
|
||||
.cb_mem = &crankTaskControlBlock,
|
||||
.cb_size = sizeof(crankTaskControlBlock),
|
||||
.stack_mem = &crankTaskBuffer[0],
|
||||
.stack_size = sizeof(crankTaskBuffer),
|
||||
.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",
|
||||
.cb_mem = &camTaskControlBlock,
|
||||
.cb_size = sizeof(camTaskControlBlock),
|
||||
.stack_mem = &camTaskBuffer[0],
|
||||
.stack_size = sizeof(camTaskBuffer),
|
||||
.priority = (osPriority_t) osPriorityRealtime7,
|
||||
.name = "camTask",
|
||||
.cb_mem = &camTaskControlBlock,
|
||||
.cb_size = sizeof(camTaskControlBlock),
|
||||
.stack_mem = &camTaskBuffer[0],
|
||||
.stack_size = sizeof(camTaskBuffer),
|
||||
.priority = (osPriority_t)osPriorityRealtime7,
|
||||
};
|
||||
/* USER CODE BEGIN PV */
|
||||
volatile global_state_t state_g = {0};
|
||||
@@ -108,18 +109,22 @@ extern void camHandler(void *argument);
|
||||
|
||||
void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim) {
|
||||
if (htim->Instance == TIM2 && htim->Channel == HAL_TIM_ACTIVE_CHANNEL_3) {
|
||||
if (state_g.next_spark_state == SPARK_IDLE &&
|
||||
state_g.current_spark_state == SPARK_CHARGING) {
|
||||
DEBUG_LOG("Releasing spark");
|
||||
} else if (state_g.next_spark_state == SPARK_CHARGING &&
|
||||
state_g.current_spark_state == SPARK_IDLE) {
|
||||
DEBUG_LOG("Charging spark");
|
||||
osThreadFlagsSet(crankTaskHandle, 0x02);
|
||||
}
|
||||
|
||||
if (state_g.next_spark_state != SPARK_NONE) {
|
||||
state_g.current_spark_state = state_g.next_spark_state;
|
||||
state_g.next_spark_state = SPARK_NONE;
|
||||
if (state_g.current_spark_state == SPARK_PENDING_CHARGE) {
|
||||
state_g.current_spark_state = SPARK_CHARGING;
|
||||
if (state_g.spark_defer) {
|
||||
if (timer_safe_to_arm(state_g.t_defer_spark, &htim2)) {
|
||||
TIM2_CH3_SetOCMode(TIM2_CH3_OC_INACTIVE);
|
||||
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_3, state_g.t_defer_spark);
|
||||
state_g.spark_defer = false;
|
||||
state_g.current_spark_state = SPARK_PENDING_RELEASE;
|
||||
} else {
|
||||
// TODO: Implement SSD on ignition IC if charging and complete
|
||||
// recovery
|
||||
state_g.spark_errors.isr_release_error_ctr++;
|
||||
}
|
||||
}
|
||||
} else if (state_g.current_spark_state == SPARK_PENDING_RELEASE) {
|
||||
state_g.current_spark_state = SPARK_IDLE;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -148,11 +153,10 @@ void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim) {
|
||||
/* USER CODE END 0 */
|
||||
|
||||
/**
|
||||
* @brief The application entry point.
|
||||
* @retval int
|
||||
*/
|
||||
int main(void)
|
||||
{
|
||||
* @brief The application entry point.
|
||||
* @retval int
|
||||
*/
|
||||
int main(void) {
|
||||
|
||||
/* USER CODE BEGIN 1 */
|
||||
|
||||
@@ -161,7 +165,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();
|
||||
@@ -176,10 +180,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,6 +194,13 @@ int main(void)
|
||||
state_g.cam_miss_ctr = 0;
|
||||
state_g.next_spark_state = SPARK_NONE;
|
||||
state_g.current_spark_state = SPARK_IDLE;
|
||||
state_g.charge_defer = false;
|
||||
state_g.spark_errors.overspeed_error_ctr = 0;
|
||||
state_g.spark_errors.defer_charge_error_ctr = 0;
|
||||
state_g.spark_errors.revised_charge_error_ctr = 0;
|
||||
state_g.spark_errors.task_release_error_ctr = 0;
|
||||
state_g.spark_errors.state_error_ctr = 0;
|
||||
state_g.spark_errors.isr_release_error_ctr = 0;
|
||||
/* USER CODE END Init */
|
||||
|
||||
/* Configure the system clock */
|
||||
@@ -211,7 +223,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 */
|
||||
|
||||
@@ -251,7 +263,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);
|
||||
@@ -283,27 +296,27 @@ int main(void)
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief System Clock Configuration
|
||||
* @retval None
|
||||
*/
|
||||
void SystemClock_Config(void)
|
||||
{
|
||||
* @brief System Clock Configuration
|
||||
* @retval None
|
||||
*/
|
||||
void SystemClock_Config(void) {
|
||||
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
|
||||
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
|
||||
|
||||
/** Supply configuration update enable
|
||||
*/
|
||||
*/
|
||||
HAL_PWREx_ConfigSupply(PWR_LDO_SUPPLY);
|
||||
|
||||
/** Configure the main internal regulator output voltage
|
||||
*/
|
||||
*/
|
||||
__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.
|
||||
*/
|
||||
* in the RCC_OscInitTypeDef structure.
|
||||
*/
|
||||
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
|
||||
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
|
||||
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
|
||||
@@ -316,16 +329,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;
|
||||
@@ -334,19 +346,17 @@ 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();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief TIM2 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_TIM2_Init(void)
|
||||
{
|
||||
* @brief TIM2 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_TIM2_Init(void) {
|
||||
|
||||
/* USER CODE BEGIN TIM2_Init 0 */
|
||||
|
||||
@@ -361,53 +371,45 @@ static void MX_TIM2_Init(void)
|
||||
|
||||
/* USER CODE END TIM2_Init 1 */
|
||||
htim2.Instance = TIM2;
|
||||
htim2.Init.Prescaler = 0;
|
||||
htim2.Init.Prescaler = 99;
|
||||
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||
htim2.Init.Period = 4294967295;
|
||||
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV4;
|
||||
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
||||
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();
|
||||
}
|
||||
if (HAL_TIM_OC_Init(&htim2) != HAL_OK)
|
||||
{
|
||||
if (HAL_TIM_OC_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_FALLING;
|
||||
sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI;
|
||||
sConfigIC.ICPrescaler = TIM_ICPSC_DIV1;
|
||||
sConfigIC.ICFilter = 10;
|
||||
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();
|
||||
}
|
||||
sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING;
|
||||
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();
|
||||
}
|
||||
sConfigOC.OCMode = TIM_OCMODE_TOGGLE;
|
||||
sConfigOC.Pulse = 0;
|
||||
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
|
||||
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
|
||||
if (HAL_TIM_OC_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
|
||||
{
|
||||
if (HAL_TIM_OC_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_3) != HAL_OK) {
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN TIM2_Init 2 */
|
||||
@@ -416,16 +418,14 @@ static void MX_TIM2_Init(void)
|
||||
HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_3);
|
||||
/* USER CODE END TIM2_Init 2 */
|
||||
HAL_TIM_MspPostInit(&htim2);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief GPIO Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_GPIO_Init(void)
|
||||
{
|
||||
* @brief GPIO Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_GPIO_Init(void) {
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
/* USER CODE BEGIN MX_GPIO_Init_1 */
|
||||
|
||||
@@ -461,8 +461,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 (;;) {
|
||||
@@ -471,17 +470,16 @@ void StartDefaultTask(void *argument)
|
||||
/* USER CODE END 5 */
|
||||
}
|
||||
|
||||
/* MPU Configuration */
|
||||
/* MPU Configuration */
|
||||
|
||||
void MPU_Config(void)
|
||||
{
|
||||
void MPU_Config(void) {
|
||||
MPU_Region_InitTypeDef MPU_InitStruct = {0};
|
||||
|
||||
/* Disables the MPU */
|
||||
HAL_MPU_Disable();
|
||||
|
||||
/** Initializes and configures the Region and the memory to be protected
|
||||
*/
|
||||
*/
|
||||
MPU_InitStruct.Enable = MPU_REGION_ENABLE;
|
||||
MPU_InitStruct.Number = MPU_REGION_NUMBER0;
|
||||
MPU_InitStruct.BaseAddress = 0x0;
|
||||
@@ -497,15 +495,33 @@ 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)
|
||||
{
|
||||
* @brief Period elapsed callback in non blocking mode
|
||||
* @note This function is called when TIM6 interrupt took place, inside
|
||||
* HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
|
||||
* a global variable "uwTick" used as application time base.
|
||||
* @param htim : TIM handle
|
||||
* @retval None
|
||||
*/
|
||||
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
|
||||
/* USER CODE BEGIN Callback 0 */
|
||||
|
||||
/* USER CODE END Callback 0 */
|
||||
if (htim->Instance == TIM6) {
|
||||
HAL_IncTick();
|
||||
}
|
||||
/* USER CODE BEGIN Callback 1 */
|
||||
|
||||
/* USER CODE END Callback 1 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function is executed in case of error occurrence.
|
||||
* @retval None
|
||||
*/
|
||||
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();
|
||||
@@ -515,14 +531,13 @@ void Error_Handler(void)
|
||||
}
|
||||
#ifdef USE_FULL_ASSERT
|
||||
/**
|
||||
* @brief Reports the name of the source file and the source line number
|
||||
* where the assert_param error has occurred.
|
||||
* @param file: pointer to the source file name
|
||||
* @param line: assert_param error line source number
|
||||
* @retval None
|
||||
*/
|
||||
void assert_failed(uint8_t *file, uint32_t line)
|
||||
{
|
||||
* @brief Reports the name of the source file and the source line number
|
||||
* where the assert_param error has occurred.
|
||||
* @param file: pointer to the source file name
|
||||
* @param line: assert_param error line source number
|
||||
* @retval None
|
||||
*/
|
||||
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,
|
||||
|
||||
@@ -0,0 +1,131 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file stm32h7xx_hal_timebase_tim.c
|
||||
* @brief HAL time base based on the hardware TIM.
|
||||
******************************************************************************
|
||||
* @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.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "stm32h7xx_hal.h"
|
||||
#include "stm32h7xx_hal_tim.h"
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
/* Private define ------------------------------------------------------------*/
|
||||
/* Private macro -------------------------------------------------------------*/
|
||||
/* Private variables ---------------------------------------------------------*/
|
||||
TIM_HandleTypeDef htim6;
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
/* Private functions ---------------------------------------------------------*/
|
||||
|
||||
/**
|
||||
* @brief This function configures the TIM6 as a time base source.
|
||||
* The time source is configured to have 1ms time base with a dedicated
|
||||
* Tick interrupt priority.
|
||||
* @note This function is called automatically at the beginning of program after
|
||||
* reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig().
|
||||
* @param TickPriority: Tick interrupt priority.
|
||||
* @retval HAL status
|
||||
*/
|
||||
HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
|
||||
{
|
||||
RCC_ClkInitTypeDef clkconfig;
|
||||
uint32_t uwTimclock, uwAPB1Prescaler;
|
||||
uint32_t uwPrescalerValue;
|
||||
uint32_t pFLatency;
|
||||
|
||||
/*Configure the TIM6 IRQ priority */
|
||||
if (TickPriority < (1UL << __NVIC_PRIO_BITS))
|
||||
{
|
||||
HAL_NVIC_SetPriority(TIM6_DAC_IRQn, TickPriority ,0);
|
||||
|
||||
/* Enable the TIM6 global Interrupt */
|
||||
HAL_NVIC_EnableIRQ(TIM6_DAC_IRQn);
|
||||
uwTickPrio = TickPriority;
|
||||
}
|
||||
else
|
||||
{
|
||||
return HAL_ERROR;
|
||||
}
|
||||
|
||||
/* Enable TIM6 clock */
|
||||
__HAL_RCC_TIM6_CLK_ENABLE();
|
||||
|
||||
/* Get clock configuration */
|
||||
HAL_RCC_GetClockConfig(&clkconfig, &pFLatency);
|
||||
|
||||
/* Get APB1 prescaler */
|
||||
uwAPB1Prescaler = clkconfig.APB1CLKDivider;
|
||||
/* Compute TIM6 clock */
|
||||
if (uwAPB1Prescaler == RCC_HCLK_DIV1)
|
||||
{
|
||||
uwTimclock = HAL_RCC_GetPCLK1Freq();
|
||||
}
|
||||
else
|
||||
{
|
||||
uwTimclock = 2UL * HAL_RCC_GetPCLK1Freq();
|
||||
}
|
||||
|
||||
/* Compute the prescaler value to have TIM6 counter clock equal to 1MHz */
|
||||
uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000U) - 1U);
|
||||
|
||||
/* Initialize TIM6 */
|
||||
htim6.Instance = TIM6;
|
||||
|
||||
/* Initialize TIMx peripheral as follow:
|
||||
* Period = [(TIM6CLK/1000) - 1]. to have a (1/1000) s time base.
|
||||
* Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock.
|
||||
* ClockDivision = 0
|
||||
* Counter direction = Up
|
||||
*/
|
||||
htim6.Init.Period = (1000000U / 1000U) - 1U;
|
||||
htim6.Init.Prescaler = uwPrescalerValue;
|
||||
htim6.Init.ClockDivision = 0;
|
||||
htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||
|
||||
if(HAL_TIM_Base_Init(&htim6) == HAL_OK)
|
||||
{
|
||||
|
||||
/* Start the TIM time Base generation in interrupt mode */
|
||||
return HAL_TIM_Base_Start_IT(&htim6);
|
||||
}
|
||||
|
||||
/* Return function status */
|
||||
return HAL_ERROR;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Suspend Tick increment.
|
||||
* @note Disable the tick increment by disabling TIM6 update interrupt.
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
void HAL_SuspendTick(void)
|
||||
{
|
||||
/* Disable TIM6 update Interrupt */
|
||||
__HAL_TIM_DISABLE_IT(&htim6, TIM_IT_UPDATE);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Resume Tick increment.
|
||||
* @note Enable the tick increment by Enabling TIM6 update interrupt.
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
void HAL_ResumeTick(void)
|
||||
{
|
||||
/* Enable TIM6 Update interrupt */
|
||||
__HAL_TIM_ENABLE_IT(&htim6, TIM_IT_UPDATE);
|
||||
}
|
||||
|
||||
@@ -20,8 +20,6 @@
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "main.h"
|
||||
#include "stm32h7xx_it.h"
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
/* USER CODE END Includes */
|
||||
@@ -58,6 +56,8 @@
|
||||
|
||||
/* External variables --------------------------------------------------------*/
|
||||
extern TIM_HandleTypeDef htim2;
|
||||
extern TIM_HandleTypeDef htim6;
|
||||
|
||||
/* USER CODE BEGIN EV */
|
||||
|
||||
/* USER CODE END EV */
|
||||
@@ -153,28 +153,6 @@ void DebugMon_Handler(void)
|
||||
/* USER CODE END DebugMonitor_IRQn 1 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles System tick timer.
|
||||
*/
|
||||
void SysTick_Handler(void)
|
||||
{
|
||||
/* USER CODE BEGIN SysTick_IRQn 0 */
|
||||
|
||||
/* USER CODE END SysTick_IRQn 0 */
|
||||
HAL_IncTick();
|
||||
#if (INCLUDE_xTaskGetSchedulerState == 1 )
|
||||
if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED)
|
||||
{
|
||||
#endif /* INCLUDE_xTaskGetSchedulerState */
|
||||
xPortSysTickHandler();
|
||||
#if (INCLUDE_xTaskGetSchedulerState == 1 )
|
||||
}
|
||||
#endif /* INCLUDE_xTaskGetSchedulerState */
|
||||
/* USER CODE BEGIN SysTick_IRQn 1 */
|
||||
|
||||
/* USER CODE END SysTick_IRQn 1 */
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/* STM32H7xx Peripheral Interrupt Handlers */
|
||||
/* Add here the Interrupt Handlers for the used peripherals. */
|
||||
@@ -196,6 +174,20 @@ void TIM2_IRQHandler(void)
|
||||
/* USER CODE END TIM2_IRQn 1 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles TIM6 global interrupt, DAC1_CH1 and DAC1_CH2 underrun error interrupts.
|
||||
*/
|
||||
void TIM6_DAC_IRQHandler(void)
|
||||
{
|
||||
/* USER CODE BEGIN TIM6_DAC_IRQn 0 */
|
||||
|
||||
/* USER CODE END TIM6_DAC_IRQn 0 */
|
||||
HAL_TIM_IRQHandler(&htim6);
|
||||
/* USER CODE BEGIN TIM6_DAC_IRQn 1 */
|
||||
|
||||
/* USER CODE END TIM6_DAC_IRQn 1 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 1 */
|
||||
|
||||
/* USER CODE END 1 */
|
||||
@@ -8,36 +8,42 @@
|
||||
#include "macros.h"
|
||||
#include "main.h"
|
||||
#include "ring_buffer.h"
|
||||
#include "stm32h747xx.h"
|
||||
#include "stm32h7xx.h"
|
||||
#include "stm32h7xx_hal_tim.h"
|
||||
#include "tasks.h"
|
||||
#include "timer_utils.h"
|
||||
#include <stdint.h>
|
||||
|
||||
static inline void TIM2_CH3_SetOCMode(uint32_t oc_mode) {
|
||||
MODIFY_REG(TIM2->CCMR2, TIM_CCMR2_OC3M | TIM_CCMR2_OC3M_3, oc_mode);
|
||||
}
|
||||
|
||||
void crankHandler(void *argument) {
|
||||
for (;;) {
|
||||
osThreadFlagsWait(0x01, osFlagsWaitAny, osWaitForever);
|
||||
DEBUG_LOG("Crank pulse detected at: %lu\n\r",
|
||||
ringBufferRead(&state_g.crank_RB, 0));
|
||||
// FILTER
|
||||
|
||||
/* WINDOW FILTER
|
||||
*
|
||||
* This filter limits incoming signals to a certain max percentage deviation
|
||||
* to blank out false triggers that would be impossible in real life
|
||||
*
|
||||
* It computes the percantage difference between d1a and d1b then if this
|
||||
* has an absolute value over a certain threshold the signal is discarded
|
||||
*/
|
||||
if (state_g.sync_state == SYNC_OK) {
|
||||
uint32_t d1a = CRANK(0) - CRANK(1);
|
||||
uint32_t d1b = CRANK(1) - CRANK(2);
|
||||
|
||||
int32_t delta_percentage =
|
||||
((int64_t)d1a - (int64_t)d1b) * 100 / (int64_t)d1b;
|
||||
|
||||
DEBUG_LOG("%ld\n\r", delta_percentage);
|
||||
|
||||
if (delta_percentage > 40 || delta_percentage < -40) {
|
||||
// state_g.sync_state = SYNC_NOT_OK;
|
||||
// state_g.crank_state = CYCLE_UNKNOWN;
|
||||
// ringBufferRevert(&state_g.crank_RB, 1);
|
||||
state_g.crank_RB.w_head -= 1;
|
||||
// Updated to use clean API
|
||||
// state_g.crank_RB.w_head -= 1;
|
||||
ringBufferRevert(&state_g.crank_RB, 1);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// INCREMENT SWITCH
|
||||
switch (state_g.crank_state) {
|
||||
case CYCLE_COMPRESSION: {
|
||||
@@ -81,31 +87,83 @@ void crankHandler(void *argument) {
|
||||
// SPARK SCHEDULE
|
||||
if (state_g.sync_state == SYNC_OK) {
|
||||
switch (state_g.crank_state) {
|
||||
case CYCLE_INTAKE:
|
||||
case CYCLE_COMPRESSION: {
|
||||
DEBUG_LOG("Spark schedule reached, congrats\n\r");
|
||||
uint32_t d1a = CRANK(0) - CRANK(1);
|
||||
// TODO: Map interpolation rather than SPARK_ADVANCE CONSTANT
|
||||
uint32_t d_spark = d1a * (45 - SPARK_ADVANCE) / 180;
|
||||
uint32_t t_spark = CRANK(0) + d_spark;
|
||||
// TODO: schedule spark
|
||||
if (state_g.current_spark_state != SPARK_CHARGING) {
|
||||
uint32_t force_charge_time = __HAL_TIM_GET_COUNTER(&htim2) + 2000;
|
||||
// TIM2_CH3_SetOCMode(TIM_OCMODE_ACTIVE);
|
||||
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_3, force_charge_time);
|
||||
|
||||
state_g.next_spark_state = SPARK_CHARGING;
|
||||
osThreadFlagsWait(0x02, osFlagsWaitAny, osWaitForever);
|
||||
uint32_t d_spark = 0;
|
||||
if (state_g.crank_state == CYCLE_INTAKE) {
|
||||
d_spark += d1a;
|
||||
}
|
||||
DEBUG_LOG("Spark scheduled for: %lu , current time: %lu\n\r", t_spark,
|
||||
__HAL_TIM_GET_COUNTER(&htim2));
|
||||
// TIM2_CH3_SetOCMode(TIM_OCMODE_INACTIVE);
|
||||
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_3, t_spark);
|
||||
state_g.next_spark_state = SPARK_IDLE;
|
||||
d_spark += d1a * (45 - SPARK_ADVANCE) / 180;
|
||||
uint32_t t_spark = CRANK(0) + d_spark;
|
||||
uint32_t t_spark_charge = t_spark - SPARK_CHARGE_TIME;
|
||||
if (state_g.crank_state == CYCLE_INTAKE) {
|
||||
if (t_spark_charge > CRANK(0) + 500 + ((d1a * 102U) / 100U)) {
|
||||
state_g.charge_defer = true;
|
||||
} else {
|
||||
state_g.charge_defer = false;
|
||||
if (timer_safe_to_arm(t_spark_charge, &htim2)) {
|
||||
state_g.current_spark_state = SPARK_PENDING_CHARGE;
|
||||
TIM2_CH3_SetOCMode(TIM2_CH3_OC_ACTIVE);
|
||||
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_3, t_spark_charge);
|
||||
} else {
|
||||
state_g.current_spark_state = SPARK_IDLE;
|
||||
state_g.spark_errors.overspeed_error_ctr++;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (state_g.charge_defer) {
|
||||
state_g.spark_defer = true;
|
||||
state_g.t_defer_spark = t_spark;
|
||||
state_g.charge_defer = false;
|
||||
if (timer_safe_to_arm(t_spark_charge, &htim2)) {
|
||||
state_g.current_spark_state = SPARK_PENDING_CHARGE;
|
||||
TIM2_CH3_SetOCMode(TIM2_CH3_OC_ACTIVE);
|
||||
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_3, t_spark_charge);
|
||||
} else {
|
||||
state_g.spark_errors.defer_charge_error_ctr++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
switch (state_g.current_spark_state) {
|
||||
case SPARK_PENDING_CHARGE: {
|
||||
if (timer_safe_to_arm(t_spark_charge, &htim2)) {
|
||||
state_g.current_spark_state = SPARK_PENDING_CHARGE;
|
||||
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_3, t_spark_charge);
|
||||
} else {
|
||||
state_g.spark_errors.revised_charge_error_ctr++;
|
||||
}
|
||||
state_g.spark_defer = true;
|
||||
state_g.t_defer_spark = t_spark;
|
||||
break;
|
||||
}
|
||||
case SPARK_CHARGING: {
|
||||
if (timer_safe_to_arm(t_spark, &htim2)) {
|
||||
|
||||
state_g.current_spark_state = SPARK_PENDING_RELEASE;
|
||||
TIM2_CH3_SetOCMode(TIM2_CH3_OC_INACTIVE);
|
||||
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_3, t_spark);
|
||||
state_g.spark_defer = false;
|
||||
} else {
|
||||
// TODO: Implement SSD on ignition IC if charging and complete
|
||||
// recovery
|
||||
state_g.spark_errors.task_release_error_ctr++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
state_g.spark_errors.state_error_ctr++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case CYCLE_EXHAUST: {
|
||||
uint32_t t_injection =
|
||||
CRANK(0) + (45 + INJECTION_PHASE) * (CRANK(4) - CRANK(0)) / 720;
|
||||
// TODO: Schedule injection
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
// 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 "timer_utils.h"
|
||||
#include "stm32h7xx.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
void TIM2_CH3_SetOCMode(uint32_t oc_mode) {
|
||||
MODIFY_REG(TIM2->CCMR2, TIM_CCMR2_OC3M | TIM_CCMR2_OC3M_3, oc_mode);
|
||||
}
|
||||
|
||||
bool timer_safe_to_arm(uint32_t deadline, TIM_HandleTypeDef *tim) {
|
||||
uint32_t time_left = deadline - __HAL_TIM_GET_COUNTER(tim);
|
||||
return time_left > 100U && time_left < 0x80000000U;
|
||||
}
|
||||
Reference in new issue
Block a user