ESP32 IDF SPI to receive data from an external ADC

taifarif94
Posts: 1
Joined: Wed Aug 25, 2021 9:32 pm

ESP32 IDF SPI to receive data from an external ADC

Postby taifarif94 » Wed Aug 25, 2021 9:54 pm

Hi everyone,

I want to receive values from an external ADC over SPI to my ESP32 Wroom development board. I am using an external library for this purpose (https://github.com/natanaeljr/esp32-SPIbus). I have modified it a bit to suit my needs. I have already achieved this with the Arduino framework but because of the unwanted delay of the CS signal, I have switched over to the IDF framework which made the CS delay negligible.

The issue is that the data I have to receive from the sensor is 16 bits long (with two leading zeros and a zero at the end, that I can get rid of using masking.) I do receive the data but only the last 8 bits. I have also tried using the macro SPI_SWAP_DATA_RX but that does not give me the correct data. I am using the correct pin configuration and the correct SPI mode because I have cross-verified it with my Arduino framework code.

Please review the code below and let me know if I can provide you with any additional information. I have left some commented code so you guys can have an idea of the things I have tried.

main.cpp

Code: Select all

#define SPI_MODE  0
#define MISO_PIN  12
#define MOSI_PIN  -1
#define SCLK_PIN  14
#define CS_PIN    2
#define SPI_CLOCK 20000000  // 1 MHz



extern "C" void app_main() {
    printf("SPIbus Example \n");
    fflush(stdout);

    SPI_t &mySPI = hspi;  // vspi and hspi are the default objects
    
    spi_device_handle_t device;
    ESP_ERROR_CHECK( mySPI.begin(MOSI_PIN, MISO_PIN, SCLK_PIN));
    ESP_ERROR_CHECK( mySPI.addDevice(SPI_MODE, SPI_CLOCK, CS_PIN, &device));

    uint8_t buffer[6];
    while (1) {
        ESP_ERROR_CHECK(mySPI.readBytes(device, 0, 6, buffer));
        
      //  ESP_ERROR_CHECK(mySPI.readBits(device, 0, 1, 16, buffer));
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }

    mySPI.removeDevice(device);
    mySPI.close();
    vTaskDelay(portMAX_DELAY);
}
SPIBus.cpp

Code: Select all

esp_err_t SPI::readBytes(spi_device_handle_t handle, uint8_t regAddr, size_t lengthy, uint8_t *data) {
    if(lengthy == 0) return ESP_ERR_INVALID_SIZE;
    spi_transaction_t transaction;
    //for (int i=0; i<4; i++)
    //transaction.rx_data[i]=0;
    uint32_t dats;
 //One
    uint8_t rxdata[4] = {0};
    
    uint8_t length =16*2;
    

    //transaction.flags = 0;
    transaction.cmd = 0;

   // transaction.addr = regAddr | SPIBUS_READ;
    transaction.addr = regAddr;
    //transaction.length = length * 8;
    //transaction.rxlength = length * 8;

    transaction.length = length;
    transaction.rxlength = length;
    transaction.flags = SPI_TRANS_USE_RXDATA;
    transaction.user = NULL;
    transaction.tx_buffer = NULL;
    
   // two
   // transaction.rx_buffer = &rxdata;
    
    esp_err_t err = spi_device_transmit(handle, &transaction);    
    for (int i = 0; i<4; i++)
    rxdata[i] = transaction.rx_data[i];

   // rxdata = *(uint32_t*)transaction.rx_data;
//three
/*
    dats = rxdata[0];
    dats = dats >>1;
    dats = dats & 0x3FFF;
    dats = SPI_SWAP_DATA_RX(dats, 16);
*/ 

   // dats = SPI_SWAP_DATA_RX(*(uint32_t*)rxdata, 16);
    //dats = dats >>1;
   // dats = dats & 0x3FFF;

    //rxdata = transaction.rx_data[0];

    //*data=16000;
    #if defined CONFIG_SPIBUS_LOG_READWRITES
        if (!err) { 
            char str[2*5+1]; 
           // for(size_t i = 0; i < 2; i++) 
            // four
           // SPIBUS_LOGE("First buffer: %X, Second buffer: %X", *rxdata,*(rxdata-1));
           // SPIBUS_LOGE("First buffer: %X, Second buffer: %X", rxdata,rxdata);
           SPIBUS_LOGE("First buffer: %X, Second buffer: %X", rxdata[0],rxdata[1]);
          //  sprintf(str+i*5, "0x%s%X ", (data[i] < 0x10 ? "0" : ""), data[i]);
           // SPIBUS_LOG_RW("[%s, handle:0x%X] Read_ %d bytes from register 0x%X, data: %s", (host == 1 ? "HSPI" : "VSPI"), (uint32_t)handle, length, regAddr, str);
            //dats=dats>>1;
            //five
        //   ESP_LOGE(TAG_two, "ADC Value: %d", dats);
        ESP_LOGE(TAG_two, "Sup! ");
        }

    #endif
    return err;
}


Who is online

Users browsing this forum: Bing [Bot], ok-home and 222 guests