Firmware SDK
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
bc_timer.c
1 #include <bc_timer.h>
2 
3 typedef struct bc_timer_irq_t
4 {
5  void (*irq_handler)(void *);
6  void *irq_param;
8 
9 bc_timer_irq_t bc_timer_tim2_irq;
10 bc_timer_irq_t bc_timer_tim3_irq;
11 bc_timer_irq_t bc_timer_tim6_irq;
12 
13 const uint16_t _bc_timer_prescaler_lut[3] =
14 {
15  2,
16  15,
17  31,
18 };
19 
20 inline void bc_timer_init(void)
21 {
22  // Enable clock for TIM22
23  RCC->APB2ENR |= RCC_APB2ENR_TIM22EN;
24 }
25 
26 inline void bc_timer_start(void)
27 {
28  TIM22->PSC = _bc_timer_prescaler_lut[bc_system_clock_get()]; // 7 instructions
29 
30  TIM22->CNT = 0;
31 
32  TIM22->EGR = TIM_EGR_UG;
33 
34  TIM22->CR1 |= TIM_CR1_CEN;
35 }
36 
37 inline uint16_t bc_timer_get_microseconds(void)
38 {
39  return TIM22->CNT;
40 }
41 
42 inline void bc_timer_delay(uint16_t microseconds)
43 {
44  uint16_t t = bc_timer_get_microseconds() + microseconds;
45 
46  while (bc_timer_get_microseconds() < t)
47  {
48  continue;
49  }
50 }
51 
52 inline void bc_timer_clear(void)
53 {
54  TIM22->CNT = 0;
55 }
56 
57 inline void bc_timer_stop(void)
58 {
59  TIM22->CR1 &= ~TIM_CR1_CEN;
60 }
61 
62 void bc_timer_clear_irq_handler(TIM_TypeDef *tim)
63 {
64  if (tim == TIM3)
65  {
66  bc_timer_tim3_irq.irq_handler = NULL;
67  }
68  if (tim == TIM6)
69  {
70  bc_timer_tim6_irq.irq_handler = NULL;
71  }
72 }
73 
74 bool bc_timer_set_irq_handler(TIM_TypeDef *tim, void (*irq_handler)(void *), void *irq_param)
75 {
76  if (tim == TIM3)
77  {
78  if (bc_timer_tim3_irq.irq_handler == NULL)
79  {
80  bc_timer_tim3_irq.irq_handler = irq_handler;
81  bc_timer_tim3_irq.irq_param = irq_param;
82  return true;
83  }
84  }
85  if (tim == TIM6)
86  {
87  if (bc_timer_tim6_irq.irq_handler == NULL)
88  {
89  bc_timer_tim6_irq.irq_handler = irq_handler;
90  bc_timer_tim6_irq.irq_param = irq_param;
91  return true;
92  }
93  }
94 
95  if (tim == TIM2)
96  {
97  if (bc_timer_tim2_irq.irq_handler == NULL)
98  {
99  bc_timer_tim2_irq.irq_handler = irq_handler;
100  bc_timer_tim2_irq.irq_param = irq_param;
101  return true;
102  }
103  }
104 
105  return false;
106 }
107 
108 void TIM3_IRQHandler(void)
109 {
110  if (bc_timer_tim3_irq.irq_handler)
111  {
112  bc_timer_tim3_irq.irq_handler(bc_timer_tim3_irq.irq_param);
113  }
114 }
115 
116 void TIM2_IRQHandler(void)
117 {
118  if (bc_timer_tim2_irq.irq_handler)
119  {
120  bc_timer_tim2_irq.irq_handler(bc_timer_tim2_irq.irq_param);
121  }
122 }
123 
124 void TIM6_IRQHandler()
125 {
126  if (bc_timer_tim6_irq.irq_handler)
127  {
128  bc_timer_tim6_irq.irq_handler(bc_timer_tim6_irq.irq_param);
129  }
130 }
void bc_timer_start(void)
Start timer.
Definition: bc_timer.c:26
void bc_timer_delay(uint16_t microseconds)
Relative delay.
Definition: bc_timer.c:42
void bc_timer_stop(void)
Stop timer.
Definition: bc_timer.c:57
void bc_timer_clear(void)
Clear timer counter.
Definition: bc_timer.c:52
uint16_t bc_timer_get_microseconds(void)
Get actual tick of timer.
Definition: bc_timer.c:37
bool bc_timer_set_irq_handler(TIM_TypeDef *tim, void(*irq_handler)(void *), void *irq_param)
Register timer IRQ handler.
Definition: bc_timer.c:74
void bc_timer_clear_irq_handler(TIM_TypeDef *tim)
Unregister timer IRQ handler.
Definition: bc_timer.c:62
void bc_timer_init(void)
Initialize timer.
Definition: bc_timer.c:20