max80.ino 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. fpga_service_init();
  57. init_config();
  58. setenv("status.max80.fw.date", fwdate, 1);
  59. fpga_service_enable(true);
  60. SetupWiFi();
  61. printf("[RDY]\n");
  62. dump_config();
  63. led_set(LED_BLUE, LED_ON); // Software ready
  64. printf("Total heap: %d\n"
  65. "Free heap: %d\n"
  66. "Total PSRAM: %d\n"
  67. "Free PSRAM: %d\n",
  68. ESP.getHeapSize(), ESP.getFreeHeap(),
  69. ESP.getPsramSize(), ESP.getFreePsram());
  70. }
  71. static inline char task_state(eTaskState state)
  72. {
  73. switch (state) {
  74. case eInvalid:
  75. return 'X';
  76. case eReady:
  77. case eRunning:
  78. return 'R';
  79. case eBlocked:
  80. return 'D';
  81. case eSuspended:
  82. return 'S';
  83. case eDeleted:
  84. return 'Z';
  85. default:
  86. return '?';
  87. }
  88. }
  89. static void dump_tasks(void)
  90. {
  91. TaskHandle_t task = NULL;
  92. while (1) {
  93. task = pxTaskGetNext(task);
  94. if (!task)
  95. break;
  96. printf("%-16s %c %2u\n",
  97. pcTaskGetName(task),
  98. task_state(eTaskGetState(task)),
  99. uxTaskPriorityGet(task));
  100. }
  101. }
  102. void loop() {
  103. printf("loop task: %s\n", pcTaskGetName(xTaskGetCurrentTaskHandle()));
  104. printf("idle task: %s\n", pcTaskGetName(xTaskGetIdleTaskHandle()));
  105. dump_tasks();
  106. putchar('\n');
  107. vTaskDelay(120 * configTICK_RATE_HZ);
  108. }