| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 | // #define DEBUG#define BAUD_RATE 115200#include "common.h"#include "fpga.h"#include "wifi.h"#include "config.h"#include "led.h"#include "tty.h"#include <freertos/task_snapshot.h>#include <esp_heap_caps.h>#include <esp_task_wdt.h>#include <esp_mac.h>#include <USB.h>#include <HardwareSerial.h>#define PIN_USB_PWR_EN		7#define PIN_USB_PWR_SINK	8void setup_usb_ids(){    uint8_t mac[8];    char serial[16];    esp_efuse_mac_get_default(mac);    snprintf(serial, sizeof serial, "%02X%02X%02X-%02X%02X%02X",	     mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);    USB.VID(0x4680);    USB.PID(0x0882);    USB.firmwareVersion(1);	// Do something smarter here    USB.productName("MAX80 Network Controller");    USB.manufacturerName("Peter & Per");    USB.serialNumber(serial);}static void dump_config(){    printf("--- Configuration:\n");    write_env(stdout, false);    printf("--- Status:\n");    write_env(stdout, true);    printf("--- End configuration and status\n");}uint8_t max80_board_version;static void init_hw(){    // Start out with disabled shared I/O pins    for (int i = 1; i <= 18; i++)	pinMode(i, INPUT);    // Make sure FPGA nCE input (board 2.1+) is not accidentally pulled high    fpga_enable_nce();    // Configure USB power control. Try to detect 36k pulldown    // resistor on USB_PWR_EN first, to determine board version 2    // (on board v1, this pin in N/C.)    //    // This detection algorithm is sketchy at best. In the end the    // better option probably would be to use the ADC mode of the input    // pin...    pinMode(PIN_USB_PWR_SINK, OUTPUT);		// IO8: USB_PWR_SINK    digitalWrite(PIN_USB_PWR_SINK, 0);		// This is a power sink    pinMode(PIN_USB_PWR_EN, OUTPUT);    digitalWrite(PIN_USB_PWR_EN, 0);    delayMicroseconds(100);    pinMode(PIN_USB_PWR_EN, INPUT_PULLUP);    delayMicroseconds(50);    pinMode(PIN_USB_PWR_EN, INPUT);    delayMicroseconds(50);    max80_board_version = digitalRead(PIN_USB_PWR_EN) ? 1 : 2;    pinMode(PIN_USB_PWR_EN, OUTPUT);    digitalWrite(PIN_USB_PWR_EN, 0);    delayMicroseconds(50);    // Configure LEDs    led_init();    led_set(LED_BLUE, LED_FLASH);	// ESP32 software initializing}void setup() {    const char *fwdate = __DATE__ " " __TIME__;    printf("[START] MAX80 firmware compiled on %s\n", fwdate);    init_hw();    // Enable external PSRAM for heap    heap_caps_malloc_extmem_enable(2048); // >= 2K allocations in PSRAM    TTY::init();    printf("[PCB]  MAX80 board version: %u\n", max80_board_version);    setenv_ul("status.max80.hw.ver", max80_board_version);    Serial.println("MAX80 start");    fpga_service_init();    Serial.println("0.2");    init_config();    Serial.println("0.3");    setenv_cond("status.max80.fw.date", fwdate);    fpga_service_enable(true);    Serial.println("0.4");    SetupWiFi();    Serial.println("0.5");    printf("[RDY]\n");    dump_config();    led_set(LED_BLUE, LED_ON);	// Software ready    Serial.println("0.5");    printf("Total heap:  %d\n"	   "Free heap:   %d\n"	   "Total PSRAM: %d\n"	   "Free PSRAM:  %d\n",	   ESP.getHeapSize(), ESP.getFreeHeap(),	   ESP.getPsramSize(), ESP.getFreePsram());}static inline char task_state(eTaskState state){    switch (state) {    case eInvalid:	return 'X';    case eReady:    case eRunning:	return 'R';    case eBlocked:	return 'D';    case eSuspended:	return 'S';    case eDeleted:	return 'Z';    default:	return '?';    }}static void dump_tasks(void){    TaskHandle_t task = NULL;    while (1) {	task = pxTaskGetNext(task);	if (!task)	    break;	printf("%-16s %c %2u\n",	       pcTaskGetName(task),	       task_state(eTaskGetState(task)),	       uxTaskPriorityGet(task));    }}void loop() {    if (1) {	printf("loop task: %s\n", pcTaskGetName(xTaskGetCurrentTaskHandle()));	printf("idle task: %s\n", pcTaskGetName(xTaskGetIdleTaskHandle()));	dump_tasks();	putchar('\n');    }    TTY::ping();    vTaskDelay(30 * configTICK_RATE_HZ);}
 |