From 78498910cf4ba71fc035f2145a2cced0b0116046 Mon Sep 17 00:00:00 2001 From: Alessio Morale Date: Sat, 27 Apr 2013 15:15:28 +0200 Subject: [PATCH] OP-917 Moved all the flash architecture related code to pios_bl_helper, get the flash addresses from board defines, major cleanup. +review OPReview --- flight/pios/inc/pios_bl_helper.h | 1 + flight/pios/stm32f10x/pios_bl_helper.c | 62 +++++--- flight/pios/stm32f10x/pios_sys.c | 3 + .../STM32F4xx/Source/osd/system_stm32f4xx.c | 2 +- .../Source/revolution/system_stm32f4xx.c | 2 +- .../Source/revoproto/system_stm32f4xx.c | 2 +- flight/pios/stm32f4xx/pios_bl_helper.c | 76 ++++++--- .../bootloader_updater/inc/pios_config.h | 1 + .../targets/common/bootloader_updater/main.c | 150 ++++++++---------- .../common/bootloader_updater/pios_board.c | 10 -- 10 files changed, 166 insertions(+), 143 deletions(-) diff --git a/flight/pios/inc/pios_bl_helper.h b/flight/pios/inc/pios_bl_helper.h index 620c6c7d9..7b527d95d 100644 --- a/flight/pios/inc/pios_bl_helper.h +++ b/flight/pios/inc/pios_bl_helper.h @@ -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 */ diff --git a/flight/pios/stm32f10x/pios_bl_helper.c b/flight/pios/stm32f10x/pios_bl_helper.c index f8c8e1db3..7fe351e17 100644 --- a/flight/pios/stm32f10x/pios_bl_helper.c +++ b/flight/pios/stm32f10x/pios_bl_helper.c @@ -35,44 +35,66 @@ #include #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() diff --git a/flight/pios/stm32f10x/pios_sys.c b/flight/pios/stm32f10x/pios_sys.c index 5a73d338b..9d52e34eb 100644 --- a/flight/pios/stm32f10x/pios_sys.c +++ b/flight/pios/stm32f10x/pios_sys.c @@ -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); diff --git a/flight/pios/stm32f4xx/libraries/CMSIS2/Device/ST/STM32F4xx/Source/osd/system_stm32f4xx.c b/flight/pios/stm32f4xx/libraries/CMSIS2/Device/ST/STM32F4xx/Source/osd/system_stm32f4xx.c index 65c30e1aa..9cb23aa19 100644 --- a/flight/pios/stm32f4xx/libraries/CMSIS2/Device/ST/STM32F4xx/Source/osd/system_stm32f4xx.c +++ b/flight/pios/stm32f4xx/libraries/CMSIS2/Device/ST/STM32F4xx/Source/osd/system_stm32f4xx.c @@ -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)); diff --git a/flight/pios/stm32f4xx/libraries/CMSIS2/Device/ST/STM32F4xx/Source/revolution/system_stm32f4xx.c b/flight/pios/stm32f4xx/libraries/CMSIS2/Device/ST/STM32F4xx/Source/revolution/system_stm32f4xx.c index bb7776d4b..65da26470 100644 --- a/flight/pios/stm32f4xx/libraries/CMSIS2/Device/ST/STM32F4xx/Source/revolution/system_stm32f4xx.c +++ b/flight/pios/stm32f4xx/libraries/CMSIS2/Device/ST/STM32F4xx/Source/revolution/system_stm32f4xx.c @@ -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)); diff --git a/flight/pios/stm32f4xx/libraries/CMSIS2/Device/ST/STM32F4xx/Source/revoproto/system_stm32f4xx.c b/flight/pios/stm32f4xx/libraries/CMSIS2/Device/ST/STM32F4xx/Source/revoproto/system_stm32f4xx.c index bb7776d4b..f98b4a14b 100644 --- a/flight/pios/stm32f4xx/libraries/CMSIS2/Device/ST/STM32F4xx/Source/revoproto/system_stm32f4xx.c +++ b/flight/pios/stm32f4xx/libraries/CMSIS2/Device/ST/STM32F4xx/Source/revoproto/system_stm32f4xx.c @@ -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)); diff --git a/flight/pios/stm32f4xx/pios_bl_helper.c b/flight/pios/stm32f4xx/pios_bl_helper.c index afa59b3e5..3458120c6 100644 --- a/flight/pios/stm32f4xx/pios_bl_helper.c +++ b/flight/pios/stm32f4xx/pios_bl_helper.c @@ -36,12 +36,16 @@ #include "stm32f4xx_flash.h" #include + 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, - §or_number, - §or_start, - §or_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, + §or_number, + §or_start, + §or_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() diff --git a/flight/targets/common/bootloader_updater/inc/pios_config.h b/flight/targets/common/bootloader_updater/inc/pios_config.h index f99347f25..ce6c6fc2b 100644 --- a/flight/targets/common/bootloader_updater/inc/pios_config.h +++ b/flight/targets/common/bootloader_updater/inc/pios_config.h @@ -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 */ diff --git a/flight/targets/common/bootloader_updater/main.c b/flight/targets/common/bootloader_updater/main.c index 6655c4904..387d3a16c 100644 --- a/flight/targets/common/bootloader_updater/main.c +++ b/flight/targets/common/bootloader_updater/main.c @@ -29,12 +29,14 @@ #include #include #include +#include #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); + } } diff --git a/flight/targets/common/bootloader_updater/pios_board.c b/flight/targets/common/bootloader_updater/pios_board.c index 184cf56c1..fe68d66aa 100644 --- a/flight/targets/common/bootloader_updater/pios_board.c +++ b/flight/targets/common/bootloader_updater/pios_board.c @@ -36,22 +36,12 @@ #include #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);