ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
main.c File Reference

Join the bench Wi-Fi over the ESP32-C6 and get an IP by DHCP (#492). More...

#include <stdint.h>
#include "c6_join.h"
#include "nx_ether_driver_c6.h"
#include "ra8_board_ek_ra8d2.h"
#include "ra8_boot_entry.h"
#include "ra8_c6link.h"
#include "ra8_c6link_wifi.h"
#include "ra8_cgc.h"
#include "ra8_err.h"
#include "ra8_esp_hosted_c6link.h"
#include "ra8_esp_hosted_port.h"
#include "ra8_isr.h"
#include "ra8_mstp.h"
#include "ra8_net_provision.h"
#include "ra8_secure.h"
#include "ra8_time.h"
#include "tx_api.h"
Include dependency graph for main.c:

Go to the source code of this file.

Functions

static void c6_join_panic_halt (void)
 Halt the processor after an unrecoverable start-up failure.
static void c6_join_setup_or_halt (void)
 Initialize platform services required by the join application.
static void c6_join_on_event (void *ctx, const ra8_c6link_event_t *ev)
 Record one C6 announcement and update association latches.
static void c6_join_report_fault (const char *what, ra8_err_t err)
 Report one failed C6 join stage without revealing credentials.
static ra8_err_t c6_join_open_link (void)
 Bind ESP-hosted transport and open the application C6 link.
static bool c6_join_phase_ready (ra8_c6link_fw_version_t *out)
 Open the C6 link and establish co-processor readiness.
static bool c6_join_wait_connected (void)
 Poll until station association succeeds, fails, or times out.
static bool c6_join_phase_associate (ra8_c6link_mac_t *mac_out, const ra8_net_credentials_t *credentials)
 Start Wi-Fi and associate using one runtime credential record.
static bool c6_join_phase_ip (const ra8_c6link_mac_t *mac)
 Bring up NetX, print the lease, and judge address acquisition.
static void c6_join_heartbeat (bool passed)
 Preserve the terminal verdict as a periodic console heartbeat.
static void c6_join_worker_entry (ULONG thread_input)
 Run provisioning, C6 readiness, association, DHCP, and heartbeat.
void tx_application_define (void *first_unused_memory)
 ThreadX define hook: bring the port up and start the worker.
void main (void)
 Application entry: clocks, console, banner, then ThreadX.

Variables

static uint32_t s_cpuclk_hz
 Cached CPUCLK0 rate.
static uint32_t s_pclka_hz
 Cached PCLKA rate.
static ra8_err_t s_init_err = k_ra8_err_not_initialized
 Port-init result from tx_application_define.
static TX_THREAD s_worker
 Application worker thread.
static CHAR s_worker_name [] = "c6_wifi_join"
 Worker thread name.
static UCHAR s_worker_stack [k_c6_join_worker_stack]
 Worker thread stack.
static uint8_t s_arena [k_c6_join_arena_bytes]
 Decode arena handed to the facade.
static ra8_c6link_t s_link
 The one C6 link handle this app owns.
static volatile uint32_t s_events
 Count of co-processor announcements seen.
static volatile uint8_t s_connected
 Non-zero once a station-connected event arrived.
static volatile uint8_t s_disconnected
 Non-zero once a station-disconnected event arrived.
static volatile uint16_t s_reason
 Last 802.11 disconnect reason code.
static const ra8_net_provision_uart_t s_provision_uart
 Board-console binding for the shared runtime provisioner.

Detailed Description

Join the bench Wi-Fi over the ESP32-C6 and get an IP by DHCP (#492).

Tag
[Ring 6 / APP] {World: S}

The first application on this board that takes a network all the way up over the ESP32-C6: it associates the co-processor's Wi-Fi station with a bench access point through ra8_c6link, then runs a NetX Duo DHCP client over the nx_ether_driver_c6 link driver to obtain a lease, and finally pings the gateway to prove the path carries traffic.

Two threads share the C6 link once the IP is up: the NetX IP thread transmits through the driver, and the driver's own RX poll worker pumps the link for received frames. Both are serialised behind the driver's wire mutex, so the application worker does no direct ra8_c6link calls after c6_join_net_up begins – the driver owns the wire from there.

The image contains no network credential. Its worker receives one bounded, versioned provisioning record over the board console after boot and erases that record immediately after the synchronous association request.

Since
0.1.0

Definition in file main.c.

Function Documentation

◆ c6_join_heartbeat()

void c6_join_heartbeat ( bool passed)
static

Preserve the terminal verdict as a periodic console heartbeat.

Emits the immutable pass or fail line and sleeps for the fixed heartbeat period forever after the network journey completes.

Parameters
[in]passedFinal application verdict.
Returns
Never returns.
Precondition
The console and ThreadX scheduler are operational.
No further network setup step is required.
Postcondition
The selected verdict is never changed.
Each loop iteration yields through tx_thread_sleep.
Note
Not thread-safe; this consumes the worker thread permanently.
Since
0.1.0

Definition at line 400 of file main.c.

References c6_join_puts(), k_c6_join_heartbeat_ms, and tx_thread_sleep.

Referenced by c6_join_worker_entry().

◆ c6_join_on_event()

void c6_join_on_event ( void * ctx,
const ra8_c6link_event_t * ev )
static

Record one C6 announcement and update association latches.

Counts every non-null event and records connected or disconnected station state while preserving the last disconnect reason.

Parameters
[in]ctxUnused callback context.
[in]evEvent to record; null is ignored.
Returns
Nothing.
Precondition
The callback is installed on the application-owned C6 link.
Event fields follow the C6 link contract when ev is non-null.
Postcondition
A non-null event increments the event counter once.
Station events update only their corresponding latches.
Note
Called by the single C6 pump context; shared latches are volatile.
Since
0.1.0

Definition at line 147 of file main.c.

References k_ra8_c6link_event_sta_connected, k_ra8_c6link_event_sta_disconnected, ra8_c6link_event::kind, ra8_c6link_event::reason, s_connected, s_disconnected, s_events, and s_reason.

Referenced by c6_join_open_link().

◆ c6_join_open_link()

ra8_err_t c6_join_open_link ( void )
static

Bind ESP-hosted transport and open the application C6 link.

Builds the fixed-arena link configuration with event and NetX RX callbacks, then delegates transport ownership to ra8_c6link_open.

Returns
Repository error code.
Return values
k_ra8_okThe transport was bound and link opened.
otherThe bind or link-open error propagated unchanged.
Precondition
ESP-hosted platform initialization has completed.
The static link and arena are not owned by another caller.
Postcondition
Success leaves s_link open with callbacks installed.
Bind failure leaves the link unopened.
Note
Not thread-safe; called once by the worker.
Since
0.1.0

Definition at line 203 of file main.c.

References ra8_c6link_cfg::arena, ra8_c6link_cfg::arena_bytes, c6_join_on_event(), ra8_c6link_cfg::cb_ctx, ra8_c6link_cfg::event_cb, k_ra8_ok, nx_ether_driver_c6_rx(), ra8_c6link_open(), ra8_esp_hosted_c6link_bind(), ra8_c6link_cfg::rx_cb, s_arena, s_link, and ra8_c6link_cfg::transport.

Referenced by c6_join_phase_ready().

◆ c6_join_panic_halt()

void c6_join_panic_halt ( void )
static

Halt the processor after an unrecoverable start-up failure.

Executes wait-for-interrupt forever so a failed kernel or platform initialization cannot fall through into partially configured code.

Returns
Never returns.
Precondition
A terminal start-up failure was detected.
Interrupt state is safe for an indefinite wait.
Postcondition
No application operation executes after entry.
The processor remains in the bounded halt loop.
Note
Not thread-safe; this is a terminal single-context path.
Since
0.1.0

Definition at line 92 of file main.c.

Referenced by c6_join_setup_or_halt(), and main().

◆ c6_join_phase_associate()

bool c6_join_phase_associate ( ra8_c6link_mac_t * mac_out,
const ra8_net_credentials_t * credentials )
static

Start Wi-Fi and associate using one runtime credential record.

Starts station mode, reads the local MAC, builds a transient C6 station request, submits it synchronously, erases that request, and waits for the association event without logging the SSID or PSK.

Parameters
[out]mac_outDestination for the station MAC.
[in]credentialsValidated runtime SSID and PSK record.
Returns
Association outcome.
Return values
trueThe station associated within the fixed poll budget.
falseStart, MAC, config, join, or association failed.
Precondition
mac_out and credentials are non-null.
The C6 link is open and ready.
Postcondition
The transient station configuration is securely erased.
Success initializes mac_out; failure emits a non-secret diagnostic.
Note
Not thread-safe; called once by the worker.
Since
0.1.0

Definition at line 307 of file main.c.

References c6_join_put_mac(), c6_join_put_u32(), c6_join_puts(), c6_join_report_fault(), c6_join_wait_connected(), k_ra8_ok, ra8_net_credentials::psk, ra8_c6link_sta_cfg_set(), ra8_c6link_wifi_join(), ra8_c6link_wifi_mac(), ra8_c6link_wifi_start(), ra8_secure_memzero(), s_link, s_reason, and ra8_net_credentials::ssid.

Referenced by c6_join_worker_entry().

◆ c6_join_phase_ip()

bool c6_join_phase_ip ( const ra8_c6link_mac_t * mac)
static

Bring up NetX, print the lease, and judge address acquisition.

Delegates DHCP binding to c6_join_net_up and emits only address, gateway, server, and reachability status fields.

Parameters
[in]macAssociated station MAC address.
Returns
Whether DHCP produced a non-zero address.
Return values
trueA non-zero lease address was bound.
falseNetwork bring-up failed or returned a zero address.
Precondition
mac is non-null and initialized.
Station association completed successfully.
Postcondition
Success prints the complete lease and ping status.
Failure prints a non-secret DHCP diagnostic.
Note
Not thread-safe; the worker transfers link ownership to NetX here.
Since
0.1.0

Definition at line 362 of file main.c.

References c6_join_net_up(), c6_join_put_ip(), c6_join_puts(), c6_join_lease::dhcp_server, c6_join_lease::gateway, c6_join_lease::ip, k_ra8_ok, c6_join_lease::mask, c6_join_lease::ping_ok, ra8_err_to_str(), and s_link.

Referenced by c6_join_worker_entry().

◆ c6_join_phase_ready()

bool c6_join_phase_ready ( ra8_c6link_fw_version_t * out)
static

Open the C6 link and establish co-processor readiness.

Opens the application link, waits for the bounded ready announcement, and emits only non-secret firmware and chip identity fields.

Parameters
[out]outFirmware-version destination.
Returns
Whether the link reached ready state.
Return values
trueThe link opened and readiness completed.
falseLink open or readiness failed.
Precondition
out is non-null and writable.
The ESP-hosted port was initialized successfully.
Postcondition
Success initializes out and leaves the link open.
Failure emits a stage-specific diagnostic.
Note
Not thread-safe; called once by the worker.
Since
0.1.0

Definition at line 233 of file main.c.

References c6_join_open_link(), c6_join_put_hex(), c6_join_put_u32(), c6_join_puts(), c6_join_report_fault(), ra8_c6link_fw_version::chip_id, k_c6_join_hex_byte, k_ra8_c6link_announce_transfers, k_ra8_ok, ra8_c6link_fw_version::major, ra8_c6link_fw_version::minor, ra8_c6link_fw_version::patch, ra8_c6link_await_ready(), ra8_err_to_str(), and s_link.

Referenced by c6_join_worker_entry().

◆ c6_join_report_fault()

void c6_join_report_fault ( const char * what,
ra8_err_t err )
static

Report one failed C6 join stage without revealing credentials.

Snapshots the link fault for diagnostic retention, then prints only the caller-supplied stage name and repository error description.

Parameters
[in]whatNUL-terminated non-secret stage name.
[in]errRepository error returned by that stage.
Returns
Nothing.
Precondition
The console is initialized.
what is valid within the console string bound.
Postcondition
One failure line is emitted.
No credential or SSID bytes are transmitted.
Note
Not thread-safe; the application worker is the sole caller.
Since
0.1.0

Definition at line 178 of file main.c.

References c6_join_puts(), ra8_c6link_last_fault(), ra8_err_to_str(), and s_link.

Referenced by c6_join_phase_associate(), and c6_join_phase_ready().

◆ c6_join_setup_or_halt()

void c6_join_setup_or_halt ( void )
static

Initialize platform services required by the join application.

Brings up clocks, records live rates, enables module-stop control, initializes delay timing, and opens the console in dependency order.

Returns
Nothing after successful initialization; failures halt permanently.
Precondition
Reset-time clock and board state is available.
No worker or C6 transaction has started.
Postcondition
Live CPUCLK0 and PCLKA rates are cached.
Timing and board-console services are initialized on return.
Note
Not thread-safe; call exactly once during boot.
Since
0.1.0

Definition at line 111 of file main.c.

References c6_join_panic_halt(), k_c6_join_uart_baud, k_ra8_clock_id_cpuclk0, k_ra8_clock_id_pclka, k_ra8_ok, ra8_board_uart_console_init(), ra8_cgc_get_clock_hz(), ra8_cgc_init(), ra8_mstp_init(), ra8_time_init(), s_cpuclk_hz, and s_pclka_hz.

Referenced by main().

◆ c6_join_wait_connected()

bool c6_join_wait_connected ( void )
static

Poll until station association succeeds, fails, or times out.

Pumps one bounded C6 announcement batch per iteration, checks the connection latches, and sleeps between attempts.

Returns
Association outcome.
Return values
trueA connected event arrived within the fixed budget.
falseA disconnected event arrived or the budget expired.
Precondition
The link is open and a join request was issued.
The callback latches belong to this worker journey.
Postcondition
At most k_c6_join_assoc_tries polls occurred.
Link ownership remains with the caller.
Note
Not thread-safe; the worker is the sole link-pump owner here.
Since
0.1.0

Definition at line 274 of file main.c.

References k_c6_join_assoc_gap_ms, k_c6_join_assoc_tries, k_ra8_c6link_announce_transfers, ra8_c6link_poll(), s_connected, s_disconnected, s_link, and tx_thread_sleep.

Referenced by c6_join_phase_associate().

◆ c6_join_worker_entry()

void c6_join_worker_entry ( ULONG thread_input)
static

Run provisioning, C6 readiness, association, DHCP, and heartbeat.

Refuses a failed port, receives one bounded runtime record, uses it only for synchronous association, erases it, completes DHCP, and enters the terminal heartbeat with the aggregate verdict.

Parameters
[in]thread_inputUnused ThreadX entry argument.
Returns
Nothing on provisioning failure; successful journeys enter heartbeat.
Precondition
ThreadX started this worker after tx_application_define.
s_init_err contains the ESP-hosted initialization result.
Postcondition
The credential record is erased before DHCP or return.
No credential-bearing field is written to the console.
Note
Not thread-safe; this is the application's sole worker thread.
Since
0.1.0

Definition at line 422 of file main.c.

References c6_join_heartbeat(), c6_join_phase_associate(), c6_join_phase_ip(), c6_join_phase_ready(), c6_join_put_u32(), c6_join_puts(), k_c6_join_boot_wait_ms, k_ra8_net_provision_timeout_ms, k_ra8_ok, ra8_delay_ms(), ra8_err_to_str(), ra8_net_provision_clear(), ra8_net_provision_receive(), s_events, s_init_err, and s_provision_uart.

Referenced by tx_application_define().

◆ main()

void main ( void )

Application entry: clocks, console, banner, then ThreadX.

The application entry point Reset_Handler hands control to.

Precondition
Reset_Handler has copied .data and zeroed .bss.
SystemInit has set VTOR, the FPU and the priority grouping.
Postcondition
The banner was printed exactly once.
Control passed to ThreadX and never came back.
Note
Everything after tx_kernel_enter runs on ThreadX; the panic-halt below is reached only if the kernel refuses to start.
Since
0.1.0

Definition at line 518 of file main.c.

References c6_join_panic_halt(), c6_join_print_banner(), c6_join_puts(), c6_join_setup_or_halt(), ra8_isr_globals_enable(), s_cpuclk_hz, and s_pclka_hz.

◆ tx_application_define()

void tx_application_define ( void * first_unused_memory)

ThreadX define hook: bring the port up and start the worker.

Parameters
[in]first_unused_memoryFree RAM from the ThreadX port; unused, every object here is static.
Returns
Nothing.
Precondition
tx_kernel_enter has been called and the clock cache is valid.
No vendored esp-hosted entry point has been called yet.
Postcondition
s_init_err holds the exact port-init result.
Exactly one worker thread was created.
Note
The port creates ThreadX byte pools, mutexes and timers, so its init must run where ThreadX permits object creation – here, not from main.
Since
0.1.0

Definition at line 482 of file main.c.

References c6_join_puts(), c6_join_worker_entry(), k_c6_join_edge_poll_ms, k_c6_join_sck_hz, k_c6_join_worker_prio, k_ra8_board_pmod1_sci_channel, ra8_esp_hosted_port_init(), s_init_err, s_pclka_hz, s_worker, s_worker_name, s_worker_stack, TX_AUTO_START, TX_NO_TIME_SLICE, TX_SUCCESS, and tx_thread_create.

Variable Documentation

◆ s_arena

uint8_t s_arena[k_c6_join_arena_bytes]
static

Decode arena handed to the facade.

Since
0.1.0

Definition at line 62 of file main.c.

◆ s_connected

volatile uint8_t s_connected
static

Non-zero once a station-connected event arrived.

Since
0.1.0

Definition at line 68 of file main.c.

◆ s_cpuclk_hz

uint32_t s_cpuclk_hz
static

Cached CPUCLK0 rate.

Since
0.1.0

Definition at line 50 of file main.c.

◆ s_disconnected

volatile uint8_t s_disconnected
static

Non-zero once a station-disconnected event arrived.

Since
0.1.0

Definition at line 70 of file main.c.

◆ s_events

volatile uint32_t s_events
static

Count of co-processor announcements seen.

Since
0.1.0

Definition at line 66 of file main.c.

◆ s_init_err

ra8_err_t s_init_err = k_ra8_err_not_initialized
static

Port-init result from tx_application_define.

Since
0.1.0

Definition at line 54 of file main.c.

Referenced by c6_join_worker_entry(), tx_application_define(), and wifi_hal_worker_entry().

◆ s_link

ra8_c6link_t s_link
static

The one C6 link handle this app owns.

Since
0.1.0

Definition at line 64 of file main.c.

◆ s_pclka_hz

uint32_t s_pclka_hz
static

Cached PCLKA rate.

Since
0.1.0

Definition at line 52 of file main.c.

◆ s_provision_uart

const ra8_net_provision_uart_t s_provision_uart
static
Initial value:
= {
.wait_ms = ra8_delay_ms,
}
ra8_err_t ra8_board_uart_console_read(uint8_t *out, size_t cap, size_t *out_len)
Polled non-blocking read from the J-Link OB VCOM console.
ra8_err_t ra8_board_uart_console_write(const uint8_t *data, size_t len)
Polled blocking write to the J-Link OB VCOM console.
void ra8_delay_ms(uint32_t ms)
Busy-wait for at least ms milliseconds.
Definition ra8_time.c:129

Board-console binding for the shared runtime provisioner.

Since
0.1.0

Definition at line 74 of file main.c.

◆ s_reason

volatile uint16_t s_reason
static

Last 802.11 disconnect reason code.

Since
0.1.0

Definition at line 72 of file main.c.

Referenced by c6_join_on_event(), and c6_join_phase_associate().

◆ s_worker

TX_THREAD s_worker
static

Application worker thread.

Since
0.1.0

Definition at line 56 of file main.c.

◆ s_worker_name

CHAR s_worker_name[] = "c6_wifi_join"
static

Worker thread name.

Since
0.1.0

Definition at line 58 of file main.c.

◆ s_worker_stack

UCHAR s_worker_stack[k_c6_join_worker_stack]
static

Worker thread stack.

Since
0.1.0

Definition at line 60 of file main.c.