-
Notifications
You must be signed in to change notification settings - Fork 3
/
clock_timer.c
53 lines (40 loc) · 1.45 KB
/
clock_timer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "clock_timer.h"
#include <stdlib.h>
#include <furi_hal_interrupt.h>
#include <furi_hal_bus.h>
#include <stm32wbxx_ll_tim.h>
#define FURI_HAL_CLOCK_TIMER TIM2
#define FURI_HAL_CLOCK_TIMER_BUS FuriHalBusTIM2
#define FURI_HAL_CLOCK_TIMER_IRQ FuriHalInterruptIdTIM2
typedef struct {
ClockTimerCallback callback;
void* context;
} ClockTimer;
static ClockTimer clock_timer = {
.callback = NULL,
.context = NULL,
};
static void clock_timer_isr(void* context) {
if(clock_timer.callback) {
clock_timer.callback(context);
}
LL_TIM_ClearFlag_UPDATE(FURI_HAL_CLOCK_TIMER);
}
void clock_timer_start(ClockTimerCallback callback, void* context, float period) {
clock_timer.callback = callback;
clock_timer.context = context;
furi_hal_bus_enable(FURI_HAL_CLOCK_TIMER_BUS);
// init timer to produce interrupts
LL_TIM_InitTypeDef TIM_InitStruct = {0};
TIM_InitStruct.Autoreload = (SystemCoreClock / period) - 1;
LL_TIM_Init(FURI_HAL_CLOCK_TIMER, &TIM_InitStruct);
furi_hal_interrupt_set_isr(FURI_HAL_CLOCK_TIMER_IRQ, clock_timer_isr, clock_timer.context);
LL_TIM_EnableIT_UPDATE(FURI_HAL_CLOCK_TIMER);
LL_TIM_EnableCounter(FURI_HAL_CLOCK_TIMER);
}
void clock_timer_stop(void) {
LL_TIM_DisableIT_UPDATE(FURI_HAL_CLOCK_TIMER);
LL_TIM_DisableCounter(FURI_HAL_CLOCK_TIMER);
furi_hal_bus_disable(FURI_HAL_CLOCK_TIMER_BUS);
furi_hal_interrupt_set_isr(FURI_HAL_CLOCK_TIMER_IRQ, NULL, NULL);
}