2

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.

4
  • first of all *(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 your uint8_t new_password[5]; uses 8 bit big endian ASCII encoding, but uint32_t user_password1 = "1234"; (BTW should be 1234 not "1234") is 32 bit unsigned binary integer, native endian. Those two representations are not compatible with each other. Commented Dec 1, 2016 at 15:56
  • 1
    Seriously, you are provided with library function like FMC_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? Commented Dec 1, 2016 at 16:02
  • uint32_t user_password1 = "1234"; --> uint32_t user_password1 = 0x31323334; and FMC_Write(PASSWORD1_LOCATION,user_password1); --> FMC_Write(PASSWORD1_LOCATION, &user_password1); Commented Dec 1, 2016 at 16:24
  • 1
    @user3528438 I made it "1234" because its a password which normally is a char. I mean I can change my password to "12AB". Thanks for your answers! Commented Dec 1, 2016 at 17:14

1 Answer 1

1

If you really want to store ascii values, you can simply translate it to its value in hex:

"1234" will be 0x31 0x32 0x33 0x34 0x00

To store it into a uint32_t, get rid of the null terminator and

FMC_Erase(PASSWORD1_LOCATION); //u32addr 
if (*(uint32_t *)(PASSWORD1_LOCATION) == 0xffffffff)
{
    uint32_t user_password1 = 0x31323334;
    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);
Sign up to request clarification or add additional context in comments.

2 Comments

how about if its in an array? can i do this? uint8_t new_pass1[4]; new_pass1[0] = 0x31000000; new_pass1[1] = 0x00320000; new_pass1[2] = 0x00003300; new_pass1[3] = 0x00000034; then uint32_t user_password1 = new_pass[0] + new_pass[1] + . . . .
No. You can do uint8_t new_array [4] = { 0x31, 0x32, 0x33, 0x34 };

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.