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 
30 /* Bootloader Includes */
31 #include <pios.h>
32 #include "pios_board_info.h"
33 
34 /* Prototype of PIOS_Board_Init() function */
35 extern void PIOS_Board_Init(void);
36 void error(int);
37 
38 /* The ADDRESSES of the _bu_payload_* symbols are the important data */
39 extern const uint32_t _bu_payload_start;
40 extern const uint32_t _bu_payload_end;
41 extern const uint32_t _bu_payload_size;
42 
43 int main(void) {
44 
45  PIOS_SYS_Init();
48  PIOS_DELAY_WaitmS(1000);
50 
51 #ifndef BU_DONT_CHECK_BOARDINFO
52  /*
53  * Make sure the bootloader we're carrying is for the same
54  * board type and board revision as the one we're running on.
55  *
56  * Assume the bootloader in flash and the bootloader contained in
57  * the updater both carry a board_info_blob at the end of the image.
58  */
59 
60  /* Calculate how far the board_info_blob is from the beginning of the bootloader */
61  uint32_t board_info_blob_offset = (uint32_t)&pios_board_info_blob - (uint32_t)0x08000000;
62 
63  /* Use the same offset into our embedded bootloader image */
64  struct pios_board_info * new_board_info_blob = (struct pios_board_info *)
65  ((uintptr_t)&_bu_payload_start + board_info_blob_offset);
66 
67  /* Compare the two board info blobs to make sure they're for the same HW revision */
68  if ((pios_board_info_blob.magic != new_board_info_blob->magic) ||
69  (pios_board_info_blob.board_type != new_board_info_blob->board_type) ||
70  (pios_board_info_blob.board_rev != new_board_info_blob->board_rev)) {
72  }
73 #endif
74 
75  /* Embedded bootloader looks like it's the right one for this HW, proceed... */
76 
77  uintptr_t bl_partition_id;
78  if (PIOS_FLASH_find_partition_id(FLASH_PARTITION_LABEL_BL, &bl_partition_id) != 0)
80 
81  /* Erase the partition */
83  PIOS_FLASH_start_transaction(bl_partition_id);
84  PIOS_FLASH_erase_partition(bl_partition_id);
85  PIOS_FLASH_end_transaction(bl_partition_id);
87 
88  /* Write in the new bootloader */
90  PIOS_FLASH_start_transaction(bl_partition_id);
91  PIOS_FLASH_write_data(bl_partition_id, 0, (uint8_t *)&_bu_payload_start, _bu_payload_size);
92  PIOS_FLASH_end_transaction(bl_partition_id);
94 
95  PIOS_DELAY_WaitmS(500);
96 
97  /* Invalidate the FW partition so that we don't run the updater again on next boot */
98  uintptr_t fw_partition_id;
99  if (PIOS_FLASH_find_partition_id(FLASH_PARTITION_LABEL_FW, &fw_partition_id) != 0)
101 
103  const uint32_t zero = 0;
104  PIOS_FLASH_start_transaction(fw_partition_id);
105  PIOS_FLASH_write_data(fw_partition_id, 0, (uint8_t *)&zero, sizeof(zero));
106  /*
107  * Invalidate FW metadata so GCS can know FW is invalid
108  * We replace the 3rd and 4th letters of the TlFw sentinel with 0
109  */
111  PIOS_FLASH_write_data(fw_partition_id, offset, (uint8_t *)&zero, 2);
112  PIOS_FLASH_end_transaction(fw_partition_id);
114 
115  PIOS_DELAY_WaitmS(100);
116 
117  /* Flash the LED to indicate finished */
118  for (uint8_t x = 0; x < 2; ++x) {
120  PIOS_DELAY_WaitmS(1000);
122  PIOS_DELAY_WaitmS(1000);
123  }
124 
125  PIOS_SYS_Reset();
126 
127  while(1); // just in case reset fails
128 }
129 
130 void error(int led) {
131  for (;;) {
132  PIOS_ANNUNC_On(led);
133  PIOS_DELAY_WaitmS(500);
134  PIOS_ANNUNC_Off(led);
135  PIOS_DELAY_WaitmS(500);
136  }
137 }
138 
int32_t PIOS_FLASH_find_partition_id(enum pios_flash_partition_labels label, uintptr_t *partition_id)
int main(void)
Definition: main.c:441
int32_t PIOS_FLASH_erase_partition(uintptr_t partition_id)
Main PiOS header to include all the compiled in PiOS options.
void PIOS_SYS_Init(void)
void PIOS_ANNUNC_Off(uint32_t annunc_id)
const struct pios_board_info pios_board_info_blob
void PIOS_Board_Init(void)
Definition: pios_board.c:44
int32_t PIOS_FLASH_write_data(uintptr_t partition_id, uint32_t offset, const uint8_t *data, uint16_t len)
int32_t PIOS_FLASH_end_transaction(uintptr_t partition_id)
const uint32_t _bu_payload_end
void PIOS_ANNUNC_On(uint32_t annunc_id)
int32_t PIOS_SYS_Reset(void)
const uint32_t _bu_payload_size
uint32_t offset
Definition: uavtalk_priv.h:51
const uint32_t _bu_payload_start
int32_t PIOS_FLASH_start_transaction(uintptr_t partition_id)
void error(int)
Definition: main.c:130
int32_t PIOS_DELAY_WaitmS(uint32_t mS)
Definition: pios_delay.c:140
#define PIOS_LED_HEARTBEAT
Definition: pios_board.h:85