ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
usb_selftest_hs_host_host.c
Go to the documentation of this file.
1
27
28#include <stdint.h>
29#include <string.h>
30
32
33#ifndef RA8_OFF_TARGET
34
35#include "ra8_board_ek_ra8d2.h"
36#include "ra8_fs.h"
37#include "ra8_io_blockdev.h"
39#include "ra8_time.h"
40#include "ra8_usb_hmsc.h"
41
42/* -------------------------------------------------------------------------- */
43/* J-Link probes */
44/* -------------------------------------------------------------------------- */
45
47static volatile uint32_t s_dbg_phase;
49static volatile uint32_t s_dbg_verified_bytes;
51static volatile uint32_t s_dbg_mismatch_off = (uint32_t)k_selftest_no_mismatch;
53static volatile uint32_t s_dbg_verify_ms;
55static volatile uint32_t s_dbg_pass_count;
56
57/* -------------------------------------------------------------------------- */
58/* Console helpers (SCI8 -> J-Link OB CDC) */
59/* -------------------------------------------------------------------------- */
60
79static uint8_t selftest_nibble_to_hex(uint32_t nibble)
80{
81 if (nibble < k_selftest_hex_digit_split) {
82 return (uint8_t)((uint8_t)'0' + (uint8_t)nibble);
83 }
84 return (uint8_t)((uint8_t)'A' + (uint8_t)nibble - (uint8_t)k_selftest_hex_digit_split);
85}
86
105static uint32_t selftest_str_len(const char* text)
106{
107 uint32_t len = 0U;
108 while (len < (uint32_t)k_selftest_print_cap) {
109 if (text[len] == '\0') {
110 break;
111 }
112 len++;
113 }
114 return len;
115}
116
136[[nodiscard]] static ra8_err_t selftest_sci_write(const uint8_t* data, uint32_t len)
137{
138 return ra8_board_uart_console_write(data, (size_t)len);
139}
140
159[[nodiscard]] static ra8_err_t selftest_print(const char* text)
160{
161 return selftest_sci_write((const uint8_t*)text, selftest_str_len(text));
162}
163
182[[nodiscard]] static ra8_err_t selftest_print_dec(uint32_t value)
183{
184 uint8_t scratch[k_selftest_dec_chars_u32] = {};
185 uint8_t out[k_selftest_dec_chars_u32] = {};
186 uint8_t count = 0U;
187 uint32_t v = value;
188 if (v == 0U) {
189 out[0] = (uint8_t)'0';
190 return selftest_sci_write(out, 1U);
191 }
192 while (v != 0U) {
193 if (count >= (uint8_t)k_selftest_dec_chars_u32) {
194 break;
195 }
196 scratch[count] = (uint8_t)((uint8_t)'0' + (uint8_t)(v % k_selftest_dec_radix));
197 v = v / k_selftest_dec_radix;
198 count++;
199 }
200 for (uint8_t i = 0U; i < count; i++) {
201 out[i] = scratch[count - 1U - i];
202 }
203 return selftest_sci_write(out, (uint32_t)count);
204}
205
225[[nodiscard]] static ra8_err_t selftest_print_hex(uint32_t value, uint8_t digits)
226{
227 uint8_t out[k_selftest_hex_chars_u32] = {};
228 uint8_t width = digits;
229 if (width > (uint8_t)k_selftest_hex_chars_u32) {
230 width = (uint8_t)k_selftest_hex_chars_u32;
231 }
232 for (uint8_t i = 0U; i < width; i++) {
233 const uint8_t shift = (uint8_t)((width - 1U - i) * k_selftest_nibble_bits);
234 out[i] = selftest_nibble_to_hex((value >> shift) & k_selftest_nibble_mask);
235 }
236 return selftest_sci_write(out, (uint32_t)width);
237}
238
259[[nodiscard]] static ra8_err_t selftest_print_fail(const char* what, ra8_err_t err)
260{
261 ra8_err_t e = selftest_print("ra8d2 selftest: FAIL ");
262 if (e != k_ra8_ok) {
263 return e;
264 }
265 e = selftest_print(what);
266 if (e != k_ra8_ok) {
267 return e;
268 }
269 e = selftest_print(" err=0x");
270 if (e != k_ra8_ok) {
271 return e;
272 }
273 e = selftest_print_hex((uint32_t)err, (uint8_t)k_selftest_hex_chars_u32);
274 if (e != k_ra8_ok) {
275 return e;
276 }
277 return selftest_print("\r\n");
278}
279
280/* -------------------------------------------------------------------------- */
281/* Host side: ra8_fs mount over the ra8_io USB-MSC block device */
282/* -------------------------------------------------------------------------- */
283
299
313
332static const char* selftest_fs_type_name(ra8_fs_type_t type)
333{
334 switch (type) {
336 return "fat12";
338 return "fat16";
340 return "fat32";
342 return "exfat";
344 default:
345 return "unknown";
346 }
347}
348
370[[nodiscard]] static ra8_err_t selftest_mount_volume(ra8_fs_mount_t** out_mount)
371{
374 (uint8_t)k_selftest_target_lun);
375 if (err != k_ra8_ok) {
376 (void)selftest_print_fail("blockdev", err);
377 return err;
378 }
379 ra8_fs_backend_t backend = {};
381 if (err != k_ra8_ok) {
382 (void)selftest_print_fail("backend", err);
383 return err;
384 }
385 err = ra8_fs_mount(&backend, out_mount);
386 if (err != k_ra8_ok) {
387 (void)selftest_print_fail("mount", err);
388 return err;
389 }
390 err = selftest_print("ra8d2 selftest: mounted fs=");
391 if (err != k_ra8_ok) {
392 return err;
393 }
394 err = selftest_print(selftest_fs_type_name((*out_mount)->type));
395 if (err != k_ra8_ok) {
396 return err;
397 }
398 return selftest_print("\r\n");
399}
400
431[[nodiscard]] static ra8_err_t selftest_verify_mram_raw(void)
432{
433 static uint8_t s_burst[k_selftest_burst_bytes] = {};
434
437 const uint32_t t0 = ra8_time_ms();
438 for (uint32_t blk = 0U; blk < (uint32_t)k_fat_mram_clusters;
439 blk += (uint32_t)k_selftest_burst_blocks) {
440 const uint32_t lba = (uint32_t)k_fat_data_lba + blk;
442 lba,
443 (uint16_t)k_selftest_burst_blocks,
444 s_burst);
445 if (err != k_ra8_ok) {
446 (void)selftest_print_fail("READ(10) burst", err);
447 return err;
448 }
449 const uint32_t offset = blk * (uint32_t)k_selftest_block_size;
450 const uint8_t* mram = (const uint8_t*)(uintptr_t)((uint32_t)k_mram_base_addr + offset);
451 if (memcmp(s_burst, mram, (size_t)k_selftest_burst_bytes) != 0) {
452 s_dbg_mismatch_off = offset;
453 (void)selftest_print_fail("content mismatch at 0x", (ra8_err_t)offset);
455 }
456 s_dbg_verified_bytes = offset + (uint32_t)k_selftest_burst_bytes;
457 }
459 return k_ra8_ok;
460}
461
478[[nodiscard]] static ra8_err_t selftest_print_verify_verdict(void)
479{
480 ra8_err_t err = selftest_print("ra8d2 selftest: verified ");
481 if (err != k_ra8_ok) {
482 return err;
483 }
485 if (err != k_ra8_ok) {
486 return err;
487 }
488 err = selftest_print(" bytes vs MRAM in ");
489 if (err != k_ra8_ok) {
490 return err;
491 }
493 if (err != k_ra8_ok) {
494 return err;
495 }
496 err = selftest_print(" ms (");
497 if (err != k_ra8_ok) {
498 return err;
499 }
500 uint32_t ms = s_dbg_verify_ms;
501 if (ms == 0U) {
502 ms = 1U;
503 }
504 const uint32_t kib_per_s =
505 (uint32_t)(((uint64_t)s_dbg_verified_bytes * (uint64_t)k_selftest_ms_per_sec) /
506 ((uint64_t)ms * (uint64_t)k_selftest_bytes_per_kib));
507 err = selftest_print_dec(kib_per_s);
508 if (err != k_ra8_ok) {
509 return err;
510 }
511 return selftest_print(" KiB/s)\r\n");
512}
513
543[[nodiscard]] static ra8_err_t selftest_write_protect_probe(void)
544{
545 static uint8_t s_wp_buf[k_selftest_block_size] = {};
546
547 ra8_err_t err = selftest_print("ra8d2 selftest: WRITE(10) into RO LUN must be rejected...\r\n");
548 if (err != k_ra8_ok) {
549 return err;
550 }
552 (uint32_t)k_selftest_wp_probe_lba,
553 1U,
554 s_wp_buf);
555 if (wr == k_ra8_ok) {
556 (void)selftest_print_fail("write unexpectedly accepted", k_ra8_err_invalid_state);
558 }
559 err = selftest_print("ra8d2 selftest: write rejected (code 0x");
560 if (err != k_ra8_ok) {
561 return err;
562 }
563 err = selftest_print_hex((uint32_t)wr, (uint8_t)k_selftest_hex_chars_u32);
564 if (err != k_ra8_ok) {
565 return err;
566 }
567 return selftest_print("), MRAM protected\r\n");
568}
569
591{
593 ra8_err_t err = selftest_print("ra8d2 selftest: host up on USB-HS, probing the loop...\r\n");
594 if (err != k_ra8_ok) {
595 return err;
596 }
598 if (err != k_ra8_ok) {
599 (void)selftest_print_fail("host init", err);
600 return err;
601 }
603 err = ra8_usb_hmsc_enumerate(out_device);
604 if (err != k_ra8_ok) {
605 (void)selftest_print_fail("enumerate", err);
606 (void)ra8_usb_hmsc_close();
607 return err;
608 }
609 err = selftest_print("ra8d2 selftest: enumerated vid=0x");
610 if (err != k_ra8_ok) {
611 return err;
612 }
613 err = selftest_print_hex((uint32_t)out_device->vendor_id, (uint8_t)k_selftest_hex_chars_u16);
614 if (err != k_ra8_ok) {
615 return err;
616 }
617 err = selftest_print(" pid=0x");
618 if (err != k_ra8_ok) {
619 return err;
620 }
621 err = selftest_print_hex((uint32_t)out_device->product_id, (uint8_t)k_selftest_hex_chars_u16);
622 if (err != k_ra8_ok) {
623 return err;
624 }
625 return selftest_print(" over the loop cable\r\n");
626}
627
628[[nodiscard]] ra8_err_t selftest_host_pass(void)
629{
630 ra8_usb_hmsc_device_t device = {};
631 ra8_err_t err = selftest_host_enumerate(&device);
632 if (err != k_ra8_ok) {
633 return err;
634 }
635
636 /* Mount proves the host can PARSE the FAT16 volume over the loop;
637 * then unmount and do the heavy integrity check with raw multi-block
638 * READ(10) (ra8_fs's per-cluster metadata re-reads are far too slow
639 * for a 1 MiB sweep). */
641 ra8_fs_mount_t* mount = nullptr;
642 err = selftest_mount_volume(&mount);
643 if (err != k_ra8_ok) {
644 (void)ra8_usb_hmsc_close();
645 return err;
646 }
647 (void)ra8_fs_unmount(mount);
648
651 if (err == k_ra8_ok) {
653 }
654 if (err == k_ra8_ok) {
657 }
658 if (err != k_ra8_ok) {
659 (void)ra8_usb_hmsc_close();
660 return err;
661 }
662
665 err = selftest_print("ra8d2 selftest: USB SELFTEST CONFIG A PASS\r\n");
666 if (err != k_ra8_ok) {
667 return err;
668 }
670 return k_ra8_ok;
671}
672
673#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.
@ 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_hs
High-Speed controller (USBHS @ 0x40351000).
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.
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_block_size
SCSI logical block size (bytes).
@ 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.
ra8_err_t selftest_host_pass(void)
One full host-side pass: enumerate, mount, browse, verify, WP.
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 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).
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.
Shared constants + step prototypes for the USB self-loop config A app.
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).