File
Blob: 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) |
| 32 | xEventGroupSetBits(wifi_events, BIT0); |
| 33 | else if (base == WIFI_EVENT && id == WIFI_EVENT_STA_DISCONNECTED) { |
| 34 | xEventGroupClearBits(wifi_events, BIT0); |
| 35 | esp_wifi_connect(); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | int32_t radio_board_init(void) |
| 40 | { |
| 41 | esp_log_level_set("wifi", ESP_LOG_WARN); |
| 42 | led_strip_config_t strip = {.strip_gpio_num = 38, .max_leds = 1}; |
| 43 | led_strip_rmt_config_t rmt = {.resolution_hz = 10000000}; |
| 44 | ESP_ERROR_CHECK(led_strip_new_rmt_device(&strip, &rmt, &led)); |
| 45 | radio_led(8, 4, 0); |
| 46 | ESP_ERROR_CHECK(esp_netif_init()); |
| 47 | ESP_ERROR_CHECK(esp_event_loop_create_default()); |
| 48 | if (!esp_netif_create_default_wifi_sta()) |
| 49 | return -1; |
| 50 | wifi_events = xEventGroupCreate(); |
| 51 | if (!wifi_events) |
| 52 | return -1; |
| 53 | wifi_init_config_t init = WIFI_INIT_CONFIG_DEFAULT(); |
| 54 | init.nvs_enable = 0; |
| 55 | ESP_ERROR_CHECK(esp_wifi_init(&init)); |
| 56 | ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); |
| 57 | ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, wifi_event, NULL)); |
| 58 | ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, wifi_event, NULL)); |
| 59 | wifi_config_t config = {0}; |
| 60 | memcpy(config.sta.ssid, WIFI_SSID, sizeof(WIFI_SSID) - 1); |
| 61 | memcpy(config.sta.password, WIFI_PASSWORD, sizeof(WIFI_PASSWORD) - 1); |
| 62 | config.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK; |
| 63 | config.sta.sae_pwe_h2e = WPA3_SAE_PWE_BOTH; |
| 64 | ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); |
| 65 | ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &config)); |
| 66 | ESP_ERROR_CHECK(esp_wifi_start()); |
| 67 | ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_NONE)); |
| 68 | ESP_ERROR_CHECK(esp_wifi_connect()); |
| 69 | if (!(xEventGroupWaitBits(wifi_events, BIT0, false, false, pdMS_TO_TICKS(60000)) & BIT0)) |
| 70 | return -1; |
| 71 | radio_led(0, 12, 4); |
| 72 | printf("PROBE " |
| 73 | "{\"event\":\"hardware_ready\",\"firmware\":\"rust\",\"wifi\":true,\"psram_bytes\":%u," |
| 74 | "\"internal_free\":%u}\n", |
| 75 | (unsigned)esp_psram_get_size(), (unsigned)radio_heap_free()); |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | int32_t radio_network_ready(void) |
| 80 | { |
| 81 | return wifi_events && (xEventGroupGetBits(wifi_events) & BIT0); |
| 82 | } |
| 83 | |
| 84 | int32_t radio_ipv4(uint8_t octets[4]) |
| 85 | { |
| 86 | esp_netif_ip_info_t info; |
| 87 | esp_netif_t *netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"); |
| 88 | if (!netif || !radio_network_ready() || esp_netif_get_ip_info(netif, &info) != ESP_OK) |
| 89 | return -1; |
| 90 | memcpy(octets, &info.ip.addr, 4); |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | int32_t radio_clock_sync(void) |
| 95 | { |
| 96 | static bool initialized; |
| 97 | if (!initialized) { |
| 98 | esp_sntp_config_t config = ESP_NETIF_SNTP_DEFAULT_CONFIG_MULTIPLE( |
| 99 | 2, ESP_SNTP_SERVER_LIST("time.cloudflare.com", "pool.ntp.org")); |
| 100 | if (esp_netif_sntp_init(&config) != ESP_OK) |
| 101 | return -1; |
| 102 | initialized = true; |
| 103 | } |
| 104 | if (time(NULL) < 1700000000) |
| 105 | esp_netif_sntp_sync_wait(pdMS_TO_TICKS(20000)); |
| 106 | return time(NULL) >= 1700000000; |
| 107 | } |
| 108 | |
| 109 | uint64_t radio_now_us(void) |
| 110 | { |
| 111 | return esp_timer_get_time(); |
| 112 | } |
| 113 | uint32_t radio_random(void) |
| 114 | { |
| 115 | return esp_random(); |
| 116 | } |
| 117 | uint32_t radio_heap_free(void) |
| 118 | { |
| 119 | return heap_caps_get_free_size(MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); |
| 120 | } |
| 121 | uint32_t radio_stack_free(void) |
| 122 | { |
| 123 | return uxTaskGetStackHighWaterMark(NULL); |
| 124 | } |
| 125 | int32_t radio_current_core(void) |
| 126 | { |
| 127 | return xPortGetCoreID(); |
| 128 | } |
| 129 | int32_t radio_led(uint8_t r, uint8_t g, uint8_t b) |
| 130 | { |
| 131 | int err = led_strip_set_pixel(led, 0, r, g, b); |
| 132 | return err ? err : led_strip_refresh(led); |
| 133 | } |
| 134 | int32_t radio_console_byte(void) |
| 135 | { |
| 136 | int ch = getchar(); |
| 137 | if (ch == EOF) |
| 138 | clearerr(stdin); |
| 139 | return ch; |
| 140 | } |
| 141 | void radio_log(const char *message) |
| 142 | { |
| 143 | ESP_LOGI("signaling", "%s", message); |
| 144 | } |
| 145 | uint32_t radio_recovery_attempt(int32_t reset) |
| 146 | { |
| 147 | portENTER_CRITICAL(&recovery_lock); |
| 148 | uint32_t previous = recovery_attempt; |
| 149 | if (reset) |
| 150 | recovery_attempt = 0; |
| 151 | else if (recovery_attempt < 3) |
| 152 | recovery_attempt++; |
| 153 | portEXIT_CRITICAL(&recovery_lock); |
| 154 | return previous; |
| 155 | } |
| 156 | void radio_restart(void) |
| 157 | { |
| 158 | esp_restart(); |
| 159 | abort(); |
| 160 | } |
| 161 | |
| 162 | size_t radio_music_size(void) |
| 163 | { |
| 164 | const esp_partition_t *part = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, 0x40, "music"); |
| 165 | return part && part->size <= 16 * 1024 * 1024 ? part->size : 0; |
| 166 | } |
| 167 | int32_t radio_music_read(size_t offset, uint8_t *out, size_t length) |
| 168 | { |
| 169 | const esp_partition_t *part = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, 0x40, "music"); |
| 170 | if (!part || !out || offset > part->size || length > part->size - offset) |
| 171 | return ESP_ERR_INVALID_ARG; |
| 172 | return esp_partition_read(part, offset, out, length); |
| 173 | } |