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

Minimal SEGGER RTT logging demo for EK-RA8D2. More...

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

Go to the source code of this file.

Data Structures

struct  rtt_buf_t
 One direction of an RTT channel (matches SEGGER layout). More...
struct  rtt_cb_t
 SEGGER RTT control block. More...

Enumerations

enum  rtt_demo_const_t : uint32_t {
  k_rtt_demo_period_ms = 1000U ,
  k_rtt_up_buf_bytes = 1024U ,
  k_rtt_down_buf_bytes = 32U ,
  k_rtt_decimal_base = 10U ,
  k_rtt_max_digits = 10U ,
  k_rtt_msg_buf_bytes = 32U
}
 Demo tunables. More...

Functions

static void internal_panic_halt (void)
 Park the core after an unrecoverable RTT demo failure.
static void internal_init (void)
 Initialise the RTT control block in place.
static ra8_err_t internal_write (const uint8_t *data, uint32_t len)
 Push len bytes into channel 0's up-buffer (non-blocking).
static uint8_t internal_format_line (uint32_t counter, char *buf)
 Format "rtt_log_demo: NNN\r\n" into buf.
static void internal_setup_or_halt (void)
 Bring CGC + SysTick + LED1 + RTT control block up.
void main (void)
 Publish a monotonically increasing RTT line once per second.

Variables

static uint8_t s_rtt_up_buf [k_rtt_up_buf_bytes]
 Up-buffer storage for channel 0.
static uint8_t s_rtt_down_buf [k_rtt_down_buf_bytes]
 Down-buffer storage for channel 0.
static rtt_cb_t s_rtt_cb
 The control block itself.

Detailed Description

Minimal SEGGER RTT logging demo for EK-RA8D2.

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

Allocates a SEGGER-compatible RTT control block in SRAM and writes a counter line into the up-buffer once per second. The on-board J-Link OB picks the block up by scanning RAM for the magic string "SEGGER RTT"; JLinkRTTViewer on the host then renders the stream live with no UART pin needed.

The control-block layout matches SEGGER_RTT.h (ring buffer with read/write indices). Channel 0 is the standard "Terminal" up-buffer. We keep a 1 KiB up-buffer and a 32 byte down-buffer (unused here).

Sequence:

  1. ra8_cgc_init – bring the chip up to the canonical clock tree.
  2. ra8_time_init – 1 ms SysTick.
  3. Initialise the RTT control block in BSS (idempotent).
  4. Loop: rtt_write a counter line, toggle LED1, sleep 1 s.
Since
0.1.0

Definition in file main.c.

Enumeration Type Documentation

◆ rtt_demo_const_t

enum rtt_demo_const_t : uint32_t

Demo tunables.

Enumerator
k_rtt_demo_period_ms 

Rtt demo period ms.

k_rtt_up_buf_bytes 

Rtt up buffer bytes.

k_rtt_down_buf_bytes 

Rtt down buffer bytes.

k_rtt_decimal_base 

Rtt decimal base.

k_rtt_max_digits 

Rtt maximum digits.

k_rtt_msg_buf_bytes 

Rtt message buffer bytes.

Definition at line 43 of file main.c.

Function Documentation

◆ internal_format_line()

uint8_t internal_format_line ( uint32_t counter,
char * buf )
static

Format "rtt_log_demo: NNN\r\n" into buf.

Copies the fixed prefix, converts the counter through a bounded reverse digit buffer, and appends CRLF without a NUL terminator.

Parameters
[in]counterNumber to render in base-10 ASCII.
[out]bufOutput buffer (>= k_rtt_msg_buf_bytes).
Returns
Number of bytes written.
Return values
17..26Complete line length, depending on counter width.
Precondition
buf points to at least k_rtt_msg_buf_bytes writable bytes.
counter is a complete 32-bit sample.
Postcondition
The returned prefix of buf contains one CRLF-terminated line.
No byte beyond the returned count is modified.
Note
Reentrant when callers provide distinct output buffers.
Since
0.1.0

Definition at line 199 of file main.c.

References k_rtt_decimal_base, k_rtt_max_digits, and RA8_INTERNAL.

◆ internal_init()

void internal_init ( void )
static

Initialise the RTT control block in place.

The id is written byte-by-byte (rather than as a string literal) so a search for "SEGGER RTT" in the firmware image only matches the runtime control block, not a stale literal pool entry.

Precondition
Called once before the first internal_write.
The file-scope control block and buffers occupy writable SRAM.
Postcondition
s_rtt_cb.id begins with S E G G E R sp R T T.
Both channel descriptors use empty, bounded caller-owned rings.
Note
Not thread-safe; initialization replaces every descriptor field.
Since
0.1.0

Definition at line 118 of file main.c.

References k_rtt_down_buf_bytes, k_rtt_up_buf_bytes, RA8_INTERNAL, s_rtt_cb, s_rtt_down_buf, and s_rtt_up_buf.

Referenced by internal_setup_or_halt().

◆ internal_panic_halt()

void internal_panic_halt ( void )
static

Park the core after an unrecoverable RTT demo failure.

Repeatedly executes WFI while preserving the ring and clock state.

Precondition
Called only from a fatal boot or terminal foreground path.
The caller does not require recovery without reset.
Postcondition
The core stays in WFI until external intervention.
No RTT producer index changes after entry.
Note
Not thread-safe; this is the terminal single-threaded path.
Since
0.1.0

Definition at line 95 of file main.c.

References RA8_INTERNAL.

◆ internal_setup_or_halt()

void internal_setup_or_halt ( void )
static

Bring CGC + SysTick + LED1 + RTT control block up.

Initializes clocks, the delay service, LED1, and the in-SRAM RTT descriptor in dependency order, halting on a failed HAL operation.

Precondition
Reset startup initialized static storage and the vector table.
Called once before global interrupt enable.
Postcondition
On return, channel zero is ready for non-blocking firmware writes.
LED1 and the millisecond delay service are ready.
Note
Not thread-safe; it owns the demo's global initialization.
Since
0.1.0

Definition at line 232 of file main.c.

References internal_init(), internal_panic_halt(), k_ra8_board_led1, k_ra8_clock_id_cpuclk0, k_ra8_ok, ra8_board_led_init(), ra8_cgc_get_clock_hz(), ra8_cgc_init(), RA8_INTERNAL, and ra8_time_init().

◆ internal_write()

ra8_err_t internal_write ( const uint8_t * data,
uint32_t len )
staticnodiscard

Push len bytes into channel 0's up-buffer (non-blocking).

MC/DC:
Compound decision: next == rd || size == 0. Two atomic conditions x N+1 = 3 vectors; the host test exercises full / empty / mid-buffer wrap independently.
Parameters
[in]dataBytes to enqueue.
[in]lenByte count; data fitting beyond head is dropped.
Returns
RTT producer status.
Return values
k_ra8_okBytes were appended (or dropped silently).
k_ra8_err_null_ptrdata was NULL.
Precondition
internal_init configured channel zero and its backing buffer.
The host owns only rd_off while this producer owns wr_off.
Postcondition
Successfully enqueued bytes precede the published write offset.
The producer never advances into the consumer's current read slot.
Note
Single-producer only; not thread-safe across firmware writers.
Since
0.1.0

Definition at line 163 of file main.c.

References rtt_buf_t::buf, k_ra8_err_null_ptr, k_ra8_ok, RA8_INTERNAL, rtt_buf_t::rd_off, s_rtt_cb, rtt_buf_t::size, and rtt_buf_t::wr_off.

Referenced by main().

◆ main()

void main ( void )

Publish a monotonically increasing RTT line once per second.

The application entry point Reset_Handler hands control to.

Initializes channel zero, formats and enqueues each counter line, toggles LED1, and delays before incrementing the next sample.

Precondition
Reset startup and SystemInit completed successfully.
J-Link may update only the consumer offset in the shared descriptor.
Postcondition
Each successful iteration advances the counter and toggles LED1.
Any producer or LED error leads to the terminal panic helper.
Note
Does not return during normal operation.
Since
0.1.0

Definition at line 261 of file main.c.

References internal_format_line(), internal_panic_halt(), internal_setup_or_halt(), internal_write(), k_ra8_board_led1, k_ra8_ok, k_rtt_demo_period_ms, k_rtt_msg_buf_bytes, ra8_board_led_toggle(), ra8_delay_ms(), and ra8_isr_globals_enable().

Variable Documentation

◆ s_rtt_cb

rtt_cb_t s_rtt_cb
static

The control block itself.

Definition at line 83 of file main.c.

Referenced by internal_init(), and internal_write().

◆ s_rtt_down_buf

uint8_t s_rtt_down_buf[k_rtt_down_buf_bytes]
static

Down-buffer storage for channel 0.

Definition at line 80 of file main.c.

Referenced by internal_init().

◆ s_rtt_up_buf

uint8_t s_rtt_up_buf[k_rtt_up_buf_bytes]
static

Up-buffer storage for channel 0.

Definition at line 77 of file main.c.

Referenced by internal_init().