Newbie here. I am currently working on a project that involves saving password on MCU(NUC200LE3AN) Flash memory.
These codes works just fine. After writing it I am able to read the exact value of user_password1 even after the MCU restarts.
FMC_Erase(PASSWORD1_LOCATION); //u32addr
if (*(uint32_t *)(PASSWORD1_LOCATION) == 0xffffffff)
{
uint32_t user_password1 = "1234";
FMC_Write(PASSWORD1_LOCATION,user_password1);
}
uint32_t ReadPass1 = *(uint32_t *)(PASSWORD1_LOCATION);
UART_Write(UART0,ReadPass1,4); //4 -> string length
_UART_SENDBYTE(UART0,0x0D);
But I will be using uint8_t array of 5(including the terminating '\0') as a source in changing my password. Example:
FMC_Erase(PASSWORD1_LOCATION);
uint8_t new_password[5];
new_password[0] = '1';
new_password[1] = '2';
new_password[2] = '3';
new_password[3] = '4';
new_password[4] = '\0';
if (*(uint32_t *)(PASSWORD1_LOCATION) == 0xffffffff)
{
user_password1 = (uint32_t *)(new_password);
FMC_Write(PASSWORD1_LOCATION,user_password1);
}
uint32_t ReadPass1 = *(uint32_t *)(PASSWORD1_LOCATION);
UART_Write(UART0,ReadPass1,4); //4 -> string length
_UART_SENDBYTE(UART0,0x0D);
With this, I can write the password and read it as long as those fix values are there and those fix values are just for default password. After I changed my password as long as i don't turn off the MCU it could still be read which is not acceptable due to the MCU needs to be turned on/off. If I apply this and then restart the MCU, reading PASSWORD1_LOCATION returns garbage/null.
Is there a way to turn this:
uint8_t new_password[5];
new_password[0] = '1';
new_password[1] = '2';
new_password[2] = '3';
new_password[3] = '4';
new_password[4] = '\0';
Into This:
uint32_t user_password1 = "1234";
I hope you know what I mean. Thank you.
*(uint32_t *)
probably does not work since it's not volatile so all those reads and writes that uses it would very likely fail. Also youruint8_t new_password[5];
uses 8 bit big endian ASCII encoding, butuint32_t user_password1 = "1234";
(BTW should be1234
not"1234"
) is 32 bit unsigned binary integer, native endian. Those two representations are not compatible with each other. – user3528438FMC_Write()
FMC_Read()
FMC_Erase()
and yet you wrote your own "read" function with regular pointer deference, and then ask people why it doesn't work? Why do you think it could work? – user3528438uint32_t user_password1 = "1234";
-->uint32_t user_password1 = 0x31323334;
andFMC_Write(PASSWORD1_LOCATION,user_password1);
-->FMC_Write(PASSWORD1_LOCATION, &user_password1);
– LPs