SPI schema now uses typed_schema with `type` key (#6353)

This commit is contained in:
Clyde Stubbs 2024-03-12 13:50:24 +11:00 committed by GitHub
parent 51ab15c40e
commit 782d662c20
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 39 additions and 18 deletions

View File

@ -32,7 +32,10 @@ from esphome.const import (
CONF_ALLOW_OTHER_USES, CONF_ALLOW_OTHER_USES,
CONF_DATA_PINS, CONF_DATA_PINS,
) )
from esphome.core import coroutine_with_priority, CORE from esphome.core import (
coroutine_with_priority,
CORE,
)
CODEOWNERS = ["@esphome/core", "@clydebarrow"] CODEOWNERS = ["@esphome/core", "@clydebarrow"]
spi_ns = cg.esphome_ns.namespace("spi") spi_ns = cg.esphome_ns.namespace("spi")
@ -73,6 +76,8 @@ CONF_SPI_MODE = "spi_mode"
CONF_FORCE_SW = "force_sw" CONF_FORCE_SW = "force_sw"
CONF_INTERFACE = "interface" CONF_INTERFACE = "interface"
CONF_INTERFACE_INDEX = "interface_index" CONF_INTERFACE_INDEX = "interface_index"
TYPE_SINGLE = "single"
TYPE_QUAD = "quad"
# RP2040 SPI pin assignments are complicated; # RP2040 SPI pin assignments are complicated;
# refer to GPIO function select table in https://datasheets.raspberrypi.com/rp2040/rp2040-datasheet.pdf # refer to GPIO function select table in https://datasheets.raspberrypi.com/rp2040/rp2040-datasheet.pdf
@ -297,18 +302,19 @@ SPI_QUAD_SCHEMA = cv.All(
), ),
} }
), ),
cv.only_on([PLATFORM_ESP32]),
cv.only_with_esp_idf, cv.only_with_esp_idf,
) )
CONFIG_SCHEMA = cv.All( CONFIG_SCHEMA = cv.All(
# Order is important. SPI_SCHEMA is the default.
cv.ensure_list( cv.ensure_list(
cv.Any( cv.typed_schema(
SPI_SCHEMA, {
SPI_QUAD_SCHEMA, TYPE_SINGLE: SPI_SCHEMA,
msg="Standard SPI requires mosi_pin and/or miso_pin; quad SPI requires data_pins only." TYPE_QUAD: SPI_QUAD_SCHEMA,
+ " A clock pin is always required", },
), default_type=TYPE_SINGLE,
)
), ),
validate_spi_config, validate_spi_config,
) )

View File

@ -0,0 +1,23 @@
spi:
- id: spi_id_1
type: single
clk_pin:
number: GPIO7
allow_other_uses: false
mosi_pin: GPIO6
interface: hardware
- id: quad_spi
type: quad
clk_pin: 47
interface: spi3
data_pins:
- number: 40
allow_other_uses: false
- 41
- 42
- 43
- id: spi_id_3
clk_pin: 8
mosi_pin: 9
interface: any

View File

@ -23,20 +23,12 @@ psram:
spi: spi:
- id: spi_id_1 - id: spi_id_1
type: single
clk_pin: clk_pin:
number: GPIO7 number: GPIO7
allow_other_uses: false allow_other_uses: false
mosi_pin: GPIO6 mosi_pin: GPIO6
interface: any interface: hardware
- id: quad_spi
clk_pin: 47
data_pins:
- number: 40
allow_other_uses: false
- 41
- 42
- 43
spi_device: spi_device:
id: spidev id: spidev
data_rate: 2MHz data_rate: 2MHz