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.

66 lines
1.8 KiB

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