Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TW#23413] spi master management of byte order when the size of not byte aligned is crazy #2062

Closed
3 tasks
Jegeva opened this issue Jun 13, 2018 · 7 comments
Closed
3 tasks

Comments

@Jegeva
Copy link

Jegeva commented Jun 13, 2018

----------------------------- Delete below -----------------------------

If your issue is a general question, starts similar to "How do I..", or is related to 3rd party development kits/libs, please discuss this on our community forum at esp32.com instead.

INSTRUCTIONS

----------------------------- Delete above -----------------------------

Environment

  • Development Kit: [ESP32-Wrover-Kit|]
  • Kit version (for WroverKit/PicoKit/DevKitC): [NA]
  • Core (if using chip or module): [NA]
  • IDF version (git rev-parse --short HEAD to get the commit id.): 0e501e5
  • Development Env: [Make]
  • Operating System: [Debian]
  • Power Supply: [USB]

Problem Description

//Detailed problem description goes here.
while sending data with the spi master :

void send_9(uint16_t data){
esp_err_t ret;
spi_transaction_t t;
uint16_t workaround = data;
memset(&t, 0, sizeof(t)); //Zero out the transaction
t.length=9; //Command is >9< bits
t.tx_buffer=&workaround; //The data is the cmd itself
ret=spi_device_transmit(spi, &t);//Transmit!
assert(ret==ESP_OK);
}

Expected Behavior

data_bit8, data_bit7,...,data_bit0 as expected

Actual Behavior

data_bit7,...,data_bit0,data_bit15 is sent (check with a logic analyser)

Steps to repropduce

hook an esp32 to a LA,
set it up to use VSPI

send 0x00ff with a size of 9 : 0b111111110 is sent
send 0x80ff with a size of 9 : 0b111111111 is sent

this makes no sense

// It helps if you attach a picture of your setup/wiring here.

Code to reproduce this issue

-> use the spi master exemple, change the size, test the patterns i explained with a LA.

// the code should be wrapped in the ```cpp tag so that it will be displayed better.
#include "esp_log.h"

void app_main()
{
    
}

// If your code is longer than 30 lines, GIST is preferred.

Debug Logs

Debug log goes here, should contain the backtrace, as well as the reset source if it is a crash.
Please copy the plain text here for us to search the error log. Or attach the complete logs but leave the main part here if the log is *too* long.

Other items if possible

  • sdkconfig file (attach the sdkconfig file from your project folder)
  • elf file in the build folder (note this may contain all the code details and symbols of your project.)
  • coredump (This provides stacks of tasks.)
@xiongyumail
Copy link
Contributor

Hi,@Jegeva
Can you provide a completed test program for us to use? Let us solve your problem faster.

@FayeY FayeY changed the title spi master management of byte order when the size of not byte aligned is crazy [TW#23413] spi master management of byte order when the size of not byte aligned is crazy Jun 19, 2018
@FayeY
Copy link
Collaborator

FayeY commented Jun 22, 2018

Any update?

Hi @Jegeva,

Could you provide a test project so we can reproduce the problem and help solve it?

Thanks.

@ginkgm
Copy link
Collaborator

ginkgm commented Jun 27, 2018

Hi @Jegeva ,
ESP32 is a little endian system, and this is a common issue existing in little endien systems.

In such systems, an unit32_t variable a=0x12345678 is placed like following in the memory.
0x78, 0x56, 0x34, 0x12

However the DMA don't know how your data is organized (as uint8_t, uint16_t or uint32_t), it always assume your data are uint_8s. The DMA takes data from the memory from low to high address, byte by byte. i.e. 0x78->0x56->0x34->0x12, and send from the MSB to LSB, namely:
01111000->01010110->00110100->00010010

This is how ESP32 hardware works.

To send uint16_t and uint32_t and even random length of data, please:

  1. Move your data to start from the MSB of your variable (bit15 for uint16_t and bit31 for uint32_t).
  2. use __builtin_bswap16 or __builtin_bswap32 to re-order the MSB byte to the lowest address

To resolve data received, please do in the reverse way.

Example:

#define SPI_SHIFT_DATA_8(data, len) (data<<(8-len))
#define SPI_SHIFT_DATA_16(data, len) __builtin_bswap16(data<<(16-len))
#define SPI_SHIFT_DATA_32(data, len) __builtin_bswap32(data<<(32-len))

#define SPI_GET_DATA_8(data, len) (data>>(8-len))
#define SPI_GET_DATA_16(data, len) (__builtin_bswap16(data)>>(16-len))
#define SPI_GET_DATA_32(data, len) (__builtin_bswap32(data)>>(32-len))

void send_9(uint16_t data){
    esp_err_t ret;
    spi_transaction_t t;
    uint16_t workaround = SPI_SHIFT_DATA_16(data, 9);
    memset(&t, 0, sizeof(t)); //Zero out the transaction
    t.length=9; //Command is >9< bits
    t.tx_buffer=&workaround; //The data is the cmd itself
    ret=spi_device_transmit(spi, &t);//Transmit!
    assert(ret==ESP_OK);
}

We plan to provide macros in the future version.

Another notice, please take care that your variable sent to the driver should be 32-bit aligned.

@ginkgm
Copy link
Collaborator

ginkgm commented Jun 27, 2018

BTW, there're such issues with command and address fields, and are re-ordered by the driver:

https://github.com/espressif/esp-idf/blob/master/components/driver/spi_master.c#L730

@Jegeva
Copy link
Author

Jegeva commented Jun 27, 2018 via email

@ginkgm
Copy link
Collaborator

ginkgm commented Jun 27, 2018

Update:

#define SPI_SHIFT_DATA(data, len) __builtin_bswap32((uint32_t)data<<(32-len))
#define SPI_REARRANGE_DATA(data, len) (__builtin_bswap32(data)>>(32-len))

@igrr igrr closed this as completed in 3d23fe9 Jul 4, 2018
@ginkgm
Copy link
Collaborator

ginkgm commented Jul 4, 2018

Hi @Jegeva ,
The IDF is updated according to your problem. Please update and have a try.
If there's any issue, please come and feel free to reopen this.

catalinio pushed a commit to catalinio/pycom-esp-idf that referenced this issue Jun 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants