Compare commits

...

8 Commits

Author SHA1 Message Date
Ken Van Hoeylandt
bcbfa70943 Refactored I2S GPIO to use GpioPinSpec 2026-02-10 23:08:25 +01:00
Ken Van Hoeylandt
e960cd93c3 DTS parsing improvement for phandle-array created by #define 2026-02-10 23:07:56 +01:00
Ken Van Hoeylandt
f491a86064 Update pin type 2026-02-10 21:26:26 +01:00
Ken Van Hoeylandt
e1064101a6 Bindings update for I2C 2026-02-10 21:26:19 +01:00
Ken Van Hoeylandt
3a4acd1313 Fixes 2026-02-10 21:18:56 +01:00
Ken Van Hoeylandt
d178d674f4 Fixes 2026-02-10 21:11:33 +01:00
Ken Van Hoeylandt
fc941ff495 Fixes 2026-02-10 20:50:32 +01:00
Ken Van Hoeylandt
d9a39726d1 GPIO driver with pin ownership, updated I2C driver 2026-02-10 20:30:07 +01:00
55 changed files with 513 additions and 325 deletions

View File

@ -57,7 +57,7 @@ def find_phandle(devices: list[Device], phandle: str):
for device in devices:
if device.node_name == phandle or device.node_alias == phandle:
return f"&{get_device_node_name_safe(device)}"
raise DevicetreeException(f"phandle '{phandle}' not found in device tree")
raise DevicetreeException(f"phandle '{phandle}' not found in devicetree")
def property_to_string(property: DeviceProperty, devices: list[Device]) -> str:
type = property.type
@ -77,6 +77,20 @@ 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":
value_list = list()
if isinstance(property.value, list):
for item in property.value:
if isinstance(item, PropertyValue):
value_list.append(property_to_string(DeviceProperty(name="", type=item.type, value=item.value), devices))
else:
value_list.append(str(item))
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}")
else:
raise DevicetreeException(f"property_to_string() has an unsupported type: {type}")

View File

@ -18,15 +18,15 @@
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_0>;
clock-frequency = <400000>;
pin-sda = <2>;
pin-scl = <1>;
pin-sda = <&gpio0 2 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 1 GPIO_FLAG_NONE>;
};
i2c_external: i2c1 {
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_1>;
clock-frequency = <400000>;
pin-sda = <4>;
pin-scl = <3>;
pin-sda = <&gpio0 4 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 3 GPIO_FLAG_NONE>;
};
};

View File

@ -18,8 +18,8 @@
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_0>;
clock-frequency = <400000>;
pin-sda = <33>;
pin-scl = <32>;
pin-sda = <&gpio0 33 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 32 GPIO_FLAG_NONE>;
};
display_spi: spi0 {

View File

@ -19,8 +19,8 @@
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_0>;
clock-frequency = <400000>;
pin-sda = <27>;
pin-scl = <22>;
pin-sda = <&gpio0 27 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 22 GPIO_FLAG_NONE>;
};
display_spi: spi0 {

View File

@ -19,8 +19,8 @@
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_0>;
clock-frequency = <400000>;
pin-sda = <27>;
pin-scl = <22>;
pin-sda = <&gpio0 27 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 22 GPIO_FLAG_NONE>;
};
display_spi: spi0 {

View File

@ -18,8 +18,8 @@
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_0>;
clock-frequency = <400000>;
pin-sda = <33>;
pin-scl = <32>;
pin-sda = <&gpio1 33 GPIO_FLAG_NONE>;
pin-scl = <&gpio1 32 GPIO_FLAG_NONE>;
};
display_spi: spi0 {

View File

@ -18,8 +18,8 @@
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_0>;
clock-frequency = <400000>;
pin-sda = <19>;
pin-scl = <45>;
pin-sda = <&gpio0 19 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 45 GPIO_FLAG_NONE>;
};
spi0 {

View File

@ -19,16 +19,16 @@
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_0>;
clock-frequency = <400000>;
pin-sda = <19>;
pin-scl = <20>;
pin-sda = <&gpio0 19 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 20 GPIO_FLAG_NONE>;
};
i2c_external {
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_1>;
clock-frequency = <400000>;
pin-sda = <17>;
pin-scl = <18>;
pin-sda = <&gpio0 17 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 18 GPIO_FLAG_NONE>;
};
spi0 {

View File

@ -18,8 +18,8 @@
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_0>;
clock-frequency = <400000>;
pin-sda = <32>;
pin-scl = <25>;
pin-sda = <&gpio0 32 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 25 GPIO_FLAG_NONE>;
};
display_spi: spi0 {

View File

@ -19,8 +19,8 @@
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_0>;
clock-frequency = <400000>;
pin-sda = <15>;
pin-scl = <16>;
pin-sda = <&gpio0 15 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 16 GPIO_FLAG_NONE>;
};
display_spi: spi0 {

View File

@ -19,8 +19,8 @@
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_0>;
clock-frequency = <400000>;
pin-sda = <15>;
pin-scl = <16>;
pin-sda = <&gpio0 15 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 16 GPIO_FLAG_NONE>;
};
display_spi: spi0 {

View File

@ -19,8 +19,8 @@
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_0>;
clock-frequency = <400000>;
pin-sda = <15>;
pin-scl = <16>;
pin-sda = <&gpio0 15 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 16 GPIO_FLAG_NONE>;
};
sdcard_spi: spi0 {

View File

@ -19,8 +19,8 @@
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_0>;
clock-frequency = <400000>;
pin-sda = <22>;
pin-scl = <21>;
pin-sda = <&gpio0 22 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 21 GPIO_FLAG_NONE>;
};
display_spi: spi0 {

View File

@ -19,8 +19,8 @@
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_0>;
clock-frequency = <400000>;
pin-sda = <22>;
pin-scl = <21>;
pin-sda = <&gpio0 22 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 21 GPIO_FLAG_NONE>;
};
display_spi: spi0 {

View File

@ -19,8 +19,8 @@
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_0>;
clock-frequency = <400000>;
pin-sda = <19>;
pin-scl = <20>;
pin-sda = <&gpio0 19 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 20 GPIO_FLAG_NONE>;
};
sdcard_spi: spi0 {

View File

@ -26,8 +26,8 @@
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_0>;
clock-frequency = <400000>;
pin-sda = <7>;
pin-scl = <8>;
pin-sda = <&gpio0 7 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 8 GPIO_FLAG_NONE>;
};
i2s0 {

View File

@ -19,8 +19,8 @@
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_0>;
clock-frequency = <400000>;
pin-sda = <33>;
pin-scl = <32>;
pin-sda = <&gpio0 33 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 32 GPIO_FLAG_NONE>;
};
// CN1 header
@ -28,8 +28,8 @@
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_1>;
clock-frequency = <400000>;
pin-sda = <21>;
pin-scl = <22>;
pin-sda = <&gpio0 21 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 22 GPIO_FLAG_NONE>;
};
display_spi: spi0 {

View File

@ -19,16 +19,16 @@
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_0>;
clock-frequency = <400000>;
pin-sda = <4>;
pin-scl = <8>;
pin-sda = <&gpio0 4 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 8 GPIO_FLAG_NONE>;
};
i2c_external: i2c1 {
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_1>;
clock-frequency = <400000>;
pin-sda = <17>;
pin-scl = <18>;
pin-sda = <&gpio0 17 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 18 GPIO_FLAG_NONE>;
};
display_spi: spi0 {

View File

@ -19,16 +19,16 @@
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_0>;
clock-frequency = <400000>;
pin-sda = <19>;
pin-scl = <20>;
pin-sda = <&gpio0 19 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 20 GPIO_FLAG_NONE>;
};
i2c_external: i2c1 {
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_1>;
clock-frequency = <400000>;
pin-sda = <17>;
pin-scl = <18>;
pin-sda = <&gpio0 17 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 18 GPIO_FLAG_NONE>;
};
spi0 {

View File

@ -17,7 +17,7 @@
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_0>;
clock-frequency = <200000>;
pin-sda = <17>;
pin-scl = <18>;
pin-sda = <&gpio0 17 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 18 GPIO_FLAG_NONE>;
};
};

View File

@ -21,24 +21,24 @@
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_0>;
clock-frequency = <400000>;
pin-sda = <18>;
pin-scl = <8>;
pin-sda = <&gpio0 18 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 8 GPIO_FLAG_NONE>;
};
i2c_external: i2c1 {
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_1>;
clock-frequency = <400000>;
pin-sda = <43>;
pin-scl = <44>;
pin-sda = <&gpio0 43 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 44 GPIO_FLAG_NONE>;
};
i2s0 {
compatible = "espressif,esp32-i2s";
port = <I2S_NUM_0>;
pin-bclk = <7>;
pin-ws = <5>;
pin-data-out = <6>;
pin-bclk = <&gpio0 7 GPIO_FLAG_NONE>;
pin-ws = <&gpio0 5 GPIO_FLAG_NONE>;
pin-data-out = <&gpio0 6 GPIO_FLAG_NONE>;
};
spi0 {

View File

@ -19,8 +19,8 @@
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_0>;
clock-frequency = <400000>;
pin-sda = <44>;
pin-scl = <43>;
pin-sda = <&gpio0 44 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 43 GPIO_FLAG_NONE>;
};
spi0 {

View File

@ -21,8 +21,8 @@
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_0>;
clock-frequency = <100000>;
pin-sda = <3>;
pin-scl = <2>;
pin-sda = <&gpio0 3 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 2 GPIO_FLAG_NONE>;
};
spi0 {

View File

@ -21,16 +21,16 @@
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_0>;
clock-frequency = <400000>;
pin-sda = <8>;
pin-scl = <9>;
pin-sda = <&gpio0 8 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 9 GPIO_FLAG_NONE>;
};
i2c_port_a {
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_1>;
clock-frequency = <400000>;
pin-sda = <2>;
pin-scl = <1>;
pin-sda = <&gpio0 2 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 1 GPIO_FLAG_NONE>;
};
display_spi: spi0 {

View File

@ -21,8 +21,8 @@
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_0>;
clock-frequency = <400000>;
pin-sda = <2>;
pin-scl = <1>;
pin-sda = <&gpio0 2 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 1 GPIO_FLAG_NONE>;
};
display_spi: spi0 {

View File

@ -21,16 +21,16 @@
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_0>;
clock-frequency = <400000>;
pin-sda = <21>;
pin-scl = <22>;
pin-sda = <&gpio0 21 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 22 GPIO_FLAG_NONE>;
};
i2c_port_a {
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_1>;
clock-frequency = <400000>;
pin-sda = <32>;
pin-scl = <33>;
pin-sda = <&gpio0 32 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 33 GPIO_FLAG_NONE>;
};
spi0 {

View File

@ -21,32 +21,32 @@
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_0>;
clock-frequency = <400000>;
pin-sda = <12>;
pin-scl = <11>;
pin-sda = <&gpio0 12 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 11 GPIO_FLAG_NONE>;
};
i2c_port_a {
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_1>;
clock-frequency = <400000>;
pin-sda = <2>;
pin-scl = <1>;
pin-sda = <&gpio0 2 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 1 GPIO_FLAG_NONE>;
};
/*
i2c_port_b {
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_1>;
clock-frequency = <400000>;
pin-sda = <9>;
pin-scl = <8>;
pin-sda = <&gpio0 9 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 8 GPIO_FLAG_NONE>;
};
i2c_port_c {
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_1>;
clock-frequency = <400000>;
pin-sda = <18>;
pin-scl = <17>;
pin-sda = <&gpio0 18 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 17 GPIO_FLAG_NONE>;
};
*/
spi0 {

View File

@ -18,8 +18,8 @@
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_0>;
clock-frequency = <400000>;
pin-sda = <41>;
pin-scl = <42>;
pin-sda = <&gpio0 41 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 42 GPIO_FLAG_NONE>;
};
spi0 {

View File

@ -19,16 +19,16 @@
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_0>;
clock-frequency = <400000>;
pin-sda = <21>;
pin-scl = <22>;
pin-sda = <&gpio0 21 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 22 GPIO_FLAG_NONE>;
};
i2c_grove {
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_1>;
clock-frequency = <400000>;
pin-sda = <32>;
pin-scl = <33>;
pin-sda = <&gpio0 32 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 33 GPIO_FLAG_NONE>;
};
spi0 {

View File

@ -18,16 +18,16 @@
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_0>;
clock-frequency = <400000>;
pin-sda = <21>;
pin-scl = <22>;
pin-sda = <&gpio0 21 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 22 GPIO_FLAG_NONE>;
};
i2c_grove {
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_1>;
clock-frequency = <400000>;
pin-sda = <32>;
pin-scl = <33>;
pin-sda = <&gpio0 32 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 33 GPIO_FLAG_NONE>;
};
spi0 {

View File

@ -18,16 +18,16 @@
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_0>;
clock-frequency = <400000>;
pin-sda = <31>;
pin-scl = <32>;
pin-sda = <&gpio0 31 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 32 GPIO_FLAG_NONE>;
};
i2c_port_a {
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_1>;
clock-frequency = <400000>;
pin-sda = <53>;
pin-scl = <54>;
pin-sda = <&gpio0 53 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 54 GPIO_FLAG_NONE>;
};
sdcard_spi: spi0 {

View File

@ -18,8 +18,8 @@
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_0>;
clock-frequency = <400000>;
pin-sda = <3>;
pin-scl = <4>;
pin-sda = <&gpio0 3 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 4 GPIO_FLAG_NONE>;
};
sdcard_spi: spi0 {

View File

@ -19,8 +19,8 @@
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_0>;
clock-frequency = <400000>;
pin-sda = <16>;
pin-scl = <17>;
pin-sda = <&gpio0 16 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 17 GPIO_FLAG_NONE>;
};
spi0 {

View File

@ -19,8 +19,8 @@
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_0>;
clock-frequency = <400000>;
pin-sda = <47>;
pin-scl = <48>;
pin-sda = <&gpio0 47 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 48 GPIO_FLAG_NONE>;
};
spi0 {

View File

@ -19,8 +19,8 @@
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_0>;
clock-frequency = <400000>;
pin-sda = <6>;
pin-scl = <7>;
pin-sda = <&gpio0 6 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 7 GPIO_FLAG_NONE>;
};
display_spi: spi0 {

View File

@ -19,8 +19,8 @@
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_0>;
clock-frequency = <400000>;
pin-sda = <42>;
pin-scl = <41>;
pin-sda = <&gpio0 42 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 41 GPIO_FLAG_NONE>;
};
display_spi: spi0 {

View File

@ -19,8 +19,8 @@
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_0>;
clock-frequency = <400000>;
pin-sda = <8>;
pin-scl = <9>;
pin-sda = <&gpio0 8 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 9 GPIO_FLAG_NONE>;
};
spi0 {

View File

@ -18,8 +18,8 @@
compatible = "espressif,esp32-i2c";
port = <I2C_NUM_0>;
clock-frequency = <400000>;
pin-sda = <6>;
pin-scl = <5>;
pin-sda = <&gpio0 6 GPIO_FLAG_NONE>;
pin-scl = <&gpio0 5 GPIO_FLAG_NONE>;
};
spi0 {

View File

@ -53,7 +53,7 @@ add_custom_command(
"${GENERATED_DIR}/devicetree.h"
COMMAND pip install lark==1.3.1 pyyaml==6.0.3
COMMAND python "${CMAKE_SOURCE_DIR}/Buildscripts/DevicetreeCompiler/compile.py"
"${DEVICETREE_LOCATION}" "${GENERATED_DIR}"
"${DEVICETREE_LOCATION}" "${GENERATED_DIR}"
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
DEPENDS AlwaysRun "${DEVICETREE_LOCATION}/devicetree.yaml" # AlwaysRun ensures it always gets built
COMMENT "Generating devicetree source files..."

View File

@ -16,14 +16,8 @@ properties:
required: true
description: Initial clock frequency in Hz
pin-sda:
type: int
type: phandle-array
required: true
pin-scl:
type: int
type: phandle-array
required: true
pin-sda-pull-up:
type: bool
description: enable internal pull-up resistor for SDA pin
pin-scl-pull-up:
type: bool
description: enable internal pull-up resistor for SCL pin

View File

@ -12,23 +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: int
type: phandle-array
required: true
description: BCK pin
description: Bit clock pin
pin-ws:
type: int
type: phandle-array
required: true
description: WS pin
description: Word (slot) select pin
pin-data-out:
type: int
default: GPIO_PIN_NONE
description: DATA OUT pin
type: phandle-array
default: GPIO_PIN_SPEC_NONE
description: Data output pin
pin-data-in:
type: int
default: GPIO_PIN_NONE
description: DATA IN pin
type: phandle-array
default: GPIO_PIN_SPEC_NONE
description: Data input pin
pin-mclk:
type: int
required: false
default: GPIO_PIN_NONE
description: MCLK pin
type: phandle-array
default: GPIO_PIN_SPEC_NONE
description: Master clock pin

View File

@ -9,6 +9,16 @@
extern "C" {
#endif
#define GPIO_ACTIVE_HIGH GPIO_FLAG_ACTIVE_HIGH
#define GPIO_ACTIVE_LOW GPIO_FLAG_ACTIVE_LOW
#define GPIO_DIRECTION_INPUT GPIO_FLAG_DIRECTION_INPUT
#define GPIO_DIRECTION_OUTPUT GPIO_FLAG_DIRECTION_OUTPUT
#define GPIO_DIRECTION_INPUT_OUTPUT GPIO_FLAG_DIRECTION_INPUT_OUTPUT
#define GPIO_PULL_UP GPIO_FLAG_PULL_UP
#define GPIO_PULL_DOWN GPIO_FLAG_PULL_DOWN
DEFINE_DEVICETREE(esp32_gpio, struct Esp32GpioConfig)
#ifdef __cplusplus

View File

@ -3,6 +3,7 @@
#include <tactility/bindings/bindings.h>
#include <tactility/drivers/esp32_i2s.h>
#include <tactility/drivers/esp32_gpio.h>
#include <driver/i2s_common.h>
#ifdef __cplusplus

View File

@ -11,13 +11,10 @@ extern "C" {
struct Esp32I2cConfig {
i2c_port_t port;
uint32_t clockFrequency;
gpio_pin_t pinSda;
gpio_pin_t pinScl;
bool pinSdaPullUp;
bool pinSclPullUp;
struct GpioPinSpec pinSda;
struct GpioPinSpec pinScl;
};
#ifdef __cplusplus
}
#endif

View File

@ -2,6 +2,7 @@
#pragma once
#include <tactility/drivers/i2s_controller.h>
#include <tactility/drivers/gpio.h>
#include <driver/i2s_common.h>
#ifdef __cplusplus
@ -10,11 +11,11 @@ extern "C" {
struct Esp32I2sConfig {
i2s_port_t port;
int pin_bclk;
int pin_ws;
int pin_data_out;
int pin_data_in;
int pin_mclk;
struct GpioPinSpec pin_bclk;
struct GpioPinSpec pin_ws;
struct GpioPinSpec pin_data_out;
struct GpioPinSpec pin_data_in;
struct GpioPinSpec pin_mclk;
};
#ifdef __cplusplus

View File

@ -26,7 +26,7 @@ static error_t get_level(GpioDescriptor* descriptor, bool* high) {
return ERROR_NONE;
}
static error_t set_options(GpioDescriptor* descriptor, gpio_flags_t options) {
static error_t set_flags(GpioDescriptor* descriptor, gpio_flags_t flags) {
const Esp32GpioConfig* config = GET_CONFIG(descriptor->controller);
if (descriptor->pin >= config->gpioCount) {
@ -34,11 +34,11 @@ static error_t set_options(GpioDescriptor* descriptor, gpio_flags_t options) {
}
gpio_mode_t mode;
if ((options & GPIO_DIRECTION_INPUT_OUTPUT) == GPIO_DIRECTION_INPUT_OUTPUT) {
if ((flags & GPIO_FLAG_DIRECTION_INPUT_OUTPUT) == GPIO_FLAG_DIRECTION_INPUT_OUTPUT) {
mode = GPIO_MODE_INPUT_OUTPUT;
} else if (options & GPIO_DIRECTION_INPUT) {
} else if (flags & GPIO_FLAG_DIRECTION_INPUT) {
mode = GPIO_MODE_INPUT;
} else if (options & GPIO_DIRECTION_OUTPUT) {
} else if (flags & GPIO_FLAG_DIRECTION_OUTPUT) {
mode = GPIO_MODE_OUTPUT;
} else {
return ERROR_INVALID_ARGUMENT;
@ -47,9 +47,9 @@ static error_t set_options(GpioDescriptor* descriptor, gpio_flags_t options) {
const gpio_config_t esp_config = {
.pin_bit_mask = 1ULL << descriptor->pin,
.mode = mode,
.pull_up_en = (options & GPIO_PULL_UP) ? GPIO_PULLUP_ENABLE : GPIO_PULLUP_DISABLE,
.pull_down_en = (options & GPIO_PULL_DOWN) ? GPIO_PULLDOWN_ENABLE : GPIO_PULLDOWN_DISABLE,
.intr_type = GPIO_INTERRUPT_FROM_OPTIONS(options),
.pull_up_en = (flags & GPIO_FLAG_PULL_UP) ? GPIO_PULLUP_ENABLE : GPIO_PULLUP_DISABLE,
.pull_down_en = (flags & GPIO_FLAG_PULL_DOWN) ? GPIO_PULLDOWN_ENABLE : GPIO_PULLDOWN_DISABLE,
.intr_type = GPIO_FLAG_INTERRUPT_FROM_OPTIONS(flags),
#if SOC_GPIO_SUPPORT_PIN_HYS_FILTER
.hys_ctrl_mode = GPIO_HYS_SOFT_DISABLE
#endif
@ -59,7 +59,7 @@ static error_t set_options(GpioDescriptor* descriptor, gpio_flags_t options) {
return esp_err_to_error(esp_error);
}
static error_t get_options(GpioDescriptor* descriptor, gpio_flags_t* options) {
static error_t get_flags(GpioDescriptor* descriptor, gpio_flags_t* flags) {
gpio_io_config_t esp_config;
if (gpio_get_io_config(static_cast<gpio_num_t>(descriptor->pin), &esp_config) != ESP_OK) {
return ERROR_RESOURCE;
@ -68,32 +68,32 @@ static error_t get_options(GpioDescriptor* descriptor, gpio_flags_t* options) {
gpio_flags_t output = 0;
if (esp_config.pu) {
output |= GPIO_PULL_UP;
output |= GPIO_FLAG_PULL_UP;
}
if (esp_config.pd) {
output |= GPIO_PULL_DOWN;
output |= GPIO_FLAG_PULL_DOWN;
}
if (esp_config.ie) {
output |= GPIO_DIRECTION_INPUT;
output |= GPIO_FLAG_DIRECTION_INPUT;
}
if (esp_config.oe) {
output |= GPIO_DIRECTION_OUTPUT;
output |= GPIO_FLAG_DIRECTION_OUTPUT;
}
if (esp_config.oe_inv) {
output |= GPIO_ACTIVE_LOW;
output |= GPIO_FLAG_ACTIVE_LOW;
}
*options = output;
*flags = output;
return ERROR_NONE;
}
static error_t get_pin_number(struct GpioDescriptor* descriptor, gpio_pin_t* pin) {
if (!descriptor) return ERROR_INVALID_ARGUMENT;
*pin = descriptor->pin;
static error_t get_native_pin_number(GpioDescriptor* descriptor, void* pin_number) {
auto* esp_pin_number = reinterpret_cast<gpio_num_t*>(pin_number);
*esp_pin_number = static_cast<gpio_num_t>(descriptor->pin);
return ERROR_NONE;
}
@ -109,13 +109,11 @@ static error_t stop(Device* device) {
}
const static GpioControllerApi esp32_gpio_api = {
.acquire_descriptor = acquire_descriptor,
.release_descriptor = release_descriptor,
.get_pin_number = get_pin_number,
.set_level = set_level,
.get_level = get_level,
.set_options = set_options,
.get_options = get_options
.set_flags = set_flags,
.get_flags = get_flags,
.get_native_pin_number = get_native_pin_number
};
extern struct Module platform_module;

View File

@ -1,31 +1,38 @@
// SPDX-License-Identifier: Apache-2.0
#include <driver/i2c.h>
#include <tactility/driver.h>
#include <tactility/drivers/i2c_controller.h>
#include <tactility/log.h>
#include <new>
#include <tactility/time.h>
#include <tactility/error_esp32.h>
#include <tactility/driver.h>
#include <tactility/drivers/gpio_controller.h>
#include <tactility/drivers/i2c_controller.h>
#include <tactility/drivers/esp32_i2c.h>
#include <tactility/log.h>
#include <tactility/time.h>
#define TAG "esp32_i2c"
#define ACK_CHECK_EN 1
struct Esp32SpiInternal {
Mutex mutex { 0 };
struct Esp32I2cInternal {
Mutex mutex {};
GpioDescriptor* sda_descriptor = nullptr;
GpioDescriptor* scl_descriptor = nullptr;
Esp32SpiInternal() {
Esp32I2cInternal(GpioDescriptor* sda_descriptor, GpioDescriptor* scl_descriptor) :
sda_descriptor(sda_descriptor),
scl_descriptor(scl_descriptor)
{
mutex_construct(&mutex);
}
~Esp32SpiInternal() {
~Esp32I2cInternal() {
mutex_destruct(&mutex);
}
};
#define GET_CONFIG(device) ((Esp32I2cConfig*)device->config)
#define GET_DATA(device) ((Esp32SpiInternal*)device_get_driver_data(device))
#define GET_DATA(device) ((Esp32I2cInternal*)device_get_driver_data(device))
#define lock(data) mutex_lock(&data->mutex);
#define unlock(data) mutex_unlock(&data->mutex);
@ -149,12 +156,35 @@ static error_t start(Device* device) {
ESP_LOGI(TAG, "start %s", device->name);
auto dts_config = GET_CONFIG(device);
auto& sda_spec = dts_config->pinSda;
auto& scl_spec = dts_config->pinScl;
auto* sda_descriptor = gpio_descriptor_acquire(sda_spec.gpio_controller, sda_spec.pin, GPIO_OWNER_GPIO);
if (!sda_descriptor) {
LOG_E(TAG, "Failed to acquire pin %u", sda_spec.pin);
return ERROR_RESOURCE;
}
auto* scl_descriptor = gpio_descriptor_acquire(scl_spec.gpio_controller, scl_spec.pin, GPIO_OWNER_GPIO);
if (!scl_descriptor) {
LOG_E(TAG, "Failed to acquire pin %u", scl_spec.pin);
gpio_descriptor_release(sda_descriptor);
return ERROR_RESOURCE;
}
gpio_num_t sda_pin, scl_pin;
check(gpio_descriptor_get_native_pin_number(sda_descriptor, &sda_pin) == ERROR_NONE);
check(gpio_descriptor_get_native_pin_number(scl_descriptor, &scl_pin) == ERROR_NONE);
gpio_flags_t sda_flags, scl_flags;
check(gpio_descriptor_get_flags(sda_descriptor, &sda_flags) == ERROR_NONE);
check(gpio_descriptor_get_flags(scl_descriptor, &scl_flags) == ERROR_NONE);
i2c_config_t esp_config = {
.mode = I2C_MODE_MASTER,
.sda_io_num = dts_config->pinSda,
.scl_io_num = dts_config->pinScl,
.sda_pullup_en = dts_config->pinSdaPullUp,
.scl_pullup_en = dts_config->pinSclPullUp,
.sda_io_num = sda_pin,
.scl_io_num = scl_pin,
.sda_pullup_en = (sda_flags & GPIO_FLAG_PULL_UP) != 0,
.scl_pullup_en = (scl_flags & GPIO_FLAG_PULL_UP) != 0,
.master {
.clk_speed = dts_config->clockFrequency
},
@ -164,22 +194,33 @@ static error_t start(Device* device) {
esp_err_t error = i2c_param_config(dts_config->port, &esp_config);
if (error != ESP_OK) {
LOG_E(TAG, "Failed to configure port %d: %s", static_cast<int>(dts_config->port), esp_err_to_name(error));
check(gpio_descriptor_release(sda_descriptor) == ERROR_NONE);
check(gpio_descriptor_release(scl_descriptor) == ERROR_NONE);
return ERROR_RESOURCE;
}
error = i2c_driver_install(dts_config->port, esp_config.mode, 0, 0, 0);
if (error != ESP_OK) {
LOG_E(TAG, "Failed to install driver at port %d: %s", static_cast<int>(dts_config->port), esp_err_to_name(error));
check(gpio_descriptor_release(sda_descriptor) == ERROR_NONE);
check(gpio_descriptor_release(scl_descriptor) == ERROR_NONE);
return ERROR_RESOURCE;
}
auto* data = new Esp32SpiInternal();
auto* data = new(std::nothrow) Esp32I2cInternal(sda_descriptor, scl_descriptor);
if (data == nullptr) {
check(gpio_descriptor_release(sda_descriptor) == ERROR_NONE);
check(gpio_descriptor_release(scl_descriptor) == ERROR_NONE);
return ERROR_OUT_OF_MEMORY;
}
device_set_driver_data(device, data);
return ERROR_NONE;
}
static error_t stop(Device* device) {
ESP_LOGI(TAG, "stop %s", device->name);
auto* driver_data = static_cast<Esp32SpiInternal*>(device_get_driver_data(device));
auto* driver_data = static_cast<Esp32I2cInternal*>(device_get_driver_data(device));
i2c_port_t port = GET_CONFIG(device)->port;
esp_err_t result = i2c_driver_delete(port);
@ -188,6 +229,9 @@ static error_t stop(Device* device) {
return ERROR_RESOURCE;
}
check(gpio_descriptor_release(driver_data->sda_descriptor) == ERROR_NONE);
check(gpio_descriptor_release(driver_data->scl_descriptor) == ERROR_NONE);
device_set_driver_data(device, nullptr);
delete driver_data;
return ERROR_NONE;

View File

@ -3,6 +3,8 @@
#include <driver/i2s_common.h>
#include <tactility/driver.h>
#include <tactility/drivers/gpio_controller.h>
#include <tactility/drivers/gpio_descriptor.h>
#include <tactility/drivers/i2s_controller.h>
#include <tactility/log.h>
@ -14,23 +16,106 @@
#define TAG "esp32_i2s"
static void release_pin(GpioDescriptor** gpio_descriptor) {
if (*gpio_descriptor == nullptr) return;
check(gpio_descriptor_release(*gpio_descriptor) == ERROR_NONE);
*gpio_descriptor = nullptr;
}
static bool acquire_pin_or_set_null(const GpioPinSpec& pin_spec, GpioDescriptor** gpio_descriptor) {
if (pin_spec.gpio_controller == nullptr) {
*gpio_descriptor = nullptr;
return true;
}
*gpio_descriptor = gpio_descriptor_acquire(pin_spec.gpio_controller, pin_spec.pin, GPIO_OWNER_GPIO);
if (*gpio_descriptor == nullptr) {
LOG_E(TAG, "Failed to acquire pin %u", pin_spec.pin);
}
return *gpio_descriptor != nullptr;
}
/**
* Safely acquire the native pin avalue.
* Set to GPIO_NUM_NC if the descriptor is null.
* @param[in] descriptor Pin descriptor to acquire
* @return Native pin number
*/
static gpio_num_t get_native_pin(GpioDescriptor* descriptor) {
if (descriptor != nullptr) {
gpio_num_t pin;
check(gpio_descriptor_get_native_pin_number(descriptor, &pin) == ERROR_NONE);
return pin;
} else {
return GPIO_NUM_NC;
}
}
/**
* Returns true if the given pin is inverted
* @param[in] descriptor Pin descriptor to check, nullable
*/
static bool is_pin_inverted(GpioDescriptor* descriptor) {
if (!descriptor) return false;
gpio_flags_t flags;
check(gpio_descriptor_get_flags(descriptor, &flags) == ERROR_NONE);
return (flags & GPIO_FLAG_ACTIVE_LOW) != 0;
}
struct Esp32I2sInternal {
Mutex mutex {};
i2s_chan_handle_t tx_handle = nullptr;
i2s_chan_handle_t rx_handle = nullptr;
I2sConfig config {};
bool config_set = false;
GpioDescriptor* bclk_descriptor = nullptr;
GpioDescriptor* ws_descriptor = nullptr;
GpioDescriptor* data_out_descriptor = nullptr;
GpioDescriptor* data_in_descriptor = nullptr;
GpioDescriptor* mclk_descriptor = nullptr;
i2s_chan_handle_t tx_handle = nullptr;
i2s_chan_handle_t rx_handle = nullptr;
Esp32I2sInternal() {
mutex_construct(&mutex);
}
~Esp32I2sInternal() {
cleanup_pins();
mutex_destruct(&mutex);
}
void cleanup_pins() {
release_pin(&bclk_descriptor);
release_pin(&ws_descriptor);
release_pin(&data_out_descriptor);
release_pin(&data_in_descriptor);
release_pin(&mclk_descriptor);
}
bool init_pins(Esp32I2sConfig* dts_config) {
check (!ws_descriptor && !bclk_descriptor && !data_out_descriptor && !data_in_descriptor && !mclk_descriptor);
auto& ws_spec = dts_config->pin_ws;
auto& bclk_spec = dts_config->pin_bclk;
auto& data_in_spec = dts_config->pin_data_in;
auto& data_out_spec = dts_config->pin_data_out;
auto& mclk_spec = dts_config->pin_mclk;
bool success = acquire_pin_or_set_null(ws_spec, &ws_descriptor) &&
acquire_pin_or_set_null(bclk_spec, &bclk_descriptor) &&
acquire_pin_or_set_null(data_in_spec, &data_in_descriptor) &&
acquire_pin_or_set_null(data_out_spec, &data_out_descriptor) &&
acquire_pin_or_set_null(mclk_spec, &mclk_descriptor);
if (!success) {
cleanup_pins();
LOG_E(TAG, "Failed to acquire all pins");
return false;
}
return true;
}
};
#define GET_CONFIG(device) ((Esp32I2sConfig*)device->config)
#define GET_CONFIG(device) ((Esp32I2sConfig*)(device)->config)
#define GET_DATA(device) ((Esp32I2sInternal*)device_get_driver_data(device))
#define lock(data) mutex_lock(&data->mutex);
@ -63,10 +148,10 @@ static i2s_data_bit_width_t to_esp32_bits_per_sample(uint8_t bits) {
}
}
static void get_esp32_std_config(const I2sConfig* config, const Esp32I2sConfig* dts_config, i2s_std_config_t* std_cfg) {
static void get_esp32_std_config(Esp32I2sInternal* internal, const I2sConfig* config, i2s_std_config_t* std_cfg) {
std_cfg->clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(config->sample_rate);
std_cfg->slot_cfg = I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(to_esp32_bits_per_sample(config->bits_per_sample), I2S_SLOT_MODE_STEREO);
if (config->communication_format & I2S_FORMAT_STAND_MSB) {
std_cfg->slot_cfg = I2S_STD_MSB_SLOT_DEFAULT_CONFIG(to_esp32_bits_per_sample(config->bits_per_sample), I2S_SLOT_MODE_STEREO);
} else if (config->communication_format & (I2S_FORMAT_STAND_PCM_SHORT | I2S_FORMAT_STAND_PCM_LONG)) {
@ -81,16 +166,28 @@ static void get_esp32_std_config(const I2sConfig* config, const Esp32I2sConfig*
std_cfg->slot_cfg.slot_mask = I2S_STD_SLOT_BOTH;
}
gpio_num_t mclk_pin = get_native_pin(internal->mclk_descriptor);
gpio_num_t bclk_pin = get_native_pin(internal->bclk_descriptor);
gpio_num_t ws_pin = get_native_pin(internal->ws_descriptor);
gpio_num_t data_out_pin = get_native_pin(internal->data_out_descriptor);
gpio_num_t data_in_pin = get_native_pin(internal->data_in_descriptor);
LOG_I(TAG, "Configuring I2S pins: MCLK=%d, BCLK=%d, WS=%d, DATA_OUT=%d, DATA_IN=%d", mclk_pin, bclk_pin, ws_pin, data_out_pin, data_in_pin);
bool mclk_inverted = is_pin_inverted(internal->mclk_descriptor);
bool bclk_inverted = is_pin_inverted(internal->bclk_descriptor);
bool ws_inverted = is_pin_inverted(internal->ws_descriptor);
LOG_I(TAG, "Inverted pins: MCLK=%u, BCLK=%u, WS=%u", mclk_inverted, bclk_inverted, ws_inverted);
std_cfg->gpio_cfg = {
.mclk = (gpio_num_t)dts_config->pin_mclk,
.bclk = (gpio_num_t)dts_config->pin_bclk,
.ws = (gpio_num_t)dts_config->pin_ws,
.dout = (gpio_num_t)dts_config->pin_data_out,
.din = (gpio_num_t)dts_config->pin_data_in,
.mclk = mclk_pin,
.bclk = bclk_pin,
.ws = ws_pin,
.dout = data_out_pin,
.din = data_in_pin,
.invert_flags = {
.mclk_inv = false,
.bclk_inv = false,
.ws_inv = false
.mclk_inv = mclk_inverted,
.bclk_inv = bclk_inverted,
.ws_inv = ws_inverted
}
};
}
@ -129,46 +226,46 @@ static error_t set_config(Device* device, const struct I2sConfig* config) {
return ERROR_INVALID_ARGUMENT;
}
auto* driver_data = GET_DATA(device);
auto* internal = GET_DATA(device);
auto* dts_config = GET_CONFIG(device);
lock(driver_data);
lock(internal);
cleanup_channel_handles(driver_data);
driver_data->config_set = false;
cleanup_channel_handles(internal);
internal->config_set = false;
// Create new channel handles
i2s_chan_config_t chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(dts_config->port, I2S_ROLE_MASTER);
esp_err_t esp_error = i2s_new_channel(&chan_cfg, &driver_data->tx_handle, &driver_data->rx_handle);
esp_err_t esp_error = i2s_new_channel(&chan_cfg, &internal->tx_handle, &internal->rx_handle);
if (esp_error != ESP_OK) {
LOG_E(TAG, "Failed to create I2S channels: %s", esp_err_to_name(esp_error));
unlock(driver_data);
unlock(internal);
return ERROR_RESOURCE;
}
i2s_std_config_t std_cfg;
get_esp32_std_config(config, dts_config, &std_cfg);
i2s_std_config_t std_cfg = {};
get_esp32_std_config(internal, config, &std_cfg);
if (driver_data->tx_handle) {
esp_error = i2s_channel_init_std_mode(driver_data->tx_handle, &std_cfg);
if (esp_error == ESP_OK) esp_error = i2s_channel_enable(driver_data->tx_handle);
if (internal->tx_handle) {
esp_error = i2s_channel_init_std_mode(internal->tx_handle, &std_cfg);
if (esp_error == ESP_OK) esp_error = i2s_channel_enable(internal->tx_handle);
}
if (esp_error == ESP_OK && driver_data->rx_handle) {
esp_error = i2s_channel_init_std_mode(driver_data->rx_handle, &std_cfg);
if (esp_error == ESP_OK) esp_error = i2s_channel_enable(driver_data->rx_handle);
if (esp_error == ESP_OK && internal->rx_handle) {
esp_error = i2s_channel_init_std_mode(internal->rx_handle, &std_cfg);
if (esp_error == ESP_OK) esp_error = i2s_channel_enable(internal->rx_handle);
}
if (esp_error != ESP_OK) {
LOG_E(TAG, "Failed to initialize/enable I2S channels: %s", esp_err_to_name(esp_error));
cleanup_channel_handles(driver_data);
unlock(driver_data);
cleanup_channel_handles(internal);
unlock(internal);
return esp_err_to_error(esp_error);
}
// Update runtime config to reflect current state
memcpy(&driver_data->config, config, sizeof(I2sConfig));
driver_data->config_set = true;
memcpy(&internal->config, config, sizeof(I2sConfig));
internal->config_set = true;
unlock(driver_data);
unlock(internal);
return ERROR_NONE;
}
@ -188,9 +285,17 @@ static error_t get_config(Device* device, struct I2sConfig* config) {
static error_t start(Device* device) {
ESP_LOGI(TAG, "start %s", device->name);
auto* dts_config = GET_CONFIG(device);
auto* data = new(std::nothrow) Esp32I2sInternal();
if (!data) return ERROR_OUT_OF_MEMORY;
if (!data->init_pins(dts_config)) {
LOG_E(TAG, "Failed to init one or more pins");
return ERROR_RESOURCE;
}
device_set_driver_data(device, data);
return ERROR_NONE;

View File

@ -3,6 +3,7 @@
#include <tactility/drivers/esp32_spi.h>
#include <tactility/concurrent/recursive_mutex.h>
#include "tactility/drivers/gpio_descriptor.h"
#include <cstring>
#include <esp_log.h>
#include <new>
@ -19,7 +20,7 @@ struct Esp32SpiInternal {
RecursiveMutex mutex = {};
bool initialized = false;
Esp32SpiInternal() {
explicit Esp32SpiInternal() {
recursive_mutex_construct(&mutex);
}
@ -50,7 +51,7 @@ static error_t unlock(Device* device) {
static error_t start(Device* device) {
ESP_LOGI(TAG, "start %s", device->name);
auto* data = new(std::nothrow) Esp32SpiInternal();
auto* data = new (std::nothrow) Esp32SpiInternal();
if (!data) return ERROR_OUT_OF_MEMORY;
device_set_driver_data(device, data);

View File

@ -53,6 +53,7 @@
#include <driver/i2s_common.h>
#include <driver/i2s_std.h>
#include <driver/gpio.h>
extern "C" {
@ -374,7 +375,11 @@ const esp_elfsym main_symbols[] {
ESP_ELFSYM_EXPORT(esp_netif_get_handle_from_ifkey),
// Locale
ESP_ELFSYM_EXPORT(localeconv),
//i2s_common.h
// driver/gpio.h
ESP_ELFSYM_EXPORT(gpio_config),
ESP_ELFSYM_EXPORT(gpio_get_level),
ESP_ELFSYM_EXPORT(gpio_set_level),
// driver/i2s_common.h
ESP_ELFSYM_EXPORT(i2s_new_channel),
ESP_ELFSYM_EXPORT(i2s_del_channel),
ESP_ELFSYM_EXPORT(i2s_channel_enable),
@ -385,7 +390,7 @@ const esp_elfsym main_symbols[] {
ESP_ELFSYM_EXPORT(i2s_channel_register_event_callback),
ESP_ELFSYM_EXPORT(i2s_channel_preload_data),
ESP_ELFSYM_EXPORT(i2s_channel_tune_rate),
//i2s_std.h
// driver/i2s_std.h
ESP_ELFSYM_EXPORT(i2s_channel_init_std_mode),
ESP_ELFSYM_EXPORT(i2s_channel_reconfig_std_clock),
ESP_ELFSYM_EXPORT(i2s_channel_reconfig_std_slot),

View File

@ -10,24 +10,24 @@ extern "C" {
#include <stdbool.h>
#include <stdint.h>
#define GPIO_OPTIONS_MASK 0x1f
#define GPIO_ACTIVE_HIGH (0 << 0)
#define GPIO_ACTIVE_LOW (1 << 0)
#define GPIO_DIRECTION_INPUT (1 << 1)
#define GPIO_DIRECTION_OUTPUT (1 << 2)
#define GPIO_DIRECTION_INPUT_OUTPUT (GPIO_DIRECTION_INPUT | GPIO_DIRECTION_OUTPUT)
#define GPIO_PULL_UP (0 << 3)
#define GPIO_PULL_DOWN (1 << 4)
#define GPIO_INTERRUPT_BITMASK (0b111 << 5) // 3 bits to hold the values [0, 5]
#define GPIO_INTERRUPT_FROM_OPTIONS(options) (gpio_int_type_t)((options & GPIO_INTERRUPT_BITMASK) >> 5)
#define GPIO_INTERRUPT_TO_OPTIONS(options, interrupt) (options | (interrupt << 5))
#define GPIO_FLAGS_MASK 0x1f
#define GPIO_PIN_NONE -1
#define GPIO_PIN_SPEC_NONE ((struct GpioPinSpec) { NULL, 0, GPIO_FLAG_NONE })
#define GPIO_FLAG_NONE 0
#define GPIO_FLAG_ACTIVE_HIGH (0 << 0)
#define GPIO_FLAG_ACTIVE_LOW (1 << 0)
#define GPIO_FLAG_DIRECTION_INPUT (1 << 1)
#define GPIO_FLAG_DIRECTION_OUTPUT (1 << 2)
#define GPIO_FLAG_DIRECTION_INPUT_OUTPUT (GPIO_FLAG_DIRECTION_INPUT | GPIO_FLAG_DIRECTION_OUTPUT)
#define GPIO_FLAG_PULL_UP (0 << 3)
#define GPIO_FLAG_PULL_DOWN (1 << 4)
#define GPIO_FLAG_INTERRUPT_BITMASK (0b111 << 5) // 3 bits to hold the values [0, 5]
#define GPIO_FLAG_INTERRUPT_FROM_OPTIONS(options) (gpio_int_type_t)((options & GPIO_FLAG_INTERRUPT_BITMASK) >> 5)
#define GPIO_FLAG_INTERRUPT_TO_OPTIONS(options, interrupt) (options | (interrupt << 5))
typedef enum {
GPIO_INTERRUPT_DISABLE = 0,
GPIO_INTERRUPT_POS_EDGE = 1,
@ -35,7 +35,7 @@ typedef enum {
GPIO_INTERRUPT_ANY_EDGE = 3,
GPIO_INTERRUPT_LOW_LEVEL = 4,
GPIO_INTERRUPT_HIGH_LEVEL = 5,
GPIO__MAX,
GPIO_MAX,
} GpioInterruptType;
enum GpioOwnerType {
@ -55,28 +55,19 @@ typedef uint8_t gpio_pin_t;
/** Specifies the configuration flags for a GPIO pin (or set of pins) */
typedef uint16_t gpio_flags_t;
typedef uint8_t gpio_level_t;
/** A configuration for a single GPIO pin */
struct GpioPinConfig {
/**
* Specifies a pin and its properties for a specific GPIO controller.
* Used by the devicetree, drivers and application code to refer to GPIO pins and acquire them via the gpio_controller API.
*/
struct GpioPinSpec {
/** GPIO device controlling the pin */
const struct Device* port;
struct Device* gpio_controller;
/** The pin's number on the device */
gpio_pin_t pin;
/** The pin's configuration flags as specified in devicetree */
gpio_flags_t flags;
};
/**
* Check if the pin is ready to be used.
*
* @param pinConfig the specifications of the pin
* @return true if the pin is ready to be used
*/
static inline bool gpio_is_ready(const struct GpioPinConfig* pinConfig) {
return device_is_ready(pinConfig->port);
}
#ifdef __cplusplus
}
#endif

View File

@ -10,25 +10,9 @@ extern "C" {
#include <tactility/error.h>
struct Device;
struct GpioDescriptor;
struct GpioControllerApi {
struct GpioDescriptor* (*acquire_descriptor)(
struct Device* controller,
gpio_pin_t pin_number,
enum GpioOwnerType owner,
void* owner_context
);
error_t (*release_descriptor)(struct GpioDescriptor* descriptor);
/**
* @brief Gets the pin number associated with a descriptor.
* @param[in] descriptor the pin descriptor
* @param[out] pin pointer to store the pin number
* @return ERROR_NONE if successful
*/
error_t (*get_pin_number)(struct GpioDescriptor* descriptor, gpio_pin_t* pin);
/**
* @brief Sets the logical level of a GPIO pin.
* @param[in,out] descriptor the pin descriptor
@ -48,36 +32,35 @@ struct GpioControllerApi {
/**
* @brief Configures the options for a GPIO pin.
* @param[in,out] descriptor the pin descriptor
* @param[in] options configuration flags (direction, pull-up/down, etc.)
* @param[in] flags configuration flags (direction, pull-up/down, etc.)
* @return ERROR_NONE if successful
*/
error_t (*set_options)(struct GpioDescriptor* descriptor, gpio_flags_t options);
error_t (*set_flags)(struct GpioDescriptor* descriptor, gpio_flags_t flags);
/**
* @brief Gets the configuration options for a GPIO pin.
* @param[in] descriptor the pin descriptor
* @param[out] options pointer to store the configuration flags
* @param[out] flags pointer to store the configuration flags
* @return ERROR_NONE if successful
*/
error_t (*get_options)(struct GpioDescriptor* descriptor, gpio_flags_t* options);
error_t (*get_flags)(struct GpioDescriptor* descriptor, gpio_flags_t* flags);
/**
* @brief Gets the native pin number associated with a descriptor.
* @param[in] descriptor the pin descriptor
* @param[out] pin_number pointer to store the pin number
* @return ERROR_NONE if successful
*/
error_t (*get_native_pin_number)(struct GpioDescriptor* descriptor, void* pin_number);
};
struct GpioDescriptor* acquire_descriptor(
struct GpioDescriptor* gpio_descriptor_acquire(
struct Device* controller,
gpio_pin_t pin_number,
enum GpioOwnerType owner,
void* owner_context
enum GpioOwnerType owner
);
error_t release_descriptor(struct GpioDescriptor* descriptor);
/**
* @brief Gets the number of pins supported by the controller.
* @param[in] device the GPIO controller device
* @param[out] count pointer to store the number of pins
* @return ERROR_NONE if successful
*/
error_t gpio_controller_get_pin_count(struct Device* device, uint32_t* count);
error_t gpio_descriptor_release(struct GpioDescriptor* descriptor);
/**
* @brief Gets the pin number associated with a descriptor.
@ -85,7 +68,15 @@ error_t gpio_controller_get_pin_count(struct Device* device, uint32_t* count);
* @param[out] pin pointer to store the pin number
* @return ERROR_NONE if successful
*/
error_t gpio_descriptor_pin_number(struct GpioDescriptor* descriptor, gpio_pin_t* pin);
error_t gpio_descriptor_get_pin_number(struct GpioDescriptor* descriptor, gpio_pin_t* pin);
/**
* @brief Gets the pin owner type associated with a descriptor.
* @param[in] descriptor the pin descriptor
* @param[out] owner_type pointer to output owner type
* @return ERROR_NONE if successful
*/
error_t gpio_descriptor_get_owner_type(struct GpioDescriptor* descriptor, enum GpioOwnerType* owner_type);
/**
* @brief Sets the logical level of a GPIO pin.
@ -106,26 +97,42 @@ error_t gpio_descriptor_get_level(struct GpioDescriptor* descriptor, bool* high)
/**
* @brief Configures the options for a GPIO pin.
* @param[in] descriptor the pin descriptor
* @param[in] options configuration flags (direction, pull-up/down, etc.)
* @param[in] flags configuration flags (direction, pull-up/down, etc.)
* @return ERROR_NONE if successful
*/
error_t gpio_descriptor_set_options(struct GpioDescriptor* descriptor, gpio_flags_t options);
error_t gpio_descriptor_set_flags(struct GpioDescriptor* descriptor, gpio_flags_t flags);
/**
* @brief Gets the configuration options for a GPIO pin.
* @param[in] descriptor the pin descriptor
* @param[out] options pointer to store the configuration flags
* @param[out] flags pointer to store the configuration flags
* @return ERROR_NONE if successful
*/
error_t gpio_descriptor_get_options(struct GpioDescriptor* descriptor, gpio_flags_t* options);
error_t gpio_descriptor_get_flags(struct GpioDescriptor* descriptor, gpio_flags_t* flags);
/**
* @brief Gets the native pin number associated with a descriptor.
* @param[in] descriptor the pin descriptor
* @param[out] pin_number pointer to store the pin number
* @return ERROR_NONE if successful
*/
error_t gpio_descriptor_get_native_pin_number(struct GpioDescriptor* descriptor, void* pin_number);
/**
* @brief Gets the number of pins supported by the controller.
* @param[in] device the GPIO controller device
* @param[out] count pointer to store the number of pins
* @return ERROR_NONE if successful
*/
error_t gpio_controller_get_pin_count(struct Device* device, uint32_t* count);
/**
* @brief Initializes GPIO descriptors for a controller.
* @param[in,out] device the GPIO controller device
* @param[in] owner_data pointer to store in the descriptor's owner_data field
* @param[in] controller_context pointer to store in the controller's internal data
* @return ERROR_NONE if successful
*/
error_t gpio_controller_init_descriptors(struct Device* device, uint32_t pin_count, void* owner_data);
error_t gpio_controller_init_descriptors(struct Device* device, uint32_t pin_count, void* controller_context);
/**
* @brief Deinitializes GPIO descriptors for a controller.

View File

@ -2,6 +2,8 @@
#include "gpio.h"
struct Device;
struct GpioDescriptor {
/** @brief The controller that owns this pin */
struct Device* controller;
@ -9,12 +11,6 @@ struct GpioDescriptor {
gpio_pin_t pin;
/** @brief Current owner */
enum GpioOwnerType owner_type;
/** @brief Owner identity for validation */
void* owner_context;
/** @brief Pin level */
gpio_level_t level;
/** @brief Pin configuration flags */
gpio_flags_t flags;
/** @brief Implementation-specific context (e.g. from esp32 controller internally) */
void* controller_context;
};

View File

@ -6,22 +6,49 @@
#include <tactility/concurrent/mutex.h>
#include <tactility/drivers/gpio_descriptor.h>
#define GPIO_INTERNAL_API(driver) ((struct GpioControllerApi*)driver->api)
#include <cstdlib>
#include <new>
#define GPIO_INTERNAL_API(driver) ((struct GpioControllerApi*)(driver)->api)
extern "C" {
struct GpioControllerData {
struct Mutex mutex;
struct Mutex mutex {};
uint32_t pin_count;
struct GpioDescriptor* descriptors;
struct GpioDescriptor* descriptors = nullptr;
explicit GpioControllerData(uint32_t pin_count) : pin_count(pin_count) {
mutex_construct(&mutex);
}
error_t init_descriptors(Device* device, void* controller_context) {
descriptors = (struct GpioDescriptor*)calloc(pin_count, sizeof(struct GpioDescriptor));
if (!descriptors) return ERROR_OUT_OF_MEMORY;
for (uint32_t i = 0; i < pin_count; ++i) {
descriptors[i].controller = device;
descriptors[i].pin = (gpio_pin_t)i;
descriptors[i].owner_type = GPIO_OWNER_NONE;
descriptors[i].controller_context = controller_context;
}
return ERROR_NONE;
}
~GpioControllerData() {
if (descriptors != nullptr) {
free(descriptors);
}
mutex_destruct(&mutex);
}
};
struct GpioDescriptor* acquire_descriptor(
struct GpioDescriptor* gpio_descriptor_acquire(
struct Device* controller,
gpio_pin_t pin_number,
enum GpioOwnerType owner,
void* owner_context
enum GpioOwnerType owner
) {
check(owner != GPIO_OWNER_NONE);
auto* data = (struct GpioControllerData*)device_get_driver_data(controller);
mutex_lock(&data->mutex);
@ -37,15 +64,13 @@ struct GpioDescriptor* acquire_descriptor(
}
desc->owner_type = owner;
desc->owner_context = owner_context;
mutex_unlock(&data->mutex);
return desc;
}
error_t release_descriptor(struct GpioDescriptor* descriptor) {
error_t gpio_descriptor_release(struct GpioDescriptor* descriptor) {
descriptor->owner_type = GPIO_OWNER_NONE;
descriptor->owner_context = nullptr;
return ERROR_NONE;
}
@ -56,37 +81,22 @@ error_t gpio_controller_get_pin_count(struct Device* device, uint32_t* count) {
}
error_t gpio_controller_init_descriptors(struct Device* device, uint32_t pin_count, void* controller_context) {
auto* data = (struct GpioControllerData*)malloc(sizeof(struct GpioControllerData));
auto* data = new(std::nothrow) GpioControllerData(pin_count);
if (!data) return ERROR_OUT_OF_MEMORY;
data->pin_count = pin_count;
data->descriptors = (struct GpioDescriptor*)calloc(pin_count, sizeof(struct GpioDescriptor));
if (!data->descriptors) {
free(data);
if (data->init_descriptors(device, controller_context) != ERROR_NONE) {
delete data;
return ERROR_OUT_OF_MEMORY;
}
for (uint32_t i = 0; i < pin_count; ++i) {
data->descriptors[i].controller = device;
data->descriptors[i].pin = (gpio_pin_t)i;
data->descriptors[i].owner_type = GPIO_OWNER_NONE;
data->descriptors[i].controller_context = controller_context;
}
mutex_construct(&data->mutex);
device_set_driver_data(device, data);
return ERROR_NONE;
}
error_t gpio_controller_deinit_descriptors(struct Device* device) {
auto* data = (struct GpioControllerData*)device_get_driver_data(device);
free(data->descriptors);
mutex_destruct(&data->mutex);
free(data);
auto* data = static_cast<struct GpioControllerData*>(device_get_driver_data(device));
delete data;
device_set_driver_data(device, nullptr);
return ERROR_NONE;
}
@ -100,19 +110,29 @@ error_t gpio_descriptor_get_level(struct GpioDescriptor* descriptor, bool* high)
return GPIO_INTERNAL_API(driver)->get_level(descriptor, high);
}
error_t gpio_descriptor_set_options(struct GpioDescriptor* descriptor, gpio_flags_t options) {
error_t gpio_descriptor_set_flags(struct GpioDescriptor* descriptor, gpio_flags_t flags) {
const auto* driver = device_get_driver(descriptor->controller);
return GPIO_INTERNAL_API(driver)->set_options(descriptor, options);
return GPIO_INTERNAL_API(driver)->set_flags(descriptor, flags);
}
error_t gpio_descriptor_get_options(struct GpioDescriptor* descriptor, gpio_flags_t* options) {
error_t gpio_descriptor_get_flags(struct GpioDescriptor* descriptor, gpio_flags_t* flags) {
const auto* driver = device_get_driver(descriptor->controller);
return GPIO_INTERNAL_API(driver)->get_options(descriptor, options);
return GPIO_INTERNAL_API(driver)->get_flags(descriptor, flags);
}
error_t gpio_descriptor_get_pin_number(struct GpioDescriptor* descriptor, gpio_pin_t* pin) {
*pin = descriptor->pin;
return ERROR_NONE;
}
error_t gpio_descriptor_get_native_pin_number(struct GpioDescriptor* descriptor, void* pin_number) {
const auto* driver = device_get_driver(descriptor->controller);
return GPIO_INTERNAL_API(driver)->get_pin_number(descriptor, pin);
return GPIO_INTERNAL_API(driver)->get_native_pin_number(descriptor, pin_number);
}
error_t gpio_descriptor_get_owner_type(struct GpioDescriptor* descriptor, GpioOwnerType* owner_type) {
*owner_type = descriptor->owner_type;
return ERROR_NONE;
}
const struct DeviceType GPIO_CONTROLLER_TYPE {

View File

@ -59,10 +59,15 @@ const struct ModuleSymbol KERNEL_SYMBOLS[] = {
DEFINE_MODULE_SYMBOL(driver_find_compatible),
DEFINE_MODULE_SYMBOL(driver_get_device_type),
// drivers/gpio_controller
DEFINE_MODULE_SYMBOL(gpio_descriptor_acquire),
DEFINE_MODULE_SYMBOL(gpio_descriptor_release),
DEFINE_MODULE_SYMBOL(gpio_descriptor_set_level),
DEFINE_MODULE_SYMBOL(gpio_descriptor_get_level),
DEFINE_MODULE_SYMBOL(gpio_descriptor_set_options),
DEFINE_MODULE_SYMBOL(gpio_descriptor_get_options),
DEFINE_MODULE_SYMBOL(gpio_descriptor_set_flags),
DEFINE_MODULE_SYMBOL(gpio_descriptor_get_flags),
DEFINE_MODULE_SYMBOL(gpio_descriptor_get_native_pin_number),
DEFINE_MODULE_SYMBOL(gpio_descriptor_get_pin_number),
DEFINE_MODULE_SYMBOL(gpio_descriptor_get_owner_type),
DEFINE_MODULE_SYMBOL(gpio_controller_get_pin_count),
DEFINE_MODULE_SYMBOL(gpio_controller_init_descriptors),
DEFINE_MODULE_SYMBOL(gpio_controller_deinit_descriptors),