From 8b0415d9055a28bba680ef81e4459f9db407efad Mon Sep 17 00:00:00 2001 From: Stacey Sheldon Date: Fri, 29 Jul 2011 13:50:46 -0400 Subject: [PATCH] bl: alternate stopwatch for small bootloaders The small bootloaders (CC and PipX) are out of flash space so their stopwatch implementation has been swapped out for one based on the DELAY clock that takes about 500 bytes less of code space. Identical functionality is preserved. --- flight/Bootloaders/CopterControl/Makefile | 3 -- flight/Bootloaders/CopterControl/main.c | 38 +++++++++++---------- flight/Bootloaders/PipXtreme/Makefile | 3 -- flight/Bootloaders/PipXtreme/main.c | 40 +++++++++++------------ flight/PiOS/STM32F10x/pios_delay.c | 14 ++++++-- flight/PiOS/inc/pios_delay.h | 1 + 6 files changed, 53 insertions(+), 46 deletions(-) diff --git a/flight/Bootloaders/CopterControl/Makefile b/flight/Bootloaders/CopterControl/Makefile index 4fc2f0f08..fe613f909 100644 --- a/flight/Bootloaders/CopterControl/Makefile +++ b/flight/Bootloaders/CopterControl/Makefile @@ -94,8 +94,6 @@ DOXYGENDIR = ../Doc/Doxygen SRC += $(OPSYSTEM)/main.c SRC += $(OPSYSTEM)/pios_board.c SRC += $(OPSYSTEM)/op_dfu.c -SRC += $(FLIGHTLIB)/stopwatch.c - ## PIOS Hardware (STM32F10x) SRC += $(PIOSSTM32F10X)/pios_sys.c @@ -139,7 +137,6 @@ SRC += $(STMSPDSRCDIR)/stm32f10x_pwr.c SRC += $(STMSPDSRCDIR)/stm32f10x_rcc.c SRC += $(STMSPDSRCDIR)/stm32f10x_rtc.c SRC += $(STMSPDSRCDIR)/stm32f10x_spi.c -SRC += $(STMSPDSRCDIR)/stm32f10x_tim.c SRC += $(STMSPDSRCDIR)/stm32f10x_usart.c SRC += $(STMSPDSRCDIR)/stm32f10x_dbgmcu.c SRC += $(STMSPDSRCDIR)/misc.c diff --git a/flight/Bootloaders/CopterControl/main.c b/flight/Bootloaders/CopterControl/main.c index 5e20e5cb7..d8d06424e 100644 --- a/flight/Bootloaders/CopterControl/main.c +++ b/flight/Bootloaders/CopterControl/main.c @@ -28,7 +28,6 @@ /* Bootloader Includes */ #include #include -#include "stopwatch.h" #include "op_dfu.h" #include "usb_lib.h" #include "pios_iap.h" @@ -47,9 +46,9 @@ pFunction Jump_To_Application; uint32_t JumpAddress; /// LEDs PWM -uint32_t period1 = 50; // *100 uS -> 5 mS +uint32_t period1 = 5000; // 5 mS uint32_t sweep_steps1 = 100; // * 5 mS -> 500 mS -uint32_t period2 = 50; // *100 uS -> 5 mS +uint32_t period2 = 5000; // 5 mS uint32_t sweep_steps2 = 100; // * 5 mS -> 500 mS @@ -70,7 +69,6 @@ uint8_t processRX(); void jump_to_app(); #define BLUE LED1 -#define LED_PWM_TIMER TIM1 int main() { PIOS_SYS_Init(); if (BSL_HOLD_STATE == 0) @@ -93,13 +91,17 @@ int main() { DeviceState = DFUidle; else DeviceState = BLidle; - STOPWATCH_Init(100, LED_PWM_TIMER); } else JumpToApp = TRUE; - STOPWATCH_Reset(LED_PWM_TIMER); - + uint32_t stopwatch = 0; + uint32_t prev_ticks = PIOS_DELAY_GetuS(); while (TRUE) { + /* Update the stopwatch */ + uint32_t elapsed_ticks = PIOS_DELAY_GetuSSince(prev_ticks); + prev_ticks += elapsed_ticks; + stopwatch += elapsed_ticks; + if (JumpToApp == TRUE) jump_to_app(); @@ -107,19 +109,19 @@ int main() { case Last_operation_Success: case uploadingStarting: case DFUidle: - period1 = 50; + period1 = 5000; sweep_steps1 = 100; PIOS_LED_Off(BLUE); period2 = 0; break; case uploading: - period1 = 50; + period1 = 5000; sweep_steps1 = 100; - period2 = 25; + period2 = 2500; sweep_steps2 = 50; break; case downloading: - period1 = 25; + period1 = 2500; sweep_steps1 = 50; PIOS_LED_Off(BLUE); period2 = 0; @@ -130,14 +132,14 @@ int main() { period2 = 0; break; default://error - period1 = 50; + period1 = 5000; sweep_steps1 = 100; - period2 = 50; + period2 = 5000; sweep_steps2 = 100; } if (period1 != 0) { - if (LedPWM(period1, sweep_steps1, STOPWATCH_ValueGet(LED_PWM_TIMER))) + if (LedPWM(period1, sweep_steps1, stopwatch)) PIOS_LED_On(BLUE); else PIOS_LED_Off(BLUE); @@ -145,16 +147,16 @@ int main() { PIOS_LED_On(BLUE); if (period2 != 0) { - if (LedPWM(period2, sweep_steps2, STOPWATCH_ValueGet(LED_PWM_TIMER))) + if (LedPWM(period2, sweep_steps2, stopwatch)) PIOS_LED_On(BLUE); else PIOS_LED_Off(BLUE); } else PIOS_LED_Off(BLUE); - if (STOPWATCH_ValueGet(LED_PWM_TIMER) > 100 * 50 * 100) - STOPWATCH_Reset(LED_PWM_TIMER); - if ((STOPWATCH_ValueGet(LED_PWM_TIMER) > 60000) && (DeviceState + if (stopwatch > 50 * 1000 * 1000) + stopwatch = 0; + if ((stopwatch > 6 * 1000 * 1000) && (DeviceState == BLidle)) JumpToApp = TRUE; diff --git a/flight/Bootloaders/PipXtreme/Makefile b/flight/Bootloaders/PipXtreme/Makefile index dbf537bab..64f13d0d2 100644 --- a/flight/Bootloaders/PipXtreme/Makefile +++ b/flight/Bootloaders/PipXtreme/Makefile @@ -94,8 +94,6 @@ DOXYGENDIR = ../Doc/Doxygen SRC += $(OPSYSTEM)/main.c SRC += $(OPSYSTEM)/pios_board.c SRC += $(OPSYSTEM)/op_dfu.c -SRC += $(FLIGHTLIB)/stopwatch.c - ## PIOS Hardware (STM32F10x) SRC += $(PIOSSTM32F10X)/pios_sys.c @@ -138,7 +136,6 @@ SRC += $(STMSPDSRCDIR)/stm32f10x_pwr.c SRC += $(STMSPDSRCDIR)/stm32f10x_rcc.c SRC += $(STMSPDSRCDIR)/stm32f10x_rtc.c SRC += $(STMSPDSRCDIR)/stm32f10x_spi.c -SRC += $(STMSPDSRCDIR)/stm32f10x_tim.c SRC += $(STMSPDSRCDIR)/stm32f10x_usart.c SRC += $(STMSPDSRCDIR)/stm32f10x_dbgmcu.c SRC += $(STMSPDSRCDIR)/misc.c diff --git a/flight/Bootloaders/PipXtreme/main.c b/flight/Bootloaders/PipXtreme/main.c index eeb1b06e3..0337c51a5 100644 --- a/flight/Bootloaders/PipXtreme/main.c +++ b/flight/Bootloaders/PipXtreme/main.c @@ -28,7 +28,6 @@ /* Bootloader Includes */ #include #include -#include "stopwatch.h" #include "op_dfu.h" #include "usb_lib.h" #include "pios_iap.h" @@ -47,9 +46,9 @@ pFunction Jump_To_Application; uint32_t JumpAddress; /// LEDs PWM -uint32_t period1 = 50; // *100 uS -> 5 mS +uint32_t period1 = 5000; // 5 mS uint32_t sweep_steps1 = 100; // * 5 mS -> 500 mS -uint32_t period2 = 50; // *100 uS -> 5 mS +uint32_t period2 = 5000; // 5 mS uint32_t sweep_steps2 = 100; // * 5 mS -> 500 mS @@ -71,7 +70,6 @@ void jump_to_app(); #define BLUE LED1 #define RED LED4 -#define LED_PWM_TIMER TIM3 int main() { /* NOTE: Do NOT modify the following start-up sequence */ /* Any new initialization functions should be added in OpenPilotInit() */ @@ -98,35 +96,37 @@ int main() { DeviceState = DFUidle; else DeviceState = BLidle; - STOPWATCH_Init(100, LED_PWM_TIMER); } else JumpToApp = TRUE; - STOPWATCH_Reset(LED_PWM_TIMER); - + uint32_t stopwatch = 0; + uint32_t prev_ticks = PIOS_DELAY_GetuS(); while (TRUE) { + /* Update the stopwatch */ + uint32_t elapsed_ticks = PIOS_DELAY_GetuSSince(prev_ticks); + prev_ticks += elapsed_ticks; + stopwatch += elapsed_ticks; + if (JumpToApp == TRUE) jump_to_app(); - //pwm_period = 50; // *100 uS -> 5 mS - //pwm_sweep_steps =100; // * 5 mS -> 500 mS switch (DeviceState) { case Last_operation_Success: case uploadingStarting: case DFUidle: - period1 = 50; + period1 = 5000; sweep_steps1 = 100; PIOS_LED_Off(RED); period2 = 0; break; case uploading: - period1 = 50; + period1 = 5000; sweep_steps1 = 100; - period2 = 25; + period2 = 2500; sweep_steps2 = 50; break; case downloading: - period1 = 25; + period1 = 2500; sweep_steps1 = 50; PIOS_LED_Off(RED); period2 = 0; @@ -137,14 +137,14 @@ int main() { period2 = 0; break; default://error - period1 = 50; + period1 = 5000; sweep_steps1 = 100; - period2 = 50; + period2 = 5000; sweep_steps2 = 100; } if (period1 != 0) { - if (LedPWM(period1, sweep_steps1, STOPWATCH_ValueGet(LED_PWM_TIMER))) + if (LedPWM(period1, sweep_steps1, stopwatch)) PIOS_LED_On(BLUE); else PIOS_LED_Off(BLUE); @@ -152,16 +152,16 @@ int main() { PIOS_LED_On(BLUE); if (period2 != 0) { - if (LedPWM(period2, sweep_steps2, STOPWATCH_ValueGet(LED_PWM_TIMER))) + if (LedPWM(period2, sweep_steps2, stopwatch)) PIOS_LED_On(RED); else PIOS_LED_Off(RED); } else PIOS_LED_Off(RED); - if (STOPWATCH_ValueGet(LED_PWM_TIMER) > 100 * 50 * 100) - STOPWATCH_Reset(LED_PWM_TIMER); - if ((STOPWATCH_ValueGet(LED_PWM_TIMER) > 60000) && (DeviceState + if (stopwatch > 50 * 1000 * 1000) + stopwatch = 0; + if ((stopwatch > 6 * 1000 * 1000) && (DeviceState == BLidle)) JumpToApp = TRUE; diff --git a/flight/PiOS/STM32F10x/pios_delay.c b/flight/PiOS/STM32F10x/pios_delay.c index 8081877a7..c4e74717b 100644 --- a/flight/PiOS/STM32F10x/pios_delay.c +++ b/flight/PiOS/STM32F10x/pios_delay.c @@ -116,7 +116,7 @@ int32_t PIOS_DELAY_WaituS(uint32_t uS) * // Wait for 500 mS * PIOS_DELAY_Wait_mS(500); * \endcode - * \param[in] mS delay (1..65535 milliseconds) + * \param[in] mS delay * \return < 0 on errors */ int32_t PIOS_DELAY_WaitmS(uint32_t mS) @@ -133,11 +133,21 @@ int32_t PIOS_DELAY_WaitmS(uint32_t mS) * @brief Query the Delay timer for the current uS * @return A microsecond value */ -uint32_t PIOS_DELAY_GetuS() +uint32_t PIOS_DELAY_GetuS(void) { return DWT_CYCCNT / us_ticks; } +/** + * @brief Calculate time in microseconds since a previous time + * @param[in] t previous time + * @return time in us since previous time t. + */ +uint32_t PIOS_DELAY_GetuSSince(uint32_t t) +{ + return (PIOS_DELAY_GetuS() - t); +} + #endif /** diff --git a/flight/PiOS/inc/pios_delay.h b/flight/PiOS/inc/pios_delay.h index 30c483a71..17e6d0452 100644 --- a/flight/PiOS/inc/pios_delay.h +++ b/flight/PiOS/inc/pios_delay.h @@ -37,6 +37,7 @@ extern int32_t PIOS_DELAY_Init(void); extern int32_t PIOS_DELAY_WaituS(uint32_t uS); extern int32_t PIOS_DELAY_WaitmS(uint32_t mS); extern uint32_t PIOS_DELAY_GetuS(); +extern uint32_t PIOS_DELAY_GetuSSince(uint32_t t); #endif /* PIOS_DELAY_H */