max80.ino 5.4 KB

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