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
48
49#include <stdint.h>
50#include <string.h>
51
52#include "ra8_board_ek_ra8d2.h"
53#include "ra8_boot_entry.h"
54#include "ra8_cgc.h"
55#include "ra8_err.h"
56#include "ra8_gpio_constants.h"
57#include "ra8_isr.h"
58#include "ra8_port_constants.h"
59#include "ra8_port_utils.h"
60#include "ra8_time.h"
61#include "ra8_usb.h"
62#include "ra8_usb_hmsc.h"
64
65#ifndef RA8_OFF_TARGET
66#include "tx_api.h"
67#include "ux_api.h"
68#include "ux_dcd_ra8_usb.h"
69#include "ux_device_class_storage.h"
70#include "ux_device_stack.h"
71
72/* Strong SysTick override: route the tick into BOTH the ra8_time millisecond
73 * counter (for ra8_delay_ms and the polled host stack's timeouts) AND
74 * ThreadX's timer; the 1 ms pulse also recovers the DCD's storm-guard mask. */
75
76extern void _tx_timer_interrupt(void);
77
87static volatile bool s_tx_kernel_up = false;
88
89void SysTick_Handler(void);
98#endif
99
100/* -------------------------------------------------------------------------- */
101/* Pinout (FSP-aligned, EK-RA8D2 v1 User's Manual) */
102/* -------------------------------------------------------------------------- */
103
106
109
112
115
118
121
122/* -------------------------------------------------------------------------- */
123/* Tunables */
124/* -------------------------------------------------------------------------- */
125
126/*
127 * The tunable / formatter / geometry / phase enums (mlun_config_t, mlun_hex_t,
128 * mlun_mask_t, mlun_geom_t, mlun_phase_t) are shared with the host-side steps
129 * TU and live in usb_selftest_mlun_steps.h. The device-only step + SCSI-sense
130 * enums below stay here because only main.c uses them.
131 */
132
144
153
154#ifndef RA8_OFF_TARGET
155
156/* -------------------------------------------------------------------------- */
157/* ThreadX workers + USBX pool storage */
158/* -------------------------------------------------------------------------- */
159
167
174
182
189
196
197/* SCSI INQUIRY strings -- 8 / 16 / 4 byte fields per SBC-3. */
198static UCHAR s_msc_vendor_id[] = "RA8D2 ";
199static UCHAR s_msc_product_id[] = "MULTI-LUN x3 RO ";
200static UCHAR s_msc_product_rev[] = "0001";
201
202/* -------------------------------------------------------------------------- */
203/* J-Link probes (device-side; host-ladder probes live in the steps TU) */
204/* -------------------------------------------------------------------------- */
205
207static volatile uint32_t s_dbg_read_calls;
209static volatile uint32_t s_dbg_dev_step;
211static volatile uint32_t s_dbg_dev_err;
212
213/* -------------------------------------------------------------------------- */
214/* USB descriptors (single-interface MSC; multi-LUN is a BOT-level concept) */
215/* -------------------------------------------------------------------------- */
216
217/* MSC config: bulk-only transport, SCSI command set, EP1 IN + EP2 OUT,
218 * 64-byte MPS. The number of LUNs is a class-registration parameter, not
219 * a descriptor field, so this framework is the standard single-interface
220 * MSC blob. PID 0x0013 marks the multi-LUN self-test identity. */
221static UCHAR s_device_framework_fs[] = {
222 /* Device descriptor (USB 2.0 sec 9.6.1) -- 18 bytes. */
223 0x12U,
224 0x01U,
225 0x00U,
226 0x02U,
227 0x00U,
228 0x00U,
229 0x00U,
230 0x40U,
231 0x09U,
232 0x12U,
233 0x13U, /* PID = 0x0013 (pid.codes test). */
234 0x00U,
235 0x00U,
236 0x01U,
237 0x01U,
238 0x02U,
239 0x03U,
240 0x01U,
241 /* Configuration descriptor (32 bytes total). */
242 0x09U,
243 0x02U,
244 0x20U,
245 0x00U,
246 0x01U,
247 0x01U,
248 0x00U,
249 0x80U,
250 0x32U,
251 /* Interface descriptor -- MSC, SCSI, BBB. */
252 0x09U,
253 0x04U,
254 0x00U,
255 0x00U,
256 0x02U,
257 0x08U,
258 0x06U,
259 0x50U,
260 0x00U,
261 /* Bulk-IN endpoint (EP1 IN, 64-byte MPS). */
262 0x07U,
263 0x05U,
264 0x81U,
265 0x02U,
266 0x40U,
267 0x00U,
268 0x00U,
269 /* Bulk-OUT endpoint (EP2 OUT, 64-byte MPS). */
270 0x07U,
271 0x05U,
272 0x02U,
273 0x02U,
274 0x40U,
275 0x00U,
276 0x00U,
277};
278
286static UCHAR s_string_framework[] = {
287 /* idx 1: "Brighton Sikarskie". */
288 0x09U,
289 0x04U,
290 0x01U,
291 0x12U,
292 'B',
293 'r',
294 'i',
295 'g',
296 'h',
297 't',
298 'o',
299 'n',
300 ' ',
301 'S',
302 'i',
303 'k',
304 'a',
305 'r',
306 's',
307 'k',
308 'i',
309 'e',
310 /* idx 2: "RA8D2 MULTILUN". */
311 0x09U,
312 0x04U,
313 0x02U,
314 0x0EU,
315 'R',
316 'A',
317 '8',
318 'D',
319 '2',
320 ' ',
321 'M',
322 'U',
323 'L',
324 'T',
325 'I',
326 'L',
327 'U',
328 'N',
329 /* idx 3: serial. */
330 0x09U,
331 0x04U,
332 0x03U,
333 0x08U,
334 '0',
335 '0',
336 '0',
337 '0',
338 '0',
339 '0',
340 '1',
341 '3',
342};
343
344/* USBX LANGID descriptor 0x0409 (English-US), little-endian byte pair. */
345typedef enum : uint8_t {
349
356
357/* -------------------------------------------------------------------------- */
358/* Storage class media callbacks (shared across both LUNs) */
359/* -------------------------------------------------------------------------- */
360
388static UINT mlun_msc_read(VOID* storage,
389 ULONG lun,
390 UCHAR* data_pointer,
391 ULONG number_blocks,
392 ULONG lba,
393 ULONG* media_status)
394{
395 (void)storage;
397 if ((lba + number_blocks) > (ULONG)k_mlun_sectors) {
398 *media_status = UX_DEVICE_CLASS_STORAGE_SENSE_STATUS(k_scsi_sense_illegal_request,
401 return UX_ERROR;
402 }
403 for (ULONG i = 0UL; i < number_blocks; i++) {
404 mlun_pattern_fill((uint32_t)lun,
405 (uint32_t)(lba + i),
406 &data_pointer[i * (ULONG)k_mlun_block_size]);
407 }
408 *media_status = 0UL;
410 return UX_SUCCESS;
411}
412
438static UINT mlun_msc_write(VOID* storage,
439 ULONG lun,
440 UCHAR* data_pointer,
441 ULONG number_blocks,
442 ULONG lba,
443 ULONG* media_status)
444{
445 (void)storage;
446 (void)lun;
447 (void)data_pointer;
448 (void)number_blocks;
449 (void)lba;
450 *media_status = UX_DEVICE_CLASS_STORAGE_SENSE_STATUS(k_scsi_sense_data_protect,
453 return UX_ERROR;
454}
455
477static UINT mlun_msc_status(VOID* storage, ULONG lun, ULONG media_id, ULONG* media_status)
478{
479 (void)storage;
480 (void)lun;
481 (void)media_id;
482 *media_status = 0UL;
483 return UX_SUCCESS;
484}
485
486/* -------------------------------------------------------------------------- */
487/* Device side: USBX MSC with two LUNs */
488/* -------------------------------------------------------------------------- */
489
507{
508 if (_ux_system_initialize(s_usbx_pool, k_mlun_usbx_pool_bytes, UX_NULL, 0) != UX_SUCCESS) {
509 return UX_ERROR;
510 }
511 return _ux_device_stack_initialize((UCHAR*)UX_NULL,
512 0,
514 sizeof(s_device_framework_fs),
516 sizeof(s_string_framework),
519 UX_NULL);
520}
521
540static void mlun_fill_lun(UX_SLAVE_CLASS_STORAGE_PARAMETER* p, uint32_t idx)
541{
542 p->ux_slave_class_storage_parameter_lun[idx].ux_slave_class_storage_media_last_lba =
543 (ULONG)k_mlun_sectors - 1UL;
544 p->ux_slave_class_storage_parameter_lun[idx].ux_slave_class_storage_media_block_length =
546 p->ux_slave_class_storage_parameter_lun[idx].ux_slave_class_storage_media_type =
547 UX_SLAVE_CLASS_STORAGE_MEDIA_FAT_DISK;
548 p->ux_slave_class_storage_parameter_lun[idx].ux_slave_class_storage_media_removable_flag =
549 UX_SLAVE_CLASS_STORAGE_MEDIA_IS_REMOVABLE;
550 p->ux_slave_class_storage_parameter_lun[idx].ux_slave_class_storage_media_read_only_flag =
551 UX_TRUE;
552 p->ux_slave_class_storage_parameter_lun[idx].ux_slave_class_storage_media_read = mlun_msc_read;
553 p->ux_slave_class_storage_parameter_lun[idx].ux_slave_class_storage_media_write = mlun_msc_write;
554 p->ux_slave_class_storage_parameter_lun[idx].ux_slave_class_storage_media_status =
556}
557
577{
578 UX_SLAVE_CLASS_STORAGE_PARAMETER msc_params;
579 (void)memset(&msc_params, 0, sizeof(msc_params));
580 msc_params.ux_slave_class_storage_parameter_number_lun = (ULONG)k_mlun_count;
581 msc_params.ux_slave_class_storage_parameter_vendor_id = s_msc_vendor_id;
582 msc_params.ux_slave_class_storage_parameter_product_id = s_msc_product_id;
583 msc_params.ux_slave_class_storage_parameter_product_rev = s_msc_product_rev;
584 for (uint32_t idx = 0U; idx < (uint32_t)k_mlun_count; idx++) {
585 mlun_fill_lun(&msc_params, idx);
586 }
587 static UCHAR s_class_name[] = "ux_slave_class_storage";
588
589 return _ux_device_stack_class_register(s_class_name,
590 _ux_device_class_storage_entry,
591 1,
592 0,
593 &msc_params);
594}
595
613static VOID mlun_device_worker(ULONG arg)
614{
615 (void)arg;
616
618 if (ux != UX_SUCCESS) {
619 s_dbg_dev_err = (uint32_t)ux;
620 return;
621 }
623 ux = mlun_class_register();
624 if (ux != UX_SUCCESS) {
625 s_dbg_dev_err = (uint32_t)ux;
626 return;
627 }
630 if (e != k_ra8_ok) {
631 s_dbg_dev_err = (uint32_t)e;
632 return;
633 }
636 if (e != k_ra8_ok) {
637 s_dbg_dev_err = (uint32_t)e;
638 return;
639 }
641
642 while (1) {
645 }
646}
647
665VOID tx_application_define(VOID* first_unused_memory)
666{
667 static CHAR s_device_thread_name[] = "mlun_device";
668 static CHAR s_host_thread_name[] = "mlun_host";
669
670 (void)first_unused_memory;
671 s_tx_kernel_up = true;
675 0UL,
685 0UL,
692}
693#endif /* !RA8_OFF_TARGET */
694
695/* -------------------------------------------------------------------------- */
696/* Startup */
697/* -------------------------------------------------------------------------- */
698
712static void mlun_panic_halt(void)
713{
714 while (1) {
715 __asm__ volatile("wfi");
716 }
717}
718
759
774static void mlun_setup_or_halt(void)
775{
776 uint32_t cpuclk0_hz = 0U;
777 if (ra8_cgc_init() != k_ra8_ok) {
779 }
782 }
785 }
788 }
789 if (ra8_time_init(cpuclk0_hz) != k_ra8_ok) {
791 }
794 }
797 }
800 }
802}
803
818void main(void)
819{
821
823
824#ifndef RA8_OFF_TARGET
825 tx_kernel_enter();
826#endif
827
829}
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
void _tx_timer_interrupt(void)
void SysTick_Handler(void)
SysTick exception handler – tail-calls the ThreadX timer.
Definition main.c:146
static volatile bool s_tx_kernel_up
Set in tx_application_define; gates ThreadX tick delivery.
Definition main.c:86
static UCHAR s_msc_product_id[]
Definition main.c:197
static UCHAR s_host_stack[k_wlun_host_stack]
Stack backing storage for s_host_thread.
Definition main.c:186
static TX_THREAD s_device_thread
ThreadX TCB for the USBX device-side worker thread.
Definition main.c:164
static UCHAR s_string_framework[]
USBX string descriptor table (vendor / product / serial).
Definition main.c:299
scsi_sense_code_t
SCSI sense triple for an unsupported / out-of-range request.
Definition main.c:144
@ k_scsi_ascq_none
ASCQ: none.
Definition main.c:147
@ k_scsi_asc_write_protected
ASC: WRITE PROTECTED.
Definition main.c:149
@ k_scsi_sense_illegal_request
Sense key: ILLEGAL REQUEST.
Definition main.c:145
@ k_scsi_sense_data_protect
Sense key: DATA PROTECT.
Definition main.c:148
@ k_scsi_asc_lba_out_of_range
ASC: LBA out of range.
Definition main.c:146
static UCHAR s_usbx_pool[k_wlun_usbx_pool_bytes]
USBX memory pool (USBX uses tx_byte_pool internally).
Definition main.c:193
usb_langid_byte_t
Definition main.c:358
@ k_usb_langid_en_us_lo
LANGID 0x0409 low byte.
Definition main.c:359
@ k_usb_langid_en_us_hi
LANGID 0x0409 high byte.
Definition main.c:360
static CHAR s_host_thread_name[]
Definition main.c:202
static TX_THREAD s_host_thread
ThreadX TCB for the host-side worker thread.
Definition main.c:179
static UCHAR s_device_framework_fs[]
Definition main.c:234
static UCHAR s_msc_product_rev[]
Definition main.c:198
static UCHAR s_language_id_framework[]
USBX language-id table – US English.
Definition main.c:368
static UCHAR s_device_stack[k_wlun_thread_stack]
Stack backing storage for s_device_thread.
Definition main.c:171
static volatile uint32_t s_dbg_read_calls
Device-side media_read invocations.
Definition main.c:213
static volatile uint32_t s_dbg_dev_step
Device worker progress: 1 stack, 2 class, 3 dcd, 4 attach, 5 parked.
Definition main.c:219
static volatile uint32_t s_dbg_dev_err
Device worker first failing return code (0 = none).
Definition main.c:221
static CHAR s_device_thread_name[]
Definition main.c:201
static UCHAR s_msc_vendor_id[]
Definition main.c:196
mlun_dev_step_t
J-Link probe values marking device-worker bring-up progress.
Definition main.c:137
@ k_mlun_dev_step_attach
Device attached (DPRPU).
Definition main.c:141
@ k_mlun_dev_step_parked
Bring-up done; worker parked.
Definition main.c:142
@ k_mlun_dev_step_dcd
DCD bridge initialized.
Definition main.c:140
@ k_mlun_dev_step_class
MSC class registered.
Definition main.c:139
@ k_mlun_dev_step_stack
USBX system + device stack up.
Definition main.c:138
static UINT mlun_msc_status(VOID *storage, ULONG lun, ULONG media_id, ULONG *media_status)
Storage media-status callback.
Definition main.c:477
static void mlun_fill_lun(UX_SLAVE_CLASS_STORAGE_PARAMETER *p, uint32_t idx)
Populate one LUN parameter slot with geometry + callbacks.
Definition main.c:540
static void mlun_setup_or_halt(void)
Bring CGC + both USB clocks + SysTick + SCI8 + LEDs + pins up.
Definition main.c:774
static const ra8_port_pin_t k_mlun_pin_fs_dp
USBFS D+ (P8_14).
Definition main.c:111
static UINT mlun_class_register(void)
Register the Mass-Storage class with two logical units.
Definition main.c:576
static const ra8_port_pin_t k_mlun_pin_hs_vbus
USBHS_VBUS sense pin (P4_08, PSEL = 0x14).
Definition main.c:117
static const ra8_port_pin_t k_mlun_pin_hs_pwr
J7 host-power switch (PD07): HIGH = U18 supplies VBUS (UM 6.2).
Definition main.c:120
static void mlun_route_usb_or_halt(void)
Route both ports' pins: FS as device, HS as host.
Definition main.c:735
static UINT mlun_msc_write(VOID *storage, ULONG lun, UCHAR *data_pointer, ULONG number_blocks, ULONG lba, ULONG *media_status)
Storage media-write callback: reject (all LUNs are read-only).
Definition main.c:438
static UINT mlun_usbx_stack_up(void)
Bring USBX system + device stack up with the MSC framework.
Definition main.c:506
static const ra8_port_pin_t k_mlun_pin_fs_dm
USBFS D- (P8_15).
Definition main.c:114
static UINT mlun_msc_read(VOID *storage, ULONG lun, UCHAR *data_pointer, ULONG number_blocks, ULONG lba, ULONG *media_status)
Storage media-read callback: synthesize the per-LUN pattern.
Definition main.c:388
static void mlun_panic_halt(void)
Halt forever in WFI – panic stop on init failure.
Definition main.c:712
static const ra8_port_pin_t k_mlun_pin_fs_vbus
USBFS VBUS sense pin (P4_07, PSEL = 0x13).
Definition main.c:105
static VOID mlun_device_worker(ULONG arg)
Device-side worker: bring the 2-LUN FS device up, then park.
Definition main.c:613
static const ra8_port_pin_t k_mlun_pin_fs_vbusen
USBFS VBUSEN (P5_00) – GPIO LOW for the device role.
Definition main.c:108
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_led_init(ra8_board_led_id_t led)
Configure led as a digital output, initial level low (off).
@ k_ra8_board_led2
LED2, GREEN, P303 (jumper E26).
@ k_ra8_board_led1
LED1, BLUE, P600 (jumper E27).
ra8_err_t ra8_board_io_expander_set_usbhs_host_mode(void)
Drive the U15 I/O expander to select USB-HS HOST mode.
ra8_err_t ra8_board_uart_console_init(uint32_t baud)
Configure SCI8 + PD02/PD03 as the debug-console UART.
@ k_ra8_board_usbhs_pin_pwr
PD07 J7 host-power switch (HIGH = U18 drives 5 V VBUS).
@ k_ra8_board_usbhs_pin_vbus
P4_08 USBHS_VBUS sense.
@ k_ra8_board_usbfs_pin_dm
P8_15 D-.
@ k_ra8_board_usbfs_pin_vbusen
P5_00 VBUSEN GPIO.
@ k_ra8_board_usbfs_pin_vbus
P4_07 VBUS sense.
@ k_ra8_board_usbfs_pin_dp
P8_14 D+.
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
ra8_err_t ra8_cgc_usbfs_clock_enable(void)
Bring up the USB-FS module clock (USBCKCR / USBCKDIVCR).
ra8_err_t ra8_cgc_usbhs_pll_enable(void)
Bring up the USBHS PHY reference clock + module clock.
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
void * memset(void *dst, int value, size_t n)
Fill memory with a constant byte value.
PSEL codes for peripheral pin routing on the RA8D2.
@ k_ra8_psel_usb_fs
10011b: USB Full-Speed.
@ k_ra8_psel_usb_hs
10100b: USB High-Speed (e.g.
NVIC + ICU IELSR allocator.
void ra8_isr_globals_enable(void)
Globally enable maskable interrupts (PRIMASK = 0).
Definition ra8_isr.c:439
Typed Port / Pin Constants for the RA8D2 IOPORT Module.
ra8_port_pin_t
Packed (port << 8) | pin pin identifier.
@ k_ra8_level_high
Drive or read 1.
@ k_ra8_level_low
Drive or read 0.
High-level GPIO helpers on top of the PORT + PFS register layer.
ra8_err_t ra8_gpio_output_init(ra8_port_pin_t pin, ra8_level_t init_level)
Configure a pin as a digital output and drive it to an initial level.
Definition gpio.c:88
ra8_err_t ra8_pfs_route_peripheral(ra8_port_pin_t pin, ra8_psel_t psel, const char *owner)
Route a pin to a non-IRQ peripheral function via PFS.PSEL.
Definition gpio.c:238
void ux_dcd_ra8_usb_irq_reenable(void)
Storm-guard recovery: zero the run counter and re-enable the USB IRQ.
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_time_on_tick(void)
SysTick IRQ handler – called from the vector table.
Definition ra8_time.c:170
Native USB controller driver public API (device + host modes).
ra8_err_t ra8_usb_device_attach(ra8_usb_speed_t speed, bool attached)
Raise / drop the D+ pull-up to advertise the device to the host.
@ k_ra8_usb_speed_fs
Full-Speed controller (USBFS @ 0x40250000).
Native USB host-side MSC (Mass Storage Class) class layer.
unsigned int UINT
ThreadX-compatible unsigned int (host stub).
#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.
Shared constants + cross-TU prototypes for usb_selftest_mlun.
void mlun_pattern_fill(uint32_t lun, uint32_t lba, UCHAR *out)
Fill one 512-byte sector with this LUN/LBA's deterministic bytes.
@ k_mlun_count
Logical units exposed (UX_MAX_SLAVE_LUN).
@ k_mlun_block_size
SCSI logical block size.
@ k_mlun_sectors
512-byte sectors per LUN.
VOID mlun_host_worker(ULONG arg)
Host-side worker: retry the full pass until it succeeds.
@ k_mlun_thread_stack
Device worker stack (bytes).
@ k_mlun_host_stack
Host worker stack (bytes).
@ k_mlun_dev_priority
Device bring-up worker priority.
@ k_mlun_idle_ticks
Parked-loop back-off (ticks).
@ k_mlun_usbx_pool_bytes
USBX memory pool (bytes).
@ k_mlun_host_priority
Host worker priority (below USBX).
@ k_mlun_baud
J-Link OB CDC log baud.
USBX device-controller-driver (DCD) bridge to ra8_usb.
ra8_err_t ux_dcd_ra8_usb_initialize(ra8_usb_speed_t speed)
Register the ra8_usb bridge as the active USBX DCD.