ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
ra8_io_blockdev_ram.c
Go to the documentation of this file.
1
16
17#include "ra8_io_blockdev_ram.h"
18
19#include <stddef.h>
20#include <stdint.h>
21#include <string.h>
22
23#include "ra8_attributes.h"
24#include "ra8_check.h"
25#include "ra8_err.h"
26#include "ra8_io_blockdev.h"
28
30static const char* const s_tag = "ra8_io_blockdev_ram";
31
42
69internal_ram_bounds(const ra8_io_blockdev_ram_state_t* st, uint32_t lba, uint32_t count)
70{
71 if (count > st->block_count) {
73 }
74 if (lba > st->block_count - count) {
76 }
77 return k_ra8_ok;
78}
79
108internal_ram_read(void* ctx, uint32_t lba, uint32_t count, uint8_t* buf)
109{
110 RA8_CHECK_NULL_PTR(ctx, s_tag, "ctx must not be nullptr");
111 RA8_CHECK_NULL_PTR(buf, s_tag, "buf must not be nullptr");
113 const ra8_err_t b = internal_ram_bounds(st, lba, count);
114 if (b != k_ra8_ok) {
115 return b;
116 }
117 const size_t off = (size_t)lba * (size_t)k_ra8_io_block_size_bytes;
118 const size_t n = (size_t)count * (size_t)k_ra8_io_block_size_bytes;
119 (void)memcpy(buf, &st->storage[off], n);
120 return k_ra8_ok;
121}
122
152internal_ram_write(void* ctx, uint32_t lba, uint32_t count, const uint8_t* buf)
153{
154 RA8_CHECK_NULL_PTR(ctx, s_tag, "ctx must not be nullptr");
155 RA8_CHECK_NULL_PTR(buf, s_tag, "buf must not be nullptr");
157 if (st->read_only) {
159 }
160 const ra8_err_t b = internal_ram_bounds(st, lba, count);
161 if (b != k_ra8_ok) {
162 return b;
163 }
164 const size_t off = (size_t)lba * (size_t)k_ra8_io_block_size_bytes;
165 const size_t n = (size_t)count * (size_t)k_ra8_io_block_size_bytes;
166 (void)memcpy(&st->storage[off], buf, n);
167 return k_ra8_ok;
168}
169
197RA8_INTERNAL static ra8_err_t internal_ram_erase(void* ctx, uint32_t lba, uint32_t count)
198{
199 RA8_CHECK_NULL_PTR(ctx, s_tag, "ctx must not be nullptr");
201 if (st->read_only) {
203 }
204 const ra8_err_t b = internal_ram_bounds(st, lba, count);
205 if (b != k_ra8_ok) {
206 return b;
207 }
208 const size_t off = (size_t)lba * (size_t)k_ra8_io_block_size_bytes;
209 const size_t n = (size_t)count * (size_t)k_ra8_io_block_size_bytes;
210 (void)memset(&st->storage[off], (int)k_ra8_io_erase_value_zero, n);
211 return k_ra8_ok;
212}
213
239{
240 RA8_CHECK_NULL_PTR(ctx, s_tag, "ctx must not be nullptr");
241 RA8_CHECK_NULL_PTR(out, s_tag, "out must not be nullptr");
243 out->block_count = st->block_count;
248 out->must_erase_before_write = false;
249 out->read_only = st->read_only;
250 return k_ra8_ok;
251}
252
255 .read = internal_ram_read,
256 .write = internal_ram_write,
257 .erase = internal_ram_erase,
258 .get_caps = internal_ram_get_caps,
259 .sync = nullptr,
260};
261
264 uint8_t* storage,
265 uint32_t block_count,
266 bool read_only)
267{
268 RA8_CHECK_NULL_PTR(bd, s_tag, "bd must not be nullptr");
269 RA8_CHECK_NULL_PTR(state, s_tag, "state must not be nullptr");
270 RA8_CHECK_NULL_PTR(storage, s_tag, "storage must not be nullptr");
271 if (block_count < (uint32_t)k_ra8_io_ram_min_blocks) {
273 }
274 state->storage = storage;
275 state->block_count = block_count;
276 state->read_only = read_only;
277 bd->iface = &s_ram_iface;
278 bd->ctx = state;
279 return k_ra8_ok;
280}
static const char * s_tag
Logging / check tag.
Definition ra8_app.c:17
Annotation-attribute framework macros for ra8-firmware.
#define RA8_INTERNAL
Marker that a function is intended to be static (file-local).
Validation and Error-Checking Macros for ra8-firmware.
#define RA8_CHECK_NULL_PTR(ptr, tag, message)
Reject nullptr pointer, returning k_ra8_err_null_ptr.
Definition ra8_check.h:243
Error Code Definitions for ra8-firmware.
@ k_ra8_err_not_supported
Requested feature not compiled in, not wired, or not supported by this MCU variant.
Definition ra8_err.h:180
@ k_ra8_err_out_of_range
Sensor or peripheral output out of valid range.
Definition ra8_err.h:337
@ k_ra8_ok
Success – operation completed with all postconditions satisfied.
Definition ra8_err.h:119
@ k_ra8_err_invalid_size
Invalid size parameter (too large, too small, or misaligned).
Definition ra8_err.h:167
ra8_err_codes_t ra8_err_t
Canonical error-return type used by every ra8-firmware API.
Definition ra8_err.h:546
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.
ra8_io block-device fabric – one LBA vtable across every storage medium.
struct ra8_io_blockdev_iface ra8_io_blockdev_iface_t
@ k_ra8_io_erase_value_zero
Block reads back as 0x00 after erase.
@ k_ra8_io_block_size_bytes
Bytes per logical block (the only size).
Backend implementation contract for the ra8_io block-device fabric.
ra8_err_t ra8_io_blockdev_ram_init(ra8_io_blockdev_t *bd, ra8_io_blockdev_ram_state_t *state, uint8_t *storage, uint32_t block_count, bool read_only)
Bind a RAM block-device backend into a caller-owned handle.
static ra8_err_t internal_ram_bounds(const ra8_io_blockdev_ram_state_t *st, uint32_t lba, uint32_t count)
Reject an out-of-range [lba, lba+count) block range.
static ra8_err_t internal_ram_get_caps(const void *ctx, ra8_io_blockdev_caps_t *out)
RAM backend: report medium capabilities.
static ra8_err_t internal_ram_read(void *ctx, uint32_t lba, uint32_t count, uint8_t *buf)
RAM backend: read count blocks at lba into buf.
static ra8_err_t internal_ram_erase(void *ctx, uint32_t lba, uint32_t count)
RAM backend: erase (zero-fill) count blocks at lba.
ra8_io_ram_const_t
RAM-backend layout constants.
@ k_ra8_io_ram_erase_unit_blocks
One logical block per erase unit.
@ k_ra8_io_ram_min_blocks
Smallest legal device size.
static ra8_err_t internal_ram_write(void *ctx, uint32_t lba, uint32_t count, const uint8_t *buf)
RAM backend: write count blocks from buf at lba.
static const ra8_io_blockdev_iface_t s_ram_iface
RAM backend vtable.
ra8_io block-device backend over a caller-owned RAM buffer.
static ra8_err_t internal_ram_write(void *context, const uint8_t *data, uint16_t length, uint16_t *written)
Append one complete transfer fragment to private RAM storage.
Static properties a backend reports about its medium.
uint32_t erase_unit_blocks
Erase granularity, in logical blocks.
uint32_t block_count
Total addressable logical blocks.
bool must_erase_before_write
true => program needs a prior erase.
bool read_only
true => writes/erases are rejected.
uint8_t erase_value
Post-erase byte (ra8_io_erase_value_t).
uint16_t logical_block_bytes
Logical block size (always 512).
uint32_t program_size_bytes
Minimum program granularity in bytes.
Caller-owned private state for a RAM block device.
uint8_t * storage
Caller-owned backing buffer (private).
bool read_only
true => writes and erases are rejected (private).
uint32_t block_count
Number of 512-byte logical blocks (private).
Caller-allocated block-device handle binding a backend to its context.
const ra8_io_blockdev_iface_t * iface
Bound backend vtable (private).
void * ctx
Backend-private context (private).