2
0

max80.ino 2.9 KB

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