ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
usb_selftest_fs_host_host.c
Go to the documentation of this file.
1
34
35#include <stdint.h>
36#include <string.h>
37
39
40#ifndef RA8_OFF_TARGET
41
42#include "ra8_board_ek_ra8d2.h"
43#include "ra8_err.h"
44#include "ra8_fs.h"
45#include "ra8_io_blockdev.h"
47#include "ra8_time.h"
48#include "ra8_usb_hmsc.h"
49#include "tx_api.h"
50
51/* -------------------------------------------------------------------------- */
52/* J-Link probes (host side) */
53/* -------------------------------------------------------------------------- */
54
56static volatile uint32_t s_dbg_phase;
58static volatile uint32_t s_dbg_verified_bytes;
60static volatile uint32_t s_dbg_mismatch_off = (uint32_t)k_selftest_no_mismatch;
62static volatile uint32_t s_dbg_verify_ms;
64static volatile uint32_t s_dbg_pass_count;
65
66/* -------------------------------------------------------------------------- */
67/* Console helpers (SCI8 -> J-Link OB CDC) */
68/* -------------------------------------------------------------------------- */
69
88static uint8_t selftest_nibble_to_hex(uint32_t nibble)
89{
90 if (nibble < k_selftest_hex_digit_split) {
91 return (uint8_t)((uint8_t)'0' + (uint8_t)nibble);
92 }
93 return (uint8_t)((uint8_t)'A' + (uint8_t)nibble - (uint8_t)k_selftest_hex_digit_split);
94}
95
114static uint32_t selftest_str_len(const char* text)
115{
116 uint32_t len = 0U;
117 while (len < (uint32_t)k_selftest_print_cap) {
118 if (text[len] == '\0') {
119 break;
120 }
121 len++;
122 }
123 return len;
124}
125
145[[nodiscard]] static ra8_err_t selftest_sci_write(const uint8_t* data, uint32_t len)
146{
147 return ra8_board_uart_console_write(data, (size_t)len);
148}
149
168[[nodiscard]] static ra8_err_t selftest_print(const char* text)
169{
170 return selftest_sci_write((const uint8_t*)text, selftest_str_len(text));
171}
172
191[[nodiscard]] static ra8_err_t selftest_print_dec(uint32_t value)
192{
193 uint8_t scratch[k_selftest_dec_chars_u32] = {};
194 uint8_t out[k_selftest_dec_chars_u32] = {};
195 uint8_t count = 0U;
196 uint32_t v = value;
197 if (v == 0U) {
198 out[0] = (uint8_t)'0';
199 return selftest_sci_write(out, 1U);
200 }
201 while (v != 0U) {
202 if (count >= (uint8_t)k_selftest_dec_chars_u32) {
203 break;
204 }
205 scratch[count] = (uint8_t)((uint8_t)'0' + (uint8_t)(v % k_selftest_dec_radix));
206 v = v / k_selftest_dec_radix;
207 count++;
208 }
209 for (uint8_t i = 0U; i < count; i++) {
210 out[i] = scratch[count - 1U - i];
211 }
212 return selftest_sci_write(out, (uint32_t)count);
213}
214
234[[nodiscard]] static ra8_err_t selftest_print_hex(uint32_t value, uint8_t digits)
235{
236 uint8_t out[k_selftest_hex_chars_u32] = {};
237 uint8_t width = digits;
238 if (width > (uint8_t)k_selftest_hex_chars_u32) {
239 width = (uint8_t)k_selftest_hex_chars_u32;
240 }
241 for (uint8_t i = 0U; i < width; i++) {
242 const uint8_t shift = (uint8_t)((width - 1U - i) * k_selftest_nibble_bits);
243 out[i] = selftest_nibble_to_hex((value >> shift) & k_selftest_nibble_mask);
244 }
245 return selftest_sci_write(out, (uint32_t)width);
246}
247
268[[nodiscard]] static ra8_err_t selftest_print_fail(const char* what, ra8_err_t err)
269{
270 ra8_err_t e = selftest_print("ra8d2 selftest: FAIL ");
271 if (e != k_ra8_ok) {
272 return e;
273 }
274 e = selftest_print(what);
275 if (e != k_ra8_ok) {
276 return e;
277 }
278 e = selftest_print(" err=0x");
279 if (e != k_ra8_ok) {
280 return e;
281 }
282 e = selftest_print_hex((uint32_t)err, (uint8_t)k_selftest_hex_chars_u32);
283 if (e != k_ra8_ok) {
284 return e;
285 }
286 return selftest_print("\r\n");
287}
288
289/* -------------------------------------------------------------------------- */
290/* Host side: ra8_fs mount over the ra8_io USB-MSC block device */
291/* -------------------------------------------------------------------------- */
292
308
322
341static const char* selftest_fs_type_name(ra8_fs_type_t type)
342{
343 switch (type) {
345 return "fat12";
347 return "fat16";
349 return "fat32";
351 return "exfat";
353 default:
354 return "unknown";
355 }
356}
357
379[[nodiscard]] static ra8_err_t selftest_mount_volume(ra8_fs_mount_t** out_mount)
380{
383 (uint8_t)k_selftest_target_lun);
384 if (err != k_ra8_ok) {
385 (void)selftest_print_fail("blockdev", err);
386 return err;
387 }
388 ra8_fs_backend_t backend = {};
390 if (err != k_ra8_ok) {
391 (void)selftest_print_fail("backend", err);
392 return err;
393 }
394 err = ra8_fs_mount(&backend, out_mount);
395 if (err != k_ra8_ok) {
396 (void)selftest_print_fail("mount", err);
397 return err;
398 }
399 err = selftest_print("ra8d2 selftest: mounted fs=");
400 if (err != k_ra8_ok) {
401 return err;
402 }
403 err = selftest_print(selftest_fs_type_name((*out_mount)->type));
404 if (err != k_ra8_ok) {
405 return err;
406 }
407 return selftest_print("\r\n");
408}
409
440[[nodiscard]] static ra8_err_t selftest_verify_mram_raw(void)
441{
442 static uint8_t s_burst[k_selftest_burst_bytes] = {};
443
446 const uint32_t t0 = ra8_time_ms();
447 for (uint32_t blk = 0U; blk < (uint32_t)k_fat_mram_clusters;
448 blk += (uint32_t)k_selftest_burst_blocks) {
449 const uint32_t lba = (uint32_t)k_fat_data_lba + blk;
451 lba,
452 (uint16_t)k_selftest_burst_blocks,
453 s_burst);
454 if (err != k_ra8_ok) {
455 (void)selftest_print_fail("READ(10) burst", err);
456 return err;
457 }
458 const uint32_t offset = blk * (uint32_t)k_selftest_block_size;
459 const uint8_t* mram = (const uint8_t*)(uintptr_t)((uint32_t)k_mram_base_addr + offset);
460 if (memcmp(s_burst, mram, (size_t)k_selftest_burst_bytes) != 0) {
461 s_dbg_mismatch_off = offset;
462 (void)selftest_print_fail("content mismatch at 0x", (ra8_err_t)offset);
464 }
465 s_dbg_verified_bytes = offset + (uint32_t)k_selftest_burst_bytes;
466 }
468 return k_ra8_ok;
469}
470
487[[nodiscard]] static ra8_err_t selftest_print_verify_verdict(void)
488{
489 ra8_err_t err = selftest_print("ra8d2 selftest: verified ");
490 if (err != k_ra8_ok) {
491 return err;
492 }
494 if (err != k_ra8_ok) {
495 return err;
496 }
497 err = selftest_print(" bytes vs MRAM in ");
498 if (err != k_ra8_ok) {
499 return err;
500 }
502 if (err != k_ra8_ok) {
503 return err;
504 }
505 err = selftest_print(" ms (");
506 if (err != k_ra8_ok) {
507 return err;
508 }
509 uint32_t ms = s_dbg_verify_ms;
510 if (ms == 0U) {
511 ms = 1U;
512 }
513 const uint32_t kib_per_s =
514 (uint32_t)(((uint64_t)s_dbg_verified_bytes * (uint64_t)k_selftest_ms_per_sec) /
515 ((uint64_t)ms * (uint64_t)k_selftest_bytes_per_kib));
516 err = selftest_print_dec(kib_per_s);
517 if (err != k_ra8_ok) {
518 return err;
519 }
520 return selftest_print(" KiB/s)\r\n");
521}
522
552[[nodiscard]] static ra8_err_t selftest_write_protect_probe(void)
553{
554 static uint8_t s_wp_buf[k_selftest_block_size] = {};
555
556 ra8_err_t err = selftest_print("ra8d2 selftest: WRITE(10) into RO LUN must be rejected...\r\n");
557 if (err != k_ra8_ok) {
558 return err;
559 }
561 (uint32_t)k_selftest_wp_probe_lba,
562 1U,
563 s_wp_buf);
564 if (wr == k_ra8_ok) {
565 (void)selftest_print_fail("write unexpectedly accepted", k_ra8_err_invalid_state);
567 }
568 err = selftest_print("ra8d2 selftest: write rejected (code 0x");
569 if (err != k_ra8_ok) {
570 return err;
571 }
572 err = selftest_print_hex((uint32_t)wr, (uint8_t)k_selftest_hex_chars_u32);
573 if (err != k_ra8_ok) {
574 return err;
575 }
576 return selftest_print("), MRAM protected\r\n");
577}
578
600{
602 ra8_err_t err = selftest_print("ra8d2 selftest: host up on USB-FS, probing the loop...\r\n");
603 if (err != k_ra8_ok) {
604 return err;
605 }
607 if (err != k_ra8_ok) {
608 (void)selftest_print_fail("host init", err);
609 return err;
610 }
612 err = ra8_usb_hmsc_enumerate(out_device);
613 if (err != k_ra8_ok) {
614 (void)selftest_print_fail("enumerate", err);
615 (void)ra8_usb_hmsc_close();
616 return err;
617 }
618 err = selftest_print("ra8d2 selftest: enumerated vid=0x");
619 if (err != k_ra8_ok) {
620 return err;
621 }
622 err = selftest_print_hex((uint32_t)out_device->vendor_id, (uint8_t)k_selftest_hex_chars_u16);
623 if (err != k_ra8_ok) {
624 return err;
625 }
626 err = selftest_print(" pid=0x");
627 if (err != k_ra8_ok) {
628 return err;
629 }
630 err = selftest_print_hex((uint32_t)out_device->product_id, (uint8_t)k_selftest_hex_chars_u16);
631 if (err != k_ra8_ok) {
632 return err;
633 }
634 return selftest_print(" over the loop cable\r\n");
635}
636
655[[nodiscard]] static ra8_err_t selftest_host_pass(void)
656{
657 ra8_usb_hmsc_device_t device = {};
658 ra8_err_t err = selftest_host_enumerate(&device);
659 if (err != k_ra8_ok) {
660 return err;
661 }
662
663 /* Mount proves the host can PARSE the FAT16 volume over the loop;
664 * then unmount and do the heavy integrity check with raw multi-block
665 * READ(10) (ra8_fs's per-cluster metadata re-reads are far too slow
666 * for a 1 MiB sweep). */
668 ra8_fs_mount_t* mount = nullptr;
669 err = selftest_mount_volume(&mount);
670 if (err != k_ra8_ok) {
671 (void)ra8_usb_hmsc_close();
672 return err;
673 }
674 (void)ra8_fs_unmount(mount);
675
678 if (err == k_ra8_ok) {
680 }
681 if (err == k_ra8_ok) {
684 }
685 if (err != k_ra8_ok) {
686 (void)ra8_usb_hmsc_close();
687 return err;
688 }
689
692 err = selftest_print("ra8d2 selftest: USB SELFTEST CONFIG B PASS\r\n");
693 if (err != k_ra8_ok) {
694 return err;
695 }
697 return k_ra8_ok;
698}
699
719{
720 (void)arg;
721
723 for (;;) {
724 const ra8_err_t err = selftest_host_pass();
725 if (err == k_ra8_ok) {
726 break;
727 }
729 }
730 while (1) {
732 }
733}
734
735#endif /* !RA8_OFF_TARGET */
Board-support layer for the Renesas EK-RA8D2 v1 evaluation kit.
ra8_err_t ra8_board_led_on(ra8_board_led_id_t led)
Drive led HIGH (light it).
@ k_ra8_board_led2
LED2, GREEN, P303 (jumper E26).
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.
Error Code Definitions for ra8-firmware.
@ k_ra8_err_invalid_state
Module in wrong state for requested operation.
Definition ra8_err.h:161
@ 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
int memcmp(const void *a, const void *b, size_t n)
Compare bytes in two memory areas.
Minimal FAT12/FAT16/FAT32 filesystem adapter (read + write).
ra8_err_t ra8_fs_mount(const ra8_fs_backend_t *backend, ra8_fs_mount_t **out_handle)
Mount a FAT volume from a block-device backend, auto-selecting the first partition.
ra8_err_t ra8_fs_unmount(ra8_fs_mount_t *handle)
Unmount a previously mounted volume and release its slot.
ra8_fs_type_t
FAT variant detected from the BPB cluster-count rule.
@ k_ra8_fs_type_fat12
count_of_clusters < 4085.
@ k_ra8_fs_type_fat32
count_of_clusters >= 65525.
@ k_ra8_fs_type_exfat
exFAT (read + streaming write + format).
@ k_ra8_fs_type_fat16
4085 <= count_of_clusters < 65525.
@ k_ra8_fs_type_unknown
Not yet detected / mount failed.
ra8_io block-device fabric – one LBA vtable across every storage medium.
ra8_err_t ra8_io_blockdev_as_fs_backend(const ra8_io_blockdev_t *bd, ra8_fs_backend_t *out)
Expose a bound block device as an ra8_fs_backend_t for ra8_fs.
ra8_io block-device backend over a hosted USB mass-storage device.
ra8_err_t ra8_io_blockdev_usbmsc_init(ra8_io_blockdev_t *bd, ra8_io_blockdev_usbmsc_state_t *state, uint8_t lun)
Bind a hosted USB mass-storage backend into a caller-owned handle.
SysTick-based tick counter, delay and timestamp helpers.
uint32_t ra8_time_ms(void)
Get the current 1 kHz tick count.
Definition ra8_time.c:108
@ k_ra8_usb_speed_fs
Full-Speed controller (USBFS @ 0x40250000).
Native USB host-side MSC (Mass Storage Class) class layer.
ra8_err_t ra8_usb_hmsc_read10(uint8_t target_lun, uint32_t lba, uint16_t block_count, uint8_t *out_buf)
Issue a SCSI READ(10) (opcode 0x28) over BBB.
ra8_err_t ra8_usb_hmsc_enumerate(ra8_usb_hmsc_device_t *out_device)
Enumerate the attached MSC device end to end (polled).
ra8_err_t ra8_usb_hmsc_close(void)
Tear down the host-MSC driver and release the controller.
ra8_err_t ra8_usb_hmsc_init(ra8_usb_speed_t speed)
Bring up the host-MSC driver on a chosen USB controller.
ra8_err_t ra8_usb_hmsc_write10(uint8_t target_lun, uint32_t lba, uint16_t block_count, const uint8_t *in_buf)
Issue a SCSI WRITE(10) (opcode 0x2A) over BBB.
#define tx_thread_sleep
unsigned long ULONG
ThreadX-compatible unsigned long (host stub).
Block-device interface that ra8_fs runs on top of.
Cached parse of one mounted FAT volume.
Caller-allocated block-device handle binding a backend to its context.
Caller-owned private state for a USB mass-storage block device.
Snapshot of the attached MSC device, passed to the attach callback.
uint16_t vendor_id
idVendor from device descriptor.
uint16_t product_id
idProduct from device descriptor.
@ k_selftest_hex_digit_split
Threshold between '0-9'/'A-F'.
@ k_selftest_hex_chars_u16
16-bit value -> "ABCD".
@ k_selftest_dec_chars_u32
Max digits for a 32-bit count.
@ k_selftest_hex_chars_u32
32-bit value -> "ABCDEF01".
@ k_selftest_nibble_bits
Bits per hex nibble.
static ra8_io_blockdev_usbmsc_state_t s_selftest_usb_state
Caller-owned backend state for s_selftest_usb_dev.
@ k_selftest_phase_wp
Write-protect rejection test.
@ k_selftest_phase_mount
Mounting the FAT16 volume.
@ k_selftest_phase_enum
Enumerating the FS device.
@ k_selftest_phase_pass
Full config A pass.
@ k_selftest_phase_host_init
ra8_usb_hmsc_init issued.
@ k_selftest_phase_verify
Streaming + comparing MRAM.BIN.
static ra8_io_blockdev_t s_selftest_usb_dev
Block device fronting the hosted MSC volume for this ladder.
@ k_selftest_wp_probe_lba
Data-region LBA for WP test.
@ k_selftest_ms_per_sec
Milliseconds per second.
@ k_selftest_target_lun
Single-LUN device.
@ k_selftest_burst_bytes
8 x 512 B burst buffer size.
@ k_selftest_burst_blocks
Blocks per READ(10) burst.
@ k_selftest_no_mismatch
Probe: no mismatch found.
@ k_selftest_bytes_per_kib
Bytes per KiB (rate math).
static volatile uint32_t s_dbg_verified_bytes
Bytes content-verified so far this pass.
static volatile uint32_t s_dbg_mismatch_off
First mismatching MRAM offset (k_selftest_no_mismatch = none).
static volatile uint32_t s_dbg_verify_ms
Milliseconds the 1 MiB verify streamed for.
@ k_selftest_dec_radix
Base for decimal conversion.
@ k_selftest_nibble_mask
4-bit nibble mask.
@ k_fat_data_lba
First data sector (cluster 2).
@ k_fat_mram_clusters
Clusters backed by MRAM (1 MiB).
@ k_mram_base_addr
MRAM code window base address.
@ k_selftest_boot_wait_ticks
Host start delay (1 ms ticks).
@ k_selftest_block_size
SCSI logical block size (bytes).
@ k_selftest_idle_ticks
Parked-loop back-off (ticks).
@ k_selftest_retry_ticks
Pause between ladder retries.
@ k_selftest_print_cap
Bound for console-string scans.
static ra8_err_t selftest_print_dec(uint32_t value)
Print a uint32_t as ASCII decimal.
static const char * selftest_fs_type_name(ra8_fs_type_t type)
Map a detected filesystem type to a printable name.
static ra8_err_t selftest_print_hex(uint32_t value, uint8_t digits)
Print a value as fixed-width uppercase hex.
static uint32_t selftest_str_len(const char *text)
Bounded ASCII string length (cap k_selftest_print_cap).
static ra8_err_t selftest_host_enumerate(ra8_usb_hmsc_device_t *out_device)
Bring the host controller up and enumerate the loop device.
static ra8_err_t selftest_verify_mram_raw(void)
Raw multi-block READ(10) of the MRAM data region vs MRAM.
static uint8_t selftest_nibble_to_hex(uint32_t nibble)
Format one nibble (0..15) into an uppercase hex character.
static ra8_err_t selftest_sci_write(const uint8_t *data, uint32_t len)
Push a literal block over the board UART console (SCI8) polled.
static ra8_err_t selftest_print(const char *text)
Print a NUL-terminated ASCII string over the console.
static ra8_err_t selftest_print_verify_verdict(void)
Print the verify verdict line (bytes + duration + rate).
VOID selftest_host_worker(ULONG arg)
Host-side worker: retry the full pass until it succeeds.
static ra8_err_t selftest_write_protect_probe(void)
WRITE(10) into the read-only LUN must be rejected.
static ra8_err_t selftest_print_fail(const char *what, ra8_err_t err)
Print "FAIL <what> err=0xNNNNNNNN" on its own line.
static ra8_err_t selftest_mount_volume(ra8_fs_mount_t **out_mount)
Mount the loop device's volume through the USB-MSC backend.
static ra8_err_t selftest_host_pass(void)
One full host-side pass: enumerate, mount, verify, WP.
examples/ek_ra8d2/hw_validated/hil/usb_selftest_fs_host/inc/usb_selftest_fs_host_steps....
static volatile uint32_t s_dbg_phase
Host-ladder phase marker (wlun_phase_t).
static volatile uint32_t s_dbg_pass_count
Completed full passes (sticky success counter).