2
0

max80.ino 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. uint8_t efuse_default_mac[6];
  19. char serial_number[16]; // Canonical board serial number
  20. void setup_usb_ids()
  21. {
  22. uint8_t * const mac = efuse_default_mac;
  23. esp_efuse_mac_get_default(mac);
  24. snprintf(serial_number, sizeof serial_number, "%02X%02X%02X-%02X%02X%02X",
  25. mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  26. USB.VID(0x4680);
  27. USB.PID(0x0882);
  28. USB.firmwareVersion(1); // Do something smarter here
  29. USB.productName("MAX80 Network Controller");
  30. USB.manufacturerName("Peter & Per");
  31. USB.serialNumber(serial_number);
  32. }
  33. static void heap_info()
  34. {
  35. size_t il = heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL);
  36. size_t ia = heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
  37. size_t sl = heap_caps_get_largest_free_block(MALLOC_CAP_SPIRAM);
  38. size_t sa = heap_caps_get_free_size(MALLOC_CAP_SPIRAM);
  39. printf("Heap: sram %zu/%zu, spiram %zu/%zu\n", il, ia, sl, sa);
  40. }
  41. static void dump_config()
  42. {
  43. printf("--- Configuration:\n");
  44. write_env(stdout, false);
  45. printf("--- Status:\n");
  46. write_env(stdout, true);
  47. printf("--- End configuration and status\n");
  48. }
  49. static void init_hw()
  50. {
  51. // Start out with disabled shared I/O pins
  52. for (int i = 1; i <= 18; i++)
  53. pinMode(i, INPUT);
  54. // Query board info
  55. board_info_init();
  56. // Make sure FPGA nCE input (board 2.1+) is not accidentally pulled high
  57. fpga_enable_nce();
  58. // Configure USB power control. Try to detect 36k pulldown
  59. // resistor on USB_PWR_EN first, to determine board version 2
  60. // (on board v1, this pin in N/C.)
  61. //
  62. // This detection algorithm is sketchy at best. In the end the
  63. // better option probably would be to use the ADC mode of the input
  64. // pin...
  65. pinMode(PIN_USB_PWR_SINK, OUTPUT); // IO8: USB_PWR_SINK
  66. digitalWrite(PIN_USB_PWR_SINK, 0); // This is a power sink
  67. pinMode(PIN_USB_PWR_EN, OUTPUT);
  68. digitalWrite(PIN_USB_PWR_EN, 0);
  69. delayMicroseconds(50);
  70. // Configure LEDs
  71. led_init();
  72. led_set(LED_BLUE, LED_FLASH); // ESP32 software initializing
  73. }
  74. void setup() {
  75. const char *fwdate = __DATE__ " " __TIME__;
  76. init_hw();
  77. // Enable external PSRAM for heap
  78. heap_caps_malloc_extmem_enable(3000); // >= 3K allocations in PSRAM
  79. heap_info();
  80. TTY::init();
  81. heap_info();
  82. printf("[FW] MAX80 firmware compiled on %s\n", fwdate);
  83. printf("[PCB] MAX80 board version: %s\n", board_info.version_str);
  84. setenv_cond("status.max80.hw.ver", board_info.version_str);
  85. printf("[PCB] MAX80 serial number: %s\n", serial_number);
  86. setenv_cond("status.max80.hw.serial", serial_number);
  87. Serial.println("MAX80 start");
  88. init_config();
  89. setenv_cond("status.max80.fw.date", fwdate);
  90. fpga_service_init();
  91. fpga_service_enable(true);
  92. SetupWiFi();
  93. Serial.println("[RDY]");
  94. dump_config();
  95. led_set(LED_BLUE, LED_ON); // Software ready
  96. heap_info();
  97. }
  98. static inline char task_state(eTaskState state)
  99. {
  100. switch (state) {
  101. case eInvalid:
  102. return 'X';
  103. case eReady:
  104. case eRunning:
  105. return 'R';
  106. case eBlocked:
  107. return 'D';
  108. case eSuspended:
  109. return 'S';
  110. case eDeleted:
  111. return 'Z';
  112. default:
  113. return '?';
  114. }
  115. }
  116. static void dump_tasks(void)
  117. {
  118. TaskHandle_t task = NULL;
  119. while (1) {
  120. task = pxTaskGetNext(task);
  121. if (!task)
  122. break;
  123. printf("%-16s %c %2u\n",
  124. pcTaskGetName(task),
  125. task_state(eTaskGetState(task)),
  126. uxTaskPriorityGet(task));
  127. }
  128. }
  129. void loop() {
  130. if (0) {
  131. printf("loop task: %s\n", pcTaskGetName(xTaskGetCurrentTaskHandle()));
  132. printf("idle task: %s\n", pcTaskGetName(xTaskGetIdleTaskHandle()));
  133. dump_tasks();
  134. heap_info();
  135. putchar('\n');
  136. }
  137. TTY::ping();
  138. vTaskDelay(5 * configTICK_RATE_HZ);
  139. }