From f5e26aad24f9a456e55c153033e19d5f7fb8a3bd Mon Sep 17 00:00:00 2001 From: "Jasper v. Blanckenburg" Date: Sat, 15 Apr 2023 22:47:16 +0200 Subject: [PATCH] Run main loop every 50ms --- Core/Src/main.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Core/Src/main.c b/Core/Src/main.c index 3026162..6178e7f 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -70,7 +70,19 @@ static void MX_USART1_UART_Init(void); /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ +#define MAIN_LOOP_PERIOD 50 +static void loop_delay() { + static uint32_t last_loop = 0; + uint32_t dt = HAL_GetTick() - last_loop; + if (dt < MAIN_LOOP_PERIOD) { + HAL_Delay(MAIN_LOOP_PERIOD - dt); + HAL_GPIO_WritePin(STATUS2_GPIO_Port, STATUS2_Pin, GPIO_PIN_RESET); + } else { + HAL_GPIO_WritePin(STATUS2_GPIO_Port, STATUS2_Pin, GPIO_PIN_SET); + } + last_loop = HAL_GetTick(); +} /* USER CODE END 0 */ /** @@ -127,7 +139,7 @@ int main(void) { soc_update(); can_send_status(); - HAL_Delay(10); + loop_delay(); } /* USER CODE END 3 */ }