From 2be0790c4a7b59c9a4bfad42039340f6c57cf90c Mon Sep 17 00:00:00 2001 From: Gavin Liu Date: Thu, 2 Apr 2020 16:58:44 +0800 Subject: [PATCH] targets:TARGET_IMX: Fix the memset issue for FLASHIAP The memset function from c library will be linked in flash space, it's risk for FLASHIAP. So I wrote flexspi_memset to replace the memset for IMX FLASHIAP, and put the function into targets/.../TARGET_IMX/flash_api.c file. All IMX Soc platforms can declare it as extern and use in their Soc flexspi driver files. Signed-off-by: Gavin Liu --- .../TARGET_IMX/flash_api.c | 55 +++++++++++++++++-- .../TARGET_MIMXRT1050/drivers/fsl_flexspi.c | 4 +- .../TARGET_MIMXRT1050/drivers/fsl_flexspi.h | 8 +++ 3 files changed, 60 insertions(+), 7 deletions(-) diff --git a/targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_IMX/flash_api.c b/targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_IMX/flash_api.c index 69e50b29b1..1241af4959 100644 --- a/targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_IMX/flash_api.c +++ b/targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_IMX/flash_api.c @@ -37,12 +37,57 @@ AT_QUICKACCESS_SECTION_CODE(status_t flexspi_nor_flash_page_program_ram(uint32_t AT_QUICKACCESS_SECTION_CODE(void flexspi_nor_flash_read_data_ram(uint32_t addr, uint32_t *buffer, uint32_t size)); +AT_QUICKACCESS_SECTION_CODE(void *flexspi_memset(void *buf, int c, size_t n)); +/** + * @brief Set bytes in memory. If put this code in SRAM, Make sure this code + * does not call functions in Flash. + * + * @return pointer to start of buffer + */ +void *flexspi_memset(void *buf, int c, size_t n) +{ + /* do byte-sized initialization until word-aligned or finished */ + unsigned char *d_byte = (unsigned char *)buf; + unsigned char c_byte = (unsigned char)c; + + while (((unsigned int)d_byte) & 0x3) { + if (n == 0) { + return buf; + } + *(d_byte++) = c_byte; + n--; + }; + + /* do word-sized initialization as long as possible */ + + unsigned int *d_word = (unsigned int *)d_byte; + unsigned int c_word = (unsigned int)(unsigned char)c; + + c_word |= c_word << 8; + c_word |= c_word << 16; + + while (n >= sizeof(unsigned int)) { + *(d_word++) = c_word; + n -= sizeof(unsigned int); + } + + /* do byte-sized initialization until finished */ + + d_byte = (unsigned char *)d_word; + + while (n > 0) { + *(d_byte++) = c_byte; + n--; + } + + return buf; +} void flexspi_update_lut_ram(void) { flexspi_config_t config; - memset(&config, 0, sizeof(config)); + flexspi_memset(&config, 0, sizeof(config)); /*Get FLEXSPI default settings and configure the flexspi. */ FLEXSPI_GetDefaultConfig(&config); @@ -77,7 +122,7 @@ status_t flexspi_nor_write_enable_ram(uint32_t baseAddr) flexspi_transfer_t flashXfer; status_t status = kStatus_Success; - memset(&flashXfer, 0, sizeof(flashXfer)); + flexspi_memset(&flashXfer, 0, sizeof(flashXfer)); /* Write enable */ flashXfer.deviceAddress = baseAddr; @@ -99,7 +144,7 @@ status_t flexspi_nor_wait_bus_busy_ram(void) status_t status = kStatus_Success; flexspi_transfer_t flashXfer; - memset(&flashXfer, 0, sizeof(flashXfer)); + flexspi_memset(&flashXfer, 0, sizeof(flashXfer)); flashXfer.deviceAddress = 0; flashXfer.port = kFLEXSPI_PortA1; @@ -138,7 +183,7 @@ status_t flexspi_nor_flash_erase_sector_ram(uint32_t address) status_t status = kStatus_Success; flexspi_transfer_t flashXfer; - memset(&flashXfer, 0, sizeof(flashXfer)); + flexspi_memset(&flashXfer, 0, sizeof(flashXfer)); /* Write enable */ status = flexspi_nor_write_enable_ram(address); @@ -229,7 +274,7 @@ status_t flexspi_nor_flash_page_program_ram(uint32_t address, const uint32_t *sr flexspi_transfer_t flashXfer; uint32_t offset = 0; - memset(&flashXfer, 0, sizeof(flashXfer)); + flexspi_memset(&flashXfer, 0, sizeof(flashXfer)); flexspi_lower_clock_ram(); diff --git a/targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_MIMXRT1050/drivers/fsl_flexspi.c b/targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_MIMXRT1050/drivers/fsl_flexspi.c index c925526dc4..ad2f3fd683 100644 --- a/targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_MIMXRT1050/drivers/fsl_flexspi.c +++ b/targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_MIMXRT1050/drivers/fsl_flexspi.c @@ -337,7 +337,7 @@ void FLEXSPI_Init(FLEXSPI_Type *base, const flexspi_config_t *config) void FLEXSPI_GetDefaultConfig(flexspi_config_t *config) { /* Initializes the configure structure to zero. */ - (void)memset(config, 0, sizeof(*config)); + (void)flexspi_memset(config, 0, sizeof(*config)); config->rxSampleClock = kFLEXSPI_ReadSampleClkLoopbackInternally; config->enableSckFreeRunning = false; @@ -359,7 +359,7 @@ void FLEXSPI_GetDefaultConfig(flexspi_config_t *config) config->ahbConfig.ahbGrantTimeoutCycle = 0xFFU; config->ahbConfig.ahbBusTimeoutCycle = 0xFFFFU; config->ahbConfig.resumeWaitCycle = 0x20U; - (void)memset(config->ahbConfig.buffer, 0, sizeof(config->ahbConfig.buffer)); + (void)flexspi_memset(config->ahbConfig.buffer, 0, sizeof(config->ahbConfig.buffer)); for (uint8_t i = 0; i < (uint32_t)FSL_FEATURE_FLEXSPI_AHB_BUFFER_COUNT; i++) { config->ahbConfig.buffer[i].enablePrefetch = true; /* Default enable AHB prefetch. */ diff --git a/targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_MIMXRT1050/drivers/fsl_flexspi.h b/targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_MIMXRT1050/drivers/fsl_flexspi.h index 2b1e7ac2fe..eb05a4b972 100644 --- a/targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_MIMXRT1050/drivers/fsl_flexspi.h +++ b/targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_MIMXRT1050/drivers/fsl_flexspi.h @@ -333,6 +333,14 @@ struct _flexspi_handle extern "C" { #endif /*_cplusplus. */ +/** + * @brief Set bytes in memory. If put this code in SRAM, Make sure this code + * does not call functions in Flash. + * + * @return pointer to start of buffer + */ +extern void *flexspi_memset(void *buf, int c, size_t n); + /*! * @name Initialization and deinitialization * @{