SPI no clock or data

keithco
Posts: 13
Joined: Fri Jun 17, 2022 8:17 pm

SPI no clock or data

Postby keithco » Mon Apr 24, 2023 1:17 pm

I have been struggling trying to communicate with a TMP127-Q1 digital temperature sensor using a ESP32-S3-MINI.
Using my Oscilloscope, I am not seeing clock or data. The error I am getting is this: E (5804824) spi_master: check_trans_valid(684): invalid dev handle
Here is my SPI code.
  1. /*
  2.  * temp_SPI-6.c
  3.  *
  4.  *  Created on: Apr 12, 2023
  5.  *      Author: keith
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include "freertos/FreeRTOS.h"
  10. #include "freertos/task.h"
  11. #include "driver/gpio.h"
  12. #include "driver/spi_master.h"
  13.  
  14. #include "temp_SPI-6.h"
  15.  
  16. #define USE_SPI_MODE
  17. #define SPI_BUS       SPI3_HOST
  18. #define GPIO_CS         4
  19. #define GPIO_SIO        37
  20. #define GPIO_CLK        36
  21. //#define GPIO_IRQ      2   // IRQ pin
  22.  
  23. #define TMP127_REG_TEMP     0x01
  24.  
  25. spi_device_handle_t spi_device;
  26. spi_transaction_t *trans_desc;
  27.  
  28. void tmp127_spi_init()
  29. {
  30.     spi_bus_config_t bus_cfg = {
  31.         .miso_io_num = GPIO_SIO,
  32.         .mosi_io_num = -1,
  33.         .sclk_io_num = GPIO_CLK,
  34.         .quadwp_io_num = -1,
  35.         .quadhd_io_num = -1,
  36.         .max_transfer_sz = 32,
  37.     };
  38.  
  39.     spi_bus_initialize(SPI3_HOST, &bus_cfg, 1);
  40.  
  41.     spi_device_interface_config_t dev_cfg = {
  42.         .clock_speed_hz = 1000000,          // Clock out at 1 MHz
  43.         .mode = 0,                          // SPI mode 0
  44.         .spics_io_num = GPIO_CS,            // CS pin
  45.         .queue_size = 1,
  46. //      .flags = SPI_DEVICE_NO_DUMMY,
  47.     };
  48.  
  49. //    spi_bus_initialize(SPI_BUS, &bus_cfg, 1);
  50. //    spi_bus_add_device(SPI_BUS, &dev_cfg, &spi_device);
  51.     spi_bus_initialize(SPI3_HOST, &bus_cfg, 1);
  52.     spi_bus_add_device(SPI3_HOST, &dev_cfg, &spi_device);
  53.     spi_device_acquire_bus(spi_device, portMAX_DELAY);
  54.  
  55. //    gpio_set_direction(GPIO_CS, GPIO_MODE_OUTPUT);
  56. //    gpio_set_direction(GPIO_CLK, GPIO_MODE_OUTPUT);
  57. //    gpio_set_direction(GPIO_SIO, GPIO_MODE_INPUT);
  58. }
  59.  
  60. int16_t tmp127_read_temperature()
  61. {
  62. //    uint8_t tx_data[2] = {TMP127_REG_TEMP, 0};
  63. //    uint8_t rx_data[2] = {0, 0};
  64.     uint8_t rx_data[2] = {0};
  65.     spi_transaction_t trans_desc = {
  66. //      .tx_buffer = tx_data,
  67.         .rx_buffer = rx_data,
  68.         .length = 16,
  69.         .flags = SPI_TRANS_USE_RXDATA,
  70.     };
  71.  
  72. //    spi_device_transmit(spi_device, &trans_desc);
  73.     spi_device_polling_start(spi_device, &trans_desc, portMAX_DELAY);
  74.  
  75.     spi_device_polling_transmit(spi_device, &trans_desc);
  76.     int16_t temperature = (rx_data[0] << 8) | rx_data[1] >> 4;
  77. //    int16_t temperature = (rx_data[1] << 8) | rx_data[2];
  78. //    temperature >>= 4;  // Remove lower 4 bits (fractional part)
  79. //    temperature = temperature / 128;
  80.     return temperature;
  81. }

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

Re: SPI no clock or data

Postby ESP_Sprite » Tue Apr 25, 2023 3:35 am

You are not checking any of your spi driver calls for errors. Either do that manually or use the ESP_ERROR_CHECK macro. That'll likely tell you what fails.

keithco
Posts: 13
Joined: Fri Jun 17, 2022 8:17 pm

Re: SPI no clock or data

Postby keithco » Tue Apr 25, 2023 12:27 pm

Thank you! I'll give that a try. I forgot to point out that I am using these pins for clock and data. Is this okay?
  1. #define GPIO_CS         4
  2. #define GPIO_SIO        37
  3. #define GPIO_CLK        36

keithco
Posts: 13
Joined: Fri Jun 17, 2022 8:17 pm

Re: SPI no clock or data

Postby keithco » Wed Apr 26, 2023 1:59 am

I've implemented the macro call ESP_ERROR_CHECK now. No errors until I add:
  1. ESP_ERROR_CHECK(spi_device_polling_transmit(spi_device, &trans_desc));

Still no clock or data... Here is my most recent code.
  1. /*
  2.  * temp_SPI-6.c
  3.  *
  4.  *  Created on: Apr 12, 2023
  5.  *      Author: keith
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include "freertos/FreeRTOS.h"
  10. #include "freertos/task.h"
  11. #include "driver/gpio.h"
  12. #include "driver/spi_master.h"
  13. #include "esp_log.h"
  14.  
  15. #include "temp_SPI-6.h"
  16.  
  17. #define USE_SPI_MODE
  18. #define SPI_BUS       SPI2_HOST
  19. #define GPIO_CS         4
  20. #define GPIO_SIO        37
  21. #define GPIO_CLK        36
  22.  
  23. #define TMP127_REG_TEMP     0x01
  24.  
  25. spi_device_handle_t spi_device;
  26. spi_transaction_t *trans_desc;
  27.  
  28. void tmp127_spi_init()
  29. {
  30.     spi_bus_config_t bus_cfg = {
  31.         .miso_io_num = GPIO_SIO,
  32.         .mosi_io_num = -1,
  33.         .sclk_io_num = GPIO_CLK,
  34.         .quadwp_io_num = -1,
  35.         .quadhd_io_num = -1,
  36.         .max_transfer_sz = 32,
  37.     };
  38.  
  39.     // Initialize the SPI bus
  40.     ESP_ERROR_CHECK(spi_bus_initialize(SPI2_HOST, &bus_cfg, 1));
  41.  
  42.     spi_device_interface_config_t dev_cfg = {
  43.         .clock_speed_hz = 1000000,          // Clock out at 1 MHz
  44.         .mode = 0,                          // SPI mode 0
  45.         .spics_io_num = GPIO_CS,            // CS pin
  46.         .queue_size = 1,
  47. //      .flags = SPI_DEVICE_NO_DUMMY,
  48.     };
  49.  
  50.     ESP_ERROR_CHECK(spi_bus_add_device(SPI2_HOST, &dev_cfg, &spi_device));
  51.     ESP_ERROR_CHECK(spi_device_acquire_bus(spi_device, portMAX_DELAY));
  52.  
  53. }
  54.  
  55. int16_t tmp127_read_temperature()
  56. {
  57. //    uint8_t tx_data[2] = {TMP127_REG_TEMP, 0};
  58.     uint8_t rx_data[2] = {0};
  59.     spi_transaction_t trans_desc = {
  60. //      .tx_buffer = tx_data,
  61.         .rx_buffer = rx_data,
  62.         .length = 32,
  63.         .flags = SPI_TRANS_USE_RXDATA,
  64.     };
  65.  
  66. //    ESP_ERROR_CHECK(spi_device_transmit(spi_device, &trans_desc));
  67. //    ESP_ERROR_CHECK(spi_device_polling_start(spi_device, &trans_desc, portMAX_DELAY));
  68.     ESP_ERROR_CHECK(spi_device_polling_transmit(spi_device, &trans_desc));
  69.     int16_t temperature = (trans_desc.rx_data[0] << 4);
  70.  
  71.     return temperature;
  72. }

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

Re: SPI no clock or data

Postby ESP_Sprite » Wed Apr 26, 2023 2:23 am

Are you sure your code calls tmp127_spi_init before tmp127_read_temperature is first called?

keithco
Posts: 13
Joined: Fri Jun 17, 2022 8:17 pm

Re: SPI no clock or data

Postby keithco » Sat Apr 29, 2023 2:28 am

Hello ESP_Sprite,

You nailed it. I forgot that I had commented out the function call "tmp127_spi_init".
There were several other issues but now are reliably reading from the TMP127-Q1 digital temperature sensor.
Thanks for your help...

Here is the working code:

Code: Select all

/*
 * temp_SPI-6.c
 *
 *  Created on: Apr 12, 2023
 *      Author: keith
 */

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "driver/spi_master.h"
#include "esp_log.h"

#include "temp_SPI-6.h"

#define USE_SPI_MODE
#define SPI_BUS       SPI3_HOST
#define GPIO_CS       	4
#define GPIO_SIO       	37
#define GPIO_CLK       	36

#define TEMP_FACTOR    0.118 //
#define TMP127_REG_TEMP     0x01

spi_device_handle_t spi_device;
spi_transaction_t *trans_desc;

void tmp127_spi_init()
{
    spi_bus_config_t bus_cfg = {
        .miso_io_num = GPIO_SIO,
        .mosi_io_num = -1,
        .sclk_io_num = GPIO_CLK,
        .quadwp_io_num = -1,
        .quadhd_io_num = -1,
        .max_transfer_sz = 32,
    };

    // Initialize the SPI bus
    ESP_ERROR_CHECK(spi_bus_initialize(SPI3_HOST, &bus_cfg, 0));

    spi_device_interface_config_t dev_cfg = {
        .clock_speed_hz = 1000000,          // Clock out at 1 MHz
        .mode = 0,                          // SPI mode 0
        .spics_io_num = GPIO_CS,            // CS pin
        .queue_size = 1,
		.flags = SPI_DEVICE_NO_DUMMY,
    };

    ESP_ERROR_CHECK(spi_bus_add_device(SPI3_HOST, &dev_cfg, &spi_device));
    ESP_ERROR_CHECK(spi_device_acquire_bus(spi_device, portMAX_DELAY));
}

int16_t tmp127_read_temperature()
{
    uint8_t tx_data[2] = {TMP127_REG_TEMP, 0};
    uint8_t rx_data[2] = {0,0};
    spi_transaction_t trans_desc = {
		.tx_buffer = tx_data,
		.rx_buffer = rx_data,
        .length = 16,
    };

    ESP_ERROR_CHECK(spi_device_polling_transmit(spi_device, &trans_desc));

    int16_t temperature = (rx_data[0] << 8) | rx_data[1];
    temperature >>= 4;  // Remove lower 4 bits
    return temperature;
}

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

Re: SPI no clock or data

Postby ESP_Sprite » Sat Apr 29, 2023 4:13 am

You're welcome, glad you got it to work!

Who is online

Users browsing this forum: No registered users and 136 guests