max80.ino 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. // Make sure FPGA nCE input (board 2.1+) is not accidentally pulled high
  46. fpga_enable_nce();
  47. // Configure USB power control. Try to detect 36k pulldown
  48. // resistor on USB_PWR_EN first, to determine board version 2
  49. // (on board v1, this pin in N/C.)
  50. //
  51. // This detection algorithm is sketchy at best. In the end the
  52. // better option probably would be to use the ADC mode of the input
  53. // pin...
  54. pinMode(PIN_USB_PWR_SINK, OUTPUT); // IO8: USB_PWR_SINK
  55. digitalWrite(PIN_USB_PWR_SINK, 0); // This is a power sink
  56. pinMode(PIN_USB_PWR_EN, OUTPUT);
  57. digitalWrite(PIN_USB_PWR_EN, 0);
  58. delayMicroseconds(100);
  59. pinMode(PIN_USB_PWR_EN, INPUT_PULLUP);
  60. delayMicroseconds(50);
  61. pinMode(PIN_USB_PWR_EN, INPUT);
  62. delayMicroseconds(50);
  63. max80_board_version = digitalRead(PIN_USB_PWR_EN) ? 1 : 2;
  64. pinMode(PIN_USB_PWR_EN, OUTPUT);
  65. digitalWrite(PIN_USB_PWR_EN, 0);
  66. delayMicroseconds(50);
  67. // Configure LEDs
  68. led_init();
  69. led_set(LED_BLUE, LED_FLASH); // ESP32 software initializing
  70. }
  71. void setup() {
  72. const char *fwdate = __DATE__ " " __TIME__;
  73. printf("[START] MAX80 firmware compiled on %s\n", fwdate);
  74. init_hw();
  75. // Enable external PSRAM for heap
  76. heap_caps_malloc_extmem_enable(2048); // >= 2K allocations in PSRAM
  77. TTY::init();
  78. printf("[PCB] MAX80 board version: %u\n", max80_board_version);
  79. setenv_ul("status.max80.hw.ver", max80_board_version);
  80. Serial.println("MAX80 start");
  81. fpga_service_init();
  82. Serial.println("0.2");
  83. init_config();
  84. Serial.println("0.3");
  85. setenv_cond("status.max80.fw.date", fwdate);
  86. fpga_service_enable(true);
  87. Serial.println("0.4");
  88. SetupWiFi();
  89. Serial.println("0.5");
  90. printf("[RDY]\n");
  91. dump_config();
  92. led_set(LED_BLUE, LED_ON); // Software ready
  93. Serial.println("0.5");
  94. printf("Total heap: %d\n"
  95. "Free heap: %d\n"
  96. "Total PSRAM: %d\n"
  97. "Free PSRAM: %d\n",
  98. ESP.getHeapSize(), ESP.getFreeHeap(),
  99. ESP.getPsramSize(), ESP.getFreePsram());
  100. }
  101. static inline char task_state(eTaskState state)
  102. {
  103. switch (state) {
  104. case eInvalid:
  105. return 'X';
  106. case eReady:
  107. case eRunning:
  108. return 'R';
  109. case eBlocked:
  110. return 'D';
  111. case eSuspended:
  112. return 'S';
  113. case eDeleted:
  114. return 'Z';
  115. default:
  116. return '?';
  117. }
  118. }
  119. static void dump_tasks(void)
  120. {
  121. TaskHandle_t task = NULL;
  122. while (1) {
  123. task = pxTaskGetNext(task);
  124. if (!task)
  125. break;
  126. printf("%-16s %c %2u\n",
  127. pcTaskGetName(task),
  128. task_state(eTaskGetState(task)),
  129. uxTaskPriorityGet(task));
  130. }
  131. }
  132. void loop() {
  133. if (1) {
  134. printf("loop task: %s\n", pcTaskGetName(xTaskGetCurrentTaskHandle()));
  135. printf("idle task: %s\n", pcTaskGetName(xTaskGetIdleTaskHandle()));
  136. dump_tasks();
  137. putchar('\n');
  138. }
  139. TTY::ping();
  140. vTaskDelay(30 * configTICK_RATE_HZ);
  141. }