2
0

max80.ino 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. void setup_usb_ids()
  20. {
  21. uint8_t * const mac = efuse_default_mac;
  22. char serial[16];
  23. esp_efuse_mac_get_default(mac);
  24. snprintf(serial, sizeof serial, "%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);
  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. uint8_t max80_board_version;
  50. static void init_hw()
  51. {
  52. // Start out with disabled shared I/O pins
  53. for (int i = 1; i <= 18; i++)
  54. pinMode(i, INPUT);
  55. // Query board info
  56. board_info_init();
  57. // Make sure FPGA nCE input (board 2.1+) is not accidentally pulled high
  58. fpga_enable_nce();
  59. // Configure USB power control. Try to detect 36k pulldown
  60. // resistor on USB_PWR_EN first, to determine board version 2
  61. // (on board v1, this pin in N/C.)
  62. //
  63. // This detection algorithm is sketchy at best. In the end the
  64. // better option probably would be to use the ADC mode of the input
  65. // pin...
  66. pinMode(PIN_USB_PWR_SINK, OUTPUT); // IO8: USB_PWR_SINK
  67. digitalWrite(PIN_USB_PWR_SINK, 0); // This is a power sink
  68. #if 0
  69. pinMode(PIN_USB_PWR_EN, OUTPUT);
  70. digitalWrite(PIN_USB_PWR_EN, 0);
  71. delayMicroseconds(100);
  72. pinMode(PIN_USB_PWR_EN, INPUT_PULLUP);
  73. delayMicroseconds(50);
  74. pinMode(PIN_USB_PWR_EN, INPUT);
  75. delayMicroseconds(50);
  76. max80_board_version = digitalRead(PIN_USB_PWR_EN) ? 1 : 2;
  77. #else
  78. max80_board_version = 2;
  79. #endif
  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. void setup() {
  88. const char *fwdate = __DATE__ " " __TIME__;
  89. init_hw();
  90. // Enable external PSRAM for heap
  91. heap_caps_malloc_extmem_enable(3000); // >= 3K allocations in PSRAM
  92. heap_info();
  93. TTY::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. setenv_cond("status.max80.hw.ver", board_info.version_str);
  98. Serial.println("MAX80 start");
  99. init_config();
  100. setenv_cond("status.max80.fw.date", fwdate);
  101. fpga_service_init();
  102. fpga_service_enable(true);
  103. SetupWiFi();
  104. Serial.println("[RDY]");
  105. dump_config();
  106. led_set(LED_BLUE, LED_ON); // Software ready
  107. heap_info();
  108. }
  109. static inline char task_state(eTaskState state)
  110. {
  111. switch (state) {
  112. case eInvalid:
  113. return 'X';
  114. case eReady:
  115. case eRunning:
  116. return 'R';
  117. case eBlocked:
  118. return 'D';
  119. case eSuspended:
  120. return 'S';
  121. case eDeleted:
  122. return 'Z';
  123. default:
  124. return '?';
  125. }
  126. }
  127. static void dump_tasks(void)
  128. {
  129. TaskHandle_t task = NULL;
  130. while (1) {
  131. task = pxTaskGetNext(task);
  132. if (!task)
  133. break;
  134. printf("%-16s %c %2u\n",
  135. pcTaskGetName(task),
  136. task_state(eTaskGetState(task)),
  137. uxTaskPriorityGet(task));
  138. }
  139. }
  140. void loop() {
  141. if (1) {
  142. printf("loop task: %s\n", pcTaskGetName(xTaskGetCurrentTaskHandle()));
  143. printf("idle task: %s\n", pcTaskGetName(xTaskGetIdleTaskHandle()));
  144. dump_tasks();
  145. heap_info();
  146. putchar('\n');
  147. }
  148. TTY::ping();
  149. vTaskDelay(5 * configTICK_RATE_HZ);
  150. }