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
36
37#include <stddef.h>
38#include <stdint.h>
39
40#include "ra8_attributes.h"
41#include "ra8_board_ek_ra8d2.h"
42#include "ra8_boot_entry.h"
43#include "ra8_cgc.h"
44#include "ra8_err.h"
45#include "ra8_isr.h"
46#include "ra8_sdcard.h"
47
48/*
49 * The host unit-test build (RA8_OFF_TARGET) does not link the
50 * ThreadX vendor tree, so ``tx_api.h`` (and the ThreadX-port header
51 * ``ra8_threadx.h``) are pulled in only on the cross-compile target.
52 */
53#ifndef RA8_OFF_TARGET
54#include "ra8_threadx.h"
55#include "tx_api.h"
56#endif
57
58/* ---------------------------------------------------------------------------
59 * Tunables (typed enums per the project's no-magic-number rule).
60 * --------------------------------------------------------------------------- */
61
65typedef enum : uint16_t {
68
72typedef enum : uint8_t {
75
79typedef enum : uint32_t {
82
86typedef enum : uint16_t {
89
93typedef enum : uint8_t {
97
107
111typedef enum : uint16_t {
114
115/* ---------------------------------------------------------------------------
116 * Vector table override and thread storage (cross-build only).
117 * --------------------------------------------------------------------------- */
118
119#ifndef RA8_OFF_TARGET
120extern void _tx_timer_interrupt(void);
121
125[[gnu::aligned(8)]] static uint8_t s_sdcard_stack[k_sdcard_thread_stack_bytes];
126
131
137
145void SysTick_Handler(void);
147{
149}
150
151/* ---------------------------------------------------------------------------
152 * Helpers.
153 * --------------------------------------------------------------------------- */
154
172{
173 const uint8_t n = (uint8_t)(nibble & (uint8_t)k_sdcard_hex_nibble_mask);
174 if (n < (uint8_t)k_sdcard_hex_alpha_threshold) {
175 return (char)('0' + (char)n);
176 }
177 return (char)('A' + (char)(n - (uint8_t)k_sdcard_hex_alpha_threshold));
178}
179
196RA8_INTERNAL static void internal_sdcard_hex_dump(const uint8_t* in, uint8_t len, char* out)
197{
198 for (uint8_t i = 0U; i < len; i++) {
199 const uint8_t b = in[i];
200 out[i * k_sdcard_hex_chars_per_byte + 0U] =
202 out[i * k_sdcard_hex_chars_per_byte + 1U] =
204 out[i * k_sdcard_hex_chars_per_byte + 2U] = ' ';
205 }
206}
207
222RA8_INTERNAL static void internal_sdcard_log(const char* s)
223{
224 if (s == nullptr) {
225 return;
226 }
227 size_t len = 0U;
228 while (s[len] != '\0') {
229 len++;
230 }
231 (void)ra8_board_uart_console_write((const uint8_t*)s, len);
232}
233
234/* ---------------------------------------------------------------------------
235 * Thread body.
236 * --------------------------------------------------------------------------- */
237
251{
253 if (err != k_ra8_ok) {
254 internal_sdcard_log("sdcard: read block 0 failed\r\n");
255 return;
256 }
260 internal_sdcard_log("sdcard: blk0[0..15] = ");
262 internal_sdcard_log("\r\n");
263}
264
280{
281 (void)thread_input;
282
283 const ra8_sdcard_cfg_t cfg = {.instance = k_sdcard_sdhi_instance};
284 if (ra8_sdcard_init(&cfg) != k_ra8_ok) {
285 internal_sdcard_log("sdcard: init failed (no card / bad pinmux?)\r\n");
286 while (1) {
288 }
289 }
290 internal_sdcard_log("sdcard: card initialized\r\n");
291
292 while (1) {
296 }
297}
298
299/* ---------------------------------------------------------------------------
300 * tx_application_define -- ThreadX calls this once before the first
301 * scheduling decision.
302 * --------------------------------------------------------------------------- */
303
313void tx_application_define(void* first_unused_memory)
314{
315 static CHAR s_thread_name[] = "sdcard";
316
317 (void)first_unused_memory;
318
319 /* Retune SysTick to the live CPUCLK0 (raised to 1 GHz by
320 * ra8_cgc_init() in main()). tx_initialize_low_level.S already
321 * programmed the reload from the same compile-time 1 GHz assumption,
322 * but this makes the 1 ms kernel tick correct-by-construction for
323 * whatever clock the app actually brought up (issue #287). Runs after
324 * _tx_initialize_low_level and before the first scheduling decision. */
326 while (1) {
327 __asm__ volatile("wfi");
328 }
329 }
330
334 0U,
341 if (err != TX_SUCCESS) {
342 while (1) {
343 __asm__ volatile("wfi");
344 }
345 }
346}
347#endif /* !RA8_OFF_TARGET */
348
349/* ---------------------------------------------------------------------------
350 * main() -- driver init, then drop into the ThreadX scheduler.
351 * --------------------------------------------------------------------------- */
352
364void main(void)
365{
366 /* CGC bring-up FIRST. tx_initialize_low_level.S programs SysTick from
367 * the post-CGC CPUCLK0 target (1 GHz); entering the kernel on the
368 * boot-default MOCO (~8 MHz) instead would run the "1 ms" tick ~119x
369 * too slow, so tx_thread_sleep(5000) would stretch to ~10 minutes.
370 * Raise the PLL before tx_kernel_enter (and tx_application_define
371 * additionally retunes SysTick from the live clock -- issue #287). */
372 if (ra8_cgc_init() != k_ra8_ok) {
373 while (1) {
374 __asm__ volatile("wfi");
375 }
376 }
378 while (1) {
379 __asm__ volatile("wfi");
380 }
381 }
383 while (1) {
384 __asm__ volatile("wfi");
385 }
386 }
387#ifndef RA8_OFF_TARGET
389 while (1) {
390 __asm__ volatile("wfi");
391 }
392 }
394 tx_kernel_enter();
395#endif
396
397 while (1) {
398 __asm__ volatile("wfi");
399 }
400}
void main(void)
Secure fallback main entry point.
Definition main.c:37
void tx_application_define(void *first_unused_memory)
ThreadX system-define hook: build worker thread + byte pool.
Definition main.c:942
sdcard_priority_t
Thread priority for the SD card thread.
Definition main.c:72
@ k_sdcard_thread_priority
Sdcard thread priority.
Definition main.c:73
static void internal_sdcard_one_pass(void)
Read sector 0 into s_sdcard_block and log status / hex dump.
Definition main.c:250
sdcard_stack_t
Stack size, in bytes, for the SD card thread.
Definition main.c:65
@ k_sdcard_thread_stack_bytes
Sdcard thread stack bytes.
Definition main.c:66
static void internal_sdcard_thread_entry(ULONG thread_input)
Thread entry: init the card once, then loop reading sector 0.
Definition main.c:279
sdcard_period_t
Loop period in ThreadX ticks (port/threadx/inc/tx_user.h pins to 1 ms).
Definition main.c:86
@ k_sdcard_loop_ticks
Sdcard loop ticks.
Definition main.c:87
void _tx_timer_interrupt(void)
static void internal_sdcard_hex_dump(const uint8_t *in, uint8_t len, char *out)
Format len bytes from in into "XX XX ..." ASCII.
Definition main.c:196
sdcard_block_size_t
SD logical block size (512 bytes for SDHC/SDXC).
Definition main.c:111
@ k_sdcard_block_bytes
Sdcard block bytes.
Definition main.c:112
sdcard_hex_t
ASCII-hex formatting constants.
Definition main.c:101
@ k_sdcard_hex_nibble_mask
Low-nibble mask.
Definition main.c:104
@ k_sdcard_hex_alpha_threshold
10..15 -> 'A'..'F'.
Definition main.c:105
@ k_sdcard_hex_nibble_shift
High-nibble right-shift.
Definition main.c:103
@ k_sdcard_hex_chars_per_byte
"XX " incl.
Definition main.c:102
static uint8_t s_sdcard_block[k_sdcard_block_bytes]
Block buffer for one SD sector.
Definition main.c:136
static TX_THREAD s_sdcard_thread
Thread control block.
Definition main.c:130
void SysTick_Handler(void)
SysTick exception handler – tail-calls the ThreadX timer.
Definition main.c:146
static void internal_sdcard_log(const char *s)
Push a NUL-terminated ASCII string out the J-Link OB VCOM.
Definition main.c:222
static char internal_sdcard_nibble_to_hex(uint8_t nibble)
Convert a single nibble to its ASCII hex character.
Definition main.c:171
sdcard_baud_t
Console baud (115200 8N1).
Definition main.c:79
@ k_sdcard_console_baud
Sdcard console baud.
Definition main.c:80
sdcard_layout_t
SDHI pin layout constants.
Definition main.c:93
@ k_sdcard_sdhi_instance
SDHI instance index.
Definition main.c:94
@ k_sdcard_dump_bytes
Bytes from sector 0 to print.
Definition main.c:95
static uint8_t s_sdcard_stack[k_sdcard_thread_stack_bytes]
Stack for the SD card thread (32-bit aligned per ARMv8-M AAPCS).
Definition main.c:125
static CHAR s_thread_name[]
Definition main.c:212
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_sdhi_pins_init(void)
Route the eight SDHI0 bus pins to the SDHI peripheral function.
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).
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_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
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
SD card driver layered on top of the RA8D2 SDHI peripheral.
ra8_err_t ra8_sdcard_init(const ra8_sdcard_cfg_t *cfg)
Bring up the SDHI block and perform full SD card initialization.
Definition ra8_sdcard.c:544
ra8_err_t ra8_sdcard_read_blocks(uint32_t lba, uint8_t *buf, uint32_t count)
Read count 512-byte blocks starting at lba.
Definition ra8_sdcard.c:612
RA8 <-> Eclipse ThreadX glue: SysTick kernel-tick retuning.
ra8_err_t ra8_threadx_systick_retune(void)
Reprogram SysTick.LOAD from the live CPUCLK0 rate.
unsigned int UINT
ThreadX-compatible unsigned int (host stub).
#define TX_SUCCESS
#define tx_thread_create
#define tx_thread_sleep
#define TX_NO_TIME_SLICE
char CHAR
ThreadX-compatible CHAR (host stub).
#define TX_AUTO_START
unsigned long ULONG
ThreadX-compatible unsigned long (host stub).
Opaque thread stand-in for the host build.
User-supplied configuration for ra8_sdcard_init.
Definition ra8_sdcard.h:88