2 ******************************************************************************
3 * @addtogroup RevolutionBL Revolution BootLoader
4 * @brief These files contain the code to the Revolution Bootloader.
8 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
9 * @brief This is the file with the main function of the Revolution BootLoader
10 * @see The GNU Public License (GPL) Version 3
12 *****************************************************************************/
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include <pios_board_info.h>
33 #include <fifo_buffer.h>
34 #include <pios_com_msg.h>
35 #include <pios_usbhook.h> /* PIOS_USBHOOK_* */
37 #include <pios_board_init.h>
38 #include <pios_board_io.h>
40 extern void FLASH_Download();
42 #define BSL_HOLD_STATE ((PIOS_USB_DETECT_GPIO_PORT->IDR & PIOS_USB_DETECT_GPIO_PIN) ? 0 : 1)
44 /* Private typedef -----------------------------------------------------------*/
45 typedef void (*pFunction
)(void);
46 /* Private define ------------------------------------------------------------*/
47 /* Private macro -------------------------------------------------------------*/
48 /* Private variables ---------------------------------------------------------*/
49 pFunction Jump_To_Application
;
53 uint32_t period1
= 5000; // 5 mS
54 uint32_t sweep_steps1
= 100; // * 5 mS -> 500 mS
55 uint32_t period2
= 5000; // 5 mS
56 uint32_t sweep_steps2
= 100; // * 5 mS -> 500 mS
59 ////////////////////////////////////////
60 uint8_t tempcount
= 0;
62 /* Extern variables ----------------------------------------------------------*/
63 DFUStates DeviceState
;
65 bool JumpToApp
= false;
67 bool USB_connected
= false;
68 bool User_DFU_request
= false;
69 static uint8_t mReceive_Buffer
[63];
70 /* Private function prototypes -----------------------------------------------*/
71 uint32_t LedPWM(uint32_t pwm_period
, uint32_t pwm_sweep_steps
, uint32_t count
);
81 // Make sure the brown out reset value for this chip
85 USB_connected
= PIOS_USB_CableConnected(0);
87 if (PIOS_IAP_CheckRequest() == true) {
88 PIOS_DELAY_WaitmS(1000);
89 User_DFU_request
= true;
90 PIOS_IAP_ClearRequest();
93 GO_dfu
= (USB_connected
== true) || (User_DFU_request
== true);
96 if (User_DFU_request
== true) {
97 DeviceState
= DFUidle
;
105 uint32_t stopwatch
= 0;
106 uint32_t prev_ticks
= PIOS_DELAY_GetuS();
108 /* Update the stopwatch */
109 uint32_t elapsed_ticks
= PIOS_DELAY_GetuSSince(prev_ticks
);
110 prev_ticks
+= elapsed_ticks
;
111 stopwatch
+= elapsed_ticks
;
113 if (JumpToApp
== true) {
117 switch (DeviceState
) {
118 case Last_operation_Success
:
119 case uploadingStarting
:
123 PIOS_LED_Off(PIOS_LED_HEARTBEAT
);
135 PIOS_LED_Off(PIOS_LED_HEARTBEAT
);
140 PIOS_LED_On(PIOS_LED_HEARTBEAT
);
151 if (LedPWM(period1
, sweep_steps1
, stopwatch
)) {
152 PIOS_LED_On(PIOS_LED_HEARTBEAT
);
154 PIOS_LED_Off(PIOS_LED_HEARTBEAT
);
157 PIOS_LED_On(PIOS_LED_HEARTBEAT
);
161 if (LedPWM(period2
, sweep_steps2
, stopwatch
)) {
162 PIOS_LED_On(PIOS_LED_HEARTBEAT
);
164 PIOS_LED_Off(PIOS_LED_HEARTBEAT
);
167 PIOS_LED_Off(PIOS_LED_HEARTBEAT
);
170 if (stopwatch
> 50 * 1000 * 1000) {
173 if ((stopwatch
> 6 * 1000 * 1000) && ((DeviceState
== BLidle
) || (DeviceState
== DFUidle
&& !USB_connected
))) {
184 const struct pios_board_info
*bdinfo
= &pios_board_info_blob
;
186 PIOS_LED_On(PIOS_LED_HEARTBEAT
);
187 // Look at cm3_vectors struct in startup. In a fw image the first uint32_t contains the address of the top of irqstack
188 uint32_t fwIrqStackBase
= (*(__IO
uint32_t *)bdinfo
->fw_base
) & 0xFFFE0000;
189 // Check for the two possible irqstack locations (sram or core coupled sram)
190 if (fwIrqStackBase
== 0x20000000 || fwIrqStackBase
== 0x10000000) {
191 /* Jump to user application */
193 RCC_APB2PeriphResetCmd(0xffffffff, ENABLE
);
194 RCC_APB1PeriphResetCmd(0xffffffff, ENABLE
);
195 RCC_APB2PeriphResetCmd(0xffffffff, DISABLE
);
196 RCC_APB1PeriphResetCmd(0xffffffff, DISABLE
);
198 PIOS_USBHOOK_Deactivate();
200 JumpAddress
= *(__IO
uint32_t *)(bdinfo
->fw_base
+ 4);
201 Jump_To_Application
= (pFunction
)JumpAddress
;
202 /* Initialize user application's Stack Pointer */
203 __set_MSP(*(__IO
uint32_t *)bdinfo
->fw_base
);
204 Jump_To_Application();
206 DeviceState
= failed_jump
;
210 uint32_t LedPWM(uint32_t pwm_period
, uint32_t pwm_sweep_steps
, uint32_t count
)
212 uint32_t curr_step
= (count
/ pwm_period
) % pwm_sweep_steps
; /* 0 - pwm_sweep_steps */
213 uint32_t pwm_duty
= pwm_period
* curr_step
/ pwm_sweep_steps
; /* fraction of pwm_period */
215 uint32_t curr_sweep
= (count
/ (pwm_period
* pwm_sweep_steps
)); /* ticks once per full sweep */
217 if (curr_sweep
& 1) {
218 pwm_duty
= pwm_period
- pwm_duty
; /* reverse direction in odd sweeps */
220 return ((count
% pwm_period
) > pwm_duty
) ? 1 : 0;
225 if (PIOS_COM_MSG_Receive(PIOS_COM_TELEM_USB
, mReceive_Buffer
, sizeof(mReceive_Buffer
))) {
226 processComand(mReceive_Buffer
);
232 * Check the brown out reset threshold is 2.7 volts and if not
233 * resets it. This solves an issue that can prevent boards
234 * powering up with some BEC
238 uint8_t bor
= FLASH_OB_GetBOR();
240 if (bor
!= OB_BOR_LEVEL3
) {
242 FLASH_OB_BORConfig(OB_BOR_LEVEL3
);
244 while (FLASH_WaitForLastOperation() == FLASH_BUSY
) {
248 while (FLASH_WaitForLastOperation() == FLASH_BUSY
) {
254 int32_t platform_senddata(const uint8_t *msg
, uint16_t msg_len
)
256 return PIOS_COM_MSG_Send(PIOS_COM_TELEM_USB
, msg
, msg_len
);