ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
main.c File Reference

Viewable tap-to-zoom image viewer: full-screen zoom + panel loupe (#478). More...

#include <stddef.h>
#include <stdint.h>
#include "ez_scene.h"
#include "ra8_board_ek_ra8d2.h"
#include "ra8_board_ek_ra8d2_touch.h"
#include "ra8_boot_entry.h"
#include "ra8_cgc.h"
#include "ra8_display_pal.h"
#include "ra8_display_pal_lcd.h"
#include "ra8_err.h"
#include "ra8_gfx.h"
#include "ra8_isr.h"
#include "ra8_mstp.h"
#include "ra8_panel.h"
#include "ra8_panel_timing.h"
#include "ra8_sdramc.h"
#include "ra8_time.h"
#include "ra8_touch.h"
Include dependency graph for main.c:

Go to the source code of this file.

Enumerations

enum  ez_boot_t : uint32_t {
  k_ez_uart_baud = 115200U ,
  k_ez_settle_ms = 500U ,
  k_ez_frame_ms = 25U ,
  k_ez_led_every = 16U ,
  k_ez_touch_max_points = 1U
}
 Boot, console and touch knobs. More...
enum  ez_fb_t : uint16_t {
  k_ez_fb_w = k_panel_width_px ,
  k_ez_fb_h = k_panel_height_px
}
 Framebuffer geometry (mirrors the EK-RA8D2 panel). More...
enum  ez_print_t : uint8_t {
  k_ez_hex_nibbles = 8U ,
  k_ez_nibble_bits = 4U ,
  k_ez_nibble_mask = 0x0FU ,
  k_ez_dec_ten = 10U
}
 Decimal / hexadecimal formatting constants for the banner. More...

Functions

 RA8_BOARD_PANEL_FRAMEBUFFER (s_framebuffer)
static void ez_print (const uint8_t *msg, uint32_t len)
 Emit a byte run on the console.
static void ez_panic_halt (const uint8_t *msg, uint32_t len)
 Print a fail banner, then trap (ra8_emulator halts on the BKPT).
static void ez_print_hex (uint32_t value)
 Print a 32-bit value as 8 upper-case hex digits.
static void ez_print_uint (uint32_t value)
 Print a small unsigned integer in decimal.
static void ez_bringup_clocks (void)
 Bring up clocks, MSTP, system tick, LEDs and the SCI console.
static void ez_bringup_panel (void)
 Bring up SDRAM + the GLCDC panel, then bind ra8_gfx to the FB.
static void ez_bringup_touch (void)
 Open the GT911 touch controller (best-effort, polled).
static void ez_flush (const ez_present_t *plan)
 Flush a scene plan through the PAL with the waveform it asked for.
static bool ez_redraw (void)
 Render, then flush whatever the scene says changed.
static void ez_print_banner (const ez_selftest_t *st)
 Print the deterministic boot banner (geometry + the four hashes).
static void ez_poll_touch (bool *was_touching)
 Poll the GT911 once; on a fresh tap, mutate + redraw the viewport.
static bool ez_setup_scene (void)
 Wire the scene over the SDRAM cache storage and the SRAM scratch.
void main (void)
 App entry: panel bring-up -> tiled page -> self-check -> zoom loop.

Variables

static uint8_t s_cell_mem [(size_t) k_ez_cells *(size_t) k_ez_cell_bytes]
 Tile-cache cell storage: k_ez_cells gray8 256x256 tiles, in SDRAM.
static ra8_keycache_cell_t s_meta [k_ez_cells]
 Tile-cache link metadata, one entry per cell.
static ra8_tile_key_t s_keys [k_ez_cells]
 Tile-cache key storage, one entry per cell.
static ra8_tile_dims_t s_dims [k_ez_cells]
 Tile-cache decoded-extent descriptors, one entry per cell.
static int32_t s_buckets [k_ez_buckets]
 Tile-cache hash-bucket heads.
static uint8_t s_row [k_ez_row_bytes]
 Composite scratch: one source row at the widest viewport (SRAM).
static uint8_t s_strip [k_ez_strip_bytes]
 Composite scratch: the gray8 destination strip (SRAM).
static uint8_t s_packed [k_ez_packed_bytes]
 Composite scratch: the dithered strip packed to 4 bpp (SRAM).
static const display_cfg_t k_ez_display_cfg
 Display PAL configuration: LCD/GLCDC backend over the SDRAM framebuffer.
static display_handle_ts_display = nullptr
 Display PAL handle returned by display_init.
static display_fb_t s_fb
 Mutable copy of the framebuffer descriptor, populated at boot.
static ez_scene_t s_scene
 The demo scene: tiled page, zoom viewport and loupe.
static const uint8_t k_msg_boot [] = "ereader-zoom: boot\r\n"
 Console banner fragment: boot line.
static const uint8_t k_msg_fail [] = "ereader-zoom: FAIL init\r\n"
 Console banner fragment: bring-up failure.
static const uint8_t k_msg_scene [] = "ereader-zoom: FAIL scene\r\n"
 Console banner fragment: scene wiring failure.
static const uint8_t k_msg_rend [] = "ereader-zoom: FAIL render\r\n"
 Console banner fragment: render / self-check failure.
static const uint8_t k_msg_pre [] = "ereader-zoom: page "
 Console banner fragment: geometry prefix.
static const uint8_t k_msg_x [] = "x"
 Console banner fragment: dimension separator.
static const uint8_t k_msg_tile [] = " tile "
 Console banner fragment: tile edge label.
static const uint8_t k_msg_cells [] = " cells "
 Console banner fragment: cache cell-count label.
static const uint8_t k_msg_z1 [] = " z1="
 Console banner fragment: 1:1 render hash label.
static const uint8_t k_msg_pan [] = " pan="
 Console banner fragment: panned render hash label.
static const uint8_t k_msg_z2 [] = " z2="
 Console banner fragment: 2x render hash label.
static const uint8_t k_msg_lens [] = " lens="
 Console banner fragment: loupe render hash label.
static const uint8_t k_msg_hit [] = " hit="
 Console banner fragment: cache hit counter label.
static const uint8_t k_msg_miss [] = " miss="
 Console banner fragment: cache miss counter label.
static const uint8_t k_msg_evict [] = " evict="
 Console banner fragment: cache eviction counter label.
static const uint8_t k_msg_warm [] = " warm="
 Console banner fragment: prefetch counter label.
static const uint8_t k_msg_ok [] = " ok\r\n"
 Console banner fragment: success suffix.

Detailed Description

Viewable tap-to-zoom image viewer: full-screen zoom + panel loupe (#478).

Tag
[Ring 7 / App] {World: NS}

The other half of the decision not to downscale .rabook images: keep every pixel at import, and let the reader magnify into them. This app makes that viewable. It brings up the 1024x600 GLCDC panel, binds a 4096x3072 gray8 page – 12 MiB, an order of magnitude past what the part can hold – through an ::ra8_tile_cache, and drives it with the zoom viewport engine:

  1. Boot clocks / MSTP / SysTick / LEDs, then SDRAM and the GLCDC panel (ra8_display_pal with k_display_backend_lcd_ra8_glcdc), then bind ra8_gfx to the RGB565 framebuffer in SDRAM.
  2. Wire the tile cache (decode-on-miss = the procedural page sampler) and open two viewports over it: the full content area, and a 320x320 loupe.
  3. Run the scripted boot self-check – 1:1, one pan step, 2x about a fixed panel point, then the 4x loupe – printing a framebuffer hash for each plus the cache's own residency counters.
  4. Main loop: poll the GT911, route the tap to a zone, re-render, and flush only what changed with the waveform zoom asks for – A2 during an interactive burst, GC16 once the gesture settles.

The banner is deterministic because every stage is integer arithmetic (the sampler is shifts and masks, the composite is nearest-neighbour plus a const blue-noise mask, and the chrome is filled rectangles rather than antialiased glyphs), so it is identical on host, ra8_emulator and silicon:

ereader-zoom: page 4096x3072 tile 256 cells 35 z1=<8hex> pan=<8hex> z2=<8hex> lens=<8hex> hit=<n> miss=<n> evict=<n> warm=<n> ok

Since
0.1.0

Definition in file main.c.

Enumeration Type Documentation

◆ ez_boot_t

enum ez_boot_t : uint32_t

Boot, console and touch knobs.

Copied from the sibling reader demos so every viewable app on this board brings the console and the GT911 up the same way.

Invariant
k_ez_frame_ms is short enough that a tap is never missed.
Example:
@ k_ez_uart_baud
Console baud.
Definition main.c:74
ra8_err_t ra8_board_uart_console_init(uint32_t baud)
Configure SCI8 + PD02/PD03 as the debug-console UART.
See also
main
Since
0.1.0
Enumerator
k_ez_uart_baud 

Console baud.

k_ez_settle_ms 

PLL / SDRAM / panel-POR settle.

k_ez_frame_ms 

Input poll period, ms.

k_ez_led_every 

Heartbeat LED toggle sub-cadence.

k_ez_touch_max_points 

One contact = one tap.

Definition at line 73 of file main.c.

◆ ez_fb_t

enum ez_fb_t : uint16_t

Framebuffer geometry (mirrors the EK-RA8D2 panel).

Held separately from the panel constants so the scene's layout asserts have compile-time numbers to check against.

Invariant
k_ra8_board_fb_align_bytes is a multiple of the AXI burst length.
Example:
static uint16_t s_framebuffer[(size_t)k_ez_fb_h * (size_t)k_ez_fb_w];
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
@ k_ez_fb_h
Framebuffer height, pixels.
Definition main.c:96
@ k_ez_fb_w
Framebuffer width, pixels.
Definition main.c:95
See also
main
Since
0.1.0
Enumerator
k_ez_fb_w 

Framebuffer width, pixels.

k_ez_fb_h 

Framebuffer height, pixels.

Definition at line 94 of file main.c.

◆ ez_print_t

enum ez_print_t : uint8_t

Decimal / hexadecimal formatting constants for the banner.

The banner is emitted without printf (no float, no heap), so the radix and nibble geometry are named here rather than inlined.

Invariant
k_ez_nibble_mask == (1 << k_ez_nibble_bits) - 1.
Example:
ez_print_hex(st.crc_1x);
static void ez_print_hex(uint32_t value)
Print a 32-bit value as 8 upper-case hex digits.
Definition main.c:314
See also
ez_print_banner
Since
0.1.0
Enumerator
k_ez_hex_nibbles 

Hex digits in a 32-bit value.

k_ez_nibble_bits 

Bits per hex nibble.

k_ez_nibble_mask 

Low-nibble mask.

k_ez_dec_ten 

Decimal radix.

Definition at line 112 of file main.c.

Function Documentation

◆ ez_bringup_clocks()

void ez_bringup_clocks ( void )
static

◆ ez_bringup_panel()

void ez_bringup_panel ( void )
static

Bring up SDRAM + the GLCDC panel, then bind ra8_gfx to the FB.

Definition at line 368 of file main.c.

References display_get_framebuffer(), display_init(), ez_panic_halt(), k_ez_display_cfg, k_ez_settle_ms, k_msg_fail, k_ra8_gfx_format_rgb565, k_ra8_ok, ra8_delay_ms(), ra8_gfx_init(), ra8_sdramc_init(), s_display, and s_fb.

Referenced by main().

◆ ez_bringup_touch()

void ez_bringup_touch ( void )
static

Open the GT911 touch controller (best-effort, polled).

Definition at line 387 of file main.c.

References k_ez_touch_max_points, k_ra8_touch_irq_pin_unset, and ra8_board_touch_open().

Referenced by main().

◆ ez_flush()

void ez_flush ( const ez_present_t * plan)
static

Flush a scene plan through the PAL with the waveform it asked for.

Definition at line 395 of file main.c.

References display_flush(), ra8_ui_rect_t::h, k_display_refresh_fast, k_display_refresh_quality, ez_present_t::quality, ez_present_t::rect, s_display, ra8_ui_rect_t::w, ra8_ui_rect_t::x, and ra8_ui_rect_t::y.

Referenced by ez_redraw(), and main().

◆ ez_panic_halt()

void ez_panic_halt ( const uint8_t * msg,
uint32_t len )
static

Print a fail banner, then trap (ra8_emulator halts on the BKPT).

Definition at line 303 of file main.c.

References ez_print(), k_ra8_board_led_red, and ra8_board_led_on().

Referenced by ez_bringup_clocks(), ez_bringup_panel(), and main().

◆ ez_poll_touch()

void ez_poll_touch ( bool * was_touching)
static

Poll the GT911 once; on a fresh tap, mutate + redraw the viewport.

Definition at line 459 of file main.c.

References ez_redraw(), ez_scene_tap(), k_ra8_ok, ra8_time_ms(), ra8_touch_read(), s_scene, ra8_touch_point_t::x, and ra8_touch_point_t::y.

Referenced by main().

◆ ez_print()

void ez_print ( const uint8_t * msg,
uint32_t len )
static

Emit a byte run on the console.

Definition at line 297 of file main.c.

References ra8_board_uart_console_write().

Referenced by ez_panic_halt(), ez_print_banner(), ez_print_hex(), ez_print_uint(), and main().

◆ ez_print_banner()

◆ ez_print_hex()

void ez_print_hex ( uint32_t value)
static

Print a 32-bit value as 8 upper-case hex digits.

Definition at line 314 of file main.c.

References ez_print(), k_ez_dec_ten, k_ez_hex_nibbles, k_ez_nibble_bits, and k_ez_nibble_mask.

Referenced by ez_print_banner().

◆ ez_print_uint()

void ez_print_uint ( uint32_t value)
static

Print a small unsigned integer in decimal.

Definition at line 326 of file main.c.

References ez_print(), and k_ez_dec_ten.

Referenced by ez_print_banner().

◆ ez_redraw()

bool ez_redraw ( void )
static

Render, then flush whatever the scene says changed.

Definition at line 413 of file main.c.

References ez_flush(), ez_scene_present(), ez_scene_render(), k_ra8_ok, ez_present_t::present, and s_scene.

Referenced by ez_poll_touch(), and main().

◆ ez_setup_scene()

bool ez_setup_scene ( void )
static

Wire the scene over the SDRAM cache storage and the SRAM scratch.

Definition at line 476 of file main.c.

References ez_scene_init(), k_ez_fb_h, k_ez_fb_w, k_ra8_ok, s_buckets, s_cell_mem, s_dims, s_framebuffer, s_keys, s_meta, s_packed, s_row, s_scene, and s_strip.

Referenced by main().

◆ main()

void main ( void )

App entry: panel bring-up -> tiled page -> self-check -> zoom loop.

The application entry point Reset_Handler hands control to.

Precondition
Reset_Handler copied .data and zeroed .bss.
SystemInit set VTOR / FPU / priority grouping.
Postcondition
The boot banner is emitted; the reader loop services taps forever.
Since
0.1.0

Definition at line 502 of file main.c.

References ez_bringup_clocks(), ez_bringup_panel(), ez_bringup_touch(), ez_content_rect(), ez_flush(), ez_panic_halt(), ez_poll_touch(), ez_print(), ez_print_banner(), ez_redraw(), ez_scene_selftest(), ez_scene_tick(), ez_setup_scene(), k_ez_fb_h, k_ez_fb_w, k_ez_frame_ms, k_ez_led_every, k_msg_boot, k_msg_rend, k_msg_scene, k_ra8_board_led_blue, k_ra8_ok, ra8_board_led_toggle(), ra8_delay_ms(), ra8_time_ms(), and s_scene.

◆ RA8_BOARD_PANEL_FRAMEBUFFER()

RA8_BOARD_PANEL_FRAMEBUFFER ( s_framebuffer )

References s_framebuffer.

Variable Documentation

◆ k_ez_display_cfg

const display_cfg_t k_ez_display_cfg
static
Initial value:
= {
.framebuffer = s_framebuffer,
.framebuffer_bytes = sizeof(s_framebuffer),
.width_px = (uint16_t)k_ez_fb_w,
.height_px = (uint16_t)k_ez_fb_h,
.panel_timing = &s_ra8_panel_ek_ra8d2_timing,
}
@ k_display_pixfmt_rgb565
16 bpp, 5/6/5 packed.
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.
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.

Display PAL configuration: LCD/GLCDC backend over the SDRAM framebuffer.

Immutable; the PAL copies what it needs at display_init.

Note
Read once at boot.
Warning
Changing the pixel format here must match the ra8_gfx binding.
Since
0.1.0

Definition at line 221 of file main.c.

Referenced by ez_bringup_panel().

◆ k_msg_boot

const uint8_t k_msg_boot[] = "ereader-zoom: boot\r\n"
static

Console banner fragment: boot line.

Definition at line 262 of file main.c.

◆ k_msg_cells

const uint8_t k_msg_cells[] = " cells "
static

Console banner fragment: cache cell-count label.

Definition at line 276 of file main.c.

Referenced by ez_print_banner().

◆ k_msg_evict

const uint8_t k_msg_evict[] = " evict="
static

Console banner fragment: cache eviction counter label.

Definition at line 290 of file main.c.

Referenced by ez_print_banner().

◆ k_msg_fail

const uint8_t k_msg_fail[] = "ereader-zoom: FAIL init\r\n"
static

Console banner fragment: bring-up failure.

Definition at line 264 of file main.c.

◆ k_msg_hit

const uint8_t k_msg_hit[] = " hit="
static

Console banner fragment: cache hit counter label.

Definition at line 286 of file main.c.

Referenced by ez_print_banner(), and imp_run().

◆ k_msg_lens

const uint8_t k_msg_lens[] = " lens="
static

Console banner fragment: loupe render hash label.

Definition at line 284 of file main.c.

Referenced by ez_print_banner().

◆ k_msg_miss

const uint8_t k_msg_miss[] = " miss="
static

Console banner fragment: cache miss counter label.

Definition at line 288 of file main.c.

Referenced by ez_print_banner(), imp_run(), and main().

◆ k_msg_ok

const uint8_t k_msg_ok[] = " ok\r\n"
static

Console banner fragment: success suffix.

Definition at line 294 of file main.c.

◆ k_msg_pan

const uint8_t k_msg_pan[] = " pan="
static

Console banner fragment: panned render hash label.

Definition at line 280 of file main.c.

Referenced by ez_print_banner().

◆ k_msg_pre

const uint8_t k_msg_pre[] = "ereader-zoom: page "
static

Console banner fragment: geometry prefix.

Definition at line 270 of file main.c.

◆ k_msg_rend

const uint8_t k_msg_rend[] = "ereader-zoom: FAIL render\r\n"
static

Console banner fragment: render / self-check failure.

Definition at line 268 of file main.c.

◆ k_msg_scene

const uint8_t k_msg_scene[] = "ereader-zoom: FAIL scene\r\n"
static

Console banner fragment: scene wiring failure.

Definition at line 266 of file main.c.

Referenced by main().

◆ k_msg_tile

const uint8_t k_msg_tile[] = " tile "
static

Console banner fragment: tile edge label.

Definition at line 274 of file main.c.

◆ k_msg_warm

const uint8_t k_msg_warm[] = " warm="
static

Console banner fragment: prefetch counter label.

Definition at line 292 of file main.c.

Referenced by ez_print_banner().

◆ k_msg_x

const uint8_t k_msg_x[] = "x"
static

Console banner fragment: dimension separator.

Definition at line 272 of file main.c.

◆ k_msg_z1

const uint8_t k_msg_z1[] = " z1="
static

Console banner fragment: 1:1 render hash label.

Definition at line 278 of file main.c.

Referenced by ez_print_banner().

◆ k_msg_z2

const uint8_t k_msg_z2[] = " z2="
static

Console banner fragment: 2x render hash label.

Definition at line 282 of file main.c.

Referenced by ez_print_banner().

◆ s_buckets

int32_t s_buckets[k_ez_buckets]
static

Tile-cache hash-bucket heads.

Sized above the cell count so lookups stay near O(1).

Note
Owned by the tile cache.
Warning
Do not write outside ra8_tile_cache.
Since
0.1.0

Definition at line 180 of file main.c.

◆ s_cell_mem

uint8_t s_cell_mem[(size_t) k_ez_cells *(size_t) k_ez_cell_bytes]
static

Tile-cache cell storage: k_ez_cells gray8 256x256 tiles, in SDRAM.

The viewer's large working set. It lives here, not in the zoom engine, which is the whole point of the strip composite: the magnifier's own scratch is 25 KiB of SRAM.

Note
Owned by the tile cache once ez_scene_init has run.
Warning
Never addressed directly; the cache hands out pinned cells.
Since
0.1.0

Definition at line 140 of file main.c.

◆ s_dims

ra8_tile_dims_t s_dims[k_ez_cells]
static

Tile-cache decoded-extent descriptors, one entry per cell.

Cross-checked against the declared tile geometry on every fetch.

Note
Owned by the tile cache.
Warning
Do not write outside ra8_tile_cache.
Since
0.1.0

Definition at line 170 of file main.c.

◆ s_display

display_handle_t* s_display = nullptr
static

Display PAL handle returned by display_init.

NULL until the panel is up.

Note
Single display; the PAL rejects a second display_init.
Warning
Do not flush through this before bring-up completes.
Since
0.1.0

Definition at line 239 of file main.c.

◆ s_fb

display_fb_t s_fb
static

Mutable copy of the framebuffer descriptor, populated at boot.

Used to bind ra8_gfx to whatever the PAL actually handed back.

Note
Written once during bring-up.
Warning
Stale until display_get_framebuffer has run.
Since
0.1.0

Definition at line 249 of file main.c.

◆ s_keys

ra8_tile_key_t s_keys[k_ez_cells]
static

Tile-cache key storage, one entry per cell.

Compared byte-wise, so the cache zero-fills it at init.

Note
Owned by the tile cache.
Warning
Do not write outside ra8_tile_cache.
Since
0.1.0

Definition at line 160 of file main.c.

◆ s_meta

Tile-cache link metadata, one entry per cell.

Small and hot, so it stays in internal SRAM rather than SDRAM.

Note
Owned by the tile cache.
Warning
Do not write outside ra8_tile_cache.
Since
0.1.0

Definition at line 150 of file main.c.

◆ s_packed

uint8_t s_packed[k_ez_packed_bytes]
static

Composite scratch: the dithered strip packed to 4 bpp (SRAM).

Handed straight to ra8_gfx_blit_gray4_zoom.

Note
Shared by both viewports.
Warning
Overwritten by every zoom render.
Since
0.1.0

Definition at line 211 of file main.c.

Referenced by ez_setup_scene(), internal_publish(), internal_publish_fragment(), and internal_workspace().

◆ s_row

uint8_t s_row[k_ez_row_bytes]
static

Composite scratch: one source row at the widest viewport (SRAM).

Part of the viewer's whole 25 KiB footprint; see ez_scene.h.

Note
Shared by both viewports (only one renders at a time).
Warning
Overwritten by every zoom render.
Since
0.1.0

Definition at line 190 of file main.c.

Referenced by ez_setup_scene().

◆ s_scene

ez_scene_t s_scene
static

The demo scene: tiled page, zoom viewport and loupe.

All of the app's presentation state; see ez_scene.h.

Note
Single-threaded reader loop only.
Warning
Not valid until ez_scene_init returns k_ra8_ok.
Since
0.1.0

Definition at line 259 of file main.c.

Referenced by ez_poll_touch(), ez_redraw(), ez_setup_scene(), and main().

◆ s_strip

uint8_t s_strip[k_ez_strip_bytes]
static

Composite scratch: the gray8 destination strip (SRAM).

16 destination rows at 1024 px; the strip height is derived from this capacity by zoom_view_open().

Note
Shared by both viewports.
Warning
Overwritten by every zoom render.
Since
0.1.0

Definition at line 201 of file main.c.