ESP32 inverted byte order due to Little Endian ( how to invert back )

zazas321
Posts: 231
Joined: Mon Feb 01, 2021 9:41 am

ESP32 inverted byte order due to Little Endian ( how to invert back )

Postby zazas321 » 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:

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));


Now I want to write this structure to the flash memory. I pass the pointer of the structure to my function and assign

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;

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:

Code: Select all

0x04030201060509
but instead, it should send:

Code: Select all

0x01020304050609
Please help me understand why this is happening and how to fix this

User avatar
rudi ;-)
Posts: 1727
Joined: Fri Nov 13, 2015 3:25 pm

Re: ESP32 inverted byte order due to Little Endian ( how to invert back )

Postby rudi ;-) » Wed Nov 10, 2021 7:48 am

https://github.com/espressif/esp-idf/is ... -400629287
https://demo-dijiudu.readthedocs.io/en/ ... r.html#id4

Code: Select all

SPI_DEVICE_TXBIT_LSBFIRST
Transmit command/address/data LSB first instead of the default MSB first.

Code: Select all

SPI_DEVICE_RXBIT_LSBFIRST
Receive data LSB first instead of the default MSB first.

Code: Select all

SPI_DEVICE_BIT_LSBFIRST
Transmit and receive LSB first.
-------------------------------------
love it, change it or leave it.
-------------------------------------
問候飛出去的朋友遍全球魯迪

zazas321
Posts: 231
Joined: Mon Feb 01, 2021 9:41 am

Re: ESP32 inverted byte order due to Little Endian ( how to invert back )

Postby zazas321 » Thu Nov 11, 2021 5:16 am

Thank you all for suggestions. I got it to work by inverting the bits when writing to flash memory that way, when I read it back, it is in required format.

Who is online

Users browsing this forum: No registered users and 64 guests