max80.ino 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 "boardinfo_esp.h"
  10. #include <freertos/task_snapshot.h>
  11. #include <esp_heap_caps.h>
  12. #include <esp_task_wdt.h>
  13. #include <esp_mac.h>
  14. #include <USB.h>
  15. #include <HardwareSerial.h>
  16. #define PIN_USB_PWR_EN 7
  17. #define PIN_USB_PWR_SINK 8
  18. volatile bool do_log_config_status;
  19. uint8_t efuse_default_mac[6];
  20. char serial_number[16]; // Canonical board serial number
  21. void setup_usb_ids()
  22. {
  23. uint8_t * const mac = efuse_default_mac;
  24. esp_efuse_mac_get_default(mac);
  25. snprintf(serial_number, sizeof serial_number, "%02X%02X%02X-%02X%02X%02X",
  26. mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  27. USB.VID(0x4680);
  28. USB.PID(0x0882);
  29. USB.firmwareVersion(1); // Do something smarter here
  30. USB.productName("MAX80 Network Controller");
  31. USB.manufacturerName("Peter & Per");
  32. USB.serialNumber(serial_number);
  33. }
  34. static void heap_info()
  35. {
  36. size_t il = heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL);
  37. size_t ia = heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
  38. size_t sl = heap_caps_get_largest_free_block(MALLOC_CAP_SPIRAM);
  39. size_t sa = heap_caps_get_free_size(MALLOC_CAP_SPIRAM);
  40. printf("Heap: sram %zu/%zu, spiram %zu/%zu\n", il, ia, sl, sa);
  41. }
  42. #if 0
  43. static void dump_config()
  44. {
  45. printf("--- Configuration:\n");
  46. write_sysvars(stdout, false);
  47. printf("--- Status:\n");
  48. write_sysvars(stdout, true);
  49. printf("--- End configuration and status\n");
  50. }
  51. #endif
  52. static void init_hw()
  53. {
  54. // Start out with disabled shared I/O pins
  55. for (int i = 1; i <= 18; i++)
  56. pinMode(i, INPUT);
  57. // Query board info
  58. board_info_init();
  59. // Make sure FPGA nCE input (board 2.1+) is not accidentally pulled high
  60. fpga_enable_nce();
  61. // Configure USB power control. Try to detect 36k pulldown
  62. // resistor on USB_PWR_EN first, to determine board version 2
  63. // (on board v1, this pin in N/C.)
  64. //
  65. // This detection algorithm is sketchy at best. In the end the
  66. // better option probably would be to use the ADC mode of the input
  67. // pin...
  68. pinMode(PIN_USB_PWR_SINK, OUTPUT); // IO8: USB_PWR_SINK
  69. digitalWrite(PIN_USB_PWR_SINK, 0); // This is a power sink
  70. pinMode(PIN_USB_PWR_EN, OUTPUT);
  71. digitalWrite(PIN_USB_PWR_EN, 0);
  72. delayMicroseconds(50);
  73. // Configure LEDs
  74. led_init();
  75. led_set(LED_BLUE, LED_FLASH); // ESP32 software initializing
  76. }
  77. void setup() {
  78. const char *fwdate = __DATE__ " " __TIME__;
  79. init_hw();
  80. // Enable external PSRAM for heap
  81. heap_caps_malloc_extmem_enable(3000); // >= 3K allocations in PSRAM
  82. heap_info();
  83. TTY::init();
  84. heap_info();
  85. sysvar_init();
  86. printf("[FW] MAX80 firmware compiled on %s\n", fwdate);
  87. printf("[PCB] MAX80 board version: %s\n", board_info.version_str);
  88. setvar_str(status_max80_hw_ver, board_info.version_str);
  89. printf("[PCB] MAX80 serial number: %s\n", serial_number);
  90. setvar_str(status_max80_hw_serial, serial_number);
  91. Serial.println("MAX80 start");
  92. init_config();
  93. setvar_str(status_max80_fw_date, fwdate);
  94. fpga_service_init();
  95. fpga_service_enable(true);
  96. SetupWiFi();
  97. Serial.println("[RDY]");
  98. sysvar_print_updates = true;
  99. do_log_config_status = true; // Print configuration from main loop
  100. led_set(LED_BLUE, LED_ON); // Software ready
  101. heap_info();
  102. }
  103. static inline char task_state(eTaskState state)
  104. {
  105. switch (state) {
  106. case eInvalid:
  107. return 'X';
  108. case eReady:
  109. case eRunning:
  110. return 'R';
  111. case eBlocked:
  112. return 'D';
  113. case eSuspended:
  114. return 'S';
  115. case eDeleted:
  116. return 'Z';
  117. default:
  118. return '?';
  119. }
  120. }
  121. static void dump_tasks(void)
  122. {
  123. TaskHandle_t task = NULL;
  124. while (1) {
  125. task = pxTaskGetNext(task);
  126. if (!task)
  127. break;
  128. printf("%-16s %c %2u\n",
  129. pcTaskGetName(task),
  130. task_state(eTaskGetState(task)),
  131. uxTaskPriorityGet(task));
  132. }
  133. }
  134. void loop() {
  135. if (0) {
  136. printf("loop task: %s\n", pcTaskGetName(xTaskGetCurrentTaskHandle()));
  137. printf("idle task: %s\n", pcTaskGetName(xTaskGetIdleTaskHandle()));
  138. dump_tasks();
  139. heap_info();
  140. putchar('\n');
  141. }
  142. if (do_log_config_status) {
  143. do_log_config_status = false;
  144. log_config_status();
  145. }
  146. fflush(stdout);
  147. TTY::ping();
  148. vTaskDelay(5 * configTICK_RATE_HZ);
  149. }