ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
main.c
Go to the documentation of this file.
1
28
29#include <stdint.h>
30
31#include "ra8_agt.h"
32#include "ra8_agt_regs.h"
33#include "ra8_attributes.h"
34#include "ra8_board_ek_ra8d2.h"
35#include "ra8_boot_entry.h"
36#include "ra8_cgc.h"
37#include "ra8_err.h"
38#include "ra8_isr.h"
39#include "ra8_time.h"
40
42typedef enum : uint32_t {
43 k_agt_cas_baud = 115200U,
45 /* 32-bit virtual reload. Low 16 bits go to AGT0, high 16 bits to
46 * AGT1. Keep it small enough that AGT1 underflows within the HIL
47 * timeout but large enough to demonstrate the cascade chain. */
48 k_agt_cas_reload32 = 0x00017FFFU,
50
52typedef enum : uint32_t {
57
59typedef enum : uint8_t {
62
64static const char s_agt_cas_hex_chars[] = "0123456789ABCDEF";
65
67static const uint8_t s_agt_cas_msg_head[] = "agt_cas: tick=";
68static const uint8_t s_agt_cas_msg_tail[] = "\r\n";
69
87{
88 while (1) {
89 __asm__ volatile("wfi");
90 }
91}
92
111{
112 uint32_t cpuclk0_hz = 0U;
113 if (ra8_cgc_init() != k_ra8_ok) {
115 }
118 }
119 if (ra8_time_init(cpuclk0_hz) != k_ra8_ok) {
121 }
124 }
127 }
128}
129
150[[nodiscard]] RA8_INTERNAL static ra8_err_t internal_arm(void)
151{
152 const ra8_agt_cascade_cfg_t cfg = {
153 .reload32 = (uint32_t)k_agt_cas_reload32,
155 .on_underflow = nullptr,
156 .ctx = nullptr,
157 };
158 return ra8_agt_start_cascade(&cfg);
159}
160
180RA8_INTERNAL static void internal_format_tick(uint32_t tick, uint8_t* digits)
181{
182 for (uint32_t i = 0U; i < (uint32_t)k_agt_cas_hex_digits; ++i) {
183 const uint32_t shift =
184 ((uint32_t)k_agt_cas_hex_digits - 1U - i) * (uint32_t)k_agt_cas_hex_shift;
185 const uint32_t nib = (tick >> shift) & (uint32_t)k_agt_cas_hex_mask;
186 digits[i] = (uint8_t)s_agt_cas_hex_chars[nib];
187 }
188}
189
210[[nodiscard]] RA8_INTERNAL static ra8_err_t internal_emit_tick(uint32_t tick)
211{
212 uint8_t digits[k_agt_cas_hex_digits] = {};
213 internal_format_tick(tick, digits);
214 ra8_err_t err =
216 if (err != k_ra8_ok) {
217 return err;
218 }
219 err = ra8_board_uart_console_write(digits, (size_t)sizeof(digits));
220 if (err != k_ra8_ok) {
221 return err;
222 }
224 (size_t)(sizeof(s_agt_cas_msg_tail) - 1U));
225}
226
227void main(void)
228{
231
232 if (internal_arm() != k_ra8_ok) {
234 }
235
236 uint32_t tick = 0U;
237 while (1) {
238 uint8_t status = 0U;
239 if (ra8_agt_get_status((uint8_t)k_agt_cas_hi_channel, &status) != k_ra8_ok) {
240 break;
241 }
242 if ((status & (uint8_t)k_ra8_agt_agtcr_tundf_msk) != 0U) {
244 break;
245 }
246 ++tick;
247 if (internal_emit_tick(tick) != k_ra8_ok) {
248 break;
249 }
250 /* Stop both halves + re-arm to clear AGT1.AGTCR.TUNDF. */
251 if (ra8_agt_stop(0U) != k_ra8_ok) {
252 break;
253 }
254 if (ra8_agt_stop((uint8_t)k_agt_cas_hi_channel) != k_ra8_ok) {
255 break;
256 }
257 if (internal_arm() != k_ra8_ok) {
258 break;
259 }
260 }
262 }
264}
void main(void)
Secure fallback main entry point.
Definition main.c:37
static void internal_setup_or_halt(void)
Bring up clocks, MSTP, delay timing, and the board console.
Definition main.c:211
static void internal_panic_halt(void)
Park the core after an unrecoverable ADC demo failure.
Definition main.c:123
static ra8_err_t internal_arm(void)
Configure ADC_B in 12-bit software-trigger mode.
Definition main.c:179
static ra8_err_t internal_emit_tick(uint32_t tick)
Emit one cascade-tick banner over SCI8.
Definition main.c:210
static const uint8_t s_agt_cas_msg_tail[]
Definition main.c:68
agt_cas_chan_t
AGT1 = "high half" channel; its TUNDF flags a full cascade tick.
Definition main.c:59
@ k_agt_cas_hi_channel
AGT cas hi channel.
Definition main.c:60
agt_cas_fmt_t
Hex-digit width + mask for the per-tick banner formatter.
Definition main.c:52
@ k_agt_cas_hex_shift
AGT cas hex shift.
Definition main.c:54
@ k_agt_cas_hex_mask
AGT cas hex mask.
Definition main.c:55
@ k_agt_cas_hex_digits
AGT cas hex digits.
Definition main.c:53
static const uint8_t s_agt_cas_msg_head[]
Tick-line prefix and CR/LF tail.
Definition main.c:67
agt_cas_const_t
Demo tunables.
Definition main.c:42
@ k_agt_cas_poll_ms
AGT cas poll ms.
Definition main.c:44
@ k_agt_cas_reload32
AGT cas reload32.
Definition main.c:48
@ k_agt_cas_baud
AGT cas baud.
Definition main.c:43
static const char s_agt_cas_hex_chars[]
Hex print map for the tick counter.
Definition main.c:64
static void internal_format_tick(uint32_t tick, uint8_t *digits)
Render the 32-bit tick counter into an 8-byte ASCII-hex buf.
Definition main.c:180
Asynchronous General-Purpose Timer (AGT) driver header.
@ k_ra8_agt_cascade_clk_pclkb
TCK = 000b.
Definition ra8_agt.h:215
ra8_err_t ra8_agt_start_cascade(const ra8_agt_cascade_cfg_t *cfg)
Cascade AGT0 and AGT1 into a 32-bit virtual down-counter.
Definition ra8_agt.c:949
ra8_err_t ra8_agt_get_status(uint8_t channel, uint8_t *out_mask)
Read the AGTCR status register.
Definition ra8_agt.c:289
ra8_err_t ra8_agt_stop(uint8_t channel)
Stop an AGT channel.
Definition ra8_agt.c:177
Asynchronous General-Purpose Timer (AGT) register layout for RA8D2.
@ k_ra8_agt_agtcr_tundf_msk
Underflow flag.
Annotation-attribute framework macros for ra8-firmware.
#define RA8_INTERNAL
Marker that a function is intended to be static (file-local).
Board-support layer for the Renesas EK-RA8D2 v1 evaluation kit.
ra8_err_t ra8_board_led_toggle(ra8_board_led_id_t led)
Toggle led's output state.
ra8_err_t ra8_board_led_init(ra8_board_led_id_t led)
Configure led as a digital output, initial level low (off).
@ k_ra8_board_led1
LED1, BLUE, P600 (jumper E27).
ra8_err_t ra8_board_uart_console_write(const uint8_t *data, size_t len)
Polled blocking write to the J-Link OB VCOM console.
ra8_err_t ra8_board_uart_console_init(uint32_t baud)
Configure SCI8 + PD02/PD03 as the debug-console UART.
Boot entry points shared between a vector table and its startup code.
High-level Clock Generation Circuit driver.
ra8_err_t ra8_cgc_get_clock_hz(ra8_clock_id_t id, uint32_t *out_hz)
Query the current frequency of a clock-tree domain.
Definition ra8_cgc.c:132
@ k_ra8_clock_id_cpuclk0
Cortex-M85 CPUCLK0.
Definition ra8_cgc.h:70
ra8_err_t ra8_cgc_init(void)
Configure the clock tree to a safe default.
Definition ra8_cgc.c:727
Error Code Definitions for ra8-firmware.
@ k_ra8_ok
Success – operation completed with all postconditions satisfied.
Definition ra8_err.h:119
ra8_err_codes_t ra8_err_t
Canonical error-return type used by every ra8-firmware API.
Definition ra8_err.h:546
NVIC + ICU IELSR allocator.
void ra8_isr_globals_enable(void)
Globally enable maskable interrupts (PRIMASK = 0).
Definition ra8_isr.c:439
SysTick-based tick counter, delay and timestamp helpers.
ra8_err_t ra8_time_init(uint32_t cpu_hz)
Initialise SysTick for a 1 kHz tick interrupt.
Definition ra8_time.c:59
void ra8_delay_ms(uint32_t ms)
Busy-wait for at least ms milliseconds.
Definition ra8_time.c:129
Configuration for ra8_agt_start_cascade.
Definition ra8_agt.h:237