2013-04-05 22:46:56 +02:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
* @addtogroup OpenPilotBL OpenPilot BootLoader
|
|
|
|
* @brief These files contain the code to the OpenPilot MB Bootloader.
|
|
|
|
*
|
|
|
|
* @{
|
|
|
|
* @file main.c
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
|
|
|
* @brief This is the file with the main function of the OpenPilot BootLoader
|
|
|
|
* @see The GNU Public License (GPL) Version 3
|
2013-05-19 16:37:30 +02:00
|
|
|
*
|
2013-04-05 22:46:56 +02:00
|
|
|
*****************************************************************************/
|
2013-05-19 16:37:30 +02:00
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2013-04-05 22:46:56 +02:00
|
|
|
* (at your option) any later version.
|
2013-05-19 16:37:30 +02:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
2013-04-05 22:46:56 +02:00
|
|
|
* for more details.
|
2013-05-19 16:37:30 +02:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
2013-04-05 22:46:56 +02:00
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
2013-04-16 22:03:08 +02:00
|
|
|
|
2013-04-05 22:46:56 +02:00
|
|
|
#include <pios.h>
|
2013-04-16 22:03:08 +02:00
|
|
|
#include <pios_board_info.h>
|
2013-04-05 22:46:56 +02:00
|
|
|
#include <stdbool.h>
|
2013-04-27 15:15:28 +02:00
|
|
|
#include <pios_bl_helper.h>
|
2013-04-05 22:46:56 +02:00
|
|
|
|
|
|
|
#define MAX_WRI_RETRYS 3
|
|
|
|
/* Prototype of PIOS_Board_Init() function */
|
|
|
|
extern void PIOS_Board_Init(void);
|
|
|
|
extern void FLASH_Download();
|
|
|
|
void error(int, int);
|
2013-04-27 15:15:28 +02:00
|
|
|
|
2013-04-05 22:46:56 +02:00
|
|
|
/* The ADDRESSES of the _binary_* symbols are the important
|
|
|
|
* data. This is non-intuitive for _binary_size where you
|
|
|
|
* might expect its value to hold the size but you'd be wrong.
|
|
|
|
*/
|
|
|
|
extern uint32_t _binary_start;
|
|
|
|
extern uint32_t _binary_end;
|
|
|
|
extern uint32_t _binary_size;
|
2013-05-19 16:37:30 +02:00
|
|
|
const uint32_t *embedded_image_start = (uint32_t *)&(_binary_start);
|
|
|
|
const uint32_t *embedded_image_end = (uint32_t *)&(_binary_end);
|
|
|
|
const uint32_t embedded_image_size = (uint32_t)&(_binary_size);
|
2013-04-05 22:46:56 +02:00
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
2013-04-27 15:15:28 +02:00
|
|
|
PIOS_SYS_Init();
|
|
|
|
PIOS_Board_Init();
|
|
|
|
PIOS_LED_On(PIOS_LED_HEARTBEAT);
|
|
|
|
PIOS_DELAY_WaitmS(3000);
|
|
|
|
PIOS_LED_Off(PIOS_LED_HEARTBEAT);
|
|
|
|
|
|
|
|
/// Self overwrite check
|
2013-05-19 16:37:30 +02:00
|
|
|
uint32_t base_address = SCB->VTOR;
|
|
|
|
if ((BL_BANK_BASE + embedded_image_size) > base_address) {
|
2013-04-27 15:15:28 +02:00
|
|
|
error(PIOS_LED_HEARTBEAT, 1);
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
2013-04-27 15:15:28 +02:00
|
|
|
///
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure the bootloader we're carrying is for the same
|
|
|
|
* board type and board revision as the one we're running on.
|
|
|
|
*
|
|
|
|
* Assume the bootloader in flash and the bootloader contained in
|
|
|
|
* the updater both carry a board_info_blob at the end of the image.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Calculate how far the board_info_blob is from the beginning of the bootloader */
|
2013-05-19 16:37:30 +02:00
|
|
|
uint32_t board_info_blob_offset = (uint32_t)&pios_board_info_blob - (uint32_t)BL_BANK_BASE;
|
2013-04-27 15:15:28 +02:00
|
|
|
|
|
|
|
/* Use the same offset into our embedded bootloader image */
|
2013-05-19 16:37:30 +02:00
|
|
|
struct pios_board_info *new_board_info_blob = (struct pios_board_info *)((uint32_t)embedded_image_start + board_info_blob_offset);
|
2013-04-27 15:15:28 +02:00
|
|
|
|
|
|
|
/* Compare the two board info blobs to make sure they're for the same HW revision */
|
|
|
|
if ((pios_board_info_blob.magic != new_board_info_blob->magic) ||
|
|
|
|
(pios_board_info_blob.board_type != new_board_info_blob->board_type) ||
|
|
|
|
(pios_board_info_blob.board_rev != new_board_info_blob->board_rev)) {
|
|
|
|
error(PIOS_LED_HEARTBEAT, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Embedded bootloader looks like it's the right one for this HW, proceed... */
|
|
|
|
|
|
|
|
FLASH_Unlock();
|
|
|
|
|
|
|
|
bool fail;
|
|
|
|
|
2013-04-28 10:19:08 +02:00
|
|
|
fail = (PIOS_BL_HELPER_FLASH_Erase_Bootloader() != 1);
|
2013-04-27 15:15:28 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (fail == true) {
|
2013-04-27 15:15:28 +02:00
|
|
|
error(PIOS_LED_HEARTBEAT, 3);
|
|
|
|
}
|
|
|
|
///
|
|
|
|
/// Bootloader programing
|
|
|
|
for (uint32_t offset = 0; offset < embedded_image_size / sizeof(uint32_t); ++offset) {
|
|
|
|
bool result = false;
|
|
|
|
PIOS_LED_Toggle(PIOS_LED_HEARTBEAT);
|
|
|
|
for (uint8_t retry = 0; retry < MAX_WRI_RETRYS; ++retry) {
|
|
|
|
if (result == false) {
|
2013-04-28 10:19:08 +02:00
|
|
|
result = (FLASH_ProgramWord(BL_BANK_BASE + (offset * 4), embedded_image_start[offset]) == FLASH_COMPLETE);
|
2013-04-27 15:15:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (result == false) {
|
|
|
|
error(PIOS_LED_HEARTBEAT, 4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
///
|
|
|
|
for (uint8_t x = 0; x < 3; ++x) {
|
|
|
|
PIOS_LED_On(PIOS_LED_HEARTBEAT);
|
|
|
|
PIOS_DELAY_WaitmS(1000);
|
|
|
|
PIOS_LED_Off(PIOS_LED_HEARTBEAT);
|
|
|
|
PIOS_DELAY_WaitmS(1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Invalidate the bootloader updater so we won't run
|
|
|
|
/// the update again on the next power cycle.
|
|
|
|
FLASH_ProgramWord(base_address, 0);
|
|
|
|
FLASH_Lock();
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
PIOS_DELAY_WaitmS(1000);
|
|
|
|
}
|
2013-04-05 22:46:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void error(int led, int code)
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
for (;;) {
|
|
|
|
PIOS_DELAY_WaitmS(1000);
|
|
|
|
for (int x = 0; x < code; x++) {
|
|
|
|
PIOS_LED_On(led);
|
|
|
|
PIOS_DELAY_WaitmS(200);
|
|
|
|
PIOS_LED_Off(led);
|
|
|
|
PIOS_DELAY_WaitmS(1000);
|
|
|
|
}
|
|
|
|
PIOS_DELAY_WaitmS(3000);
|
|
|
|
}
|
2013-04-27 00:51:52 +02:00
|
|
|
}
|