-->
Page 1 of 1

SPI + ESP8266_RTOS_SDK

PostPosted: Sun Nov 14, 2021 12:11 pm
by rlake
Hi,

This is my first try to use SPI with the ESP8266 and FreeRTOS but for some reason I there is only zeroes in the MISO buffer. The SPI device I am using is the MCP3008 10-bit ADC. I can see from my oscilloscope that the MCP3008 is returning data on the MISO line so I'm at a loss as to what's happening. Here are some code snippets to how I'm configuring the ESP and executing spi_trans.

Code: Select all  spi_config_t spi_config;
  spi_config.interface.val = SPI_DEFAULT_INTERFACE;
  spi_config.intr_enable.val = 0;
  spi_config.event_cb = NULL;
  spi_config.mode = SPI_MASTER_MODE;
  spi_config.clk_div = SPI_2MHz_DIV;
  spi_init(HSPI_HOST, &spi_config);


Code: Select all  spi_trans_t trans;
  uint16_t cmd = SPI_MASTER_WRITE_DATA_TO_SLAVE_CMD;
  uint32_t addr = 0;
  uint32_t MOSI = 0x01800000;
  uint32_t MISO = 0xFFFFFFFF;

  trans.cmd = &cmd;
  trans.addr = &addr;
  trans.mosi = &MOSI;
  trans.miso = &MISO;
  trans.bits.cmd = 8;
  trans.bits.addr = 0;
  trans.bits.mosi = sizeof(MOSI) * 8;
  trans.bits.miso = sizeof(MISO) * 8;
  while(1)
  {
    spi_trans(HSPI_HOST, &trans);
    ESP_LOGI(TAG, "Read %08x", MISO);
    vTaskDelay(1000 / portTICK_RATE_MS);
  }


The ESP8266 I'm using is the Adafruit HUZZAH board with the following pins hooked up to the MCP3008
Code: Select allSPI       ESP8266    MCP3008
CLK            14         13
SS             15         10
MOSI           13         11
MISO           12         12


Does anybody see what I could be doing incorrectly here to have MISO filled with 0's?
Thanks

Re: SPI + ESP8266_RTOS_SDK

PostPosted: Mon Nov 15, 2021 11:14 pm
by rlake
I think I solved my own problem.

I changed the initialisation to include the bit order for TX and RX.

Code: Select all  spi_config_t spi_config;
  spi_config.interface.val = SPI_DEFAULT_INTERFACE;
  spi_config.intr_enable.val = 0;
  spi_config.event_cb = NULL;
  spi_config.mode = SPI_MASTER_MODE;
  spi_config.clk_div = SPI_2MHz_DIV;
  spi_config.interface.bit_tx_order = SPI_BIT_ORDER_MSB_FIRST;
  spi_config.interface.bit_rx_order = SPI_BIT_ORDER_MSB_FIRST;
  spi_init(HSPI_HOST, &spi_config);


I also changed the call to spi_trans to remove the command, it wasn't required and had nothing to do with what I thought it was there for.

Code: Select all  spi_trans_t trans;
  uint16_t cmd = 0;
  uint32_t addr = 0;
  uint32_t MOSI = 0x01800000;
  uint32_t MISO;

  trans.cmd = &cmd;
  trans.addr = &addr;
  trans.mosi = &MOSI;
  trans.miso = &MISO;
  trans.bits.cmd = 0;
  trans.bits.addr = 0;
  trans.bits.mosi = sizeof(MOSI) * 8;
  trans.bits.miso = sizeof(MISO) * 8;
  while(1)
  {
    spi_trans(HSPI_HOST, &trans);
    ESP_LOGI(TAG, "%04x", MISO >> 7);
    vTaskDelay(1000 / portTICK_RATE_MS);
  }


The code now logs the value sent by the MCP3008.