13#include "driver/spi_common.h"
14#include "driver/spi_master.h"
15#include "driver/gpio.h"
23static const char *
const k_tag =
"SPP_HAL";
40 gpio_config_t misoCfg = {
42 .mode = GPIO_MODE_INPUT,
43 .pull_up_en = GPIO_PULLUP_ENABLE,
44 .pull_down_en = GPIO_PULLDOWN_DISABLE,
45 .intr_type = GPIO_INTR_DISABLE,
47 gpio_config(&misoCfg);
50 spi_bus_config_t busCfg = {
59 esp_err_t ret = spi_bus_initialize(
K_ESP32_SPI_HOST, &busCfg, SPI_DMA_CH_AUTO);
62 ESP_LOGE(
k_tag,
"SPI bus init failed: %s", esp_err_to_name(ret));
102 spi_device_handle_t *p_h = (spi_device_handle_t *)p_handle;
103 spi_device_interface_config_t devCfg = {0};
107 devCfg.clock_speed_hz = 1 * 1000 * 1000;
110 devCfg.queue_size = 1;
114 devCfg.clock_speed_hz = 500 * 1000;
117 devCfg.queue_size = 1;
121 devCfg.clock_speed_hz = 1 * 1000 * 1000;
124 devCfg.queue_size = 1;
128 ESP_LOGE(
k_tag,
"Unexpected device index %u", deviceIdx);
132 printf(
"SPI DEV INIT | idx=%u CS=%d\n", (
unsigned)deviceIdx, devCfg.spics_io_num);
137 ESP_LOGE(
k_tag,
"spi_bus_add_device failed: %s", esp_err_to_name(ret));
146 if ((p_handle == NULL) || (p_data == NULL) || (length == 0U))
151 spi_device_handle_t hDev = *(spi_device_handle_t *)p_handle;
162 spi_transaction_t trans = {0};
164 if ((p_data[i] & 0x80U) != 0U)
166 trans.length = 8U * 3U;
167 trans.tx_buffer = &p_data[i];
168 trans.rx_buffer = &p_data[i];
174 trans.length = 8U * 2U;
175 trans.tx_buffer = &p_data[i];
180 esp_err_t ret = spi_device_polling_transmit(hDev, &trans);
193 if (p_handle == NULL)
198 spi_device_handle_t *p_h = (spi_device_handle_t *)p_handle;
200 spi_bus_remove_device(*p_h);
202 spi_device_interface_config_t devCfg = {0};
203 devCfg.clock_speed_hz = (int)speedHz;
206 devCfg.queue_size = 1;
211 ESP_LOGE(
k_tag,
"spiDeviceSetSpeed failed: %s", esp_err_to_name(ret));
225 if ((p_handle == NULL) || (p_data == NULL) || (length == 0U))
230 spi_device_handle_t hDev = *(spi_device_handle_t *)p_handle;
237 spi_transaction_t trans = {0};
239 trans.length = 8U * length;
240 trans.tx_buffer = p_data;
241 trans.rx_buffer = p_data;
243 esp_err_t ret = spi_device_polling_transmit(hDev, &trans);
ESP32 hardware pin definitions and SPI constants.
#define K_ESP32_SPI_IDX_BMP
SPI device index for the BMP390.
#define K_ESP32_SPI_IDX_SX1262
SPI device index for the sx1262.
#define K_ESP32_SPI_HOST
SPI peripheral to use.
#define K_ESP32_PIN_MISO
MISO (CIPO) GPIO pin.
#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_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_PIN_CS_SX1262
SX1262 chip-select GPIO.
#define K_ESP32_PIN_CS_SDC
SD card chip-select GPIO.
SPP_RetVal_t
Unified return type for all SPP API functions.
@ K_SPP_ERROR_ON_SPI_TRANSACTION
@ K_SPP_ERROR_NULL_POINTER
static const char *const k_tag
SPP_RetVal_t SPP_PORTS_HAL_ESP32_spiBusInit(void)
void * SPP_PORTS_HAL_ESP32_spiGetHandle(spp_uint8_t deviceIdx)
Returns the storage location of an SPI device handle.
SPP_RetVal_t SPP_PORTS_HAL_ESP32_spiDeviceSetSpeed(void *p_handle, spp_uint32_t speedHz)
Reconfigures an SPI device with a new clock frequency.
SPP_RetVal_t SPP_PORTS_HAL_ESP32_spiTransmit(void *p_handle, spp_uint8_t *p_data, spp_uint8_t length)
Performs register-based SPI transactions.
static spp_bool_t s_busInitialized
SPP_RetVal_t SPP_PORTS_HAL_ESP32_spiDeviceInit(void *p_handle)
static spi_device_handle_t s_spiHandles[K_ESP32_MAX_SPI_DEVICES]
SPP_RetVal_t SPP_PORTS_HAL_ESP32_spiTransmitRaw(void *p_handle, spp_uint8_t *p_data, spp_uint8_t length)
Performs a raw full-duplex SPI transaction.
spp_bool_t SPP_PORTS_HAL_ESP32_spiIsBusInitialized(void)
Check whether the SPI bus has been initialised.
ESP32-S3 SPI port implementation.