Problem with converting arduino SPI into IDF

vishal.borle
Posts: 13
Joined: Thu May 07, 2020 1:32 pm

Problem with converting arduino SPI into IDF

Postby vishal.borle » Thu May 14, 2020 4:43 am

Hello to all,
We have started using ESP32 IDF in that SPI communication with ADE9000 chip. But problem getting able to read version.
Here is below code snippet.
  1. [void ade_cmd(spi_device_handle_t spi, const uint16_t cmd)
  2. {
  3.     esp_err_t ret;
  4.     spi_transaction_t t;
  5.     memset(&t, 0, sizeof(t));       //Zero out the transaction
  6.     t.length=16;                     //Command is 16 bits
  7.     t.tx_buffer=&cmd;               //The data is the cmd itself
  8.     t.user=(void*)0;                //D/C needs to be set to 0
  9.     ret=spi_device_polling_transmit(spi, &t);  //Transmit!
  10.     assert(ret==ESP_OK);            //Should have had no issues.
  11. }
  12.  
  13. //This function is called (in irq context!) just before a transmission starts. It will
  14. //set the D/C line to the value indicated in the user field.
  15. void lcd_spi_pre_transfer_callback(spi_transaction_t *t)
  16. {
  17.     int dc=(int)t->user;
  18.     gpio_set_level(PIN_NUM_CS, dc);
  19. }
  20.  
  21.  
  22. uint32_t ade_get_version(spi_device_handle_t spi)
  23. {
  24.     uint16_t Address = 0x000004FE;
  25.     uint16_t temp_address;
  26.  
  27.     temp_address = (((Address << 4) & 0xFFF0)+8);
  28.     ade_cmd(spi, temp_address);
  29.  
  30.     spi_transaction_t t;
  31.     memset(&t, 0, sizeof(t));
  32.     t.length=8*4;
  33.     t.flags = SPI_TRANS_USE_RXDATA;
  34.     t.user = (void*)1;
  35.  
  36.     esp_err_t ret = spi_device_polling_transmit(spi, &t);
  37.     assert( ret == ESP_OK );
  38. //    gpio_set_level(PIN_NUM_CS, 1);
  39.     printf("ADE9000 VERSION 1 byte: %02X%02X%02X%02X\n", t.rx_data[0],t.rx_data[1],t.rx_data[2],t.rx_data[3]);
  40.     printf("ADE9000 VERSION before: %08X\n", *(uint32_t*)t.rx_data);
  41.     return *(uint32_t*)t.rx_data;
  42. }
  43. void ade9000_init(spi_device_handle_t spi)
  44. {
  45.     gpio_set_direction(PIN_NUM_RST, GPIO_MODE_OUTPUT);
  46.      //Reset the display
  47.     gpio_set_level(PIN_NUM_RST, 0);
  48.     vTaskDelay(100 / portTICK_RATE_MS);
  49.     gpio_set_level(PIN_NUM_RST, 1);
  50.     vTaskDelay(100 / portTICK_RATE_MS);
  51.  
  52.     uint32_t ade_version = ade_get_version(spi);
  53.     printf("ADE9000 VERSION: %08X\n", ade_version);
  54.  
  55. }
  56.  
  57. void app_main(void)
  58. {
  59.     esp_err_t ret;
  60.     spi_device_handle_t spi;
  61.     spi_bus_config_t buscfg={
  62.         .miso_io_num=PIN_NUM_MISO,
  63.         .mosi_io_num=PIN_NUM_MOSI,
  64.         .sclk_io_num=PIN_NUM_CLK,
  65.         .quadwp_io_num=-1,
  66.         .quadhd_io_num=-1,
  67.         //.max_transfer_sz=PARALLEL_LINES*320*2+8
  68.     };
  69.     spi_device_interface_config_t devcfg={
  70. #ifdef CONFIG_LCD_OVERCLOCK
  71.         .clock_speed_hz=26*1000*1000,           //Clock out at 26 MHz
  72. #else
  73.         .clock_speed_hz=10*1000*1000,           //Clock out at 10 MHz
  74. #endif
  75.         .mode=0,                                //SPI mode 0
  76.         .spics_io_num=PIN_NUM_CS,               //CS pin
  77.         .queue_size=7,                          //We want to be able to queue 7 transactions at a time
  78.         //.pre_cb=lcd_spi_pre_transfer_callback,  //Specify pre-transfer callback to handle D/C line
  79.     };
  80.     //Initialize the SPI bus
  81.     ret=spi_bus_initialize(HSPI_HOST, &buscfg, DMA_CHAN);
  82.     ESP_ERROR_CHECK(ret);
  83.     //Attach the LCD to the SPI bus
  84.     ret=spi_bus_add_device(HSPI_HOST, &devcfg, &spi);
  85.     ESP_ERROR_CHECK(ret);
  86.     //Initialize the LCD
  87.     //lcd_init(spi);
  88.     ade9000_init(spi);
  89.     //Initialize the effect displayed
  90.     /*ret=pretty_effect_init();
  91.     ESP_ERROR_CHECK(ret);
  92.  
  93.     //Go do nice stuff.
  94.     display_pretty_colors(spi);*/
  95. }]
  96.  
  97. Working Arduino code is
  98. [Codebox=c file=Untitled.c][void ADE9000Class::SPI_Init(uint32_t SPI_speed , uint8_t chipSelect_Pin)
  99. {
  100.     SPI.begin();        //Initiate SPI port
  101.     SPI.beginTransaction(SPISettings(SPI_speed,MSBFIRST,SPI_MODE0));        //Setup SPI parameters
  102.     pinMode(chipSelect_Pin, OUTPUT);        //Set Chip select pin as output
  103.     digitalWrite(chipSelect_Pin, HIGH);     //Set Chip select pin high
  104.  
  105.     _chipSelect_Pin = chipSelect_Pin;
  106. }
  107. uint32_t ADE9000Class:: SPI_Read_32(uint16_t Address)
  108. {
  109.     uint16_t temp_address;
  110.     uint16_t temp_highpacket;
  111.     uint16_t temp_lowpacket;
  112.     uint32_t returnData;
  113.     digitalWrite(_chipSelect_Pin, LOW);
  114.     temp_address = (((Address << 4) & 0xFFF0)+8);
  115.     SPI.transfer16(temp_address);
  116.     temp_highpacket = SPI.transfer16(0);
  117.     temp_lowpacket = SPI.transfer16(0);
  118.     digitalWrite(_chipSelect_Pin, HIGH);
  119.     returnData = temp_highpacket << 16;
  120.     returnData = returnData + temp_lowpacket;
  121.     return returnData;
  122.  
  123. }]
  124. I am new in IDF please provide me sample SPI code with how write and read data.
  125. Looking forward your response.
  126. Thank you........

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

Re: Problem with converting arduino SPI into IDF

Postby ESP_Sprite » Thu May 14, 2020 7:55 am

Do you happen to have a LA or oscilloscope you can use to look at the signals? Makes it easier to figure out what's going wrong here.

vishal.borle
Posts: 13
Joined: Thu May 07, 2020 1:32 pm

Re: Problem with converting arduino SPI into IDF

Postby vishal.borle » Thu May 14, 2020 10:09 am

Thanks for the replay....
NO currently not having any instruments. Can you tell me Is my SPI program looks correct?
How I can do my SPI code working.
Thanks...

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

Re: Problem with converting arduino SPI into IDF

Postby ESP_Sprite » Thu May 14, 2020 2:03 pm

Nothing that I can see. lcd_spi_pre_transfer_callback in your code is a bit suspicious, but because the line that uses it is commented out, it shouldn't stop your program from working.

vishal.borle
Posts: 13
Joined: Thu May 07, 2020 1:32 pm

Re: Problem with converting arduino SPI into IDF

Postby vishal.borle » Fri May 15, 2020 4:07 am

But in my program I am not using any D/C line so that's why I have commented this line.

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

Re: Problem with converting arduino SPI into IDF

Postby ESP_Sprite » Fri May 15, 2020 7:20 am

Yes, I saw, and that is fine, it's just a bit confusing to anyone who reads it that that routine still is in there.

vishal.borle
Posts: 13
Joined: Thu May 07, 2020 1:32 pm

Re: Problem with converting arduino SPI into IDF

Postby vishal.borle » Fri May 15, 2020 7:37 am

I am getting strange for next What should I do now, We need to find solution as soon as possible. My other confusion is it neccessary to send dummy byte to read bytes from slave.
I am send 16 bit address to read interface ADE chip version.
Here is below code:
  1. [void ade_cmd(spi_device_handle_t spi, const uint16_t cmd)
  2. {
  3.     esp_err_t ret;
  4.     spi_transaction_t t;
  5.     memset(&t, 0, sizeof(t));       //Zero out the transaction
  6.     t.length=16;                     //Command is 16 bits
  7.     t.tx_buffer=&cmd;               //The data is the cmd itself
  8.     t.user=(void*)0;                //D/C needs to be set to 0
  9.     ret=spi_device_polling_transmit(spi, &t);  //Transmit!
  10.     assert(ret==ESP_OK);            //Should have had no issues.
  11. }
  12.  
  13.  
  14. uint32_t ade_get_version(spi_device_handle_t spi)
  15. {
  16.     //get_id cmd
  17.     uint16_t Address = 0x000004FE; //Address of read ADE version 16 bit address
  18.     uint16_t temp_address;
  19. //  gpio_set_level(PIN_NUM_CS, 0); // no necessary to handle chip select pin
  20.     temp_address = (((Address << 4) & 0xFFF0)+8); // this done in arduino library code
  21.     ade_cmd(spi, temp_address);
  22.  
  23.     spi_transaction_t t;
  24.     memset(&t, 0, sizeof(t));
  25.     t.length=8*4; //length of recive buffer in bit
  26.     t.flags = SPI_TRANS_USE_RXDATA;
  27.     t.user = (void*)1;
  28. //    esp_err_t ret = spi_device_polling_transmit(spi, &t);
  29. //    assert( ret == ESP_OK );
  30.     esp_err_t ret = spi_device_transmit(spi, &t);
  31.     assert( ret == ESP_OK );
  32.  
  33. //    gpio_set_level(PIN_NUM_CS, 1);
  34.     printf("ADE9000 VERSION all bytes: %02X%02X%02X%02X\n", t.rx_data[0],t.rx_data[1],t.rx_data[2],t.rx_data[3]);
  35.     printf("ADE9000 VERSION before: %08X\n", *(uint32_t*)t.rx_data);
  36.     return *(uint32_t*)t.rx_data;
  37. }]

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

Re: Problem with converting arduino SPI into IDF

Postby ESP_Sprite » Sat May 16, 2020 5:56 am

Sorry, you are getting a strange whatnow?

vishal.borle
Posts: 13
Joined: Thu May 07, 2020 1:32 pm

Re: Problem with converting arduino SPI into IDF

Postby vishal.borle » Sat May 16, 2020 7:16 am

Can you please give me ans of few questions:
I am trying to read version of ADE9000 chip for SPI communication with below code
uint32_t ade_get_version(spi_device_handle_t spi)
{
uint16_t Address = 0x000004FE; //Address of read ADE version 16 bit address
uint16_t temp_address;
uint32_t returnData;
// gpio_set_level(PIN_NUM_CS, 0); // no necessary to handle chip select pin
temp_address = (((Address << 4) & 0xFFF0)+8); // this done in arduino library code

spi_transaction_t t;
memset(&t, 0, sizeof(t));
t.length=8*4; //length of recive buffer in bit
t.cmd = temp_address;
// t.tx_buffer=0;
t.flags = SPI_TRANS_USE_RXDATA;
esp_err_t ret = spi_device_transmit(spi, &t);
assert( ret == ESP_OK );

// gpio_set_level(PIN_NUM_CS, 1);
printf("ADE9000 VERSION all bytes: %02X%02X%02X%02X\n", t.rx_data[0],t.rx_data[1],t.rx_data[2],t.rx_data[3]);
printf("ADE9000 VERSION before: %08X\n", *(uint32_t*)t.rx_data);
return *(uint32_t*)t.rx_data;
}
This code return ADE9000 VERSION: EF097F00 but vesion is FE13DE
In datasheet given SPI waveform I have attached here.
Questions:
Is my code snippet is looks correct?
If any changes is need where I need to change?

Thanks for your support..!!
Attachments
spi.PNG
spi.PNG (27.88 KiB) Viewed 7396 times

rsimpsonbusa
Posts: 124
Joined: Tue May 17, 2016 8:12 pm

Re: Problem with converting arduino SPI into IDF

Postby rsimpsonbusa » Mon May 18, 2020 2:09 am

In

Code: Select all

spi_transaction_t t;
    memset(&t, 0, sizeof(t));
    t.length=8*4;
    t.flags = SPI_TRANS_USE_RXDATA;
    t.user = (void*)1;
try setting the t.rxlength to a specific number fo bits. If u want all 4 bytes of USE_RXDATA then set it to 32. Maybe this can help.

I looks like u are confusing the transmission data length (t.length) with the receive length (t.rxlength) buffer for receiving.

As I see it, the SPI will try to SEND 32 bits from the t.tx_buffer (not set in your code) and receive 0 bits into the internal 4 bytes buffer (USE_RX_DATA).

BTW, bzero(&t, sizeof(t)) is faster...

Hope it helps.

Who is online

Users browsing this forum: No registered users and 175 guests