dRonin  adbada4
dRonin firmware
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
firmwareiap.c
Go to the documentation of this file.
1 
18 /*
19  * This program is free software; you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation; either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful, but
25  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
26  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
27  * for more details.
28  *
29  * You should have received a copy of the GNU General Public License along
30  * with this program; if not, see <http://www.gnu.org/licenses/>
31  */
32 #include <stdint.h>
33 
34 #include "pios.h"
35 #include <pios_board_info.h>
36 #include "openpilot.h"
37 #include <eventdispatcher.h>
38 #include "firmwareiap.h"
39 #include "firmwareiapobj.h"
40 #include "flightstatus.h"
41 #include "pios_servo.h"
42 #include "pios_thread.h"
43 
44 // Private constants
45 #define IAP_CMD_STEP_1 1122
46 #define IAP_CMD_STEP_2 2233
47 #define IAP_CMD_STEP_3 3344
48 #define IAP_CMD_STEP_3NB 4455
49 
50 #define IAP_CMD_CRC 100
51 #define IAP_CMD_VERIFY 101
52 #define IAP_CMD_VERSION 102
53 
54 #define IAP_STATE_READY 0
55 #define IAP_STATE_STEP_1 1
56 #define IAP_STATE_STEP_2 2
57 #define IAP_STATE_RESETTING 3
58 
59 #define RESET_DELAY_MS 500 /* delay before sending reset to INS */
60 
61 const uint32_t iap_time_2_low_end = 500;
62 const uint32_t iap_time_2_high_end = 5000;
63 const uint32_t iap_time_3_low_end = 500;
64 const uint32_t iap_time_3_high_end = 5000;
65 
66 // Private types
67 
68 // Private variables
69 static uint8_t reset_count = 0;
70 static uint32_t lastResetSysTime;
71 static uint8_t board_rev = 0;
72 
73 // Private functions
74 static void FirmwareIAPCallback(const UAVObjEvent *ev,
75  void *ctx, void *obj, int len);
76 
77 static uint32_t get_time(void);
78 
79 static void resetTask(const UAVObjEvent *ev, void *ctx, void *obj, int len);
80 
95 {
96 
97  if (FirmwareIAPObjInitialize() == -1) {
98  return -1;
99  }
100 
101  const struct pios_board_info * bdinfo = &pios_board_info_blob;
102 
103  if (!board_rev)
104  board_rev = bdinfo->board_rev;
105 
106  FirmwareIAPObjData data;
107  FirmwareIAPObjGet(&data);
108 
109  data.BoardType= bdinfo->board_type;
110  PIOS_BL_HELPER_FLASH_Read_Description(data.Description,FIRMWAREIAPOBJ_DESCRIPTION_NUMELEM);
111  PIOS_SYS_SerialNumberGetBinary(data.CPUSerial);
112  data.BoardRevision = board_rev;
113  data.crc = PIOS_BL_HELPER_CRC_Memory_Calc();
114  FirmwareIAPObjSet( &data );
115  if(bdinfo->magic==PIOS_BOARD_INFO_BLOB_MAGIC) FirmwareIAPObjConnectCallback( &FirmwareIAPCallback );
116  return 0;
117 }
118 
127 static void FirmwareIAPCallback(const UAVObjEvent *ev,
128  void *ctx, void *obj, int len)
129 {
130  (void) ctx; (void) obj; (void) len;
131 
132  static uint8_t iap_state = IAP_STATE_READY;
133  static uint32_t last_time = 0;
134  uint32_t this_time;
135  uint32_t delta;
136 
137  if(iap_state == IAP_STATE_RESETTING)
138  return;
139 
140  FirmwareIAPObjData data;
141  FirmwareIAPObjGet(&data);
142 
143  if ( ev->obj == FirmwareIAPObjHandle() ) {
144  // Get the input object data
145  FirmwareIAPObjGet(&data);
146  this_time = get_time();
147  delta = this_time - last_time;
148  last_time = this_time;
149 
150  switch(iap_state) {
151  case IAP_STATE_READY:
152  if( data.Command == IAP_CMD_STEP_1 ) {
153  iap_state = IAP_STATE_STEP_1;
154  } break;
155  case IAP_STATE_STEP_1:
156  if( data.Command == IAP_CMD_STEP_2 ) {
157  if( delta > iap_time_2_low_end && delta < iap_time_2_high_end ) {
158  iap_state = IAP_STATE_STEP_2;
159  } else {
160  iap_state = IAP_STATE_READY;
161  }
162  } else {
163  iap_state = IAP_STATE_READY;
164  }
165  break;
166  case IAP_STATE_STEP_2:
167  if( (data.Command == IAP_CMD_STEP_3) || (data.Command == IAP_CMD_STEP_3NB) ) {
168  if( delta > iap_time_3_low_end && delta < iap_time_3_high_end ) {
169 
170  FlightStatusData flightStatus;
171  FlightStatusGet(&flightStatus);
172 
173  if(flightStatus.Armed != FLIGHTSTATUS_ARMED_DISARMED) {
174  // Abort any attempts if not disarmed
175  iap_state = IAP_STATE_READY;
176  break;
177  }
178 
179  // we've met the three sequence of command numbers
180  // we've met the time requirements.
181  if(data.Command == IAP_CMD_STEP_3) {
184  }
185  else if(data.Command == IAP_CMD_STEP_3NB) {
188  }
189 
190  /* Note: Cant just wait timeout value, because first time is randomized */
191  reset_count = 0;
194  memset(ev,0,sizeof(UAVObjEvent));
196  PIOS_Servo_PrepareForReset(); // Stop PWM
197  iap_state = IAP_STATE_RESETTING;
198  } else {
199  iap_state = IAP_STATE_READY;
200  }
201  } else {
202  iap_state = IAP_STATE_READY;
203  }
204  break;
205  case IAP_STATE_RESETTING:
206  // stay here permanentally, should reboot
207  break;
208  default:
209  iap_state = IAP_STATE_READY;
210  last_time = 0; // Reset the time counter, as we are not doing a IAP reset
211  break;
212  }
213  }
214 }
215 
216 
217 
218 // Returns number of milliseconds from the start of the kernel.
219 
228 static uint32_t get_time(void)
229 {
230  return PIOS_Thread_Systime();
231 }
232 
236 static void resetTask(const UAVObjEvent *ev, void *ctx, void *obj, int len)
237 {
238  (void) ctx; (void) obj; (void) len;
239 
240 #if defined(PIOS_INCLUDE_ANNUNC) && defined(PIOS_LED_HEARTBEAT)
242 #endif /* PIOS_LED_HEARTBEAT */
243 
244 #if defined(PIOS_INCLUDE_ANNUNC) && defined(PIOS_LED_ALARM)
246 #endif /* PIOS_LED_ALARM */
247 
248  FirmwareIAPObjData data;
249  FirmwareIAPObjGet(&data);
250 
251  if((uint32_t) (PIOS_Thread_Systime() - lastResetSysTime) > RESET_DELAY_MS) {
253  PIOS_SYS_Reset();
254  }
255 }
256 
258 {
259  return 0;
260 };
261 
262 void FirmwareIAPSetBoardRev(uint8_t rev)
263 {
264  board_rev = rev;
265 }
266 
#define IAP_CMD_STEP_3NB
Definition: firmwareiap.c:48
uint32_t PIOS_Thread_Systime(void)
Definition: pios_thread.c:212
static uint8_t board_rev
Definition: firmwareiap.c:71
Main PiOS header to include all the compiled in PiOS options.
uint32_t PIOS_BL_HELPER_CRC_Memory_Calc()
static uint32_t get_time(void)
Returns number of milliseconds from the start of the kernel.
Definition: firmwareiap.c:228
#define IAP_STATE_STEP_1
Definition: firmwareiap.c:55
const uint32_t iap_time_2_low_end
Definition: firmwareiap.c:61
int32_t FirmwareIAPInitialize()
Performs object initialization functions.
Definition: firmwareiap.c:94
const uint32_t iap_time_3_high_end
Definition: firmwareiap.c:64
static uint8_t reset_count
Definition: firmwareiap.c:69
void * PIOS_malloc_no_dma(size_t size)
Definition: pios_heap.c:166
const struct pios_board_info pios_board_info_blob
UAVObjHandle obj
static uint32_t lastResetSysTime
Definition: firmwareiap.c:70
int32_t EventPeriodicCallbackCreate(UAVObjEvent *ev, UAVObjEventCallback cb, uint16_t periodMs)
Definition: systemmod.c:884
uint8_t data[XFER_BYTES_PER_PACKET]
Definition: bl_messages.h:129
#define PIOS_LED_ALARM
Definition: pios_board.h:86
int32_t FirmwareIAPStart()
Definition: firmwareiap.c:257
#define MODULE_HIPRI_INITCALL(ifn, sfn)
Definition: pios_initcall.h:66
void FirmwareIAPSetBoardRev(uint8_t rev)
Definition: firmwareiap.c:262
void PIOS_Servo_PrepareForReset()
Determines the APB clock used by a given timer.
Definition: pios_servo.c:948
void PIOS_BL_HELPER_FLASH_Read_Description(uint8_t *array, uint8_t size)
#define IAP_CMD_STEP_2
Definition: firmwareiap.c:46
#define PIOS_BOARD_INFO_BLOB_MAGIC
Event dispatcher, distributes object events as callbacks. Alternative to using tasks and queues...
void PIOS_IAP_SetRequest3(void)
Definition: pios_iap.c:84
int32_t PIOS_SYS_Reset(void)
#define IAP_STATE_RESETTING
Definition: firmwareiap.c:57
const uint32_t iap_time_2_high_end
Definition: firmwareiap.c:62
#define RESET_DELAY_MS
Definition: firmwareiap.c:59
void PIOS_ANNUNC_Toggle(uint32_t annunc_id)
#define IAP_CMD_STEP_1
Definition: firmwareiap.c:45
#define IAP_STATE_STEP_2
Definition: firmwareiap.c:56
static void resetTask(const UAVObjEvent *ev, void *ctx, void *obj, int len)
Definition: firmwareiap.c:236
#define IAP_STATE_READY
Definition: firmwareiap.c:54
Includes PiOS and core architecture components.
const uint32_t iap_time_3_low_end
Definition: firmwareiap.c:63
#define IAP_CMD_STEP_3
Definition: firmwareiap.c:47
void PIOS_IAP_SetRequest2(void)
Definition: pios_iap.c:79
void PIOS_IAP_SetRequest1(void)
Sets the 1st word of the request sequence.
Definition: pios_iap.c:74
RC Servo functions header.
static void FirmwareIAPCallback(const UAVObjEvent *ev, void *ctx, void *obj, int len)
FirmwareIAPCallback - callback function for firmware IAP requests.
Definition: firmwareiap.c:127
#define PIOS_LED_HEARTBEAT
Definition: pios_board.h:85
int32_t PIOS_SYS_SerialNumberGetBinary(uint8_t array[PIOS_SYS_SERIAL_NUM_BINARY_LEN])