Limit main loop frequency

This commit is contained in:
Jasper Blanckenburg 2022-08-17 08:46:40 +02:00 committed by jazzpi
parent 38590d433b
commit 9fa034f10f
2 changed files with 11 additions and 2 deletions

View File

@ -41,7 +41,7 @@ extern "C" {
/* Exported constants --------------------------------------------------------*/
/* USER CODE BEGIN EC */
#define MAIN_LOOP_PERIOD 50 /* ms */
/* USER CODE END EC */
/* Exported macro ------------------------------------------------------------*/

View File

@ -29,6 +29,7 @@
#include "stm32g4xx_hal.h"
#include "stm32g4xx_hal_gpio.h"
#include "stm32g4xx_hal_tim.h"
#include <stdint.h>
@ -74,7 +75,14 @@ static void MX_TIM2_Init(void);
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void loop_delay() {
static uint32_t last_loop = 0;
int32_t diff = MAIN_LOOP_PERIOD - (HAL_GetTick() - last_loop);
if (diff > 0) {
HAL_Delay(diff);
}
last_loop = HAL_GetTick();
}
/* USER CODE END 0 */
/**
@ -134,6 +142,7 @@ int main(void) {
// vehicle_send_mission_select(MISSION_ACCELERATION);
last_can = now;
}
loop_delay();
}
/* USER CODE END 3 */
}