3 #ifdef PIOS_INCLUDE_TIM
5 #include "pios_tim_priv.h"
7 enum pios_tim_dev_magic
{
8 PIOS_TIM_DEV_MAGIC
= 0x87654098,
12 enum pios_tim_dev_magic magic
;
14 const struct pios_tim_channel
*channels
;
17 const struct pios_tim_callbacks
*callbacks
;
22 static bool PIOS_TIM_validate(struct pios_tim_dev
*tim_dev
)
24 return tim_dev
->magic
== PIOS_TIM_DEV_MAGIC
;
28 #if defined(PIOS_INCLUDE_FREERTOS) && 0
29 static struct pios_tim_dev
*PIOS_TIM_alloc(void)
31 struct pios_tim_dev
*tim_dev
;
33 tim_dev
= (struct pios_tim_dev
*)malloc(sizeof(*tim_dev
));
38 tim_dev
->magic
= PIOS_TIM_DEV_MAGIC
;
42 static struct pios_tim_dev pios_tim_devs
[PIOS_TIM_MAX_DEVS
];
43 static uint8_t pios_tim_num_devs
;
44 static struct pios_tim_dev
*PIOS_TIM_alloc(void)
46 struct pios_tim_dev
*tim_dev
;
48 if (pios_tim_num_devs
>= PIOS_TIM_MAX_DEVS
) {
52 tim_dev
= &pios_tim_devs
[pios_tim_num_devs
++];
53 tim_dev
->magic
= PIOS_TIM_DEV_MAGIC
;
57 #endif /* if defined(PIOS_INCLUDE_FREERTOS) && 0 */
60 int32_t PIOS_TIM_InitClock(const struct pios_tim_clock_cfg
*cfg
)
62 PIOS_DEBUG_Assert(cfg
);
64 /* Configure the dividers for this timer */
65 TIM_TimeBaseInit(cfg
->timer
, cfg
->time_base_init
);
67 /* Configure internal timer clocks */
68 TIM_InternalClockConfig(cfg
->timer
);
71 TIM_Cmd(cfg
->timer
, ENABLE
);
73 /* Enable Interrupts */
74 NVIC_Init(&cfg
->irq
.init
);
79 int32_t PIOS_TIM_InitChannels(uint32_t *tim_id
, const struct pios_tim_channel
*channels
, uint8_t num_channels
, const struct pios_tim_callbacks
*callbacks
, uint32_t context
)
81 PIOS_Assert(channels
);
82 PIOS_Assert(num_channels
);
84 struct pios_tim_dev
*tim_dev
;
85 tim_dev
= (struct pios_tim_dev
*)PIOS_TIM_alloc();
90 /* Bind the configuration to the device instance */
91 tim_dev
->channels
= channels
;
92 tim_dev
->num_channels
= num_channels
;
93 tim_dev
->callbacks
= callbacks
;
94 tim_dev
->context
= context
;
96 /* Configure the pins */
97 for (uint8_t i
= 0; i
< num_channels
; i
++) {
98 const struct pios_tim_channel
*chan
= &(channels
[i
]);
100 GPIO_Init(chan
->pin
.gpio
, &chan
->pin
.init
);
103 GPIO_PinRemapConfig(chan
->remap
, ENABLE
);
107 *tim_id
= (uint32_t)tim_dev
;
115 static void PIOS_TIM_generic_irq_handler(TIM_TypeDef
*timer
)
117 /* Iterate over all registered clients of the TIM layer to find channels on this timer */
118 for (uint8_t i
= 0; i
< pios_tim_num_devs
; i
++) {
119 const struct pios_tim_dev
*tim_dev
= &pios_tim_devs
[i
];
121 if (!tim_dev
->channels
|| tim_dev
->num_channels
== 0) {
122 /* No channels to process on this client */
126 /* Check for an overflow event on this timer */
128 uint16_t overflow_count
;
129 if (TIM_GetITStatus(timer
, TIM_IT_Update
) == SET
) {
130 TIM_ClearITPendingBit(timer
, TIM_IT_Update
);
131 overflow_count
= timer
->ARR
;
132 overflow_event
= true;
135 overflow_event
= false;
138 for (uint8_t j
= 0; j
< tim_dev
->num_channels
; j
++) {
139 const struct pios_tim_channel
*chan
= &tim_dev
->channels
[j
];
141 if (chan
->timer
!= timer
) {
142 /* channel is not on this timer */
146 /* Figure out which interrupt bit we should be looking at */
148 switch (chan
->timer_chan
) {
150 timer_it
= TIM_IT_CC1
;
153 timer_it
= TIM_IT_CC2
;
156 timer_it
= TIM_IT_CC3
;
159 timer_it
= TIM_IT_CC4
;
168 if (TIM_GetITStatus(chan
->timer
, timer_it
) == SET
) {
169 TIM_ClearITPendingBit(chan
->timer
, timer_it
);
171 /* Read the current counter */
172 switch (chan
->timer_chan
) {
174 edge_count
= TIM_GetCapture1(chan
->timer
);
177 edge_count
= TIM_GetCapture2(chan
->timer
);
180 edge_count
= TIM_GetCapture3(chan
->timer
);
183 edge_count
= TIM_GetCapture4(chan
->timer
);
195 if (!tim_dev
->callbacks
) {
196 /* No callbacks registered, we're done with this channel */
200 /* Generate the appropriate callbacks */
201 if (overflow_event
& edge_event
) {
203 * When both edge and overflow happen in the same interrupt, we
204 * need a heuristic to determine the order of the edge and overflow
205 * events so that the callbacks happen in the right order. If we
206 * get the order wrong, our pulse width calculations could be off by up
207 * to ARR ticks. That could be bad.
209 * Heuristic: If the edge_count is < 16 ticks above zero then we assume the
210 * edge happened just after the overflow.
213 if (edge_count
< 16) {
214 /* Call the overflow callback first */
215 if (tim_dev
->callbacks
->overflow
) {
216 (*tim_dev
->callbacks
->overflow
)((uint32_t)tim_dev
,
221 /* Call the edge callback second */
222 if (tim_dev
->callbacks
->edge
) {
223 (*tim_dev
->callbacks
->edge
)((uint32_t)tim_dev
,
229 /* Call the edge callback first */
230 if (tim_dev
->callbacks
->edge
) {
231 (*tim_dev
->callbacks
->edge
)((uint32_t)tim_dev
,
236 /* Call the overflow callback second */
237 if (tim_dev
->callbacks
->overflow
) {
238 (*tim_dev
->callbacks
->overflow
)((uint32_t)tim_dev
,
244 } else if (overflow_event
&& tim_dev
->callbacks
->overflow
) {
245 (*tim_dev
->callbacks
->overflow
)((uint32_t)tim_dev
,
249 } else if (edge_event
&& tim_dev
->callbacks
->edge
) {
250 (*tim_dev
->callbacks
->edge
)((uint32_t)tim_dev
,
260 for (uint8_t i
= 0; i
< pios_pwm_cfg
.num_channels
; i
++) {
261 struct pios_pwm_channel channel
= pios_pwm_cfg
.channels
[i
];
262 if ((channel
.timer
== timer
) && (TIM_GetITStatus(channel
.timer
, channel
.ccr
) == SET
)) {
263 TIM_ClearITPendingBit(channel
.timer
, channel
.ccr
);
265 switch (channel
.channel
) {
267 val
= TIM_GetCapture1(channel
.timer
);
270 val
= TIM_GetCapture2(channel
.timer
);
273 val
= TIM_GetCapture3(channel
.timer
);
276 val
= TIM_GetCapture4(channel
.timer
);
280 if (CaptureState
[i
] == 0) {
286 // flip state machine and capture value here
287 /* Simple rise or fall state machine */
288 TIM_ICInitTypeDef TIM_ICInitStructure
= pios_pwm_cfg
.tim_ic_init
;
289 if (CaptureState
[i
] == 0) {
293 /* Switch polarity of input capture */
294 TIM_ICInitStructure
.TIM_ICPolarity
= TIM_ICPolarity_Falling
;
295 TIM_ICInitStructure
.TIM_Channel
= channel
.channel
;
296 TIM_ICInit(channel
.timer
, &TIM_ICInitStructure
);
298 /* Capture computation */
299 if (FallValue
[i
] > RiseValue
[i
]) {
300 CaptureValue
[i
] = (FallValue
[i
] - RiseValue
[i
]);
302 CaptureValue
[i
] = ((channel
.timer
->ARR
- RiseValue
[i
]) + FallValue
[i
]);
308 /* Increase supervisor counter */
311 /* Switch polarity of input capture */
312 TIM_ICInitStructure
.TIM_ICPolarity
= TIM_ICPolarity_Rising
;
313 TIM_ICInitStructure
.TIM_Channel
= channel
.channel
;
314 TIM_ICInit(channel
.timer
, &TIM_ICInitStructure
);
320 /* Bind Interrupt Handlers
322 * Map all valid TIM IRQs to the common interrupt handler
323 * and give it enough context to properly demux the various timers
325 void TIM1_UP_IRQHandler(void) __attribute__((alias("PIOS_TIM_1_UP_irq_handler")));
326 static void PIOS_TIM_1_UP_irq_handler(void)
328 PIOS_TIM_generic_irq_handler(TIM1
);
331 void TIM1_CC_IRQHandler(void) __attribute__((alias("PIOS_TIM_1_CC_irq_handler")));
332 static void PIOS_TIM_1_CC_irq_handler(void)
334 PIOS_TIM_generic_irq_handler(TIM1
);
337 void TIM2_IRQHandler(void) __attribute__((alias("PIOS_TIM_2_irq_handler")));
338 static void PIOS_TIM_2_irq_handler(void)
340 PIOS_TIM_generic_irq_handler(TIM2
);
343 void TIM3_IRQHandler(void) __attribute__((alias("PIOS_TIM_3_irq_handler")));
344 static void PIOS_TIM_3_irq_handler(void)
346 PIOS_TIM_generic_irq_handler(TIM3
);
349 void TIM4_IRQHandler(void) __attribute__((alias("PIOS_TIM_4_irq_handler")));
350 static void PIOS_TIM_4_irq_handler(void)
352 PIOS_TIM_generic_irq_handler(TIM4
);
355 void TIM5_IRQHandler(void) __attribute__((alias("PIOS_TIM_5_irq_handler")));
356 static void PIOS_TIM_5_irq_handler(void)
358 PIOS_TIM_generic_irq_handler(TIM5
);
361 void TIM6_IRQHandler(void) __attribute__((alias("PIOS_TIM_6_irq_handler")));
362 static void PIOS_TIM_6_irq_handler(void)
364 PIOS_TIM_generic_irq_handler(TIM6
);
367 void TIM7_IRQHandler(void) __attribute__((alias("PIOS_TIM_7_irq_handler")));
368 static void PIOS_TIM_7_irq_handler(void)
370 PIOS_TIM_generic_irq_handler(TIM7
);
373 void TIM8_UP_IRQHandler(void) __attribute__((alias("PIOS_TIM_8_UP_irq_handler")));
374 static void PIOS_TIM_8_UP_irq_handler(void)
376 PIOS_TIM_generic_irq_handler(TIM8
);
379 void TIM8_CC_IRQHandler(void) __attribute__((alias("PIOS_TIM_8_CC_irq_handler")));
380 static void PIOS_TIM_8_CC_irq_handler(void)
382 PIOS_TIM_generic_irq_handler(TIM8
);
385 #endif /* PIOS_INCLUDE_TIM */