max80.ino 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // #define DEBUG
  2. #define BAUD_RATE 115200
  3. #include "common.h"
  4. #include "fpga.h"
  5. #include "wifi.h"
  6. #include "config.h"
  7. #include "led.h"
  8. #include <freertos/task_snapshot.h>
  9. #include <esp_heap_caps.h>
  10. #define PIN_USB_PWR_EN 7
  11. #define PIN_USB_PWR_SINK 8
  12. static void dump_config()
  13. {
  14. printf("--- Configuration:\n");
  15. write_env(stdout, false);
  16. printf("--- Status:\n");
  17. write_env(stdout, true);
  18. printf("--- End configuration and status\n");
  19. }
  20. uint8_t max80_board_version;
  21. static void init_hw()
  22. {
  23. // Start out with disabled shared I/O pins
  24. for (int i = 1; i <= 18; i++)
  25. pinMode(i, INPUT);
  26. // Configure USB power control. Try to detect 36k pulldown
  27. // resistor on USB_PWR_EN first, to determine board version 2
  28. // (on board v1, this pin in N/C.)
  29. //
  30. // This detection algorithm is sketchy at best. In the end the
  31. // better option probably would be to use the ADC mode of the input
  32. // pin...
  33. pinMode(PIN_USB_PWR_SINK, OUTPUT); // IO8: USB_PWR_SINK
  34. digitalWrite(PIN_USB_PWR_SINK, 0); // This is a power sink
  35. pinMode(PIN_USB_PWR_EN, OUTPUT);
  36. digitalWrite(PIN_USB_PWR_EN, 0);
  37. delayMicroseconds(100);
  38. pinMode(PIN_USB_PWR_EN, INPUT_PULLUP);
  39. delayMicroseconds(50);
  40. pinMode(PIN_USB_PWR_EN, INPUT);
  41. delayMicroseconds(50);
  42. max80_board_version = digitalRead(PIN_USB_PWR_EN) ? 1 : 2;
  43. pinMode(PIN_USB_PWR_EN, OUTPUT);
  44. digitalWrite(PIN_USB_PWR_EN, 0);
  45. // Configure LEDs
  46. led_init();
  47. led_set(LED_BLUE, LED_FLASH); // ESP32 software initializing
  48. // Enable external PSRAM for heap
  49. heap_caps_malloc_extmem_enable(2048); // >= 2K allocations in PSRAM
  50. printf("[PCB] MAX80 board version: %u\n", max80_board_version);
  51. setenv_ul("status.max80.hw.ver", max80_board_version);
  52. }
  53. void setup() {
  54. const char *fwdate = __DATE__ " " __TIME__;
  55. printf("[START] MAX80 firmware compiled on %s\n", fwdate);
  56. init_hw();
  57. fpga_service_init();
  58. init_config();
  59. setenv_cond("status.max80.fw.date", fwdate);
  60. fpga_service_enable(true);
  61. SetupWiFi();
  62. printf("[RDY]\n");
  63. dump_config();
  64. led_set(LED_BLUE, LED_ON); // Software ready
  65. printf("Total heap: %d\n"
  66. "Free heap: %d\n"
  67. "Total PSRAM: %d\n"
  68. "Free PSRAM: %d\n",
  69. ESP.getHeapSize(), ESP.getFreeHeap(),
  70. ESP.getPsramSize(), ESP.getFreePsram());
  71. }
  72. static inline char task_state(eTaskState state)
  73. {
  74. switch (state) {
  75. case eInvalid:
  76. return 'X';
  77. case eReady:
  78. case eRunning:
  79. return 'R';
  80. case eBlocked:
  81. return 'D';
  82. case eSuspended:
  83. return 'S';
  84. case eDeleted:
  85. return 'Z';
  86. default:
  87. return '?';
  88. }
  89. }
  90. static void dump_tasks(void)
  91. {
  92. TaskHandle_t task = NULL;
  93. while (1) {
  94. task = pxTaskGetNext(task);
  95. if (!task)
  96. break;
  97. printf("%-16s %c %2u\n",
  98. pcTaskGetName(task),
  99. task_state(eTaskGetState(task)),
  100. uxTaskPriorityGet(task));
  101. }
  102. }
  103. void loop() {
  104. printf("loop task: %s\n", pcTaskGetName(xTaskGetCurrentTaskHandle()));
  105. printf("idle task: %s\n", pcTaskGetName(xTaskGetIdleTaskHandle()));
  106. dump_tasks();
  107. putchar('\n');
  108. vTaskDelay(120 * configTICK_RATE_HZ);
  109. }