Merged in f5soh/librepilot/update_credits (pull request #529)
[librepilot.git] / flight / targets / boards / revolution / bootloader / main.c
blobc85ef675e60a5bf6f5291865aaec51fcc2068f9c
1 /**
2 ******************************************************************************
3 * @addtogroup RevolutionBL Revolution BootLoader
4 * @brief These files contain the code to the Revolution Bootloader.
6 * @{
7 * @file main.c
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
22 * for more details.
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
29 #include <pios.h>
30 #include <pios_board_info.h>
31 #include <op_dfu.h>
32 #include <pios_iap.h>
33 #include <fifo_buffer.h>
34 #include <pios_com_msg.h>
35 #include <pios_usbhook.h> /* PIOS_USBHOOK_* */
36 #include <stdbool.h>
37 #include <pios_board_init.h>
38 #include <pios_board_io.h>
40 extern void FLASH_Download();
41 void check_bor();
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;
50 uint32_t JumpAddress;
52 /// LEDs PWM
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;
64 int16_t status = 0;
65 bool JumpToApp = false;
66 bool GO_dfu = 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);
72 uint8_t processRX();
73 void jump_to_app();
75 int main()
77 PIOS_SYS_Init();
78 PIOS_Board_Init();
79 PIOS_IAP_Init();
81 // Make sure the brown out reset value for this chip
82 // is 2.7 volts
83 check_bor();
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);
95 if (GO_dfu == true) {
96 if (User_DFU_request == true) {
97 DeviceState = DFUidle;
98 } else {
99 DeviceState = BLidle;
101 } else {
102 JumpToApp = true;
105 uint32_t stopwatch = 0;
106 uint32_t prev_ticks = PIOS_DELAY_GetuS();
107 while (true) {
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) {
114 jump_to_app();
117 switch (DeviceState) {
118 case Last_operation_Success:
119 case uploadingStarting:
120 case DFUidle:
121 period1 = 5000;
122 sweep_steps1 = 100;
123 PIOS_LED_Off(PIOS_LED_HEARTBEAT);
124 period2 = 0;
125 break;
126 case uploading:
127 period1 = 5000;
128 sweep_steps1 = 100;
129 period2 = 2500;
130 sweep_steps2 = 50;
131 break;
132 case downloading:
133 period1 = 2500;
134 sweep_steps1 = 50;
135 PIOS_LED_Off(PIOS_LED_HEARTBEAT);
136 period2 = 0;
137 break;
138 case BLidle:
139 period1 = 0;
140 PIOS_LED_On(PIOS_LED_HEARTBEAT);
141 period2 = 0;
142 break;
143 default: // error
144 period1 = 5000;
145 sweep_steps1 = 100;
146 period2 = 5000;
147 sweep_steps2 = 100;
150 if (period1 != 0) {
151 if (LedPWM(period1, sweep_steps1, stopwatch)) {
152 PIOS_LED_On(PIOS_LED_HEARTBEAT);
153 } else {
154 PIOS_LED_Off(PIOS_LED_HEARTBEAT);
156 } else {
157 PIOS_LED_On(PIOS_LED_HEARTBEAT);
160 if (period2 != 0) {
161 if (LedPWM(period2, sweep_steps2, stopwatch)) {
162 PIOS_LED_On(PIOS_LED_HEARTBEAT);
163 } else {
164 PIOS_LED_Off(PIOS_LED_HEARTBEAT);
166 } else {
167 PIOS_LED_Off(PIOS_LED_HEARTBEAT);
170 if (stopwatch > 50 * 1000 * 1000) {
171 stopwatch = 0;
173 if ((stopwatch > 6 * 1000 * 1000) && ((DeviceState == BLidle) || (DeviceState == DFUidle && !USB_connected))) {
174 JumpToApp = true;
177 processRX();
178 DataDownload(start);
182 void jump_to_app()
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 */
192 FLASH_Lock();
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();
205 } else {
206 DeviceState = failed_jump;
207 return;
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;
223 uint8_t processRX()
225 if (PIOS_COM_MSG_Receive(PIOS_COM_TELEM_USB, mReceive_Buffer, sizeof(mReceive_Buffer))) {
226 processComand(mReceive_Buffer);
228 return true;
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
236 void check_bor()
238 uint8_t bor = FLASH_OB_GetBOR();
240 if (bor != OB_BOR_LEVEL3) {
241 FLASH_OB_Unlock();
242 FLASH_OB_BORConfig(OB_BOR_LEVEL3);
243 FLASH_OB_Launch();
244 while (FLASH_WaitForLastOperation() == FLASH_BUSY) {
247 FLASH_OB_Lock();
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);