Skip to main content
Visitor II
April 12, 2024
Question

storing string's on flash

  • April 12, 2024
  • 3 replies
  • 4569 views

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.

    3 replies

    Super User
    April 12, 2024

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

    Graduate II
    April 12, 2024

    >>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.

     

    cjaya.2Author
    Visitor II
    April 13, 2024

    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);
     }

     

    Graduate II
    April 13, 2024

    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

    Graduate II
    April 14, 2024

    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] ??

    cjaya.2Author
    Visitor II
    April 15, 2024

    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

     

     

    Graduate II
    April 15, 2024

    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