Skip to main content
cjaya.2
Associate III
Apr 12, 2024 7:56 PM UTC
Question

storing string's on flash

hi,

I initialized string array with strings and type cast to uint32_t to store in flash memory as uint32_t. And then read the array as uint32_t. I update one of the string of the  array and type cast uint32_t to store that array in Flash memory back. But whenever I update string value and store on the flash memory. The string has strange characters on it. If you see the string that I am trying to store in string array "21.76".
string.jpg

But in flash memory showing as "yyyy6.Its not storing properly.If you see the picture below in memory browser its showing as "yyyy6".

memory.jpg

 

 

I dont know why?. Any suggestions.

 

 

 

 

 

This topic has been closed for replies.

19 replies

Pavel A.
Apr 12, 2024 8:18 PM UTC

Suppose someone asks you this question. What would you say? Do you have enough information to help him? 

Tesla DeLorean
Guru
Apr 12, 2024 9:20 PM UTC

>>Any suggestions.

Doing it wrong, show code, use the </> code pasting tools if you in-line in post, or link to github project illustrating.

Show enough to be coherent, not selective snippets.

Looks like you're not writing the first word.

 

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
cjaya.2
cjaya.2Author
Associate III
Apr 13, 2024 6:46 AM UTC

Here is the code.

 

if(strncmp((char*)ptr, "tempc", 5) == 0)
 {
 MY_FLASH_SetSectorAddrs(6, 0x08040000);
 if (strncmp((char*)ptr, "tempc1", 6) == 0)
 {
 Counter = 0;
 }

 MY_FLASH_ReadN(0, (uint32_t*)CStore, sizeof(Ctore),DATA_TYPE_32);
 HAL_Delay(100);
 memset(CStore[Counter], '\0', 10);

 for(char *str =strtok((char*)ptr, " "); str; str = strtok(NULL, " "))
	 {
	 if(strncmp((char*)str, "tempc", 5) == 0)
	 continue;

	 strncpy(CStore[Counter], str, strlen(str)); 
	 Counter++;
	 }

 MY_FLASH_WriteN(0, (uint32_t*)CStore, sizeof(CStore), DATA_TYPE_32);
 HAL_Delay(100);
 }

 

MM..1
Super User
Apr 13, 2024 6:53 AM UTC

Flash possition can be writed only once. For next write require erase before. And your code is chaos of pointers.

Read and apply EEPROM emulation code for STM. an4894-how-to-use-eeprom-emulation-on-stm32-mcus-stmicroelectronics.pdf

Tesla DeLorean
Guru
Apr 14, 2024 5:59 PM UTC

You have an amazing skill of not really showing enough of the code to understand the context properly, and don't review the code you're interfacing with.

Need to pass a count of words

MY_FLASH_ReadN(0, (uint32_t*)CStore, (sizeof(CStore) + sizeof(uint32_t) - 1)/sizeof(uint32_t),DATA_TYPE_32);

 

Is CStore an array of pointer?

You have CStore[Counter] did you mean &CStore[Counter] ??

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
cjaya.2
cjaya.2Author
Associate III
Apr 15, 2024 5:10 PM UTC

Thank you for the reply. CStore is initialized as "static char CStore[4][10]={0}". I made the changes according to you have mentioned on the code. But still I am getting like this, if you check on the memory browser.Is there a possibility, if the string has "-32.445656556789" long ended decimal places when it is doing typecast of uint32_t is messing up storing on flash. 

memory2.jpg

 

 

Tesla DeLorean
Guru
Apr 15, 2024 5:20 PM UTC

A two dimensional array ?? None of your other code uses in that form, pretty hard to follow, and lacks comments.

Perhaps add some output, say hex dumping the memory prior to the MY_FLASH_WriteN() so you can be sure what you're attempting to write.

Check that's correct. Show that so we can see what's happening.

And then follow the code flow/execution into MY_FLASH_WriteN()

Debugging is about finding out the point at which desired behaviour stops, and incorrect behaviour starts, and why.

Not really sure why you need to layer in a bunch of third party code when you could use the HAL more directly

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..