2
0

max80.ino 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 <freertos/task_snapshot.h>
  9. #include <esp_heap_caps.h>
  10. #include <esp_task_wdt.h>
  11. #include <esp_mac.h>
  12. #include <USB.h>
  13. #include <HardwareSerial.h>
  14. static void Read_Echo(Stream &from)
  15. {
  16. int byte;
  17. while ((byte = from.read()) >= 0)
  18. from.write(byte);
  19. }
  20. static void onrx_usb(void *foo, const char *bar, int baz, void *quux)
  21. {
  22. Read_Echo(Serial);
  23. }
  24. static void onrx_uart(void)
  25. {
  26. Read_Echo(Serial0);
  27. }
  28. #define PIN_USB_PWR_EN 7
  29. #define PIN_USB_PWR_SINK 8
  30. void setup_usb_ids()
  31. {
  32. uint8_t mac[8];
  33. char serial[16];
  34. esp_efuse_mac_get_default(mac);
  35. snprintf(serial, sizeof serial, "%02X%02X%02X-%02X%02X%02X",
  36. mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  37. USB.VID(0x4680);
  38. USB.PID(0x0882);
  39. USB.firmwareVersion(1); // Do something smarter here
  40. USB.productName("MAX80 Network Controller");
  41. USB.manufacturerName("Peter & Per");
  42. USB.serialNumber(serial);
  43. }
  44. static void dump_config()
  45. {
  46. printf("--- Configuration:\n");
  47. write_env(stdout, false);
  48. printf("--- Status:\n");
  49. write_env(stdout, true);
  50. printf("--- End configuration and status\n");
  51. }
  52. uint8_t max80_board_version;
  53. static void init_hw()
  54. {
  55. Serial0.begin(115200);
  56. Serial0.onReceive(onrx_uart, false);
  57. // Start out with disabled shared I/O pins
  58. for (int i = 1; i <= 18; i++)
  59. pinMode(i, INPUT);
  60. // Configure USB power control. Try to detect 36k pulldown
  61. // resistor on USB_PWR_EN first, to determine board version 2
  62. // (on board v1, this pin in N/C.)
  63. //
  64. // This detection algorithm is sketchy at best. In the end the
  65. // better option probably would be to use the ADC mode of the input
  66. // pin...
  67. pinMode(PIN_USB_PWR_SINK, OUTPUT); // IO8: USB_PWR_SINK
  68. digitalWrite(PIN_USB_PWR_SINK, 0); // This is a power sink
  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. pinMode(PIN_USB_PWR_EN, OUTPUT);
  78. digitalWrite(PIN_USB_PWR_EN, 0);
  79. delayMicroseconds(50);
  80. Serial.onEvent(ARDUINO_USB_CDC_RX_EVENT, onrx_usb);
  81. // Configure LEDs
  82. led_init();
  83. led_set(LED_BLUE, LED_FLASH); // ESP32 software initializing
  84. // Enable external PSRAM for heap
  85. heap_caps_malloc_extmem_enable(2048); // >= 2K allocations in PSRAM
  86. printf("[PCB] MAX80 board version: %u\n", max80_board_version);
  87. setenv_ul("status.max80.hw.ver", max80_board_version);
  88. }
  89. void setup() {
  90. const char *fwdate = __DATE__ " " __TIME__;
  91. printf("[START] MAX80 firmware compiled on %s\n", fwdate);
  92. init_hw();
  93. Serial.println("MAX80 start");
  94. fpga_service_init();
  95. Serial.println("0.2");
  96. init_config();
  97. Serial.println("0.3");
  98. setenv_cond("status.max80.fw.date", fwdate);
  99. fpga_service_enable(true);
  100. Serial.println("0.4");
  101. SetupWiFi();
  102. Serial.println("0.5");
  103. printf("[RDY]\n");
  104. dump_config();
  105. led_set(LED_BLUE, LED_ON); // Software ready
  106. Serial.println("0.5");
  107. printf("Total heap: %d\n"
  108. "Free heap: %d\n"
  109. "Total PSRAM: %d\n"
  110. "Free PSRAM: %d\n",
  111. ESP.getHeapSize(), ESP.getFreeHeap(),
  112. ESP.getPsramSize(), ESP.getFreePsram());
  113. }
  114. static inline char task_state(eTaskState state)
  115. {
  116. switch (state) {
  117. case eInvalid:
  118. return 'X';
  119. case eReady:
  120. case eRunning:
  121. return 'R';
  122. case eBlocked:
  123. return 'D';
  124. case eSuspended:
  125. return 'S';
  126. case eDeleted:
  127. return 'Z';
  128. default:
  129. return '?';
  130. }
  131. }
  132. static void dump_tasks(void)
  133. {
  134. TaskHandle_t task = NULL;
  135. while (1) {
  136. task = pxTaskGetNext(task);
  137. if (!task)
  138. break;
  139. printf("%-16s %c %2u\n",
  140. pcTaskGetName(task),
  141. task_state(eTaskGetState(task)),
  142. uxTaskPriorityGet(task));
  143. }
  144. }
  145. void loop() {
  146. static int x = 0;
  147. if (0) {
  148. printf("loop task: %s\n", pcTaskGetName(xTaskGetCurrentTaskHandle()));
  149. printf("idle task: %s\n", pcTaskGetName(xTaskGetIdleTaskHandle()));
  150. dump_tasks();
  151. putchar('\n');
  152. }
  153. Serial.println(++x);
  154. vTaskDelay(2 * configTICK_RATE_HZ);
  155. }