Fixed remaining issues with flash organization and writing

This commit is contained in:
Hierophect 2019-08-13 18:05:40 -04:00
parent 300dc68955
commit c4c55fffb1
4 changed files with 116 additions and 182 deletions

View File

@ -1,189 +1,109 @@
/* /*
****************************************************************************** GNU linker script for STM32F411 via Micropython
**
** File : LinkerScript.ld
**
** Author : Auto-generated by Ac6 System Workbench
**
** Abstract : Linker script for STM32F411VETx series
** 512Kbytes FLASH and 128Kbytes RAM
**
** Set heap size, stack size and stack location according
** to application requirements.
**
** Set memory bank area and size if external memory is used.
**
** Target : STMicroelectronics STM32
**
** Distribution: The file is distributed “as is,” without any warranty
** of any kind.
**
*****************************************************************************
** @attention
**
** <h2><center>&copy; COPYRIGHT(c) 2014 Ac6</center></h2>
**
** Redistribution and use in source and binary forms, with or without modification,
** are permitted provided that the following conditions are met:
** 1. Redistributions of source code must retain the above copyright notice,
** this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright notice,
** this list of conditions and the following disclaimer in the documentation
** and/or other materials provided with the distribution.
** 3. Neither the name of Ac6 nor the names of its contributors
** may be used to endorse or promote products derived from this software
** without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
** DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**
*****************************************************************************
*/ */
/* Entry Point */
ENTRY(Reset_Handler)
/* Highest address of the user mode stack */
_estack = 0x20020000; /* end of RAM */
/* Generate a link error if heap and stack don't fit into RAM */
_Min_Heap_Size = 0x200; /* required amount of heap */
_Min_Stack_Size = 0x400; /* required amount of stack */
/* Specify the memory areas */ /* Specify the memory areas */
MEMORY MEMORY
{ {
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 512K /* entire flash */
FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 512K - 256 - 100K FLASH_ISR (rx) : ORIGIN = 0x08000000, LENGTH = 16K /* sector 0 */
FLASH_FS (rx) : ORIGIN = 0x08004000, LENGTH = 112K /* sectors 1,2,3 are 16K, 4 is 64K */
FLASH_TEXT (rx) : ORIGIN = 0x08020000, LENGTH = 384K /* sectors 5,6,7 are 128K */
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K
} }
/* Define output sections */ /* produce a link error if there is not this amount of RAM for these sections */
_minimum_stack_size = 2K;
_minimum_heap_size = 16K;
/* Define tho top end of the stack. The stack is full descending so begins just
above last byte of RAM. Note that EABI requires the stack to be 8-byte
aligned for a call. */
_estack = ORIGIN(RAM) + LENGTH(RAM);
/* RAM extents for the garbage collector */
_ram_start = ORIGIN(RAM);
_ram_end = ORIGIN(RAM) + LENGTH(RAM);
_heap_start = _ebss; /* heap starts just after statically allocated memory */
_heap_end = 0x2001c000; /* tunable */
ENTRY(Reset_Handler)
/* define output sections */
SECTIONS SECTIONS
{ {
/* The startup code goes first into FLASH */ /* The startup code goes first into FLASH */
.isr_vector : .isr_vector :
{ {
. = ALIGN(4); . = ALIGN(4);
KEEP(*(.isr_vector)) /* Startup code */ KEEP(*(.isr_vector)) /* Startup code */
. = ALIGN(4);
} >FLASH
/* The program code and other data goes into FLASH */ /* This first flash block is 16K annd the isr vectors only take up
.text : about 400 bytes. Micropython pads this with files, but this didn't
{ work with the size of Circuitpython's ff object. */
. = ALIGN(4);
*(.text) /* .text sections (code) */
*(.text*) /* .text* sections (code) */
*(.glue_7) /* glue arm to thumb code */
*(.glue_7t) /* glue thumb to arm code */
*(.eh_frame)
KEEP (*(.init)) . = ALIGN(4);
KEEP (*(.fini)) } >FLASH_ISR
. = ALIGN(4); /* The program code and other data goes into FLASH */
_etext = .; /* define a global symbols at end of code */ .text :
} >FLASH {
. = ALIGN(4);
*(.text*) /* .text* sections (code) */
*(.rodata*) /* .rodata* sections (constants, strings, etc.) */
/* *(.glue_7) */ /* glue arm to thumb code */
/* *(.glue_7t) */ /* glue thumb to arm code */
/* Constant data goes into FLASH */ . = ALIGN(4);
.rodata : _etext = .; /* define a global symbol at end of code */
{ } >FLASH_TEXT
. = ALIGN(4);
*(.rodata) /* .rodata sections (constants, strings, etc.) */
*(.rodata*) /* .rodata* sections (constants, strings, etc.) */
. = ALIGN(4);
} >FLASH
.ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) } >FLASH /* used by the startup to initialize data */
.ARM : { _sidata = LOADADDR(.data);
__exidx_start = .;
*(.ARM.exidx*)
__exidx_end = .;
} >FLASH
.preinit_array : /* This is the initialized data section
{ The program executes knowing that the data is in the RAM
PROVIDE_HIDDEN (__preinit_array_start = .); but the loader puts the initial values in the FLASH (inidata).
KEEP (*(.preinit_array*)) It is one task of the startup to copy the initial values from FLASH to RAM. */
PROVIDE_HIDDEN (__preinit_array_end = .); .data :
} >FLASH {
.init_array : . = ALIGN(4);
{ _sdata = .; /* create a global symbol at data start; used by startup code in order to initialise the .data section in RAM */
PROVIDE_HIDDEN (__init_array_start = .); *(.data*) /* .data* sections */
KEEP (*(SORT(.init_array.*)))
KEEP (*(.init_array*))
PROVIDE_HIDDEN (__init_array_end = .);
} >FLASH
.fini_array :
{
PROVIDE_HIDDEN (__fini_array_start = .);
KEEP (*(SORT(.fini_array.*)))
KEEP (*(.fini_array*))
PROVIDE_HIDDEN (__fini_array_end = .);
} >FLASH
/* used by the startup to initialize data */ . = ALIGN(4);
_sidata = LOADADDR(.data); _edata = .; /* define a global symbol at data end; used by startup code in order to initialise the .data section in RAM */
} >RAM AT> FLASH_TEXT
/* Initialized data sections goes into RAM, load LMA copy after code */ /* Uninitialized data section */
.data : .bss :
{ {
. = ALIGN(4); . = ALIGN(4);
_sdata = .; /* create a global symbol at data start */ _sbss = .; /* define a global symbol at bss start; used by startup code */
*(.data) /* .data sections */ *(.bss*)
*(.data*) /* .data* sections */ *(COMMON)
. = ALIGN(4); . = ALIGN(4);
_edata = .; /* define a global symbol at data end */ _ebss = .; /* define a global symbol at bss end; used by startup code and GC */
} >RAM AT> FLASH } >RAM
/* this is to define the start of the heap, and make sure we have a minimum size */
/* Uninitialized data section */ .heap :
. = ALIGN(4); {
.bss : . = ALIGN(4);
{ . = . + _minimum_heap_size;
/* This is used by the startup in order to initialize the .bss secion */ . = ALIGN(4);
_sbss = .; /* define a global symbol at bss start */ } >RAM
__bss_start__ = _sbss;
*(.bss)
*(.bss*)
*(COMMON)
. = ALIGN(4); /* this just checks there is enough RAM for the stack */
_ebss = .; /* define a global symbol at bss end */ .stack :
__bss_end__ = _ebss; {
} >RAM . = ALIGN(4);
. = . + _minimum_stack_size;
. = ALIGN(4);
} >RAM
/* User_heap_stack section, used to check that there is enough RAM left */ .ARM.attributes 0 : { *(.ARM.attributes) }
._user_heap_stack :
{
. = ALIGN(8);
PROVIDE ( end = . );
PROVIDE ( _end = . );
. = . + _Min_Heap_Size;
. = . + _Min_Stack_Size;
. = ALIGN(8);
} >RAM
/* Remove information from the standard libraries */
/DISCARD/ :
{
libc.a ( * )
libm.a ( * )
libgcc.a ( * )
}
.ARM.attributes 0 : { *(.ARM.attributes) }
} }

View File

@ -37,6 +37,6 @@
#define CIRCUITPY_INTERNAL_NVM_SIZE 256 #define CIRCUITPY_INTERNAL_NVM_SIZE 256
#define BOARD_FLASH_SIZE (0x8080000 - 0x2000 - 0x019000 - CIRCUITPY_INTERNAL_NVM_SIZE) #define BOARD_FLASH_SIZE (FLASH_SIZE - 0x2000 - 0x01C000 - CIRCUITPY_INTERNAL_NVM_SIZE)
#define AUTORESET_DELAY_MS 500 #define AUTORESET_DELAY_MS 500

View File

@ -1,4 +1,4 @@
/* /*f
* This file is part of the MicroPython project, http://micropython.org/ * This file is part of the MicroPython project, http://micropython.org/
* *
* The MIT License (MIT) * The MIT License (MIT)
@ -61,6 +61,8 @@ static const flash_layout_t flash_layout[] = {
#endif #endif
}; };
static uint8_t sector_copy[0x4000] __attribute__((aligned(4)));
//Return the sector of a given flash address. //Return the sector of a given flash address.
uint32_t flash_get_sector_info(uint32_t addr, uint32_t *start_addr, uint32_t *size) { uint32_t flash_get_sector_info(uint32_t addr, uint32_t *start_addr, uint32_t *size) {
if (addr >= flash_layout[0].base_address) { if (addr >= flash_layout[0].base_address) {
@ -127,19 +129,31 @@ bool supervisor_flash_write_block(const uint8_t *src, uint32_t block) {
return false; return false;
} }
// unlock // unlock flash
HAL_FLASH_Unlock(); HAL_FLASH_Unlock();
// set up for erase
FLASH_EraseInitTypeDef EraseInitStruct; FLASH_EraseInitTypeDef EraseInitStruct;
// erase the sector(s)
EraseInitStruct.TypeErase = TYPEERASE_SECTORS; EraseInitStruct.TypeErase = TYPEERASE_SECTORS;
EraseInitStruct.VoltageRange = VOLTAGE_RANGE_3; // voltage range needs to be 2.7V to 3.6V EraseInitStruct.VoltageRange = VOLTAGE_RANGE_3; // voltage range needs to be 2.7V to 3.6V
//get the sector number // get the sector information
EraseInitStruct.Sector = flash_get_sector_info(dest, NULL, NULL); uint32_t sector_size;
//find end address, subtract for number of sectors uint32_t sector_start_addr;
EraseInitStruct.NbSectors = flash_get_sector_info(dest + FILESYSTEM_BLOCK_SIZE - 1, NULL, NULL) - EraseInitStruct.Sector + 1; EraseInitStruct.Sector = flash_get_sector_info(dest, &sector_start_addr, &sector_size);
EraseInitStruct.NbSectors = 1;
if (sector_size>0x4000) return false;
// copy the sector
memcpy(sector_copy,(void *)sector_start_addr,sector_size);
// // overwrite sector data
memcpy(sector_copy+(dest-sector_start_addr),src,FILESYSTEM_BLOCK_SIZE);
// find end address, subtract for number of sectors
// Shouldn't be required since blocks will always fit in a single sector, they should never overlap
//EraseInitStruct.NbSectors = flash_get_sector_info(dest + FILESYSTEM_BLOCK_SIZE - 1, NULL, NULL) - EraseInitStruct.Sector + 1;
// erase the sector
uint32_t SectorError = 0; uint32_t SectorError = 0;
if (HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK) { if (HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK) {
// error occurred during sector erase // error occurred during sector erase
@ -157,16 +171,15 @@ bool supervisor_flash_write_block(const uint8_t *src, uint32_t block) {
__HAL_FLASH_INSTRUCTION_CACHE_ENABLE(); __HAL_FLASH_INSTRUCTION_CACHE_ENABLE();
__HAL_FLASH_DATA_CACHE_ENABLE(); __HAL_FLASH_DATA_CACHE_ENABLE();
// program the flash word by word // reprogram the sector
for (int i = 0; i < (FILESYSTEM_BLOCK_SIZE / 4); i++) { for (int i = 0; i < sector_size; i++) {
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, dest, *src) != HAL_OK) { if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, sector_start_addr, (uint64_t)sector_copy[i]) != HAL_OK) {
// error occurred during flash write // error occurred during flash write
HAL_FLASH_Lock(); // lock the flash HAL_FLASH_Lock(); // lock the flash
mp_printf(&mp_plat_print, "FLASH WRITE ERROR"); mp_printf(&mp_plat_print, "FLASH WRITE ERROR");
return false; return false;
} }
dest += 4; sector_start_addr += 1;
src += 1; //src += 4;
} }
// lock the flash // lock the flash

View File

@ -33,17 +33,18 @@
#ifdef STM32F411xE #ifdef STM32F411xE
#define STM32_FLASH_SIZE 0x80000 //512KiB #define STM32_FLASH_SIZE 0x80000 //512KiB
#define INTERNAL_FLASH_FILESYSTEM_SIZE 0x19000 //100KiB #define INTERNAL_FLASH_FILESYSTEM_SIZE 0x1C000 //112KiB
#endif #endif
#ifdef STM32F412Zx #ifdef STM32F412Zx
#define STM32_FLASH_SIZE 0x100000 //512KiB #define STM32_FLASH_SIZE 0x100000 //1MB
#define INTERNAL_FLASH_FILESYSTEM_SIZE 0x19000 //100KiB #define INTERNAL_FLASH_FILESYSTEM_SIZE 0x1C000 //112KiB
#endif #endif
#define STM32_FLASH_OFFSET 0x8000000 //All STM32 chips map to this flash location #define STM32_FLASH_OFFSET 0x8000000 //All STM32 chips map to this flash location
#define INTERNAL_FLASH_FILESYSTEM_START_ADDR ((STM32_FLASH_SIZE + STM32_FLASH_OFFSET) - INTERNAL_FLASH_FILESYSTEM_SIZE - CIRCUITPY_INTERNAL_NVM_SIZE) #define INTERNAL_FLASH_FILESYSTEM_START_ADDR 0x08004000
//#define INTERNAL_FLASH_FILESYSTEM_START_ADDR ((STM32_FLASH_SIZE + STM32_FLASH_OFFSET) - INTERNAL_FLASH_FILESYSTEM_SIZE - CIRCUITPY_INTERNAL_NVM_SIZE)
#define INTERNAL_FLASH_FILESYSTEM_NUM_BLOCKS (INTERNAL_FLASH_FILESYSTEM_SIZE / FILESYSTEM_BLOCK_SIZE) #define INTERNAL_FLASH_FILESYSTEM_NUM_BLOCKS (INTERNAL_FLASH_FILESYSTEM_SIZE / FILESYSTEM_BLOCK_SIZE)
#define INTERNAL_FLASH_SYSTICK_MASK (0x1ff) // 512ms #define INTERNAL_FLASH_SYSTICK_MASK (0x1ff) // 512ms