From d71ade82277038100a42a9b6735c69001cf18cd0 Mon Sep 17 00:00:00 2001 From: zedamota Date: Wed, 25 Aug 2010 22:31:14 +0000 Subject: [PATCH] OP-21/Flight Bootloader - Now if USB not connected it jumps directly to FW. If connected it will wait 5 seconds for command, then jumps. Also changed led flashing to PWM and created a timer for the delays. git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@1409 ebee16cc-31ac-478f-84a7-5cbb03baadba --- flight/Bootloaders/OpenPilot/Makefile | 4 +- flight/Bootloaders/OpenPilot/hw_config.c | 8 +- .../OpenPilot/inc/platform_config.h | 5 +- flight/Bootloaders/OpenPilot/inc/stopwatch.h | 29 +++++++ flight/Bootloaders/OpenPilot/main.c | 47 +++++++++--- flight/Bootloaders/OpenPilot/stopwatch.c | 75 +++++++++++++++++++ flight/Bootloaders/OpenPilot/usb_desc.c | 4 +- flight/Bootloaders/OpenPilot/usb_endp.c | 2 + 8 files changed, 151 insertions(+), 23 deletions(-) create mode 100644 flight/Bootloaders/OpenPilot/inc/stopwatch.h create mode 100644 flight/Bootloaders/OpenPilot/stopwatch.c diff --git a/flight/Bootloaders/OpenPilot/Makefile b/flight/Bootloaders/OpenPilot/Makefile index cb2ad5dbc..2b4ebb530 100644 --- a/flight/Bootloaders/OpenPilot/Makefile +++ b/flight/Bootloaders/OpenPilot/Makefile @@ -92,7 +92,7 @@ SRC += $(HIDSYSTEM)/usb_istr.c SRC += $(HIDSYSTEM)/usb_prop.c SRC += $(HIDSYSTEM)/usb_pwr.c SRC += $(HIDSYSTEM)/stm3210e_eval.c - +SRC += $(HIDSYSTEM)/stopwatch.c ## CMSIS for STM32 SRC += $(CMSISDIR)/core_cm3.c @@ -112,7 +112,7 @@ SRC += $(STMSPDSRCDIR)/stm32f10x_gpio.c SRC += $(STMSPDSRCDIR)/stm32f10x_rcc.c #SRC += $(STMSPDSRCDIR)/stm32f10x_rtc.c #SRC += $(STMSPDSRCDIR)/stm32f10x_spi.c -#SRC += $(STMSPDSRCDIR)/stm32f10x_tim.c +SRC += $(STMSPDSRCDIR)/stm32f10x_tim.c #SRC += $(STMSPDSRCDIR)/stm32f10x_usart.c #SRC += $(STMSPDSRCDIR)/stm32f10x_wwdg.c SRC += $(STMSPDSRCDIR)/misc.c diff --git a/flight/Bootloaders/OpenPilot/hw_config.c b/flight/Bootloaders/OpenPilot/hw_config.c index ebd2e4d24..5fe7a7998 100644 --- a/flight/Bootloaders/OpenPilot/hw_config.c +++ b/flight/Bootloaders/OpenPilot/hw_config.c @@ -301,8 +301,7 @@ void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; - RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIO_DISCONNECT | - RCC_APB2Periph_GPIO_IOAIN , ENABLE); + #ifndef USE_STM3210C_EVAL /* USB_DISCONNECT used as USB pull-up */ @@ -312,10 +311,7 @@ void GPIO_Configuration(void) GPIO_Init(USB_DISCONNECT, &GPIO_InitStructure); #endif /* USE_STM3210C_EVAL */ - /* Configure Potentiometer IO as analog input */ - GPIO_InitStructure.GPIO_Pin = GPIO_IOAIN_PIN; - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; - GPIO_Init(GPIO_IOAIN, &GPIO_InitStructure); + } /******************************************************************************* diff --git a/flight/Bootloaders/OpenPilot/inc/platform_config.h b/flight/Bootloaders/OpenPilot/inc/platform_config.h index 3e83dd9ce..7640c8c9d 100644 --- a/flight/Bootloaders/OpenPilot/inc/platform_config.h +++ b/flight/Bootloaders/OpenPilot/inc/platform_config.h @@ -48,10 +48,7 @@ #endif /* USE_STM3210B_EVAL */ - #define RCC_APB2Periph_GPIO_IOAIN RCC_APB2Periph_GPIOC - #define GPIO_IOAIN GPIOC - #define GPIO_IOAIN_PIN GPIO_Pin_4 /* PC.04 */ - #define ADC_AIN_CHANNEL ADC_Channel_14 + /* Exported macro ------------------------------------------------------------*/ /* Exported functions ------------------------------------------------------- */ diff --git a/flight/Bootloaders/OpenPilot/inc/stopwatch.h b/flight/Bootloaders/OpenPilot/inc/stopwatch.h new file mode 100644 index 000000000..32e89f527 --- /dev/null +++ b/flight/Bootloaders/OpenPilot/inc/stopwatch.h @@ -0,0 +1,29 @@ + +#ifndef _STOPWATCH_H +#define _STOPWATCH_H + +///////////////////////////////////////////////////////////////////////////// +// Global definitions +///////////////////////////////////////////////////////////////////////////// + + +///////////////////////////////////////////////////////////////////////////// +// Global Types +///////////////////////////////////////////////////////////////////////////// + + +///////////////////////////////////////////////////////////////////////////// +// Prototypes +///////////////////////////////////////////////////////////////////////////// + +extern s32 STOPWATCH_Init(u32 resolution); +extern s32 STOPWATCH_Reset(void); +extern u32 STOPWATCH_ValueGet(void); + + +///////////////////////////////////////////////////////////////////////////// +// Export global variables +///////////////////////////////////////////////////////////////////////////// + + +#endif /* _STOPWATCH_H */ diff --git a/flight/Bootloaders/OpenPilot/main.c b/flight/Bootloaders/OpenPilot/main.c index 7c24b1024..ede89c012 100644 --- a/flight/Bootloaders/OpenPilot/main.c +++ b/flight/Bootloaders/OpenPilot/main.c @@ -19,8 +19,10 @@ #include "hw_config.h" #include "stm32_eval.h" #include "common.h" - +#include "platform_config.h" +#include "stopwatch.h" extern void FLASH_Download(); +#define BSL_HOLD_STATE ((USB_DISCONNECT->IDR & USB_DISCONNECT_PIN) ? 0 : 1) /* Private typedef -----------------------------------------------------------*/ typedef void (*pFunction)(void); @@ -29,6 +31,11 @@ typedef void (*pFunction)(void); /* Private variables ---------------------------------------------------------*/ pFunction Jump_To_Application; uint32_t JumpAddress; + +uint32_t cnt; +uint32_t pwm_period; +uint32_t pwm_sweep_steps; + /* Extern variables ----------------------------------------------------------*/ uint8_t DeviceState; uint8_t JumpToApp = 0; @@ -47,17 +54,38 @@ void DelayWithDown(__IO uint32_t nCount); int main(void) { Set_System(); + if (BSL_HOLD_STATE==0) { - USB_Interrupts_Config(); + USB_Interrupts_Config(); + Set_USBClock(); + USB_Init(); + DeviceState = idle; + STOPWATCH_Init(100); + } + else + JumpToApp = TRUE; + STOPWATCH_Reset(); - Set_USBClock(); - - USB_Init(); - - DeviceState = idle; while (JumpToApp == 0) { - STM_EVAL_LEDToggle(LED1); - DelayWithDown(10);//1000000); + + cnt = STOPWATCH_ValueGet(); // the reference counter (incremented each 100 uS) + pwm_period = 50; // *100 uS -> 5 mS + pwm_sweep_steps =100; // * 5 mS -> 500 mS + uint32_t pwm_duty = ((cnt / pwm_period) % pwm_sweep_steps) + / (pwm_sweep_steps / pwm_period); + if ((cnt % (2 * pwm_period * pwm_sweep_steps)) > pwm_period + * pwm_sweep_steps) + pwm_duty = pwm_period - pwm_duty; // negative direction each 50*100 ticks + uint32_t led_on = ((cnt % pwm_period) > pwm_duty) ? 1 : 0; + if(led_on==0) + STM_EVAL_LEDOn(LED1); + else + STM_EVAL_LEDOff(LED1); + if(STOPWATCH_ValueGet()>100*pwm_period*pwm_sweep_steps) + STOPWATCH_Reset(); + if(STOPWATCH_ValueGet()>50000) + JumpToApp=TRUE; + //DelayWithDown(10);//1000000); } if (((*(__IO uint32_t*) StartOfUserCode) & 0x2FFE0000) == 0x20000000) { /* Jump to user application */ FLASH_Lock(); @@ -74,6 +102,7 @@ int main(void) { __set_MSP(*(__IO uint32_t*) StartOfUserCode); Jump_To_Application(); } + while (1) { STM_EVAL_LEDToggle(LED1); STM_EVAL_LEDToggle(LED2); diff --git a/flight/Bootloaders/OpenPilot/stopwatch.c b/flight/Bootloaders/OpenPilot/stopwatch.c new file mode 100644 index 000000000..37ca80d97 --- /dev/null +++ b/flight/Bootloaders/OpenPilot/stopwatch.c @@ -0,0 +1,75 @@ + + +///////////////////////////////////////////////////////////////////////////// +// Include files +///////////////////////////////////////////////////////////////////////////// + +#include "stm32_eval.h" +#include "stm32f10x_tim.h" + + + + + +///////////////////////////////////////////////////////////////////////////// +// Local definitions +///////////////////////////////////////////////////////////////////////////// + +#define STOPWATCH_TIMER_BASE TIM6 +#define STOPWATCH_TIMER_RCC RCC_APB1Periph_TIM6 + +uint32_t STOPWATCH_Init(u32 resolution) +{ + // enable timer clock + if( STOPWATCH_TIMER_RCC == RCC_APB2Periph_TIM1 || STOPWATCH_TIMER_RCC == RCC_APB2Periph_TIM8 ) + RCC_APB2PeriphClockCmd(STOPWATCH_TIMER_RCC, ENABLE); + else + RCC_APB1PeriphClockCmd(STOPWATCH_TIMER_RCC, ENABLE); + + // time base configuration + TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; + TIM_TimeBaseStructure.TIM_Period = 0xffff; // max period + TIM_TimeBaseStructure.TIM_Prescaler = (72 * resolution)-1; // uS accuracy @ 72 MHz + TIM_TimeBaseStructure.TIM_ClockDivision = 0; + TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; + TIM_TimeBaseInit(STOPWATCH_TIMER_BASE, &TIM_TimeBaseStructure); + + // enable interrupt request + TIM_ITConfig(STOPWATCH_TIMER_BASE, TIM_IT_Update, ENABLE); + + // start counter + TIM_Cmd(STOPWATCH_TIMER_BASE, ENABLE); + + return 0; // no error +} + + +///////////////////////////////////////////////////////////////////////////// +//! Resets the stopwatch +//! \return < 0 on errors +///////////////////////////////////////////////////////////////////////////// +uint32_t STOPWATCH_Reset(void) +{ + // reset counter + STOPWATCH_TIMER_BASE->CNT = 1; // set to 1 instead of 0 to avoid new IRQ request + TIM_ClearITPendingBit(STOPWATCH_TIMER_BASE, TIM_IT_Update); + + return 0; // no error +} + + +///////////////////////////////////////////////////////////////////////////// +//! Returns current value of stopwatch +//! \return 1..65535: valid stopwatch value +//! \return 0xffffffff: counter overrun +///////////////////////////////////////////////////////////////////////////// +uint32_t STOPWATCH_ValueGet(void) +{ + uint32_t value = STOPWATCH_TIMER_BASE->CNT; + + if( TIM_GetITStatus(STOPWATCH_TIMER_BASE, TIM_IT_Update) != RESET ) + value = 0xffffffff; + + return value; +} + diff --git a/flight/Bootloaders/OpenPilot/usb_desc.c b/flight/Bootloaders/OpenPilot/usb_desc.c index ec10432b8..c77291b52 100644 --- a/flight/Bootloaders/OpenPilot/usb_desc.c +++ b/flight/Bootloaders/OpenPilot/usb_desc.c @@ -103,7 +103,7 @@ const uint8_t CustomHID_ConfigDescriptor[CUSTOMHID_SIZ_CONFIG_DESC] = 0x03, /* bmAttributes: Interrupt endpoint */ 0x40, /* diff:0x40 wMaxPacketSize: 2 Bytes max */ 0x00, - 0x08, /* diff:0x08 bInterval: Polling Interval (32 ms) */ + 0x01, /* diff:0x08 bInterval: Polling Interval (32 ms) */ /* 34 */ 0x07, /* bLength: Endpoint Descriptor size */ @@ -114,7 +114,7 @@ const uint8_t CustomHID_ConfigDescriptor[CUSTOMHID_SIZ_CONFIG_DESC] = 0x03, /* bmAttributes: Interrupt endpoint */ 0x40, /* diff:0x40 wMaxPacketSize: 2 Bytes max */ 0x00, - 0x08, /* diff:0x08 bInterval: Polling Interval (20 ms) */ + 0x01, /* diff:0x08 bInterval: Polling Interval (20 ms) */ /* 41 */ } ; /* diff:lots CustomHID_ConfigDescriptor */ diff --git a/flight/Bootloaders/OpenPilot/usb_endp.c b/flight/Bootloaders/OpenPilot/usb_endp.c index 14e731f7e..4cd88e9ce 100644 --- a/flight/Bootloaders/OpenPilot/usb_endp.c +++ b/flight/Bootloaders/OpenPilot/usb_endp.c @@ -23,6 +23,8 @@ #include "common.h" #include "hw_config.h" + + /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ /* Private macro -------------------------------------------------------------*/