serled.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2015 by Thorsten von Eicken, see LICENSE.txt
  2. #include <esp8266.h>
  3. #include <config.h>
  4. #include <serled.h>
  5. static ETSTimer serledTimer;
  6. static void ICACHE_FLASH_ATTR setSerled(int on) {
  7. int8_t pin = flashConfig.ser_led_pin;
  8. if (pin < 0) return; // disabled
  9. // LED is active-low
  10. if (on) {
  11. gpio_output_set(0, (1<<pin), (1<<pin), 0);
  12. } else {
  13. gpio_output_set((1<<pin), 0, (1<<pin), 0);
  14. }
  15. }
  16. static void ICACHE_FLASH_ATTR serledTimerCb(void *v) {
  17. setSerled(0);
  18. }
  19. void ICACHE_FLASH_ATTR serledFlash(int duration) {
  20. setSerled(1);
  21. os_timer_disarm(&serledTimer);
  22. os_timer_setfn(&serledTimer, serledTimerCb, NULL);
  23. os_timer_arm(&serledTimer, duration, 0);
  24. }
  25. void ICACHE_FLASH_ATTR serledInit(void) {
  26. int8_t pin = flashConfig.ser_led_pin;
  27. if (pin >= 0) {
  28. makeGpio(pin);
  29. gpio_output_set(0, 0, (1<<pin), 0);
  30. serledFlash(1000); // turn it on for 1 second
  31. }
  32. #ifdef SERLED_DBG
  33. os_printf("SER led=%d\n", pin);
  34. #endif
  35. }
  36. // Make a pin be GPIO, i.e. set the mux so the pin has the gpio function
  37. void ICACHE_FLASH_ATTR makeGpio(uint8_t pin) {
  38. uint32_t addr;
  39. uint8_t func = 3;
  40. switch (pin) {
  41. case 0:
  42. addr = PERIPHS_IO_MUX_GPIO0_U;
  43. func = 0;
  44. break;
  45. case 1:
  46. addr = PERIPHS_IO_MUX_U0TXD_U;
  47. break;
  48. case 2:
  49. addr = PERIPHS_IO_MUX_GPIO2_U;
  50. func = 0;
  51. break;
  52. case 3:
  53. addr = PERIPHS_IO_MUX_U0RXD_U;
  54. break;
  55. case 4:
  56. addr = PERIPHS_IO_MUX_GPIO4_U;
  57. func = 0;
  58. break;
  59. case 5:
  60. addr = PERIPHS_IO_MUX_GPIO5_U;
  61. func = 0;
  62. break;
  63. case 6:
  64. case 7:
  65. case 8:
  66. case 9:
  67. case 10:
  68. case 11:
  69. addr = PERIPHS_IO_MUX_SD_CMD_U - 4 * (11-pin);
  70. break;
  71. case 12:
  72. case 13:
  73. case 14:
  74. case 15:
  75. addr = PERIPHS_IO_MUX_MTDO_U - 4 * (15-pin);
  76. break;
  77. default:
  78. return;
  79. }
  80. PIN_FUNC_SELECT(addr, func);
  81. }