How to R/W internal flash of K66FN2M0VLQ18 when using FreeRTOS and DRam

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to R/W internal flash of K66FN2M0VLQ18 when using FreeRTOS and DRam

Jump to solution
422 Views
thanhnambka
Contributor III

Hi all,

My board using MCU K66FN2M0VLQ18, DRam IS42S16400J-7TLI, IDE IAR Embedded Workbench.

I have init DRam in SystemInit(void):

thanhnambka_0-1708920390022.png

 

How to erase internal flash if i using FreeRTOS and Dram?

 

 

 
Labels (1)
0 Kudos
Reply
1 Solution
392 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi,

I suppose that you can download SDK package, in the package, there is flash example.

If you want to erase/program flash in FreeRtos environment, you have to do the following:

1)stop the FreeRtos scheduling with

void vTaskEndScheduler( void )

 

2)disable all interrupt with code:

__STATIC_FORCEINLINE void __disable_irq(void)

{

__ASM volatile ("cpsid i" : : : "memory");

}

3)erase/program flash with SDK example

4)recover the interrupt and FreeRtos scheduling.

 

Hope it can help you

BR

XiangJun Rong

 

This is the flash operation example in SDK:

int main(void)

{

flash_security_state_t securityStatus = kFLASH_SecurityStateNotSecure; /* Return protection status */

status_t result; /* Return code from each flash driver function */

uint32_t destAdrss; /* Address of the target location */

uint32_t i, failAddr, failDat;

 

uint32_t pflashBlockBase = 0;

uint32_t pflashTotalSize = 0;

uint32_t pflashSectorSize = 0;

 

/* Init hardware */

BOARD_InitPins();

BOARD_BootClockRUN();

BOARD_InitDebugConsole();

 

/* Clean up Flash driver Structure*/

memset(&s_flashDriver, 0, sizeof(flash_config_t));

 

/* Setup flash driver structure for device and initialize variables. */

result = FLASH_Init(&s_flashDriver);

if (kStatus_FLASH_Success != result)

{

error_trap();

}

/* Get flash properties*/

FLASH_GetProperty(&s_flashDriver, kFLASH_PropertyPflashBlockBaseAddr, &pflashBlockBase);

FLASH_GetProperty(&s_flashDriver, kFLASH_PropertyPflashTotalSize, &pflashTotalSize);

FLASH_GetProperty(&s_flashDriver, kFLASH_PropertyPflashSectorSize, &pflashSectorSize);

 

/* print welcome message */

PRINTF("\r\n PFlash Example Start \r\n");

/* Print flash information - PFlash. */

PRINTF("\r\n PFlash Information: ");

PRINTF("\r\n Total Program Flash Size:\t%d KB, Hex: (0x%x)", (pflashTotalSize / 1024), pflashTotalSize);

PRINTF("\r\n Program Flash Sector Size:\t%d KB, Hex: (0x%x) ", (pflashSectorSize / 1024), pflashSectorSize);

 

/* Check security status. */

result = FLASH_GetSecurityState(&s_flashDriver, &securityStatus);

if (kStatus_FLASH_Success != result)

{

error_trap();

}

/* Print security status. */

switch (securityStatus)

{

case kFLASH_SecurityStateNotSecure:

PRINTF("\r\n Flash is UNSECURE!");

break;

case kFLASH_SecurityStateBackdoorEnabled:

PRINTF("\r\n Flash is SECURE, BACKDOOR is ENABLED!");

break;

case kFLASH_SecurityStateBackdoorDisabled:

PRINTF("\r\n Flash is SECURE, BACKDOOR is DISABLED!");

break;

default:

break;

}

PRINTF("\r\n");

 

/* Test pflash basic opeation only if flash is unsecure. */

if (kFLASH_SecurityStateNotSecure == securityStatus)

{

/* Debug message for user. */

/* Erase several sectors on upper pflash block where there is no code */

PRINTF("\r\n Erase a sector of flash");

 

/* Erase a sector from destAdrss. */

#if defined(FSL_FEATURE_FLASH_HAS_PFLASH_BLOCK_SWAP) && FSL_FEATURE_FLASH_HAS_PFLASH_BLOCK_SWAP

/* Note: we should make sure that the sector shouldn't be swap indicator sector*/

destAdrss = pflashBlockBase + (pflashTotalSize - (pflashSectorSize * 2));

#else

destAdrss = pflashBlockBase + (pflashTotalSize - pflashSectorSize);

#endif

result = FLASH_Erase(&s_flashDriver, destAdrss, pflashSectorSize, kFLASH_ApiEraseKey);

if (kStatus_FLASH_Success != result)

{

error_trap();

}

 

/* Verify sector if it's been erased. */

result = FLASH_VerifyErase(&s_flashDriver, destAdrss, pflashSectorSize, kFLASH_MarginValueUser);

if (kStatus_FLASH_Success != result)

{

error_trap();

}

 

/* Print message for user. */

PRINTF("\r\n Successfully Erased Sector 0x%x -> 0x%x\r\n", destAdrss, (destAdrss + pflashSectorSize));

 

/* Print message for user. */

PRINTF("\r\n Program a buffer to a sector of flash ");

/* Prepare user buffer. */

for (i = 0; i < BUFFER_LEN; i++)

{

s_buffer[i] = i;

}

/* Program user buffer into flash*/

result = FLASH_Program(&s_flashDriver, destAdrss, s_buffer, sizeof(s_buffer));

if (kStatus_FLASH_Success != result)

{

error_trap();

}

 

/* Verify programming by Program Check command with user margin levels */

result = FLASH_VerifyProgram(&s_flashDriver, destAdrss, sizeof(s_buffer), s_buffer, kFLASH_MarginValueUser,

&failAddr, &failDat);

if (kStatus_FLASH_Success != result)

{

error_trap();

}

 

#if defined(__DCACHE_PRESENT) && __DCACHE_PRESENT

/* Clean the D-Cache before reading the flash data*/

SCB_CleanInvalidateDCache();

#endif

/* Verify programming by reading back from flash directly*/

for (uint32_t i = 0; i < BUFFER_LEN; i++)

{

s_buffer_rbc[i] = *(volatile uint32_t *)(destAdrss + i * 4);

if (s_buffer_rbc[i] != s_buffer[i])

{

error_trap();

}

}

 

PRINTF("\r\n Successfully Programmed and Verified Location 0x%x -> 0x%x \r\n", destAdrss,

(destAdrss + sizeof(s_buffer)));

 

/* Erase the context we have progeammed before*/

/* Note: we should make sure that the sector which will be set as swap indicator should be blank*/

FLASH_Erase(&s_flashDriver, destAdrss, pflashSectorSize, kFLASH_ApiEraseKey);

}

else

{

PRINTF("\r\n Erase/Program opeation will not be executed, as Flash is SECURE!");

}

 

app_finalize();

 

return 0;

}

 

View solution in original post

1 Reply
393 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi,

I suppose that you can download SDK package, in the package, there is flash example.

If you want to erase/program flash in FreeRtos environment, you have to do the following:

1)stop the FreeRtos scheduling with

void vTaskEndScheduler( void )

 

2)disable all interrupt with code:

__STATIC_FORCEINLINE void __disable_irq(void)

{

__ASM volatile ("cpsid i" : : : "memory");

}

3)erase/program flash with SDK example

4)recover the interrupt and FreeRtos scheduling.

 

Hope it can help you

BR

XiangJun Rong

 

This is the flash operation example in SDK:

int main(void)

{

flash_security_state_t securityStatus = kFLASH_SecurityStateNotSecure; /* Return protection status */

status_t result; /* Return code from each flash driver function */

uint32_t destAdrss; /* Address of the target location */

uint32_t i, failAddr, failDat;

 

uint32_t pflashBlockBase = 0;

uint32_t pflashTotalSize = 0;

uint32_t pflashSectorSize = 0;

 

/* Init hardware */

BOARD_InitPins();

BOARD_BootClockRUN();

BOARD_InitDebugConsole();

 

/* Clean up Flash driver Structure*/

memset(&s_flashDriver, 0, sizeof(flash_config_t));

 

/* Setup flash driver structure for device and initialize variables. */

result = FLASH_Init(&s_flashDriver);

if (kStatus_FLASH_Success != result)

{

error_trap();

}

/* Get flash properties*/

FLASH_GetProperty(&s_flashDriver, kFLASH_PropertyPflashBlockBaseAddr, &pflashBlockBase);

FLASH_GetProperty(&s_flashDriver, kFLASH_PropertyPflashTotalSize, &pflashTotalSize);

FLASH_GetProperty(&s_flashDriver, kFLASH_PropertyPflashSectorSize, &pflashSectorSize);

 

/* print welcome message */

PRINTF("\r\n PFlash Example Start \r\n");

/* Print flash information - PFlash. */

PRINTF("\r\n PFlash Information: ");

PRINTF("\r\n Total Program Flash Size:\t%d KB, Hex: (0x%x)", (pflashTotalSize / 1024), pflashTotalSize);

PRINTF("\r\n Program Flash Sector Size:\t%d KB, Hex: (0x%x) ", (pflashSectorSize / 1024), pflashSectorSize);

 

/* Check security status. */

result = FLASH_GetSecurityState(&s_flashDriver, &securityStatus);

if (kStatus_FLASH_Success != result)

{

error_trap();

}

/* Print security status. */

switch (securityStatus)

{

case kFLASH_SecurityStateNotSecure:

PRINTF("\r\n Flash is UNSECURE!");

break;

case kFLASH_SecurityStateBackdoorEnabled:

PRINTF("\r\n Flash is SECURE, BACKDOOR is ENABLED!");

break;

case kFLASH_SecurityStateBackdoorDisabled:

PRINTF("\r\n Flash is SECURE, BACKDOOR is DISABLED!");

break;

default:

break;

}

PRINTF("\r\n");

 

/* Test pflash basic opeation only if flash is unsecure. */

if (kFLASH_SecurityStateNotSecure == securityStatus)

{

/* Debug message for user. */

/* Erase several sectors on upper pflash block where there is no code */

PRINTF("\r\n Erase a sector of flash");

 

/* Erase a sector from destAdrss. */

#if defined(FSL_FEATURE_FLASH_HAS_PFLASH_BLOCK_SWAP) && FSL_FEATURE_FLASH_HAS_PFLASH_BLOCK_SWAP

/* Note: we should make sure that the sector shouldn't be swap indicator sector*/

destAdrss = pflashBlockBase + (pflashTotalSize - (pflashSectorSize * 2));

#else

destAdrss = pflashBlockBase + (pflashTotalSize - pflashSectorSize);

#endif

result = FLASH_Erase(&s_flashDriver, destAdrss, pflashSectorSize, kFLASH_ApiEraseKey);

if (kStatus_FLASH_Success != result)

{

error_trap();

}

 

/* Verify sector if it's been erased. */

result = FLASH_VerifyErase(&s_flashDriver, destAdrss, pflashSectorSize, kFLASH_MarginValueUser);

if (kStatus_FLASH_Success != result)

{

error_trap();

}

 

/* Print message for user. */

PRINTF("\r\n Successfully Erased Sector 0x%x -> 0x%x\r\n", destAdrss, (destAdrss + pflashSectorSize));

 

/* Print message for user. */

PRINTF("\r\n Program a buffer to a sector of flash ");

/* Prepare user buffer. */

for (i = 0; i < BUFFER_LEN; i++)

{

s_buffer[i] = i;

}

/* Program user buffer into flash*/

result = FLASH_Program(&s_flashDriver, destAdrss, s_buffer, sizeof(s_buffer));

if (kStatus_FLASH_Success != result)

{

error_trap();

}

 

/* Verify programming by Program Check command with user margin levels */

result = FLASH_VerifyProgram(&s_flashDriver, destAdrss, sizeof(s_buffer), s_buffer, kFLASH_MarginValueUser,

&failAddr, &failDat);

if (kStatus_FLASH_Success != result)

{

error_trap();

}

 

#if defined(__DCACHE_PRESENT) && __DCACHE_PRESENT

/* Clean the D-Cache before reading the flash data*/

SCB_CleanInvalidateDCache();

#endif

/* Verify programming by reading back from flash directly*/

for (uint32_t i = 0; i < BUFFER_LEN; i++)

{

s_buffer_rbc[i] = *(volatile uint32_t *)(destAdrss + i * 4);

if (s_buffer_rbc[i] != s_buffer[i])

{

error_trap();

}

}

 

PRINTF("\r\n Successfully Programmed and Verified Location 0x%x -> 0x%x \r\n", destAdrss,

(destAdrss + sizeof(s_buffer)));

 

/* Erase the context we have progeammed before*/

/* Note: we should make sure that the sector which will be set as swap indicator should be blank*/

FLASH_Erase(&s_flashDriver, destAdrss, pflashSectorSize, kFLASH_ApiEraseKey);

}

else

{

PRINTF("\r\n Erase/Program opeation will not be executed, as Flash is SECURE!");

}

 

app_finalize();

 

return 0;

}