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

LIN commander frame-driver HIL demo for EK-RA8D2 (SCI2 on Pmod1). More...

#include <stdint.h>
#include "ra8_board_ek_ra8d2.h"
#include "ra8_boot_entry.h"
#include "ra8_cgc.h"
#include "ra8_err.h"
#include "ra8_gpio_constants.h"
#include "ra8_isr.h"
#include "ra8_port_constants.h"
#include "ra8_port_utils.h"
#include "ra8_sci_lin.h"
#include "ra8_time.h"
Include dependency graph for main.c:

Go to the source code of this file.

Enumerations

enum  lin_hil_config_t : uint32_t {
  k_lin_hil_console_baud = 115200U ,
  k_lin_hil_lin_baud = 19200U ,
  k_lin_hil_pclk_hz = 60000000U ,
  k_lin_hil_period_ms = 500U
}
 Compile-time settings for the LIN commander demo. More...
enum  lin_hil_frame_t : uint8_t {
  k_lin_hil_channel = 2U ,
  k_lin_hil_id_count = 4U ,
  k_lin_hil_data_len = 2U
}
 LIN channel + frame constants. More...
enum  lin_hil_break_t : uint16_t { k_lin_hil_break_len = 0x00A0U }
 Break-field length programmed into XCR2.BFLW. More...
enum  lin_hil_id_t : uint8_t {
  k_lin_hil_id_0 = 0x01U ,
  k_lin_hil_id_1 = 0x02U ,
  k_lin_hil_id_2 = 0x11U ,
  k_lin_hil_id_3 = 0x3CU
}
 The four rotating demo frame ids (arbitrary 6-bit LIN identifiers). More...
enum  lin_hil_payload_byte_t : uint8_t {
  k_lin_hil_pay_0 = 0xA5U ,
  k_lin_hil_pay_1 = 0x5AU
}
 The two-byte demo response payload bytes. More...

Functions

static void lin_hil_panic_halt (void)
 Halt forever in WFI – used as a panic stop on init failure.
static void lin_hil_setup_or_halt (void)
 Bring up CGC + SysTick + console + LED, route Pmod1, init LIN.
static ra8_err_t lin_hil_send_frame (uint8_t id)
 Send one LIN frame: header + published data response.
void main (void)
 Application entry.

Variables

static const uint8_t k_lin_hil_ids [k_lin_hil_id_count]
 Rotating demo frame ids (arbitrary 6-bit LIN identifiers).
static const uint8_t k_lin_hil_payload [k_lin_hil_data_len]
 Two-byte response payload published for every frame.
static const uint8_t k_lin_hil_banner [] = "lin_commander_hil: SCI2 LIN commander up\r\n"
 Banner + per-frame markers (must remain 7-bit ASCII).
static const uint8_t k_lin_hil_frame_msg [] = "lin_commander_hil: frame sent\r\n"
static const uint8_t k_lin_hil_fault_msg [] = "lin_commander_hil: LIN TX error\r\n"

Detailed Description

LIN commander frame-driver HIL demo for EK-RA8D2 (SCI2 on Pmod1).

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

Drives a Local Interconnect Network (LIN) commander on SCI2, whose TXD2 / RXD2 signals are broken out on the EK-RA8D2 Pmod1 header (J26, P801 = TXD2, P802 = RXD2). Once a period it emits one LIN frame – a header (break + SYNC 0x55 + protected identifier) followed by a published data response (payload bytes + enhanced checksum) – rotating through a small set of frame ids, and reports progress on the J-Link OB console (SCI8 @ 115200 8N1) while blinking LED1.

Sequence:

  1. ra8_cgc_init() – clock tree up (CPUCLK0 = 1 GHz, SCICLK routed).
  2. ra8_time_init() for the heartbeat delay.
  3. ra8_board_uart_console_init(115200) – SCI8 status console.
  4. ra8_board_led_init(LED1) for the visual heartbeat.
  5. Route Pmod1 TXD2 / RXD2 to SCI async via ra8_pfs_route_peripheral.
  6. ra8_sci_lin_init(SCI2, commander) – Simple-LIN break-field timer.
  7. Loop: send a header + response for a rotating id, print, blink, delay.
Hardware not yet validated (hw_pending)
LIN is a physical automotive bus: the SCI's TXD/RXD are logic-level UART, so a real LIN segment needs an external LIN transceiver (e.g. TJA1021 / MCP2003) between P801/P802 and the single-wire bus, plus at least one responder node to answer subscribe frames. Without that partner this app only exercises the COMMANDER transmit path: a logic analyzer on P801 (TXD2) can confirm the break / SYNC / PID / data / checksum framing at the UART level, but end-to-end bus behaviour (the responder detection path in ra8_sci_lin_wait_break / ra8_sci_lin_read_response) is proven only by the host unit tests, not on silicon. Promote to hw_validated only after a transceiver + partner run. Pmod1 shares pins with the Octo-SPI bus; set the board's Pmod1/OSPI mux (SW4) to the Pmod1 side before use.
Author
Brighton Sikarskie
Date
2026-07-09
Since
0.1.0

Definition in file main.c.

Enumeration Type Documentation

◆ lin_hil_break_t

enum lin_hil_break_t : uint16_t

Break-field length programmed into XCR2.BFLW.

The dominant break time is (BFLW + 1) x (TCLK / 16) (div16 timer clock). This illustrative value yields a break comfortably longer than the 13-bit-time LIN minimum at the demo baud; tune it to the actual SCICLK when validating on the bench.

Enumerator
k_lin_hil_break_len 

XCR2.BFLW break-field length.

Definition at line 82 of file main.c.

◆ lin_hil_config_t

enum lin_hil_config_t : uint32_t

Compile-time settings for the LIN commander demo.

Enumerator
k_lin_hil_console_baud 

SCI8 status-console baud.

k_lin_hil_lin_baud 

SCI2 LIN bus baud (<= 20 kbps).

k_lin_hil_pclk_hz 

PCLKB used for the LIN BRR calc.

k_lin_hil_period_ms 

Delay between LIN frames.

Definition at line 60 of file main.c.

◆ lin_hil_frame_t

enum lin_hil_frame_t : uint8_t

LIN channel + frame constants.

Enumerator
k_lin_hil_channel 

SCI2 (Pmod1 TXD2/RXD2) is the LIN bus.

k_lin_hil_id_count 

Number of rotating demo frame ids.

k_lin_hil_data_len 

Response payload length.

Definition at line 68 of file main.c.

◆ lin_hil_id_t

enum lin_hil_id_t : uint8_t

The four rotating demo frame ids (arbitrary 6-bit LIN identifiers).

Enumerator
k_lin_hil_id_0 

Demo frame id 0.

k_lin_hil_id_1 

Demo frame id 1.

k_lin_hil_id_2 

Demo frame id 2.

k_lin_hil_id_3 

Demo frame id 3.

Definition at line 87 of file main.c.

◆ lin_hil_payload_byte_t

enum lin_hil_payload_byte_t : uint8_t

The two-byte demo response payload bytes.

Enumerator
k_lin_hil_pay_0 

Response payload byte 0.

k_lin_hil_pay_1 

Response payload byte 1.

Definition at line 95 of file main.c.

Function Documentation

◆ lin_hil_panic_halt()

void lin_hil_panic_halt ( void )
static

Halt forever in WFI – used as a panic stop on init failure.

Precondition
Called only after a fatal error in boot.
Postcondition
CPU is parked; only a debugger or external reset wakes it.
Since
0.1.0

Definition at line 126 of file main.c.

Referenced by lin_hil_setup_or_halt(), and main().

◆ lin_hil_send_frame()

ra8_err_t lin_hil_send_frame ( uint8_t id)
static

Send one LIN frame: header + published data response.

Emits the break + SYNC + PID header for id and then publishes the demo payload with the enhanced (PID-folded) checksum, exercising the commander transmit path end-to-end.

Parameters
[in]id6-bit LIN frame identifier to drive.
Returns
ra8_err_tk_ra8_ok or the first driver error.
Return values
k_ra8_okHeader + response clocked out.
otherPropagated from the LIN driver.
Precondition
lin_hil_setup_or_halt has configured SCI2 as a LIN commander.
id is a 6-bit LIN identifier.
Postcondition
On success one complete LIN frame has been transmitted.
On failure no further bytes are pushed.
Since
0.1.0

Definition at line 216 of file main.c.

References k_lin_hil_channel, k_lin_hil_data_len, k_lin_hil_payload, k_ra8_ok, k_ra8_sci_lin_checksum_enhanced, ra8_sci_lin_pid(), ra8_sci_lin_send_header(), and ra8_sci_lin_send_response().

Referenced by main().

◆ lin_hil_setup_or_halt()

void lin_hil_setup_or_halt ( void )
static

Bring up CGC + SysTick + console + LED, route Pmod1, init LIN.

The LIN commander lives on SCI2; its TXD2 (P801) and RXD2 (P802) are routed to SCI async before ra8_sci_lin_init programs the Simple-LIN break-field timer. Any failure panic-halts so the fault is visible on a debugger.

Precondition
Reset_Handler has run; single-threaded boot context.
Postcondition
On return, SCI2 is a live LIN commander and the console is up.
Since
0.1.0

Definition at line 146 of file main.c.

References k_lin_hil_break_len, k_lin_hil_channel, k_lin_hil_console_baud, k_lin_hil_lin_baud, k_lin_hil_pclk_hz, k_ra8_board_led1, k_ra8_board_pmod1_uart_rxd, k_ra8_board_pmod1_uart_txd, k_ra8_clock_id_cpuclk0, k_ra8_ok, k_ra8_psel_sci_async, k_ra8_sci_data_8, k_ra8_sci_lin_clk_div16, k_ra8_sci_lin_role_commander, k_ra8_sci_parity_none, k_ra8_sci_stop_1, lin_hil_panic_halt(), ra8_board_led_init(), ra8_board_uart_console_init(), ra8_cgc_get_clock_hz(), ra8_cgc_init(), ra8_pfs_route_peripheral(), ra8_sci_lin_init(), and ra8_time_init().

Referenced by main().

◆ main()

void main ( void )

Application entry.

The application entry point Reset_Handler hands control to.

Brings up the LIN commander then drives frames.

Precondition
Reset_Handler has copied .data and zeroed .bss.
SystemInit has set VTOR, FPU, and priority grouping.
Postcondition
On clean entry the CPU stays in the LIN-frame + blink loop forever.
On any HAL init failure the function halts in WFI.
Since
0.1.0

Definition at line 239 of file main.c.

References k_lin_hil_banner, k_lin_hil_fault_msg, k_lin_hil_frame_msg, k_lin_hil_id_count, k_lin_hil_ids, k_lin_hil_period_ms, k_ra8_board_led1, k_ra8_ok, lin_hil_panic_halt(), lin_hil_send_frame(), lin_hil_setup_or_halt(), ra8_board_led_toggle(), ra8_board_uart_console_write(), ra8_delay_ms(), and ra8_isr_globals_enable().

Variable Documentation

◆ k_lin_hil_banner

const uint8_t k_lin_hil_banner[] = "lin_commander_hil: SCI2 LIN commander up\r\n"
static

Banner + per-frame markers (must remain 7-bit ASCII).

Definition at line 115 of file main.c.

Referenced by main().

◆ k_lin_hil_fault_msg

const uint8_t k_lin_hil_fault_msg[] = "lin_commander_hil: LIN TX error\r\n"
static

Definition at line 117 of file main.c.

Referenced by main().

◆ k_lin_hil_frame_msg

const uint8_t k_lin_hil_frame_msg[] = "lin_commander_hil: frame sent\r\n"
static

Definition at line 116 of file main.c.

Referenced by main().

◆ k_lin_hil_ids

const uint8_t k_lin_hil_ids[k_lin_hil_id_count]
static
Initial value:
= {
(uint8_t)k_lin_hil_id_0,
(uint8_t)k_lin_hil_id_1,
(uint8_t)k_lin_hil_id_2,
(uint8_t)k_lin_hil_id_3,
}
@ k_lin_hil_id_0
Demo frame id 0.
Definition main.c:88
@ k_lin_hil_id_2
Demo frame id 2.
Definition main.c:90
@ k_lin_hil_id_1
Demo frame id 1.
Definition main.c:89
@ k_lin_hil_id_3
Demo frame id 3.
Definition main.c:91

Rotating demo frame ids (arbitrary 6-bit LIN identifiers).

Definition at line 101 of file main.c.

Referenced by main().

◆ k_lin_hil_payload

const uint8_t k_lin_hil_payload[k_lin_hil_data_len]
static
Initial value:
= {
(uint8_t)k_lin_hil_pay_0,
(uint8_t)k_lin_hil_pay_1,
}
@ k_lin_hil_pay_1
Response payload byte 1.
Definition main.c:97
@ k_lin_hil_pay_0
Response payload byte 0.
Definition main.c:96

Two-byte response payload published for every frame.

Definition at line 109 of file main.c.

Referenced by lin_hil_send_frame().