I want to use the ESP32-C6 device as a master to communicate with the DWM1000 module over SPI. After completing the necessary configurations and read/write operations, I can see the expected value (DE CA 01 30) in the Logic Analyzer. (To read DECA0130, I write the value 0x00) However, there is an issue: when I run the firmware at a clock speed of 2 MHz, the data I receive is incorrect. When I increase the clock speed to 16 MHz, the firmware shows the correct data, but I cannot establish communication at 2 MHz.
To clarify, in the Logic Analyzer, I can successfully receive and communicate at both 16 MHz and 2 MHz clock speeds. However, in the firmware, the received data is correct at 16 MHz but incorrect at 2 MHz, and I cannot establish communication at this lower speed. I am seeking assistance regarding this matter and would appreciate any help or insights. Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/spi_master.h"
#include "driver/gpio.h"
#include "sdkconfig.h"
#include "driver/uart.h"
#define PIN_NUM_MISO 2
#define PIN_NUM_MOSI 7
#define PIN_NUM_CLK 6
#define PIN_NUM_CS 10
#define spi_num SPI2_HOST
#define TXD_PIN (GPIO_NUM_16)
#define RXD_PIN (GPIO_NUM_17)
const uart_port_t uart_num = UART_NUM_0;
uart_config_t uart_config;
spi_bus_config_t spi_config;
spi_device_interface_config_t slave_config;
spi_device_handle_t spi_handle;
void uart_init();
void spi_init();
void slave_init();
void app_main()
{
uart_init();
spi_init();
slave_init();
spi_bus_initialize(spi_num, &spi_config, SPI_DMA_DISABLED);
spi_bus_add_device(spi_num, &slave_config, &spi_handle);
uint8_t tx_data[5] = {0x00, 0x00, 0x00, 0x00, 0x00};
uint8_t rx_data[5]; // DWM1000'den alınacak veri
spi_transaction_t trans;
memset(&trans, 0, sizeof(trans));
// trans.flags = SPI_TRANS_USE_RXDATA | SPI_TRANS_USE_TXDATA;
trans.length = 40;
trans.rxlength = 40;
trans.rx_buffer = rx_data;
trans.tx_buffer = tx_data;
esp_err_t ret = spi_device_polling_transmit(spi_handle, &trans);
if (ret == ESP_OK) {
// printf("Gonderilen veri: 0x%02X\n", tx_data);
vTaskDelay(5 / portTICK_PERIOD_MS);
printf("Cevap: 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X\n", rx_data[0], rx_data[1], rx_data[2], rx_data[3], rx_data[4]);
} else {
printf("Hata: SPI iletisiminde bir sorun olustu.\n");
}
}
// Configuration for the SPI bus
void spi_init()
{
spi_config.miso_io_num = PIN_NUM_MISO;
spi_config.mosi_io_num = PIN_NUM_MOSI;
spi_config.sclk_io_num = PIN_NUM_CLK;
spi_config.quadwp_io_num = -1;
spi_config.quadhd_io_num = -1;
spi_config.max_transfer_sz = 32;
}
// Configuration for the SPI device on the other side of the bus (slave)
void slave_init()
{
slave_config.clock_speed_hz = 2000000; // 16 MHz
slave_config.mode = 0; //SPI mode 0
slave_config.spics_io_num = PIN_NUM_CS; // CS Pin
slave_config.queue_size = 1;
slave_config.flags = 0;
slave_config.pre_cb = NULL;
slave_config.post_cb = NULL;
}
void uart_init()
{
uart_config.baud_rate = 115200;
uart_config.data_bits = UART_DATA_8_BITS;
uart_config.parity = UART_PARITY_DISABLE;
uart_config.stop_bits = UART_STOP_BITS_1;
uart_config.flow_ctrl = UART_HW_FLOWCTRL_DISABLE;
uart_config.rx_flow_ctrl_thresh = 122;
ESP_ERROR_CHECK(uart_param_config(uart_num, &uart_config));
ESP_ERROR_CHECK(uart_set_pin(uart_num, TXD_PIN, RXD_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
ESP_ERROR_CHECK(uart_driver_install(uart_num, 1024 * 2, 0, 0, NULL, 0));
}
ESP32-C6 can't communicate over SPI with <2 MHz clock
-
- Posts: 1
- Joined: Thu Aug 03, 2023 2:41 pm
ESP32-C6 can't communicate over SPI with <2 MHz clock
- Attachments
-
- Termite result at 2 MHz
- Ekran Alıntısı2.PNG (10.26 KiB) Viewed 912 times
-
- Logic Analyzer Result
- Ekran Alıntısı.PNG (6.39 KiB) Viewed 912 times
Who is online
Users browsing this forum: Majestic-12 [Bot] and 92 guests