max80.ino 3.9 KB

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