This commit is contained in:
Ken Van Hoeylandt 2026-06-21 18:14:08 +02:00
parent 8711521a36
commit 4909b7075b
59 changed files with 227 additions and 209 deletions

View File

@ -45,6 +45,7 @@ def parse_binding(file_path: str, binding_dirs: list[str]) -> Binding:
required=details.get('required', False),
description=details.get('description', '').strip(),
default=details.get('default', None),
element_type=details.get('element-type', None),
)
properties_dict[name] = prop
filename = os.path.basename(file_path)

View File

@ -20,10 +20,8 @@ def write_define(file, define: DefineC, verbose: bool):
def get_device_node_name_safe(device: Device):
if device.node_name == "/":
return "root"
name = device.node_name.replace("-", "_")
if device.node_address is not None:
name += "_" + device.node_address.replace("-", "_")
return name
else:
return device.node_name.replace("-", "_")
def get_device_type_name(device: Device, bindings: list[Binding]):
device_binding = find_device_binding(device, bindings)
@ -82,7 +80,7 @@ def property_to_string(property: DeviceProperty, devices: list[Device]) -> str:
return "{ " + ",".join(value_list) + " }"
elif type == "phandle":
return find_phandle(devices, property.value)
elif type == "phandle-array":
elif type == "phandles":
value_list = list()
if isinstance(property.value, list):
for item in property.value:
@ -90,16 +88,31 @@ def property_to_string(property: DeviceProperty, devices: list[Device]) -> str:
value_list.append(property_to_string(DeviceProperty(name="", type=item.type, value=item.value), devices))
else:
value_list.append(str(item))
value_list.append("{ 0 }")
return "{ " + ",".join(value_list) + " }"
elif isinstance(property.value, str):
# If it's a string, assume it's a #define and show it as-is
return property.value
else:
raise Exception(f"Unsupported phandle-array type for {property.value}")
raise Exception(f"Unsupported phandles type for {property.name} with value {property.value} ")
else:
raise DevicetreeException(f"property_to_string() has an unsupported type: {type}")
def resolve_phandle_array_entries(device_property, devices):
"""Convert a phandle-array DTS property into a list of C initializer strings."""
entries = []
if device_property.type == "phandle-array":
items = device_property.value
elif device_property.type == "values":
items = [PropertyValue(type="values", value=device_property.value)]
else:
return []
for item in items:
if isinstance(item, PropertyValue):
entries.append(property_to_string(DeviceProperty(name="", type=item.type, value=item.value), devices))
else:
entries.append(str(item))
return entries
def resolve_parameters_from_bindings(device: Device, bindings: list[Binding], devices: list[Device]) -> list:
compatible_property = find_device_property(device, "compatible")
if compatible_property is None:
@ -122,11 +135,32 @@ def resolve_parameters_from_bindings(device: Device, bindings: list[Binding], de
if device_property.name not in binding_property_names:
raise DevicetreeException(f"Device '{device.node_name}' has invalid property '{device_property.name}'")
# Allocate total expected configuration arguments
result = [0] * len(binding_properties)
for index, binding_property in enumerate(binding_properties):
node_name = get_device_node_name_safe(device)
result = []
phandle_arrays = []
for binding_property in binding_properties:
device_property = find_device_property(device, binding_property.name)
# No property specified in DTS, use binding defaults
if binding_property.type == "phandle-array":
if binding_property.element_type is None:
raise DevicetreeException(f"phandle-array property '{binding_property.name}' requires 'element-type' in binding")
prop_safe = binding_property.name.replace("-", "_")
array_var = f"{node_name}_{prop_safe}"
if device_property is not None:
entries = resolve_phandle_array_entries(device_property, devices)
phandle_arrays.append((array_var, binding_property.element_type, entries))
result.append(f"({binding_property.element_type}*){array_var}")
result.append(str(len(entries)))
elif binding_property.default is not None:
result.append("NULL")
result.append("0")
elif binding_property.required:
raise DevicetreeException(f"device {device.node_name} doesn't have property '{binding_property.name}'")
else:
result.append("NULL")
result.append("0")
continue
if device_property is None:
if binding_property.default is not None:
temp_prop = DeviceProperty(
@ -134,30 +168,38 @@ def resolve_parameters_from_bindings(device: Device, bindings: list[Binding], de
type=binding_property.type,
value=binding_property.default
)
result[index] = property_to_string(temp_prop, devices)
result.append(property_to_string(temp_prop, devices))
elif binding_property.required:
raise DevicetreeException(f"device {device.node_name} doesn't have property '{binding_property.name}'")
elif binding_property.type == "bool" or binding_property.type == "boolean":
if binding_property.default == "true" or binding_property.default == None:
result[index] = "true"
else: # Explicit or implied false
result[index] = "false"
result.append("true")
else:
result.append("false")
else:
raise DevicetreeException(f"Device {device.node_name} doesn't have property '{binding_property.name}' and no default value is set")
else:
result[index] = property_to_string(device_property, devices)
return result
result.append(property_to_string(device_property, devices))
return result, phandle_arrays
def write_config(file, device: Device, bindings: list[Binding], devices: list[Device], type_name: str):
node_name = get_device_node_name_safe(device)
config_type = f"{type_name}_config_dt"
config_variable_name = f"{node_name}_config"
config_params, phandle_arrays = resolve_parameters_from_bindings(device, bindings, devices)
# Write phandle-array variables before the config struct
for array_var, element_type, entries in phandle_arrays:
entries_str = ", ".join(entries)
file.write(f"static {element_type} {array_var}[] = {{ {entries_str} }};\n")
file.write(f"static const {config_type} {config_variable_name}" " = {\n")
config_params = resolve_parameters_from_bindings(device, bindings, devices)
# Indent all params
for index, config_param in enumerate(config_params):
config_params[index] = f"\t{config_param}"
# Join with command and newline
# Join with comma and newline
if len(config_params) > 0:
config_params_joined = ",\n".join(config_params)
file.write(f"{config_params_joined}\n")

View File

@ -39,6 +39,7 @@ class BindingProperty:
required: bool
description: str
default: object = None
element_type: str = None
@dataclass
class Binding:

View File

@ -44,9 +44,8 @@
pin-miso = <&gpio0 19 GPIO_FLAG_NONE>;
pin-sclk = <&gpio0 18 GPIO_FLAG_NONE>;
sdcard {
sdcard@0 {
compatible = "espressif,esp32-sdspi";
pin-cs = <&gpio0 5 GPIO_FLAG_NONE>;
frequency-khz = <20000>;
};
};

View File

@ -43,9 +43,8 @@
pin-sclk = <&gpio0 18 GPIO_FLAG_NONE>;
cs-gpios = <&gpio0 5 GPIO_FLAG_NONE>;
sdcard {
sdcard@0 {
compatible = "espressif,esp32-sdspi";
pin-cs = <&gpio0 5 GPIO_FLAG_NONE>;
frequency-khz = <20000>;
};
};

View File

@ -52,9 +52,8 @@
pin-sclk = <&gpio0 18 GPIO_FLAG_NONE>;
cs-gpios = <&gpio0 5 GPIO_FLAG_NONE>;
sdcard {
sdcard@0 {
compatible = "espressif,esp32-sdspi";
pin-cs = <&gpio0 5 GPIO_FLAG_NONE>;
frequency-khz = <20000>;
};
};

View File

@ -52,9 +52,8 @@
pin-miso = <&gpio0 19 GPIO_FLAG_NONE>;
pin-sclk = <&gpio0 18 GPIO_FLAG_NONE>;
sdcard {
sdcard@0 {
compatible = "espressif,esp32-sdspi";
pin-cs = <&gpio0 5 GPIO_FLAG_NONE>;
frequency-khz = <20000>;
};
};

View File

@ -44,9 +44,8 @@
pin-miso = <&gpio0 19 GPIO_FLAG_NONE>;
pin-sclk = <&gpio0 18 GPIO_FLAG_NONE>;
sdcard {
sdcard@0 {
compatible = "espressif,esp32-sdspi";
pin-cs = <&gpio0 5 GPIO_FLAG_NONE>;
frequency-khz = <20000>;
};
};

View File

@ -54,9 +54,8 @@
pin-miso = <&gpio0 19 GPIO_FLAG_NONE>;
pin-sclk = <&gpio0 18 GPIO_FLAG_NONE>;
sdcard {
sdcard@0 {
compatible = "espressif,esp32-sdspi";
pin-cs = <&gpio0 5 GPIO_FLAG_NONE>;
frequency-khz = <20000>;
};
};

View File

@ -36,9 +36,8 @@
pin-miso = <&gpio0 41 GPIO_FLAG_NONE>;
pin-sclk = <&gpio0 48 GPIO_FLAG_NONE>;
sdcard {
sdcard@0 {
compatible = "espressif,esp32-sdspi";
pin-cs = <&gpio0 42 GPIO_FLAG_NONE>;
frequency-khz = <20000>;
};
};

View File

@ -45,9 +45,8 @@
pin-miso = <&gpio0 13 GPIO_FLAG_NONE>;
pin-sclk = <&gpio0 12 GPIO_FLAG_NONE>;
sdcard {
sdcard@0 {
compatible = "espressif,esp32-sdspi";
pin-cs = <&gpio0 10 GPIO_FLAG_NONE>;
frequency-khz = <20000>;
};
};

View File

@ -42,9 +42,8 @@
pin-miso = <&gpio0 19 GPIO_FLAG_NONE>;
pin-sclk = <&gpio0 18 GPIO_FLAG_NONE>;
sdcard {
sdcard@0 {
compatible = "espressif,esp32-sdspi";
pin-cs = <&gpio0 5 GPIO_FLAG_NONE>;
frequency-khz = <20000>;
};
};

View File

@ -51,9 +51,8 @@
pin-miso = <&gpio0 19 GPIO_FLAG_NONE>;
pin-sclk = <&gpio0 18 GPIO_FLAG_NONE>;
sdcard {
sdcard@0 {
compatible = "espressif,esp32-sdspi";
pin-cs = <&gpio0 5 GPIO_FLAG_NONE>;
frequency-khz = <20000>;
};
};

View File

@ -50,9 +50,8 @@
pin-miso = <&gpio0 4 GPIO_FLAG_NONE>;
pin-sclk = <&gpio0 5 GPIO_FLAG_NONE>;
sdcard {
sdcard@0 {
compatible = "espressif,esp32-sdspi";
pin-cs = <&gpio0 7 GPIO_FLAG_NONE>;
frequency-khz = <20000>;
};
};

View File

@ -50,9 +50,8 @@
pin-miso = <&gpio0 4 GPIO_FLAG_NONE>;
pin-sclk = <&gpio0 5 GPIO_FLAG_NONE>;
sdcard {
sdcard@0 {
compatible = "espressif,esp32-sdspi";
pin-cs = <&gpio0 7 GPIO_FLAG_NONE>;
frequency-khz = <20000>;
};
};

View File

@ -37,9 +37,8 @@
pin-miso = <&gpio0 4 GPIO_FLAG_NONE>;
pin-sclk = <&gpio0 5 GPIO_FLAG_NONE>;
sdcard {
sdcard@0 {
compatible = "espressif,esp32-sdspi";
pin-cs = <&gpio0 0 GPIO_FLAG_NONE>;
frequency-khz = <20000>;
};
};

View File

@ -53,9 +53,8 @@
pin-miso = <&gpio0 19 GPIO_FLAG_NONE>;
pin-sclk = <&gpio0 18 GPIO_FLAG_NONE>;
sdcard {
sdcard@0 {
compatible = "espressif,esp32-sdspi";
pin-cs = <&gpio0 5 GPIO_FLAG_NONE>;
frequency-khz = <20000>;
};
};

View File

@ -53,9 +53,8 @@
pin-miso = <&gpio0 19 GPIO_FLAG_NONE>;
pin-sclk = <&gpio0 18 GPIO_FLAG_NONE>;
sdcard {
sdcard@0 {
compatible = "espressif,esp32-sdspi";
pin-cs = <&gpio0 5 GPIO_FLAG_NONE>;
frequency-khz = <20000>;
};
};

View File

@ -37,9 +37,8 @@
pin-miso = <&gpio0 13 GPIO_FLAG_NONE>;
pin-sclk = <&gpio0 12 GPIO_FLAG_NONE>;
sdcard {
sdcard@0 {
compatible = "espressif,esp32-sdspi";
pin-cs = <&gpio0 10 GPIO_FLAG_NONE>;
frequency-khz = <20000>;
};
};

View File

@ -54,9 +54,8 @@
pin-miso = <&gpio0 19 GPIO_FLAG_NONE>;
pin-sclk = <&gpio0 18 GPIO_FLAG_NONE>;
sdcard {
sdcard@0 {
compatible = "espressif,esp32-sdspi";
pin-cs = <&gpio0 5 GPIO_FLAG_NONE>;
frequency-khz = <20000>;
};
};

View File

@ -8,6 +8,7 @@
#include <tactility/bindings/esp32_spi.h>
#include <tactility/bindings/esp32_uart.h>
#include <tactility/bindings/esp32_sdspi.h>
#include <tactility/bindings/display_placeholder.h>
/ {
compatible = "root";
@ -64,13 +65,13 @@
spi1 {
compatible = "espressif,esp32-spi";
host = <SPI3_HOST>;
cs-gpios = <&gpio0 10 GPIO_FLAG_NONE>;
pin-mosi = <&gpio0 11 GPIO_FLAG_NONE>;
pin-miso = <&gpio0 13 GPIO_FLAG_NONE>;
pin-sclk = <&gpio0 12 GPIO_FLAG_NONE>;
sdcard {
sdcard@0 {
compatible = "espressif,esp32-sdspi";
pin-cs = <&gpio0 10 GPIO_FLAG_NONE>;
frequency-khz = <20000>;
};
};

View File

@ -55,9 +55,8 @@
pin-miso = <&gpio0 13 GPIO_FLAG_NONE>;
pin-sclk = <&gpio0 12 GPIO_FLAG_NONE>;
sdcard {
sdcard@0 {
compatible = "espressif,esp32-sdspi";
pin-cs = <&gpio0 10 GPIO_FLAG_NONE>;
frequency-khz = <20000>;
};
};

View File

@ -11,7 +11,6 @@
static const auto LOGGER = tt::Logger("T-Deck");
// Power on
constexpr auto TDECK_POWERON_GPIO = GPIO_NUM_10;
static bool powerOn() {

View File

@ -45,6 +45,7 @@
compatible = "espressif,esp32-spi";
host = <SPI2_HOST>;
cs-gpios = <&gpio0 12 GPIO_FLAG_NONE>, // Display
<&gpio0 9 GPIO_FLAG_NONE>, // Radio
<&gpio0 39 GPIO_FLAG_NONE>; // SD card
pin-mosi = <&gpio0 41 GPIO_FLAG_NONE>;
pin-miso = <&gpio0 38 GPIO_FLAG_NONE>;
@ -54,9 +55,8 @@
compatible = "display-placeholder";
};
sdcard@1 {
sdcard@2 {
compatible = "espressif,esp32-sdspi";
pin-cs = <&gpio0 39 GPIO_FLAG_NONE>;
frequency-khz = <20000>;
};
};

View File

@ -6,6 +6,8 @@
#include <tactility/bindings/esp32_i2c.h>
#include <tactility/bindings/esp32_sdmmc.h>
#include <tactility/bindings/esp32_spi.h>
#include <tactility/bindings/display_placeholder.h>
#include <tactility/bindings/touch_placeholder.h>
/ {
compatible = "root";

View File

@ -43,7 +43,6 @@
sdcard@0 {
compatible = "espressif,esp32-sdspi";
pin-cs = <&gpio0 21 GPIO_FLAG_NONE>;
frequency-khz = <20000>;
};

View File

@ -69,9 +69,8 @@
pin-miso = <&gpio0 39 GPIO_FLAG_NONE>;
pin-sclk = <&gpio0 40 GPIO_FLAG_NONE>;
sdcard {
sdcard@0 {
compatible = "espressif,esp32-sdspi";
pin-cs = <&gpio0 12 GPIO_FLAG_NONE>;
frequency-khz = <20000>;
};
};

View File

@ -55,9 +55,8 @@
pin-miso = <&gpio0 39 GPIO_FLAG_NONE>;
pin-sclk = <&gpio0 40 GPIO_FLAG_NONE>;
sdcard {
sdcard@0 {
compatible = "espressif,esp32-sdspi";
pin-cs = <&gpio0 12 GPIO_FLAG_NONE>;
frequency-khz = <20000>;
};
};

View File

@ -61,7 +61,6 @@
sdcard@0 {
compatible = "espressif,esp32-sdspi";
pin-cs = <&gpio0 4 GPIO_FLAG_NONE>;
frequency-khz = <20000>;
};

View File

@ -86,7 +86,6 @@
sdcard@0 {
compatible = "espressif,esp32-sdspi";
pin-cs = <&gpio0 4 GPIO_FLAG_NONE>;
frequency-khz = <20000>;
};

View File

@ -49,9 +49,8 @@
pin-sclk = <&gpio0 39 GPIO_FLAG_NONE>;
max-transfer-size = <4096>;
sdcard {
sdcard@0 {
compatible = "espressif,esp32-sdspi";
pin-cs = <&gpio0 47 GPIO_FLAG_NONE>;
frequency-khz = <20000>;
};
};

View File

@ -120,7 +120,6 @@
sdcard@0 {
compatible = "espressif,esp32-sdspi";
pin-cs = <&gpio0 4 GPIO_FLAG_NONE>;
frequency-khz = <20000>;
};

View File

@ -71,9 +71,8 @@
pin-miso = <&gpio0 4 GPIO_FLAG_NONE>;
pin-sclk = <&gpio0 5 GPIO_FLAG_NONE>;
sdcard {
sdcard@0 {
compatible = "espressif,esp32-sdspi";
pin-cs = <&gpio0 7 GPIO_FLAG_NONE>;
frequency-khz = <20000>;
};
};

View File

@ -6,6 +6,8 @@
#include <tactility/bindings/esp32_i2c.h>
#include <tactility/bindings/esp32_spi.h>
#include <tactility/bindings/esp32_sdspi.h>
#include <tactility/bindings/display_placeholder.h>
#include <tactility/bindings/touch_placeholder.h>
/ {
compatible = "root";
@ -49,7 +51,6 @@
sdcard@2 {
compatible = "espressif,esp32-sdspi";
pin-cs = <&gpio0 43 GPIO_FLAG_NONE>;
frequency-khz = <20000>;
};
};

View File

@ -56,9 +56,8 @@
pin-miso = <&gpio0 16 GPIO_FLAG_NONE>;
pin-sclk = <&gpio0 21 GPIO_FLAG_NONE>;
sdcard {
sdcard@0 {
compatible = "espressif,esp32-sdspi";
pin-cs = <&gpio0 17 GPIO_FLAG_NONE>;
frequency-khz = <20000>;
};
};

View File

@ -57,9 +57,8 @@
pin-miso = <&gpio0 15 GPIO_FLAG_NONE>;
pin-sclk = <&gpio0 17 GPIO_FLAG_NONE>;
sdcard {
sdcard@0 {
compatible = "espressif,esp32-sdspi";
pin-cs = <&gpio0 18 GPIO_FLAG_NONE>;
frequency-khz = <20000>;
};
};

View File

@ -6,6 +6,7 @@
#include <tactility/bindings/esp32_i2c.h>
#include <tactility/bindings/esp32_spi.h>
#include <tactility/bindings/esp32_sdspi.h>
#include <tactility/bindings/display_placeholder.h>
// Reference: https://www.waveshare.com/wiki/ESP32-S3-Touch-LCD-1.47
/ {
@ -44,13 +45,13 @@
spi1 {
compatible = "espressif,esp32-spi";
host = <SPI3_HOST>;
cs-gpios = <&gpio0 14 GPIO_FLAG_NONE>;
pin-mosi = <&gpio0 15 GPIO_FLAG_NONE>;
pin-miso = <&gpio0 17 GPIO_FLAG_NONE>;
pin-sclk = <&gpio0 16 GPIO_FLAG_NONE>;
sdcard {
sdcard@0 {
compatible = "espressif,esp32-sdspi";
pin-cs = <&gpio0 14 GPIO_FLAG_NONE>;
frequency-khz = <20000>;
};
};

View File

@ -37,9 +37,8 @@
pin-miso = <&gpio0 13 GPIO_FLAG_NONE>;
pin-sclk = <&gpio0 12 GPIO_FLAG_NONE>;
sdcard {
sdcard@0 {
compatible = "espressif,esp32-sdspi";
pin-cs = <&gpio0 10 GPIO_FLAG_NONE>;
frequency-khz = <20000>;
};
};

View File

@ -36,9 +36,8 @@
pin-miso = <&gpio0 38 GPIO_FLAG_NONE>;
pin-sclk = <&gpio0 39 GPIO_FLAG_NONE>;
sdcard {
sdcard@0 {
compatible = "espressif,esp32-sdspi";
pin-cs = <&gpio0 41 GPIO_FLAG_NONE>;
frequency-khz = <20000>;
};
};

View File

@ -8,11 +8,11 @@ properties:
required: true
description: "One of enum Esp32GroveMode"
pinSdaTx:
type: phandle-array
type: phandles
required: true
description: SDA (I2C) or TX (UART) pin
pinSclRx:
type: phandle-array
type: phandles
required: true
description: SCL (I2C) or RX (UART) pin
uartPort:

View File

@ -22,8 +22,8 @@ properties:
Clock source for the I2C peripheral.
If not specified, a default clock source will be used.
pin-sda:
type: phandle-array
type: phandles
required: true
pin-scl:
type: phandle-array
type: phandles
required: true

View File

@ -16,8 +16,8 @@ properties:
required: true
description: Initial clock frequency in Hz
pin-sda:
type: phandle-array
type: phandles
required: true
pin-scl:
type: phandle-array
type: phandles
required: true

View File

@ -12,22 +12,22 @@ properties:
The port number, defined by i2s_port_t.
Depending on the hardware, these values are available: I2S_NUM_0, I2S_NUM_1
pin-bclk:
type: phandle-array
type: phandles
required: true
description: Bit clock pin
pin-ws:
type: phandle-array
type: phandles
required: true
description: Word (slot) select pin
pin-data-out:
type: phandle-array
type: phandles
default: GPIO_PIN_SPEC_NONE
description: Data output pin
pin-data-in:
type: phandle-array
type: phandles
default: GPIO_PIN_SPEC_NONE
description: Data input pin
pin-mclk:
type: phandle-array
type: phandles
default: GPIO_PIN_SPEC_NONE
description: Master clock pin

View File

@ -4,40 +4,40 @@ compatible: "espressif,esp32-sdmmc"
properties:
pin-clk:
type: phandle-array
type: phandles
required: true
pin-cmd:
type: phandle-array
type: phandles
required: true
pin-d0:
type: phandle-array
type: phandles
required: true
pin-d1:
type: phandle-array
type: phandles
default: GPIO_PIN_SPEC_NONE
pin-d2:
type: phandle-array
type: phandles
default: GPIO_PIN_SPEC_NONE
pin-d3:
type: phandle-array
type: phandles
default: GPIO_PIN_SPEC_NONE
pin-d4:
type: phandle-array
type: phandles
default: GPIO_PIN_SPEC_NONE
pin-d5:
type: phandle-array
type: phandles
default: GPIO_PIN_SPEC_NONE
pin-d6:
type: phandle-array
type: phandles
default: GPIO_PIN_SPEC_NONE
pin-d7:
type: phandle-array
type: phandles
default: GPIO_PIN_SPEC_NONE
pin-cd:
type: phandle-array
type: phandles
default: GPIO_PIN_SPEC_NONE
pin-wp:
type: phandle-array
type: phandles
default: GPIO_PIN_SPEC_NONE
bus-width:
type: int

View File

@ -5,20 +5,16 @@ compatible: "espressif,esp32-sdspi"
bus: spi
properties:
pin-cs:
type: phandle-array
required: true
description: Chip select GPIO
pin-cd:
type: phandle-array
type: phandles
default: GPIO_PIN_SPEC_NONE
description: Card detect GPIO
pin-wp:
type: phandle-array
type: phandles
default: GPIO_PIN_SPEC_NONE
description: Write protect GPIO
pin-int:
type: phandle-array
type: phandles
default: GPIO_PIN_SPEC_NONE
description: Interrupt GPIO
frequency-khz:

View File

@ -12,22 +12,22 @@ properties:
The SPI host (controller) to use.
Defined by spi_host_device_t (e.g. SPI2_HOST, SPI3_HOST).
pin-sclk:
type: phandle-array
type: phandles
default: GPIO_PIN_SPEC_NONE
pin-mosi:
type: phandle-array
type: phandles
default: GPIO_PIN_SPEC_NONE
description: MOSI (Data 0) pin
pin-miso:
type: phandle-array
type: phandles
default: GPIO_PIN_SPEC_NONE
description: MISO (Data 1) pin
pin-wp:
type: phandle-array
type: phandles
default: GPIO_PIN_SPEC_NONE
description: WP (Data 2) pin
pin-hd:
type: phandle-array
type: phandles
default: GPIO_PIN_SPEC_NONE
description: HD (Data 3) pin
max-transfer-size:
@ -38,5 +38,6 @@ properties:
0 means the platform decides the limit.
cs-gpios:
type: phandle-array
default: { 0 }
element-type: "struct GpioPinSpec"
default: "{ }"
description: Null-terminated array of chip select GPIO pin specs for peripherals on this bus

View File

@ -12,18 +12,18 @@ properties:
The port number, defined by uart_port_t.
Depending on the hardware, these values are available: UART_NUM_0, UART_NUM_1, UART_NUM_2
pin-tx:
type: phandle-array
type: phandles
required: true
description: TX pin
pin-rx:
type: phandle-array
type: phandles
required: true
description: RX pin
pin-cts:
type: phandle-array
type: phandles
default: GPIO_PIN_SPEC_NONE
description: CTS pin
pin-rts:
type: phandle-array
type: phandles
default: GPIO_PIN_SPEC_NONE
description: RTS pin

View File

@ -9,7 +9,6 @@ extern "C" {
#endif
struct Esp32SdspiConfig {
struct GpioPinSpec pin_cs;
struct GpioPinSpec pin_cd;
struct GpioPinSpec pin_wp;
struct GpioPinSpec pin_int;

View File

@ -23,10 +23,29 @@ struct Esp32SpiConfig {
struct GpioPinSpec pin_hd;
/** Data transfer size limit in bytes. 0 means the platform decides the limit. */
int max_transfer_size;
/** Null-terminated array of chip select GPIO pin specs */
struct GpioPinSpec cs_gpios[];
/** Array of chip select GPIO pin specs */
struct GpioPinSpec* cs_gpios;
/** The item count of cs_gpios */
uint8_t cs_gpios_count;
};
/**
* @brief Get the CS pin spec for a child device on this SPI bus.
* Uses the child device's address as index into the parent's cs_gpios array.
* @param[in] child_device a child device of an SPI controller
* @param[out] out_pin the GPIO pin spec for the CS pin
* @retval ERROR_NONE on success
* @retval ERROR_INVALID_STATE if the parent is not an SPI controller
* @retval ERROR_OUT_OF_RANGE if the device address exceeds the cs_gpios array
*/
error_t esp32_spi_get_cs_pin(struct Device* child_device, struct GpioPinSpec* out_pin);
/**
* @brief Drive all CS pins on this SPI bus high (deselected).
* @param[in] device the SPI controller device
*/
void esp32_spi_deselect_all_cs(struct Device* device);
#ifdef __cplusplus
}
#endif

View File

@ -11,7 +11,7 @@ extern "C" {
struct Esp32SdspiConfig;
typedef void* Esp32SdspiHandle;
Esp32SdspiHandle esp32_sdspi_fs_alloc(const struct Esp32SdspiConfig* config, int spi_host, const char* mount_path);
Esp32SdspiHandle esp32_sdspi_fs_alloc(const struct Esp32SdspiConfig* config, int spi_host, int cs_pin, const char* mount_path);
void esp32_sdspi_fs_free(Esp32SdspiHandle handle);
sdmmc_card_t* esp32_sdspi_fs_get_card(Esp32SdspiHandle handle);

View File

@ -1000,7 +1000,7 @@ const BluetoothApi nimble_bluetooth_api = {
static void create_child_device(struct Device* parent, const char* name,
Driver* drv, struct Device*& out) {
out = new Device { .name = name, .config = nullptr, .parent = nullptr, .internal = nullptr };
out = new Device { .address = 0, .name = name, .config = nullptr, .parent = nullptr, .internal = nullptr };
device_construct(out);
device_set_parent(out, parent);
device_set_driver(out, drv);

View File

@ -238,7 +238,8 @@ static constexpr I2cControllerApi ESP32_I2C_API = {
.write = write,
.write_read = write_read,
.read_register = read_register,
.write_register = write_register
.write_register = write_register,
.probe = nullptr
};
extern Module platform_esp32_module;

View File

@ -24,7 +24,6 @@ struct Esp32SdspiInternal {
RecursiveMutex mutex = {};
Esp32SdspiHandle fs_handle = nullptr;
FileSystem* file_system = nullptr;
GpioDescriptor* pin_cs_descriptor = nullptr;
GpioDescriptor* pin_cd_descriptor = nullptr;
GpioDescriptor* pin_wp_descriptor = nullptr;
GpioDescriptor* pin_int_descriptor = nullptr;
@ -40,7 +39,6 @@ struct Esp32SdspiInternal {
}
void cleanup_pins() {
release_pin(&pin_cs_descriptor);
release_pin(&pin_cd_descriptor);
release_pin(&pin_wp_descriptor);
release_pin(&pin_int_descriptor);
@ -68,7 +66,6 @@ static error_t start(Device* device) {
auto* config = GET_CONFIG(device);
bool pins_ok =
acquire_pin_or_set_null(config->pin_cs, &data->pin_cs_descriptor) &&
acquire_pin_or_set_null(config->pin_cd, &data->pin_cd_descriptor) &&
acquire_pin_or_set_null(config->pin_wp, &data->pin_wp_descriptor) &&
acquire_pin_or_set_null(config->pin_int, &data->pin_int_descriptor);
@ -82,8 +79,25 @@ static error_t start(Device* device) {
return ERROR_RESOURCE;
}
GpioPinSpec cs_pin_spec;
if (esp32_spi_get_cs_pin(device, &cs_pin_spec) != ERROR_NONE) {
LOG_E(TAG, "Failed to get CS pin from parent SPI controller");
data->cleanup_pins();
device_set_driver_data(device, nullptr);
data->unlock();
delete data;
return ERROR_RESOURCE;
}
auto* spi_config = static_cast<const Esp32SpiConfig*>(parent->config);
data->fs_handle = esp32_sdspi_fs_alloc(config, spi_config->host, "/sdcard");
// Lower all CS pins
esp32_spi_deselect_all_cs(parent);
// Manually set the CS pin fo
gpio_set_direction(static_cast<gpio_num_t>(cs_pin_spec.pin), GPIO_MODE_OUTPUT);
gpio_set_level(static_cast<gpio_num_t>(cs_pin_spec.pin), 255);
data->fs_handle = esp32_sdspi_fs_alloc(config, spi_config->host, cs_pin_spec.pin, "/sdcard");
if (!data->fs_handle) {
data->cleanup_pins();
device_set_driver_data(device, nullptr);

View File

@ -17,12 +17,14 @@ struct Esp32SdspiFsData {
const std::string mount_path;
const Esp32SdspiConfig* config;
int spi_host;
int cs_pin;
sdmmc_card_t* card;
Esp32SdspiFsData(const Esp32SdspiConfig* config, int spi_host, const std::string& mount_path) :
Esp32SdspiFsData(const Esp32SdspiConfig* config, int spi_host, int cs_pin, const std::string& mount_path) :
mount_path(mount_path),
config(config),
spi_host(spi_host),
cs_pin(cs_pin),
card(nullptr)
{}
};
@ -34,8 +36,8 @@ static gpio_num_t to_native_pin(GpioPinSpec pin_spec) {
extern "C" {
Esp32SdspiHandle esp32_sdspi_fs_alloc(const Esp32SdspiConfig* config, int spi_host, const char* mount_path) {
return new(std::nothrow) Esp32SdspiFsData(config, spi_host, mount_path);
Esp32SdspiHandle esp32_sdspi_fs_alloc(const Esp32SdspiConfig* config, int spi_host, int cs_pin, const char* mount_path) {
return new(std::nothrow) Esp32SdspiFsData(config, spi_host, cs_pin, mount_path);
}
void esp32_sdspi_fs_free(Esp32SdspiHandle handle) {
@ -62,7 +64,7 @@ static error_t mount(void* data) {
sdspi_device_config_t slot_config = SDSPI_DEVICE_CONFIG_DEFAULT();
slot_config.host_id = static_cast<spi_host_device_t>(fs_data->spi_host);
slot_config.gpio_cs = to_native_pin(config->pin_cs);
slot_config.gpio_cs = static_cast<gpio_num_t>(fs_data->cs_pin);
slot_config.gpio_cd = to_native_pin(config->pin_cd);
slot_config.gpio_wp = to_native_pin(config->pin_wp);
slot_config.gpio_int = to_native_pin(config->pin_int);

View File

@ -10,6 +10,7 @@
#include <cstring>
#include <new>
#include <soc/gpio_num.h>
#include <vector>
#define TAG "esp32_spi"
@ -22,13 +23,16 @@ struct Esp32SpiInternal {
RecursiveMutex mutex = {};
bool initialized = false;
// Pin descriptors
// Bus pin descriptors
GpioDescriptor* sclk_descriptor = nullptr;
GpioDescriptor* mosi_descriptor = nullptr;
GpioDescriptor* miso_descriptor = nullptr;
GpioDescriptor* wp_descriptor = nullptr;
GpioDescriptor* hd_descriptor = nullptr;
// CS pin descriptors
std::vector<GpioDescriptor*> cs_descriptors;
explicit Esp32SpiInternal() {
recursive_mutex_construct(&mutex);
}
@ -44,6 +48,10 @@ struct Esp32SpiInternal {
release_pin(&miso_descriptor);
release_pin(&wp_descriptor);
release_pin(&hd_descriptor);
for (auto*& desc : cs_descriptors) {
release_pin(&desc);
}
cs_descriptors.clear();
}
};
@ -118,22 +126,30 @@ static error_t start(Device* device) {
return ERROR_RESOURCE;
}
// Deselect all CS pins (drive high) before any SPI communication
const GpioPinSpec* cs = dts_config->cs_gpios;
while (cs->gpio_controller != nullptr) {
// Acquire and deselect all CS pins (drive high)
for (uint8_t i = 0; i < dts_config->cs_gpios_count; i++) {
const GpioPinSpec* cs = &dts_config->cs_gpios[i];
if (cs->gpio_controller == nullptr) continue;
GpioDescriptor* desc = gpio_descriptor_acquire(cs->gpio_controller, cs->pin, GPIO_OWNER_SPI);
if (desc != nullptr) {
gpio_descriptor_set_flags(desc, GPIO_FLAG_DIRECTION_OUTPUT);
gpio_descriptor_set_level(desc, true);
gpio_descriptor_release(desc);
data->cs_descriptors.push_back(desc);
}
cs++;
}
data->initialized = true;
return ERROR_NONE;
}
void esp32_spi_deselect_all_cs(Device* device) {
auto* data = GET_DATA(device);
if (data == nullptr) return;
for (auto* desc : data->cs_descriptors) {
gpio_descriptor_set_level(desc, true);
}
}
static error_t stop(Device* device) {
LOG_I(TAG, "stop %s", device->name);
auto* driver_data = GET_DATA(device);
@ -149,6 +165,16 @@ static error_t stop(Device* device) {
return ERROR_NONE;
}
error_t esp32_spi_get_cs_pin(Device* child_device, GpioPinSpec* out_pin) {
auto* parent = device_get_parent(child_device);
if (parent == nullptr || device_get_type(parent) != &SPI_CONTROLLER_TYPE) return ERROR_INVALID_STATE;
auto* config = GET_CONFIG(parent);
int32_t index = child_device->address;
if (index < 0 || index >= config->cs_gpios_count) return ERROR_OUT_OF_RANGE;
*out_pin = config->cs_gpios[index];
return ERROR_NONE;
}
const static struct SpiControllerApi esp32_spi_api = {
.lock = lock,
.try_lock = try_lock,

View File

@ -1,9 +1,5 @@
description: SPI peripheral with chip select
description: SPI peripheral
compatible: "spi-peripheral"
properties:
pin-cs:
type: phandle-array
required: true
description: Chip select GPIO
properties: {}

View File

@ -1,23 +1,17 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <stdint.h>
#include <tactility/device.h>
#include <tactility/drivers/gpio.h>
#ifdef __cplusplus
extern "C" {
#endif
struct SpiPeripheralConfig {
struct GpioPinSpec pin_cs;
uint8_t _unused;
};
struct SpiPeripheralApi {
struct GpioDescriptor* (*get_cs_descriptor)(struct Device* device);
};
struct GpioDescriptor* spi_peripheral_get_cs_descriptor(struct Device* device);
extern const struct DeviceType SPI_PERIPHERAL_TYPE;
#ifdef __cplusplus

View File

@ -24,6 +24,7 @@ typedef int error_t;
#define ERROR_NOT_SUPPORTED 10
#define ERROR_NOT_ALLOWED 11
#define ERROR_BUFFER_OVERFLOW 12
#define ERROR_OUT_OF_RANGE 13
/** Convert an error_t to a human-readable text. Useful for logging. */
const char* error_to_string(error_t error);

View File

@ -1,61 +1,12 @@
// SPDX-License-Identifier: Apache-2.0
#include <tactility/drivers/spi_peripheral.h>
#include <tactility/drivers/gpio_controller.h>
#include <tactility/drivers/spi_controller.h>
#include <tactility/driver.h>
#include <tactility/module.h>
#include <tactility/log.h>
#define TAG "spi_peripheral"
#define GET_CONFIG(device) ((const struct SpiPeripheralConfig*)device->config)
extern "C" {
static error_t start(Device* device) {
auto* parent = device_get_parent(device);
if (parent == nullptr || device_get_type(parent) != &SPI_CONTROLLER_TYPE) {
LOG_E(TAG, "Parent is not an SPI controller");
return ERROR_INVALID_STATE;
}
auto* config = GET_CONFIG(device);
GpioDescriptor* cs = nullptr;
if (config->pin_cs.gpio_controller != nullptr) {
cs = gpio_descriptor_acquire(config->pin_cs.gpio_controller, config->pin_cs.pin, GPIO_OWNER_SPI);
if (cs == nullptr) {
LOG_E(TAG, "Failed to acquire CS pin");
return ERROR_RESOURCE;
}
gpio_descriptor_set_flags(cs, GPIO_FLAG_DIRECTION_OUTPUT);
gpio_descriptor_set_level(cs, true);
}
device_set_driver_data(device, cs);
return ERROR_NONE;
}
static error_t stop(Device* device) {
auto* cs = static_cast<GpioDescriptor*>(device_get_driver_data(device));
if (cs != nullptr) {
gpio_descriptor_release(cs);
}
device_set_driver_data(device, nullptr);
return ERROR_NONE;
}
static GpioDescriptor* get_cs_descriptor(Device* device) {
return static_cast<GpioDescriptor*>(device_get_driver_data(device));
}
static const SpiPeripheralApi spi_peripheral_api = {
.get_cs_descriptor = get_cs_descriptor
};
GpioDescriptor* spi_peripheral_get_cs_descriptor(Device* device) {
auto* driver = device_get_driver(device);
return ((const SpiPeripheralApi*)driver->api)->get_cs_descriptor(device);
}
static error_t start(Device*) { return ERROR_NONE; }
static error_t stop(Device*) { return ERROR_NONE; }
const DeviceType SPI_PERIPHERAL_TYPE = {
.name = "spi_peripheral"
@ -68,7 +19,7 @@ Driver spi_peripheral_driver = {
.compatible = (const char*[]) { "spi-peripheral", nullptr },
.start_device = start,
.stop_device = stop,
.api = &spi_peripheral_api,
.api = nullptr,
.device_type = &SPI_PERIPHERAL_TYPE,
.owner = &root_module,
.internal = nullptr