Merged in f5soh/librepilot/update_credits (pull request #529)
[librepilot.git] / flight / targets / boards / oplinkmini / bootloader / main.c
blob566caea735ec8ef0aefdd00e5d3294bbc1b3f30d
1 /**
2 ******************************************************************************
3 * @addtogroup OpenPilotBL OpenPilot BootLoader
4 * @brief These files contain the code to the OpenPilot MB 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 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
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 <usb_lib.h>
33 #include <pios_iap.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;
48 uint32_t JumpAddress;
50 /// LEDs PWM
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;
64 int16_t status = 0;
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);
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 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);
91 if (GO_dfu == TRUE) {
92 PIOS_Board_Init();
93 if (User_DFU_request == TRUE) {
94 DeviceState = DFUidle;
95 } else {
96 DeviceState = BLidle;
98 } else {
99 JumpToApp = TRUE;
102 uint32_t stopwatch = 0;
103 uint32_t prev_ticks = PIOS_DELAY_GetuS();
104 while (TRUE) {
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) {
111 jump_to_app();
114 switch (DeviceState) {
115 case Last_operation_Success:
116 case uploadingStarting:
117 case DFUidle:
118 period1 = 5000;
119 sweep_steps1 = 100;
120 PIOS_LED_Off(PIOS_LED_ALARM);
121 period2 = 0;
122 break;
123 case uploading:
124 period1 = 5000;
125 sweep_steps1 = 100;
126 period2 = 2500;
127 sweep_steps2 = 50;
128 break;
129 case downloading:
130 period1 = 2500;
131 sweep_steps1 = 50;
132 PIOS_LED_Off(PIOS_LED_ALARM);
133 period2 = 0;
134 break;
135 case BLidle:
136 period1 = 0;
137 PIOS_LED_On(PIOS_LED_HEARTBEAT);
138 period2 = 0;
139 break;
140 default: // error
141 period1 = 5000;
142 sweep_steps1 = 100;
143 period2 = 5000;
144 sweep_steps2 = 100;
147 if (period1 != 0) {
148 if (LedPWM(period1, sweep_steps1, stopwatch)) {
149 PIOS_LED_On(PIOS_LED_HEARTBEAT);
150 } else {
151 PIOS_LED_Off(PIOS_LED_HEARTBEAT);
153 } else {
154 PIOS_LED_On(PIOS_LED_HEARTBEAT);
157 if (period2 != 0) {
158 if (LedPWM(period2, sweep_steps2, stopwatch)) {
159 PIOS_LED_On(PIOS_LED_ALARM);
160 } else {
161 PIOS_LED_Off(PIOS_LED_ALARM);
163 } else {
164 PIOS_LED_Off(PIOS_LED_ALARM);
167 if (stopwatch > 50 * 1000 * 1000) {
168 stopwatch = 0;
170 if ((stopwatch > 6 * 1000 * 1000) && (DeviceState
171 == BLidle)) {
172 JumpToApp = TRUE;
175 processRX();
176 DataDownload(start);
180 void jump_to_app()
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 */
185 FLASH_Lock();
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();
197 } else {
198 DeviceState = failed_jump;
199 return;
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;
215 uint8_t processRX()
217 if (PIOS_COM_MSG_Receive(pios_com_telem_usb_id, mReceive_Buffer, sizeof(mReceive_Buffer))) {
218 processComand(mReceive_Buffer);
220 return TRUE;
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);