2011-08-13 05:23:16 +02:00
|
|
|
#include "pios.h"
|
|
|
|
|
2013-03-15 19:25:30 +01:00
|
|
|
#ifdef PIOS_INCLUDE_TIM
|
|
|
|
|
2011-08-13 05:23:16 +02:00
|
|
|
#include "pios_tim_priv.h"
|
|
|
|
|
|
|
|
enum pios_tim_dev_magic {
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_TIM_DEV_MAGIC = 0x87654098,
|
2011-08-13 05:23:16 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct pios_tim_dev {
|
2013-05-19 16:37:30 +02:00
|
|
|
enum pios_tim_dev_magic magic;
|
2011-08-13 05:23:16 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
const struct pios_tim_channel *channels;
|
|
|
|
uint8_t num_channels;
|
2011-08-13 05:23:16 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
const struct pios_tim_callbacks *callbacks;
|
|
|
|
uint32_t context;
|
2011-08-13 05:23:16 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#if 0
|
2013-05-19 16:37:30 +02:00
|
|
|
static bool PIOS_TIM_validate(struct pios_tim_dev *tim_dev)
|
2011-08-13 05:23:16 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
return tim_dev->magic == PIOS_TIM_DEV_MAGIC;
|
2011-08-13 05:23:16 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(PIOS_INCLUDE_FREERTOS) && 0
|
2013-05-19 16:37:30 +02:00
|
|
|
static struct pios_tim_dev *PIOS_TIM_alloc(void)
|
2011-08-13 05:23:16 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
struct pios_tim_dev *tim_dev;
|
2011-08-13 05:23:16 +02:00
|
|
|
|
2014-06-11 20:11:48 +02:00
|
|
|
tim_dev = (struct pios_tim_dev *)pios_malloc(sizeof(*tim_dev));
|
2013-05-19 16:37:30 +02:00
|
|
|
if (!tim_dev) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-08-13 05:23:16 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
tim_dev->magic = PIOS_TIM_DEV_MAGIC;
|
|
|
|
return tim_dev;
|
2011-08-13 05:23:16 +02:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
static struct pios_tim_dev pios_tim_devs[PIOS_TIM_MAX_DEVS];
|
|
|
|
static uint8_t pios_tim_num_devs;
|
2013-05-19 16:37:30 +02:00
|
|
|
static struct pios_tim_dev *PIOS_TIM_alloc(void)
|
2011-08-13 05:23:16 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
struct pios_tim_dev *tim_dev;
|
2011-08-13 05:23:16 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (pios_tim_num_devs >= PIOS_TIM_MAX_DEVS) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-08-13 05:23:16 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
tim_dev = &pios_tim_devs[pios_tim_num_devs++];
|
|
|
|
tim_dev->magic = PIOS_TIM_DEV_MAGIC;
|
2011-08-13 05:23:16 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
return tim_dev;
|
2011-08-13 05:23:16 +02:00
|
|
|
}
|
2013-05-19 16:37:30 +02:00
|
|
|
#endif /* if defined(PIOS_INCLUDE_FREERTOS) && 0 */
|
2011-08-13 05:23:16 +02:00
|
|
|
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
int32_t PIOS_TIM_InitClock(const struct pios_tim_clock_cfg *cfg)
|
2011-08-13 05:23:16 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_DEBUG_Assert(cfg);
|
|
|
|
|
|
|
|
/* Enable appropriate clock to timer module */
|
|
|
|
switch ((uint32_t)cfg->timer) {
|
|
|
|
case (uint32_t)TIM1:
|
|
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
|
|
|
|
break;
|
|
|
|
case (uint32_t)TIM2:
|
|
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
|
|
|
|
break;
|
|
|
|
case (uint32_t)TIM3:
|
|
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
|
|
|
|
break;
|
|
|
|
case (uint32_t)TIM4:
|
|
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
|
|
|
|
break;
|
2011-08-13 05:23:16 +02:00
|
|
|
#ifdef STM32F10X_HD
|
2013-05-19 16:37:30 +02:00
|
|
|
case (uint32_t)TIM5:
|
|
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
|
|
|
|
break;
|
|
|
|
case (uint32_t)TIM6:
|
|
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE);
|
|
|
|
break;
|
|
|
|
case (uint32_t)TIM7:
|
|
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM7, ENABLE);
|
|
|
|
break;
|
|
|
|
case (uint32_t)TIM8:
|
|
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE);
|
|
|
|
break;
|
2011-08-13 05:23:16 +02:00
|
|
|
#endif
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
2011-08-13 05:23:16 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Configure the dividers for this timer */
|
|
|
|
TIM_TimeBaseInit(cfg->timer, cfg->time_base_init);
|
2011-08-13 05:23:16 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Configure internal timer clocks */
|
|
|
|
TIM_InternalClockConfig(cfg->timer);
|
2011-08-13 05:23:16 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Enable timers */
|
|
|
|
TIM_Cmd(cfg->timer, ENABLE);
|
2011-08-13 05:23:16 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Enable Interrupts */
|
|
|
|
NVIC_Init(&cfg->irq.init);
|
2011-08-13 05:23:16 +02:00
|
|
|
|
2017-02-21 16:35:33 +01:00
|
|
|
/* Advanced TIM1 has additional IRQ */
|
|
|
|
if (cfg->timer == TIM1) {
|
|
|
|
NVIC_InitTypeDef init = cfg->irq.init;
|
|
|
|
init.NVIC_IRQChannel = TIM1_UP_IRQn;
|
|
|
|
NVIC_Init(&init);
|
|
|
|
}
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
return 0;
|
2011-08-13 05:23:16 +02:00
|
|
|
}
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
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)
|
2011-08-13 05:23:16 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(channels);
|
|
|
|
PIOS_Assert(num_channels);
|
|
|
|
|
|
|
|
struct pios_tim_dev *tim_dev;
|
|
|
|
tim_dev = (struct pios_tim_dev *)PIOS_TIM_alloc();
|
|
|
|
if (!tim_dev) {
|
|
|
|
goto out_fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Bind the configuration to the device instance */
|
|
|
|
tim_dev->channels = channels;
|
|
|
|
tim_dev->num_channels = num_channels;
|
|
|
|
tim_dev->callbacks = callbacks;
|
|
|
|
tim_dev->context = context;
|
|
|
|
|
|
|
|
/* Configure the pins */
|
|
|
|
for (uint8_t i = 0; i < num_channels; i++) {
|
|
|
|
const struct pios_tim_channel *chan = &(channels[i]);
|
|
|
|
|
|
|
|
/* Enable the peripheral clock for the GPIO */
|
|
|
|
switch ((uint32_t)chan->pin.gpio) {
|
|
|
|
case (uint32_t)GPIOA:
|
|
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
|
|
|
|
break;
|
|
|
|
case (uint32_t)GPIOB:
|
|
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
|
|
|
|
break;
|
|
|
|
case (uint32_t)GPIOC:
|
|
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
PIOS_Assert(0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
GPIO_Init(chan->pin.gpio, &chan->pin.init);
|
|
|
|
|
|
|
|
if (chan->remap) {
|
|
|
|
GPIO_PinRemapConfig(chan->remap, ENABLE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*tim_id = (uint32_t)tim_dev;
|
|
|
|
|
|
|
|
return 0;
|
2011-08-13 05:23:16 +02:00
|
|
|
|
|
|
|
out_fail:
|
2013-05-19 16:37:30 +02:00
|
|
|
return -1;
|
2011-08-13 05:23:16 +02:00
|
|
|
}
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
static void PIOS_TIM_generic_irq_handler(TIM_TypeDef *timer)
|
2011-08-13 05:23:16 +02:00
|
|
|
{
|
2015-01-19 21:55:40 +01:00
|
|
|
/* Check for an overflow event on this timer */
|
|
|
|
bool overflow_event = 0;
|
|
|
|
uint16_t overflow_count = false;
|
|
|
|
|
|
|
|
if (TIM_GetITStatus(timer, TIM_IT_Update) == SET) {
|
|
|
|
TIM_ClearITPendingBit(timer, TIM_IT_Update);
|
|
|
|
overflow_count = timer->ARR;
|
|
|
|
overflow_event = true;
|
|
|
|
}
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Iterate over all registered clients of the TIM layer to find channels on this timer */
|
|
|
|
for (uint8_t i = 0; i < pios_tim_num_devs; i++) {
|
|
|
|
const struct pios_tim_dev *tim_dev = &pios_tim_devs[i];
|
|
|
|
|
|
|
|
if (!tim_dev->channels || tim_dev->num_channels == 0) {
|
|
|
|
/* No channels to process on this client */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint8_t j = 0; j < tim_dev->num_channels; j++) {
|
|
|
|
const struct pios_tim_channel *chan = &tim_dev->channels[j];
|
|
|
|
|
|
|
|
if (chan->timer != timer) {
|
|
|
|
/* channel is not on this timer */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Figure out which interrupt bit we should be looking at */
|
|
|
|
uint16_t timer_it;
|
|
|
|
switch (chan->timer_chan) {
|
|
|
|
case TIM_Channel_1:
|
|
|
|
timer_it = TIM_IT_CC1;
|
|
|
|
break;
|
|
|
|
case TIM_Channel_2:
|
|
|
|
timer_it = TIM_IT_CC2;
|
|
|
|
break;
|
|
|
|
case TIM_Channel_3:
|
|
|
|
timer_it = TIM_IT_CC3;
|
|
|
|
break;
|
|
|
|
case TIM_Channel_4:
|
|
|
|
timer_it = TIM_IT_CC4;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
PIOS_Assert(0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool edge_event;
|
|
|
|
uint16_t edge_count;
|
|
|
|
if (TIM_GetITStatus(chan->timer, timer_it) == SET) {
|
|
|
|
TIM_ClearITPendingBit(chan->timer, timer_it);
|
|
|
|
|
|
|
|
/* Read the current counter */
|
|
|
|
switch (chan->timer_chan) {
|
|
|
|
case TIM_Channel_1:
|
|
|
|
edge_count = TIM_GetCapture1(chan->timer);
|
|
|
|
break;
|
|
|
|
case TIM_Channel_2:
|
|
|
|
edge_count = TIM_GetCapture2(chan->timer);
|
|
|
|
break;
|
|
|
|
case TIM_Channel_3:
|
|
|
|
edge_count = TIM_GetCapture3(chan->timer);
|
|
|
|
break;
|
|
|
|
case TIM_Channel_4:
|
|
|
|
edge_count = TIM_GetCapture4(chan->timer);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
PIOS_Assert(0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
edge_event = true;
|
|
|
|
} else {
|
|
|
|
edge_event = false;
|
|
|
|
edge_count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!tim_dev->callbacks) {
|
|
|
|
/* No callbacks registered, we're done with this channel */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Generate the appropriate callbacks */
|
|
|
|
if (overflow_event & edge_event) {
|
|
|
|
/*
|
|
|
|
* When both edge and overflow happen in the same interrupt, we
|
|
|
|
* need a heuristic to determine the order of the edge and overflow
|
|
|
|
* events so that the callbacks happen in the right order. If we
|
|
|
|
* get the order wrong, our pulse width calculations could be off by up
|
|
|
|
* to ARR ticks. That could be bad.
|
|
|
|
*
|
2015-01-25 21:51:37 +01:00
|
|
|
* Heuristic: If the edge_count is < 32 ticks above zero then we assume the
|
2013-05-19 16:37:30 +02:00
|
|
|
* edge happened just after the overflow.
|
|
|
|
*/
|
|
|
|
|
2015-01-19 21:55:40 +01:00
|
|
|
if (edge_count < 32) {
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Call the overflow callback first */
|
|
|
|
if (tim_dev->callbacks->overflow) {
|
|
|
|
(*tim_dev->callbacks->overflow)((uint32_t)tim_dev,
|
|
|
|
tim_dev->context,
|
|
|
|
j,
|
|
|
|
overflow_count);
|
|
|
|
}
|
|
|
|
/* Call the edge callback second */
|
|
|
|
if (tim_dev->callbacks->edge) {
|
|
|
|
(*tim_dev->callbacks->edge)((uint32_t)tim_dev,
|
|
|
|
tim_dev->context,
|
|
|
|
j,
|
|
|
|
edge_count);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Call the edge callback first */
|
|
|
|
if (tim_dev->callbacks->edge) {
|
|
|
|
(*tim_dev->callbacks->edge)((uint32_t)tim_dev,
|
|
|
|
tim_dev->context,
|
|
|
|
j,
|
|
|
|
edge_count);
|
|
|
|
}
|
|
|
|
/* Call the overflow callback second */
|
|
|
|
if (tim_dev->callbacks->overflow) {
|
|
|
|
(*tim_dev->callbacks->overflow)((uint32_t)tim_dev,
|
|
|
|
tim_dev->context,
|
|
|
|
j,
|
|
|
|
overflow_count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (overflow_event && tim_dev->callbacks->overflow) {
|
|
|
|
(*tim_dev->callbacks->overflow)((uint32_t)tim_dev,
|
|
|
|
tim_dev->context,
|
|
|
|
j,
|
|
|
|
overflow_count);
|
|
|
|
} else if (edge_event && tim_dev->callbacks->edge) {
|
|
|
|
(*tim_dev->callbacks->edge)((uint32_t)tim_dev,
|
|
|
|
tim_dev->context,
|
|
|
|
j,
|
|
|
|
edge_count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-08-13 05:23:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Bind Interrupt Handlers
|
|
|
|
*
|
|
|
|
* Map all valid TIM IRQs to the common interrupt handler
|
|
|
|
* and give it enough context to properly demux the various timers
|
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
void TIM1_UP_IRQHandler(void) __attribute__((alias("PIOS_TIM_1_UP_irq_handler")));
|
|
|
|
static void PIOS_TIM_1_UP_irq_handler(void)
|
2011-08-13 05:23:16 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_TIM_generic_irq_handler(TIM1);
|
2011-08-13 05:23:16 +02:00
|
|
|
}
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
void TIM1_CC_IRQHandler(void) __attribute__((alias("PIOS_TIM_1_CC_irq_handler")));
|
|
|
|
static void PIOS_TIM_1_CC_irq_handler(void)
|
2011-09-10 20:11:21 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_TIM_generic_irq_handler(TIM1);
|
2011-09-10 20:11:21 +02:00
|
|
|
}
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
void TIM2_IRQHandler(void) __attribute__((alias("PIOS_TIM_2_irq_handler")));
|
|
|
|
static void PIOS_TIM_2_irq_handler(void)
|
2011-08-13 05:23:16 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_TIM_generic_irq_handler(TIM2);
|
2011-08-13 05:23:16 +02:00
|
|
|
}
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
void TIM3_IRQHandler(void) __attribute__((alias("PIOS_TIM_3_irq_handler")));
|
|
|
|
static void PIOS_TIM_3_irq_handler(void)
|
2011-08-13 05:23:16 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_TIM_generic_irq_handler(TIM3);
|
2011-08-13 05:23:16 +02:00
|
|
|
}
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
void TIM4_IRQHandler(void) __attribute__((alias("PIOS_TIM_4_irq_handler")));
|
|
|
|
static void PIOS_TIM_4_irq_handler(void)
|
2011-08-13 05:23:16 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_TIM_generic_irq_handler(TIM4);
|
2011-08-13 05:23:16 +02:00
|
|
|
}
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
void TIM5_IRQHandler(void) __attribute__((alias("PIOS_TIM_5_irq_handler")));
|
|
|
|
static void PIOS_TIM_5_irq_handler(void)
|
2011-09-10 20:11:21 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_TIM_generic_irq_handler(TIM5);
|
2011-09-10 20:11:21 +02:00
|
|
|
}
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
void TIM6_IRQHandler(void) __attribute__((alias("PIOS_TIM_6_irq_handler")));
|
|
|
|
static void PIOS_TIM_6_irq_handler(void)
|
2011-09-10 20:11:21 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_TIM_generic_irq_handler(TIM6);
|
2011-09-10 20:11:21 +02:00
|
|
|
}
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
void TIM7_IRQHandler(void) __attribute__((alias("PIOS_TIM_7_irq_handler")));
|
|
|
|
static void PIOS_TIM_7_irq_handler(void)
|
2011-09-10 20:11:21 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_TIM_generic_irq_handler(TIM7);
|
2011-09-10 20:11:21 +02:00
|
|
|
}
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
void TIM8_UP_IRQHandler(void) __attribute__((alias("PIOS_TIM_8_UP_irq_handler")));
|
|
|
|
static void PIOS_TIM_8_UP_irq_handler(void)
|
2011-09-10 20:11:21 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_TIM_generic_irq_handler(TIM8);
|
2011-09-10 20:11:21 +02:00
|
|
|
}
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
void TIM8_CC_IRQHandler(void) __attribute__((alias("PIOS_TIM_8_CC_irq_handler")));
|
|
|
|
static void PIOS_TIM_8_CC_irq_handler(void)
|
2011-09-10 20:11:21 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_TIM_generic_irq_handler(TIM8);
|
2011-09-10 20:11:21 +02:00
|
|
|
}
|
|
|
|
|
2013-03-15 19:25:30 +01:00
|
|
|
#endif /* PIOS_INCLUDE_TIM */
|