max80.ino 3.0 KB

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