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
42
43#include <stdint.h>
44#include <string.h>
45
46#include "literata_latin1.h"
47#include "ra8_board_ek_ra8d2.h"
48#include "ra8_boot_entry.h"
49#include "ra8_cgc.h"
50#include "ra8_display_pal.h"
51#include "ra8_display_pal_lcd.h"
52#include "ra8_err.h"
53#include "ra8_gfx.h"
54#include "ra8_isr.h"
55#include "ra8_mstp.h"
56#include "ra8_panel.h"
57#include "ra8_panel_timing.h"
58#include "ra8_sdfont.h"
59#include "ra8_sdramc.h"
60#include "ra8_time.h"
61#include "reflow.h"
62
63/* ===========================================================================
64 * Compile-time configuration -- typed enums per the no-magic-number rule.
65 * =========================================================================== */
66
72
74typedef enum : uint16_t {
78
80typedef enum : uint32_t {
81 k_sfr_font_cap = 512U * 1024U,
85 k_sfr_paper_argb = 0xFFFFFFFFU,
86 k_sfr_ink_argb = 0xFF101010U,
87 k_sfr_link_argb = 0xFF2A52BEU,
90
100
115
121
128static const char k_sfr_body[] = "<html><body><h1>SD font OK</h1>"
129 "<p>Read off the card.</p></body></html>";
130
131/* ===========================================================================
132 * Static storage
133 * =========================================================================== */
134
137
139[[gnu::section(".sdram_data")]] static uint8_t s_font_buf[k_sfr_font_cap];
140
143
147 .framebuffer = s_framebuffer,
148 .framebuffer_bytes = sizeof(s_framebuffer),
149 .width_px = (uint16_t)k_sfr_fb_w,
150 .height_px = (uint16_t)k_sfr_fb_h,
151 .pixfmt = k_display_pixfmt_rgb565,
152 .panel_timing = &s_ra8_panel_ek_ra8d2_timing,
153};
154
156static display_handle_t* s_display = nullptr;
157
160
162static uint32_t s_pclka_hz = 0U;
163
164/* ---- J-Link / ra8_emulator readable diagnostics ----------------------------- */
165
167volatile uint32_t g_sfr_stage = k_sfr_stage_boot;
169volatile uint32_t g_sfr_font_len = 0U;
171volatile uint32_t g_sfr_pages = 0U;
173volatile uint32_t g_sfr_ink = 0U;
175volatile uint32_t g_sfr_source = 0U;
177volatile uint32_t g_sfr_err = 0U;
179volatile uint32_t g_sfr_heartbeat = 0U;
180
181/* ===========================================================================
182 * Boot helpers
183 * =========================================================================== */
184
190static void sfr_panic_halt(uint32_t stage)
191{
192 g_sfr_stage = stage;
194 const uint8_t lvl =
195 (uint8_t)((uint32_t)k_sfr_diag_lvl_base +
196 ((stage & (uint32_t)k_sfr_diag_lvl_mask) * (uint32_t)k_sfr_diag_lvl_step));
197 const uint32_t argb = (uint32_t)k_sfr_diag_argb_opaque |
198 ((uint32_t)lvl << (uint32_t)k_sfr_diag_shift_r) |
199 ((uint32_t)lvl << (uint32_t)k_sfr_diag_shift_g) | (uint32_t)lvl;
200 (void)ra8_gfx_clear(argb);
201 while (1) {
202 __asm__ volatile("wfi");
203 }
204}
205
233
254
262static void sfr_load_font_or_halt(void)
263{
264 const ra8_sdfont_cfg_t cfg = {
265 .spi_channel = (uint8_t)k_sfr_spi_chan,
266 .sck = k_sfr_pin_sck,
267 .cipo = k_sfr_pin_cipo,
268 .copi = k_sfr_pin_copi,
269 .cs = k_sfr_pin_cs,
270 .pclka_hz = s_pclka_hz,
271 .filename = "FONT.OTF",
272 .provision_blob = g_ra8_font_literata_latin1,
273 .provision_len = g_ra8_font_literata_latin1_len,
274 };
275 uint32_t got = 0U;
277 const ra8_err_t err = ra8_sdfont_load(&cfg, s_font_buf, (uint32_t)k_sfr_font_cap, &got, &src);
278 g_sfr_err = (uint32_t)err;
279 if (err != k_ra8_ok) {
281 }
282 g_sfr_font_len = got;
283 g_sfr_source = (uint32_t)src;
285}
286
288static void sfr_render_or_halt(void)
289{
290 if (reflow_init((uint16_t)k_sfr_fb_w,
291 (uint16_t)k_sfr_fb_h,
294 (uint16_t)k_sfr_font_px,
295 (uint32_t)k_sfr_ink_argb,
296 (uint32_t)k_sfr_link_argb,
297 &s_engine) != k_ra8_ok) {
299 }
300 uint32_t pages = 0U;
302 (const uint8_t*)k_sfr_body,
303 (uint32_t)(sizeof(k_sfr_body) - 1U),
304 &pages) != k_ra8_ok) {
306 }
307 g_sfr_pages = pages;
309 if (reflow_render_page(&s_engine, 0U, nullptr) != k_ra8_ok) {
311 }
312
313 /* Count inked pixels so a HIL probe / emulator can prove text actually landed. */
314 uint32_t ink = 0U;
315 const uint16_t paper565 = (uint16_t)k_sfr_paper_rgb565;
316 for (size_t i = 0U; i < (size_t)k_sfr_fb_w * (size_t)k_sfr_fb_h; i++) {
317 if (s_framebuffer[i] != paper565) {
318 ink++;
319 }
320 }
321 g_sfr_ink = ink;
324}
325
326/* ===========================================================================
327 * Main
328 * =========================================================================== */
329
330void main(void)
331{
336
337 /* The page is rendered; GLCDC scans out the framebuffer continuously.
338 * ra8_emulator snapshots it via --ppm. Idle with a heartbeat that advances only
339 * on this success path -- the panic-halt loop does not bump it -- so a J-Link
340 * memprobe gate proves the full SD-font render pipeline ran end to end. */
341 while (1) {
342 ra8_delay_ms((uint32_t)k_sfr_frame_ms);
344 }
345}
void main(void)
Secure fallback main entry point.
Definition main.c:37
static uint32_t s_pclka_hz
Definition c6_cam_app.c:38
static display_handle_t * s_display
Definition main.c:78
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 reflow_t s_engine
Reflow engine (glyph / page / token / text pools live inside).
Definition main.c:199
static uint8_t s_font_buf[k_pc_font_cap]
Font blob read off the card.
Definition main.c:197
static display_fb_t s_fb
Definition main.c:203
static void sfr_render_or_halt(void)
Reflow the body XHTML with the SD font into the framebuffer.
Definition main.c:288
static void sfr_bringup_clocks(void)
Clocks, MSTP, SysTick, LEDs, then enable IRQs.
Definition main.c:207
static const ra8_port_pin_t k_sfr_pin_sck
Pmod2 SPI pins (J25) – SCI0 Simple-SPI per HUM Table 20.13.
Definition main.c:117
volatile uint32_t g_sfr_ink
Inked (non-paper) pixel count after render – liveness proof.
Definition main.c:173
sfr_fb_dim_t
Framebuffer dimensions, sourced from the BSP panel descriptor.
Definition main.c:68
@ k_sfr_fb_w
Framebuffer width (pixels).
Definition main.c:69
@ k_sfr_fb_h
Framebuffer height (pixels).
Definition main.c:70
static const ra8_port_pin_t k_sfr_pin_cipo
Definition main.c:118
static void sfr_panic_halt(uint32_t stage)
Halt forever with the red board LED on (fatal init error marker).
Definition main.c:190
sfr_reflow_cfg_t
Reflow + font-load tunables.
Definition main.c:80
@ k_sfr_paper_rgb565
White paper in RGB565 (all bits set).
Definition main.c:88
@ k_sfr_ink_argb
Body text colour (near-black ink).
Definition main.c:86
@ k_sfr_spi_chan
Pmod2 / J25 = SCI0 Simple-SPI.
Definition main.c:83
@ k_sfr_font_cap
Max font we will read off the card.
Definition main.c:81
@ k_sfr_spi_idle
Idle byte clocked on read-only xfer.
Definition main.c:84
@ k_sfr_font_px
Body font size in pixels.
Definition main.c:82
@ k_sfr_link_argb
Anchor text colour (cerulean).
Definition main.c:87
@ k_sfr_paper_argb
Page background the FB is cleared to.
Definition main.c:85
static void sfr_bringup_panel(void)
External SDRAM + GLCDC panel + ra8_gfx binding.
Definition main.c:235
volatile uint32_t g_sfr_source
Font provenance (ra8_sdfont_source_t): 0 = on-card, 1 = provisioned.
Definition main.c:175
sfr_fb_misc_t
Framebuffer + pacing byte-math constants.
Definition main.c:74
@ k_sfr_settle_ms
PLL / SDRAM / panel-POR settle.
Definition main.c:75
@ k_sfr_frame_ms
Idle heartbeat pacing (ms).
Definition main.c:76
static const ra8_port_pin_t k_sfr_pin_copi
Definition main.c:119
volatile uint32_t g_sfr_err
Last ra8_err_t from ra8_sdfont_load (0 = k_ra8_ok), for SWD triage.
Definition main.c:177
static const ra8_port_pin_t k_sfr_pin_cs
Definition main.c:120
volatile uint32_t g_sfr_font_len
Bytes of font read off the card.
Definition main.c:169
sfr_stage_t
Diagnostic stage codes stamped to g_sfr_stage (J-Link / emulator).
Definition main.c:102
@ k_sfr_stage_boot
Pre-init.
Definition main.c:103
@ k_sfr_stage_panel_ok
SDRAM + GLCDC + ra8_gfx up.
Definition main.c:104
@ k_sfr_stage_mount_fail
FAT mount failed.
Definition main.c:111
@ k_sfr_stage_mount_ok
ra8_fs mounted the FAT volume.
Definition main.c:106
@ k_sfr_stage_layout_ok
reflow laid out the chapter.
Definition main.c:108
@ k_sfr_stage_reflow_fail
Layout/render failed.
Definition main.c:113
@ k_sfr_stage_card_fail
SD bring-up failed.
Definition main.c:110
@ k_sfr_stage_card_ok
ra8_sdmmc_spi init succeeded.
Definition main.c:105
@ k_sfr_stage_render_ok
Page rasterised to the FB.
Definition main.c:109
@ k_sfr_stage_font_fail
Font open/read failed.
Definition main.c:112
@ k_sfr_stage_font_ok
Font read off the card.
Definition main.c:107
sfr_diag_fill_t
Constants for the stage-coded gray panic fill (diagnostic only).
Definition main.c:92
@ k_sfr_diag_shift_g
Green byte position in ARGB.
Definition main.c:98
@ k_sfr_diag_shift_r
Red byte position in ARGB.
Definition main.c:97
@ k_sfr_diag_lvl_step
Gray step per stage code.
Definition main.c:95
@ k_sfr_diag_lvl_mask
Low-nibble stage selector.
Definition main.c:96
@ k_sfr_diag_argb_opaque
Opaque-alpha bits of an ARGB fill.
Definition main.c:93
@ k_sfr_diag_lvl_base
Darkest panic gray level.
Definition main.c:94
static void sfr_load_font_or_halt(void)
Load FONT.OTF off the Pmod2 card (self-provisioning) into s_font_buf.
Definition main.c:262
volatile uint32_t g_sfr_heartbeat
Idle-loop heartbeat; advances only after a clean render (HIL gate).
Definition main.c:179
volatile uint32_t g_sfr_pages
Pages produced by reflow_layout_chapter.
Definition main.c:171
volatile uint32_t g_sfr_stage
Last stage reached (sfr_stage_t); read externally over SWD.
Definition main.c:167
static const display_cfg_t k_sfr_display_cfg
Display PAL config – LCD/GLCDC backend over the SDRAM buffer.
Definition main.c:145
static const char k_sfr_body[]
XHTML body the app reflows with the SD-loaded font.
Definition main.c:128
Declaration of the Latin-1 Literata font baked into internal flash.
const unsigned int g_ra8_font_literata_latin1_len
Length of g_ra8_font_literata_latin1 in bytes.
const unsigned char g_ra8_font_literata_latin1[]
Baked Latin-1 subset of Literata Regular (TrueType/glyf), in flash.
Board-support layer for the Renesas EK-RA8D2 v1 evaluation kit.
@ k_ra8_board_pmod2_spi_cipo
Pmod2.3 CIPO (CIPO0_B), P602.
@ k_ra8_board_pmod2_spi_cs
Pmod2.1 CS (SS0_B), P604.
@ k_ra8_board_pmod2_spi_copi
Pmod2.2 COPI (COPI0_B), P603.
@ k_ra8_board_pmod2_spi_sck
Pmod2.4 SCK (SCK0_B), P601.
ra8_err_t ra8_board_led_init(ra8_board_led_id_t led)
Configure led as a digital output, initial level low (off).
ra8_err_t ra8_board_led_on(ra8_board_led_id_t led)
Drive led HIGH (light it).
@ k_ra8_board_led_blue
Convenience aliases by colour.
@ k_ra8_board_led_red
RA8 board led red.
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
@ k_ra8_clock_id_pclka
PCLKA.
Definition ra8_cgc.h:73
ra8_err_t ra8_cgc_init(void)
Configure the clock tree to a safe default.
Definition ra8_cgc.c:727
Display Platform Abstraction Layer for the RA8D2.
@ k_display_pixfmt_rgb565
16 bpp, 5/6/5 packed.
ra8_err_t display_get_framebuffer(const display_handle_t *d, display_fb_t *out)
Return the framebuffer descriptor the backend is targeting.
struct display_handle display_handle_t
ra8_err_t display_init(const display_cfg_t *cfg, display_handle_t **out_handle)
Initialise the display PAL and bring the selected backend up to a state where display_flush succeeds.
LCD backend (ra8_glcdc) for the display PAL.
const display_backend_iface_t k_display_backend_lcd_ra8_glcdc
LCD backend vtable – pass its address through display_cfg_t.iface to drive the EK-RA8D2 panel.
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
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
Active display-panel descriptor for the EK-RA8D2 (ER-TFT070-6).
@ k_panel_height_px
Native height (pixels).
Definition ra8_panel.h:52
@ k_panel_width_px
Native width (pixels).
Definition ra8_panel.h:51
#define RA8_BOARD_PANEL_FRAMEBUFFER(name)
Declare a correctly aligned, sized and placed RGB565 framebuffer for the on-board panel.
Definition ra8_panel.h:165
EK-RA8D2 panel timing as the HAL's ra8_glcdc_timing_t (ER-TFT070-6).
static const ra8_glcdc_timing_t s_ra8_panel_ek_ra8d2_timing
EK-RA8D2 ER-TFT070-6 RGB timing for ra8_glcdc_init / the LCD backend.
ra8_port_pin_t
Packed (port << 8) | pin pin identifier.
Load a TTF/OTF font off a Pmod SD card, self-provisioning if absent.
ra8_err_t ra8_sdfont_load(const ra8_sdfont_cfg_t *cfg, uint8_t *buf, uint32_t cap, uint32_t *out_len, ra8_sdfont_source_t *out_source)
Mount the Pmod SD card and load a font, provisioning it if absent.
Definition ra8_sdfont.c:392
ra8_sdfont_source_t
Provenance of the font bytes returned by ra8_sdfont_load.
Definition ra8_sdfont.h:70
@ k_ra8_sdfont_source_card
File already on the card; read as-is.
Definition ra8_sdfont.h:71
External SDRAM controller driver (framework).
ra8_err_t ra8_sdramc_init(void)
Initialise the external SDRAM at k_ra8_sdram_base_addr.
Definition ra8_sdramc.c:257
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
HTML / CSS reflow + paginate engine for the ra8d2 ereader.
ra8_err_t reflow_render_page(const reflow_t *engine, uint32_t page_idx, void *framebuffer)
Render one page into the active ra8_gfx framebuffer.
ra8_err_t reflow_layout_chapter(reflow_t *engine, const uint8_t *xhtml_buf, size_t xhtml_len, uint32_t *out_total_pages)
Parse + lay out one chapter of XHTML.
ra8_err_t reflow_init(uint16_t viewport_w, uint16_t viewport_h, const uint8_t *font_data, size_t font_len, uint16_t font_px, uint32_t body_color, uint32_t link_color, reflow_t *out_engine)
Initialise a reflow engine for a given viewport / font.
Configuration descriptor passed into display_init.
Framebuffer descriptor returned by display_get_framebuffer.
SD-card font-store bus description + provisioning policy.
Definition ra8_sdfont.h:92
Reflow / pagination engine state.