Post Go back to editing

How do I interface the MAX35103 IC with the ESP32S3 using ESPIDF I am using SPI communication but the output is always 0xFF What can I do to fix this

Thread Summary

The user is experiencing issues with SPI communication between the MAX35103 and ESP32-S3, where the output is always 0xFF. The final answer suggests checking the SPI configuration, ensuring the correct mode and clock speed, and verifying the connection and initialization of the CS pin. The MAX35103 and ESP32-S3 are the key components, and the SPI commands and register addresses are provided in the code.
AI Generated Content
Category: Datasheet/Specs
Product Number: MAX35103
Software Version: v1.1

How do I interface the MAX35103 IC with the ESP32-S3 using ESP-IDF? I am using SPI communication, but the output is always 0xFF. What can I do to fix this?

this is our code

    


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "driver/spi_master.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#define TAG "MAX35103"

// Updated SPI Pins
#define SPI_HOST       SPI2_HOST
#define SPI_MISO_PIN   8
#define SPI_MOSI_PIN   9
#define SPI_CLK_PIN    7
#define SPI_CS_PIN     6
#define PIN_NUM_INT    4  // Interrupt pin (optional)

// SPI Commands for MAX35103
#define CMD_TOF_DIFF   0x02  // Measure TOF Difference
#define CMD_TEMP_MEAS  0x03  // Measure Temperature
#define CMD_READ_REG   0xB0  // Read Register
#define CMD_WRITE_REG  0x30  // Write Register

// MAX35103 Register Addresses
#define REG_TOF_DIFF_INT  0x08  // TOF Integer Result
#define REG_TOF_DIFF_FRAC 0x09  // TOF Fractional Result
#define REG_TEMP1_INT     0x0A  // Temperature 1 Integer
#define REG_TEMP1_FRAC    0x0B  // Temperature 1 Fractional
#define REG_TEMP2_INT     0x0C  // Temperature 2 Integer
#define REG_TEMP2_FRAC    0x0D  // Temperature 2 Fractional

spi_device_handle_t max35103;

// SPI Initialization
void spi_init() {
    spi_bus_config_t buscfg = {
        .miso_io_num = SPI_MISO_PIN,
        .mosi_io_num = SPI_MOSI_PIN,
        .sclk_io_num = SPI_CLK_PIN,
        .quadwp_io_num = -1,
        .quadhd_io_num = -1,
    };

    spi_device_interface_config_t devcfg = {
        .clock_speed_hz = 1 * 1000 * 1000,  // 1MHz
        .mode = 0,                          // SPI mode 0
        .spics_io_num = SPI_CS_PIN,         // CS pin
        .queue_size = 1,
    };

    ESP_ERROR_CHECK(spi_bus_initialize(SPI_HOST, &buscfg, SPI_DMA_CH_AUTO));
    ESP_ERROR_CHECK(spi_bus_add_device(SPI_HOST, &devcfg, &max35103));

    ESP_LOGI(TAG, "SPI Initialized with MISO: %d, MOSI: %d, CLK: %d, CS: %d",
             SPI_MISO_PIN, SPI_MOSI_PIN, SPI_CLK_PIN, SPI_CS_PIN);
}

// Read 16-bit Register from MAX35103
uint16_t spi_read_register(uint8_t reg) {
    uint8_t tx_data[2] = { CMD_READ_REG | (reg & 0x0F), 0x00 };
    uint8_t rx_data[2] = {0};

    spi_transaction_t trans = {
        .length = 16,
        .tx_buffer = tx_data,
        .rxlength = 16,
        .rx_buffer = rx_data
    };

    esp_err_t ret = spi_device_transmit(max35103, &trans);
    if (ret != ESP_OK) {
        ESP_LOGE(TAG, "SPI Read Failed for Register 0x%02X", reg);
        return 0xFFFF;
    }

    return (rx_data[0] << 8) | rx_data[1];
}

// Start TOF Measurement
void max35103_measure_tof() {
    uint8_t cmd = CMD_TOF_DIFF;
    spi_transaction_t trans = {
        .length = 8,
        .tx_buffer = &cmd,
    };

    esp_err_t ret = spi_device_transmit(max35103, &trans);
    if (ret == ESP_OK) {
        ESP_LOGI(TAG, "TOF Measurement Started");
    } else {
        ESP_LOGE(TAG, "Failed to Start TOF Measurement");
    }
}

// Start Temperature Measurement
void max35103_measure_temperature() {
    uint8_t cmd = CMD_TEMP_MEAS;
    spi_transaction_t trans = {
        .length = 8,
        .tx_buffer = &cmd,
    };

    esp_err_t ret = spi_device_transmit(max35103, &trans);
    if (ret == ESP_OK) {
        ESP_LOGI(TAG, "Temperature Measurement Started");
    } else {
        ESP_LOGE(TAG, "Failed to Start Temperature Measurement");
    }
}

// Interrupt Handler (Optional)
void IRAM_ATTR max35103_isr_handler(void *arg) {
    ESP_LOGI(TAG, "MAX35103 Interrupt Triggered!");
}

// Setup Interrupt for MAX35103
void interrupt_init() {
    gpio_config_t io_conf = {
        .intr_type = GPIO_INTR_NEGEDGE,  // Falling edge trigger
        .pin_bit_mask = (1ULL << PIN_NUM_INT),
        .mode = GPIO_MODE_INPUT,
        .pull_up_en = GPIO_PULLUP_ENABLE,
        .pull_down_en = GPIO_PULLDOWN_DISABLE,
    };
    gpio_config(&io_conf);

    gpio_install_isr_service(0);
    gpio_isr_handler_add(PIN_NUM_INT, max35103_isr_handler, NULL);

    ESP_LOGI(TAG, "Interrupt Initialized on GPIO %d", PIN_NUM_INT);
}

// Main Loop
void app_main() {
    spi_init();
    interrupt_init();  // Optional: Initialize interrupt handling

    while (1) {
        max35103_measure_tof();
        vTaskDelay(100 / portTICK_PERIOD_MS);
       
        uint16_t tof_int = spi_read_register(REG_TOF_DIFF_INT);
        uint16_t tof_frac = spi_read_register(REG_TOF_DIFF_FRAC);
        ESP_LOGI(TAG, "TOF Difference: %u.%u", tof_int, tof_frac);

        max35103_measure_temperature();
        vTaskDelay(100 / portTICK_PERIOD_MS);

        uint16_t temp1_int = spi_read_register(REG_TEMP1_INT);
        uint16_t temp1_frac = spi_read_register(REG_TEMP1_FRAC);
        uint16_t temp2_int = spi_read_register(REG_TEMP2_INT);
        uint16_t temp2_frac = spi_read_register(REG_TEMP2_FRAC);
       
        ESP_LOGI(TAG, "Temperature 1: %u.%u", temp1_int, temp1_frac);
        ESP_LOGI(TAG, "Temperature 2: %u.%u", temp2_int, temp2_frac);

        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
}