Created
March 5, 2022 01:26
-
-
Save DavidJRobertson/0e33a51ddb2462d7c48bdc5f14a355db to your computer and use it in GitHub Desktop.
Example to reproduce 9-bit spi data corruption on ESP32-S3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 "esp_log.h" | |
#ifdef CONFIG_IDF_TARGET_ESP32 | |
# define SPI_HOST HSPI_HOST | |
# define PIN_NUM_MISO 18 | |
# define PIN_NUM_MOSI 23 | |
# define PIN_NUM_CLK 19 | |
# define PIN_NUM_CS 13 | |
#elif CONFIG_IDF_TARGET_ESP32S3 | |
# define SPI_HOST SPI2_HOST | |
# define PIN_NUM_MISO 13 | |
# define PIN_NUM_MOSI 11 | |
# define PIN_NUM_CLK 12 | |
# define PIN_NUM_CS 10 | |
#endif | |
static const char TAG[] = "main"; | |
void app_main(void) | |
{ | |
esp_err_t ret; | |
// Initialise bus | |
ESP_LOGI(TAG, "Initializing bus SPI%d...", SPI_HOST+1); | |
spi_bus_config_t buscfg={ | |
.miso_io_num = PIN_NUM_MISO, | |
.mosi_io_num = PIN_NUM_MOSI, | |
.sclk_io_num = PIN_NUM_CLK, | |
.quadwp_io_num = -1, | |
.quadhd_io_num = -1, | |
.max_transfer_sz = 32, | |
}; | |
ret = spi_bus_initialize(SPI_HOST, &buscfg, SPI_DMA_CH_AUTO); | |
ESP_ERROR_CHECK(ret); | |
// Initialise device | |
ESP_LOGI(TAG, "Initializing device"); | |
spi_device_handle_t spiDevice; | |
spi_device_interface_config_t deviceConfig = { | |
.command_bits = 0, | |
.address_bits = 0, | |
.dummy_bits = 0, | |
.mode = 0, | |
.duty_cycle_pos = 0, | |
.cs_ena_pretrans = 0, | |
.cs_ena_posttrans = 0, | |
.clock_speed_hz = 100000, | |
.input_delay_ns = 0, | |
.spics_io_num = PIN_NUM_CS, | |
.flags = 0, | |
.queue_size = 200, | |
}; | |
ESP_ERROR_CHECK(spi_bus_add_device(SPI_HOST, &deviceConfig, &spiDevice)); | |
// Send 9-bits of data, all ones | |
ESP_LOGI(TAG, "Sending data"); | |
uint8_t testData[2] = {0xff, 0xff}; | |
spi_transaction_t transaction = { | |
.flags = 0, | |
.cmd = 0, | |
.addr = 0, | |
.length = 9, | |
.rxlength = 0, | |
.tx_buffer = testData, | |
}; | |
ESP_ERROR_CHECK(spi_device_transmit(spiDevice, &transaction)); | |
while (1) { | |
// Add your main loop handling code here. | |
vTaskDelay(1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment