1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2024-12-01 09:24:10 +01:00

OP-917 Moved all the flash architecture related code to pios_bl_helper, get the flash addresses from board defines, major cleanup.

+review OPReview
This commit is contained in:
Alessio Morale 2013-04-27 15:15:28 +02:00
parent 2e6835a2a3
commit 78498910cf
10 changed files with 166 additions and 143 deletions

View File

@ -36,6 +36,7 @@ extern uint8_t PIOS_BL_HELPER_FLASH_Ini();
extern uint32_t PIOS_BL_HELPER_CRC_Memory_Calc();
extern void PIOS_BL_HELPER_FLASH_Read_Description(uint8_t * array, uint8_t size);
extern uint8_t PIOS_BL_HELPER_FLASH_Start();
extern uint8_t PIOS_BL_HELPER_FLASH_ERASE_BOOTLOADER();
extern void PIOS_BL_HELPER_CRC_Ini();
#endif /* PIOS_BL_HELPER_H */

View File

@ -35,44 +35,66 @@
#include <pios_board_info.h>
#include "stm32f10x_flash.h"
uint8_t *PIOS_BL_HELPER_FLASH_If_Read(uint32_t SectorAddress)
{
return (uint8_t *) (SectorAddress);
}
#if defined(PIOS_INCLUDE_BL_HELPER_WRITE_SUPPORT)
static bool erase_flash(uint32_t startAddress, uint32_t endAddress);
uint8_t PIOS_BL_HELPER_FLASH_Ini()
{
FLASH_Unlock();
return 1;
FLASH_Unlock();
return 1;
}
uint8_t PIOS_BL_HELPER_FLASH_Start()
{
const struct pios_board_info * bdinfo = &pios_board_info_blob;
uint32_t pageAdress = bdinfo->fw_base;
uint8_t fail = FALSE;
while ((pageAdress < (bdinfo->fw_base + bdinfo->fw_size + bdinfo->desc_size))
|| (fail == TRUE)) {
for (int retry = 0; retry < MAX_DEL_RETRYS; ++retry) {
if (FLASH_ErasePage(pageAdress) == FLASH_COMPLETE) {
fail = FALSE;
break;
} else {
fail = TRUE;
}
const struct pios_board_info * bdinfo = &pios_board_info_blob;
uint32_t startAddress = bdinfo->fw_base;
uint32_t endAddress = bdinfo->fw_base + bdinfo->fw_size + bdinfo->desc_size;
}
bool success = erase_flash(startAddress, endAddress);
return (success) ? 1 : 0;
}
uint8_t PIOS_BL_HELPER_FLASH_ERASE_BOOTLOADER() {
/// Bootloader memory space erase
uint32_t startAddress = BL_BANK_BASE;
uint32_t endAddress = BL_BANK_BASE + BL_BANK_SIZE;
bool success = erase_flash(startAddress, endAddress);
return (success) ? 1 : 0;
}
static bool erase_flash(uint32_t startAddress, uint32_t endAddress) {
uint32_t pageAdress = startAddress;
uint8_t fail = FALSE;
while ((pageAdress < endAddress)
|| (fail == TRUE)) {
for (int retry = 0; retry < MAX_DEL_RETRYS; ++retry) {
if (FLASH_ErasePage(pageAdress) == FLASH_COMPLETE) {
fail = FALSE;
break;
} else {
fail = TRUE;
}
}
#ifdef STM32F10X_HD
pageAdress += 2048;
pageAdress += 2048;
#elif defined (STM32F10X_MD)
pageAdress += 1024;
pageAdress += 1024;
#endif
}
return (fail == TRUE) ? 0 : 1;
}
return !fail;
}
#endif
uint32_t PIOS_BL_HELPER_CRC_Memory_Calc()

View File

@ -50,6 +50,9 @@ void PIOS_SYS_Init(void)
/* Setup STM32 system (RCC, clock, PLL and Flash configuration) - CMSIS Function */
SystemInit();
/* Init the delay system */
PIOS_DELAY_Init();
/* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE and AFIO clocks */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE |
RCC_APB2Periph_AFIO, ENABLE);

View File

@ -392,7 +392,7 @@ static void SetSysClock(void)
}
/* Configure Flash prefetch, Instruction cache, Data cache and wait state */
FLASH->ACR = FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_3WS;
FLASH->ACR = FLASH_ACR_PRFTEN | FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_3WS;
/* Select the main PLL as system clock source */
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));

View File

@ -393,7 +393,7 @@ static void SetSysClock(void)
}
/* Configure Flash prefetch, Instruction cache, Data cache and wait state */
FLASH->ACR = FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_5WS;
FLASH->ACR = FLASH_ACR_PRFTEN | FLASH_ACR_ICEN | FLASH_ACR_DCEN | FLASH_ACR_LATENCY_5WS;
/* Select the main PLL as system clock source */
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));

View File

@ -393,7 +393,7 @@ static void SetSysClock(void)
}
/* Configure Flash prefetch, Instruction cache, Data cache and wait state */
FLASH->ACR = FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_5WS;
FLASH->ACR = FLASH_ACR_PRFTEN | FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_5WS;
/* Select the main PLL as system clock source */
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));

View File

@ -36,12 +36,16 @@
#include "stm32f4xx_flash.h"
#include <stdbool.h>
uint8_t *PIOS_BL_HELPER_FLASH_If_Read(uint32_t SectorAddress)
{
return (uint8_t *) (SectorAddress);
}
#if defined(PIOS_INCLUDE_BL_HELPER_WRITE_SUPPORT)
static bool erase_flash(uint32_t startAddress, uint32_t endAddress);
uint8_t PIOS_BL_HELPER_FLASH_Ini()
{
FLASH_Unlock();
@ -138,35 +142,55 @@ static bool PIOS_BL_HELPER_FLASH_GetSectorInfo(uint32_t address, uint8_t * secto
uint8_t PIOS_BL_HELPER_FLASH_Start()
{
const struct pios_board_info * bdinfo = &pios_board_info_blob;
uint32_t pageAddress = bdinfo->fw_base;
bool fail = false;
while ((pageAddress < (bdinfo->fw_base + bdinfo->fw_size + bdinfo->desc_size))
&& (fail == false)) {
uint8_t sector_number;
uint32_t sector_start;
uint32_t sector_size;
if (!PIOS_BL_HELPER_FLASH_GetSectorInfo(pageAddress,
&sector_number,
&sector_start,
&sector_size)) {
/* We're asking for an invalid flash address */
PIOS_Assert(0);
}
for (int retry = 0; retry < MAX_DEL_RETRYS; ++retry) {
if (FLASH_EraseSector(sector_number, VoltageRange_3) == FLASH_COMPLETE) {
fail = false;
break;
} else {
fail = true;
}
uint32_t startAddress = bdinfo->fw_base;
uint32_t endAddress = bdinfo->fw_base + bdinfo->fw_size + bdinfo->desc_size;
}
/* Move to the next sector */
pageAddress += sector_size;
}
bool success = erase_flash(startAddress, endAddress);
return (fail == true) ? 0 : 1;
return (success) ? 1 : 0;
}
uint8_t PIOS_BL_HELPER_FLASH_ERASE_BOOTLOADER() {
/// Bootloader memory space erase
uint32_t startAddress = BL_BANK_BASE;
uint32_t endAddress = BL_BANK_BASE + BL_BANK_SIZE;
bool success = erase_flash(startAddress, endAddress);
return (success) ? 1 : 0;
}
static bool erase_flash(uint32_t startAddress, uint32_t endAddress)
{
uint32_t pageAddress = startAddress;
bool fail = false;
while ((pageAddress < endAddress)
&& (fail == false)) {
uint8_t sector_number;
uint32_t sector_start;
uint32_t sector_size;
if (!PIOS_BL_HELPER_FLASH_GetSectorInfo(pageAddress,
&sector_number,
&sector_start,
&sector_size)) {
/* We're asking for an invalid flash address */
PIOS_Assert(0);
}
for (int retry = 0; retry < MAX_DEL_RETRYS; ++retry) {
if (FLASH_EraseSector(sector_number, VoltageRange_3) == FLASH_COMPLETE) {
fail = false;
break;
} else {
fail = true;
}
}
/* Move to the next sector */
pageAddress += sector_size;
}
return !fail;
}
#endif
uint32_t PIOS_BL_HELPER_CRC_Memory_Calc()

View File

@ -37,6 +37,7 @@
//#define PIOS_INCLUDE_GPIO
#define PIOS_INCLUDE_LED
#define PIOS_INCLUDE_BL_HELPER
#define PIOS_INCLUDE_BL_HELPER_WRITE_SUPPORT
#endif /* PIOS_CONFIG_H */

View File

@ -29,12 +29,14 @@
#include <pios.h>
#include <pios_board_info.h>
#include <stdbool.h>
#include <pios_bl_helper.h>
#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);
#ifdef STM32F4XX
#define FLASH_ErasePage(x) FLASH_EraseSector((x) > 0x08000000 ? FLASH_Sector_1 : FLASH_Sector_0, VoltageRange_3)
#endif
@ -45,107 +47,87 @@ void error(int, int);
extern uint32_t _binary_start;
extern uint32_t _binary_end;
extern uint32_t _binary_size;
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_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);
int main()
{
PIOS_SYS_Init();
PIOS_Board_Init();
PIOS_LED_On(PIOS_LED_HEARTBEAT);
PIOS_DELAY_WaitmS(3000);
PIOS_LED_Off(PIOS_LED_HEARTBEAT);
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
uint32_t base_address = SCB->VTOR;
if ((BL_BANK_BASE + embedded_image_size) > base_address)
error(PIOS_LED_HEARTBEAT, 1);
///
/// Self overwrite check
uint32_t base_address = SCB ->VTOR;
if ((BL_BANK_BASE + embedded_image_size) > base_address)
error(PIOS_LED_HEARTBEAT, 1);
///
/*
* 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.
*/
/*
* 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 */
uint32_t board_info_blob_offset = (uint32_t) &pios_board_info_blob - (uint32_t)BL_BANK_BASE;
/* Calculate how far the board_info_blob is from the beginning of the bootloader */
uint32_t board_info_blob_offset = (uint32_t) &pios_board_info_blob - (uint32_t) BL_BANK_BASE;
/* Use the same offset into our embedded bootloader image */
struct pios_board_info * new_board_info_blob = (struct pios_board_info *)
((uint32_t)embedded_image_start + board_info_blob_offset);
/* Use the same offset into our embedded bootloader image */
struct pios_board_info *new_board_info_blob = (struct pios_board_info *) ((uint32_t) embedded_image_start + board_info_blob_offset);
/* 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);
}
/* 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... */
/* Embedded bootloader looks like it's the right one for this HW, proceed... */
FLASH_Unlock();
FLASH_Unlock();
/// Bootloader memory space erase
uint32_t pageAddress;
pageAddress = BL_BANK_BASE;
bool fail = false;
while ((pageAddress < BL_BANK_BASE + BL_BANK_SIZE) && (fail == false)) {
for (int retry = 0; retry < MAX_DEL_RETRYS; ++retry) {
if (FLASH_ErasePage(pageAddress) == FLASH_COMPLETE) {
fail = false;
break;
} else {
fail = true;
}
}
#ifdef STM32F10X_HD
pageAddress += 2048;
#elif defined (STM32F10X_MD)
pageAddress += 1024;
#elif defined (STM32F4XX)
pageAddress += 0x4000;
#endif
}
bool fail;
if (fail == true)
error(PIOS_LED_HEARTBEAT, 3);
fail = (PIOS_BL_HELPER_FLASH_ERASE_BOOTLOADER() != 1);
if (fail == true){
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) {
result = (FLASH_ProgramWord(0x08000000 + (offset * 4), embedded_image_start[offset]) == FLASH_COMPLETE);
}
}
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);
}
///
/// 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) {
result = (FLASH_ProgramWord(0x08000000 + (offset * 4), embedded_image_start[offset])
== FLASH_COMPLETE) ? true : false;
}
}
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();
/// 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);
}
for (;;) {
PIOS_DELAY_WaitmS(1000);
}
}

View File

@ -36,22 +36,12 @@
#include <board_hw_defs.c>
#ifdef STM32F4XX
#define FLASH_ErasePage(x) FLASH_EraseSector(x,VoltageRange_3)
#define FLASH_PrefetchBuffer_Enable ENABLE
#endif
void PIOS_Board_Init(void) {
const struct pios_board_info * bdinfo = &pios_board_info_blob;
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* Flash 2 wait state */
//FLASH_SetLatency(FLASH_Latency_5);
/* Delay system */
PIOS_DELAY_Init();
/* LEDs */
#if defined(PIOS_INCLUDE_LED)
const struct pios_led_cfg * led_cfg = PIOS_BOARD_HW_DEFS_GetLedCfg(bdinfo->board_rev);