Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
2.0 KiB

  1. /*
  2. GPIO MODULE
  3. Copyright (C) 2017-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #include "gpio.h"
  6. #include <bitset>
  7. constexpr const size_t GPIO_PINS = 17;
  8. std::bitset<GPIO_PINS> _gpio_locked;
  9. std::bitset<GPIO_PINS> _gpio_available;
  10. bool gpioValid(unsigned char gpio) {
  11. if (gpio >= GPIO_PINS) return false;
  12. return _gpio_available.test(gpio);
  13. }
  14. bool gpioGetLock(unsigned char gpio) {
  15. if (gpioValid(gpio)) {
  16. if (!_gpio_locked.test(gpio)) {
  17. _gpio_locked.set(gpio);
  18. DEBUG_MSG_P(PSTR("[GPIO] GPIO%u locked\n"), gpio);
  19. return true;
  20. }
  21. }
  22. DEBUG_MSG_P(PSTR("[GPIO] Failed getting lock for GPIO%u\n"), gpio);
  23. return false;
  24. }
  25. bool gpioReleaseLock(unsigned char gpio) {
  26. if (gpioValid(gpio)) {
  27. _gpio_locked.reset(gpio);
  28. DEBUG_MSG_P(PSTR("[GPIO] GPIO%u lock released\n"), gpio);
  29. return true;
  30. }
  31. DEBUG_MSG_P(PSTR("[GPIO] Failed releasing lock for GPIO%u\n"), gpio);
  32. return false;
  33. }
  34. void gpioSetup() {
  35. // https://github.com/espressif/esptool/blob/f04d34bcab29ace798d2d3800ba87020cccbbfdd/esptool.py#L1060-L1070
  36. // "One or the other efuse bit is set for ESP8285"
  37. // https://github.com/espressif/ESP8266_RTOS_SDK/blob/3c055779e9793e5f082afff63a011d6615e73639/components/esp8266/include/esp8266/efuse_register.h#L20-L21
  38. // "define EFUSE_IS_ESP8285 (1 << 4)"
  39. const uint32_t efuse_blocks[4] {
  40. READ_PERI_REG(0x3ff00050),
  41. READ_PERI_REG(0x3ff00054),
  42. READ_PERI_REG(0x3ff00058),
  43. READ_PERI_REG(0x3ff0005c)
  44. };
  45. const bool esp8285 = (
  46. (efuse_blocks[0] & (1 << 4))
  47. || (efuse_blocks[2] & (1 << 16))
  48. );
  49. // TODO: GPIO16 is only for basic I/O, gpioGetLock before attachInterrupt should check for that
  50. for (unsigned char pin=0; pin < GPIO_PINS; ++pin) {
  51. if (pin <= 5) _gpio_available.set(pin);
  52. if (((pin == 9) || (pin == 10)) && (esp8285)) _gpio_available.set(pin);
  53. if (12 <= pin && pin <= 16) _gpio_available.set(pin);
  54. }
  55. }