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

Eclipse ThreadX SD-card boot-sector dump on the EK-RA8D2. More...

#include <stddef.h>
#include <stdint.h>
#include "ra8_attributes.h"
#include "ra8_board_ek_ra8d2.h"
#include "ra8_boot_entry.h"
#include "ra8_cgc.h"
#include "ra8_err.h"
#include "ra8_isr.h"
#include "ra8_sdcard.h"
#include "ra8_threadx.h"
#include "tx_api.h"
Include dependency graph for main.c:

Go to the source code of this file.

Enumerations

enum  sdcard_stack_t : uint16_t { k_sdcard_thread_stack_bytes = 4096U }
 Stack size, in bytes, for the SD card thread. More...
enum  sdcard_priority_t : uint8_t { k_sdcard_thread_priority = 4U }
 Thread priority for the SD card thread. More...
enum  sdcard_baud_t : uint32_t { k_sdcard_console_baud = 115200U }
 Console baud (115200 8N1). More...
enum  sdcard_period_t : uint16_t { k_sdcard_loop_ticks = 5000U }
 Loop period in ThreadX ticks (port/threadx/inc/tx_user.h pins to 1 ms). More...
enum  sdcard_layout_t : uint8_t {
  k_sdcard_sdhi_instance = 0U ,
  k_sdcard_dump_bytes = 16U
}
 SDHI pin layout constants. More...
enum  sdcard_hex_t : uint8_t {
  k_sdcard_hex_chars_per_byte = 3U ,
  k_sdcard_hex_nibble_shift = 4U ,
  k_sdcard_hex_nibble_mask = 0x0FU ,
  k_sdcard_hex_alpha_threshold = 10U
}
 ASCII-hex formatting constants. More...
enum  sdcard_block_size_t : uint16_t { k_sdcard_block_bytes = 512U }
 SD logical block size (512 bytes for SDHC/SDXC). More...

Functions

void _tx_timer_interrupt (void)
void SysTick_Handler (void)
 SysTick exception handler – tail-calls the ThreadX timer.
static char internal_sdcard_nibble_to_hex (uint8_t nibble)
 Convert a single nibble to its ASCII hex character.
static void internal_sdcard_hex_dump (const uint8_t *in, uint8_t len, char *out)
 Format len bytes from in into "XX XX ..." ASCII.
static void internal_sdcard_log (const char *s)
 Push a NUL-terminated ASCII string out the J-Link OB VCOM.
static void internal_sdcard_one_pass (void)
 Read sector 0 into s_sdcard_block and log status / hex dump.
static void internal_sdcard_thread_entry (ULONG thread_input)
 Thread entry: init the card once, then loop reading sector 0.
void tx_application_define (void *first_unused_memory)
 ThreadX application initialisation callback.
void main (void)
 Application entry.

Variables

static uint8_t s_sdcard_stack [k_sdcard_thread_stack_bytes]
 Stack for the SD card thread (32-bit aligned per ARMv8-M AAPCS).
static TX_THREAD s_sdcard_thread
 Thread control block.
static uint8_t s_sdcard_block [k_sdcard_block_bytes]
 Block buffer for one SD sector.

Detailed Description

Eclipse ThreadX SD-card boot-sector dump on the EK-RA8D2.

Tag
[Ring 6 / APP] {World: S}

One ThreadX thread that exercises the full ra8_sdcard HAL stack:

  1. Route the eight SDHI pins (CMD, CLK, DAT0..3, WP, CD) on port 4 to the SDHI peripheral function via ra8_board_sdhi_pins_init.
  2. Bring the J-Link OB VCOM console up at 115200 8N1 via ra8_board_uart_console_init.
  3. ra8_sdcard_init runs the standard SD Physical Layer initialization sequence (CMD0 -> CMD7) on SDHI instance 0.
  4. ra8_sdcard_read_blocks(0, ...) reads sector 0 (the MBR / boot sector) into a 512-byte SRAM buffer.
  5. The first 16 bytes are formatted as ASCII hex (XX XX XX XX...) and pushed out the console; LED1 toggles once per pass to make the activity visible.

SDHI pin map is owned by the BSP (ra8_board_sdhi_pins_init): port 4, pins 0..7 routed to the on-chip SDHI block.

Threads
Name Priority Period Action
sdcard 4 5000 ms loop Read block 0, dump 16 bytes
Since
0.1.0

Definition in file main.c.

Enumeration Type Documentation

◆ sdcard_baud_t

enum sdcard_baud_t : uint32_t

Console baud (115200 8N1).

Enumerator
k_sdcard_console_baud 

Sdcard console baud.

Definition at line 79 of file main.c.

◆ sdcard_block_size_t

enum sdcard_block_size_t : uint16_t

SD logical block size (512 bytes for SDHC/SDXC).

Enumerator
k_sdcard_block_bytes 

Sdcard block bytes.

Definition at line 111 of file main.c.

◆ sdcard_hex_t

enum sdcard_hex_t : uint8_t

ASCII-hex formatting constants.

Enumerator
k_sdcard_hex_chars_per_byte 

"XX " incl.

trailing space.

k_sdcard_hex_nibble_shift 

High-nibble right-shift.

k_sdcard_hex_nibble_mask 

Low-nibble mask.

k_sdcard_hex_alpha_threshold 

10..15 -> 'A'..'F'.

Definition at line 101 of file main.c.

◆ sdcard_layout_t

enum sdcard_layout_t : uint8_t

SDHI pin layout constants.

Enumerator
k_sdcard_sdhi_instance 

SDHI instance index.

k_sdcard_dump_bytes 

Bytes from sector 0 to print.

Definition at line 93 of file main.c.

◆ sdcard_period_t

enum sdcard_period_t : uint16_t

Loop period in ThreadX ticks (port/threadx/inc/tx_user.h pins to 1 ms).

Enumerator
k_sdcard_loop_ticks 

Sdcard loop ticks.

Definition at line 86 of file main.c.

◆ sdcard_priority_t

enum sdcard_priority_t : uint8_t

Thread priority for the SD card thread.

Enumerator
k_sdcard_thread_priority 

Sdcard thread priority.

Definition at line 72 of file main.c.

◆ sdcard_stack_t

enum sdcard_stack_t : uint16_t

Stack size, in bytes, for the SD card thread.

Enumerator
k_sdcard_thread_stack_bytes 

Sdcard thread stack bytes.

Definition at line 65 of file main.c.

Function Documentation

◆ _tx_timer_interrupt()

void _tx_timer_interrupt ( void )
extern

Referenced by SysTick_Handler().

◆ internal_sdcard_hex_dump()

void internal_sdcard_hex_dump ( const uint8_t * in,
uint8_t len,
char * out )
static

Format len bytes from in into "XX XX ..." ASCII.

Expands each source byte to two uppercase hex digits followed by a space, using no terminator so the caller controls final framing.

Parameters
[in]inSource buffer.
[in]lenNumber of source bytes to format.
[out]outDestination buffer; must hold len * 3 bytes.
Precondition
in / out non-NULL; out has space for len * 3 bytes.
Source and destination spans do not overlap.
Postcondition
out holds an ASCII hex dump (no NUL terminator written).
Exactly len * 3 destination bytes are written.
Note
The caller appends the NUL terminator used by console logging.
Since
0.1.0

Definition at line 196 of file main.c.

References internal_sdcard_nibble_to_hex(), k_sdcard_hex_chars_per_byte, k_sdcard_hex_nibble_mask, k_sdcard_hex_nibble_shift, and RA8_INTERNAL.

Referenced by internal_sdcard_one_pass().

◆ internal_sdcard_log()

void internal_sdcard_log ( const char * s)
static

Push a NUL-terminated ASCII string out the J-Link OB VCOM.

Measures a non-NULL string and submits exactly the payload bytes to the BSP console without heap allocation.

Parameters
[in]sNUL-terminated string. NULL is a no-op.
Precondition
ra8_board_uart_console_init has succeeded.
A non-NULL s denotes a readable NUL-terminated string.
Postcondition
Bytes have been handed to SCI3 TDR (or silently dropped on err).
NULL input returns without accessing the console.
Note
Diagnostic write errors do not stop the periodic card read loop.
Since
0.1.0

Definition at line 222 of file main.c.

References ra8_board_uart_console_write(), and RA8_INTERNAL.

Referenced by internal_sdcard_one_pass(), and internal_sdcard_thread_entry().

◆ internal_sdcard_nibble_to_hex()

char internal_sdcard_nibble_to_hex ( uint8_t nibble)
static

Convert a single nibble to its ASCII hex character.

Masks the input to four bits, then maps the value through the numeric or uppercase alphabetic ASCII range.

Parameters
[in]nibbleLow 4 bits of an arbitrary uint8_t.
Returns
ASCII char in '0'..'9' or 'A'..'F'.
Return values
'0'..'9'Numeric hexadecimal digit.
'A'..'F'Alphabetic hexadecimal digit.
Precondition
0 <= nibble <= 15.
The execution character set uses contiguous decimal and A-F digits.
Postcondition
Returned byte is a printable ASCII hex digit.
No file-scope or caller-owned state is modified.
Note
Inputs above 15 are deterministically reduced to their low nibble.
Since
0.1.0

Definition at line 171 of file main.c.

References k_sdcard_hex_alpha_threshold, k_sdcard_hex_nibble_mask, and RA8_INTERNAL.

Referenced by internal_sdcard_hex_dump().

◆ internal_sdcard_one_pass()

void internal_sdcard_one_pass ( void )
static

Read sector 0 into s_sdcard_block and log status / hex dump.

Performs one block read, formats the first fixed-size prefix on the stack, and emits either the formatted prefix or an error line.

Precondition
ra8_sdcard_init has succeeded.
The worker thread exclusively owns s_sdcard_block.
Postcondition
Either the dump line has been emitted or an error line has.
At most one sector is read during this invocation.
Note
Read failure is recoverable; the outer loop retries after its delay.
Since
0.1.0

Definition at line 250 of file main.c.

References internal_sdcard_hex_dump(), internal_sdcard_log(), k_ra8_ok, k_sdcard_dump_bytes, k_sdcard_hex_chars_per_byte, RA8_INTERNAL, ra8_sdcard_read_blocks(), and s_sdcard_block.

Referenced by internal_sdcard_thread_entry().

◆ internal_sdcard_thread_entry()

void internal_sdcard_thread_entry ( ULONG thread_input)
static

Thread entry: init the card once, then loop reading sector 0.

Initializes the fixed SDHI instance and, on success, performs a read/log/LED/sleep cycle forever; failed initialization sleeps without I/O.

Parameters
[in]thread_inputUnused (ThreadX cookie).
Precondition
Pins routed; console initialized; ThreadX scheduler running.
The static worker stack and sector buffer are exclusively assigned here.
Postcondition
Loops forever: read + dump + LED1 toggle + sleep.
The ThreadX input cookie is not interpreted or modified.
Note
Initialization failure is intentionally non-busy and waits each cycle.
Since
0.1.0

Definition at line 279 of file main.c.

References internal_sdcard_log(), internal_sdcard_one_pass(), k_ra8_board_led1, k_ra8_ok, k_sdcard_loop_ticks, k_sdcard_sdhi_instance, ra8_board_led_toggle(), RA8_INTERNAL, ra8_sdcard_init(), and tx_thread_sleep.

Referenced by tx_application_define().

◆ main()

void main ( void )

Application entry.

The application entry point Reset_Handler hands control to.

Brings up LED, console, SDHI pins, then ThreadX.

Precondition
Reset_Handler has copied .data + zeroed .bss.
SystemInit has set VTOR, FPU, and priority grouping.
Postcondition
CPUCLK0 is raised to the PLL1 target before the kernel starts.
On clean entry the SD card thread runs forever.
On any HAL init failure the function halts in __WFI.
Since
0.1.0

Definition at line 364 of file main.c.

References k_ra8_board_led1, k_ra8_ok, k_sdcard_console_baud, ra8_board_led_init(), ra8_board_sdhi_pins_init(), ra8_board_uart_console_init(), ra8_cgc_init(), and ra8_isr_globals_enable().

◆ SysTick_Handler()

void SysTick_Handler ( void )

SysTick exception handler – tail-calls the ThreadX timer.

Service one SysTick event for the core timebase.

Service the SysTick exception selected by the linked application.

Precondition
Called from exception context (IPSR == 15).
_tx_initialize_low_level has programmed SysTick.
Postcondition
One ThreadX tick elapsed; PendSV may be pending.

Definition at line 146 of file main.c.

References _tx_timer_interrupt().

◆ tx_application_define()

void tx_application_define ( void * first_unused_memory)

ThreadX application initialisation callback.

Parameters
[in]first_unused_memoryUnused – the demo uses a static stack.
Precondition
Called from _tx_initialize_kernel_enter with IRQs masked.
Postcondition
SysTick is retuned to the live CPUCLK0 before scheduling starts.
The SD card thread is created and auto-started.

Definition at line 313 of file main.c.

References internal_sdcard_thread_entry(), k_ra8_ok, k_sdcard_thread_priority, k_sdcard_thread_stack_bytes, ra8_threadx_systick_retune(), s_sdcard_stack, s_sdcard_thread, s_thread_name, TX_AUTO_START, TX_NO_TIME_SLICE, TX_SUCCESS, and tx_thread_create.

Variable Documentation

◆ s_sdcard_block

uint8_t s_sdcard_block[k_sdcard_block_bytes]
static

Block buffer for one SD sector.

File-scope so it does not eat thread-stack budget.

Definition at line 136 of file main.c.

Referenced by internal_sdcard_one_pass().

◆ s_sdcard_stack

uint8_t s_sdcard_stack[k_sdcard_thread_stack_bytes]
static

Stack for the SD card thread (32-bit aligned per ARMv8-M AAPCS).

Definition at line 125 of file main.c.

Referenced by tx_application_define().

◆ s_sdcard_thread

TX_THREAD s_sdcard_thread
static

Thread control block.

ThreadX zeroes it on tx_thread_create.

Definition at line 130 of file main.c.

Referenced by tx_application_define().