ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
usb_msc_mram_hs_steps.c
Go to the documentation of this file.
1
25
27
28#include <stdint.h>
29#include <string.h>
30
31#ifndef RA8_OFF_TARGET
32
37typedef enum : uint32_t {
38 k_mram_base_addr = 0x02000000U,
39 k_mram_bytes = 0x00100000U,
41
46typedef enum : uint32_t {
47 k_boot_jmp0 = 0xEBU,
48 k_boot_jmp1 = 0x3CU,
49 k_boot_jmp2 = 0x90U,
50 k_boot_media = 0xF8U,
55 k_boot_volume_id = 0x52A8D200U,
56 k_boot_sig_lo = 0x55U,
57 k_boot_sig_hi = 0xAAU,
61
94
99typedef enum : uint32_t {
101 k_word_mask = 0xFFFFU,
103
105static const UCHAR s_fat_oem_name[8] = {'R', 'A', '8', 'D', '2', 'F', 'W', ' '};
106
108static const UCHAR s_fat_volume_label[11] = {'R', 'A', '8', 'D', '2', ' ', 'M', 'R', 'A', 'M', ' '};
109
111static const UCHAR s_fat_fs_type[8] = {'F', 'A', 'T', '1', '6', ' ', ' ', ' '};
112
114static const UCHAR s_fat_file_name[11] = {'M', 'R', 'A', 'M', ' ', ' ', ' ', ' ', 'B', 'I', 'N'};
115
130static void demo_put16(UCHAR* dst, uint16_t value)
131{
132 dst[0] = (UCHAR)(value & (uint16_t)k_byte_mask);
133 dst[1] = (UCHAR)((value >> (uint16_t)k_byte_shift) & (uint16_t)k_byte_mask);
134}
135
150static void demo_put32(UCHAR* dst, uint32_t value)
151{
152 demo_put16(dst, (uint16_t)(value & (uint32_t)k_word_mask));
153 demo_put16(dst + 2U, (uint16_t)(value >> (uint32_t)k_word_shift));
154}
155
169static void demo_fat_fill_boot(UCHAR* out)
170{
171 out[k_bpb_off_jmp] = (UCHAR)k_boot_jmp0;
172 out[k_bpb_off_jmp + 1U] = (UCHAR)k_boot_jmp1;
173 out[k_bpb_off_jmp + 2U] = (UCHAR)k_boot_jmp2;
174 (void)memcpy(&out[k_bpb_off_oem], s_fat_oem_name, sizeof(s_fat_oem_name));
176 out[k_bpb_off_spc] = 1U;
178 out[k_bpb_off_nfats] = (UCHAR)k_fat_num_fats;
181 out[k_bpb_off_media] = (UCHAR)k_boot_media;
186 out[k_bpb_off_bootsig] = (UCHAR)k_boot_ext_sig;
189 (void)memcpy(&out[k_bpb_off_fstype], s_fat_fs_type, sizeof(s_fat_fs_type));
190 out[k_boot_sig_lo_off] = (UCHAR)k_boot_sig_lo;
191 out[k_boot_sig_hi_off] = (UCHAR)k_boot_sig_hi;
192}
193
213static void demo_fat_fill_fat(uint32_t fat_sector, UCHAR* out)
214{
215 const uint32_t first_entry = fat_sector * (uint32_t)k_fat_entries_per_sec;
216 for (uint32_t j = 0U; j < (uint32_t)k_fat_entries_per_sec; j++) {
217 const uint32_t entry = first_entry + j;
218 uint16_t value = 0U;
219 if (entry == 0U) {
220 value = (uint16_t)k_fat_entry0;
221 } else if (entry == 1U) {
222 value = (uint16_t)k_fat_eoc;
223 } else if (entry < (uint32_t)k_fat_last_mram_clus) {
224 value = (uint16_t)(entry + 1U);
225 } else if (entry == (uint32_t)k_fat_last_mram_clus) {
226 value = (uint16_t)k_fat_eoc;
227 } else {
228 value = 0U;
229 }
230 demo_put16(&out[j * 2U], value);
231 }
232}
233
252static void demo_fat_fill_root(uint32_t root_sector, UCHAR* out)
253{
254 if (root_sector != 0U) {
255 return;
256 }
257 /* Entry 0: volume label. */
258 (void)memcpy(&out[0], s_fat_volume_label, (size_t)k_dir_name_bytes);
259 out[k_dir_off_attr] = (UCHAR)k_dir_attr_volume;
260 /* Entry 1: MRAM.BIN, read-only, cluster 2, 1 MiB. */
261 UCHAR* entry = &out[k_dir_entry_bytes];
262 (void)memcpy(entry, s_fat_file_name, (size_t)k_dir_name_bytes);
263 entry[k_dir_off_attr] = (UCHAR)k_dir_attr_read_only;
265 demo_put32(&entry[k_dir_off_size], (uint32_t)k_mram_bytes);
266}
267
268void demo_fat_fill_sector(uint32_t lba, UCHAR* out)
269{
270 (void)memset(out, 0, (size_t)k_demo_block_size);
271 if (lba == 0U) {
273 return;
274 }
275 if (lba < (uint32_t)k_fat_root_lba) {
276 demo_fat_fill_fat(lba - (uint32_t)k_fat_fat_lba, out);
277 return;
278 }
279 if (lba < (uint32_t)k_fat_data_lba) {
280 demo_fat_fill_root(lba - (uint32_t)k_fat_root_lba, out);
281 return;
282 }
283 const uint32_t cluster = (lba - (uint32_t)k_fat_data_lba) + (uint32_t)k_fat_first_cluster;
284 if (cluster <= (uint32_t)k_fat_last_mram_clus) {
285 const uint32_t offset = (cluster - (uint32_t)k_fat_first_cluster) * (uint32_t)k_demo_block_size;
286 const UCHAR* mram = (const UCHAR*)(uintptr_t)((uint32_t)k_mram_base_addr + offset);
287 (void)memcpy(out, mram, (size_t)k_demo_block_size);
288 }
289}
290
291#endif /* !RA8_OFF_TARGET */
@ k_demo_block_size
SCSI logical block size (bytes).
Definition main.c:123
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.
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.
static void demo_fat_fill_root(uint32_t root_sector, UCHAR *out)
Synthesize one root-directory sector.
static void demo_fat_fill_fat(uint32_t fat_sector, UCHAR *out)
Synthesize one FAT16 sector of the cluster chain.
static void demo_put32(UCHAR *dst, uint32_t value)
Write a 32-bit value little-endian into a byte buffer.
void demo_fat_fill_sector(uint32_t lba, UCHAR *out)
Synthesize one 512-byte sector of the read-only volume.
static void demo_put16(UCHAR *dst, uint16_t value)
Write a 16-bit value little-endian into a byte buffer.
static void demo_fat_fill_boot(UCHAR *out)
Synthesize the FAT16 boot sector (MS FAT spec 1.03 sec 3.1).
Synthesized FAT16 volume helpers for the USB-HS MRAM MSC demo.
demo_mram_t
The MRAM window this volume exposes (RA8D2 HUM memory map).
demo_word_pack_t
32-bit little-endian split constants.
demo_fat_boot_t
Boot-sector field values (MS FAT spec 1.03 sec 3.1).
demo_fat_off_t
Byte offsets inside the boot sector and directory entries.