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
33
34#include <stdint.h>
35
36#include "ra8_board_ek_ra8d2.h"
37#include "ra8_boot_entry.h"
38#include "ra8_box.h"
39#include "ra8_cgc.h"
40#include "ra8_err.h"
41#include "ra8_gfx.h"
42#include "ra8_gfx_font.h"
43#include "ra8_isr.h"
44#include "ra8_mstp.h"
45#include "ra8_time.h"
46#include "ra8_ui.h"
47
71
76typedef enum : uint32_t {
77 k_ch_col_page = 0xF4F0E8U,
78 k_ch_col_bar = 0x303860U,
79 k_ch_col_bar_tx = 0xFFFFFFU,
80 k_ch_col_cell = 0xC8D8F0U,
81 k_ch_col_edge = 0x283048U,
82 k_ch_col_ink = 0x101010U,
84
86static uint16_t s_framebuffer[(size_t)k_ch_fb_h * (size_t)k_ch_fb_w];
87
90
92static const char* s_label[k_ch_max_nodes];
93
95static const char* const k_ch_titles[k_ch_cell_count] = {"Frankenstein",
96 "Moby Dick",
97 "Walden",
98 "The Odyssey"};
99
100static const uint8_t k_msg_boot[] = "ereader-hil: boot\r\n";
101static const uint8_t k_msg_fail[] = "ereader-hil: FAIL init\r\n";
102static const uint8_t k_msg_pre[] = "ereader-hil: chrome boxes=";
103static const uint8_t k_msg_crc[] = " crc=";
104static const uint8_t k_msg_eol[] = "\r\n";
105
106/* ===========================================================================
107 * Console
108 * ===========================================================================
109 */
110
112static void ch_print(const uint8_t* msg, uint32_t len)
113{
114 (void)ra8_board_uart_console_write(msg, (size_t)len);
115}
116
118static void ch_panic_halt(void)
119{
120 ch_print(k_msg_fail, (uint32_t)sizeof(k_msg_fail) - 1U);
121 while (1) {
122 __asm__ volatile("wfi");
123 }
124}
125
127static void ch_print_hex(uint32_t value)
128{
129 uint8_t buf[k_ch_hex_nibbles];
130 for (uint32_t i = 0U; i < (uint32_t)k_ch_hex_nibbles; i++) {
131 const uint32_t shift = ((uint32_t)k_ch_hex_nibbles - 1U - i) * (uint32_t)k_ch_nibble_bits;
132 const uint32_t nib = (value >> shift) & (uint32_t)k_ch_nibble_mask;
133 buf[i] = (uint8_t)((nib < (uint32_t)k_ch_dec_ten) ? ('0' + nib) : ('A' + (nib - k_ch_dec_ten)));
134 }
135 ch_print(buf, (uint32_t)k_ch_hex_nibbles);
136}
137
139static void ch_print_uint(uint32_t value)
140{
141 uint8_t buf[k_ch_dec_ten];
142 uint32_t n = 0U;
143 if (value == 0U) {
144 buf[n] = '0';
145 n++;
146 }
147 while ((value > 0U) && (n < (uint32_t)k_ch_dec_ten)) {
148 buf[n] = (uint8_t)('0' + (value % (uint32_t)k_ch_dec_ten));
149 n++;
150 value /= (uint32_t)k_ch_dec_ten;
151 }
152 for (uint32_t i = 0U; i < n; i++) {
153 ch_print(&buf[n - 1U - i], 1U);
154 }
155}
156
157/* ===========================================================================
158 * Chrome build + render
159 * ===========================================================================
160 */
161
163static int16_t ch_build_chrome(ra8_box_tree_t* tree)
164{
165 for (uint32_t i = 0U; i < (uint32_t)k_ch_max_nodes; i++) {
166 s_label[i] = nullptr;
167 }
168 (void)ra8_box_tree_init(tree, s_box_nodes, (uint16_t)k_ch_max_nodes);
169 const ra8_box_t root_n = {.kind = (uint8_t)k_ra8_box_stack_v,
170 .pad = (int16_t)k_ch_root_pad,
171 .gap = (int16_t)k_ch_root_pad,
172 .flex = 1U,
173 .fill = (uint32_t)k_ch_col_page};
174 const int16_t root = ra8_box_add(tree, (int16_t)k_ra8_box_none, &root_n);
175
176 const ra8_box_t bar_n = {.kind = (uint8_t)k_ra8_box_leaf,
177 .fixed = (int16_t)k_ch_status_h,
178 .fill = (uint32_t)k_ch_col_bar};
179 const int16_t bar = ra8_box_add(tree, root, &bar_n);
180 s_label[bar] = "Library";
181
182 const ra8_box_t grid_n = {.kind = (uint8_t)k_ra8_box_grid,
183 .grid_cols = 2U,
184 .flex = 1U,
185 .pad = (int16_t)k_ch_grid_pad,
186 .gap = (int16_t)k_ch_grid_gap};
187 const int16_t grid = ra8_box_add(tree, root, &grid_n);
188
189 for (uint32_t i = 0U; i < (uint32_t)k_ch_cell_count; i++) {
190 const ra8_box_t cell_n = {.kind = (uint8_t)k_ra8_box_leaf,
191 .flex = 1U,
192 .pad = (int16_t)k_ch_cell_pad,
193 .fill = (uint32_t)k_ch_col_cell,
194 .border = (uint32_t)k_ch_col_edge,
195 .border_w = (int16_t)k_ch_border_w,
196 .tag = (int16_t)i};
197 const int16_t cell = ra8_box_add(tree, grid, &cell_n);
198 s_label[cell] = k_ch_titles[i];
199 }
200 return root;
201}
202
204static void ch_fill(const ra8_ui_rect_t* r, uint32_t color)
205{
206 for (int32_t row = 0; row < r->h; row++) {
207 (void)ra8_gfx_line(r->x, r->y + row, r->x + r->w - 1, r->y + row, color);
208 }
209}
210
212static void ch_border(const ra8_ui_rect_t* r, uint32_t color)
213{
214 const int32_t x1 = r->x + r->w - 1;
215 const int32_t y1 = r->y + r->h - 1;
216 (void)ra8_gfx_line(r->x, r->y, x1, r->y, color);
217 (void)ra8_gfx_line(r->x, y1, x1, y1, color);
218 (void)ra8_gfx_line(r->x, r->y, r->x, y1, color);
219 (void)ra8_gfx_line(x1, r->y, x1, y1, color);
220}
221
223static void ch_render(const ra8_box_tree_t* tree)
224{
225 (void)ra8_gfx_clear((uint32_t)k_ch_col_page);
226 for (uint16_t i = 0U; i < tree->count; i++) {
227 const ra8_box_t* b = &tree->nodes[i];
228 if (b->fill != (uint32_t)k_ra8_box_no_colour) {
229 ch_fill(&b->rect, b->fill);
230 }
231 if ((b->border != (uint32_t)k_ra8_box_no_colour) && (b->border_w > 0)) {
232 ch_border(&b->rect, b->border);
233 }
234 if (s_label[i] != nullptr) {
235 const uint32_t fg =
236 (b->fill == (uint32_t)k_ch_col_bar) ? (uint32_t)k_ch_col_bar_tx : (uint32_t)k_ch_col_ink;
237 const uint32_t bg =
238 (b->fill != (uint32_t)k_ra8_box_no_colour) ? b->fill : (uint32_t)k_ch_col_page;
239 (void)ra8_gfx_text_out(b->rect.x + (int16_t)k_ch_cell_pad,
240 b->rect.y + (int16_t)k_ch_cell_pad,
241 s_label[i],
243 fg,
244 bg);
245 }
246 }
247}
248
250static uint32_t ch_framebuffer_hash(void)
251{
252 const uint8_t* p = (const uint8_t*)s_framebuffer;
253 const size_t n = sizeof(s_framebuffer);
254 uint32_t hsh = (uint32_t)k_ch_fnv_offset;
255 for (size_t i = 0U; i < n; i++) {
256 hsh = (hsh ^ (uint32_t)p[i]) * (uint32_t)k_ch_fnv_prime;
257 }
258 return hsh;
259}
260
261/* ===========================================================================
262 * Boot + main
263 * ===========================================================================
264 */
265
267static void ch_setup_or_halt(void)
268{
269 uint32_t cpuclk0_hz = 0U;
270 if ((ra8_cgc_init() != k_ra8_ok) || (ra8_mstp_init() != k_ra8_ok)) {
272 }
275 }
276 if (ra8_time_init(cpuclk0_hz) != k_ra8_ok) {
278 }
281 }
282}
283
292void main(void)
293{
296 ch_print(k_msg_boot, (uint32_t)sizeof(k_msg_boot) - 1U);
297
299 (uint16_t)k_ch_fb_w,
300 (uint16_t)k_ch_fb_h,
303 }
304 ra8_box_tree_t tree = {};
305 const int16_t root = ch_build_chrome(&tree);
306 const ra8_ui_rect_t frame = {.x = 0, .y = 0, .w = (int32_t)k_ch_fb_w, .h = (int32_t)k_ch_fb_h};
307 (void)ra8_box_layout(&tree, root, &frame);
308 ch_render(&tree);
309
310 ch_print(k_msg_pre, (uint32_t)sizeof(k_msg_pre) - 1U);
311 ch_print_uint((uint32_t)tree.count);
312 ch_print(k_msg_crc, (uint32_t)sizeof(k_msg_crc) - 1U);
314 ch_print(k_msg_eol, (uint32_t)sizeof(k_msg_eol) - 1U);
315
316 while (1) {
317 __asm__ volatile("wfi");
318 }
319}
void main(void)
Secure fallback main entry point.
Definition main.c:37
static const uint8_t k_msg_pre[]
Definition main.c:151
static const uint8_t k_msg_crc[]
Definition main.c:152
static const uint8_t k_msg_boot[]
Definition main.c:140
static const uint8_t k_msg_fail[]
Definition main.c:63
static uint16_t s_framebuffer[(size_t) k_panel_height_px *(size_t) k_panel_width_px]
1024x600 RGB565 framebuffer in external SDRAM (GLCDC scans this).
Definition main.c:67
static const uint8_t k_msg_eol[]
Definition main.c:328
static void ch_render(const ra8_box_tree_t *tree)
Render the laid-out tree: per node, fill + border + label.
Definition main.c:223
static ra8_box_t s_box_nodes[k_ch_max_nodes]
Box-tree node storage.
Definition main.c:89
static void ch_print_hex(uint32_t value)
Print value as 8 uppercase hex digits + a trailing EOL.
Definition main.c:127
chrome_hil_cfg_t
Framebuffer + console + render geometry constants.
Definition main.c:52
@ k_ch_cell_pad
Cell text inset, px.
Definition main.c:62
@ k_ch_nibble_bits
Bits per hex nibble.
Definition main.c:67
@ k_ch_fb_h
Framebuffer height, pixels.
Definition main.c:54
@ k_ch_dec_ten
Hex digit / decimal split.
Definition main.c:69
@ k_ch_uart_baud
Console baud.
Definition main.c:55
@ k_ch_hex_nibbles
Hex digits in a 32-bit value.
Definition main.c:66
@ k_ch_root_pad
Root padding, px.
Definition main.c:59
@ k_ch_max_nodes
Box-tree node capacity.
Definition main.c:56
@ k_ch_grid_pad
Grid padding, px.
Definition main.c:60
@ k_ch_cell_count
Book cells in the grid.
Definition main.c:57
@ k_ch_fnv_prime
FNV-1a-32 prime.
Definition main.c:65
@ k_ch_nibble_mask
Low-nibble mask.
Definition main.c:68
@ k_ch_status_h
Status-bar fixed height, px.
Definition main.c:58
@ k_ch_border_w
Cell border width, px.
Definition main.c:63
@ k_ch_fb_w
Framebuffer width, pixels.
Definition main.c:53
@ k_ch_grid_gap
Grid inter-cell gap, px.
Definition main.c:61
@ k_ch_fnv_offset
FNV-1a-32 offset basis.
Definition main.c:64
static void ch_print_uint(uint32_t value)
Print a small unsigned integer in decimal.
Definition main.c:139
static const char * s_label[k_ch_max_nodes]
Per-node label (NULL = no text), indexed by box-tree node.
Definition main.c:92
static void ch_print(const uint8_t *msg, uint32_t len)
Emit a byte run on the SCI8 console.
Definition main.c:112
static const char *const k_ch_titles[k_ch_cell_count]
Demo shelf titles (public-domain).
Definition main.c:95
static void ch_fill(const ra8_ui_rect_t *r, uint32_t color)
Fill a rectangle by drawing h horizontal spans.
Definition main.c:204
chrome_hil_color_t
Chrome palette (0xRRGGBB; ra8_gfx down-converts to RGB565).
Definition main.c:76
@ k_ch_col_edge
Cell border (dark navy).
Definition main.c:81
@ k_ch_col_page
Page / root background.
Definition main.c:77
@ k_ch_col_bar
Status-bar fill (navy).
Definition main.c:78
@ k_ch_col_ink
Cell label (near-black).
Definition main.c:82
@ k_ch_col_bar_tx
Status-bar text (white).
Definition main.c:79
@ k_ch_col_cell
Cell fill (light blue).
Definition main.c:80
static int16_t ch_build_chrome(ra8_box_tree_t *tree)
Build the status-bar-over-grid chrome tree; return the root index.
Definition main.c:163
static void ch_border(const ra8_ui_rect_t *r, uint32_t color)
Draw a 1-pixel border around a rectangle.
Definition main.c:212
static uint32_t ch_framebuffer_hash(void)
FNV-1a-32 over the rendered framebuffer bytes.
Definition main.c:250
static void ch_panic_halt(void)
Park forever in WFI after a fatal init error.
Definition main.c:118
static void ch_setup_or_halt(void)
Bring up clocks/MSTP/time + the SCI8 console; halt on failure.
Definition main.c:267
Board-support layer for the Renesas EK-RA8D2 v1 evaluation kit.
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.
Bounded, allocation-free box-model layout for e-reader chrome.
ra8_err_t ra8_box_tree_init(ra8_box_tree_t *tree, ra8_box_t *storage, uint16_t cap)
Bind a tree builder to caller-owned node storage.
Definition ra8_box.c:284
ra8_err_t ra8_box_layout(ra8_box_tree_t *tree, int16_t root, const ra8_ui_rect_t *frame)
Lay out the tree, filling every node's rect.
Definition ra8_box.c:331
@ k_ra8_box_no_colour
Carried fill/border: absent.
Definition ra8_box.h:58
@ k_ra8_box_none
No child / no sibling link.
Definition ra8_box.h:57
int16_t ra8_box_add(ra8_box_tree_t *tree, int16_t parent, const ra8_box_t *node)
Append a node and link it as a child of parent.
Definition ra8_box.c:297
@ k_ra8_box_stack_v
Vertical stack: children stack top->bottom.
Definition ra8_box.h:66
@ k_ra8_box_grid
Fixed-column grid, row-major.
Definition ra8_box.h:68
@ k_ra8_box_leaf
Terminal box (no children laid out).
Definition ra8_box.h:69
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
Software 2D graphics primitives layered on top of a caller-ownedframebuffer (DRW / D/AVE 2D / GLCDC r...
@ k_ra8_gfx_format_rgb565
16-bit RGB565, little-endian in memory.
Definition ra8_gfx.h:40
ra8_err_t ra8_gfx_line(int32_t x0, int32_t y0, int32_t x1, int32_t y1, uint32_t color)
Draw a line from (x0,y0) to (x1,y1) with Bresenham's algorithm.
ra8_err_t ra8_gfx_clear(uint32_t color)
Fill the bound framebuffer's clip region with a single colour.
ra8_err_t ra8_gfx_text_out(int32_t x, int32_t y, const char *str, const ra8_gfx_font_t *font, uint32_t fg_color, uint32_t bg_color)
Render a NUL-terminated ASCII string.
ra8_err_t ra8_gfx_init(void *fb, uint16_t width, uint16_t height, ra8_gfx_format_t format)
Bind ra8_gfx to a caller-owned framebuffer.
Bitmap font descriptor + bundled font handles for ra8_gfx.
const ra8_gfx_font_t ra8_gfx_font_8x16
Bundled 8x16 IBM PC VGA bitmap font, ASCII 0x20..0x7E.
NVIC + ICU IELSR allocator.
void ra8_isr_globals_enable(void)
Globally enable maskable interrupts (PRIMASK = 0).
Definition ra8_isr.c:439
Ref-counted Module Stop Control wrapper for the RA8D2.
ra8_err_t ra8_mstp_init(void)
Re-establish the ra8_mstp ref-count table from current hardware state.
Definition ra8_mstp.c:291
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
Bounded UI interaction core: hit-testing, screen stack, paging.
One node in a box tree (caller-owned).
Definition ra8_box.h:90
Append-only builder over caller-owned node storage.
Definition ra8_box.h:115
ra8_box_t * nodes
Caller storage.
Definition ra8_box.h:116
uint16_t count
Nodes used.
Definition ra8_box.h:118
Axis-aligned rectangle in framebuffer pixel coordinates.
Definition ra8_ui.h:72
int32_t w
Width (pixels, >= 0).
Definition ra8_ui.h:75
int32_t h
Height (pixels, >= 0).
Definition ra8_ui.h:76
int32_t x
Left edge (pixels).
Definition ra8_ui.h:73
int32_t y
Top edge (pixels).
Definition ra8_ui.h:74