I have a 32-bit microcontroller and have just written a double to the flash memory. I now want to read the double back from memory, but I am doing something illegal as the microcontroller goes to the hard fault handler.
First I tried:
double Flash_Read64(uint32_t address)
{
return *((double *) address);
}
but this did not work. Is it because address is 32-bit and the (double *) expect a 64-bit?
I then tried:
double Flash_Read64(uint32_t address)
{
uint64_t temp;
double * tempPtr = (double *) &temp;
//Increment address.
address += 4;
//Get MSB.
temp = (*((uint32_t *) (address)));
//Shift MSB to upper half.
temp = (temp << 32);
//Decrement address.
address -= 4;
//Get LSB.
temp |= (*((uint32_t *) address));
return *tempPtr;
}
but still not working.
Any suggestions?
EDIT:
bool_t Flash_Write64Address(uint32_t address, double data)
{
uint32_t MSB, LSB;
uint32_t * tempPtr = (uint32_t *) &data;
//Get LSB.
LSB = tempPtr[0];
//Write LSB to flash.
flashStatus = FLASH_ProgramWord(address, LSB);
if(flashStatus != FLASH_COMPLETE)
{
DEBUG("Failed to write to flash at address: %u", (unsigned int) address);
return FALSE;
}
//Increment address.
address += 4;
//Get MSB.
MSB = tempPtr[1];
//Write MSB to flash.
flashStatus = FLASH_ProgramWord(address, MSB);
if(flashStatus != FLASH_COMPLETE)
{
DEBUG("Failed to write to flash at address: %u", (unsigned int) address);
return FALSE;
}
return TRUE;
}