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
29
30#include <stddef.h>
31#include <stdint.h>
32#include <string.h>
33
34#include "ra8_attributes.h"
35#include "ra8_board_ek_ra8d2.h"
36#include "ra8_boot_entry.h"
37#include "ra8_cgc.h"
38#include "ra8_err.h"
39#include "ra8_isr.h"
40#include "ra8_time.h"
41
51
55typedef struct {
56 const char* name;
57 uint8_t* buf;
58 uint32_t size;
59 uint32_t wr_off;
60 uint32_t rd_off;
61 uint32_t flags;
62} rtt_buf_t;
63
68typedef struct {
69 char id[16];
70 uint32_t max_up;
71 uint32_t max_down;
74} rtt_cb_t;
75
78
81
84
96{
97 while (1) {
98 __asm__ volatile("wfi");
99 }
100}
101
119{
120 static const char k_magic[] = {'S', 'E', 'G', 'G', 'E', 'R', ' ', 'R', 'T', 'T', 0};
121 for (size_t i = 0U; i < sizeof k_magic; i++) {
122 s_rtt_cb.id[i] = k_magic[i];
123 }
124 s_rtt_cb.max_up = 1U;
125 s_rtt_cb.max_down = 1U;
126 s_rtt_cb.up[0].name = "Terminal";
127 s_rtt_cb.up[0].buf = s_rtt_up_buf;
128 s_rtt_cb.up[0].size = (uint32_t)k_rtt_up_buf_bytes;
129 s_rtt_cb.up[0].wr_off = 0U;
130 s_rtt_cb.up[0].rd_off = 0U;
131 s_rtt_cb.up[0].flags = 0U;
132 s_rtt_cb.down[0].name = "Terminal";
133 s_rtt_cb.down[0].buf = s_rtt_down_buf;
134 s_rtt_cb.down[0].size = (uint32_t)k_rtt_down_buf_bytes;
135 s_rtt_cb.down[0].wr_off = 0U;
136 s_rtt_cb.down[0].rd_off = 0U;
137 s_rtt_cb.down[0].flags = 0U;
138}
139
163[[nodiscard]] RA8_INTERNAL static ra8_err_t internal_write(const uint8_t* data, uint32_t len)
164{
165 if (data == nullptr) {
166 return k_ra8_err_null_ptr;
167 }
168 rtt_buf_t* up = &s_rtt_cb.up[0];
169 for (uint32_t i = 0U; i < len; i++) {
170 uint32_t next = up->wr_off + 1U;
171 if (next == up->size) {
172 next = 0U;
173 }
174 if (next == up->rd_off) {
175 break; /* host has not drained -- drop the tail. */
176 }
177 up->buf[up->wr_off] = data[i];
178 up->wr_off = next;
179 }
180 return k_ra8_ok;
181}
182
199RA8_INTERNAL static uint8_t internal_format_line(uint32_t counter, char* buf)
200{
201 static const char prefix[] = "rtt_log_demo: ";
202 uint8_t n = 0U;
203 for (size_t k = 0U; k < (sizeof prefix - 1U); k++) {
204 buf[n++] = prefix[k];
205 }
206 char dig[k_rtt_max_digits];
207 uint8_t d = 0U;
208 uint32_t tmp = counter;
209 do {
210 dig[d++] = (char)('0' + (uint8_t)(tmp % (uint32_t)k_rtt_decimal_base));
211 tmp /= (uint32_t)k_rtt_decimal_base;
212 } while (tmp != 0U && d < (uint8_t)k_rtt_max_digits);
213 while (d > 0U) {
214 buf[n++] = dig[--d];
215 }
216 buf[n++] = '\r';
217 buf[n++] = '\n';
218 return n;
219}
220
233{
234 uint32_t cpuclk0_hz = 0U;
235 if (ra8_cgc_init() != k_ra8_ok) {
237 }
240 }
241 if (ra8_time_init(cpuclk0_hz) != k_ra8_ok) {
243 }
246 }
248}
249
261void main(void)
262{
265
266 uint32_t counter = 0U;
267 while (1) {
268 char buf[k_rtt_msg_buf_bytes];
269 const uint8_t n = internal_format_line(counter, buf);
270 if (internal_write((const uint8_t*)buf, (uint32_t)n) != k_ra8_ok) {
271 break;
272 }
274 break;
275 }
276 counter++;
278 }
280}
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 uint32_t internal_format_line(uint8_t *out, bool enabled, uint32_t trig_count)
Format the per-cycle "elc: en=N trig=NN\r\n" line.
Definition main.c:204
static rtt_cb_t s_rtt_cb
The control block itself.
Definition main.c:83
static void internal_init(void)
Initialise the RTT control block in place.
Definition main.c:118
static uint8_t s_rtt_up_buf[k_rtt_up_buf_bytes]
Up-buffer storage for channel 0.
Definition main.c:77
static uint8_t s_rtt_down_buf[k_rtt_down_buf_bytes]
Down-buffer storage for channel 0.
Definition main.c:80
rtt_demo_const_t
Demo tunables.
Definition main.c:43
@ k_rtt_demo_period_ms
Rtt demo period ms.
Definition main.c:44
@ k_rtt_down_buf_bytes
Rtt down buffer bytes.
Definition main.c:46
@ k_rtt_msg_buf_bytes
Rtt message buffer bytes.
Definition main.c:49
@ k_rtt_max_digits
Rtt maximum digits.
Definition main.c:48
@ k_rtt_decimal_base
Rtt decimal base.
Definition main.c:47
@ k_rtt_up_buf_bytes
Rtt up buffer bytes.
Definition main.c:45
static ra8_err_t internal_write(const uint8_t *data, uint32_t len)
Push len bytes into channel 0's up-buffer (non-blocking).
Definition main.c:163
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).
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
@ k_ra8_err_null_ptr
Pointer was NULL where a valid pointer was required.
Definition ra8_err.h:478
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
One direction of an RTT channel (matches SEGGER layout).
Definition main.c:55
uint8_t * buf
Backing ring buffer.
Definition main.c:57
uint32_t rd_off
Read index (consumer = host).
Definition main.c:60
const char * name
NUL-terminated channel name.
Definition main.c:56
uint32_t size
Bytes of buf.
Definition main.c:58
uint32_t wr_off
Write index (producer).
Definition main.c:59
uint32_t flags
0 = no-block-skip, default.
Definition main.c:61
SEGGER RTT control block.
Definition main.c:68
uint32_t max_down
Number of down channels.
Definition main.c:71
rtt_buf_t down[1]
Down channel array (host TX).
Definition main.c:73
uint32_t max_up
Number of up channels.
Definition main.c:70
rtt_buf_t up[1]
Up channel array (host RX).
Definition main.c:72