max80.ino 4.7 KB

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