max80.ino 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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\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. sysvar_init();
  94. heap_info();
  95. printf("[FW] MAX80 firmware compiled on %s\n", fwdate);
  96. printf("[PCB] MAX80 board version: %s\n", board_info.version_str);
  97. setvar_str(status_max80_hw_ver, board_info.version_str);
  98. printf("[PCB] MAX80 serial number: %s\n", serial_number);
  99. setvar_str(status_max80_hw_serial, serial_number);
  100. Serial.println("MAX80 start");
  101. init_config();
  102. setvar_str(status_max80_fw_date, fwdate);
  103. fpga_service_init();
  104. fpga_service_enable(true);
  105. SetupWiFi();
  106. Serial.println("[RDY]");
  107. sysvar_print_updates = true;
  108. do_log_config_status = true; // Print configuration from main loop
  109. led_set(LED_BLUE, LED_ON); // Software ready
  110. heap_info();
  111. }
  112. static inline char task_state(eTaskState state)
  113. {
  114. switch (state) {
  115. case eInvalid:
  116. return 'X';
  117. case eReady:
  118. case eRunning:
  119. return 'R';
  120. case eBlocked:
  121. return 'D';
  122. case eSuspended:
  123. return 'S';
  124. case eDeleted:
  125. return 'Z';
  126. default:
  127. return '?';
  128. }
  129. }
  130. static void dump_tasks(void)
  131. {
  132. TaskHandle_t task = NULL;
  133. while (1) {
  134. task = pxTaskGetNext(task);
  135. if (!task)
  136. break;
  137. printf("%-16s %c %2u\n",
  138. pcTaskGetName(task),
  139. task_state(eTaskGetState(task)),
  140. uxTaskPriorityGet(task));
  141. }
  142. }
  143. void loop() {
  144. if (0) {
  145. printf("loop task: %s\n", pcTaskGetName(xTaskGetCurrentTaskHandle()));
  146. printf("idle task: %s\n", pcTaskGetName(xTaskGetIdleTaskHandle()));
  147. dump_tasks();
  148. heap_info();
  149. putchar('\n');
  150. }
  151. if (do_log_config_status) {
  152. do_log_config_status = false;
  153. heap_info();
  154. log_config_status();
  155. }
  156. fflush(stdout);
  157. TTY::ping();
  158. vTaskDelay(5 * configTICK_RATE_HZ);
  159. }