2020-05-04 06:00 AM
hi guys, im working on stm32l412kb mcu using keil v5 uvision and i ve found it difficult to write and read flash memory. all the explanation online are not relevant for my mcu. that the way i'm trying to confirm my actions, if anyone understand the problem it would be extremely helpful.
{EraseInitStruct.TypeErase=FLASH_TYPEERASE_PAGES;
EraseInitStruct.Banks=FLASH_BANK_1;
EraseInitStruct.Page=255;
EraseInitStruct.NbPages=1;
HAL_FLASH_Unlock();
HAL_FLASHEx_Erase(&EraseInitStruct, &pageError);
HAL_UART_Transmit(&huart1, (uint8_t*)"tried2\n\r", 13,100);
data=*(uint64_t *)0x08000004;
HAL_UART_Transmit(&huart1,(uint8_t*)data, 13,100);
HAL_UART_Transmit(&huart1, (uint8_t*)"\n\rtried3\n\r", 13,100);
HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD,0x08000004,0x01u);
data=*(uint64_t *)0x08000004;
HAL_UART_Transmit(&huart1,(uint8_t*)value, 13,100);
if (value==0x01u)
HAL_UART_Transmit(&huart1, (uint8_t*)"we made it\n\r", 20,100);
else
HAL_UART_Transmit(&huart1, (uint8_t*)"\n\r we are losers\n\r", 20,100);
HAL_FLASH_Lock();
HAL_Delay(100);}
2020-05-04 06:41 AM
> EraseInitStruct.Page=255;
> EraseInitStruct.NbPages=1;
So you're trying to erase page 255? How many pages of flash does that chip have? Datasheet says 64.
2020-05-04 06:59 AM
Thanks so much for the help, you're right. But even though I changed it, the code still doesn't work. The values are not equal to each other:
EraseInitStruct.TypeErase=FLASH_TYPEERASE_PAGES;
EraseInitStruct.Banks=FLASH_BANK_1;
EraseInitStruct.Page=63;
EraseInitStruct.NbPages=1;
HAL_FLASH_Unlock();
HAL_FLASHEx_Erase(&EraseInitStruct, &pageError);
HAL_UART_Transmit(&huart1, (uint8_t*)"tried2\n\r", 13,100);
data=*(uint64_t *)0x08000004;
HAL_UART_Transmit(&huart1,(uint8_t*)data, 13,100);
HAL_UART_Transmit(&huart1, (uint8_t*)"\n\rtried3\n\r", 13,100);
HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD,0x08000004,0x01u);
data=*(uint64_t *)0x08000004;
HAL_UART_Transmit(&huart1,(uint8_t*)value, 13,100);
if (value==0x01u)
HAL_UART_Transmit(&huart1, (uint8_t*)"we made it\n\r", 20,100);
else
HAL_UART_Transmit(&huart1, (uint8_t*)"\n\r we are losers\n\r", 20,100);
HAL_FLASH_Lock();
HAL_Delay(100);}
2020-05-04 07:30 AM
Read chapter 3 of the reference manual, with close attention to the section titled Flash main memory programming sequences.
data=*(uint64_t *)0x08000004;
Data should be aligned to doubleword addresses, i.e. ones that are evenly divisible by 8. The address above is not.
Moreover, it does not point to the address range that have been erased, but attempting to overwrite the reset address of the program itself. See Flash memory organization at the top of chapter 3.
2020-05-04 07:35 AM
thanx berendi, can you please send me a link to that menual, just to be sure im reading the correct file.
and if u can suggest a different way to do that it would be great!
2020-05-04 07:57 AM
https://www.st.com/en/microcontrollers-microprocessors/stm32l412kb.html
Data Sheet
https://www.st.com/resource/en/datasheet/stm32l412kb.pdf
Reference Manual
Use the address of the page/sector you are erasing, and don't use the front of FLASH where your executing code is situated.
Find a better way to send 64-bit values, perhaps sprintf() into ASCII, and then send. Write a subroutine to wrap the UART Transmit so it computes the length of the string, rather than you putting in constants.
char asciidata[32];
uint64_t value =*(uint64_t *)0x08000004;
HAL_UART_Transmit(&huart1,(uint8_t*)asciidata, sprintf(asciidata,"%lx\n", value),250); // don't recall 64-bit format, but sprintf() returns length
One the readback use the value, not data variable.
2020-05-06 03:23 AM
data=*(uint64_t *)0x08000004;
This code can be written only by someone, who hasn't read anything at all! The reference manual section 3.3.7 describes a different programming procedure and HAL_FLASH_Program() code also shows how to do it.
2020-05-06 03:30 AM
hi, im new in this.
i tried to read it but apparently didnt understood too much.
if you dont mind to enplane a little bit more.
all im trying to do is to write a number to 0x08000004, then to read it for confirmation.
what should i do?
2020-05-06 03:57 AM
Then go back to the basics. You won't get too far without it.
How is computer memory organized in general, and specifically on the Cortex-M platform? What is a byte, halfword, word, doubleword? How are they stored in memory, how can they be addressed? What is alignment, what is the difference between aligned an unaligned addresses and accesses?
How does flash memory work? What is the basic difference from the user's point of view between eeprom and flash type memory?
How is a program stored in memory in general, and specifically on the Cortex-M platform? Where and how does program execution start, what happens after reset?
2020-05-06 07:08 AM
ive been reading those topics since i started work on the project. i am not exactly understand the last 3 topics you mentioned so thats the point where i stoped. if you can direct me specifically with stm32l412 it would be great couse all the The things I find are associated with other processors.
If you can specifically point out the problem with my code it would be best.