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

SPI_B internal-loopback HIL test for the EK-RA8D2. More...

#include <stdint.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_isr.h"
#include "ra8_mstp.h"
#include "ra8_spi.h"
#include "ra8_time.h"
Include dependency graph for main.c:

Go to the source code of this file.

Enumerations

enum  spi_demo_const_t : uint32_t {
  k_spi_demo_baud = 115200U ,
  k_spi_demo_period_ms = 1000U ,
  k_spi_demo_spi_baud_hz = 1000000U ,
  k_spi_demo_spi_channel = 0U
}
 App-wide tunables. More...
enum  spi_demo_byte_t : uint8_t {
  k_spi_demo_pattern_len = 16U ,
  k_spi_demo_pattern_base = 0xA0U
}
 Test pattern width and seed. More...

Functions

static void internal_panic_halt (void)
 Park forever after a fatal initialization failure.
static void internal_clocks_or_halt (uint32_t *out_pclka)
 Bring CGC, MSTP and SysTick up.
static void internal_setup_or_halt (void)
 Bring CGC + SysTick + console + SPI_B up.
static bool internal_round_trip_ok (void)
 Walk the test pattern once and return whether RX matches TX.
void main (void)
 Application entry.

Variables

static const uint8_t s_spi_demo_msg_pass [] = "spi: pass\r\n"
static const uint8_t s_spi_demo_msg_fail [] = "spi: FAIL\r\n"

Detailed Description

SPI_B internal-loopback HIL test for the EK-RA8D2.

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

Standalone EVM-tier app that exercises the SPI_B controller driver (libs/ra8_hal/inc/ra8_spi.h) via the silicon's internal-loopback mode – no external CIPO/COPI jumper required. The flow:

  1. ra8_cgc_init – bring CPUCLK0 / PCLKA up.
  2. ra8_mstp_init + ra8_board_uart_console_init for the SCI8 J-Link console (PD02 / PD03).
  3. ra8_spi_init(0, .loopback=true, ...) at 1 MHz mode-0 MSB-first. The HAL programmes SPCR2.SPLP2=1 BEFORE asserting SPCR.SPE so the write is honored (HUM Ch 43.2.4 p 2889 – SPCR2 writes require SPE=0). SPLP2 is the non-inverting loopback variant (rx = tx); the inverting SPLP would flip every bit. No external CIPO/COPI wiring needed – the silicon ties COPI back to CIPO internally.
  4. Walk a 16-byte test pattern (0xA0..0xAF) through
  5. ra8_spi_xfer8 and compare RX vs TX byte-for-byte. LED1 toggles on each successful round-trip; LED2 latches ON if any byte mismatches.

SCI8 prints "spi: pass\r\n" once a second.

Bare EK-RA8D2 only – no shields or external loopback wiring.

Since
0.1.0

Definition in file main.c.

Enumeration Type Documentation

◆ spi_demo_byte_t

enum spi_demo_byte_t : uint8_t

Test pattern width and seed.

Enumerator
k_spi_demo_pattern_len 

SPI demo pattern length.

k_spi_demo_pattern_base 

SPI demo pattern base.

Definition at line 58 of file main.c.

◆ spi_demo_const_t

enum spi_demo_const_t : uint32_t

App-wide tunables.

Enumerator
k_spi_demo_baud 

SPI demo baud.

k_spi_demo_period_ms 

SPI demo period ms.

k_spi_demo_spi_baud_hz 

SPI demo SPI baud Hz.

k_spi_demo_spi_channel 

SPI demo SPI channel.

Definition at line 50 of file main.c.

Function Documentation

◆ internal_clocks_or_halt()

void internal_clocks_or_halt ( uint32_t * out_pclka)
static

Bring CGC, MSTP and SysTick up.

Returns PCLKA Hz via out_pclka.

Initializes the canonical clock tree, samples CPUCLK0 and PCLKA, ungates peripheral modules, and starts the millisecond timebase.

Parameters
[out]out_pclkaReceives the measured peripheral clock frequency.
Precondition
Reset_Handler has finished C-runtime init.
out_pclka points to writable caller-owned storage.
Postcondition
CGC, MSTP and SysTick are live; *out_pclka holds PCLKA in Hz.
Any failed dependency transfers control to internal_panic_halt.
Note
Not thread-safe; it mutates global clock and module-stop state.
Since
0.1.0

Definition at line 98 of file main.c.

References internal_panic_halt(), k_ra8_clock_id_cpuclk0, k_ra8_clock_id_pclka, k_ra8_ok, ra8_cgc_get_clock_hz(), ra8_cgc_init(), RA8_INTERNAL, ra8_mstp_init(), and ra8_time_init().

◆ internal_panic_halt()

void internal_panic_halt ( void )
static

Park forever after a fatal initialization failure.

Repeatedly executes WFI so an attached debugger can inspect the failed clock or SPI state without further bus traffic.

Precondition
Called only after a fatal error in boot.
The caller does not require recovery without reset.
Postcondition
CPU is parked; only a debugger or external reset wakes it.
No further SPI or console transfer is requested.
Note
Not thread-safe; this is the terminal single-threaded path.
Since
0.1.0

Definition at line 78 of file main.c.

References RA8_INTERNAL.

◆ internal_round_trip_ok()

bool internal_round_trip_ok ( void )
static

Walk the test pattern once and return whether RX matches TX.

Sends the deterministic 0xA0..0xAF sequence one byte at a time and stops at the first HAL error or non-identical loopback byte.

Returns
true when every byte read back matches the byte sent.
Return values
trueAll pattern bytes completed and matched.
falseA transfer failed or a received byte differed.
Precondition
ra8_spi_init succeeded and SPLP is set.
No other context is using SPI_B channel zero.
Postcondition
No external bus traffic occurred (loopback is internal).
The SPI channel remains configured for the next round.
Note
Not thread-safe with concurrent SPI channel access.
Since
0.1.0

Definition at line 177 of file main.c.

References k_ra8_ok, k_spi_demo_pattern_base, k_spi_demo_pattern_len, k_spi_demo_spi_channel, RA8_INTERNAL, and ra8_spi_xfer8().

Referenced by main().

◆ internal_setup_or_halt()

void internal_setup_or_halt ( void )
static

Bring CGC + SysTick + console + SPI_B up.

Panic-halts on fail.

Passes cfg.loopback = true to ra8_spi_init so the HAL programmes SPCR2.SPLP2=1 while SPE is still 0 (HUM Ch 43.2.4 p 2889). The silicon then ties COPI to CIPO internally; no external loopback jumper required.

Precondition
Reset startup initialized static storage and the vector table.
Called once before global interrupt enable.
Postcondition
On return, SPI_B channel zero runs in internal non-inverting loopback.
SCI8 and both result LEDs are ready for the foreground loop.
Note
Not thread-safe; it owns the demo's peripheral configuration.
Since
0.1.0

Definition at line 134 of file main.c.

References internal_clocks_or_halt(), internal_panic_halt(), k_ra8_board_led1, k_ra8_board_led2, k_ra8_ok, k_ra8_spi_mode_0, k_spi_demo_baud, k_spi_demo_spi_baud_hz, k_spi_demo_spi_channel, ra8_board_led_init(), ra8_board_uart_console_init(), RA8_INTERNAL, and ra8_spi_init().

◆ main()

void main ( void )

Application entry.

The application entry point Reset_Handler hands control to.

Brings up SPI_B + loops a self-test pattern.

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 loopback + blink loop.
On any HAL hard error LED2 latches ON.
Since
0.1.0

Definition at line 203 of file main.c.

References internal_panic_halt(), internal_round_trip_ok(), internal_setup_or_halt(), k_ra8_board_led1, k_ra8_board_led2, k_ra8_ok, k_spi_demo_period_ms, ra8_board_led_on(), ra8_board_led_toggle(), ra8_board_uart_console_write(), ra8_delay_ms(), ra8_isr_globals_enable(), s_spi_demo_msg_fail, and s_spi_demo_msg_pass.

Variable Documentation

◆ s_spi_demo_msg_fail

const uint8_t s_spi_demo_msg_fail[] = "spi: FAIL\r\n"
static

Definition at line 64 of file main.c.

Referenced by main().

◆ s_spi_demo_msg_pass

const uint8_t s_spi_demo_msg_pass[] = "spi: pass\r\n"
static

Definition at line 63 of file main.c.

Referenced by main().