Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
Esp8266Gpio.hxx
Go to the documentation of this file.
1
35#ifndef _DRIVERS_ESP8266_ESP8266GPIO_HXX_
36#define _DRIVERS_ESP8266_ESP8266GPIO_HXX_
37
38#include "os/Gpio.hxx"
39
40extern "C" {
41#include <gpio.h>
42#include <eagle_soc.h>
43
48
54void gpio16_output_set(uint8 value);
55
60
66uint8 gpio16_input_get(void);
67
68
69}
70
72constexpr uint32_t pinmux_to_gpio_arr[] = {
73 PERIPHS_IO_MUX_GPIO0_U, // 0
74 PERIPHS_IO_MUX_U0TXD_U, // 1
75 PERIPHS_IO_MUX_GPIO2_U, // 2
76 PERIPHS_IO_MUX_U0RXD_U, // 3
77 PERIPHS_IO_MUX_GPIO4_U, // 4
78 PERIPHS_IO_MUX_GPIO5_U, // 5
79 PERIPHS_IO_MUX_SD_CLK_U, // 6
80 PERIPHS_IO_MUX_SD_DATA0_U, // 7
81 PERIPHS_IO_MUX_SD_DATA1_U, // 8
82 PERIPHS_IO_MUX_SD_DATA2_U, // 9
83 PERIPHS_IO_MUX_SD_DATA3_U, // 10
84 PERIPHS_IO_MUX_SD_CMD_U, // 11
85 PERIPHS_IO_MUX_MTDI_U, // 12
86 PERIPHS_IO_MUX_MTCK_U, // 13
87 PERIPHS_IO_MUX_MTMS_U, // 14
88 PERIPHS_IO_MUX_MTDO_U, // 15
89};
90
96constexpr uint32_t gpio_num_to_pinmux_reg(int gpio_pin_num)
97{
98 return pinmux_to_gpio_arr[gpio_pin_num];
99}
100
108template <int PIN_NUM, uint32_t MUXREG, uint32_t FUNC_GPIO>
110{
111public:
113 static constexpr const uint32_t PIN = PIN_NUM;
115 static constexpr const uint32_t PIN_BIT = (1 << PIN_NUM);
117 static constexpr const uint32_t PIN_MUX_REG = MUXREG;
119 static constexpr const uint32_t PIN_MUX_FUNC_GPIO = FUNC_GPIO;
120
122 static void set_gpio()
123 {
124 PIN_FUNC_SELECT(PIN_MUX_REG, PIN_MUX_FUNC_GPIO);
125 }
126
128 static void set_output()
129 {
130 GPIO_REG_WRITE(GPIO_ENABLE_W1TS_ADDRESS, PIN_BIT);
131 }
132
134 static void set_input()
135 {
136 GPIO_REG_WRITE(GPIO_ENABLE_W1TC_ADDRESS, PIN_BIT);
137 }
138
140 static void set_pullup_on()
141 {
142 CLEAR_PERI_REG_MASK(PIN_MUX_REG, PERIPHS_IO_MUX_PULLUP2 | PERIPHS_IO_MUX_SLEEP_PULLUP | PERIPHS_IO_MUX_SLEEP_PULLUP2);
143 SET_PERI_REG_MASK(PIN_MUX_REG, PERIPHS_IO_MUX_PULLUP);
144 }
145
147 static void set_pullup_off()
148 {
149 CLEAR_PERI_REG_MASK(
150 PIN_MUX_REG, PERIPHS_IO_MUX_PULLUP | PERIPHS_IO_MUX_PULLUP2);
151 }
152
154 static void set_pulldown_on()
155 {
156 CLEAR_PERI_REG_MASK(PIN_MUX_REG, PERIPHS_IO_MUX_PULLUP | PERIPHS_IO_MUX_SLEEP_PULLUP | PERIPHS_IO_MUX_SLEEP_PULLUP2);
157 SET_PERI_REG_MASK(PIN_MUX_REG, PERIPHS_IO_MUX_PULLUP2);
158 }
159
161 static void set_pulldown_off()
162 {
163 CLEAR_PERI_REG_MASK(
164 PIN_MUX_REG, PERIPHS_IO_MUX_PULLUP | PERIPHS_IO_MUX_PULLUP2);
165 }
166
168 static void set_on()
169 {
170 GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, PIN_BIT);
171 }
172
174 static void set_off()
175 {
176 GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, PIN_BIT);
177 }
178
180 static bool get()
181 {
182 return (GPIO_REG_READ(GPIO_IN_ADDRESS) & PIN_BIT) != 0;
183 }
184
186 static void set(bool value)
187 {
188 if (value)
189 {
190 set_on();
191 }
192 else
193 {
194 set_off();
195 }
196 }
197
199 static void toggle()
200 {
201 set(!get());
202 }
203
205 static bool is_output()
206 {
207 return GPIO_REG_READ(GPIO_ENABLE_ADDRESS) & PIN_BIT;
208 }
209};
210
211template <class Base, bool SAFE_VALUE, bool INVERT = false>
212struct GpioOutputPin : public Base
213{
214public:
216 static void hw_init()
217 {
218 Base::set(SAFE_VALUE);
219 Base::set_output();
220 Base::set_gpio();
221 Base::set(SAFE_VALUE);
222 }
224 static void hw_set_to_safe()
225 {
226 Base::set(SAFE_VALUE);
227 }
230 static void set(bool value)
231 {
232 if (INVERT)
233 {
234 Base::set(!value);
235 }
236 else
237 {
238 Base::set(value);
239 }
240 }
241};
242
247template <class Base>
248struct GpioPullOutPin : public Base
249{
250public:
252 static void hw_init()
253 {
254 Base::set_input();
255 Base::set_gpio();
256 Base::set_pullup_on();
257 }
259 static void hw_set_to_safe()
260 {
261 Base::set_pullup_on();
262 }
265 static void set(bool value)
266 {
267 if (value) {
268 Base::set_pullup_on();
269 } else {
270 Base::set_pulldown_on();
271 }
272 }
274 static void set_on() {
275 Base::set_pullup_on();
276 }
278 static void set_off() {
279 Base::set_pulldown_on();
280 }
281};
282
286template <class Defs>
287struct GpioOutputSafeLow : public GpioOutputPin<Defs, false>
288{
289};
290
295template <class Defs>
296struct GpioOutputSafeLowInvert : public GpioOutputPin<Defs, false, true>
297{
298};
299
303template <class Defs>
304struct GpioOutputSafeHigh : public GpioOutputPin<Defs, true>
305{
306};
307
312template <class Defs>
313struct GpioOutputSafeHighInvert : public GpioOutputPin<Defs, true, true>
314{
315};
316
321template <class Base, bool PUEN> struct GpioInputPar : public Base
322{
323public:
325 static void hw_init()
326 {
327 Base::set_input();
328 if (PUEN)
329 {
330 Base::set_pullup_on();
331 }
332 else
333 {
334 Base::set_pullup_off();
335 }
336 Base::set_gpio();
337 }
339 static void hw_set_to_safe()
340 {
341 hw_init();
342 }
343};
344
348template <class Defs> struct GpioInputNP : public GpioInputPar<Defs, false>
349{
350};
351
355template <class Defs> struct GpioInputPU : public GpioInputPar<Defs, true>
356{
357};
358
375#define GPIO_PIN(NAME, BaseClass, NUM) \
376 typedef BaseClass<Esp8266StaticGpio<NUM, gpio_num_to_pinmux_reg(NUM), \
377 FUNC_GPIO##NUM>> NAME##_Pin
378
379#endif // _DRIVERS_ESP8266_ESP8266GPIO_HXX_
uint8 gpio16_input_get(void)
Sample the value of GPIO16 input.
constexpr uint32_t gpio_num_to_pinmux_reg(int gpio_pin_num)
Constexpr function that takes a GPIO number and converts it to the pinmux register address.
void gpio16_output_set(uint8 value)
Set GPIO16 output level.
constexpr uint32_t pinmux_to_gpio_arr[]
Static map from gpio number to pinmux register address.
void gpio16_input_conf(void)
Enable GPIO16 pin intput.
void gpio16_output_conf(void)
Enable GPIO16 pin output.
Defines a GPIO output pin.
static constexpr const uint32_t PIN_BIT
Bit in a 16-bit register that is assigned to this pin.
static void toggle()
Toggles output pin value.
static constexpr const uint32_t PIN
Number of pin (0..15).
static constexpr const uint32_t PIN_MUX_FUNC_GPIO
Number of the GPIO function in this specific pinmux register.
static void set_pulldown_off()
Turns off pulldown (does not work).
static bool is_output()
static void set_off()
Sets output to LOW.
static constexpr const uint32_t PIN_MUX_REG
Address of the pinmux register that belongs to this pin.
static void set_gpio()
Chooses GPIO on the pinmux.
static bool get()
static void set_input()
Sets pin to input.
static void set_on()
Sets output to HIGH.
static void set_output()
Sets pin to output.
static void set_pullup_off()
Turns off pullup.
static void set_pullup_on()
Turns on pullup.
static void set(bool value)
Set output pin level.
static void set_pulldown_on()
Turns on pulldown (does not work).
Defines a GPIO input pin.
Defines a GPIO input pin with pull-up.
Parametric GPIO input class.
static void hw_init()
Initializes the hardware pin.
static void hw_set_to_safe()
Sets the hardware pin to a safe state.
Parametric GPIO output class.
static void hw_init()
Initializes the hardware pin.
static void set(bool value)
Sets the output pinm.
static void hw_set_to_safe()
Sets the hardware pin to a safe value.
Defines a GPIO output pin, initialized to be an output pin with high level.
Defines a GPIO output pin, initialized to be an output pin with high level.
Defines a GPIO output pin, initialized to be an output pin with low level.
Defines a GPIO output pin, initialized to be an output pin with low level.
Output pin that switches between high and low state by turning on pullup and pulldowns.
static void set_on()
Sets the output pin to HIGH.
static void set_off()
Sets the output pin to LOW.
static void hw_init()
Initializes the hardware pin.
static void hw_set_to_safe()
Sets the hardware pin to a safe state.
static void set(bool value)
Sets the value of an output pin to a specific state;.