max80.ino 5.0 KB

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