ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
usb_selftest_hs_host_msc.c
Go to the documentation of this file.
1
26
27#include <stdint.h>
28#include <string.h>
29
31
32#ifndef RA8_OFF_TARGET
33
34#include "ra8_board_ek_ra8d2.h"
35#include "tx_api.h"
36#include "ux_api.h"
37#include "ux_device_class_storage.h"
38
40static const UCHAR s_fat_oem_name[8] = {'R', 'A', '8', 'D', '2', 'F', 'W', ' '};
41
43static const UCHAR s_fat_volume_label[11] = {'R', 'A', '8', 'D', '2', ' ', 'M', 'R', 'A', 'M', ' '};
44
46static const UCHAR s_fat_fs_type[8] = {'F', 'A', 'T', '1', '6', ' ', ' ', ' '};
47
49static const UCHAR s_fat_file_name[11] = {'M', 'R', 'A', 'M', ' ', ' ', ' ', ' ', 'B', 'I', 'N'};
50
52static volatile uint32_t s_dbg_read_calls;
53
54/* -------------------------------------------------------------------------- */
55/* FAT16 synthesis (identical layout to usb_msc_mram) */
56/* -------------------------------------------------------------------------- */
57
74static void selftest_put16(UCHAR* dst, uint16_t value)
75{
76 dst[0] = (UCHAR)(value & (uint16_t)k_byte_mask);
77 dst[1] = (UCHAR)((value >> (uint16_t)k_byte_shift) & (uint16_t)k_byte_mask);
78}
79
96static void selftest_put32(UCHAR* dst, uint32_t value)
97{
98 selftest_put16(dst, (uint16_t)(value & (uint32_t)k_word_mask));
99 selftest_put16(dst + 2U, (uint16_t)(value >> (uint32_t)k_word_shift));
100}
101
118static void selftest_fat_fill_boot(UCHAR* out)
119{
120 out[k_bpb_off_jmp] = (UCHAR)k_boot_jmp0;
121 out[k_bpb_off_jmp + 1U] = (UCHAR)k_boot_jmp1;
122 out[k_bpb_off_jmp + 2U] = (UCHAR)k_boot_jmp2;
123 (void)memcpy(&out[k_bpb_off_oem], s_fat_oem_name, sizeof(s_fat_oem_name));
125 out[k_bpb_off_spc] = 1U;
127 out[k_bpb_off_nfats] = (UCHAR)k_fat_num_fats;
130 out[k_bpb_off_media] = (UCHAR)k_boot_media;
135 out[k_bpb_off_bootsig] = (UCHAR)k_boot_ext_sig;
138 (void)memcpy(&out[k_bpb_off_fstype], s_fat_fs_type, sizeof(s_fat_fs_type));
139 out[k_boot_sig_lo_off] = (UCHAR)k_boot_sig_lo;
140 out[k_boot_sig_hi_off] = (UCHAR)k_boot_sig_hi;
141}
142
162static void selftest_fat_fill_fat(uint32_t fat_sector, UCHAR* out)
163{
164 const uint32_t first_entry = fat_sector * (uint32_t)k_fat_entries_per_sec;
165 for (uint32_t j = 0U; j < (uint32_t)k_fat_entries_per_sec; j++) {
166 const uint32_t entry = first_entry + j;
167 uint16_t value = 0U;
168 if (entry == 0U) {
169 value = (uint16_t)k_fat_entry0;
170 } else if (entry == 1U) {
171 value = (uint16_t)k_fat_eoc;
172 } else if (entry < (uint32_t)k_fat_last_mram_clus) {
173 value = (uint16_t)(entry + 1U);
174 } else if (entry == (uint32_t)k_fat_last_mram_clus) {
175 value = (uint16_t)k_fat_eoc;
176 } else {
177 value = 0U;
178 }
179 selftest_put16(&out[j * 2U], value);
180 }
181}
182
201static void selftest_fat_fill_root(uint32_t root_sector, UCHAR* out)
202{
203 if (root_sector != 0U) {
204 return;
205 }
206 /* Entry 0: volume label. */
207 (void)memcpy(&out[0], s_fat_volume_label, (size_t)k_dir_name_bytes);
208 out[k_dir_off_attr] = (UCHAR)k_dir_attr_volume;
209 /* Entry 1: MRAM.BIN, read-only, cluster 2, 1 MiB. */
210 UCHAR* entry = &out[k_dir_entry_bytes];
211 (void)memcpy(entry, s_fat_file_name, (size_t)k_dir_name_bytes);
212 entry[k_dir_off_attr] = (UCHAR)k_dir_attr_read_only;
214 selftest_put32(&entry[k_dir_off_size], (uint32_t)k_mram_bytes);
215}
216
236static void selftest_fat_fill_sector(uint32_t lba, UCHAR* out)
237{
238 (void)memset(out, 0, (size_t)k_selftest_block_size);
239 if (lba == 0U) {
241 return;
242 }
243 if (lba < (uint32_t)k_fat_root_lba) {
244 selftest_fat_fill_fat(lba - (uint32_t)k_fat_fat_lba, out);
245 return;
246 }
247 if (lba < (uint32_t)k_fat_data_lba) {
248 selftest_fat_fill_root(lba - (uint32_t)k_fat_root_lba, out);
249 return;
250 }
251 const uint32_t cluster = (lba - (uint32_t)k_fat_data_lba) + (uint32_t)k_fat_first_cluster;
252 if (cluster <= (uint32_t)k_fat_last_mram_clus) {
253 const uint32_t offset =
254 (cluster - (uint32_t)k_fat_first_cluster) * (uint32_t)k_selftest_block_size;
255 const UCHAR* mram = (const UCHAR*)(uintptr_t)((uint32_t)k_mram_base_addr + offset);
256 (void)memcpy(out, mram, (size_t)k_selftest_block_size);
257 }
258}
259
260/* -------------------------------------------------------------------------- */
261/* Storage class media callbacks (read / write / status) */
262/* -------------------------------------------------------------------------- */
263
265 ULONG lun,
266 UCHAR* data_pointer,
267 ULONG number_blocks,
268 ULONG lba,
269 ULONG* media_status)
270{
271 (void)storage;
272 (void)lun;
274 if ((lba + number_blocks) > (ULONG)k_fat_total_sectors) {
275 *media_status = UX_DEVICE_CLASS_STORAGE_SENSE_STATUS(k_scsi_sense_illegal_request,
278 return UX_ERROR;
279 }
280 for (ULONG i = 0UL; i < number_blocks; i++) {
281 selftest_fat_fill_sector((uint32_t)(lba + i), &data_pointer[i * (ULONG)k_selftest_block_size]);
282 }
283 *media_status = 0UL;
285 return UX_SUCCESS;
286}
287
289 ULONG lun,
290 UCHAR* data_pointer,
291 ULONG number_blocks,
292 ULONG lba,
293 ULONG* media_status)
294{
295 (void)storage;
296 (void)lun;
297 (void)data_pointer;
298 (void)number_blocks;
299 (void)lba;
300 *media_status = UX_DEVICE_CLASS_STORAGE_SENSE_STATUS(k_scsi_sense_data_protect,
303 return UX_ERROR;
304}
305
306UINT selftest_msc_status(VOID* storage, ULONG lun, ULONG media_id, ULONG* media_status)
307{
308 (void)storage;
309 (void)lun;
310 (void)media_id;
311 *media_status = 0UL;
312 return UX_SUCCESS;
313}
314
315#endif /* !RA8_OFF_TARGET */
@ 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 volatile uint32_t s_dbg_read_calls
Device-side media_read invocations.
Definition main.c:213
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.
@ k_ra8_board_led1
LED1, BLUE, P600 (jumper E27).
void * memset(void *dst, int value, size_t n)
Fill memory with a constant byte value.
void * memcpy(void *dst, const void *src, size_t n)
Copy memory area between non-overlapping regions.
@ k_dir_off_attr
MS FAT spec sec 6 "DIR_Attr".
@ k_byte_mask
Byte mask.
@ k_word_mask
Word mask.
unsigned int UINT
ThreadX-compatible unsigned int (host stub).
unsigned long ULONG
ThreadX-compatible unsigned long (host stub).
static const UCHAR s_fat_oem_name[8]
Boot-sector OEM name (8 bytes, space padded).
static const UCHAR s_fat_fs_type[8]
Filesystem-type tag, 8 bytes space padded.
static const UCHAR s_fat_volume_label[11]
Volume label, 11 bytes space padded (also the root entry).
@ k_boot_sig_hi_off
Signature high-byte offset.
@ k_boot_jmp0
Short JMP opcode.
@ k_boot_num_heads
Geometry filler.
@ k_boot_sig_lo_off
Signature low-byte offset.
@ k_boot_sig_hi
Boot signature high byte.
@ k_boot_jmp1
JMP displacement.
@ k_boot_sig_lo
Boot signature low byte.
@ k_boot_media
Fixed-disk media byte.
@ k_boot_sec_per_trk
Geometry filler.
@ k_boot_ext_sig
Extended boot signature.
@ k_boot_drive_num
BIOS drive number.
@ k_boot_jmp2
NOP.
@ k_boot_volume_id
Arbitrary volume serial.
static const UCHAR s_fat_file_name[11]
8.3 directory name of the exposed file: "MRAM.BIN".
@ k_word_shift
Bits per half-word.
@ k_fat_root_entries
Root directory entries.
@ k_fat_eoc
End-of-chain marker.
@ k_fat_data_lba
First data sector (cluster 2).
@ k_fat_root_lba
First root-directory sector.
@ k_fat_fat_sectors
FAT16 size for 4098 entries.
@ k_fat_fat_lba
First FAT sector.
@ k_fat_first_cluster
FAT data area starts at cluster 2.
@ k_fat_entry0
FAT[0]: media F8 + filler.
@ k_fat_reserved_sectors
Boot sector only.
@ k_fat_entries_per_sec
FAT16 entries per 512-byte sector.
@ k_fat_total_sectors
1 + 17 + 32 + 4096.
@ k_fat_last_mram_clus
Last cluster of MRAM.BIN.
@ k_fat_num_fats
Single FAT copy.
@ k_dir_entry_bytes
Directory entry size.
@ k_bpb_off_spt
Sectors per track.
@ k_bpb_off_label
Volume label (11 bytes).
@ k_dir_attr_volume
Volume-label attribute.
@ k_bpb_off_media
Media descriptor.
@ k_bpb_off_bps
Bytes per sector.
@ k_dir_off_size
File size (32-bit LE).
@ k_bpb_off_jmp
Jump instruction.
@ k_bpb_off_heads
Head count.
@ k_bpb_off_oem
OEM name (8 bytes).
@ k_bpb_off_volid
Volume serial (4 bytes).
@ k_bpb_off_bootsig
Extended boot signature.
@ k_bpb_off_fatsz16
Sectors per FAT.
@ k_bpb_off_rsvd
Reserved sector count.
@ k_bpb_off_drvnum
Drive number.
@ k_bpb_off_fstype
Filesystem type (8 bytes).
@ k_bpb_off_rootent
Root entry count.
@ k_dir_name_bytes
8.3 name field length.
@ k_byte_shift
Bits per byte for LE packing.
@ k_dir_off_cluster_lo
First cluster (low word).
@ k_bpb_off_nfats
Number of FATs.
@ k_bpb_off_spc
Sectors per cluster.
@ k_bpb_off_totsec16
Total sectors (16-bit).
@ k_dir_attr_read_only
Read-only attribute.
@ k_mram_bytes
1 MiB window size.
@ k_mram_base_addr
MRAM code window base address.
@ k_selftest_block_size
SCSI logical block size (bytes).
UINT selftest_msc_status(VOID *storage, ULONG lun, ULONG media_id, ULONG *media_status)
Storage media-status callback.
static void selftest_put16(UCHAR *dst, uint16_t value)
Write a 16-bit value little-endian into a byte buffer.
static void selftest_fat_fill_root(uint32_t root_sector, UCHAR *out)
Synthesize one root-directory sector.
static void selftest_fat_fill_sector(uint32_t lba, UCHAR *out)
Synthesize one 512-byte sector of the read-only volume.
UINT selftest_msc_read(VOID *storage, ULONG lun, UCHAR *data_pointer, ULONG number_blocks, ULONG lba, ULONG *media_status)
Storage media-read callback: synthesize sectors over MRAM.
static void selftest_fat_fill_boot(UCHAR *out)
Synthesize the FAT16 boot sector (MS FAT spec 1.03 sec 3.1).
static void selftest_fat_fill_fat(uint32_t fat_sector, UCHAR *out)
Synthesize one FAT16 sector of the cluster chain.
UINT selftest_msc_write(VOID *storage, ULONG lun, UCHAR *data_pointer, ULONG number_blocks, ULONG lba, ULONG *media_status)
Storage media-write callback: always rejects (write-protected).
static void selftest_put32(UCHAR *dst, uint32_t value)
Write a 32-bit value little-endian into a byte buffer.
Shared constants + step prototypes for the USB self-loop config A app.