max80.ino 3.6 KB

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