ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
ra8_io_stream_blockdev.c
Go to the documentation of this file.
1
17
19
20#include <stddef.h>
21#include <stdint.h>
22#include <string.h>
23
24#include "ra8_attributes.h"
25#include "ra8_check.h"
26#include "ra8_err.h"
27#include "ra8_io_blockdev.h"
29
31static const char* const s_tag = "ra8_io_stream_blockdev";
32
39typedef enum : uint32_t {
43
69{
70 RA8_CHECK_NULL_PTR(st, s_tag, "st must not be nullptr");
71 RA8_CHECK_NULL_PTR(st->bd, s_tag, "st->bd must not be nullptr");
72 const ra8_err_t e = ra8_io_blockdev_write(st->bd, st->lba, (uint32_t)k_bd_one_block, st->sector);
73 if (e != k_ra8_ok) {
74 return e;
75 }
76 st->lba += (uint32_t)k_bd_one_block;
77 st->fill = 0;
78 return k_ra8_ok;
79}
80
111 const uint8_t* buf,
112 uint32_t len,
113 uint32_t* done)
114{
115 const uint32_t room = (uint32_t)k_ra8_io_block_size_bytes - st->fill;
116 const uint32_t rem = len - *done;
117 const uint32_t chunk = (rem < room) ? rem : room;
118 (void)memcpy(&st->sector[st->fill], &buf[*done], (size_t)chunk);
119 st->fill += chunk;
120 *done += chunk;
121 return chunk;
122}
123
155
184internal_bdsink_write(void* ctx, const uint8_t* buf, uint32_t len, uint32_t* out_written)
185{
186 RA8_CHECK_NULL_PTR(ctx, s_tag, "ctx must not be nullptr");
187 RA8_CHECK_NULL_PTR(buf, s_tag, "buf must not be nullptr");
189 uint32_t done = 0;
190 while (done < len) {
191 (void)internal_bdsink_fill_chunk(st, buf, len, &done);
193 if (e != k_ra8_ok) {
194 if (out_written != nullptr) {
195 *out_written = done;
196 }
197 return e;
198 }
199 }
200 if (out_written != nullptr) {
201 *out_written = len;
202 }
203 return k_ra8_ok;
204}
205
230{
231 RA8_CHECK_NULL_PTR(ctx, s_tag, "ctx must not be nullptr");
233 if (st->fill == 0U) {
234 return k_ra8_ok;
235 }
236 const size_t pad = (size_t)((uint32_t)k_ra8_io_block_size_bytes - st->fill);
237 (void)memset(&st->sector[st->fill], (int)k_bd_pad_byte, pad);
239}
240
243 .write = internal_bdsink_write,
244 .flush = internal_bdsink_flush,
245};
246
249 const ra8_io_blockdev_t* bd,
250 uint32_t start_lba)
251{
252 RA8_CHECK_NULL_PTR(s, s_tag, "s must not be nullptr");
253 RA8_CHECK_NULL_PTR(state, s_tag, "state must not be nullptr");
254 RA8_CHECK_NULL_PTR(bd, s_tag, "bd must not be nullptr");
255 state->bd = bd;
256 state->lba = start_lba;
257 state->fill = 0;
258 return ra8_io_stream_bind(s, &s_bd_iface, state);
259}
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_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
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.
@ k_ra8_io_block_size_bytes
Bytes per logical block (the only size).
ra8_err_t ra8_io_blockdev_write(const ra8_io_blockdev_t *bd, uint32_t lba, uint32_t count, const uint8_t *buf)
Write count logical blocks from buf starting at lba.
struct ra8_io_stream_iface ra8_io_stream_iface_t
Public implementer contract for ra8_io byte-stream backends.
ra8_err_t ra8_io_stream_bind(ra8_io_stream_t *stream, const ra8_io_stream_iface_t *iface, void *context)
Bind a backend vtable and caller-owned state into a stream handle.
static ra8_err_t internal_bdsink_write(void *ctx, const uint8_t *buf, uint32_t len, uint32_t *out_written)
Block-device sink: buffer bytes, auto-committing full sectors.
static ra8_err_t internal_bdsink_commit_sector(ra8_io_stream_blockdev_state_t *st)
Commit the buffered sector as one logical block and advance the LBA.
static ra8_err_t internal_bdsink_commit_if_full(ra8_io_stream_blockdev_state_t *st)
Commit the buffered sector iff it is exactly full.
static const ra8_io_stream_iface_t s_bd_iface
Block-device stream sink vtable.
ra8_err_t ra8_io_stream_blockdev_init(ra8_io_stream_t *s, ra8_io_stream_blockdev_state_t *state, const ra8_io_blockdev_t *bd, uint32_t start_lba)
Bind a block-device stream sink into a caller-owned stream handle.
static uint32_t internal_bdsink_fill_chunk(ra8_io_stream_blockdev_state_t *st, const uint8_t *buf, uint32_t len, uint32_t *done)
Copy one bounded chunk from the source into the sector buffer.
static ra8_err_t internal_bdsink_flush(void *ctx)
Block-device sink: commit a partial trailing sector (zero-padded).
ra8_io_stream_bd_const_t
Block-device sink constants.
@ k_bd_one_block
Single-block transfer count.
@ k_bd_pad_byte
Pad value for a partial trailing sector.
ra8_io byte-stream sink that buffers into a raw block device.
Caller-allocated block-device handle binding a backend to its context.
Caller-owned private state for a block-device stream sink.
const ra8_io_blockdev_t * bd
Target device (private).
uint8_t sector[(uint32_t) k_ra8_io_block_size_bytes]
Sector buffer.
uint32_t fill
Bytes in the sector buf.
Caller-allocated byte-stream handle binding a sink to its context.