Skip to content

Commit 2be0790

Browse files
committed
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 <[email protected]>
1 parent f8a8401 commit 2be0790

File tree

3 files changed

+60
-7
lines changed

3 files changed

+60
-7
lines changed

targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_IMX/flash_api.c

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,57 @@ AT_QUICKACCESS_SECTION_CODE(status_t flexspi_nor_flash_page_program_ram(uint32_t
3737
AT_QUICKACCESS_SECTION_CODE(void flexspi_nor_flash_read_data_ram(uint32_t addr,
3838
uint32_t *buffer,
3939
uint32_t size));
40+
AT_QUICKACCESS_SECTION_CODE(void *flexspi_memset(void *buf, int c, size_t n));
41+
/**
42+
* @brief Set bytes in memory. If put this code in SRAM, Make sure this code
43+
* does not call functions in Flash.
44+
*
45+
* @return pointer to start of buffer
46+
*/
47+
void *flexspi_memset(void *buf, int c, size_t n)
48+
{
49+
/* do byte-sized initialization until word-aligned or finished */
50+
unsigned char *d_byte = (unsigned char *)buf;
51+
unsigned char c_byte = (unsigned char)c;
52+
53+
while (((unsigned int)d_byte) & 0x3) {
54+
if (n == 0) {
55+
return buf;
56+
}
57+
*(d_byte++) = c_byte;
58+
n--;
59+
};
60+
61+
/* do word-sized initialization as long as possible */
62+
63+
unsigned int *d_word = (unsigned int *)d_byte;
64+
unsigned int c_word = (unsigned int)(unsigned char)c;
65+
66+
c_word |= c_word << 8;
67+
c_word |= c_word << 16;
68+
69+
while (n >= sizeof(unsigned int)) {
70+
*(d_word++) = c_word;
71+
n -= sizeof(unsigned int);
72+
}
73+
74+
/* do byte-sized initialization until finished */
75+
76+
d_byte = (unsigned char *)d_word;
77+
78+
while (n > 0) {
79+
*(d_byte++) = c_byte;
80+
n--;
81+
}
82+
83+
return buf;
84+
}
4085

4186
void flexspi_update_lut_ram(void)
4287
{
4388
flexspi_config_t config;
4489

45-
memset(&config, 0, sizeof(config));
90+
flexspi_memset(&config, 0, sizeof(config));
4691

4792
/*Get FLEXSPI default settings and configure the flexspi. */
4893
FLEXSPI_GetDefaultConfig(&config);
@@ -77,7 +122,7 @@ status_t flexspi_nor_write_enable_ram(uint32_t baseAddr)
77122
flexspi_transfer_t flashXfer;
78123
status_t status = kStatus_Success;
79124

80-
memset(&flashXfer, 0, sizeof(flashXfer));
125+
flexspi_memset(&flashXfer, 0, sizeof(flashXfer));
81126

82127
/* Write enable */
83128
flashXfer.deviceAddress = baseAddr;
@@ -99,7 +144,7 @@ status_t flexspi_nor_wait_bus_busy_ram(void)
99144
status_t status = kStatus_Success;
100145
flexspi_transfer_t flashXfer;
101146

102-
memset(&flashXfer, 0, sizeof(flashXfer));
147+
flexspi_memset(&flashXfer, 0, sizeof(flashXfer));
103148

104149
flashXfer.deviceAddress = 0;
105150
flashXfer.port = kFLEXSPI_PortA1;
@@ -138,7 +183,7 @@ status_t flexspi_nor_flash_erase_sector_ram(uint32_t address)
138183
status_t status = kStatus_Success;
139184
flexspi_transfer_t flashXfer;
140185

141-
memset(&flashXfer, 0, sizeof(flashXfer));
186+
flexspi_memset(&flashXfer, 0, sizeof(flashXfer));
142187

143188
/* Write enable */
144189
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
229274
flexspi_transfer_t flashXfer;
230275
uint32_t offset = 0;
231276

232-
memset(&flashXfer, 0, sizeof(flashXfer));
277+
flexspi_memset(&flashXfer, 0, sizeof(flashXfer));
233278

234279
flexspi_lower_clock_ram();
235280

targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_MIMXRT1050/drivers/fsl_flexspi.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ void FLEXSPI_Init(FLEXSPI_Type *base, const flexspi_config_t *config)
337337
void FLEXSPI_GetDefaultConfig(flexspi_config_t *config)
338338
{
339339
/* Initializes the configure structure to zero. */
340-
(void)memset(config, 0, sizeof(*config));
340+
(void)flexspi_memset(config, 0, sizeof(*config));
341341

342342
config->rxSampleClock = kFLEXSPI_ReadSampleClkLoopbackInternally;
343343
config->enableSckFreeRunning = false;
@@ -359,7 +359,7 @@ void FLEXSPI_GetDefaultConfig(flexspi_config_t *config)
359359
config->ahbConfig.ahbGrantTimeoutCycle = 0xFFU;
360360
config->ahbConfig.ahbBusTimeoutCycle = 0xFFFFU;
361361
config->ahbConfig.resumeWaitCycle = 0x20U;
362-
(void)memset(config->ahbConfig.buffer, 0, sizeof(config->ahbConfig.buffer));
362+
(void)flexspi_memset(config->ahbConfig.buffer, 0, sizeof(config->ahbConfig.buffer));
363363
for (uint8_t i = 0; i < (uint32_t)FSL_FEATURE_FLEXSPI_AHB_BUFFER_COUNT; i++)
364364
{
365365
config->ahbConfig.buffer[i].enablePrefetch = true; /* Default enable AHB prefetch. */

targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_MIMXRT1050/drivers/fsl_flexspi.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,14 @@ struct _flexspi_handle
333333
extern "C" {
334334
#endif /*_cplusplus. */
335335

336+
/**
337+
* @brief Set bytes in memory. If put this code in SRAM, Make sure this code
338+
* does not call functions in Flash.
339+
*
340+
* @return pointer to start of buffer
341+
*/
342+
extern void *flexspi_memset(void *buf, int c, size_t n);
343+
336344
/*!
337345
* @name Initialization and deinitialization
338346
* @{

0 commit comments

Comments
 (0)