Simple read/write example on full-duplex spi

dev4all12358
Posts: 2
Joined: Mon Sep 06, 2021 2:10 pm

Simple read/write example on full-duplex spi

Postby dev4all12358 » Mon Sep 06, 2021 2:18 pm

Hello,

I try to do a simple read/write on SPI when my ESP is in master mode.

The write code seems to work properly:

Code: Select all

  ESP_LOGI(TAG, "Sensor_IO_Write");

  esp_err_t err;
  spi_transaction_t t = {
      .cmd       = WriteAddr,
      .length    = nBytesToWrite * 8,
      .flags     = 0,
      .tx_buffer = pBuffer,
      .user      = (void*) &sensor_ctx,
  };

  ESP_LOGI(TAG, "Sensor_IO_Write: spi_device_transmit");
  err = spi_device_transmit(sensor_ctx.spi, &t);

I check with my spi analyser, an I get the proper data.

The read code doesn't seems to works:

Code: Select all

 esp_err_t err;
  uint8_t pBuffer2[2];
  spi_transaction_t t = {
      .cmd       = ReadAddr,
      .length    = 8 * nBytesToRead,
      .rxlength  = 8 * nBytesToRead,
      .flags     = SPI_TRANS_USE_RXDATA,
      .rx_buffer = pBuffer2,
      .user      = (void*) &sensor_ctx,
  };

  err = spi_device_polling_transmit(sensor_ctx.spi, &t);

  uint8_t data = ((uint8_t*) pBuffer2)[0];
On my analyser I see 0x11 ( my command) and 0x63 the read data. But my buffer is filled with 192.

What do I made wrong?

ESP_Sprite
Posts: 8921
Joined: Thu Nov 26, 2015 4:08 am

Re: Simple read/write example on full-duplex spi

Postby ESP_Sprite » Tue Sep 07, 2021 12:55 am

Code: Select all

      .flags     = SPI_TRANS_USE_RXDATA,
      .rx_buffer = pBuffer2,

dev4all12358
Posts: 2
Joined: Mon Sep 06, 2021 2:10 pm

Re: Simple read/write example on full-duplex spi

Postby dev4all12358 » Tue Sep 07, 2021 10:22 am

ESP_Sprite wrote:
Tue Sep 07, 2021 12:55 am

Code: Select all

      .flags     = SPI_TRANS_USE_RXDATA,
      .rx_buffer = pBuffer2,

It seems to work thank you.

wifibeginner
Posts: 5
Joined: Fri May 13, 2022 9:22 am

Re: Simple read/write example on full-duplex spi

Postby wifibeginner » Fri Aug 19, 2022 5:39 am

Hi,
I've got the same issue where t.rx_buffer keeps a value of 108 regardless of the message value. I have MOSI connected to MISO with a wire to test the input buffer at the moment, and this is my function for sending three bytes through SPI and monitoring the input buffer:
  1. void send_spi_singlebyte (uint8_t code1, uint8_t code2, uint8_t message_value, char* message_description) {
  2.  
  3.     char code1_char[16]={code1};
  4.     char code2_char[16]={code2};
  5.     char send_pulse[16]={message_value};
  6.  
  7.     char*transmit_variable = malloc(sizeof(transmit_variable));
  8.     transmit_variable = code1_char;
  9.     uint8_t receive_variable[2];
  10.  
  11.     t.cmd = 0;
  12.     t.addr = 0;
  13.     t.flags     = SPI_TRANS_USE_RXDATA;
  14.     t.length=8;                       // 1 byte
  15.     t.rxlength = 8;                       // 1 byte
  16.     t.tx_buffer=transmit_variable;
  17.     t.rx_buffer=receive_variable;
  18.  
  19.  
  20.     printf("------------------Start of SPI Transmission------------------\n");
  21.     printf("Message: %s\n", message_description);
  22.  
  23.     //Send Code Part 1
  24.     transmit_variable = code1_char;
  25.     t.tx_buffer=transmit_variable;
  26.     ret=spi_device_polling_transmit(handle, &t);
  27.     printf("Sent: %d\n", code1);
  28.  
  29.     //Send Code Part 2
  30.     transmit_variable = code2_char;
  31.     t.tx_buffer=transmit_variable;
  32.     ret=spi_device_polling_transmit(handle, &t);
  33.     printf("Sent: %d\n", code2);
  34.  
  35.     //Send Value
  36.     transmit_variable = send_pulse;
  37.     t.tx_buffer=transmit_variable;
  38.     ret=spi_device_polling_transmit(handle, &t);
  39.     printf("Sent: %d\n", message_value);
  40.  
  41.     printf("------------------End of SPI Transmission------------------\n");
  42.     printf(" \n");
  43.  
  44.     uint8_t data = ((uint8_t*) receive_variable)[0];
  45.     if (data >1){
  46.         gpio_set_level(OUTPUT_GPIO, 1);
  47.     }
  48.     printf("Received: %d\n", data);
  49. }
I have tried initialising through
  1. receive_variable[0]=0
but this results in the value always returning as '0' rather than 108.

At the moment I'm just trying to get rx_buffer value to change when a message is sent to the bus but have had no luck.
The ultimate goal is to be able to constantly monitor and receive all messages that are sent to the bus.

Device: ESP32S2

Regards,
wifibeginner

ESP_Sprite
Posts: 8921
Joined: Thu Nov 26, 2015 4:08 am

Re: Simple read/write example on full-duplex spi

Postby ESP_Sprite » Fri Aug 19, 2022 12:54 pm

You are misunderstanding why I quoted the two lines in my message above. I quoted it because they are mutually exclusive: either you store the data directly into the trans struct and set the flag indicating this, or you use an external buffer and do not use the flag. (Also, your line with 'malloc' makes no sense.)

wifibeginner
Posts: 5
Joined: Fri May 13, 2022 9:22 am

Re: Simple read/write example on full-duplex spi

Postby wifibeginner » Fri Aug 19, 2022 2:33 pm

Hi ESP_Sprite,

Thanks that works. Makes a lot more sense now.

Regards,
wifibeginner

Who is online

Users browsing this forum: ESP_rrtandler, Majestic-12 [Bot] and 143 guests