ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
ra8_io_i2c_bus.c
Go to the documentation of this file.
1
19
20#include "ra8_io_i2c_bus.h"
21
22#include <stdint.h>
23
24#include "ra8_attributes.h"
25#include "ra8_check.h"
26#include "ra8_err.h"
27#include "ra8_i2c_bus_ops.h"
29
31static const char* const s_tag = "ra8_io_i2c_bus";
32
33/* =============================================================================
34 * Internal helpers
35 * =============================================================================
36 */
37
63{
64 if (bus == nullptr) {
65 return k_ra8_err_null_ptr;
66 }
67 if (bus->iface == nullptr) {
69 }
70 return k_ra8_ok;
71}
72
73/* =============================================================================
74 * Public API -- dispatch through the bound backend
75 * =============================================================================
76 */
77
79 uint8_t addr,
80 const uint8_t* data,
81 uint32_t len,
82 bool send_stop)
83{
84 const ra8_err_t v = internal_validate(bus);
85 if (v != k_ra8_ok) {
86 return v;
87 }
88 RA8_CHECK_NULL_PTR(bus->iface->write, s_tag, "backend write op missing");
89 return bus->iface->write(bus->ctx, addr, data, len, send_stop);
90}
91
93ra8_io_i2c_bus_read(const ra8_io_i2c_bus_t* bus, uint8_t addr, uint8_t* data, uint32_t len)
94{
95 const ra8_err_t v = internal_validate(bus);
96 if (v != k_ra8_ok) {
97 return v;
98 }
99 RA8_CHECK_NULL_PTR(bus->iface->read, s_tag, "backend read op missing");
100 return bus->iface->read(bus->ctx, addr, data, len);
101}
102
104 uint8_t addr,
105 const uint8_t* wr,
106 uint32_t wr_len,
107 uint8_t* rd,
108 uint32_t rd_len)
109{
110 const ra8_err_t v = internal_validate(bus);
111 if (v != k_ra8_ok) {
112 return v;
113 }
114 RA8_CHECK_NULL_PTR(bus->iface->transfer, s_tag, "backend transfer op missing");
115 return bus->iface->transfer(bus->ctx, addr, wr, wr_len, rd, rd_len);
116}
117
118/* =============================================================================
119 * ra8_i2c_bus_ops_t bridge (Ring-3 seam)
120 * =============================================================================
121 */
122
151static ra8_err_t
152internal_ops_write(void* ctx, uint8_t addr, const uint8_t* data, uint32_t len, bool send_stop)
153{
154 RA8_CHECK_NULL_PTR(ctx, s_tag, "ctx (i2c bus) must not be nullptr");
155 return ra8_io_i2c_bus_write((const ra8_io_i2c_bus_t*)ctx, addr, data, len, send_stop);
156}
157
185static ra8_err_t internal_ops_read(void* ctx, uint8_t addr, uint8_t* data, uint32_t len)
186{
187 RA8_CHECK_NULL_PTR(ctx, s_tag, "ctx (i2c bus) must not be nullptr");
188 return ra8_io_i2c_bus_read((const ra8_io_i2c_bus_t*)ctx, addr, data, len);
189}
190
221 uint8_t addr,
222 const uint8_t* wr,
223 uint32_t wr_len,
224 uint8_t* rd,
225 uint32_t rd_len)
226{
227 RA8_CHECK_NULL_PTR(ctx, s_tag, "ctx (i2c bus) must not be nullptr");
228 return ra8_io_i2c_bus_transfer((const ra8_io_i2c_bus_t*)ctx, addr, wr, wr_len, rd, rd_len);
229}
230
232{
233 const ra8_err_t v = internal_validate(bus);
234 if (v != k_ra8_ok) {
235 return v;
236 }
237 RA8_CHECK_NULL_PTR(out, s_tag, "out must not be nullptr");
239 out->read = internal_ops_read;
241 out->ctx = (void*)(uintptr_t)bus;
242 return k_ra8_ok;
243}
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_initialized
Module not initialized – _init() not yet called successfully.
Definition ra8_err.h:235
@ k_ra8_ok
Success – operation completed with all postconditions satisfied.
Definition ra8_err.h:119
@ k_ra8_err_null_ptr
Pointer was NULL where a valid pointer was required.
Definition ra8_err.h:478
ra8_err_codes_t ra8_err_t
Canonical error-return type used by every ra8-firmware API.
Definition ra8_err.h:546
Injected I2C-bus seam consumed by Ring-3 I2C device drivers.
static ra8_err_t internal_ops_transfer(void *ctx, uint8_t addr, const uint8_t *wr, uint32_t wr_len, uint8_t *rd, uint32_t rd_len)
Seam trampoline – forward a combined transfer into the bound bus.
ra8_err_t ra8_io_i2c_bus_as_ops(const ra8_io_i2c_bus_t *bus, ra8_i2c_bus_ops_t *out)
Expose a bound bus through the Ring-3 seam ra8_i2c_bus_ops_t.
static ra8_err_t internal_ops_write(void *ctx, uint8_t addr, const uint8_t *data, uint32_t len, bool send_stop)
Seam trampoline – forward a controller write into the bound bus.
static ra8_err_t internal_ops_read(void *ctx, uint8_t addr, uint8_t *data, uint32_t len)
Seam trampoline – forward a controller read into the bound bus.
ra8_err_t ra8_io_i2c_bus_write(const ra8_io_i2c_bus_t *bus, uint8_t addr, const uint8_t *data, uint32_t len, bool send_stop)
Controller write of len bytes to 7-bit address addr.
ra8_err_t ra8_io_i2c_bus_read(const ra8_io_i2c_bus_t *bus, uint8_t addr, uint8_t *data, uint32_t len)
Controller read of len bytes from 7-bit address addr.
ra8_err_t ra8_io_i2c_bus_transfer(const ra8_io_i2c_bus_t *bus, uint8_t addr, const uint8_t *wr, uint32_t wr_len, uint8_t *rd, uint32_t rd_len)
Combined write-then-RESTART-then-read in one bus transaction.
static ra8_err_t internal_validate(const ra8_io_i2c_bus_t *bus)
Reject a handle that is NULL or has no backend bound.
ra8_io I2C-bus facade – one controller-transfer vtable over thechip's two I2C implementations.
Internal vtable type for the ra8_io I2C-bus facade.
Caller-filled I2C controller-transfer seam (write / read / write-then-read).
ra8_err_t(* read)(void *ctx, uint8_t addr, uint8_t *data, uint32_t len)
Controller read of len bytes from 7-bit address addr.
void * ctx
Opaque cookie handed to every callback (binder-owned).
ra8_err_t(* transfer)(void *ctx, uint8_t addr, const uint8_t *wr, uint32_t wr_len, uint8_t *rd, uint32_t rd_len)
Combined write-then-RESTART-then-read in one transaction.
ra8_err_t(* write)(void *ctx, uint8_t addr, const uint8_t *data, uint32_t len, bool send_stop)
Controller write of len bytes to 7-bit address addr.
ra8_err_t(* read)(void *ctx, uint8_t addr, uint8_t *data, uint32_t len)
Controller read; always ends with STOP.
ra8_err_t(* transfer)(void *ctx, uint8_t addr, const uint8_t *wr, uint32_t wr_len, uint8_t *rd, uint32_t rd_len)
Write-then-RESTART-then-read combined transaction.
ra8_err_t(* write)(void *ctx, uint8_t addr, const uint8_t *data, uint32_t len, bool send_stop)
Controller write; send_stop false holds the bus.
Caller-allocated I2C-bus handle binding a backend to its context.
const ra8_io_i2c_bus_iface_t * iface
Bound backend vtable (private).
void * ctx
Backend-private context (private).