Skip to content

Commit 3c68eb7

Browse files
committed
Initial Peripheral Manager Implementation
1 parent bb5a840 commit 3c68eb7

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ set(CORE_SRCS
3535
cores/esp32/esp32-hal-ledc.c
3636
cores/esp32/esp32-hal-matrix.c
3737
cores/esp32/esp32-hal-misc.c
38+
cores/esp32/esp32-hal-periman.c
3839
cores/esp32/esp32-hal-psram.c
3940
cores/esp32/esp32-hal-rgb-led.c
4041
cores/esp32/esp32-hal-sigmadelta.c

cores/esp32/esp32-hal-periman.c

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2019-2023 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "esp32-hal-log.h"
8+
#include "esp32-hal-periman.h"
9+
#include "soc/soc_caps.h"
10+
11+
typedef struct {
12+
peripheral_bus_type_t type;
13+
void * bus;
14+
} peripheral_pin_item_t;
15+
16+
static peripheral_bus_deinit_cb_t deinit_functions[ESP32_BUS_TYPE_MAX];
17+
static peripheral_pin_item_t pins[SOC_GPIO_PIN_COUNT];
18+
19+
bool perimanSetPinBus(uint8_t pin, peripheral_bus_type_t type, void * bus){
20+
peripheral_bus_type_t otype = ESP32_BUS_TYPE_INIT;
21+
void * obus = NULL;
22+
if(pin >= SOC_GPIO_PIN_COUNT){
23+
log_e("Invalid pin: %u", pin);
24+
return false;
25+
}
26+
if(type >= ESP32_BUS_TYPE_MAX){
27+
log_e("Invalid type: %u", (unsigned int)type);
28+
return false;
29+
}
30+
if(type > ESP32_BUS_TYPE_GPIO && bus == NULL){
31+
log_e("Bus is NULL");
32+
return false;
33+
}
34+
otype = pins[pin].type;
35+
obus = pins[pin].bus;
36+
if(type == otype && bus == obus){
37+
log_i("Bus already set");
38+
return true;
39+
}
40+
if(obus != NULL){
41+
if(deinit_functions[otype] == NULL){
42+
log_e("Bus does not have deinit function set");
43+
return false;
44+
}
45+
if(!deinit_functions[otype](obus)){
46+
log_e("Previous bus failed to deinit");
47+
return false;
48+
}
49+
}
50+
pins[pin].type = type;
51+
pins[pin].bus = bus;
52+
return true;
53+
}
54+
55+
void * perimanGetPinBus(uint8_t pin, peripheral_bus_type_t type){
56+
if(pin >= SOC_GPIO_PIN_COUNT){
57+
log_e("Invalid pin: %u", pin);
58+
return NULL;
59+
}
60+
if(type >= ESP32_BUS_TYPE_MAX){
61+
log_e("Invalid type: %u", (unsigned int)type);
62+
return NULL;
63+
}
64+
if(pins[pin].type == type){
65+
return pins[pin].bus;
66+
}
67+
return NULL;
68+
}
69+
70+
peripheral_bus_type_t perimanGetPinBusType(uint8_t pin){
71+
if(pin >= SOC_GPIO_PIN_COUNT){
72+
log_e("Invalid pin: %u", pin);
73+
return ESP32_BUS_TYPE_MAX;
74+
}
75+
return pins[pin].type;
76+
}
77+
78+
bool perimanSetBusDeinit(peripheral_bus_type_t type, peripheral_bus_deinit_cb_t cb){
79+
if(type >= ESP32_BUS_TYPE_MAX){
80+
log_e("Invalid type: %u", (unsigned int)type);
81+
return false;
82+
}
83+
if(cb == NULL){
84+
log_e("Callback is NULL");
85+
return false;
86+
}
87+
deinit_functions[type] = cb;
88+
return true;
89+
}
90+
91+

cores/esp32/esp32-hal-periman.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2019-2023 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifdef __cplusplus
8+
extern "C"
9+
{
10+
#endif
11+
12+
#include <stdint.h>
13+
#include <stdbool.h>
14+
#include <stddef.h>
15+
16+
typedef enum {
17+
ESP32_BUS_TYPE_INIT, // IO has not been attached to a bus yet
18+
ESP32_BUS_TYPE_GPIO, // IO is used as GPIO
19+
ESP32_BUS_TYPE_SIGMADELTA, // IO is used as SigmeDelta output
20+
ESP32_BUS_TYPE_ADC_ONESHOT, // IO is used as ADC OneShot input
21+
ESP32_BUS_TYPE_ADC_CONT, // IO is used as ADC continuous input
22+
ESP32_BUS_TYPE_DAC_ONESHOT, // IO is used as DAC OneShot output
23+
ESP32_BUS_TYPE_DAC_CONT, // IO is used as DAC continuous output
24+
ESP32_BUS_TYPE_DAC_COSINE, // IO is used as DAC cosine output
25+
ESP32_BUS_TYPE_RMT_TX, // IO is used as RMT output
26+
ESP32_BUS_TYPE_RMT_RX, // IO is used as RMT input
27+
ESP32_BUS_TYPE_I2S_STD, // IO is used as I2S STD pin
28+
ESP32_BUS_TYPE_I2S_PDM, // IO is used as I2S PDM pin
29+
ESP32_BUS_TYPE_I2S_TDM, // IO is used as I2S TDM pin
30+
ESP32_BUS_TYPE_UART, // IO is used as UART pin
31+
ESP32_BUS_TYPE_I2C_MASTER, // IO is used as I2C master pin
32+
ESP32_BUS_TYPE_I2C_SLAVE, // IO is used as I2C slave pin
33+
ESP32_BUS_TYPE_SPI_MASTER, // IO is used as SPI master pin
34+
ESP32_BUS_TYPE_MAX
35+
} peripheral_bus_type_t;
36+
37+
typedef bool (*peripheral_bus_deinit_cb_t)(void * bus);
38+
39+
// Sets the bus type and bus handle for given pin.
40+
bool perimanSetPinBus(uint8_t pin, peripheral_bus_type_t type, void * bus);
41+
42+
// Returns handle of the bus for the given pin if type of bus matches. NULL otherwise
43+
void * perimanGetPinBus(uint8_t pin, peripheral_bus_type_t type);
44+
45+
// Returns the type of the bus for the given pin if attached. ESP32_BUS_TYPE_MAX otherwise
46+
peripheral_bus_type_t perimanGetPinBusType(uint8_t pin);
47+
48+
// Sets the peripheral destructor callback. Used to destroy bus when pin is assigned another function
49+
bool perimanSetBusDeinit(peripheral_bus_type_t type, peripheral_bus_deinit_cb_t cb);
50+
51+
#ifdef __cplusplus
52+
}
53+
#endif

0 commit comments

Comments
 (0)