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

ThreadX + NetX Duo TCP echo demo for EK-RA8D2. More...

#include <stdint.h>
#include <string.h>
#include "ra8_board_ek_ra8d2.h"
#include "ra8_boot_entry.h"
#include "ra8_cgc.h"
#include "ra8_err.h"
#include "ra8_eth.h"
#include "ra8_isr.h"
#include "ra8_time.h"
#include "nx_api.h"
#include "nx_ether_driver_ra8_eth.h"
#include "tx_api.h"
Include dependency graph for main.c:

Go to the source code of this file.

Enumerations

enum  demo_config_t : uint32_t {
  k_demo_baud = 115200U ,
  k_demo_thread_stack = 8192U ,
  k_demo_ip_thread_pri = 1U ,
  k_demo_app_thread_pri = 8U ,
  k_demo_packet_size = 1568U ,
  k_demo_packet_count = 16U ,
  k_demo_pool_bytes = 32768U ,
  k_demo_ip_stack = 4096U ,
  k_demo_arp_cache = 1024U ,
  k_demo_echo_port = 7U ,
  k_demo_recv_window = 4096U ,
  k_demo_socket_ttl = 64U ,
  k_demo_recv_timeout = 0xFFFFFFFFU ,
  k_demo_log_buf_bytes = 80U
}
 Compile-time settings for the NetX Duo TCP-echo demo. More...
enum  demo_ip_octet_t : uint8_t {
  k_demo_ip_oct_a = 0U ,
  k_demo_ip_oct_b = 1U ,
  k_demo_ip_oct_c = 2U ,
  k_demo_ip_oct_d = 3U
}
 Octet indices into a packed IPv4 address. More...
enum  demo_ip_shift_t : uint8_t {
  k_demo_ip_shift_a = 24U ,
  k_demo_ip_shift_b = 16U ,
  k_demo_ip_shift_c = 8U ,
  k_demo_ip_shift_d = 0U
}
 Bit shifts that pack a 4-octet IPv4 into NetX's ULONG. More...
enum  demo_mac_word_shift_t : uint8_t {
  k_demo_mac_msw_shift_b0 = 8U ,
  k_demo_mac_msw_shift_b1 = 0U ,
  k_demo_mac_lsw_shift_b2 = 24U ,
  k_demo_mac_lsw_shift_b3 = 16U ,
  k_demo_mac_lsw_shift_b4 = 8U ,
  k_demo_mac_lsw_shift_b5 = 0U
}
 Shifts that pack the 6-byte MAC into msw/lsw ULONG pairs. More...
enum  demo_decimal_t : uint8_t { k_demo_decimal_base = 10U }
 Constants used by the tiny base-10 byte-to-ASCII helper. More...
enum  demo_ipv4_t : uint16_t {
  k_demo_ipaddr_0 = 192U ,
  k_demo_ipaddr_1 = 168U ,
  k_demo_ipaddr_2 = 1U ,
  k_demo_ipaddr_3 = 42U ,
  k_demo_netmask_b = 255U ,
  k_demo_octet_mask = 0xFFU
}
 Demo IPv4 address + netmask octets and helper masks. More...
enum  demo_mac_idx_t : uint8_t {
  k_demo_mac_idx_0 = 0U ,
  k_demo_mac_idx_1 = 1U ,
  k_demo_mac_idx_2 = 2U ,
  k_demo_mac_idx_3 = 3U ,
  k_demo_mac_idx_4 = 4U ,
  k_demo_mac_idx_5 = 5U
}
 MAC-address byte indices. More...

Functions

static void demo_panic_halt (void)
 Halt forever in WFI.
static void demo_setup_or_halt (void)
 Bring CGC + the J-Link OB VCOM console + on-board Ethernet up.
static void demo_print (const char *s)
 Convenience wrapper to write a NUL-terminated string to SCI8.
static char * demo_append_byte (char *buf, uint8_t v)
 Append a single decimal byte to the staging buffer.
static ULONG demo_pack_ip (const uint8_t *octets)
 Pack a 4-octet IPv4 array into NetX's host-order ULONG.
static void demo_pack_mac (ULONG *msw, ULONG *lsw)
 Pack the local MAC into the msw/lsw fields NetX expects.
static void demo_log_echo (ULONG n, ULONG peer_ip)
 Log "echoed N bytes from a.b.c.d" to SCI8.
static UINT demo_netx_create_ip (void)
 Creates the NetX packet pool and IP instance with our MAC.
static UINT demo_netx_bring_up (void)
static void demo_echo_loop (void)
 Echo loop: accept -> recv -> send -> disconnect -> relisten.
static void demo_thread_entry (ULONG thread_input)
 ThreadX worker entry: bring NetX up, then run the echo loop.
void tx_application_define (void *first_unused_memory)
 ThreadX system-define hook: build worker thread + NetX core.
void main (void)
 Application entry.

Variables

static const uint8_t k_demo_mac [6] = {0x02U, 0x00U, 0x00U, 0x00U, 0x00U, 0x01U}
static const uint8_t k_demo_ip [4]
 IPv4 address: 192.168.1.42 / 255.255.255.0.
static const uint8_t k_demo_mask [4] = {k_demo_netmask_b, k_demo_netmask_b, k_demo_netmask_b, 0U}
 Subnet mask: 255.255.255.0.
static NX_PACKET_POOL s_packet_pool
static NX_IP s_ip
static NX_TCP_SOCKET s_echo_socket
static UCHAR s_pool_memory [k_demo_pool_bytes]
static ULONG s_ip_stack [k_demo_ip_stack/sizeof(ULONG)]
static ULONG s_arp_cache [k_demo_arp_cache/sizeof(ULONG)]
static TX_THREAD s_demo_thread
static UCHAR s_demo_stack [k_demo_thread_stack]

Detailed Description

ThreadX + NetX Duo TCP echo demo for EK-RA8D2.

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

Brings the chip up the same way uart_hello does (CGC -> SCI8 @ 115200 8N1) and then hands control to ThreadX. The ThreadX application-define hook configures NetX Duo on top of our hardware Ethernet driver shim (nx_ether_driver_ra8_eth):

  1. nx_system_initialize().
  2. nx_packet_pool_create against a static byte pool.
  3. nx_ip_create at static IP 192.168.1.42 / 255.255.255.0, passing nx_ether_driver_ra8_eth as the link driver.
  4. nx_ip_address_change_notify is omitted – we never DHCP.
  5. nx_arp_enable + nx_tcp_enable + nx_icmp_enable.
  6. nx_tcp_socket_create + nx_tcp_server_socket_listen on port 7 (the IANA echo protocol).

The single worker thread loops:

  • nx_tcp_server_socket_accept (blocking).
  • nx_tcp_socket_receive -> nx_tcp_socket_send echo.
  • On peer close, nx_tcp_socket_disconnect + nx_tcp_server_socket_unaccept + nx_tcp_server_socket_relisten and back to accept.

Each accepted byte is logged to SCI8 as "echoed N bytes from a.b.c.d\r\n" so the J-Link OB CDC port gives visible feedback. NetX Duo is now the only TCP/IP stack in the tree – the hand-rolled ra8_net adapter and its companion ethernet_tcp_echo / ethernet_udp_echo / ethernet_http_responder apps were retired in #7.

Author
Brighton Sikarskie
Date
2026-04-29
Since
0.1.0

Definition in file main.c.

Enumeration Type Documentation

◆ demo_config_t

enum demo_config_t : uint32_t

Compile-time settings for the NetX Duo TCP-echo demo.

Every numeric used during bring-up appears here so the magic- number lint never sees a bare integer. The SCI8 channel matches the on-board J-Link OB CDC bridge pins (PD_02 / PD_03). The pool sizing follows the standard NetX Duo demo recipe (16 packets at 1568 bytes each; pool overhead allowed for) which fits easily in the 2 MiB SRAM budget.

Note
k_demo_pool_bytes used to be pinned here with a warning not to grow it, because growing it to 98304 moved s_tx_pool_storage to 0x2201CA60 and from that address the Ethernet TX path corrupted bits 4 and 5 of every byte at a frame offset congruent to 13 (mod 16) – silently, under a valid FCS, so even the 60-byte ARP reply went out with the wrong sender IP and the board simply vanished off the wire (#499). That was never an Ethernet defect. SRAMWTSC.WTEN was never programmed, so every SRAM read ran with no wait state at ICLK = 250 MHz, which HUM Ch 58.3.7 p 3540 puts outside guaranteed operation; the GWCA's DMA reads of this pool were simply where it showed. ra8_cgc_init now programs it, and the 98304-byte layout was re-tested on the wire at 3/3 byte-exact after the fix (3/3 corrupt before it, same source, same board). The pool size is back to being an ordinary sizing choice.
Enumerator
k_demo_baud 

Demo baud.

k_demo_thread_stack 

Demo thread stack.

k_demo_ip_thread_pri 

Demo IP thread priority.

k_demo_app_thread_pri 

Demo app thread priority.

k_demo_packet_size 

Demo packet size.

k_demo_packet_count 

Demo packet count.

k_demo_pool_bytes 

Packet pool (16 x 1568 + overhead).

k_demo_ip_stack 

Demo IP stack.

k_demo_arp_cache 

Demo arp cache.

k_demo_echo_port 

Demo echo port.

k_demo_recv_window 

Demo recv window.

k_demo_socket_ttl 

Demo socket ttl.

k_demo_recv_timeout 

TX_WAIT_FOREVER on this port.

k_demo_log_buf_bytes 

Demo log buffer bytes.

Definition at line 87 of file main.c.

◆ demo_decimal_t

enum demo_decimal_t : uint8_t

Constants used by the tiny base-10 byte-to-ASCII helper.

Enumerator
k_demo_decimal_base 

Base used by demo_append_byte.

Definition at line 143 of file main.c.

◆ demo_ip_octet_t

enum demo_ip_octet_t : uint8_t

Octet indices into a packed IPv4 address.

Enumerator
k_demo_ip_oct_a 

Demo IP oct a.

k_demo_ip_oct_b 

Demo IP oct b.

k_demo_ip_oct_c 

Demo IP oct c.

k_demo_ip_oct_d 

Demo IP oct d.

Definition at line 108 of file main.c.

◆ demo_ip_shift_t

enum demo_ip_shift_t : uint8_t

Bit shifts that pack a 4-octet IPv4 into NetX's ULONG.

Enumerator
k_demo_ip_shift_a 

Demo IP shift a.

k_demo_ip_shift_b 

Demo IP shift b.

k_demo_ip_shift_c 

Demo IP shift c.

k_demo_ip_shift_d 

Demo IP shift d.

Definition at line 119 of file main.c.

◆ demo_ipv4_t

enum demo_ipv4_t : uint16_t

Demo IPv4 address + netmask octets and helper masks.

Enumerator
k_demo_ipaddr_0 

192.168.1.42

k_demo_ipaddr_1 

Demo ipaddr 1.

k_demo_ipaddr_2 

Demo ipaddr 2.

k_demo_ipaddr_3 

Demo ipaddr 3.

k_demo_netmask_b 

255.255.255.0 (first three octets).

k_demo_octet_mask 

Mask one octet out of a packed IPv4.

Definition at line 148 of file main.c.

◆ demo_mac_idx_t

enum demo_mac_idx_t : uint8_t

MAC-address byte indices.

Enumerator
k_demo_mac_idx_0 

Demo MAC index 0.

k_demo_mac_idx_1 

Demo MAC index 1.

k_demo_mac_idx_2 

Demo MAC index 2.

k_demo_mac_idx_3 

Demo MAC index 3.

k_demo_mac_idx_4 

Demo MAC index 4.

k_demo_mac_idx_5 

Demo MAC index 5.

Definition at line 158 of file main.c.

◆ demo_mac_word_shift_t

enum demo_mac_word_shift_t : uint8_t

Shifts that pack the 6-byte MAC into msw/lsw ULONG pairs.

Enumerator
k_demo_mac_msw_shift_b0 

Demo MAC msw shift b0.

k_demo_mac_msw_shift_b1 

Demo MAC msw shift b1.

k_demo_mac_lsw_shift_b2 

Demo MAC lsw shift b2.

k_demo_mac_lsw_shift_b3 

Demo MAC lsw shift b3.

k_demo_mac_lsw_shift_b4 

Demo MAC lsw shift b4.

k_demo_mac_lsw_shift_b5 

Demo MAC lsw shift b5.

Definition at line 130 of file main.c.

Function Documentation

◆ demo_append_byte()

char * demo_append_byte ( char * buf,
uint8_t v )
static

Append a single decimal byte to the staging buffer.

Parameters
[in,out]bufBuffer pointer (advanced past the appended digits).
[in]vByte value (0..255).
Returns
Updated buffer pointer.
Precondition
buf != NULL with at least 4 bytes of headroom.
Since
0.1.0

Definition at line 269 of file main.c.

References k_demo_decimal_base.

Referenced by demo_log_echo().

◆ demo_echo_loop()

void demo_echo_loop ( void )
static

Echo loop: accept -> recv -> send -> disconnect -> relisten.

Precondition
demo_netx_bring_up returned NX_SUCCESS.
Postcondition
Loop runs forever; never returns.
Since
0.1.0

Definition at line 506 of file main.c.

References demo_log_echo(), demo_print(), k_demo_echo_port, s_echo_socket, and s_ip.

Referenced by demo_thread_entry().

◆ demo_log_echo()

void demo_log_echo ( ULONG n,
ULONG peer_ip )
static

Log "echoed N bytes from a.b.c.d" to SCI8.

Parameters
[in]nNumber of bytes echoed.
[in]peer_ipPeer's IPv4 address as a host-order ULONG.
Since
0.1.0

Definition at line 336 of file main.c.

References demo_append_byte(), demo_print(), k_demo_decimal_base, k_demo_ip_shift_a, k_demo_ip_shift_b, k_demo_ip_shift_c, k_demo_ip_shift_d, k_demo_log_buf_bytes, and k_demo_octet_mask.

Referenced by demo_echo_loop().

◆ demo_netx_bring_up()

UINT demo_netx_bring_up ( void )
static

◆ demo_netx_create_ip()

UINT demo_netx_create_ip ( void )
static

Creates the NetX packet pool and IP instance with our MAC.

Returns
UINT NX_SUCCESS on success, propagated NetX error otherwise.
Return values
NX_SUCCESSPool and IP created; MAC pushed to link driver.
Precondition
nx_system_initialize has run.
File-scope s_packet_pool / s_ip are reserved.
Postcondition
On success, IP thread is running and link driver knows the MAC.
On failure, returned code names the failing API.
Note
Called once from demo_netx_bring_up.
Since
0.1.0

Definition at line 406 of file main.c.

References demo_pack_ip(), demo_pack_mac(), k_demo_ip, k_demo_ip_thread_pri, k_demo_mac, k_demo_mask, k_demo_packet_size, nx_ether_driver_ra8_eth(), nx_ether_driver_ra8_eth_set_mac(), s_ip, s_ip_name, s_ip_stack, s_packet_pool, s_packet_pool_name, and s_pool_memory.

Referenced by demo_netx_bring_up().

◆ demo_pack_ip()

ULONG demo_pack_ip ( const uint8_t * octets)
static

Pack a 4-octet IPv4 array into NetX's host-order ULONG.

Parameters
[in]octetsIPv4 address (4 bytes).
Returns
Host-order packed address suitable for nx_ip_interface_address_set.
Since
0.1.0

Definition at line 302 of file main.c.

References k_demo_ip_oct_a, k_demo_ip_oct_b, k_demo_ip_oct_c, k_demo_ip_oct_d, k_demo_ip_shift_a, k_demo_ip_shift_b, k_demo_ip_shift_c, and k_demo_ip_shift_d.

◆ demo_pack_mac()

void demo_pack_mac ( ULONG * msw,
ULONG * lsw )
static

Pack the local MAC into the msw/lsw fields NetX expects.

Parameters
[out]mswMost-significant word (octets 0-1).
[out]lswLeast-significant word (octets 2-5).
Since
0.1.0

Definition at line 318 of file main.c.

References k_demo_mac, k_demo_mac_idx_0, k_demo_mac_idx_1, k_demo_mac_idx_2, k_demo_mac_idx_3, k_demo_mac_idx_4, k_demo_mac_idx_5, k_demo_mac_lsw_shift_b2, k_demo_mac_lsw_shift_b3, k_demo_mac_lsw_shift_b4, k_demo_mac_lsw_shift_b5, k_demo_mac_msw_shift_b0, and k_demo_mac_msw_shift_b1.

◆ demo_panic_halt()

void demo_panic_halt ( void )
static

Halt forever in WFI.

Precondition
Called only after a fatal error.
Postcondition
CPU is parked.
Since
0.1.0

Definition at line 201 of file main.c.

◆ demo_print()

void demo_print ( const char * s)
static

Convenience wrapper to write a NUL-terminated string to SCI8.

Parameters
[in]sNUL-terminated ASCII string. Must not be NULL.
Precondition
s != NULL.
Postcondition
Bytes are queued in the SCI8 TX FIFO (best-effort).
Since
0.1.0

Definition at line 248 of file main.c.

References ra8_board_uart_console_write(), and strlen().

◆ demo_setup_or_halt()

void demo_setup_or_halt ( void )
static

Bring CGC + the J-Link OB VCOM console + on-board Ethernet up.

Precondition
Reset_Handler / SystemInit complete.
Postcondition
On success the BSP console is sending at 115200 8N1 and the on-board RGMII PHY pins + ETHA0 / RMAC0 are initialized.
Since
0.1.0

Definition at line 217 of file main.c.

References demo_panic_halt(), k_demo_baud, k_ra8_clock_id_cpuclk0, k_ra8_ok, ra8_board_ethernet_init(), ra8_board_uart_console_init(), ra8_cgc_get_clock_hz(), ra8_cgc_init(), and ra8_time_init().

◆ demo_thread_entry()

void demo_thread_entry ( ULONG thread_input)
static

ThreadX worker entry: bring NetX up, then run the echo loop.

Parameters
[in]thread_inputUnused.
Precondition
nx_system_initialize has run.
Postcondition
Echo loop runs forever.
Since
0.1.0

Definition at line 551 of file main.c.

References demo_echo_loop(), demo_netx_bring_up(), and demo_print().

◆ main()

void main ( void )

Application entry.

The application entry point Reset_Handler hands control to.

Brings up clocks + UART + RMII pins, then enters ThreadX.

Precondition
Reset_Handler has copied .data and zeroed .bss.
Postcondition
On clean entry the kernel runs the worker thread once.
Since
0.1.0

Definition at line 609 of file main.c.

References demo_panic_halt(), demo_print(), demo_setup_or_halt(), and ra8_isr_globals_enable().

◆ tx_application_define()

void tx_application_define ( void * first_unused_memory)

ThreadX system-define hook: build worker thread + NetX core.

Parameters
[in]first_unused_memoryPointer to free RAM provided by the ThreadX port; unused – we statically allocate.
Precondition
tx_kernel_enter() has been called.
Postcondition
One worker thread is created and NetX is initialized.
Since
0.1.0

Definition at line 580 of file main.c.

References demo_thread_entry(), k_demo_app_thread_pri, s_demo_stack, s_demo_thread, s_thread_name, TX_AUTO_START, TX_NO_TIME_SLICE, and tx_thread_create.

Variable Documentation

◆ k_demo_ip

const uint8_t k_demo_ip[4]
static
Initial value:
@ k_demo_ipaddr_0
192.168.1.42 board.
Definition main.c:147
@ k_demo_ipaddr_2
Demo ipaddr 2.
Definition main.c:149
@ k_demo_ipaddr_3
Demo ipaddr 3.
Definition main.c:150
@ k_demo_ipaddr_1
Demo ipaddr 1.
Definition main.c:148

IPv4 address: 192.168.1.42 / 255.255.255.0.

Definition at line 170 of file main.c.

◆ k_demo_mac

const uint8_t k_demo_mac[6] = {0x02U, 0x00U, 0x00U, 0x00U, 0x00U, 0x01U}
static

Definition at line 167 of file main.c.

◆ k_demo_mask

const uint8_t k_demo_mask[4] = {k_demo_netmask_b, k_demo_netmask_b, k_demo_netmask_b, 0U}
static

Subnet mask: 255.255.255.0.

Definition at line 176 of file main.c.

◆ s_arp_cache

ULONG s_arp_cache[k_demo_arp_cache/sizeof(ULONG)]
static

Definition at line 186 of file main.c.

◆ s_demo_stack

UCHAR s_demo_stack[k_demo_thread_stack]
static

Definition at line 190 of file main.c.

◆ s_demo_thread

TX_THREAD s_demo_thread
static

Definition at line 189 of file main.c.

◆ s_echo_socket

NX_TCP_SOCKET s_echo_socket
static

Definition at line 183 of file main.c.

Referenced by demo_echo_loop(), and demo_netx_bring_up().

◆ s_ip

NX_IP s_ip
static

Definition at line 182 of file main.c.

◆ s_ip_stack

ULONG s_ip_stack[k_demo_ip_stack/sizeof(ULONG)]
static

Definition at line 185 of file main.c.

◆ s_packet_pool

NX_PACKET_POOL s_packet_pool
static

Definition at line 181 of file main.c.

◆ s_pool_memory

UCHAR s_pool_memory[k_demo_pool_bytes]
static

Definition at line 184 of file main.c.