ESP32-C3-DevKitC-02 SPI issue
Posted: 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:
Don't hesitate to tell me if you need any other information,
I would be very grateful for you help,
JM
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);
}
}
I would be very grateful for you help,
JM