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

Rework settings (#2282) * wip based on early draft. todo benchmarking * fixup eraser, assume keys are unique * fix cursor copy, test removal at random * small benchmark via permutations. todo lambdas and novirtual * fix empty condition / reset * overwrite optimizations, fix move offsets overflows * ...erase using 0xff instead of 0 * test the theory with code, different length kv were bugged * try to check for out-of-bounds writes / reads * style * trying to fix mover again * clarify length, defend against reading len on edge * fix uncommited rewind change * prove space assumptions * more concise traces, fix move condition (agrh!!!) * slightly more internal knowledge (estimates API?) * make sure cursor is only valid within the range * ensure 0 does not blow things * go back up * cursor comments * comments * rewrite writes through cursor * in del too * estimate kv storage requirements, return available size * move raw erase / move into a method, allow ::set to avoid scanning storage twice * refactor naming, use in code * amend storage slicing test * fix crash handler offsets, cleanup configuration * start -> begin * eeprom readiness * dependencies * unused * SPI_FLASH constants for older Core * vtables -> templates * less include dependencies * gcov help, move estimate outside of the class * writer position can never match, use begin + offset * tweak save_crash to trigger only once in a serious crash * doh, header function should be inline * foreach api, tweak structs for public api * use test helper class * when not using foreach, move cursor reset closer to the loop using read_kv * coverage comments, fix typo in tests decltype * ensure set() does not break with offset * make codacy happy again
4 years ago
  1. /*
  2. GPIO MODULE
  3. Copyright (C) 2017-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #include "espurna.h"
  6. // --------------------------------------------------------------------------
  7. #include <bitset>
  8. std::bitset<GpioPins> _gpio_locked;
  9. std::bitset<GpioPins> _gpio_available;
  10. bool gpioValid(unsigned char gpio) {
  11. if (gpio >= GpioPins) 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 < GpioPins; ++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. }