Arduino AudioKit HAL
audio_gpio.h
Go to the documentation of this file.
1 /**
2  * @file audio_gpio.h
3  * @author Phil Schatzmann
4  * @brief GPIO related functionality
5  * @date 2021-12-12
6  *
7  * @copyright Copyright (c) 2021
8  *
9  */
10 #pragma once
11 #ifdef ESP32
12 #include "driver/gpio.h"
13 #else
14 #include "audio_types.h"
15 #include "audio_error.h"
16 
17 #define I2S_NUM_0 0
18 #define I2S_NUM_1 1
19 #define I2S_NUM_MAX I2S_NUM_1
20 #define I2C_NUM_0 0
21 #define I2C_NUM_1 1
22 #define GPIO_MODE_OUTPUT 1
23 #define GPIO_MODE_INPUT 0
24 #define BIT64(nr) (1ULL << (nr))
25 #define BIT(nr) (1 << (nr))
26 
27 // Suppress the following macros
28 #define PIN_FUNC_SELECT(a,b)
29 #define WRITE_PERI_REG(a,b)
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 typedef int i2s_port_t;
36 typedef int i2c_port_t;
37 typedef uint32_t TickType_t;
38 
39 typedef enum {
40  GPIO_NUM_NC = -1, /*!< Use to signal not connected to S/W */
41  GPIO_NUM_0 = 0, /*!< GPIO0, input and output */
42  GPIO_NUM_1 = 1, /*!< GPIO1, input and output */
43  GPIO_NUM_2 = 2, /*!< GPIO2, input and output */
44  GPIO_NUM_3 = 3, /*!< GPIO3, input and output */
45  GPIO_NUM_4 = 4, /*!< GPIO4, input and output */
46  GPIO_NUM_5 = 5, /*!< GPIO5, input and output */
47  GPIO_NUM_6 = 6, /*!< GPIO6, input and output */
48  GPIO_NUM_7 = 7, /*!< GPIO7, input and output */
49  GPIO_NUM_8 = 8, /*!< GPIO8, input and output */
50  GPIO_NUM_9 = 9, /*!< GPIO9, input and output */
51  GPIO_NUM_10 = 10, /*!< GPIO10, input and output */
52  GPIO_NUM_11 = 11, /*!< GPIO11, input and output */
53  GPIO_NUM_12 = 12, /*!< GPIO12, input and output */
54  GPIO_NUM_13 = 13, /*!< GPIO13, input and output */
55  GPIO_NUM_14 = 14, /*!< GPIO14, input and output */
56  GPIO_NUM_15 = 15, /*!< GPIO15, input and output */
57  GPIO_NUM_16 = 16, /*!< GPIO16, input and output */
58  GPIO_NUM_17 = 17, /*!< GPIO17, input and output */
59  GPIO_NUM_18 = 18, /*!< GPIO18, input and output */
60  GPIO_NUM_19 = 19, /*!< GPIO19, input and output */
61  GPIO_NUM_20 = 20, /*!< GPIO20, input and output */
62  GPIO_NUM_21 = 21, /*!< GPIO21, input and output */
63  GPIO_NUM_22 = 22, /*!< GPIO22, input and output */
64  GPIO_NUM_23 = 23, /*!< GPIO23, input and output */
65  GPIO_NUM_25 = 25, /*!< GPIO25, input and output */
66  GPIO_NUM_26 = 26, /*!< GPIO26, input and output */
67  GPIO_NUM_27 = 27, /*!< GPIO27, input and output */
68  GPIO_NUM_28 = 28, /*!< GPIO28, input and output */
69  GPIO_NUM_29 = 29, /*!< GPIO29, input and output */
70  GPIO_NUM_30 = 30, /*!< GPIO30, input and output */
71  GPIO_NUM_31 = 31, /*!< GPIO31, input and output */
72  GPIO_NUM_32 = 32, /*!< GPIO32, input and output */
73  GPIO_NUM_33 = 33, /*!< GPIO33, input and output */
74  GPIO_NUM_34 = 34, /*!< GPIO34, input mode only */
75  GPIO_NUM_35 = 35, /*!< GPIO35, input mode only */
76  GPIO_NUM_36 = 36, /*!< GPIO36, input mode only */
77  GPIO_NUM_37 = 37, /*!< GPIO37, input mode only */
78  GPIO_NUM_38 = 38, /*!< GPIO38, input mode only */
79  GPIO_NUM_39 = 39, /*!< GPIO39, input mode only */
80  GPIO_NUM_MAX,
81 /** @endcond */
82 } gpio_num_t;
83 
84 typedef enum{
85  I2C_MODE_SLAVE = 0, /*!< I2C slave mode */
86  I2C_MODE_MASTER, /*!< I2C master mode */
87  I2C_MODE_MAX,
88 } i2c_mode_t;
89 
90 typedef enum {
91  GPIO_INTR_DISABLE = 0, /*!< Disable GPIO interrupt */
92  GPIO_INTR_POSEDGE = 1, /*!< GPIO interrupt type : rising edge */
93  GPIO_INTR_NEGEDGE = 2, /*!< GPIO interrupt type : falling edge */
94  GPIO_INTR_ANYEDGE = 3, /*!< GPIO interrupt type : both rising and falling edge */
95  GPIO_INTR_LOW_LEVEL = 4, /*!< GPIO interrupt type : input low level trigger */
96  GPIO_INTR_HIGH_LEVEL = 5, /*!< GPIO interrupt type : input high level trigger */
97  GPIO_INTR_MAX,
99 
100 
101 /// I2C pins
102 typedef struct{
103  i2c_mode_t mode; /*!< I2C mode */
104  gpio_num_t sda_io_num; /*!< GPIO number for I2C sda signal */
105  gpio_num_t scl_io_num; /*!< GPIO number for I2C scl signal */
106  bool sda_pullup_en; /*!< Internal GPIO pull mode for I2C sda signal*/
107  bool scl_pullup_en; /*!< Internal GPIO pull mode for I2C scl signal*/
108 
109  union {
110  struct {
111  uint32_t clk_speed; /*!< I2C clock frequency for master mode, (no higher than 1MHz for now) */
112  } master; /*!< I2C master config */
113  struct {
114  uint8_t addr_10bit_en; /*!< I2C 10bit address mode enable for slave mode */
115  uint16_t slave_addr; /*!< I2C address for slave mode */
116  uint32_t maximum_speed; /*!< I2C expected clock speed from SCL. */
117  } slave; /*!< I2C slave config */
118  };
119  uint32_t clk_flags; /*!< Bitwise of ``I2C_SCLK_SRC_FLAG_**FOR_DFS**`` for clk source choice*/
120 } i2c_config_t;
121 
122 /// I2S Pins
123 typedef struct {
124  gpio_num_t bck_io_num;
125  gpio_num_t ws_io_num;
126  gpio_num_t data_out_num;
127  gpio_num_t data_in_num;
129 
130 // SPI Configuration
131 typedef struct {
132  gpio_num_t mosi_io_num; ///< GPIO pin for Master Out Slave In (=spi_d) signal, or -1 if not used.
133  gpio_num_t miso_io_num; ///< GPIO pin for Master In Slave Out (=spi_q) signal, or -1 if not used.
134  gpio_num_t sclk_io_num; ///< GPIO pin for SPI Clock signal, or -1 if not used.
135  gpio_num_t quadwp_io_num;
136  gpio_num_t quadhd_io_num;
138 
139 /// SPI device configuration
140 typedef struct {
141  int spics_io_num; ///< CS GPIO pin for this device, or -1 if not used
143 
144 typedef enum {
145  TOUCH_PAD_NUM0 = 0, /*!< Touch pad channel 0 is GPIO4 */
146  TOUCH_PAD_NUM1, /*!< Touch pad channel 0 is GPIO0 */
147  TOUCH_PAD_NUM2, /*!< Touch pad channel 0 is GPIO2 */
148  TOUCH_PAD_NUM3, /*!< Touch pad channel 0 is GPIO15 */
149  TOUCH_PAD_NUM4, /*!< Touch pad channel 0 is GPIO13 */
150  TOUCH_PAD_NUM5, /*!< Touch pad channel 0 is GPIO12 */
151  TOUCH_PAD_NUM6, /*!< Touch pad channel 0 is GPIO14 */
152  TOUCH_PAD_NUM7, /*!< Touch pad channel 0 is GPIO27*/
153  TOUCH_PAD_NUM8, /*!< Touch pad channel 0 is GPIO33*/
154  TOUCH_PAD_NUM9, /*!< Touch pad channel 0 is GPIO32*/
155  TOUCH_PAD_MAX,
156 } touch_pad_t;
157 
158 
159 
160 typedef struct {
161  uint64_t pin_bit_mask; /*!< GPIO pin: set with bit mask, each bit maps to a GPIO */
162  int mode; /*!< GPIO mode: set input/output mode */
163  int pull_up_en; /*!< GPIO pull-up */
164  int pull_down_en; /*!< GPIO pull-down */
165  int intr_type; /*!< GPIO interrupt type */
166 } gpio_config_t;
167 
168 typedef enum {
169  GPIO_PULLUP_ONLY, /*!< Pad pull up */
170  GPIO_PULLDOWN_ONLY, /*!< Pad pull down */
171  GPIO_PULLUP_PULLDOWN, /*!< Pad pull up + pull down*/
172  GPIO_FLOATING, /*!< Pad floating */
174 
175 
176 esp_err_t gpio_config(const gpio_config_t *pGPIOConfig);
177 esp_err_t gpio_pad_select_gpio(gpio_num_t);
178 esp_err_t gpio_set_direction(gpio_num_t, int);
179 esp_err_t gpio_set_level(gpio_num_t, int);
180 esp_err_t vTaskDelay(TickType_t delay);
181 
182 #ifdef __cplusplus
183 }
184 #endif
185 
186 
187 #endif
int spics_io_num
CS GPIO pin for this device, or -1 if not used.
Definition: audio_gpio.h:141
gpio_pull_mode_t
Definition: audio_gpio.h:168
@ GPIO_PULLDOWN_ONLY
Definition: audio_gpio.h:170
@ GPIO_FLOATING
Definition: audio_gpio.h:172
@ GPIO_PULLUP_ONLY
Definition: audio_gpio.h:169
@ GPIO_PULLUP_PULLDOWN
Definition: audio_gpio.h:171
uint64_t pin_bit_mask
Definition: audio_gpio.h:161
gpio_num_t sclk_io_num
GPIO pin for SPI Clock signal, or -1 if not used.
Definition: audio_gpio.h:134
gpio_num_t miso_io_num
GPIO pin for Master In Slave Out (=spi_q) signal, or -1 if not used.
Definition: audio_gpio.h:133
uint32_t clk_flags
Definition: audio_gpio.h:119
gpio_num_t sda_io_num
Definition: audio_gpio.h:104
bool sda_pullup_en
Definition: audio_gpio.h:106
bool scl_pullup_en
Definition: audio_gpio.h:107
i2c_mode_t
Definition: audio_gpio.h:84
@ I2C_MODE_SLAVE
Definition: audio_gpio.h:85
@ I2C_MODE_MASTER
Definition: audio_gpio.h:86
gpio_num_t mosi_io_num
GPIO pin for Master Out Slave In (=spi_d) signal, or -1 if not used.
Definition: audio_gpio.h:132
gpio_num_t
Definition: audio_gpio.h:39
@ GPIO_NUM_16
Definition: audio_gpio.h:57
@ GPIO_NUM_5
Definition: audio_gpio.h:46
@ GPIO_NUM_15
Definition: audio_gpio.h:56
@ GPIO_NUM_21
Definition: audio_gpio.h:62
@ GPIO_NUM_30
Definition: audio_gpio.h:70
@ GPIO_NUM_8
Definition: audio_gpio.h:49
@ GPIO_NUM_NC
Definition: audio_gpio.h:40
@ GPIO_NUM_33
Definition: audio_gpio.h:73
@ GPIO_NUM_11
Definition: audio_gpio.h:52
@ GPIO_NUM_37
Definition: audio_gpio.h:77
@ GPIO_NUM_7
Definition: audio_gpio.h:48
@ GPIO_NUM_4
Definition: audio_gpio.h:45
@ GPIO_NUM_38
Definition: audio_gpio.h:78
@ GPIO_NUM_13
Definition: audio_gpio.h:54
@ GPIO_NUM_6
Definition: audio_gpio.h:47
@ GPIO_NUM_19
Definition: audio_gpio.h:60
@ GPIO_NUM_29
Definition: audio_gpio.h:69
@ GPIO_NUM_18
Definition: audio_gpio.h:59
@ GPIO_NUM_28
Definition: audio_gpio.h:68
@ GPIO_NUM_17
Definition: audio_gpio.h:58
@ GPIO_NUM_25
Definition: audio_gpio.h:65
@ GPIO_NUM_22
Definition: audio_gpio.h:63
@ GPIO_NUM_2
Definition: audio_gpio.h:43
@ GPIO_NUM_3
Definition: audio_gpio.h:44
@ GPIO_NUM_23
Definition: audio_gpio.h:64
@ GPIO_NUM_26
Definition: audio_gpio.h:66
@ GPIO_NUM_10
Definition: audio_gpio.h:51
@ GPIO_NUM_36
Definition: audio_gpio.h:76
@ GPIO_NUM_0
Definition: audio_gpio.h:41
@ GPIO_NUM_39
Definition: audio_gpio.h:79
@ GPIO_NUM_1
Definition: audio_gpio.h:42
@ GPIO_NUM_12
Definition: audio_gpio.h:53
@ GPIO_NUM_20
Definition: audio_gpio.h:61
@ GPIO_NUM_34
Definition: audio_gpio.h:74
@ GPIO_NUM_14
Definition: audio_gpio.h:55
@ GPIO_NUM_31
Definition: audio_gpio.h:71
@ GPIO_NUM_32
Definition: audio_gpio.h:72
@ GPIO_NUM_35
Definition: audio_gpio.h:75
@ GPIO_NUM_27
Definition: audio_gpio.h:67
@ GPIO_NUM_9
Definition: audio_gpio.h:50
touch_pad_t
Definition: audio_gpio.h:144
@ TOUCH_PAD_NUM5
Definition: audio_gpio.h:150
@ TOUCH_PAD_NUM9
Definition: audio_gpio.h:154
@ TOUCH_PAD_NUM0
Definition: audio_gpio.h:145
@ TOUCH_PAD_NUM7
Definition: audio_gpio.h:152
@ TOUCH_PAD_NUM6
Definition: audio_gpio.h:151
@ TOUCH_PAD_NUM8
Definition: audio_gpio.h:153
@ TOUCH_PAD_NUM4
Definition: audio_gpio.h:149
@ TOUCH_PAD_NUM2
Definition: audio_gpio.h:147
@ TOUCH_PAD_NUM3
Definition: audio_gpio.h:148
@ TOUCH_PAD_NUM1
Definition: audio_gpio.h:146
gpio_num_t scl_io_num
Definition: audio_gpio.h:105
gpio_int_type_t
Definition: audio_gpio.h:90
@ GPIO_INTR_LOW_LEVEL
Definition: audio_gpio.h:95
@ GPIO_INTR_NEGEDGE
Definition: audio_gpio.h:93
@ GPIO_INTR_DISABLE
Definition: audio_gpio.h:91
@ GPIO_INTR_HIGH_LEVEL
Definition: audio_gpio.h:96
@ GPIO_INTR_POSEDGE
Definition: audio_gpio.h:92
@ GPIO_INTR_ANYEDGE
Definition: audio_gpio.h:94
i2c_mode_t mode
Definition: audio_gpio.h:103
I2C pins.
Definition: audio_gpio.h:102
SPI device configuration.
Definition: audio_gpio.h:140
Platform independent audio types.