|
ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
|
ThreadX + NetX Duo + Mbed TLS HTTPS client demo for EK-RA8D2. More...
#include <stdint.h>#include <string.h>#include "ra8_attributes.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_rsip.h"#include "ra8_time.h"#include "mbedtls/net_sockets.h"#include "mbedtls/platform.h"#include "mbedtls/private/ctr_drbg.h"#include "mbedtls/private/entropy.h"#include "mbedtls/private/sha256.h"#include "mbedtls/ssl.h"#include "mbedtls/x509_crt.h"#include "nx_api.h"#include "nx_ether_driver_ra8_eth.h"#include "psa/crypto.h"#include "tx_api.h"Go to the source code of this file.
Functions | |
| static void | internal_demo_panic_halt (void) |
| Halt forever in WFI. | |
| static void | internal_demo_setup_or_halt (void) |
| Bring CGC + the J-Link OB VCOM console + on-board Ethernet + RSIP up. | |
| static void | internal_demo_print (const char *s) |
| Convenience wrapper to write a NUL-terminated string to SCI8. | |
| static void | internal_demo_write_bytes (const uint8_t *buf, uint32_t len) |
| Write an arbitrary byte buffer to SCI8 (no NUL required). | |
| static ULONG | internal_demo_pack_ip (const uint8_t *octets) |
| Pack a 4-octet IPv4 array into NetX's host-order ULONG. | |
| static void | internal_demo_pack_mac (ULONG *msw, ULONG *lsw) |
| Pack the local MAC into the msw/lsw fields NetX expects. | |
| static UINT | internal_demo_netx_bring_up (void) |
| Bring NetX Duo up: pool, IP, ARP, TCP, ICMP. | |
| static UINT | internal_demo_tcp_connect (void) |
| Open a NetX Duo TCP socket and connect to www.example.com:443. | |
| static int | internal_demo_bio_send (void *ctx, const unsigned char *buf, size_t len) |
| Mbed TLS BIO send callback bound to nx_tcp_socket_send. | |
| static int | internal_demo_bio_recv (void *ctx, unsigned char *buf, size_t len) |
| Mbed TLS BIO recv callback bound to nx_tcp_socket_receive. | |
| static int | internal_demo_entropy_source (void *ctx, unsigned char *buf, size_t len, size_t *olen) |
| Pull entropy from the RSIP TRNG into Mbed TLS. | |
| static int | internal_demo_verify_cert_pin (void) |
| Verify the peer's leaf certificate matches our compile-time pin. | |
| static int | internal_demo_http_get (void) |
| Issue the HTTP GET and dump the first k_demo_dump_bytes to SCI8. | |
| static int | internal_demo_tls_session (void) |
| Run the TLS bring-up: DRBG, config, configured pin policy, GET, dump. | |
| static void | internal_demo_thread_entry (ULONG thread_input) |
| ThreadX worker entry: bring NetX up, run TLS session, dump body. | |
| static void * | internal_demo_calloc (size_t n, size_t size) |
| mbedtls_calloc hook backed by the ThreadX byte pool. | |
| static void | internal_demo_free (void *p) |
| mbedtls_free hook backed by the ThreadX byte pool. | |
| void | tx_application_define (void *first_unused_memory) |
| ThreadX system-define hook: build worker thread + byte pool. | |
| void | main (void) |
| Application entry. | |
Variables | |
| static const uint8_t | s_demo_mac [6] = {0x02U, 0x00U, 0x00U, 0x00U, 0x00U, 0x02U} |
| static const uint8_t | s_demo_ip [4] |
| IPv4 address: 192.168.1.42 / 255.255.255.0. | |
| static const uint8_t | s_demo_mask [4] = {k_demo_netmask_b, k_demo_netmask_b, k_demo_netmask_b, 0U} |
| Subnet mask: 255.255.255.0. | |
| static const uint8_t | s_demo_gw [4] = {k_demo_ipaddr_0, k_demo_ipaddr_1, k_demo_ipaddr_2, 1U} |
| Default gateway: 192.168.1.1. | |
| static const uint8_t | s_demo_host_ip [4] |
| Static IPv4 for www.example.com (legacy edge-of-net IP). | |
| static const char | s_demo_host_name [] = "www.example.com" |
| HTTP/1.1 Host header literal – matches the IP pin above. | |
| static const uint8_t | s_demo_cert_pin_sha256 [32] |
| SHA-256 of the expected leaf certificate DER, compile-time pinned. | |
| static NX_PACKET_POOL | s_packet_pool |
| static NX_IP | s_ip |
| static NX_TCP_SOCKET | s_tls_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] |
| static TX_BYTE_POOL | s_byte_pool |
| static UCHAR | s_byte_pool_memory [k_demo_byte_pool_size] |
| static mbedtls_ssl_context | s_ssl |
| static mbedtls_ssl_config | s_ssl_cfg |
| static mbedtls_ctr_drbg_context | s_drbg |
| static mbedtls_entropy_context | s_entropy |
| static UCHAR | s_request_buf [k_demo_request_buf] |
| static UCHAR | s_response_buf [k_demo_response_buf] |
| static UCHAR | s_tls_send_staging [k_demo_pool_bytes] |
ThreadX + NetX Duo + Mbed TLS HTTPS client demo for EK-RA8D2.
Brings the chip up the same way threadx_netx_tcp_echo does (CGC -> SCI8 @ 115200 8N1, RMII pins routed) and then hands control to ThreadX. The single worker thread:
Definition in file main.c.
| enum demo_config_t : uint32_t |
Compile-time settings for the HTTPS client demo.
Mirrors the threadx_netx_tcp_echo sizing for NetX Duo; the additional Mbed TLS state is sized inside the per-thread stack and the byte pool below.
| enum demo_ip_octet_t : uint8_t |
| enum demo_ip_shift_t : uint8_t |
| enum demo_ipv4_t : uint16_t |
Demo IPv4 addresses (board, gateway, host) as octet enums.
| enum demo_mac_idx_t : uint8_t |
| enum demo_mac_word_shift_t : uint8_t |
Shifts that pack the 6-byte MAC into msw/lsw ULONG pairs.
| enum http_ascii_t : uint8_t |
|
static |
Mbed TLS BIO recv callback bound to nx_tcp_socket_receive.
Receives one NetX packet, copies a bounded payload into the TLS buffer, releases the packet, and translates NetX wait states to Mbed TLS.
| [in] | ctx | Opaque context (unused). |
| [out] | buf | Output buffer. |
| [in] | len | Maximum bytes to read. |
| 0 | Empty output capacity performs no socket operation. |
| MBEDTLS_ERR_SSL_WANT_READ | No packet arrived before the timeout. |
Definition at line 573 of file main.c.
References k_demo_recv_timeout, RA8_INTERNAL, and s_tls_socket.
Referenced by internal_demo_tls_session().
|
static |
Mbed TLS BIO send callback bound to nx_tcp_socket_send.
Allocates one NetX packet, appends the requested TLS bytes, and transfers packet ownership to the connected socket on success.
| [in] | ctx | Opaque context (unused – we use the file-scope socket). |
| [in] | buf | Bytes Mbed TLS wants to send. |
| [in] | len | Buffer length. |
| 0 | Empty input requires no socket operation. |
| MBEDTLS_ERR_SSL_WANT_WRITE | NetX temporarily cannot accept a packet. |
Definition at line 524 of file main.c.
References k_demo_recv_timeout, memcpy(), RA8_INTERNAL, s_packet_pool, s_tls_send_staging, and s_tls_socket.
Referenced by internal_demo_tls_session().
|
static |
mbedtls_calloc hook backed by the ThreadX byte pool.
Computes the requested byte count safely with overflow detection, performs a nonblocking pool allocation, clears the complete span, and returns it to Mbed TLS.
| [in] | n | Element count. |
| [in] | size | Element size in bytes. |
| nullptr | The size is zero, multiplication overflows, or the pool is exhausted. |
| non-NULL | A zero-filled ThreadX-owned allocation. |
Definition at line 896 of file main.c.
References memset(), RA8_INTERNAL, s_byte_pool, and TX_SUCCESS.
Referenced by tx_application_define().
|
static |
Pull entropy from the RSIP TRNG into Mbed TLS.
Mbed TLS supplies the f_rng signature int f_rng(void* ctx, unsigned char* buf, size_t len); we bind it to ra8_rsip_trng_read so seeding the CTR_DRBG never depends on software entropy collectors.
| [in] | ctx | Unused. |
| [out] | buf | Output buffer. |
| [in] | len | Number of bytes requested. |
| [out] | olen | Receives the number of bytes actually written (always len on success). |
| 0 | Exactly len bytes were produced. |
| MBEDTLS_ERR_ENTROPY_SOURCE_FAILED | An argument or RSIP request failed. |
Definition at line 620 of file main.c.
References k_ra8_ok, and ra8_rsip_trng_read().
Referenced by internal_demo_tls_session().
|
static |
mbedtls_free hook backed by the ThreadX byte pool.
Releases a non-NULL allocation back to the byte pool while treating NULL exactly as the standard free contract requires.
| [in] | p | Pointer previously returned by internal_demo_calloc (NULL OK). |
Definition at line 925 of file main.c.
References RA8_INTERNAL.
Referenced by tx_application_define().
|
static |
Issue the HTTP GET and dump the first k_demo_dump_bytes to SCI8.
Formats and fully writes a bounded request, scans streamed response chunks for the header terminator, then emits only the capped body prefix.
| 0 | The request completed without a TLS read/write error. |
| -1 | Request formatting exceeded the fixed request buffer. |
Definition at line 683 of file main.c.
References internal_demo_write_bytes(), k_ascii_cr, k_ascii_lf, k_demo_dump_bytes, memcpy(), RA8_INTERNAL, s_demo_host_name, s_request_buf, s_response_buf, s_ssl, and strlen().
Referenced by internal_demo_tls_session().
|
static |
Bring NetX Duo up: pool, IP, ARP, TCP, ICMP.
No listen socket.
Creates the packet pool and IP instance, installs the fixed local addresses, then enables the protocols needed by outbound HTTPS.
| NX_SUCCESS | The outbound IPv4/TCP stack is ready. |
| nonzero | A NetX creation or protocol-enable operation failed. |
Definition at line 416 of file main.c.
References internal_demo_pack_ip(), internal_demo_pack_mac(), k_demo_ip_thread_pri, k_demo_packet_size, nx_ether_driver_ra8_eth(), RA8_INTERNAL, s_arp_cache, s_demo_gw, s_demo_ip, s_demo_mask, s_ip, s_ip_name, s_ip_stack, s_packet_pool, s_packet_pool_name, and s_pool_memory.
Referenced by internal_demo_thread_entry().
|
static |
Pack a 4-octet IPv4 array into NetX's host-order ULONG.
Places each octet at the NetX-defined significance so the result can be passed directly to IP, mask, and gateway APIs.
| [in] | octets | IPv4 address (4 bytes). |
| 0..UINT32_MAX | The packed four-octet address. |
Definition at line 371 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, k_demo_ip_shift_d, and RA8_INTERNAL.
Referenced by internal_demo_netx_bring_up(), and internal_demo_tcp_connect().
Pack the local MAC into the msw/lsw fields NetX expects.
Splits the immutable six-byte demo address into NetX's two-word physical-address representation.
| [out] | msw | Most-significant word (octets 0-1). |
| [out] | lsw | Least-significant word (octets 2-5). |
Definition at line 392 of file main.c.
References 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, k_demo_mac_msw_shift_b1, RA8_INTERNAL, and s_demo_mac.
Referenced by internal_demo_netx_bring_up().
|
static |
Halt forever in WFI.
Called on any fatal error.
Retains the failed boot context while preventing any further network, crypto, or board accesses.
Definition at line 265 of file main.c.
References RA8_INTERNAL.
Referenced by internal_demo_ble_or_halt(), internal_demo_clocks_or_halt(), internal_demo_setup_or_halt(), and main().
|
static |
Convenience wrapper to write a NUL-terminated string to SCI8.
Measures the payload locally and submits exactly those bytes to the BSP console without dynamic allocation.
| [in] | s | NUL-terminated ASCII string; NULL is a no-op. |
Definition at line 326 of file main.c.
References ra8_board_uart_console_write(), RA8_INTERNAL, and strlen().
Referenced by internal_demo_thread_entry(), internal_demo_tls_session(), and main().
|
static |
Bring CGC + the J-Link OB VCOM console + on-board Ethernet + RSIP up.
Initializes each dependency in clock-to-peripheral order and parks immediately if any mandatory service cannot be established.
Definition at line 285 of file main.c.
References internal_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(), RA8_INTERNAL, ra8_rsip_init(), and ra8_time_init().
Referenced by main().
|
static |
Open a NetX Duo TCP socket and connect to www.example.com:443.
Creates and binds one client socket, then performs a bounded-wait connection to the configured server address and HTTPS port.
| NX_SUCCESS | The socket is connected. |
| nonzero | Socket creation, binding, or connection failed. |
Definition at line 479 of file main.c.
References internal_demo_pack_ip(), k_demo_handshake_max, k_demo_https_port, k_demo_recv_window, k_demo_socket_ttl, RA8_INTERNAL, s_demo_host_ip, s_ip, s_socket_name, and s_tls_socket.
Referenced by internal_demo_thread_entry().
|
static |
ThreadX worker entry: bring NetX up, run TLS session, dump body.
Initializes PSA, brings up the IP stack, connects TCP, and runs one HTTPS transaction, logging the first failure rather than retrying forever.
| [in] | thread_input | Unused. |
Definition at line 852 of file main.c.
References internal_demo_netx_bring_up(), internal_demo_print(), internal_demo_tcp_connect(), internal_demo_tls_session(), and RA8_INTERNAL.
Referenced by tx_application_define().
|
static |
Run the TLS bring-up: DRBG, config, configured pin policy, GET, dump.
Configures one client session over the connected NetX socket, completes the handshake, applies the configured pin policy, and issues the GET. DEMO_DISABLE_CERT_PIN is an explicit bring-up-only override.
| 0 | TLS, configured pin policy, and HTTP processing succeeded. |
| -1 | A configuration, hostname, or pinning step failed. |
Definition at line 780 of file main.c.
References internal_demo_bio_recv(), internal_demo_bio_send(), internal_demo_entropy_source(), internal_demo_http_get(), internal_demo_print(), internal_demo_verify_cert_pin(), RA8_INTERNAL, s_demo_host_name, s_drbg, s_entropy, s_ssl, and s_ssl_cfg.
Referenced by internal_demo_thread_entry().
|
static |
Verify the peer's leaf certificate matches our compile-time pin.
Hashes the peer's leaf DER with ra8_rsip_sha256 and compares against s_demo_cert_pin_sha256. Mismatch is fatal – the caller must abort the handshake without sending the HTTP request.
| 0 | Pin matches. |
| -1 | Pin mismatch (or no peer cert available). |
Definition at line 652 of file main.c.
References k_ra8_ok, memcmp(), RA8_INTERNAL, ra8_rsip_sha256(), s_demo_cert_pin_sha256, and s_ssl.
Referenced by internal_demo_tls_session().
|
static |
Write an arbitrary byte buffer to SCI8 (no NUL required).
Sends one bounded byte span directly to the BSP console, allowing response-body fragments containing non-text bytes.
| [in] | buf | Bytes to emit. |
| [in] | len | Length in bytes. |
Definition at line 348 of file main.c.
References ra8_board_uart_console_write(), and RA8_INTERNAL.
Referenced by internal_demo_http_get().
| void main | ( | void | ) |
Application entry.
The application entry point Reset_Handler hands control to.
Brings up clocks + UART + RMII pins, then ThreadX.
Definition at line 976 of file main.c.
References internal_demo_panic_halt(), internal_demo_print(), internal_demo_setup_or_halt(), and ra8_isr_globals_enable().
| void tx_application_define | ( | void * | first_unused_memory | ) |
ThreadX system-define hook: build worker thread + byte pool.
| [in] | first_unused_memory | Pointer to free RAM provided by the ThreadX port; unused – we statically allocate. |
Definition at line 942 of file main.c.
References internal_demo_calloc(), internal_demo_free(), internal_demo_thread_entry(), k_demo_app_thread_pri, s_byte_pool, s_byte_pool_memory, s_byte_pool_name, s_demo_stack, s_demo_thread, s_thread_name, TX_AUTO_START, TX_NO_TIME_SLICE, and tx_thread_create.
|
static |
Definition at line 233 of file main.c.
Referenced by demo_netx_bring_up(), internal_c6_cam_net_create(), internal_demo_netx_bring_up(), priv_net_create_ip(), and priv_net_create_ip().
|
static |
Definition at line 239 of file main.c.
Referenced by demo_calloc(), internal_demo_calloc(), and tx_application_define().
|
static |
Definition at line 240 of file main.c.
Referenced by tx_application_define().
|
static |
SHA-256 of the expected leaf certificate DER, compile-time pinned.
Update this constant whenever www.example.com rotates its leaf certificate. Mismatch is treated as a fatal handshake failure – we will not send the HTTP request. To capture a fresh pin from a development host: echo | openssl s_client -connect www.example.com:443 \ -servername www.example.com 2>/dev/null \ | openssl x509 -outform der | openssl dgst -sha256 The 32-byte digest goes here byte-for-byte. The placeholder below is all-zero and will deliberately fail the pin check until the value is filled in – callers can disable pinning during bring-up by defining DEMO_DISABLE_CERT_PIN.
Definition at line 219 of file main.c.
Referenced by internal_demo_verify_cert_pin().
|
static |
Default gateway: 192.168.1.1.
Definition at line 186 of file main.c.
Referenced by internal_demo_netx_bring_up().
|
static |
Static IPv4 for www.example.com (legacy edge-of-net IP).
Pinned to 93.184.216.34 – the historic www.example.com IP. If the address rotates, override at compile time with -DDEMO_HOST_IPV4_OVERRIDE=0x5DB8D822UL etc., or replace this static lookup with a NetX DNS resolver in a future sweep.
Definition at line 196 of file main.c.
Referenced by internal_demo_tcp_connect().
|
static |
HTTP/1.1 Host header literal – matches the IP pin above.
Definition at line 202 of file main.c.
Referenced by internal_demo_http_get(), and internal_demo_tls_session().
|
static |
IPv4 address: 192.168.1.42 / 255.255.255.0.
Definition at line 177 of file main.c.
Referenced by internal_demo_netx_bring_up().
|
static |
Definition at line 174 of file main.c.
Referenced by internal_demo_pack_mac().
|
static |
Subnet mask: 255.255.255.0.
Definition at line 183 of file main.c.
Referenced by internal_demo_netx_bring_up().
|
static |
Definition at line 238 of file main.c.
Referenced by tx_application_define(), and tx_application_define().
|
static |
Definition at line 237 of file main.c.
Referenced by tx_application_define(), and tx_application_define().
|
static |
Definition at line 246 of file main.c.
Referenced by internal_demo_tls_session().
|
static |
Definition at line 247 of file main.c.
Referenced by internal_demo_tls_session().
|
static |
Definition at line 229 of file main.c.
Referenced by c6_cam_http_serve(), c6_cam_net_up(), demo_echo_loop(), demo_netx_bring_up(), demo_netx_create_ip(), demo_tcp_connect(), internal_c6_cam_net_create(), internal_demo_netx_bring_up(), internal_demo_tcp_connect(), priv_net_create_ip(), priv_net_create_ip(), priv_net_dhcp(), priv_net_dhcp(), and priv_net_ping().
|
static |
Definition at line 232 of file main.c.
Referenced by demo_netx_bring_up(), demo_netx_create_ip(), internal_c6_cam_net_create(), internal_demo_netx_bring_up(), priv_net_create_ip(), and priv_net_create_ip().
|
static |
Definition at line 228 of file main.c.
Referenced by demo_netx_bring_up(), demo_netx_create_ip(), internal_demo_bio_send(), internal_demo_netx_bring_up(), and tls_bio_send().
|
static |
Definition at line 231 of file main.c.
Referenced by demo_netx_bring_up(), demo_netx_create_ip(), and internal_demo_netx_bring_up().
|
static |
Definition at line 249 of file main.c.
Referenced by internal_demo_http_get().
|
static |
Definition at line 250 of file main.c.
Referenced by internal_demo_http_get().
|
static |
Definition at line 244 of file main.c.
Referenced by internal_demo_http_get(), internal_demo_tls_session(), and internal_demo_verify_cert_pin().
|
static |
Definition at line 245 of file main.c.
Referenced by internal_demo_tls_session().
|
static |
Definition at line 251 of file main.c.
Referenced by internal_demo_bio_send().
|
static |
Definition at line 230 of file main.c.
Referenced by demo_run_tls(), demo_tcp_connect(), internal_demo_bio_recv(), internal_demo_bio_send(), internal_demo_tcp_connect(), tls_bio_recv(), and tls_bio_send().