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.

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