diff options
-rw-r--r-- | include/esp8266/driver/gpio.h | 12 | ||||
-rw-r--r-- | src/arch/esp8266/driver/gpio.cc | 128 |
2 files changed, 137 insertions, 3 deletions
diff --git a/include/esp8266/driver/gpio.h b/include/esp8266/driver/gpio.h index 177e7c5..3db5d9d 100644 --- a/include/esp8266/driver/gpio.h +++ b/include/esp8266/driver/gpio.h @@ -7,10 +7,22 @@ class GPIO { public: GPIO () {} + + enum Pin : unsigned char { + d3 = 0, tx, d4, rx, d2, d1, + d6 = 12, d7, d5, d8, + d0 = 16 + }; + void setup(); void led_on(unsigned char id); void led_off(unsigned char id); void led_toggle(unsigned char id); + void input(unsigned char const pin); + void input(unsigned char const pin, bool pullup); + void output(unsigned char const pin); + unsigned char read(unsigned char const pin); + void write(unsigned char const pin, unsigned char value); }; extern GPIO gpio; diff --git a/src/arch/esp8266/driver/gpio.cc b/src/arch/esp8266/driver/gpio.cc index d06130d..dba22b0 100644 --- a/src/arch/esp8266/driver/gpio.cc +++ b/src/arch/esp8266/driver/gpio.cc @@ -22,7 +22,7 @@ void ICACHE_FLASH_ATTR GPIO::setup() #endif } -void ICACHE_FLASH_ATTR GPIO::led_on(unsigned char id) +void ICACHE_FLASH_ATTR GPIO::led_on(unsigned char const id) { #ifdef LED_ON_GPIO16 if (id == 0) { @@ -35,7 +35,7 @@ void ICACHE_FLASH_ATTR GPIO::led_on(unsigned char id) #endif } -void ICACHE_FLASH_ATTR GPIO::led_off(unsigned char id) +void ICACHE_FLASH_ATTR GPIO::led_off(unsigned char const id) { #ifdef LED_ON_GPIO16 if (id == 0) { @@ -48,7 +48,7 @@ void ICACHE_FLASH_ATTR GPIO::led_off(unsigned char id) #endif } -void ICACHE_FLASH_ATTR GPIO::led_toggle(unsigned char id) +void ICACHE_FLASH_ATTR GPIO::led_toggle(unsigned char const id) { #ifdef LED_ON_GPIO16 if (id == 0) { @@ -69,4 +69,126 @@ void ICACHE_FLASH_ATTR GPIO::led_toggle(unsigned char id) #endif } +void ICACHE_FLASH_ATTR GPIO::input(unsigned char const pin) +{ + if (pin == d0) { + PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0); + } else if (pin == tx) { + PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, FUNC_GPIO1); + } else if (pin == d4) { + PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_GPIO2); + } else if (pin == rx) { + PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, FUNC_GPIO3); + } else if (pin == d2) { + PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO4_U, FUNC_GPIO4); + } else if (pin == d1) { + PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO5_U, FUNC_GPIO5); + } else if (pin == d6) { + PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, FUNC_GPIO12); + } else if (pin == d7) { + PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, FUNC_GPIO13); + } else if (pin == d5) { + PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTMS_U, FUNC_GPIO14); + } else if (pin == d8) { + PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U, FUNC_GPIO15); + } else if (pin == d0) { + // TODO this sets it as output, not input + WRITE_PERI_REG(PAD_XPD_DCDC_CONF, (READ_PERI_REG(PAD_XPD_DCDC_CONF) & 0xffffffbc) | (uint32)0x1); + WRITE_PERI_REG(RTC_GPIO_CONF, (READ_PERI_REG(RTC_GPIO_CONF) & (uint32)0xfffffffe) | (uint32)0x0); + WRITE_PERI_REG(RTC_GPIO_ENABLE, (READ_PERI_REG(RTC_GPIO_ENABLE) & (uint32)0xfffffffe) | (uint32)0x1); + } + if (pin < d0) { + gpio_output_set(0, 0, 0, (1 << pin)); + } +} + +void ICACHE_FLASH_ATTR GPIO::input(unsigned char const pin, bool pullup) +{ + if (pin == d0) { + if (pullup) { + PIN_PULLUP_EN(PERIPHS_IO_MUX_GPIO0_U); + } else { + PIN_PULLUP_DIS(PERIPHS_IO_MUX_GPIO0_U); + } + } else if (pin == tx) { + if (pullup) { + PIN_PULLUP_EN(PERIPHS_IO_MUX_U0TXD_U); + } else { + PIN_PULLUP_DIS(PERIPHS_IO_MUX_U0TXD_U); + } + } else if (pin == d4) { + if (pullup) { + PIN_PULLUP_EN(PERIPHS_IO_MUX_GPIO2_U); + } else { + PIN_PULLUP_DIS(PERIPHS_IO_MUX_GPIO2_U); + } + } else if (pin == rx) { + PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, FUNC_GPIO3); + } else if (pin == d2) { + PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO4_U, FUNC_GPIO4); + } else if (pin == d1) { + PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO5_U, FUNC_GPIO5); + } else if (pin == d6) { + PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, FUNC_GPIO12); + } else if (pin == d7) { + PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, FUNC_GPIO13); + } else if (pin == d5) { + PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTMS_U, FUNC_GPIO14); + } else if (pin == d8) { + PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U, FUNC_GPIO15); + } else if (pin == d0) { + // TODO this sets it as output, not input + WRITE_PERI_REG(PAD_XPD_DCDC_CONF, (READ_PERI_REG(PAD_XPD_DCDC_CONF) & 0xffffffbc) | (uint32)0x1); + WRITE_PERI_REG(RTC_GPIO_CONF, (READ_PERI_REG(RTC_GPIO_CONF) & (uint32)0xfffffffe) | (uint32)0x0); + WRITE_PERI_REG(RTC_GPIO_ENABLE, (READ_PERI_REG(RTC_GPIO_ENABLE) & (uint32)0xfffffffe) | (uint32)0x1); + } + input(pin); +} + +void ICACHE_FLASH_ATTR GPIO::output(unsigned char const pin) +{ + if (pin == d0) { + PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0); + } else if (pin == tx) { + PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, FUNC_GPIO1); + } else if (pin == d4) { + PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_GPIO2); + } else if (pin == rx) { + PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, FUNC_GPIO3); + } else if (pin == d2) { + PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO4_U, FUNC_GPIO4); + } else if (pin == d1) { + PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO5_U, FUNC_GPIO5); + } else if (pin == d6) { + PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, FUNC_GPIO12); + } else if (pin == d7) { + PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, FUNC_GPIO13); + } else if (pin == d5) { + PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTMS_U, FUNC_GPIO14); + } else if (pin == d8) { + PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U, FUNC_GPIO15); + } else if (pin == d0) { + WRITE_PERI_REG(PAD_XPD_DCDC_CONF, (READ_PERI_REG(PAD_XPD_DCDC_CONF) & 0xffffffbc) | (uint32)0x1); + WRITE_PERI_REG(RTC_GPIO_CONF, (READ_PERI_REG(RTC_GPIO_CONF) & (uint32)0xfffffffe) | (uint32)0x0); + WRITE_PERI_REG(RTC_GPIO_ENABLE, (READ_PERI_REG(RTC_GPIO_ENABLE) & (uint32)0xfffffffe) | (uint32)0x1); + } + if (pin < d0) { + gpio_output_set(0, 0, (1 << pin), 0); + } +} + +unsigned char ICACHE_FLASH_ATTR GPIO::read(unsigned char const pin) +{ + return (gpio_input_get() & (1 << pin)); +} + +void ICACHE_FLASH_ATTR GPIO::write(unsigned char const pin, unsigned char value) +{ + if (value) { + gpio_output_set(1 << pin, 0, 0, 0); + } else { + gpio_output_set(0, 1 << pin, 0, 0); + } +} + GPIO gpio; |