Save the data in internal flash

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

Save the data in internal flash

Jump to solution
916 Views
smishra125
Contributor I

I want to save the 16-bit integer buffer of size 250 bytes in internal flash of the LPC55S28JBD100 controller. I am using LPCXpresso55S28 Development Board. Please provide some example and links.

 

0 Kudos
1 Solution
912 Views
ErichStyger
Senior Contributor V

Hi @smishra125 ,

did you already check the examples which come with the NXP SDK? There is one for the flash programming too:

ErichStyger_0-1669717108335.pngErichStyger_1-1669717152118.png

 

 

Otherwise:

I have shared a generic flash programming module on https://github.com/ErichStyger/McuOnEclipseLibrary/blob/master/lib/minIni/McuFlash.c , and you can even use MinINI to store data as a file in flash as described in https://mcuoneclipse.com/2021/12/19/key-value-pairs-in-flash-memory-file-system-less-minini/

 

I hope this helps,

Erich

View solution in original post

9 Replies
877 Views
ErichStyger
Senior Contributor V

What's the address you are reading from? Check with the debugger, which address causes the hard fault. I suspect it is either a wrong address, points beyond your valid memory range or the page has been erased and not verified, so causing a hard fault.

0 Kudos
869 Views
smishra125
Contributor I

@ErichStyger I used the sdk example to read data from flash. When the default code erase, write and read the data from the memory then it is doing OK. But when in the same code, I remove the erase and write part and try to read the same address then it shows me the hardfault error. Please check may be sdk has some issue.

0 Kudos
863 Views
ErichStyger
Senior Contributor V

That reading from a memory location is not SDK specific at all. It is just reading from a FLASH memory address.

I assume you have written that part with the SDK example earlier and somehow 'corrupted' it. This is what happened to me as well while developing my own flash programming routines. That's why I implemented all these safeguards in that McuFlash.c I mentioned. I really recommend that you follow that procedure described in there, and please have a look at the application note/forum discussion mentioned in there.

So again: check with the debugger the address your reading from. Use the debugger memory view to check if the memory is valid there. If in doubt: Erase the complete flash with the debugger to get a good state of it, then program again your application.

I hope this helps,

Erich

0 Kudos
856 Views
smishra125
Contributor I
Thanks, It's been solved now. There was an issue with memory address only as the address i was reading was illegal.
0 Kudos
894 Views
smishra125
Contributor I

Hi @ErichStyger I used the sdk example to save data in flash and it worked. But when I commented all the write and erase function of flash and then try to read the flash data only but it jumps into hardfault_handler file. My s_buffer_rbc variable is not able to get the value from the flash.Why it is showing the error in following code. I am sharing the code snippet.

/*
* Copyright 2018 NXP
* All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/

#include "fsl_debug_console.h"
#include "pin_mux.h"
#include "board.h"
#include "fsl_iap.h"
#include "fsl_iap_ffr.h"
#include "fsl_common.h"
#include "fsl_power.h"
////////////////////////////////////////////////////////////////////////////////
// Definitions
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
// Prototypes
////////////////////////////////////////////////////////////////////////////////
static void verify_status(status_t status);
static void error_trap();
////////////////////////////////////////////////////////////////////////////////
// Variables
////////////////////////////////////////////////////////////////////////////////
#define BUFFER_LEN 512 / 4
static uint32_t s_buffer_rbc[BUFFER_LEN];
////////////////////////////////////////////////////////////////////////////////
// Code
////////////////////////////////////////////////////////////////////////////////
int main()
{
flash_config_t flashInstance;
static uint32_t status;
uint32_t destAdrss; /* Address of the target location */
uint32_t failedAddress, failedData;
uint32_t pflashBlockBase = 0;
uint32_t pflashTotalSize = 0;
uint32_t pflashSectorSize = 0;
uint32_t PflashPageSize = 0;
const uint32_t s_buffer[BUFFER_LEN] = {12, 32, 13, 84};
/* Init board hardware. */
/* set BOD VBAT level to 1.65V */
POWER_SetBodVbatLevel(kPOWER_BodVbatLevel1650mv, kPOWER_BodHystLevel50mv, false);
/* attach 12 MHz clock to FLEXCOMM0 (debug console) */
CLOCK_AttachClk(BOARD_DEBUG_UART_CLK_ATTACH);
/* enable clock for GPIO*/
CLOCK_EnableClock(kCLOCK_Gpio0);
CLOCK_EnableClock(kCLOCK_Gpio1);

BOARD_InitBootPins();
BOARD_BootClockFROHF96M();
BOARD_InitDebugConsole();

/* Print basic information for Flash Driver API.*/
PRINTF("\r\nFlash driver API tree Demo Application...\r\n");
/* Initialize flash driver */
PRINTF("Initializing flash driver...\r\n");
if (FLASH_Init(&flashInstance) == kStatus_Success)
{
PRINTF("Flash init successfull!!. Halting...\r\n");
}
else
{
error_trap();
}
/* Get flash properties kFLASH_ApiEraseKey */
FLASH_GetProperty(&flashInstance, kFLASH_PropertyPflashBlockBaseAddr, &pflashBlockBase);
FLASH_GetProperty(&flashInstance, kFLASH_PropertyPflashSectorSize, &pflashSectorSize);
FLASH_GetProperty(&flashInstance, kFLASH_PropertyPflashTotalSize, &pflashTotalSize);
FLASH_GetProperty(&flashInstance, kFLASH_PropertyPflashPageSize, &PflashPageSize);

/* print welcome message */
PRINTF("\r\n PFlash Example Start \r\n");
/* Print flash information - PFlash. */
PRINTF("\tkFLASH_PropertyPflashBlockBaseAddr = 0x%X\r\n", pflashBlockBase);
PRINTF("\tkFLASH_PropertyPflashSectorSize = %d\r\n", pflashSectorSize);
PRINTF("\tkFLASH_PropertyPflashTotalSize = %d\r\n", pflashTotalSize);
PRINTF("\tkFLASH_PropertyPflashPageSize = 0x%X\r\n", PflashPageSize);

/*
PAGE_INDEX_FROM_END = 1 means the last page,
PAGE_INDEX_FROM_END = 2 means (the last page -1 )...
*/
#ifndef PAGE_INDEX_FROM_END
#define PAGE_INDEX_FROM_END 1U
#endif

destAdrss = pflashBlockBase + (pflashTotalSize - (PAGE_INDEX_FROM_END * PflashPageSize));

/* 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)));

/* resume flash memory status */
status = FLASH_Erase(&flashInstance, destAdrss, PflashPageSize, kFLASH_ApiEraseKey);

PRINTF("Done!\r\n");

while (1)
{
}
}

void verify_status(status_t status)
{
char *tipString = "Unknown status";
switch (status)
{
case kStatus_Success:
tipString = "Done.";
break;
case kStatus_InvalidArgument:
tipString = "Invalid argument.";
break;
case kStatus_FLASH_AlignmentError:
tipString = "Alignment Error.";
break;
case kStatus_FLASH_AccessError:
tipString = "Flash Access Error.";
break;
case kStatus_FLASH_CommandNotSupported:
tipString = "This API is not supported in current target.";
break;
default:
break;
}
PRINTF("%s\r\n\r\n", tipString);
}

/*
* @brief Gets called when an error occurs.
*/
void error_trap(void)
{
PRINTF("\r\n\r\n\r\n\t---- HALTED DUE TO FLASH ERROR! ----");
while (1)
{
}
}

////////////////////////////////////////////////////////////////////////////////
// EOF
////////////////////////////////////////////////////////////////////////////////

0 Kudos
893 Views
ErichStyger
Senior Contributor V

See that McuFlash_InitErase() I have provided.

If you just erase but not verify the memory, it will generate a hard fault accessing it.

The McuFlash_IsAccessible() has a comment about that special memory for the LPC55xx.

I hipe this helps,

Erich

0 Kudos
881 Views
smishra125
Contributor I

@ErichStyger I need to read the flash memory in the program that's why i tried this code. I havn't erased anything. I just want to read the what is on the FLASH BUFFER ADDRESS but it fails.

/* 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();
}
}

0 Kudos
908 Views
smishra125
Contributor I

@ErichStyger Thanks , I get it.

0 Kudos
913 Views
ErichStyger
Senior Contributor V

Hi @smishra125 ,

did you already check the examples which come with the NXP SDK? There is one for the flash programming too:

ErichStyger_0-1669717108335.pngErichStyger_1-1669717152118.png

 

 

Otherwise:

I have shared a generic flash programming module on https://github.com/ErichStyger/McuOnEclipseLibrary/blob/master/lib/minIni/McuFlash.c , and you can even use MinINI to store data as a file in flash as described in https://mcuoneclipse.com/2021/12/19/key-value-pairs-in-flash-memory-file-system-less-minini/

 

I hope this helps,

Erich