1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-01-18 03:52:11 +01:00

Spektrum rtc supervisor working, tested on CC and all outputs are activated. Needs review and testing before merge.

This commit is contained in:
sambas 2011-05-29 14:52:22 +03:00
parent 86d02e2d34
commit 17fb31a7fa
9 changed files with 80 additions and 81 deletions

View File

@ -187,6 +187,7 @@ SRC += $(PIOSSTM32F10X)/pios_spektrum.c
SRC += $(PIOSSTM32F10X)/pios_debug.c SRC += $(PIOSSTM32F10X)/pios_debug.c
SRC += $(PIOSSTM32F10X)/pios_gpio.c SRC += $(PIOSSTM32F10X)/pios_gpio.c
SRC += $(PIOSSTM32F10X)/pios_exti.c SRC += $(PIOSSTM32F10X)/pios_exti.c
SRC += $(PIOSSTM32F10X)/pios_rtc.c
SRC += $(PIOSSTM32F10X)/pios_wdg.c SRC += $(PIOSSTM32F10X)/pios_wdg.c

View File

@ -64,6 +64,7 @@
#define PIOS_INCLUDE_FREERTOS #define PIOS_INCLUDE_FREERTOS
#define PIOS_INCLUDE_GPIO #define PIOS_INCLUDE_GPIO
#define PIOS_INCLUDE_EXTI #define PIOS_INCLUDE_EXTI
#define PIOS_INCLUDE_RTC
#define PIOS_INCLUDE_WDG #define PIOS_INCLUDE_WDG
#define PIOS_INCLUDE_BL_HELPER #define PIOS_INCLUDE_BL_HELPER

View File

@ -348,39 +348,38 @@ void PIOS_USART_spektrum_irq_handler(void)
} }
#include <pios_spektrum_priv.h> #include <pios_spektrum_priv.h>
void TIM2_IRQHandler(); void RTC_IRQHandler();
void TIM2_IRQHandler() __attribute__ ((alias ("PIOS_TIM2_irq_handler"))); void RTC_IRQHandler() __attribute__ ((alias ("PIOS_SUPV_irq_handler")));
const struct pios_spektrum_cfg pios_spektrum_cfg = { const struct pios_spektrum_cfg pios_spektrum_cfg = {
.pios_usart_spektrum_cfg = &pios_usart_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_init = { //used for bind feature
.GPIO_Mode = GPIO_Mode_Out_PP, .GPIO_Mode = GPIO_Mode_Out_PP,
.GPIO_Speed = GPIO_Speed_2MHz, .GPIO_Speed = GPIO_Speed_2MHz,
}, },
.remap = 0, .remap = 0,
.irq = { .irq = {
.handler = TIM2_IRQHandler, .handler = RTC_IRQHandler,
.init = { .init = {
.NVIC_IRQChannelPreemptionPriority = PIOS_IRQ_PRIO_MID, .NVIC_IRQChannelPreemptionPriority = PIOS_IRQ_PRIO_MID,
.NVIC_IRQChannelSubPriority = 0, .NVIC_IRQChannelSubPriority = 0,
.NVIC_IRQChannelCmd = ENABLE, .NVIC_IRQChannelCmd = ENABLE,
}, },
}, },
.timer = TIM2,
.port = GPIOB, .port = GPIOB,
.ccr = TIM_IT_Update,
.pin = GPIO_Pin_11, .pin = GPIO_Pin_11,
}; };
void PIOS_TIM2_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); 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 */ #endif /* PIOS_INCLUDE_SPEKTRUM */
@ -441,14 +440,12 @@ const struct pios_servo_channel pios_servo_channels[] = {
.channel = TIM_Channel_1, .channel = TIM_Channel_1,
.pin = GPIO_Pin_4, .pin = GPIO_Pin_4,
}, },
#ifndef PIOS_INCLUDE_SPEKTRUM
{ {
.timer = TIM2, .timer = TIM2,
.port = GPIOA, .port = GPIOA,
.channel = TIM_Channel_3, .channel = TIM_Channel_3,
.pin = GPIO_Pin_2, .pin = GPIO_Pin_2,
}, },
#endif
}; };
const struct pios_servo_cfg pios_servo_cfg = { const struct pios_servo_cfg pios_servo_cfg = {

View File

@ -504,39 +504,38 @@ void PIOS_USART_spektrum_irq_handler(void)
} }
#include <pios_spektrum_priv.h> #include <pios_spektrum_priv.h>
void TIM6_IRQHandler(); void RTC_IRQHandler();
void TIM6_IRQHandler() __attribute__ ((alias ("PIOS_TIM6_irq_handler"))); void RTC_IRQHandler() __attribute__ ((alias ("PIOS_SUPV_irq_handler")));
const struct pios_spektrum_cfg pios_spektrum_cfg = { const struct pios_spektrum_cfg pios_spektrum_cfg = {
.pios_usart_spektrum_cfg = &pios_usart_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_init = { //used for bind feature
.GPIO_Mode = GPIO_Mode_Out_PP, .GPIO_Mode = GPIO_Mode_Out_PP,
.GPIO_Speed = GPIO_Speed_2MHz, .GPIO_Speed = GPIO_Speed_2MHz,
}, },
.remap = 0, .remap = 0,
.irq = { .irq = {
.handler = TIM6_IRQHandler, .handler = RTC_IRQHandler,
.init = { .init = {
.NVIC_IRQChannelPreemptionPriority = PIOS_IRQ_PRIO_MID, .NVIC_IRQChannelPreemptionPriority = PIOS_IRQ_PRIO_MID,
.NVIC_IRQChannelSubPriority = 0, .NVIC_IRQChannelSubPriority = 0,
.NVIC_IRQChannelCmd = ENABLE, .NVIC_IRQChannelCmd = ENABLE,
}, },
}, },
.timer = TIM6,
.port = GPIOA, .port = GPIOA,
.ccr = TIM_IT_Update,
.pin = GPIO_Pin_10, .pin = GPIO_Pin_10,
}; };
void PIOS_TIM6_irq_handler() void PIOS_SUPV_irq_handler() {
if (RTC_GetITStatus(RTC_IT_SEC))
{ {
PIOS_SPEKTRUM_irq_handler(); /* 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 */ #endif /* PIOS_COM_SPEKTRUM */

View File

@ -34,7 +34,7 @@
#if defined(PIOS_INCLUDE_RTC) #if defined(PIOS_INCLUDE_RTC)
#ifndef PIOS_RTC_PRESCALAR #ifndef PIOS_RTC_PRESCALAR
#define PIOS_RTC_PRESCALAR 0 #define PIOS_RTC_PRESCALAR 100
#endif #endif
void PIOS_RTC_Init() void PIOS_RTC_Init()
@ -48,6 +48,13 @@ void PIOS_RTC_Init()
RTC_WaitForLastTask(); RTC_WaitForLastTask();
RTC_WaitForSynchro(); RTC_WaitForSynchro();
RTC_WaitForLastTask(); 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_SetPrescaler(PIOS_RTC_PRESCALAR); // counting at 8e6 / 128
RTC_WaitForLastTask(); RTC_WaitForLastTask();
RTC_SetCounter(0); RTC_SetCounter(0);

View File

@ -44,26 +44,21 @@
/** /**
* @Note Framesyncing: * @Note Framesyncing:
* The code resets the watchdog timer whenever a single byte is received, so what watchdog code * The code resets the watchdog timer whenever a single byte is received, so what watchdog code
* is never called if regularly getting bytes * 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
* Constants
*/ */
/* Global Variables */ /* Global Variables */
/* Local Variables, use pios_usart */ /* Local Variables */
static uint16_t CaptureValue[12],CaptureValueTemp[12]; 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 }; 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; 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) void PIOS_SPEKTRUM_Init(void)
{ {
@ -72,8 +67,15 @@ void PIOS_SPEKTRUM_Init(void)
PIOS_SPEKTRUM_Bind(); PIOS_SPEKTRUM_Bind();
} }
last_updated_time = 0; /* Init RTC supervisor timer interrupt */
max_update_period = MAX_UPDATE_DELAY_MS * 1000 * PIOS_RTC_Rate(); 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) int16_t PIOS_SPEKTRUM_Get(int8_t Channel)
{ {
if(PIOS_RTC_Counter() - last_updated_time
/* Return error if channel not available */ /* Return error if channel not available */
if (Channel >= 12) { if (Channel >= 12) {
return -1; return -1;
@ -223,8 +224,7 @@ int32_t PIOS_SPEKTRUM_Decode(uint8_t b)
} }
/* Interrupt handler for USART */ /* 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 */ /* 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 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; volatile uint8_t b = pios_spektrum_cfg.pios_usart_spektrum_cfg->regs->DR;
@ -240,39 +240,35 @@ void SPEKTRUM_IRQHandler(uint32_t usart_id)
/* Disable TXE interrupt (TXEIE=0) */ /* Disable TXE interrupt (TXEIE=0) */
USART_ITConfig(pios_spektrum_cfg.pios_usart_spektrum_cfg->regs, USART_IT_TXE, DISABLE); USART_ITConfig(pios_spektrum_cfg.pios_usart_spektrum_cfg->regs, USART_IT_TXE, DISABLE);
} }
/* clear "watchdog" timer */ /* byte arrived so clear "watchdog" timer */
TIM_SetCounter(pios_spektrum_cfg.timer, 0); 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() { void PIOS_SPEKTRUM_irq_handler() {
for (int i = 0; i < 12; i++) /* 125hz */
{ supv_timer++;
CaptureValue[i] = 0; if(supv_timer > 5) {
CaptureValueTemp[i] = 0;
}
}
/* Clear timer interrupt pending bit */
TIM_ClearITPendingBit(pios_spektrum_cfg.timer, TIM_IT_Update);
/* sync between frames */ /* sync between frames */
sync = 0; sync = 0;
bytecount = 0; bytecount = 0;
prev_byte = 0xFF; prev_byte = 0xFF;
frame_error = 0; frame_error = 0;
sync_of++; sync_of++;
/* watchdog activated */ /* watchdog activated after 100ms silence */
if (sync_of > 12) { if (sync_of > 12) {
/* signal lost */ /* signal lost */
sync_of = 0; sync_of = 0;
for (int i = 0; i < 12; i++) for (int i = 0; i < 12; i++) {
{
CaptureValue[i] = 0; CaptureValue[i] = 0;
CaptureValueTemp[i] = 0; CaptureValueTemp[i] = 0;
} }
} }
supv_timer = 0;
}
} }
#endif #endif

View File

@ -27,8 +27,8 @@
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/ */
#ifndef PIOS_SERVO_H #ifndef PIOS_RTC_H
#define PIOS_SERVO_H #define PIOS_RTC_H
/* Public Functions */ /* Public Functions */
extern void PIOS_RTC_Init(); extern void PIOS_RTC_Init();

View File

@ -37,13 +37,10 @@
struct pios_spektrum_cfg { struct pios_spektrum_cfg {
const struct pios_usart_cfg * pios_usart_spektrum_cfg; const struct pios_usart_cfg * pios_usart_spektrum_cfg;
TIM_TimeBaseInitTypeDef tim_base_init;
GPIO_InitTypeDef gpio_init; GPIO_InitTypeDef gpio_init;
uint32_t remap; /* GPIO_Remap_* */ uint32_t remap; /* GPIO_Remap_* */
struct stm32_irq irq; struct stm32_irq irq;
TIM_TypeDef * timer;
GPIO_TypeDef * port; GPIO_TypeDef * port;
uint16_t ccr;
uint16_t pin; uint16_t pin;
}; };

View File

@ -73,6 +73,7 @@
#include <pios_irq.h> #include <pios_irq.h>
#include <pios_adc.h> #include <pios_adc.h>
#include <pios_servo.h> #include <pios_servo.h>
#include <pios_rtc.h>
#include <pios_i2c.h> #include <pios_i2c.h>
#include <pios_spi.h> #include <pios_spi.h>
#include <pios_ppm.h> #include <pios_ppm.h>