LP-500 HoTT Telemetry added device definitions
[librepilot.git] / flight / targets / common / bootloader_updater / main.c
blob00dd28b8e1522eaa95f731169c8f2cd32aa8d637
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 <stdbool.h>
32 #include <pios_bl_helper.h>
33 #include <pios_board_init.h>
35 #define MAX_WRI_RETRYS 3
37 extern void FLASH_Download();
38 void error(int, int);
40 /* The ADDRESSES of the _binary_* symbols are the important
41 * data. This is non-intuitive for _binary_size where you
42 * might expect its value to hold the size but you'd be wrong.
44 extern uint32_t _binary_start;
45 extern uint32_t _binary_end;
46 extern uint32_t _binary_size;
47 const uint32_t *embedded_image_start = (uint32_t *)&(_binary_start);
48 const uint32_t *embedded_image_end = (uint32_t *)&(_binary_end);
49 const uint32_t embedded_image_size = (uint32_t)&(_binary_size);
51 int main()
53 PIOS_SYS_Init();
54 PIOS_Board_Init();
55 PIOS_LED_On(PIOS_LED_HEARTBEAT);
56 PIOS_DELAY_WaitmS(3000);
57 PIOS_LED_Off(PIOS_LED_HEARTBEAT);
59 /// Self overwrite check
60 uint32_t base_address = SCB->VTOR;
61 if ((BL_BANK_BASE + embedded_image_size) > base_address) {
62 error(PIOS_LED_HEARTBEAT, 1);
64 ///
67 * Make sure the bootloader we're carrying is for the same
68 * board type and board revision as the one we're running on.
70 * Assume the bootloader in flash and the bootloader contained in
71 * the updater both carry a board_info_blob at the end of the image.
74 /* Calculate how far the board_info_blob is from the beginning of the bootloader */
75 uint32_t board_info_blob_offset = (uint32_t)&pios_board_info_blob - (uint32_t)BL_BANK_BASE;
77 /* Use the same offset into our embedded bootloader image */
78 struct pios_board_info *new_board_info_blob = (struct pios_board_info *)((uint32_t)embedded_image_start + board_info_blob_offset);
80 /* Compare the two board info blobs to make sure they're for the same HW revision */
81 if ((pios_board_info_blob.magic != new_board_info_blob->magic)
82 #if PIOS_USB_BOARD_PRODUCT_ID == USB_PRODUCT_ID_SPARKY2
83 || (((pios_board_info_blob.board_type != new_board_info_blob->board_type)
84 || (pios_board_info_blob.board_rev != new_board_info_blob->board_rev))
85 && ((pios_board_info_blob.board_type != 0x0b) || (pios_board_info_blob.board_rev != 0x01)))
86 #else
87 || (pios_board_info_blob.board_type != new_board_info_blob->board_type)
88 || (pios_board_info_blob.board_rev != new_board_info_blob->board_rev)
89 #endif
90 ) {
91 error(PIOS_LED_HEARTBEAT, 2);
94 /* Embedded bootloader looks like it's the right one for this HW, proceed... */
96 FLASH_Unlock();
98 bool fail;
100 fail = (PIOS_BL_HELPER_FLASH_Erase_Bootloader() != 1);
102 if (fail == true) {
103 error(PIOS_LED_HEARTBEAT, 3);
106 /// Bootloader programing
107 for (uint32_t offset = 0; offset < embedded_image_size / sizeof(uint32_t); ++offset) {
108 bool result = false;
109 PIOS_LED_Toggle(PIOS_LED_HEARTBEAT);
110 for (uint8_t retry = 0; retry < MAX_WRI_RETRYS; ++retry) {
111 if (result == false) {
112 result = (FLASH_ProgramWord(BL_BANK_BASE + (offset * 4), embedded_image_start[offset]) == FLASH_COMPLETE);
115 if (result == false) {
116 error(PIOS_LED_HEARTBEAT, 4);
120 for (uint8_t x = 0; x < 3; ++x) {
121 PIOS_LED_On(PIOS_LED_HEARTBEAT);
122 PIOS_DELAY_WaitmS(1000);
123 PIOS_LED_Off(PIOS_LED_HEARTBEAT);
124 PIOS_DELAY_WaitmS(1000);
127 /// Invalidate the bootloader updater so we won't run
128 /// the update again on the next power cycle.
129 FLASH_ProgramWord(base_address, 0);
130 FLASH_Lock();
132 for (;;) {
133 PIOS_DELAY_WaitmS(1000);
137 void error(int led, int code)
139 for (;;) {
140 PIOS_DELAY_WaitmS(1000);
141 for (int x = 0; x < code; x++) {
142 PIOS_LED_On(led);
143 PIOS_DELAY_WaitmS(200);
144 PIOS_LED_Off(led);
145 PIOS_DELAY_WaitmS(1000);
147 PIOS_DELAY_WaitmS(3000);