From ad339509666428046f9f7d8a95bb5c2ca5760a6e Mon Sep 17 00:00:00 2001 From: Hierophect Date: Fri, 27 Sep 2019 11:00:09 -0400 Subject: [PATCH] Flash memory changes - non functional --- ports/stm32f4/Makefile | 1 + ports/stm32f4/boards/STM32F412ZGTx_FLASH.ld | 4 +-- .../stm32f4xx_hal_conf.h | 3 +- ports/stm32f4/common-hal/busio/SPI.c | 11 +++--- ports/stm32f4/common-hal/busio/SPI.h | 3 ++ ports/stm32f4/peripherals/stm32f4/periph.h | 2 +- .../peripherals/stm32f4/stm32f412zx/periph.c | 2 +- ports/stm32f4/supervisor/internal_flash.c | 35 ++++++++++++++++--- ports/stm32f4/supervisor/internal_flash.h | 2 +- 9 files changed, 45 insertions(+), 18 deletions(-) diff --git a/ports/stm32f4/Makefile b/ports/stm32f4/Makefile index ed396fea7c..7cee0d8e79 100755 --- a/ports/stm32f4/Makefile +++ b/ports/stm32f4/Makefile @@ -137,6 +137,7 @@ SRC_STM32 = \ stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_fsmc.c \ stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_sram.c \ stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c.c \ + stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_spi.c \ stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_i2c.c \ stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_dma.c \ stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2s.c \ diff --git a/ports/stm32f4/boards/STM32F412ZGTx_FLASH.ld b/ports/stm32f4/boards/STM32F412ZGTx_FLASH.ld index e0586709e6..4774e7768d 100644 --- a/ports/stm32f4/boards/STM32F412ZGTx_FLASH.ld +++ b/ports/stm32f4/boards/STM32F412ZGTx_FLASH.ld @@ -7,8 +7,8 @@ MEMORY { FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 1024K /* entire flash */ FLASH_ISR (rx) : ORIGIN = 0x08000000, LENGTH = 16K /* sector 0 */ - FLASH_FS (rx) : ORIGIN = 0x08004000, LENGTH = 48K /* sectors 1,2,3 are 16K */ - FLASH_TEXT (rx) : ORIGIN = 0x08010000, LENGTH = 960K /* sector 4 is 64K, sectors 5,6,7 are 128K */ + FLASH_FS (rx) : ORIGIN = 0x08004000, LENGTH = 112K /* sectors 1,2,3 are 16K */ + FLASH_TEXT (rx) : ORIGIN = 0x08010000, LENGTH = 896K /* sector 4 is 64K, sectors 5,6,7 are 128K */ RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 256K } diff --git a/ports/stm32f4/boards/stm32f412zg_discovery/stm32f4xx_hal_conf.h b/ports/stm32f4/boards/stm32f412zg_discovery/stm32f4xx_hal_conf.h index dbfd423d06..2ad0fee20b 100644 --- a/ports/stm32f4/boards/stm32f412zg_discovery/stm32f4xx_hal_conf.h +++ b/ports/stm32f4/boards/stm32f412zg_discovery/stm32f4xx_hal_conf.h @@ -60,7 +60,7 @@ /* #define HAL_SAI_MODULE_ENABLED */ #define HAL_SD_MODULE_ENABLED /* #define HAL_MMC_MODULE_ENABLED */ -/* #define HAL_SPI_MODULE_ENABLED */ +#define HAL_SPI_MODULE_ENABLED /* #define HAL_TIM_MODULE_ENABLED */ #define HAL_UART_MODULE_ENABLED /* #define HAL_USART_MODULE_ENABLED */ @@ -70,7 +70,6 @@ #define HAL_PCD_MODULE_ENABLED /* #define HAL_HCD_MODULE_ENABLED */ /* #define HAL_DSI_MODULE_ENABLED */ -/* #define HAL_QSPI_MODULE_ENABLED */ #define HAL_QSPI_MODULE_ENABLED /* #define HAL_CEC_MODULE_ENABLED */ /* #define HAL_FMPI2C_MODULE_ENABLED */ diff --git a/ports/stm32f4/common-hal/busio/SPI.c b/ports/stm32f4/common-hal/busio/SPI.c index ffc23ab831..d65f44f470 100644 --- a/ports/stm32f4/common-hal/busio/SPI.c +++ b/ports/stm32f4/common-hal/busio/SPI.c @@ -37,7 +37,6 @@ STATIC bool reserved_spi[6]; void spi_reset(void) { - //Note: I2Cs are also forcibly reset in construct, due to silicon error #ifdef SPI1 reserved_spi[0] = false; __HAL_RCC_SPI1_CLK_DISABLE(); @@ -65,10 +64,10 @@ void spi_reset(void) { } void common_hal_busio_spi_construct(busio_spi_obj_t *self, - const mcu_pin_obj_t * clock, const mcu_pin_obj_t * mosi, + const mcu_pin_obj_t * sck, const mcu_pin_obj_t * mosi, const mcu_pin_obj_t * miso) { - //match pins to I2C objects + //match pins to SPI objects SPI_TypeDef * SPIx; uint8_t sck_len = sizeof(mcu_spi_sck_list)/sizeof(*mcu_spi_sck_list); @@ -77,7 +76,7 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self, //sck for(uint i=0; iclk!=NULL && self->mosi!=NULL && self->miso!=NULL ) { - SPIx = mcu_spi_banks[self->clk->spi_index-1]; + if(self->sck!=NULL && self->mosi!=NULL && self->miso!=NULL ) { + SPIx = mcu_spi_banks[self->sck->spi_index-1]; } else { mp_raise_RuntimeError(translate("Invalid SPI pin selection")); } diff --git a/ports/stm32f4/common-hal/busio/SPI.h b/ports/stm32f4/common-hal/busio/SPI.h index bb6c7bc470..cc9af6e620 100644 --- a/ports/stm32f4/common-hal/busio/SPI.h +++ b/ports/stm32f4/common-hal/busio/SPI.h @@ -29,6 +29,9 @@ #include "common-hal/microcontroller/Pin.h" +#include "stm32f4xx_hal.h" +#include "stm32f4/periph.h" + #include "py/obj.h" typedef struct { diff --git a/ports/stm32f4/peripherals/stm32f4/periph.h b/ports/stm32f4/peripherals/stm32f4/periph.h index ab4a5e1738..06ab2d3e4f 100644 --- a/ports/stm32f4/peripherals/stm32f4/periph.h +++ b/ports/stm32f4/peripherals/stm32f4/periph.h @@ -94,7 +94,7 @@ typedef struct { #define SPI(index, alt, spi_pin) \ { \ - .i2c_index = index, \ + .spi_index = index, \ .altfn_index = alt, \ .pin = spi_pin, \ } diff --git a/ports/stm32f4/peripherals/stm32f4/stm32f412zx/periph.c b/ports/stm32f4/peripherals/stm32f4/stm32f412zx/periph.c index d317031580..7df876931a 100644 --- a/ports/stm32f4/peripherals/stm32f4/stm32f412zx/periph.c +++ b/ports/stm32f4/peripherals/stm32f4/stm32f412zx/periph.c @@ -53,7 +53,7 @@ const mcu_i2c_scl_obj_t mcu_i2c_scl_list[4] = { // SPI -SPI_TypeDef * mcu_i2c_banks[3] = {SPI1, SPI2, SPI3, SPI4, SPI5}; +SPI_TypeDef * mcu_spi_banks[5] = {SPI1, SPI2, SPI3, SPI4, SPI5}; const mcu_spi_sck_obj_t mcu_spi_sck_list[16] = { SPI(1, 5, &pin_PA05), diff --git a/ports/stm32f4/supervisor/internal_flash.c b/ports/stm32f4/supervisor/internal_flash.c index 046b26baf2..8f0219616b 100644 --- a/ports/stm32f4/supervisor/internal_flash.c +++ b/ports/stm32f4/supervisor/internal_flash.c @@ -62,7 +62,11 @@ static const flash_layout_t flash_layout[] = { #endif }; -static uint8_t sector_copy[0x4000] __attribute__((aligned(4))); +static uint8_t sector_copy_16[0x4000] __attribute__((aligned(4))); + +#if INTERNAL_FLASH_FILESYSTEM_SIZE >= 0x1C000 +static uint8_t sector_copy_64[0x10000] __attribute__((aligned(4))); +#endif //Return the sector of a given flash address. uint32_t flash_get_sector_info(uint32_t addr, uint32_t *start_addr, uint32_t *size) { @@ -126,7 +130,7 @@ bool supervisor_flash_write_block(const uint8_t *src, uint32_t block) { int32_t dest = convert_block_to_flash_addr(block); if (dest == -1) { // bad block number - mp_printf(&mp_plat_print, "BAD FLASH BLOCK ERROR"); + mp_printf(&mp_plat_print, "Error: flash block not in filesystem"); return false; } @@ -142,7 +146,28 @@ bool supervisor_flash_write_block(const uint8_t *src, uint32_t block) { uint32_t sector_start_addr; EraseInitStruct.Sector = flash_get_sector_info(dest, §or_start_addr, §or_size); EraseInitStruct.NbSectors = 1; - if (sector_size>0x4000) return false; + + //If we have lots of ram the filesystem can be bigger. + if (INTERNAL_FLASH_FILESYSTEM_SIZE>=0x1C000) { + if (sector_size>0x10000) { //support 64KB sector 4 + mp_printf(&mp_plat_print, "Error: flash sector too large"); + return false; + } + } else { + if (sector_size>0x4000) { + mp_printf(&mp_plat_print, "Error: flash sector too large"); + return false; + } + } + + uint8_t * sector_copy; + if (sector_size == 0x4000) { + sector_copy = sector_copy_16; + } else if (sector_size == 0x10000) { + sector_copy = sector_copy_64; + } else { + mp_printf(&mp_plat_print, "Error: flash sector incorrect size"); + } // copy the sector memcpy(sector_copy,(void *)sector_start_addr,sector_size); @@ -159,7 +184,7 @@ bool supervisor_flash_write_block(const uint8_t *src, uint32_t block) { if (HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK) { // error occurred during sector erase HAL_FLASH_Lock(); // lock the flash - mp_printf(&mp_plat_print, "FLASH SECTOR ERASE ERROR"); + mp_printf(&mp_plat_print, "Error: flash sector erase failure"); return false; } @@ -177,7 +202,7 @@ bool supervisor_flash_write_block(const uint8_t *src, uint32_t block) { if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, sector_start_addr, (uint64_t)sector_copy[i]) != HAL_OK) { // error occurred during flash write HAL_FLASH_Lock(); // lock the flash - mp_printf(&mp_plat_print, "FLASH WRITE ERROR"); + mp_printf(&mp_plat_print, "Error: flash sector write error"); return false; } sector_start_addr += 1; diff --git a/ports/stm32f4/supervisor/internal_flash.h b/ports/stm32f4/supervisor/internal_flash.h index b6d26a07fe..0018e3ad90 100644 --- a/ports/stm32f4/supervisor/internal_flash.h +++ b/ports/stm32f4/supervisor/internal_flash.h @@ -39,7 +39,7 @@ #ifdef STM32F412Zx #define STM32_FLASH_SIZE 0x100000 //1MB -#define INTERNAL_FLASH_FILESYSTEM_SIZE 0xC000 //48KiB +#define INTERNAL_FLASH_FILESYSTEM_SIZE 0xC000 //112KiB #endif #ifdef STM32F405xx