max80.ino 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 heap_info()
  32. {
  33. size_t il = heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL);
  34. size_t ia = heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
  35. size_t sl = heap_caps_get_largest_free_block(MALLOC_CAP_SPIRAM);
  36. size_t sa = heap_caps_get_free_size(MALLOC_CAP_SPIRAM);
  37. printf("Heap: sram %zu/%zu, spiram %zu/%zu\n", il, ia, sl, sa);
  38. }
  39. static void dump_config()
  40. {
  41. printf("--- Configuration:\n");
  42. write_env(stdout, false);
  43. printf("--- Status:\n");
  44. write_env(stdout, true);
  45. printf("--- End configuration and status\n");
  46. }
  47. uint8_t max80_board_version;
  48. static void init_hw()
  49. {
  50. // Start out with disabled shared I/O pins
  51. for (int i = 1; i <= 18; i++)
  52. pinMode(i, INPUT);
  53. // Make sure FPGA nCE input (board 2.1+) is not accidentally pulled high
  54. fpga_enable_nce();
  55. // Configure USB power control. Try to detect 36k pulldown
  56. // resistor on USB_PWR_EN first, to determine board version 2
  57. // (on board v1, this pin in N/C.)
  58. //
  59. // This detection algorithm is sketchy at best. In the end the
  60. // better option probably would be to use the ADC mode of the input
  61. // pin...
  62. pinMode(PIN_USB_PWR_SINK, OUTPUT); // IO8: USB_PWR_SINK
  63. digitalWrite(PIN_USB_PWR_SINK, 0); // This is a power sink
  64. #if 0
  65. pinMode(PIN_USB_PWR_EN, OUTPUT);
  66. digitalWrite(PIN_USB_PWR_EN, 0);
  67. delayMicroseconds(100);
  68. pinMode(PIN_USB_PWR_EN, INPUT_PULLUP);
  69. delayMicroseconds(50);
  70. pinMode(PIN_USB_PWR_EN, INPUT);
  71. delayMicroseconds(50);
  72. max80_board_version = digitalRead(PIN_USB_PWR_EN) ? 1 : 2;
  73. #else
  74. max80_board_version = 2;
  75. #endif
  76. pinMode(PIN_USB_PWR_EN, OUTPUT);
  77. digitalWrite(PIN_USB_PWR_EN, 0);
  78. delayMicroseconds(50);
  79. // Configure LEDs
  80. led_init();
  81. led_set(LED_BLUE, LED_FLASH); // ESP32 software initializing
  82. }
  83. void setup() {
  84. const char *fwdate = __DATE__ " " __TIME__;
  85. printf("[START] MAX80 firmware compiled on %s\n", fwdate);
  86. init_hw();
  87. // Enable external PSRAM for heap
  88. heap_caps_malloc_extmem_enable(3000); // >= 3K allocations in PSRAM
  89. heap_info();
  90. TTY::init();
  91. heap_info();
  92. printf("[PCB] MAX80 board version: %u\n", max80_board_version);
  93. setenv_ul("status.max80.hw.ver", max80_board_version);
  94. Serial.println("MAX80 start");
  95. init_config();
  96. setenv_cond("status.max80.fw.date", fwdate);
  97. fpga_service_init();
  98. fpga_service_enable(true);
  99. SetupWiFi();
  100. Serial.println("[RDY]");
  101. dump_config();
  102. led_set(LED_BLUE, LED_ON); // Software ready
  103. heap_info();
  104. }
  105. static inline char task_state(eTaskState state)
  106. {
  107. switch (state) {
  108. case eInvalid:
  109. return 'X';
  110. case eReady:
  111. case eRunning:
  112. return 'R';
  113. case eBlocked:
  114. return 'D';
  115. case eSuspended:
  116. return 'S';
  117. case eDeleted:
  118. return 'Z';
  119. default:
  120. return '?';
  121. }
  122. }
  123. static void dump_tasks(void)
  124. {
  125. TaskHandle_t task = NULL;
  126. while (1) {
  127. task = pxTaskGetNext(task);
  128. if (!task)
  129. break;
  130. printf("%-16s %c %2u\n",
  131. pcTaskGetName(task),
  132. task_state(eTaskGetState(task)),
  133. uxTaskPriorityGet(task));
  134. }
  135. }
  136. void loop() {
  137. if (1) {
  138. printf("loop task: %s\n", pcTaskGetName(xTaskGetCurrentTaskHandle()));
  139. printf("idle task: %s\n", pcTaskGetName(xTaskGetIdleTaskHandle()));
  140. dump_tasks();
  141. heap_info();
  142. putchar('\n');
  143. }
  144. TTY::ping();
  145. vTaskDelay(5 * configTICK_RATE_HZ);
  146. }