16#include "driver/spi_common.h"
17#include "driver/spi_master.h"
18#include "driver/gpio.h"
19#include "driver/sdspi_host.h"
23#include "driver/uart.h"
114static const char *
const k_tag =
"SPP_HAL";
136 gpio_config_t misoCfg = {
138 .mode = GPIO_MODE_INPUT,
139 .pull_up_en = GPIO_PULLUP_ENABLE,
140 .pull_down_en = GPIO_PULLDOWN_DISABLE,
141 .intr_type = GPIO_INTR_DISABLE,
143 gpio_config(&misoCfg);
146 spi_bus_config_t busCfg = {
152 .max_transfer_sz = 0,
155 esp_err_t ret = spi_bus_initialize(
K_ESP32_SPI_HOST, &busCfg, SPI_DMA_CH_AUTO);
158 ESP_LOGE(
k_tag,
"SPI bus init failed: %s", esp_err_to_name(ret));
177 if (p_handle == NULL)
186 spi_device_handle_t *p_h = (spi_device_handle_t *)p_handle;
187 spi_device_interface_config_t devCfg = {0};
191 devCfg.clock_speed_hz = 1 * 1000 * 1000;
194 devCfg.queue_size = 1;
198 devCfg.clock_speed_hz = 500 * 1000;
201 devCfg.queue_size = 1;
212 ESP_LOGE(
k_tag,
"spi_bus_add_device failed: %s", esp_err_to_name(ret));
222 if ((p_handle == NULL) || (p_data == NULL) || (length == 0U))
227 spi_device_handle_t hDev = *(spi_device_handle_t *)p_handle;
238 spi_transaction_t trans = {0};
240 if ((p_data[i] & 0x80U) != 0U)
242 trans.length = 8U * 3U;
243 trans.tx_buffer = &p_data[i];
244 trans.rx_buffer = &p_data[i];
250 trans.length = 8U * 2U;
251 trans.tx_buffer = &p_data[i];
256 esp_err_t ret = spi_device_polling_transmit(hDev, &trans);
269 if (p_handle == NULL)
274 spi_device_handle_t *p_h = (spi_device_handle_t *)p_handle;
276 spi_bus_remove_device(*p_h);
278 spi_device_interface_config_t devCfg = {0};
279 devCfg.clock_speed_hz = (int)speedHz;
282 devCfg.queue_size = 1;
287 ESP_LOGE(
k_tag,
"spiDeviceSetSpeed failed: %s", esp_err_to_name(ret));
306 gpio_config_t ioCfg = {
307 .pin_bit_mask = (1ULL << pin),
308 .mode = GPIO_MODE_INPUT,
309 .intr_type = (gpio_int_type_t)intrType,
310 .pull_up_en = GPIO_PULLUP_DISABLE,
311 .pull_down_en = GPIO_PULLDOWN_DISABLE,
316 ioCfg.pull_up_en = GPIO_PULLUP_ENABLE;
320 ioCfg.pull_down_en = GPIO_PULLDOWN_ENABLE;
330 if (!s_serviceInstalled)
332 gpio_install_isr_service(0);
333 s_serviceInstalled =
true;
352 sdspi_device_config_t sdCfg = SDSPI_DEVICE_CONFIG_DEFAULT();
356 (void)sdspi_host_deinit();
363 for (
int attempt = 0; attempt < 5; attempt++)
365 ret = sdspi_host_init();
371 ret = sdspi_host_init_device(&sdCfg, &
sdHandle);
374 (void)sdspi_host_deinit();
378 sdmmc_host_t host = SDSPI_HOST_DEFAULT();
381 vTaskDelay(pdMS_TO_TICKS(10));
383 ret = sdmmc_card_init(&host, &
sdCard);
390 (void)sdspi_host_remove_device(
sdHandle);
391 (void)sdspi_host_deinit();
392 vTaskDelay(pdMS_TO_TICKS(50));
407 if (p_buffer == NULL)
412 if (
sdCard.csd.capacity <= first_block || (
sdCard.csd.capacity - first_block) < count)
417 ret = sdmmc_write_sectors(&
sdCard, p_buffer, first_block, count);
453 esp_err_t ret = ESP_OK;
454 static spp_bool_t s_uartPortInitialized =
false;
456 if (s_uartPortInitialized ==
true)
463 uart_config_t espUartPortCfg = {
465 .data_bits = UART_DATA_8_BITS,
466 .parity = UART_PARITY_DISABLE,
467 .stop_bits = UART_STOP_BITS_1,
468 .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
469 .rx_flow_ctrl_thresh = 0,
472 ret = uart_param_config(uart_num, &espUartPortCfg);
492 s_uartPortInitialized =
true;
499 if ((p_data == NULL) || (len == 0U))
506 const int written = uart_write_bytes(
K_ESP32_UART_PORT_ID, (
void *)p_dataToTransmit, (
size_t)len);
508 if (written != (
int)len)
525 if ((p_data == NULL) || (len == 0U))
SPP GPIO HAL API — dispatches through the registered HAL port.
static SPP_RetVal_t SPP_PORTS_HAL_ESP32_spiDeviceSetSpeed(void *p_handle, spp_uint32_t speedHz)
static const SPP_HALTime_t s_esp32HalTime
static const SPP_HALStorage_t s_esp32HalStorage
static SPP_RetVal_t SPP_PORTS_HAL_ESP32_uartRead(void *p_data, spp_uint32_t len, spp_uint32_t *p_readBytes)
Reads data from the ESP32 UART.
static const char *const k_tag
static const SPP_HALGpio_t s_esp32HalGpio
static void * SPP_PORTS_HAL_ESP32_spiGetHandle(spp_uint8_t deviceIdx)
static SPP_RetVal_t SPP_PORTS_HAL_ESP32_spiBusInit(void)
static sdmmc_card_t sdCard
static spp_uint32_t SPP_PORTS_HAL_ESP32_getTimeUs(void)
static SPP_RetVal_t SPP_PORTS_HAL_ESP32_uartPortInit(void)
static SPP_RetVal_t SPP_PORTS_HAL_ESP32_gpioRegisterIsr(spp_uint32_t pin, void *p_isrCtx)
static SPP_RetVal_t SPP_PORTS_HAL_ESP32_spiTransmit(void *p_handle, spp_uint8_t *p_data, spp_uint8_t length)
static const SPP_HALSpi_t s_esp32HalSpi
static spp_bool_t s_busInitialized
static spi_device_handle_t s_spiHandles[K_ESP32_MAX_SPI_DEVICES]
static SPP_RetVal_t SPP_PORTS_HAL_ESP32_spiDeviceInit(void *p_handle)
static SPP_RetVal_t SPP_PORTS_HAL_ESP32_uartTransmit(const void *p_data, spp_uint32_t len)
static spp_uint32_t SPP_PORTS_HAL_ESP32_getTimeMs(void)
static const SPP_HALUart_t s_esp32HalUart
static SPP_RetVal_t SPP_PORTS_HAL_ESP32_storageInit(void)
static const SPP_HalPort_t s_esp32HalPorts
static SPP_RetVal_t SPP_PORTS_HAL_ESP32_storageWrite(const void *p_buffer, spp_uint32_t first_block, spp_uint16_t count)
const SPP_HalPort_t * SPP_PORTS_ESP32S3_getHalPorts(void)
Returns a pointer to the ESP32-S3 HAL port descriptor.
static spp_uint8_t s_spiDevCount
static spp_bool_t s_storageInitialized
static SPP_RetVal_t SPP_PORTS_HAL_ESP32_gpioConfigInterrupt(spp_uint32_t pin, spp_uint32_t intrType, spp_uint32_t pull)
static void IRAM_ATTR SPP_PORTS_HAL_ESP32_gpioIsr(void *p_arg)
static sdspi_dev_handle_t sdHandle
static void SPP_PORTS_HAL_ESP32_delayMs(spp_uint32_t ms)
ESP32-S3 HAL port descriptor for SPP.
This file contains the structs that contain all the pointers to the functions that are used by the SP...
ESP32 hardware pin definitions and SPI constants.
#define K_ESP32_UART_TX_BUFFER_SIZE
#define K_ESP32_SPI_IDX_BMP
SPI device index for the BMP390.
#define K_ESP32_SPI_HOST
SPI peripheral to use.
#define K_ESP32_UART_RX_PIN
#define K_ESP32_UART_BAUD_RATE
#define K_ESP32_PIN_MISO
MISO (CIPO) GPIO pin.
#define K_ESP32_UART_RX_BUFFER_SIZE
#define K_ESP32_MAX_SPI_DEVICES
Maximum number of SPI device slots.
#define K_ESP32_SPI_IDX_ICM
SPI device index for the ICM20948.
#define K_ESP32_UART_CTS_PIN
#define K_ESP32_UART_PORT_ID
#define K_ESP32_PIN_CLK
SPI clock GPIO pin.
#define K_ESP32_PIN_CS_BMP
BMP390 barometer chip-select GPIO.
#define K_ESP32_PIN_MOSI
MOSI (COPI) GPIO pin.
#define K_ESP32_PIN_CS_ICM
ICM20948 IMU chip-select GPIO.
#define K_ESP32_UART_MAX_DELAY
#define K_ESP32_UART_TX_PIN
#define K_ESP32_PIN_CS_SDC
SD card chip-select GPIO.
#define K_ESP32_UART_RTS_PIN
Standard return-value type used across all SPP modules.
SPP_RetVal_t
Unified return type for all SPP API functions.
@ K_SPP_ERROR_ON_SPI_TRANSACTION
@ K_SPP_ERROR_NULL_POINTER
Context passed to the GPIO ISR handler.
volatile spp_bool_t * p_flag
GPIO struct that contains all the pointers to the GPIO functions.
SPI bus struct that contains all the pointers to the SPI functions.
Storage struct that contains all the pointers to the storage functions.
Time struct that contains all the pointers to the Time functions.
UART port struct that contains all the pointers to the UART functions.
Ports struct that contains all the pointers to the Ports functions.
Portable integer types and SPI/storage configuration types for SPP.
SPP UART HAL API — dispatches through the registered HAL port.