Skip to content

Commit 67a5c19

Browse files
docs(api): refresh API reference (signatures, return semantics, examples)
1 parent ec3d6cb commit 67a5c19

1 file changed

Lines changed: 96 additions & 18 deletions

File tree

docs/api-reference.md

Lines changed: 96 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
# API Reference
22

3-
This section documents the public C API exposed by the CRUMBS core and the platform HALs (Arduino and Linux).
3+
This file documents the public CRUMBS C API (core + provided HAL adapters) so you can use CRUMBS as a compiled library without reading the implementation.
44

5-
The core is intentionally C-friendly and small so it is easy to reuse in both Arduino and native Linux code.
5+
The core is intentionally small and C-friendly so it is easy to consume from Arduino sketches, PlatformIO projects or native Linux programs.
66

77
## Key types and constants
88

9-
- `crumbs_message_t` — fixed-size message struct (see `src/crumbs_message.h`). Fields include `slice_address`, `type_id`, `command_type`, `data[7]`, `crc8`.
10-
- `crumbs_context_t` — per-endpoint context used by the core (role, address, CRC stats, callbacks).
11-
- `CRUMBS_DATA_LENGTH` — number of float payload elements (7).
12-
- `CRUMBS_MESSAGE_SIZE` — serialized frame size in bytes (31).
9+
- `crumbs_message_t` — fixed-size message struct (see `src/crumbs_message.h`). Fields:
1310

14-
**Serialized frame**: 31 bytes (type_id + command_type + data + crc8)
11+
- `slice_address` — (logical) slice address (NOT serialized)
12+
- `type_id` — 1 byte
13+
- `command_type` — 1 byte
14+
- `data[7]` — 7 float32 payload fields (little-endian)
15+
- `crc8` — 1 byte CRC-8 computed over the serialized payload
1516

16-
## Core API (selected)
17+
- `crumbs_context_t` — per-endpoint context (role, address, CRC statistics, callbacks, user_data)
18+
- `CRUMBS_DATA_LENGTH` — number of payload floats (7)
19+
- `CRUMBS_MESSAGE_SIZE` — serialized frame size (31 bytes)
20+
21+
Serialized frame layout (31 bytes): - type_id (1) - command_type (1) - data (7 \* 4 = 28) - crc8 (1)
22+
23+
## Core API (full, concise)
1724

1825
```c
1926
/* Initialize a context */
@@ -44,19 +51,59 @@ int crumbs_peripheral_build_reply(crumbs_context_t *ctx, uint8_t *out_buf, size_
4451
uint32_t crumbs_get_crc_error_count(const crumbs_context_t *ctx);
4552
int crumbs_last_crc_ok(const crumbs_context_t *ctx);
4653
void crumbs_reset_crc_stats(crumbs_context_t *ctx);
54+
55+
---
56+
57+
### Return values and common semantics
58+
59+
- Encoding/decoding
60+
- `crumbs_encode_message()` returns CRUMBS_MESSAGE_SIZE on success, 0 on failure (e.g., buffer too small).
61+
- `crumbs_decode_message()` returns `0` on success, `-1` if buffer too small, and `-2` if the CRC check fails. When a non-NULL context is passed the function will update CRC statistics accessible via `crumbs_get_crc_error_count()` and `crumbs_last_crc_ok()`.
62+
63+
- Controller helpers
64+
- `crumbs_controller_send()` returns `0` on success; non-zero indicates an I2C write error as returned by the platform `write_fn`.
65+
66+
- Peripheral helpers
67+
- `crumbs_peripheral_handle_receive()` returns `0` on success, negative on decode/CRC errors. When successful it will invoke `ctx->on_message` if configured.
68+
- `crumbs_peripheral_build_reply()` returns `0` on success. If no reply is available the function returns 0 and sets `out_len` to 0.
69+
70+
All HAL adapters follow the `crumbs_i2c_*` function signatures in `src/crumbs_i2c.h` — write functions should return 0 on success and non-zero on error; read functions should return number-of-bytes-read (>=0) or negative on error.
4771
```
4872
49-
## Arduino HAL (high level)
73+
## Arduino HAL (high-level conveniences)
5074
51-
```c
75+
````c
5276
/* Initialize controller/peripheral using default Wire instance */
5377
void crumbs_arduino_init_controller(crumbs_context_t *ctx);
5478
void crumbs_arduino_init_peripheral(crumbs_context_t *ctx, uint8_t address);
5579
5680
/* Wire-based write function for crumbs_controller_send */
5781
int crumbs_arduino_wire_write(void *user_ctx, uint8_t addr, const uint8_t *data, size_t len);
82+
83+
Other Arduino helpers:
84+
- `crumbs_arduino_scan()` — TwoWire-compatible bus scanner (strict/non-strict), returns number found.
85+
- `crumbs_arduino_read()` — TwoWire-compatible read helper for `crumbs_i2c_read_fn`.
86+
87+
Usage (controller, Arduino):
88+
89+
```c
90+
crumbs_context_t ctx;
91+
crumbs_arduino_init_controller(&ctx);
92+
// build message
93+
crumbs_message_t msg = {0}; msg.type_id=1; msg.command_type=1; msg.data[0]=1.23f;
94+
int rc = crumbs_controller_send(&ctx, 0x08, &msg, crumbs_arduino_wire_write, NULL);
95+
````
96+
97+
Usage (peripheral, Arduino):
98+
99+
```c
100+
crumbs_context_t ctx;
101+
crumbs_arduino_init_peripheral(&ctx, 0x08);
102+
crumbs_set_callbacks(&ctx, on_message_cb, on_request_cb, NULL);
58103
```
59104

105+
````
106+
60107
## Linux HAL (selected)
61108
62109
```c
@@ -67,19 +114,37 @@ int crumbs_linux_init_controller(crumbs_context_t *ctx,
67114
void crumbs_linux_close(crumbs_linux_i2c_t *i2c);
68115
int crumbs_linux_i2c_write(void *user_ctx, uint8_t target_addr, const uint8_t *data, size_t len);
69116
int crumbs_linux_read_message(crumbs_linux_i2c_t *i2c, uint8_t target_addr, crumbs_context_t *ctx, crumbs_message_t *out_msg);
70-
```
117+
118+
Return codes and notes (Linux HAL)
119+
- `crumbs_linux_init_controller()` returns 0 on success, -1 for bad args, -2 if opening the bus failed.
120+
- `crumbs_linux_i2c_write()` and `crumbs_linux_read()` will return negative error codes on failure — see `src/crumbs_linux.h` for the small set of linux-wire-mapped errors.
121+
122+
Usage (controller, Linux):
123+
124+
```c
125+
crumbs_context_t ctx;
126+
crumbs_linux_i2c_t bus;
127+
int rc = crumbs_linux_init_controller(&ctx, &bus, "/dev/i2c-1", 10000);
128+
// send using crumbs_controller_send(&ctx, addr, &msg, crumbs_linux_i2c_write, &bus);
129+
````
130+
131+
````
71132
72133
Note: Most functions return `0` on success and negative or non-zero codes on error — consult the headers for specific return values.
73134
74135
## Scanner & read primitive
75136
76-
The core provides a CRUMBS-aware scanner which attempts to read a full CRUMBS frame from candidate addresses and accepts only CRC-valid frames. This lets controllers reliably discover devices that actually run the CRUMBS protocol (not merely devices that ACK the bus).
137+
The core scanner uses a read primitive (`crumbs_i2c_read_fn`) and an optional write primitive to reliably discover devices that actually speak CRUMBS. It reads up to `CRUMBS_MESSAGE_SIZE` bytes and only accepts addresses that provide a CRC-valid CRUMBS frame.
138+
139+
Signature (read primitive):
77140
78141
```c
79-
/* Read primitive used by the core scanner and HALs */
80142
typedef int (*crumbs_i2c_read_fn)(void *user_ctx, uint8_t addr, uint8_t *buffer, size_t len, uint32_t timeout_us);
143+
````
81144

82-
/* Perform a CRUMBS-aware scan: returns count of discovered CRUMBS devices */
145+
Scanner helper:
146+
147+
```c
83148
int crumbs_controller_scan_for_crumbs(const crumbs_context_t *ctx,
84149
uint8_t start_addr,
85150
uint8_t end_addr,
@@ -90,8 +155,21 @@ int crumbs_controller_scan_for_crumbs(const crumbs_context_t *ctx,
90155
uint8_t *found,
91156
size_t max_found,
92157
uint32_t timeout_us);
93-
94-
/* HAL read helpers (Arduino / Linux) */
95-
int crumbs_arduino_read(void *user_ctx, uint8_t addr, uint8_t *buffer, size_t len, uint32_t timeout_us);
96-
int crumbs_linux_read(void *user_ctx, uint8_t addr, uint8_t *buffer, size_t len, uint32_t timeout_us);
97158
```
159+
160+
Parameters summary:
161+
162+
- `start_addr`/`end_addr`: inclusive probe range (0x03..0x77 typical)
163+
- `strict`: non-zero => request a data-phase read (strong detection). `0` => lightweight ACK probe.
164+
- `write_fn`/`read_fn` : platform primitives (e.g., `crumbs_arduino_wire_write`, `crumbs_arduino_read`, or `crumbs_linux_i2c_write`, `crumbs_linux_read`).
165+
166+
Return value: number of discovered CRUMBS device addresses (>=0) or negative on error.
167+
168+
Platform-provided read helpers:
169+
170+
- `crumbs_arduino_read()` — TwoWire-based read implementation for Arduino.
171+
- `crumbs_linux_read()` — linux-wire based read implementation for native Linux.
172+
173+
---
174+
175+
If you need a quick reference to these types and function signatures for direct inclusion into code, the public headers in `src/` are intentionally stable and self-documenting; use them as authoritative API. The summary above is complete enough to consume the library from a binary/compressed distribution (static archive + headers) without reading implementation files.

0 commit comments

Comments
 (0)