rcbench
Servo and ESC test bench: panel, IOmcu and shared C
Loading...
Searching...
No Matches
wifi_radio.c File Reference

The radio under esp_wifi: the board's ESP32-C6, reached over SDIO (Secure Digital input/output) through esp_wifi_remote and esp_hosted. More...

#include "wifi_radio.h"
#include <stdatomic.h>
#include "board_pins.h"
#include "driver/gpio.h"
#include "esp_event.h"
#include "esp_hosted.h"
#include "esp_log.h"
#include "esp_mac.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "sdkconfig.h"

Macros

#define WIFI_RADIO_UP_WAIT_MS   1000u
 How long wifi_radio_link() waits for esp_hosted's TRANSPORT_UP event after esp_hosted_connect_to_slave() has returned, in ms. esp_hosted posts the event before its connect returns on a C6 that answered, so the wait covers the event loop's delivery only.
#define WIFI_RADIO_UP_POLL_MS   10u
 The poll period of that wait, in ms.

Functions

BaseType_t __real_xTaskCreatePinnedToCore (TaskFunction_t fn, const char *name, const uint32_t stack, void *arg, UBaseType_t prio, TaskHandle_t *handle, const BaseType_t core)
 The linker's name for ESP-IDF's xTaskCreatePinnedToCore().
BaseType_t __wrap_xTaskCreatePinnedToCore (TaskFunction_t fn, const char *name, const uint32_t stack, void *arg, UBaseType_t prio, TaskHandle_t *handle, const BaseType_t core)
 Create a task on core 0 when no core is given.
void wifi_radio_mac (uint8_t mac[6])
 6 bytes that differ from board to board, for the network name.
static void link_failed (const char *why)
 Mark the link failed, once: log it and hold the C6 in reset.
static void on_hosted (void *arg, esp_event_base_t base, int32_t id, void *data)
 esp_hosted's events: the link up, and a link that failed for good.
static bool handlers_once (void)
 Register on_hosted() for the two events, before the first connect.
static bool wait_up (void)
 Wait for the TRANSPORT_UP event, at most WIFI_RADIO_UP_WAIT_MS.
static void version_once (void)
 Once, at the first start with the link up: the C6's version.
esp_err_t wifi_radio_link (void)
 Bring the link to the C6 up, before esp_wifi_init().
bool wifi_radio_failed (void)
 Whether the link has failed since the panel started.

Variables

static const char * TAG = "wifi"
 Log tag.
static atomic_bool s_failed
static atomic_bool s_up
static bool s_handlers
static bool s_once

Detailed Description

The radio under esp_wifi: the board's ESP32-C6, reached over SDIO (Secure Digital input/output) through esp_wifi_remote and esp_hosted.

See wifi_radio.h.

The link: SDIO slot 1, 4 data lines at 40MHz, the pins of board_pins.h (BOARD_C6_*), which esp_hosted reads from sdkconfig.defaults; the checks below fail the build when the two differ. esp_hosted sets up its 6 tasks and the SDMMC (SD/MMC host) controller before app_main(); it resets and talks to the C6 from the radio's first start on (wifi_radio_link()). Between sessions the radio is off (esp_wifi_deinit() on the C6); the C6 stays powered and is not reset again until the panel restarts. A link that fails, at the first start or later, holds the C6 in reset until the panel restarts.

SPDX-License-Identifier: MIT

Function Documentation

◆ __real_xTaskCreatePinnedToCore()

BaseType_t __real_xTaskCreatePinnedToCore ( TaskFunction_t fn,
const char * name,
const uint32_t stack,
void * arg,
UBaseType_t prio,
TaskHandle_t * handle,
const BaseType_t core )

The linker's name for ESP-IDF's xTaskCreatePinnedToCore().

Parameters
fnThe task's function.
nameIts name.
stackIts stack in bytes.
argIts argument.
prioIts priority.
handleReceives its handle; may be NULL.
core0, 1 or tskNO_AFFINITY.
Returns
pdPASS, or what ESP-IDF returns for a task it could not create.

◆ __wrap_xTaskCreatePinnedToCore()

BaseType_t __wrap_xTaskCreatePinnedToCore ( TaskFunction_t fn,
const char * name,
const uint32_t stack,
void * arg,
UBaseType_t prio,
TaskHandle_t * handle,
const BaseType_t core )

Create a task on core 0 when no core is given.

Core 1 runs the control task, which owns STOP and the heartbeat, and the run log task below it; nothing of the radio's runs there. esp_hosted creates its 6 tasks (SDIO receive buffer, read, process, write; RPC (remote procedure call) receive and send) at priority 23 with no core given, which on its own lets them take core 1 from the control task at priority 10. The link (CMakeLists.txt, –wrap) sends every xTaskCreatePinnedToCore() of the image through here, xTaskCreate() included: a task created with no core (tskNO_AFFINITY) runs on core 0, and a task created for a given core stays on it. xTaskCreateStatic() and the xTaskCreate*WithCaps() calls do not come through here; esp_hosted uses them only with CONFIG_ESP_HOSTED_DFLT_TASK_FROM_SPIRAM, which the #error above refuses.

Not covered by the wrap: the SD card and the C6 share ESP-IDF's SDMMC request mutex, which inherits priority. While the run log task on core 1 holds it for a card write and an esp_hosted task waits for it, the run log task runs at 23 on core 1, above the control task. The control task's longest period under that load is not measured.

Parameters
fnThe task's function.
nameIts name.
stackIts stack in bytes.
argIts argument.
prioIts priority.
handleReceives its handle; may be NULL.
core0, 1 or tskNO_AFFINITY.
Returns
pdPASS, or what ESP-IDF returns for a task it could not create.

◆ handlers_once()

bool handlers_once ( void )
static

Register on_hosted() for the two events, before the first connect.

Returns
true when both handlers are registered.

◆ link_failed()

void link_failed ( const char * why)
static

Mark the link failed, once: log it and hold the C6 in reset.

esp_hosted's tasks keep running after a failure. A C6 that holds DAT1 (its interrupt line) low would keep esp_hosted's read task, priority 23 on core 0, failing and logging in a loop. EN low holds the C6 in reset; its SDIO pins let go, the 51kOhm pull-ups take DAT1 high, and the read task waits. The C6 stays in reset until the panel restarts.

Parameters
whyWhat failed, for the log.

◆ on_hosted()

void on_hosted ( void * arg,
esp_event_base_t base,
int32_t id,
void * data )
static

esp_hosted's events: the link up, and a link that failed for good.

esp_hosted posts ESP_HOSTED_EVENT_TRANSPORT_UP once, when the C6 has sent its start event, and ESP_HOSTED_EVENT_TRANSPORT_FAILURE when the SDIO transfers to the C6 have failed for good. With CONFIG_ESP_HOSTED_TRANSPORT_RESTART_ON_FAILURE off it does not restart the panel; the worker sees the flag, takes the radio down and fails the session, and later starts fail at wifi_radio_link().

Parameters
argUnused.
baseESP_HOSTED_EVENT.
idThe event.
dataUnused.

◆ version_once()

void version_once ( void )
static

Once, at the first start with the link up: the C6's version.

A C6 firmware that predates the version request answers it with an error or not at all; the request then ends after esp_hosted's response time-out, 5s, once.

◆ wait_up()

bool wait_up ( void )
static

Wait for the TRANSPORT_UP event, at most WIFI_RADIO_UP_WAIT_MS.

Returns
true when the link is up.

◆ wifi_radio_failed()

bool wifi_radio_failed ( void )

Whether the link has failed since the panel started.

Returns
true after esp_hosted reported a failed SDIO link; callable from any task.

◆ wifi_radio_link()

esp_err_t wifi_radio_link ( void )

Bring the link to the C6 up, before esp_wifi_init().

Called from the task that brings the radio up. The first call resets the C6 (EN low for 10ms, then a fixed 1,500ms, CONFIG_ESP_HOSTED_SDIO_RESET_DELAY_MS), starts it as an SDIO card and waits for its start event, polling every 200ms. A C6 that starts as a card and sends no start event is reset again after 50 and after 100 polls; the call returns after about 25s. A C6 that does not start as a card ends the call after the card's start-up, at least 1,500ms. Then esp_hosted's TRANSPORT_UP event is awaited for at most 1,000ms. The C6's firmware version is logged once. Later calls with the link up return at once, with no reset.

Returns
ESP_OK with the link up; ESP_FAIL when the C6 did not answer, when the handlers of esp_hosted's events cannot be registered, and on every call once the link has failed, until the panel restarts. A C6 that did not answer is held in reset.

◆ wifi_radio_mac()

void wifi_radio_mac ( uint8_t mac[6])

6 bytes that differ from board to board, for the network name.

The ESP32-P4's base MAC (media access control) address from eFuse, read for wa_name().

Parameters
macReceives the 6 bytes.

Variable Documentation

◆ s_failed

atomic_bool s_failed
static

the link has failed since start-up

◆ s_handlers

bool s_handlers
static

the worker's: event handlers registered

◆ s_once

bool s_once
static

the worker's: the version logged

◆ s_up

atomic_bool s_up
static

esp_hosted reported the link up