ESP32 inverted byte order due to Little Endian ( how to invert back )
Posted: Wed Nov 10, 2021 7:05 am
Hello. I have a project where I need to save data inside flash memory. I have a structure of data:
Now I want to write this structure to the flash memory. I pass the pointer of the structure to my function and assign
When I read back the memory that I have written, the result looks like:
Now if you notice, the bytes are swapped due to being Little Endian if I am correct. This is an issue for me, because I also want to send this data_packet via BLE to my phone. And when I try to send this structure via BLE it sens the following bytes:
but instead, it should send:
Please help me understand why this is happening and how to fix this
Code: Select all
struct flash_data_s{
uint32_t data1;
uint16_t data2;
uint8_t data3;
};
flash_data_s flash_data;
flash_data.data1 = 0x01020304;
flash_data.data2 = 0x0506;
flash_data.data3 = 0x09;
FLASH_write_7_byte_chunk(flash,0,uint8_t*(&flash_data));
Code: Select all
SPITransaction.tx_buffer = data_pointer;
bool FLASH_write_7_byte_chunk(spi_device_handle_t spi,uint16_t chunk, uint8_t* data_pointer){
{
esp_err_t ret;
spi_transaction_t SPITransaction;
if(FLASH_check_if_write_enabled(external_flash)==false){ // returns true if write is already enabled
FLASH_write_enable(external_flash);
}
if(FLASH_check_if_busy(spi) == true){ // return true if flash is not busy
memset(&SPITransaction, 0, sizeof(SPITransaction)); //Zero out the transaction
uint32_t addr = 0;
uint8_t command_byte = 0x02;
SPITransaction.cmd=command_byte;
SPITransaction.addr=addr;
SPITransaction.tx_buffer = data_pointer;
SPITransaction.length = 8*7;
ret = spi_device_polling_transmit(spi, &SPITransaction);
assert(ret==ESP_OK);
next_chunk++;
return 1;
}
return 0;
}
}
When I read back the memory that I have written, the result looks like:
Code: Select all
data_read[0] = 0x04;
data_read[1] = 0x03;
data_read[2] = 0x02;
data_read[3] = 0x01;
data_read[4] = 0x06;
data_read[5] = 0x05;
data_read[6] = 0x09;
Code: Select all
0x04030201060509
Code: Select all
0x01020304050609