ESP32-C3-DevKitC-02 SPI issue

jm2935
Posts: 3
Joined: Thu Apr 11, 2024 3:26 pm

ESP32-C3-DevKitC-02 SPI issue

Postby jm2935 » Thu Apr 11, 2024 3:43 pm

Hi

I'm very new with ESP32 microprocessors. I use ESP-IDF extension on Visual Studio.

My objective is to initialize an SPI communication between two microcontrollers.

I have the impression that I made everything right but I can't manage to have anything on my oscilloscope when I watch the CLK or the MOSI pin of my board.

According the documentation https://docs.espressif.com/projects/esp ... tc-02.html, there are 3 available SPI.
From what I understood, SPI0 and SPI1 are already dedicated to something specific so I have to use SPI2 with these PINs:
MISO -> SPIQ -> GPIO 2
MOSI -> SPID -> GPIO7
CLK -> SPICLK -> GPIO6
CS -> SPICS0 -> GPIO10

Here is my code:

Code: Select all

#include <stdio.h>
#include <inttypes.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_chip_info.h"
#include "esp_flash.h"
#include "esp_system.h"

#include "driver/spi_master.h"
#include "driver/gpio.h"
#include "esp_log.h"

#define TAG "SPI_TEST"

#define MISO_PIN    GPIO_NUM_2
#define MOSI_PIN    GPIO_NUM_7
#define CLK_PIN     GPIO_NUM_6
#define CS_PIN      GPIO_NUM_10

spi_device_handle_t spi2;

static void spi_init() {
    esp_err_t ret;

    spi_bus_config_t buscfg={
        .miso_io_num = -1,
        .mosi_io_num = MOSI_PIN,
        .sclk_io_num = CLK_PIN,
        .quadwp_io_num = -1,
        .quadhd_io_num = -1,
        .max_transfer_sz = 32,
    };

    ret = spi_bus_initialize(SPI2_HOST, &buscfg, SPI_DMA_DISABLED); //SPI_DMA_CH_AUTO
    if (ret != ESP_OK) {
        ESP_LOGE(TAG, "Error in bus init");
    } else {
        ESP_LOGI(TAG, "Bus init DONE);
    }

    spi_device_interface_config_t devcfg={
        .clock_speed_hz = 1000000,  // 1 MHz
        .mode = 0,                  //SPI mode 0
        .spics_io_num = CS_PIN,     
        .queue_size = 1,
        .flags = SPI_DEVICE_HALFDUPLEX,
        .pre_cb = NULL,
        .post_cb = NULL,
    };

    ret = spi_bus_add_device(SPI2_HOST, &devcfg, &spi2);
    if (ret != ESP_OK) {
        ESP_LOGE(TAG, "Error in bus add device");
    } else {
        ESP_LOGI(TAG, "Bus add device DONE");
    }
};

static void spi_send_message(uint8_t* data, size_t len) {
    esp_err_t ret;

    spi_transaction_t trans = {

        .length = len * 8,
        .tx_buffer = data,
    };

    ret = spi_device_transmit(spi2, &trans);
    if (ret != ESP_OK) {
        ESP_LOGE(TAG, "Error in transmit");
    } else {
        ESP_LOGI(TAG, "Msg sent!");
    }
}

void app_main(void)
{
    // Init SPI
    spi_init();

    // Init Data
    uint8_t data_to_send[4] = {0x01, 0x02, 0x03, 0x04};
    
    /* WHILE LOOP*/
    while(1) {
        spi_send_message(data_to_send, sizeof(data_to_send));
        vTaskDelay(1000);
    }
}
Don't hesitate to tell me if you need any other information,
I would be very grateful for you help,
JM

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

Re: ESP32-C3-DevKitC-02 SPI issue

Postby ESP_Sprite » Fri Apr 12, 2024 3:38 am

FWIW, I see nothing wrong with your code... I assume running it does not spit out any errors? Perhaps you're measuring the wrong pins or something?

jm2935
Posts: 3
Joined: Thu Apr 11, 2024 3:26 pm

Re: ESP32-C3-DevKitC-02 SPI issue

Postby jm2935 » Fri Apr 12, 2024 7:44 am

Hi,
Thanks for your repply, I investigate this morning and I found my problem.
Yesterday I was using a bad oscillo and the message I was sending was way too short.

I modified the message I send like this:

Code: Select all

uint8_t data_to_send[10] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10};
I also used an other oscillo to measure the CLK and MOSI Pin and I can now see something!

I don't know if I have to do it myself and how but these post can be closed :D

Have a really nice day,
JM

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

Re: ESP32-C3-DevKitC-02 SPI issue

Postby ESP_Sprite » Fri Apr 12, 2024 7:52 am

Glad you found it! You can leave your post as it is, we don't have any closing functionality here.

jm2935
Posts: 3
Joined: Thu Apr 11, 2024 3:26 pm

Re: ESP32-C3-DevKitC-02 SPI issue

Postby jm2935 » Fri Apr 12, 2024 7:55 am

I would just have a question before closing the topic,

How can I configure this SPI communication to use DMA?

What would be the maximum size of data I could send?

To transfer big amount of data (a file) could we imagine a ring buffer method to send step by step (package by package) my file?

Thanks in advance,
JM

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

Re: ESP32-C3-DevKitC-02 SPI issue

Postby ESP_Sprite » Sat Apr 13, 2024 12:52 am

jm2935 wrote:
Fri Apr 12, 2024 7:55 am
I would just have a question before closing the topic,

How can I configure this SPI communication to use DMA?

What would be the maximum size of data I could send?
You already have the answer to both questions in your code: you use SPI_DMA_CH_AUTO and set .max_transfer_sz to whatever the maximum is you transfer.
To transfer big amount of data (a file) could we imagine a ring buffer method to send step by step (package by package) my file?
You could, but note that the SPI driver doesn't have a concept of 'ring buffer'. You can start a new transaction without raising CS, so you'd divvy the file up in multiple transactions, then send them one by one keeping CS low.

Who is online

Users browsing this forum: No registered users and 111 guests