Read and write to user flash

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
EsFr_4600111
Level 3
Level 3
25 sign-ins 10 replies posted 10 questions asked

Have a board with a cyble-222014-01 device 

I want a simple example in how to write to  user flash area and read again, i have tried foloowing code:

 const uint8_t row[CY_FLASH_SIZEOF_ROW] CY_ALIGN(CY_FLASH_SIZEOF_ROW) = {0};
uint8_t data[CY_FLASH_SIZEOF_ROW];
int rowNum;
// calculate which row in the flash that the "row" array resides
rowNum = ((int)row - CY_FLASH_BASE) / CY_FLASH_SIZEOF_ROW;
memcpy(data,row,(int)CY_FLASH_SIZEOF_ROW);
data[1]=123;
uint32 rval = CySysFlashWriteRow(rowNum,data);
if(rval == CY_SYS_FLASH_SUCCESS )
{
uint8 ok = 1;

my problem here is that rval is not 0 but 4 = CY_SYS_FLASH_INVALID_ADDR, when i try to write. 

Anyone can help here

0 Likes
1 Solution
Raj_C
Moderator
Moderator
Moderator
250 solutions authored 500 replies posted 50 likes received

Hi @EsFr_4600111 ,

I have tried your code at my end and found out that you have placed ' const uint8_t row[CY_FLASH_SIZEOF_ROW] CY_ALIGN(CY_FLASH_SIZEOF_ROW) = {0};' inside the main function. This allocates the memory region in RAM not in Flash. This is causing the error while executing.

You need to place row variable outside the main function. The modified code will be as follows:

 

#include "project.h"

const uint8_t row[CY_FLASH_SIZEOF_ROW] CY_ALIGN(CY_FLASH_SIZEOF_ROW) = {0};

int main(void)
{
CyGlobalIntEnable; /* Enable global interrupts. */

/* Place your initialization/startup code here (e.g. MyInst_Start()) */

uint8_t data[CY_FLASH_SIZEOF_ROW];

int rowNum;
// calculate which row in the flash that the "row" array resides
rowNum = ((int)row - CY_FLASH_BASE) / CY_FLASH_SIZEOF_ROW;
memcpy(data,row,(int)CY_FLASH_SIZEOF_ROW);
data[1]=123;
volatile uint32 rval = CySysFlashWriteRow(rowNum,data);
if(rval == CY_SYS_FLASH_SUCCESS )
{
uint8 ok = 1;
}
for(;;)
{
/* Place your application code here. */
}
}

/* [] END OF FILE */

Best regards,

Raj

 

View solution in original post

0 Likes
3 Replies
BiBi_1928986
Level 7
Level 7
First comment on blog 500 replies posted 250 replies posted

Hello.

Is there a reason you are not using emEEPROM component?  This emulates an EEPROM using FLASH in PSoC's that do not contain EEPROM.  The API's are all written for you.

regards.

0 Likes

Im not able to see the emEEPROM component in the creator . Are you sure its enabled for cyble-222014-01 chip?

EsFr_4600111_0-1631014209134.png

 

0 Likes
Raj_C
Moderator
Moderator
Moderator
250 solutions authored 500 replies posted 50 likes received

Hi @EsFr_4600111 ,

I have tried your code at my end and found out that you have placed ' const uint8_t row[CY_FLASH_SIZEOF_ROW] CY_ALIGN(CY_FLASH_SIZEOF_ROW) = {0};' inside the main function. This allocates the memory region in RAM not in Flash. This is causing the error while executing.

You need to place row variable outside the main function. The modified code will be as follows:

 

#include "project.h"

const uint8_t row[CY_FLASH_SIZEOF_ROW] CY_ALIGN(CY_FLASH_SIZEOF_ROW) = {0};

int main(void)
{
CyGlobalIntEnable; /* Enable global interrupts. */

/* Place your initialization/startup code here (e.g. MyInst_Start()) */

uint8_t data[CY_FLASH_SIZEOF_ROW];

int rowNum;
// calculate which row in the flash that the "row" array resides
rowNum = ((int)row - CY_FLASH_BASE) / CY_FLASH_SIZEOF_ROW;
memcpy(data,row,(int)CY_FLASH_SIZEOF_ROW);
data[1]=123;
volatile uint32 rval = CySysFlashWriteRow(rowNum,data);
if(rval == CY_SYS_FLASH_SUCCESS )
{
uint8 ok = 1;
}
for(;;)
{
/* Place your application code here. */
}
}

/* [] END OF FILE */

Best regards,

Raj

 

0 Likes