dRonin  adbada4
dRonin firmware
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
main.c
Go to the documentation of this file.
1 
15 /*
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful, but
22  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24  * for more details.
25  *
26  * You should have received a copy of the GNU General Public License along
27  * with this program; if not, see <http://www.gnu.org/licenses/>
28  */
29 /* Bootloader Includes */
30 #include <pios.h>
31 #include <pios_board_info.h>
32 #include "op_dfu.h"
33 #include "usb_lib.h"
34 #include "pios_iap.h"
35 #include "pios_com_msg.h"
36 /* Prototype of PIOS_Board_Init() function */
37 extern void PIOS_Board_Init(void);
38 extern void FLASH_Download();
39 #define BSL_HOLD_STATE ((PIOS_USB_DETECT_GPIO_PORT->IDR & PIOS_USB_DETECT_GPIO_PIN) ? 0 : 1)
40 
41 /* Private typedef -----------------------------------------------------------*/
42 typedef void (*pFunction)(void);
43 /* Private define ------------------------------------------------------------*/
44 /* Private macro -------------------------------------------------------------*/
45 /* Private variables ---------------------------------------------------------*/
47 uint32_t JumpAddress;
48 
50 uint32_t period1 = 5000; // 5 mS
51 uint32_t sweep_steps1 = 100; // * 5 mS -> 500 mS
52 uint32_t period2 = 5000; // 5 mS
53 uint32_t sweep_steps2 = 100; // * 5 mS -> 500 mS
54 
55 
57 uint8_t tempcount = 0;
58 
59 /* Extern variables ----------------------------------------------------------*/
61 int16_t status = 0;
62 uint8_t JumpToApp = false;
63 uint8_t GO_dfu = false;
64 uint8_t USB_connected = false;
65 uint8_t User_DFU_request = false;
66 static uint8_t mReceive_Buffer[63];
67 /* Private function prototypes -----------------------------------------------*/
68 uint32_t LedPWM(uint32_t pwm_period, uint32_t pwm_sweep_steps, uint32_t count);
69 uint8_t processRX();
70 void jump_to_app();
71 
72 int main() {
73  PIOS_SYS_Init();
75  PIOS_IAP_Init();
76 
78 
79  if (PIOS_IAP_CheckRequest() == true) {
80  PIOS_DELAY_WaitmS(1000);
81  User_DFU_request = true;
83  } else if(PIOS_Boot_CheckRequest() == true) {
84  JumpToApp = true;
86  PIOS_DELAY_WaitmS(1000);//needed so OS can detect BL USB disconnect
87  }
88  GO_dfu = (USB_connected == true) || (User_DFU_request == true);
89 
90  if (GO_dfu == true) {
91  if (User_DFU_request == true)
93  else
95  } else
96  JumpToApp = true;
97 
98  uint32_t stopwatch = 0;
99  uint32_t prev_ticks = PIOS_DELAY_GetuS();
100  while (true) {
101  /* Update the stopwatch */
102  uint32_t elapsed_ticks = PIOS_DELAY_GetuSSince(prev_ticks);
103  prev_ticks += elapsed_ticks;
104  stopwatch += elapsed_ticks;
105 
106  if (JumpToApp == true)
107  jump_to_app();
108 
109  switch (DeviceState) {
111  case uploadingStarting:
112  case DFUidle:
113  period1 = 5000;
114  sweep_steps1 = 100;
116  period2 = 0;
117  break;
118  case uploading:
119  period1 = 5000;
120  sweep_steps1 = 100;
121  period2 = 2500;
122  sweep_steps2 = 50;
123  break;
124  case downloading:
125  period1 = 2500;
126  sweep_steps1 = 50;
128  period2 = 0;
129  break;
130  case BLidle:
131  period1 = 0;
133  period2 = 0;
134  break;
135  default://error
136  period1 = 5000;
137  sweep_steps1 = 100;
138  period2 = 5000;
139  sweep_steps2 = 100;
140  }
141 
142  if (period1 != 0) {
143  if (LedPWM(period1, sweep_steps1, stopwatch))
145  else
147  } else
149 
150  if (period2 != 0) {
151  if (LedPWM(period2, sweep_steps2, stopwatch))
153  else
155  } else
157 
158  if (stopwatch > 50 * 1000 * 1000)
159  stopwatch = 0;
160  if ((stopwatch > 6 * 1000 * 1000) && (DeviceState
161  == BLidle))
162  JumpToApp = true;
163 
164  processRX();
166  }
167 }
168 
169 void jump_to_app() {
170  const struct pios_board_info * bdinfo = &pios_board_info_blob;
171 
172  if (((*(__IO uint32_t*) bdinfo->fw_base) & 0x2FFE0000) == 0x20000000) { /* Jump to user application */
173  FLASH_Lock();
174  RCC_APB2PeriphResetCmd(0xffffffff, ENABLE);
175  RCC_APB1PeriphResetCmd(0xffffffff, ENABLE);
176  RCC_APB2PeriphResetCmd(0xffffffff, DISABLE);
177  RCC_APB1PeriphResetCmd(0xffffffff, DISABLE);
178  _SetCNTR(0); // clear interrupt mask
179  _SetISTR(0); // clear all requests
180  JumpAddress = *(__IO uint32_t*) (bdinfo->fw_base + 4);
182  /* Initialize user application's Stack Pointer */
183  __set_MSP(*(__IO uint32_t*) bdinfo->fw_base);
185  } else {
187  return;
188  }
189 }
190 uint32_t LedPWM(uint32_t pwm_period, uint32_t pwm_sweep_steps, uint32_t count) {
191  uint32_t curr_step = (count / pwm_period) % pwm_sweep_steps; /* 0 - pwm_sweep_steps */
192  uint32_t pwm_duty = pwm_period * curr_step / pwm_sweep_steps; /* fraction of pwm_period */
193 
194  uint32_t curr_sweep = (count / (pwm_period * pwm_sweep_steps)); /* ticks once per full sweep */
195  if (curr_sweep & 1) {
196  pwm_duty = pwm_period - pwm_duty; /* reverse direction in odd sweeps */
197  }
198  return ((count % pwm_period) > pwm_duty) ? 1 : 0;
199 }
200 
201 uint8_t processRX() {
204  }
205  return true;
206 }
207 
uint32_t JumpAddress
Definition: main.c:47
uint32_t PIOS_IAP_CheckRequest(void)
Determines if an In-Application-Programming request has been made.
Definition: pios_iap.c:57
uint16_t PIOS_COM_MSG_Receive(uintptr_t com_id, uint8_t *buf, uint16_t buf_len)
Definition: common.h:35
int main(void)
Definition: main.c:441
Definition: common.h:50
uint32_t PIOS_DELAY_GetuS()
Query the Delay timer for the current uS.
Definition: pios_delay.c:173
Main PiOS header to include all the compiled in PiOS options.
const uint8_t count
Definition: panel.c:515
uint8_t processRX()
Definition: main.c:201
void processComand(uint8_t *xReceive_Buffer)
Definition: op_dfu.c:112
uint8_t USB_connected
Definition: main.c:64
uint32_t sweep_steps2
Definition: main.c:53
uint32_t PIOS_DELAY_GetuSSince(uint32_t t)
Calculate time in microseconds since a previous time.
Definition: pios_delay.c:183
void PIOS_SYS_Init(void)
#define PIOS_COM_TELEM_USB
Definition: pios_board.h:114
void PIOS_ANNUNC_Off(uint32_t annunc_id)
const struct pios_board_info pios_board_info_blob
uint32_t LedPWM(uint32_t pwm_period, uint32_t pwm_sweep_steps, uint32_t count)
Definition: main.c:190
void PIOS_Board_Init(void)
Definition: pios_board.c:44
int16_t status
Definition: main.c:61
void PIOS_IAP_Init(void)
PIOS_IAP_Init - performs required initializations for iap module.
Definition: pios_iap.c:44
uint32_t period1
LEDs PWM.
Definition: main.c:50
void PIOS_ANNUNC_On(uint32_t annunc_id)
COM MSG layer functions header.
This file contains the DFU commands handling code.
uint32_t sweep_steps1
Definition: main.c:51
uint8_t User_DFU_request
Definition: main.c:65
void jump_to_app()
Definition: main.c:169
uint8_t JumpToApp
Definition: main.c:62
uint8_t GO_dfu
Definition: main.c:63
pFunction Jump_To_Application
Definition: main.c:46
void PIOS_IAP_ClearRequest(void)
Definition: pios_iap.c:89
uint32_t PIOS_Boot_CheckRequest(void)
Determines if a boot request has been made.
Definition: pios_iap.c:63
uint8_t tempcount
Definition: main.c:57
DFUStates DeviceState
Definition: main.c:60
static uint8_t mReceive_Buffer[63]
Definition: main.c:66
DFUStates
Definition: common.h:42
Definition: common.h:43
bool PIOS_USB_CableConnected(uintptr_t id)
void FLASH_Download()
int32_t PIOS_DELAY_WaitmS(uint32_t mS)
Definition: pios_delay.c:140
void(* pFunction)(void)
Definition: main.c:42
uint32_t period2
Definition: main.c:52
In application programming functions.
#define PIOS_LED_HEARTBEAT
Definition: pios_board.h:85
void DataDownload(DownloadAction action)
Definition: op_dfu.c:79