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
38
39#include <stddef.h>
40#include <stdint.h>
41
42#include "epub.h"
43#include "epub_cover_fixture.h"
44#include "ra8_board_ek_ra8d2.h"
45#include "ra8_boot_entry.h"
46#include "ra8_cgc.h"
47#include "ra8_err.h"
48#include "ra8_gfx.h"
49#include "ra8_isr.h"
50#include "ra8_mstp.h"
51#include "ra8_time.h"
52#include "reflow_image.h"
53
55typedef enum : uint32_t {
56 k_ec_uart_baud = 115200U,
57 k_ec_fb_w = 160U,
58 k_ec_fb_h = 120U,
59 k_ec_arena_bytes = 128U * 1024U,
60 k_ec_cover_cap = 64U * 1024U,
61 k_ec_col_bg = 0x202028U,
62 k_ec_fnv_offset = 0x811C9DC5U,
63 k_ec_fnv_prime = 0x01000193U,
69
73static uint8_t s_cover[k_ec_cover_cap];
75static uint16_t s_framebuffer[(size_t)k_ec_fb_h * (size_t)k_ec_fb_w];
78
79static const uint8_t k_msg_boot[] = "ereader-cover-hil: boot\r\n";
80static const uint8_t k_msg_fail[] = "ereader-cover-hil: FAIL init\r\n";
81static const uint8_t k_msg_open[] = "ereader-cover-hil: FAIL open\r\n";
82static const uint8_t k_msg_cov[] = "ereader-cover-hil: FAIL cover\r\n";
83static const uint8_t k_msg_derr[] = "ereader-cover-hil: FAIL decode\r\n";
84static const uint8_t k_msg_pre[] = "ereader-cover-hil: cover ";
85static const uint8_t k_msg_x[] = "x";
86static const uint8_t k_msg_crc[] = " crc=";
87static const uint8_t k_msg_ok[] = " PASS\r\n";
88
90static void ec_print(const uint8_t* msg, uint32_t len)
91{
92 (void)ra8_board_uart_console_write(msg, (size_t)len);
93}
94
96static void ec_panic_halt(const uint8_t* msg, uint32_t len)
97{
98 ec_print(msg, len);
99 __asm__ volatile("bkpt #0");
100 while (1) {
101 __asm__ volatile("wfi");
102 }
103}
104
106static uint32_t ec_framebuffer_hash(void)
107{
108 const uint8_t* p = (const uint8_t*)s_framebuffer;
109 const size_t n = sizeof(s_framebuffer);
110 uint32_t hsh = (uint32_t)k_ec_fnv_offset;
111 for (size_t i = 0U; i < n; i++) {
112 hsh = (hsh ^ (uint32_t)p[i]) * (uint32_t)k_ec_fnv_prime;
113 }
114 return hsh;
115}
116
118static void ec_print_hex(uint32_t value)
119{
120 uint8_t buf[k_ec_hex_nibbles];
121 for (uint32_t i = 0U; i < (uint32_t)k_ec_hex_nibbles; i++) {
122 const uint32_t shift = ((uint32_t)k_ec_hex_nibbles - 1U - i) * (uint32_t)k_ec_nibble_bits;
123 const uint32_t nib = (value >> shift) & (uint32_t)k_ec_nibble_mask;
124 buf[i] = (uint8_t)((nib < (uint32_t)k_ec_dec_ten) ? ('0' + nib) : ('A' + (nib - k_ec_dec_ten)));
125 }
126 ec_print(buf, (uint32_t)k_ec_hex_nibbles);
127}
128
130static void ec_print_uint(uint32_t value)
131{
132 uint8_t buf[k_ec_dec_ten];
133 uint32_t n = 0U;
134 if (value == 0U) {
135 buf[n] = '0';
136 n++;
137 }
138 while ((value > 0U) && (n < (uint32_t)k_ec_dec_ten)) {
139 buf[n] = (uint8_t)('0' + (value % (uint32_t)k_ec_dec_ten));
140 n++;
141 value /= (uint32_t)k_ec_dec_ten;
142 }
143 for (uint32_t i = 0U; i < n; i++) {
144 ec_print(&buf[n - 1U - i], 1U);
145 }
146}
147
149static void ec_setup_or_halt(void)
150{
151 uint32_t cpuclk0_hz = 0U;
152 if ((ra8_cgc_init() != k_ra8_ok) || (ra8_mstp_init() != k_ra8_ok)) {
153 ec_panic_halt(k_msg_fail, (uint32_t)sizeof(k_msg_fail) - 1U);
154 }
156 ec_panic_halt(k_msg_fail, (uint32_t)sizeof(k_msg_fail) - 1U);
157 }
158 if (ra8_time_init(cpuclk0_hz) != k_ra8_ok) {
159 ec_panic_halt(k_msg_fail, (uint32_t)sizeof(k_msg_fail) - 1U);
160 }
162 ec_panic_halt(k_msg_fail, (uint32_t)sizeof(k_msg_fail) - 1U);
163 }
164}
165
182static size_t ec_render_cover_or_halt(int32_t* out_w, int32_t* out_h)
183{
184 const epub_mem_media_t media = {.data = k_epub_cover_fixture,
185 .size = (size_t)k_epub_cover_fixture_len};
186 if (epub_open(&media, "book.epub", &s_book) != k_ra8_ok) {
187 ec_panic_halt(k_msg_open, (uint32_t)sizeof(k_msg_open) - 1U);
188 }
189 size_t cover_len = 0U;
190 if ((epub_get_cover_image(&s_book, s_cover, (size_t)k_ec_cover_cap, &cover_len) != k_ra8_ok) ||
191 (cover_len == 0U)) {
192 ec_panic_halt(k_msg_cov, (uint32_t)sizeof(k_msg_cov) - 1U);
193 }
194
195 ra8_img_arena_t arena = {.base = s_img_arena,
196 .cap = (size_t)k_ec_arena_bytes,
197 .offset = 0U,
198 .live = 0U};
199 if (ra8_img_decode_blit(&arena,
200 s_cover,
201 cover_len,
202 0,
203 0,
204 (int32_t)k_ec_fb_w,
205 (int32_t)k_ec_fb_h,
206 out_w,
207 out_h) != k_ra8_ok) {
208 ec_panic_halt(k_msg_derr, (uint32_t)sizeof(k_msg_derr) - 1U);
209 }
210 return cover_len;
211}
212
221void main(void)
222{
225 ec_print(k_msg_boot, (uint32_t)sizeof(k_msg_boot) - 1U);
226
228 (uint16_t)k_ec_fb_w,
229 (uint16_t)k_ec_fb_h,
231 ec_panic_halt(k_msg_fail, (uint32_t)sizeof(k_msg_fail) - 1U);
232 }
233 (void)ra8_gfx_clear((uint32_t)k_ec_col_bg);
234
235 int32_t out_w = 0;
236 int32_t out_h = 0;
237 (void)ec_render_cover_or_halt(&out_w, &out_h);
238
239 ec_print(k_msg_pre, (uint32_t)sizeof(k_msg_pre) - 1U);
240 ec_print_uint((uint32_t)out_w);
241 ec_print(k_msg_x, (uint32_t)sizeof(k_msg_x) - 1U);
242 ec_print_uint((uint32_t)out_h);
243 ec_print(k_msg_crc, (uint32_t)sizeof(k_msg_crc) - 1U);
245 ec_print(k_msg_ok, (uint32_t)sizeof(k_msg_ok) - 1U);
246
247 while (1) {
248 __asm__ volatile("wfi");
249 }
250}
void main(void)
Secure fallback main entry point.
Definition main.c:37
EPUB (.epub) reader and chapter iterator for ra8-firmware.
ra8_err_t epub_get_cover_image(epub_book_t *book, uint8_t *out_buf, size_t max_len, size_t *got_len)
Copy the raw bytes of the cover image into the caller's buffer.
ra8_err_t epub_open(const void *media, const char *path, epub_book_t *out_book)
Open an EPUB book from an opaque media handle.
Definition epub_open.c:405
Baked minimal EPUB3 with a real PNG cover (cover-image manifest item).
static const uint8_t k_epub_cover_fixture[k_epub_cover_fixture_len]
Baked cover-art EPUB3 byte stream.
@ k_epub_cover_fixture_len
EPUB cover fixture length.
static epub_book_t s_book
Opened book (large – file-scope, not on the stack).
Definition main.c:132
static const uint8_t k_msg_pre[]
Definition main.c:151
static const uint8_t k_msg_ok[]
Definition main.c:153
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_open[]
Definition main.c:64
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_cov[]
Definition main.c:70
static uint8_t s_img_arena[k_cm_arena_bytes]
Zero-heap bump arena in SDRAM backing the PNG page decode.
Definition main.c:180
static void ec_print_uint(uint32_t value)
Print a small unsigned integer in decimal.
Definition main.c:130
static void ec_print_hex(uint32_t value)
Print a 32-bit value as 8 upper-case hex digits.
Definition main.c:118
static const uint8_t k_msg_derr[]
Definition main.c:83
static uint32_t ec_framebuffer_hash(void)
FNV-1a hash over the rendered framebuffer.
Definition main.c:106
static size_t ec_render_cover_or_halt(int32_t *out_w, int32_t *out_h)
Open the EPUB, extract the cover, decode+scale+blit it; return size.
Definition main.c:182
static void ec_setup_or_halt(void)
Bring up clocks/MSTP/time + the SCI8 console; halt on failure.
Definition main.c:149
static void ec_print(const uint8_t *msg, uint32_t len)
Emit a byte run on the SCI8 console.
Definition main.c:90
static const uint8_t k_msg_x[]
Definition main.c:85
static void ec_panic_halt(const uint8_t *msg, uint32_t len)
Print the fail banner and trap (ra8_emulator halts on the BKPT).
Definition main.c:96
ec_consts_t
Console / render knobs (no magic numbers).
Definition main.c:55
@ k_ec_nibble_bits
Bits per hex nibble.
Definition main.c:65
@ k_ec_uart_baud
Console baud.
Definition main.c:56
@ k_ec_col_bg
Framebuffer clear colour.
Definition main.c:61
@ k_ec_nibble_mask
Low-nibble mask.
Definition main.c:66
@ k_ec_hex_nibbles
Hex digits in a 32-bit value.
Definition main.c:64
@ k_ec_fnv_prime
FNV-1a 32-bit prime.
Definition main.c:63
@ k_ec_cover_cap
Cover-image read buffer capacity.
Definition main.c:60
@ k_ec_fnv_offset
FNV-1a 32-bit offset basis.
Definition main.c:62
@ k_ec_fb_w
Framebuffer width, pixels.
Definition main.c:57
@ k_ec_fb_h
Framebuffer height, pixels.
Definition main.c:58
@ k_ec_arena_bytes
Image decode scratch, bytes.
Definition main.c:59
@ k_ec_dec_ten
Hex digit / decimal split.
Definition main.c:67
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.
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_clear(uint32_t color)
Fill the bound framebuffer's clip region with a single colour.
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.
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
Zero-heap raster image decode + scale + blit for reflow (#106).
ra8_err_t ra8_img_decode_blit(ra8_img_arena_t *arena, const uint8_t *bytes, size_t len, int32_t dst_x, int32_t dst_y, int32_t box_w, int32_t box_h, int32_t *out_w, int32_t *out_h)
Decode bytes and blit it, scaled to fit, into the bound framebuffer.
static uint8_t s_cover[k_sh_cover_cap]
Definition sh_book.c:56
Opened EPUB book.
Definition epub.h:286
In-memory EPUB media descriptor.
Definition epub.h:179
Caller-owned bump arena backing a single image decode.