From 17fb31a7fac6ffa7a623c131f2c689dd821977eb Mon Sep 17 00:00:00 2001 From: sambas Date: Sun, 29 May 2011 14:52:22 +0300 Subject: [PATCH] Spektrum rtc supervisor working, tested on CC and all outputs are activated. Needs review and testing before merge. --- flight/CopterControl/Makefile | 1 + flight/CopterControl/System/inc/pios_config.h | 1 + flight/CopterControl/System/pios_board.c | 31 ++++--- flight/OpenPilot/System/pios_board.c | 29 ++++--- flight/PiOS/STM32F10x/pios_rtc.c | 9 +- flight/PiOS/STM32F10x/pios_spektrum.c | 82 +++++++++---------- flight/PiOS/inc/pios_rtc.h | 4 +- flight/PiOS/inc/pios_spektrum_priv.h | 3 - flight/PiOS/pios.h | 1 + 9 files changed, 80 insertions(+), 81 deletions(-) diff --git a/flight/CopterControl/Makefile b/flight/CopterControl/Makefile index 2467968cb..bdfa8e2b5 100644 --- a/flight/CopterControl/Makefile +++ b/flight/CopterControl/Makefile @@ -187,6 +187,7 @@ SRC += $(PIOSSTM32F10X)/pios_spektrum.c SRC += $(PIOSSTM32F10X)/pios_debug.c SRC += $(PIOSSTM32F10X)/pios_gpio.c SRC += $(PIOSSTM32F10X)/pios_exti.c +SRC += $(PIOSSTM32F10X)/pios_rtc.c SRC += $(PIOSSTM32F10X)/pios_wdg.c diff --git a/flight/CopterControl/System/inc/pios_config.h b/flight/CopterControl/System/inc/pios_config.h index af1b7d22d..e483eef8a 100644 --- a/flight/CopterControl/System/inc/pios_config.h +++ b/flight/CopterControl/System/inc/pios_config.h @@ -64,6 +64,7 @@ #define PIOS_INCLUDE_FREERTOS #define PIOS_INCLUDE_GPIO #define PIOS_INCLUDE_EXTI +#define PIOS_INCLUDE_RTC #define PIOS_INCLUDE_WDG #define PIOS_INCLUDE_BL_HELPER diff --git a/flight/CopterControl/System/pios_board.c b/flight/CopterControl/System/pios_board.c index c5943ca3c..53dd5b505 100644 --- a/flight/CopterControl/System/pios_board.c +++ b/flight/CopterControl/System/pios_board.c @@ -348,39 +348,38 @@ void PIOS_USART_spektrum_irq_handler(void) } #include -void TIM2_IRQHandler(); -void TIM2_IRQHandler() __attribute__ ((alias ("PIOS_TIM2_irq_handler"))); +void RTC_IRQHandler(); +void RTC_IRQHandler() __attribute__ ((alias ("PIOS_SUPV_irq_handler"))); const struct pios_spektrum_cfg pios_spektrum_cfg = { .pios_usart_spektrum_cfg = &pios_usart_spektrum_cfg, - .tim_base_init = { - .TIM_Prescaler = (PIOS_MASTER_CLOCK / 1000000) - 1, /* For 1 uS accuracy */ - .TIM_ClockDivision = TIM_CKD_DIV1, - .TIM_CounterMode = TIM_CounterMode_Up, - .TIM_Period = ((1000000 / 120) - 1), //11ms-10*16b/115200bps atleast one interrupt between frames - .TIM_RepetitionCounter = 0x0000, - }, .gpio_init = { //used for bind feature .GPIO_Mode = GPIO_Mode_Out_PP, .GPIO_Speed = GPIO_Speed_2MHz, }, .remap = 0, .irq = { - .handler = TIM2_IRQHandler, + .handler = RTC_IRQHandler, .init = { .NVIC_IRQChannelPreemptionPriority = PIOS_IRQ_PRIO_MID, .NVIC_IRQChannelSubPriority = 0, .NVIC_IRQChannelCmd = ENABLE, }, }, - .timer = TIM2, .port = GPIOB, - .ccr = TIM_IT_Update, .pin = GPIO_Pin_11, }; -void PIOS_TIM2_irq_handler() -{ - PIOS_SPEKTRUM_irq_handler(pios_usart_spektrum_id); +void PIOS_SUPV_irq_handler() { + if (RTC_GetITStatus(RTC_IT_SEC)) + { + /* Call the right handler */ + PIOS_SPEKTRUM_irq_handler(pios_usart_spektrum_id); + + /* Wait until last write operation on RTC registers has finished */ + RTC_WaitForLastTask(); + /* Clear the RTC Second interrupt */ + RTC_ClearITPendingBit(RTC_IT_SEC); + } } #endif /* PIOS_INCLUDE_SPEKTRUM */ @@ -441,14 +440,12 @@ const struct pios_servo_channel pios_servo_channels[] = { .channel = TIM_Channel_1, .pin = GPIO_Pin_4, }, -#ifndef PIOS_INCLUDE_SPEKTRUM { .timer = TIM2, .port = GPIOA, .channel = TIM_Channel_3, .pin = GPIO_Pin_2, }, -#endif }; const struct pios_servo_cfg pios_servo_cfg = { diff --git a/flight/OpenPilot/System/pios_board.c b/flight/OpenPilot/System/pios_board.c index 57605ad9d..14be197e8 100644 --- a/flight/OpenPilot/System/pios_board.c +++ b/flight/OpenPilot/System/pios_board.c @@ -504,39 +504,38 @@ void PIOS_USART_spektrum_irq_handler(void) } #include -void TIM6_IRQHandler(); -void TIM6_IRQHandler() __attribute__ ((alias ("PIOS_TIM6_irq_handler"))); +void RTC_IRQHandler(); +void RTC_IRQHandler() __attribute__ ((alias ("PIOS_SUPV_irq_handler"))); const struct pios_spektrum_cfg pios_spektrum_cfg = { .pios_usart_spektrum_cfg = &pios_usart_spektrum_cfg, - .tim_base_init = { - .TIM_Prescaler = (PIOS_MASTER_CLOCK / 1000000) - 1, /* For 1 uS accuracy */ - .TIM_ClockDivision = TIM_CKD_DIV1, - .TIM_CounterMode = TIM_CounterMode_Up, - .TIM_Period = ((1000000 / 120) - 1), //11ms-10*16b/115200bps, atleast one interrupt between frames - .TIM_RepetitionCounter = 0x0000, - }, .gpio_init = { //used for bind feature .GPIO_Mode = GPIO_Mode_Out_PP, .GPIO_Speed = GPIO_Speed_2MHz, }, .remap = 0, .irq = { - .handler = TIM6_IRQHandler, + .handler = RTC_IRQHandler, .init = { .NVIC_IRQChannelPreemptionPriority = PIOS_IRQ_PRIO_MID, .NVIC_IRQChannelSubPriority = 0, .NVIC_IRQChannelCmd = ENABLE, }, }, - .timer = TIM6, .port = GPIOA, - .ccr = TIM_IT_Update, .pin = GPIO_Pin_10, }; -void PIOS_TIM6_irq_handler() -{ - PIOS_SPEKTRUM_irq_handler(); +void PIOS_SUPV_irq_handler() { + if (RTC_GetITStatus(RTC_IT_SEC)) + { + /* Call the right handler */ + PIOS_SPEKTRUM_irq_handler(pios_usart_spektrum_id); + + /* Wait until last write operation on RTC registers has finished */ + RTC_WaitForLastTask(); + /* Clear the RTC Second interrupt */ + RTC_ClearITPendingBit(RTC_IT_SEC); + } } #endif /* PIOS_COM_SPEKTRUM */ diff --git a/flight/PiOS/STM32F10x/pios_rtc.c b/flight/PiOS/STM32F10x/pios_rtc.c index 1f1769621..98d3160c1 100644 --- a/flight/PiOS/STM32F10x/pios_rtc.c +++ b/flight/PiOS/STM32F10x/pios_rtc.c @@ -34,7 +34,7 @@ #if defined(PIOS_INCLUDE_RTC) #ifndef PIOS_RTC_PRESCALAR -#define PIOS_RTC_PRESCALAR 0 +#define PIOS_RTC_PRESCALAR 100 #endif void PIOS_RTC_Init() @@ -48,6 +48,13 @@ void PIOS_RTC_Init() RTC_WaitForLastTask(); RTC_WaitForSynchro(); RTC_WaitForLastTask(); + +#if defined(PIOS_INCLUDE_SPEKTRUM) + /* Enable the RTC Second interrupt */ + RTC_ITConfig( RTC_IT_SEC, ENABLE ); + /* Wait until last write operation on RTC registers has finished */ + RTC_WaitForLastTask(); +#endif RTC_SetPrescaler(PIOS_RTC_PRESCALAR); // counting at 8e6 / 128 RTC_WaitForLastTask(); RTC_SetCounter(0); diff --git a/flight/PiOS/STM32F10x/pios_spektrum.c b/flight/PiOS/STM32F10x/pios_spektrum.c index c4b076d17..0eabe36a2 100644 --- a/flight/PiOS/STM32F10x/pios_spektrum.c +++ b/flight/PiOS/STM32F10x/pios_spektrum.c @@ -44,26 +44,21 @@ /** * @Note Framesyncing: * The code resets the watchdog timer whenever a single byte is received, so what watchdog code - * is never called if regularly getting bytes - -/** - * Constants + * is never called if regularly getting bytes. + * RTC timer is running @625Hz, supervisor timer has divider 5 so frame sync comes every 1/125Hz=8ms. + * Good for both 11ms and 22ms framecycles */ /* Global Variables */ -/* Local Variables, use pios_usart */ +/* Local Variables */ static uint16_t CaptureValue[12],CaptureValueTemp[12]; static uint8_t prev_byte = 0xFF, sync = 0, bytecount = 0, datalength=0, frame_error=0, byte_array[20] = { 0 }; - - -#define MAX_UPDATE_DELAY_MS 100 -static uint32_t last_updated_time = 0; -static uint32_t max_update_period = 0; uint8_t sync_of = 0; +uint16_t supv_timer=0; /** -* Initialise the onboard USARTs +* Bind and Initialise Spektrum satellite receiver */ void PIOS_SPEKTRUM_Init(void) { @@ -72,8 +67,15 @@ void PIOS_SPEKTRUM_Init(void) PIOS_SPEKTRUM_Bind(); } - last_updated_time = 0; - max_update_period = MAX_UPDATE_DELAY_MS * 1000 * PIOS_RTC_Rate(); + /* Init RTC supervisor timer interrupt */ + NVIC_InitTypeDef NVIC_InitStructure; + NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn; + NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = PIOS_IRQ_PRIO_MID; + NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; + NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; + NVIC_Init(&NVIC_InitStructure); + /* Init RTC clock */ + PIOS_RTC_Init(); } /** @@ -84,7 +86,6 @@ void PIOS_SPEKTRUM_Init(void) */ int16_t PIOS_SPEKTRUM_Get(int8_t Channel) { - if(PIOS_RTC_Counter() - last_updated_time /* Return error if channel not available */ if (Channel >= 12) { return -1; @@ -223,8 +224,7 @@ int32_t PIOS_SPEKTRUM_Decode(uint8_t b) } /* Interrupt handler for USART */ -void SPEKTRUM_IRQHandler(uint32_t usart_id) -{ +void SPEKTRUM_IRQHandler(uint32_t usart_id) { /* by always reading DR after SR make sure to clear any error interrupts */ volatile uint16_t sr = pios_spektrum_cfg.pios_usart_spektrum_cfg->regs->SR; volatile uint8_t b = pios_spektrum_cfg.pios_usart_spektrum_cfg->regs->DR; @@ -240,38 +240,34 @@ void SPEKTRUM_IRQHandler(uint32_t usart_id) /* Disable TXE interrupt (TXEIE=0) */ USART_ITConfig(pios_spektrum_cfg.pios_usart_spektrum_cfg->regs, USART_IT_TXE, DISABLE); } - /* clear "watchdog" timer */ - TIM_SetCounter(pios_spektrum_cfg.timer, 0); + /* byte arrived so clear "watchdog" timer */ + supv_timer=0; } /** - *@brief This function is called when a spektrum word hasnt been decoded for too long + *@brief This function is called between frames and when a spektrum word hasnt been decoded for too long + *@brief clears the channel values */ -void PIOS_SPEKTRUM_timeout() { - for (int i = 0; i < 12; i++) - { - CaptureValue[i] = 0; - CaptureValueTemp[i] = 0; - } -} - /* Clear timer interrupt pending bit */ - TIM_ClearITPendingBit(pios_spektrum_cfg.timer, TIM_IT_Update); - - /* sync between frames */ - sync = 0; - bytecount = 0; - prev_byte = 0xFF; - frame_error=0; - sync_of++; - /* watchdog activated */ - if (sync_of > 12) { - /* signal lost */ - sync_of = 0; - for (int i = 0; i < 12; i++) - { - CaptureValue[i] = 0; - CaptureValueTemp[i] = 0; +void PIOS_SPEKTRUM_irq_handler() { + /* 125hz */ + supv_timer++; + if(supv_timer > 5) { + /* sync between frames */ + sync = 0; + bytecount = 0; + prev_byte = 0xFF; + frame_error = 0; + sync_of++; + /* watchdog activated after 100ms silence */ + if (sync_of > 12) { + /* signal lost */ + sync_of = 0; + for (int i = 0; i < 12; i++) { + CaptureValue[i] = 0; + CaptureValueTemp[i] = 0; + } } + supv_timer = 0; } } diff --git a/flight/PiOS/inc/pios_rtc.h b/flight/PiOS/inc/pios_rtc.h index 0c9183d00..f68b54af8 100644 --- a/flight/PiOS/inc/pios_rtc.h +++ b/flight/PiOS/inc/pios_rtc.h @@ -27,8 +27,8 @@ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#ifndef PIOS_SERVO_H -#define PIOS_SERVO_H +#ifndef PIOS_RTC_H +#define PIOS_RTC_H /* Public Functions */ extern void PIOS_RTC_Init(); diff --git a/flight/PiOS/inc/pios_spektrum_priv.h b/flight/PiOS/inc/pios_spektrum_priv.h index 19a1db20b..b5dd384d1 100644 --- a/flight/PiOS/inc/pios_spektrum_priv.h +++ b/flight/PiOS/inc/pios_spektrum_priv.h @@ -37,13 +37,10 @@ struct pios_spektrum_cfg { const struct pios_usart_cfg * pios_usart_spektrum_cfg; - TIM_TimeBaseInitTypeDef tim_base_init; GPIO_InitTypeDef gpio_init; uint32_t remap; /* GPIO_Remap_* */ struct stm32_irq irq; - TIM_TypeDef * timer; GPIO_TypeDef * port; - uint16_t ccr; uint16_t pin; }; diff --git a/flight/PiOS/pios.h b/flight/PiOS/pios.h index 0fdb03570..8362a3af4 100644 --- a/flight/PiOS/pios.h +++ b/flight/PiOS/pios.h @@ -73,6 +73,7 @@ #include #include #include +#include #include #include #include