ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
usb_selftest_ospi_host.c
Go to the documentation of this file.
1
27
28#include <stdint.h>
29#include <string.h>
30
31#include "ra8_board_ek_ra8d2.h"
32#include "ra8_err.h"
33#include "ra8_fs.h"
34#include "ra8_io_blockdev.h"
36#include "ra8_time.h"
37#include "ra8_usb_hmsc.h"
39
40#ifndef RA8_OFF_TARGET
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/* Host side: ra8_fs mount over the ra8_io USB-MSC block device */
59/* -------------------------------------------------------------------------- */
60
76
90
109static const char* selftest_fs_type_name(ra8_fs_type_t type)
110{
111 switch (type) {
113 return "fat12";
115 return "fat16";
117 return "fat32";
119 return "exfat";
121 default:
122 return "unknown";
123 }
124}
125
147[[nodiscard]] static ra8_err_t selftest_mount_volume(ra8_fs_mount_t** out_mount)
148{
151 (uint8_t)k_selftest_target_lun);
152 if (err != k_ra8_ok) {
153 (void)selftest_print_fail("blockdev", err);
154 return err;
155 }
156 ra8_fs_backend_t backend = {};
158 if (err != k_ra8_ok) {
159 (void)selftest_print_fail("backend", err);
160 return err;
161 }
162 err = ra8_fs_mount(&backend, out_mount);
163 if (err != k_ra8_ok) {
164 (void)selftest_print_fail("mount", err);
165 return err;
166 }
167 err = selftest_print("ra8d2 selftest: mounted fs=");
168 if (err != k_ra8_ok) {
169 return err;
170 }
171 err = selftest_print(selftest_fs_type_name((*out_mount)->type));
172 if (err != k_ra8_ok) {
173 return err;
174 }
175 return selftest_print("\r\n");
176}
177
209[[nodiscard]] static ra8_err_t selftest_verify_ospi_raw(void)
210{
211 static uint8_t s_burst[k_selftest_burst_bytes] = {};
212 static uint8_t s_expect[k_selftest_block_size] = {};
213
216 const uint32_t t0 = ra8_time_ms();
217 for (uint32_t blk = 0U; blk < (uint32_t)k_fat_data_clusters;
218 blk += (uint32_t)k_selftest_burst_blocks) {
219 const uint32_t lba = (uint32_t)k_fat_data_lba + blk;
221 lba,
222 (uint16_t)k_selftest_burst_blocks,
223 s_burst);
224 if (err != k_ra8_ok) {
225 (void)selftest_print_fail("READ(10) burst", err);
226 return err;
227 }
228 for (uint32_t s = 0U; s < (uint32_t)k_selftest_burst_blocks; s++) {
229 const uint32_t win_sector = blk + s;
230 selftest_pattern_fill(win_sector, s_expect);
231 const uint32_t boff = s * (uint32_t)k_selftest_block_size;
232 if (memcmp(&s_burst[boff], s_expect, (size_t)k_selftest_block_size) != 0) {
233 s_dbg_mismatch_off = win_sector * (uint32_t)k_selftest_block_size;
234 (void)selftest_print_fail("OSPI pattern mismatch at 0x", (ra8_err_t)s_dbg_mismatch_off);
236 }
237 }
239 (blk + (uint32_t)k_selftest_burst_blocks) * (uint32_t)k_selftest_block_size;
240 }
242 return k_ra8_ok;
243}
244
261[[nodiscard]] static ra8_err_t selftest_print_verify_verdict(void)
262{
263 ra8_err_t err = selftest_print("ra8d2 selftest: verified ");
264 if (err != k_ra8_ok) {
265 return err;
266 }
268 if (err != k_ra8_ok) {
269 return err;
270 }
271 err = selftest_print(" bytes vs OSPI pattern in ");
272 if (err != k_ra8_ok) {
273 return err;
274 }
276 if (err != k_ra8_ok) {
277 return err;
278 }
279 err = selftest_print(" ms (");
280 if (err != k_ra8_ok) {
281 return err;
282 }
283 uint32_t ms = s_dbg_verify_ms;
284 if (ms == 0U) {
285 ms = 1U;
286 }
287 const uint32_t kib_per_s =
288 (uint32_t)(((uint64_t)s_dbg_verified_bytes * (uint64_t)k_selftest_ms_per_sec) /
289 ((uint64_t)ms * (uint64_t)k_selftest_bytes_per_kib));
290 err = selftest_print_dec(kib_per_s);
291 if (err != k_ra8_ok) {
292 return err;
293 }
294 return selftest_print(" KiB/s)\r\n");
295}
296
326[[nodiscard]] static ra8_err_t selftest_write_protect_probe(void)
327{
328 static uint8_t s_wp_buf[k_selftest_block_size] = {};
329
330 ra8_err_t err = selftest_print("ra8d2 selftest: WRITE(10) into RO LUN must be rejected...\r\n");
331 if (err != k_ra8_ok) {
332 return err;
333 }
335 (uint32_t)k_selftest_wp_probe_lba,
336 1U,
337 s_wp_buf);
338 if (wr == k_ra8_ok) {
339 (void)selftest_print_fail("write unexpectedly accepted", k_ra8_err_invalid_state);
341 }
342 err = selftest_print("ra8d2 selftest: write rejected (code 0x");
343 if (err != k_ra8_ok) {
344 return err;
345 }
346 err = selftest_print_hex((uint32_t)wr, (uint8_t)k_selftest_hex_chars_u32);
347 if (err != k_ra8_ok) {
348 return err;
349 }
350 return selftest_print("), OSPI window read-only\r\n");
351}
352
374{
376 ra8_err_t err = selftest_print("ra8d2 selftest: host up on USB-HS, probing the loop...\r\n");
377 if (err != k_ra8_ok) {
378 return err;
379 }
381 if (err != k_ra8_ok) {
382 (void)selftest_print_fail("host init", err);
383 return err;
384 }
386 err = ra8_usb_hmsc_enumerate(out_device);
387 if (err != k_ra8_ok) {
388 (void)selftest_print_fail("enumerate", err);
389 (void)ra8_usb_hmsc_close();
390 return err;
391 }
392 err = selftest_print("ra8d2 selftest: enumerated vid=0x");
393 if (err != k_ra8_ok) {
394 return err;
395 }
396 err = selftest_print_hex((uint32_t)out_device->vendor_id, (uint8_t)k_selftest_hex_chars_u16);
397 if (err != k_ra8_ok) {
398 return err;
399 }
400 err = selftest_print(" pid=0x");
401 if (err != k_ra8_ok) {
402 return err;
403 }
404 err = selftest_print_hex((uint32_t)out_device->product_id, (uint8_t)k_selftest_hex_chars_u16);
405 if (err != k_ra8_ok) {
406 return err;
407 }
408 return selftest_print(" over the loop cable\r\n");
409}
410
411[[nodiscard]] ra8_err_t selftest_host_pass(void)
412{
413 ra8_usb_hmsc_device_t device = {};
414 ra8_err_t err = selftest_host_enumerate(&device);
415 if (err != k_ra8_ok) {
416 return err;
417 }
418
419 /* Mount proves the host can PARSE the FAT16 volume over the loop;
420 * then unmount and do the heavy integrity check with raw multi-block
421 * READ(10) (ra8_fs's per-cluster metadata re-reads are far too slow
422 * for a 1 MiB sweep). */
424 ra8_fs_mount_t* mount = nullptr;
425 err = selftest_mount_volume(&mount);
426 if (err != k_ra8_ok) {
427 (void)ra8_usb_hmsc_close();
428 return err;
429 }
430 (void)ra8_fs_unmount(mount);
431
434 if (err == k_ra8_ok) {
436 }
437 if (err == k_ra8_ok) {
440 }
441 if (err != k_ra8_ok) {
442 (void)ra8_usb_hmsc_close();
443 return err;
444 }
445
448 err = selftest_print("ra8d2 selftest: USB SELFTEST OSPI PASS\r\n");
449 if (err != k_ra8_ok) {
450 return err;
451 }
453 return k_ra8_ok;
454}
455
456#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).
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_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_chars_u16
16-bit value -> "ABCD".
@ k_selftest_hex_chars_u32
32-bit value -> "ABCDEF01".
static ra8_err_t selftest_print_dec(uint32_t value)
Print a uint32_t as ASCII decimal.
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_err_t selftest_print_hex(uint32_t value, uint8_t digits)
Print a value as fixed-width uppercase hex.
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 ra8_err_t selftest_print(const char *text)
Print a NUL-terminated ASCII string over the console.
static volatile uint32_t s_dbg_verified_bytes
Bytes content-verified so far this pass.
static ra8_err_t selftest_print_fail(const char *what, ra8_err_t err)
Print "FAIL <what> err=0xNNNNNNNN" on its own line.
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_fat_data_lba
First data sector (cluster 2).
@ k_selftest_block_size
SCSI logical block size (bytes).
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_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_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_mount_volume(ra8_fs_mount_t **out_mount)
Mount the loop device's volume through the USB-MSC backend.
static ra8_err_t selftest_verify_ospi_raw(void)
Raw multi-block READ(10) of the MRAM data region vs MRAM.
Shared constants + step prototypes for the OSPI USB self-loop app.
@ k_fat_data_clusters
Clusters backed by MRAM (1 MiB).
void selftest_pattern_fill(uint32_t win_sector, UCHAR *out)
Compute the deterministic pattern for one window data sector.
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).