max80.ino 5.5 KB

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