File
Blob: archive/fft-benchmark/firmware/platform/board.c
| 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <string.h> |
| 4 | #include <time.h> |
| 5 | #include "radio_bridge.h" |
| 6 | #include "esp_attr.h" |
| 7 | #include "esp_event.h" |
| 8 | #include "esp_heap_caps.h" |
| 9 | #include "esp_log.h" |
| 10 | #include "esp_netif.h" |
| 11 | #include "esp_netif_sntp.h" |
| 12 | #include "esp_partition.h" |
| 13 | #include "esp_psram.h" |
| 14 | #include "esp_random.h" |
| 15 | #include "esp_system.h" |
| 16 | #include "esp_timer.h" |
| 17 | #include "esp_wifi.h" |
| 18 | #include "freertos/FreeRTOS.h" |
| 19 | #include "freertos/event_groups.h" |
| 20 | #include "freertos/task.h" |
| 21 | #include "led_strip.h" |
| 22 | #include "wifi_private.h" |
| 23 | |
| 24 | static EventGroupHandle_t wifi_events; |
| 25 | static led_strip_handle_t led; |
| 26 | static RTC_DATA_ATTR uint32_t recovery_attempt; |
| 27 | static portMUX_TYPE recovery_lock = portMUX_INITIALIZER_UNLOCKED; |
| 28 | |
| 29 | static void wifi_event(void *arg, esp_event_base_t base, int32_t id, void *data) |
| 30 | { |
| 31 | if (base == IP_EVENT && id == IP_EVENT_STA_GOT_IP) xEventGroupSetBits(wifi_events, BIT0); |
| 32 | else if (base == WIFI_EVENT && id == WIFI_EVENT_STA_DISCONNECTED) { |
| 33 | xEventGroupClearBits(wifi_events, BIT0); |
| 34 | esp_wifi_connect(); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | int32_t radio_board_init(void) |
| 39 | { |
| 40 | esp_log_level_set("wifi", ESP_LOG_WARN); |
| 41 | led_strip_config_t strip = {.strip_gpio_num = 38, .max_leds = 1}; |
| 42 | led_strip_rmt_config_t rmt = {.resolution_hz = 10000000}; |
| 43 | ESP_ERROR_CHECK(led_strip_new_rmt_device(&strip, &rmt, &led)); |
| 44 | radio_led(8, 4, 0); |
| 45 | ESP_ERROR_CHECK(esp_netif_init()); |
| 46 | ESP_ERROR_CHECK(esp_event_loop_create_default()); |
| 47 | if (!esp_netif_create_default_wifi_sta()) return -1; |
| 48 | wifi_events = xEventGroupCreate(); |
| 49 | if (!wifi_events) return -1; |
| 50 | wifi_init_config_t init = WIFI_INIT_CONFIG_DEFAULT(); |
| 51 | init.nvs_enable = 0; |
| 52 | ESP_ERROR_CHECK(esp_wifi_init(&init)); |
| 53 | ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); |
| 54 | ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, wifi_event, NULL)); |
| 55 | ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, wifi_event, NULL)); |
| 56 | wifi_config_t config = {0}; |
| 57 | memcpy(config.sta.ssid, WIFI_SSID, sizeof(WIFI_SSID) - 1); |
| 58 | memcpy(config.sta.password, WIFI_PASSWORD, sizeof(WIFI_PASSWORD) - 1); |
| 59 | config.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK; |
| 60 | config.sta.sae_pwe_h2e = WPA3_SAE_PWE_BOTH; |
| 61 | ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); |
| 62 | ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &config)); |
| 63 | ESP_ERROR_CHECK(esp_wifi_start()); |
| 64 | ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_NONE)); |
| 65 | ESP_ERROR_CHECK(esp_wifi_connect()); |
| 66 | if (!(xEventGroupWaitBits(wifi_events, BIT0, false, false, pdMS_TO_TICKS(60000)) & BIT0)) return -1; |
| 67 | radio_led(0, 12, 4); |
| 68 | printf("PROBE {\"event\":\"hardware_ready\",\"firmware\":\"rust\",\"wifi\":true,\"psram_bytes\":%u,\"internal_free\":%u}\n", |
| 69 | (unsigned)esp_psram_get_size(), (unsigned)radio_heap_free()); |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | int32_t radio_network_ready(void) { return wifi_events && (xEventGroupGetBits(wifi_events) & BIT0); } |
| 74 | |
| 75 | int32_t radio_clock_sync(void) |
| 76 | { |
| 77 | static bool initialized; |
| 78 | if (!initialized) { |
| 79 | esp_sntp_config_t config = ESP_NETIF_SNTP_DEFAULT_CONFIG_MULTIPLE(2, |
| 80 | ESP_SNTP_SERVER_LIST("time.cloudflare.com", "pool.ntp.org")); |
| 81 | if (esp_netif_sntp_init(&config) != ESP_OK) return -1; |
| 82 | initialized = true; |
| 83 | } |
| 84 | if (time(NULL) < 1700000000) esp_netif_sntp_sync_wait(pdMS_TO_TICKS(20000)); |
| 85 | return time(NULL) >= 1700000000; |
| 86 | } |
| 87 | |
| 88 | uint64_t radio_now_us(void) { return esp_timer_get_time(); } |
| 89 | uint32_t radio_random(void) { return esp_random(); } |
| 90 | uint32_t radio_heap_free(void) { return heap_caps_get_free_size(MALLOC_CAP_INTERNAL); } |
| 91 | uint32_t radio_stack_free(void) { return uxTaskGetStackHighWaterMark(NULL); } |
| 92 | int32_t radio_rssi(void) { wifi_ap_record_t ap = {0}; esp_wifi_sta_get_ap_info(&ap); return ap.rssi; } |
| 93 | int32_t radio_led(uint8_t r, uint8_t g, uint8_t b) |
| 94 | { |
| 95 | int err = led_strip_set_pixel(led, 0, r, g, b); |
| 96 | return err ? err : led_strip_refresh(led); |
| 97 | } |
| 98 | int32_t radio_console_byte(void) { int ch = getchar(); if (ch == EOF) clearerr(stdin); return ch; } |
| 99 | void radio_log(const char *message) { ESP_LOGI("signaling", "%s", message); } |
| 100 | uint32_t radio_recovery_attempt(int32_t reset) |
| 101 | { |
| 102 | portENTER_CRITICAL(&recovery_lock); |
| 103 | uint32_t previous = recovery_attempt; |
| 104 | if (reset) recovery_attempt = 0; |
| 105 | else if (recovery_attempt < 3) recovery_attempt++; |
| 106 | portEXIT_CRITICAL(&recovery_lock); |
| 107 | return previous; |
| 108 | } |
| 109 | void radio_restart(void) { esp_restart(); abort(); } |
| 110 | |
| 111 | uint8_t *radio_music_load(size_t *length) |
| 112 | { |
| 113 | *length = 0; |
| 114 | const esp_partition_t *part = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, 0x40, "music"); |
| 115 | if (!part || part->size != 4 * 1024 * 1024) return NULL; |
| 116 | uint8_t *bytes = heap_caps_malloc(part->size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); |
| 117 | if (!bytes) return NULL; |
| 118 | if (esp_partition_read(part, 0, bytes, part->size)) { free(bytes); return NULL; } |
| 119 | *length = part->size; |
| 120 | return bytes; |
| 121 | } |
| 122 | void radio_music_free(uint8_t *bytes) { free(bytes); } |