steering-wheel/Core/Src/shorttimer.c

30 lines
637 B
C
Raw Permalink Normal View History

2023-03-17 17:50:00 +01:00
#include "shorttimer.h"
#include "stm32h7xx_hal.h"
#include "stm32h7xx_hal_tim.h"
#include <stdint.h>
static TIM_HandleTypeDef *htim;
static uint32_t ticks;
void shorttimer_init(TIM_HandleTypeDef *handle) {
htim = handle;
ticks = 0;
HAL_TIM_Base_Start_IT(htim);
}
uint32_t shorttimer_gettick(void) { return ticks; }
void shorttimer_sleep(uint32_t microseconds) {
volatile uint32_t start = shorttimer_gettick();
// Add another tick to guarantee a minimum wait
if (microseconds < UINT32_MAX) {
microseconds++;
}
while (shorttimer_gettick() - start < microseconds) {
}
}
void shorttimer_callback() { ticks++; }