2 ******************************************************************************
3 * @addtogroup OpenPilotBL OpenPilot BootLoader
4 * @brief These files contain the code to the OpenPilot MB 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 OpenPilot 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>
34 #include <fifo_buffer.h>
35 #include <pios_com_msg.h>
36 #include <pios_board_init.h>
37 #include <pios_board_io.h>
39 extern void FLASH_Download();
40 #define BSL_HOLD_STATE ((PIOS_USB_DETECT_GPIO_PORT->IDR & PIOS_USB_DETECT_GPIO_PIN) ? 0 : 1)
42 /* Private typedef -----------------------------------------------------------*/
43 typedef void (*pFunction
)(void);
44 /* Private define ------------------------------------------------------------*/
45 /* Private macro -------------------------------------------------------------*/
46 /* Private variables ---------------------------------------------------------*/
47 pFunction Jump_To_Application
;
51 uint32_t period1
= 5000; // 5 mS
52 uint32_t sweep_steps1
= 100; // * 5 mS -> 500 mS
53 uint32_t period2
= 5000; // 5 mS
54 uint32_t sweep_steps2
= 100; // * 5 mS -> 500 mS
57 ////////////////////////////////////////
58 uint8_t tempcount
= 0;
60 extern uint32_t pios_com_telem_usb_id
;
62 /* Extern variables ----------------------------------------------------------*/
63 DFUStates DeviceState
;
65 uint8_t JumpToApp
= FALSE
;
66 uint8_t GO_dfu
= FALSE
;
67 uint8_t USB_connected
= FALSE
;
68 uint8_t 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 USB_connected
= PIOS_USB_CableConnected(0);
83 if (PIOS_IAP_CheckRequest() == TRUE
) {
84 PIOS_DELAY_WaitmS(1000);
85 User_DFU_request
= TRUE
;
86 PIOS_IAP_ClearRequest();
89 GO_dfu
= (USB_connected
== TRUE
) || (User_DFU_request
== TRUE
);
93 if (User_DFU_request
== TRUE
) {
94 DeviceState
= DFUidle
;
102 uint32_t stopwatch
= 0;
103 uint32_t prev_ticks
= PIOS_DELAY_GetuS();
105 /* Update the stopwatch */
106 uint32_t elapsed_ticks
= PIOS_DELAY_GetuSSince(prev_ticks
);
107 prev_ticks
+= elapsed_ticks
;
108 stopwatch
+= elapsed_ticks
;
110 if (JumpToApp
== TRUE
) {
114 switch (DeviceState
) {
115 case Last_operation_Success
:
116 case uploadingStarting
:
120 PIOS_LED_Off(PIOS_LED_ALARM
);
132 PIOS_LED_Off(PIOS_LED_ALARM
);
137 PIOS_LED_On(PIOS_LED_HEARTBEAT
);
148 if (LedPWM(period1
, sweep_steps1
, stopwatch
)) {
149 PIOS_LED_On(PIOS_LED_HEARTBEAT
);
151 PIOS_LED_Off(PIOS_LED_HEARTBEAT
);
154 PIOS_LED_On(PIOS_LED_HEARTBEAT
);
158 if (LedPWM(period2
, sweep_steps2
, stopwatch
)) {
159 PIOS_LED_On(PIOS_LED_ALARM
);
161 PIOS_LED_Off(PIOS_LED_ALARM
);
164 PIOS_LED_Off(PIOS_LED_ALARM
);
167 if (stopwatch
> 50 * 1000 * 1000) {
170 if ((stopwatch
> 6 * 1000 * 1000) && (DeviceState
182 const struct pios_board_info
*bdinfo
= &pios_board_info_blob
;
184 if (((*(__IO
uint32_t *)bdinfo
->fw_base
) & 0x2FFE0000) == 0x20000000) { /* Jump to user application */
186 RCC_APB2PeriphResetCmd(0xffffffff, ENABLE
);
187 RCC_APB1PeriphResetCmd(0xffffffff, ENABLE
);
188 RCC_APB2PeriphResetCmd(0xffffffff, DISABLE
);
189 RCC_APB1PeriphResetCmd(0xffffffff, DISABLE
);
190 _SetCNTR(0); // clear interrupt mask
191 _SetISTR(0); // clear all requests
192 JumpAddress
= *(__IO
uint32_t *)(bdinfo
->fw_base
+ 4);
193 Jump_To_Application
= (pFunction
)JumpAddress
;
194 /* Initialize user application's Stack Pointer */
195 __set_MSP(*(__IO
uint32_t *)bdinfo
->fw_base
);
196 Jump_To_Application();
198 DeviceState
= failed_jump
;
202 uint32_t LedPWM(uint32_t pwm_period
, uint32_t pwm_sweep_steps
, uint32_t count
)
204 uint32_t curr_step
= (count
/ pwm_period
) % pwm_sweep_steps
; /* 0 - pwm_sweep_steps */
205 uint32_t pwm_duty
= pwm_period
* curr_step
/ pwm_sweep_steps
; /* fraction of pwm_period */
207 uint32_t curr_sweep
= (count
/ (pwm_period
* pwm_sweep_steps
)); /* ticks once per full sweep */
209 if (curr_sweep
& 1) {
210 pwm_duty
= pwm_period
- pwm_duty
; /* reverse direction in odd sweeps */
212 return ((count
% pwm_period
) > pwm_duty
) ? 1 : 0;
217 if (PIOS_COM_MSG_Receive(pios_com_telem_usb_id
, mReceive_Buffer
, sizeof(mReceive_Buffer
))) {
218 processComand(mReceive_Buffer
);
223 int32_t platform_senddata(const uint8_t *msg
, uint16_t msg_len
)
225 return PIOS_COM_MSG_Send(pios_com_telem_usb_id
, msg
, msg_len
);