![]() |
TMF8829 Universal Driver 0.1.0
|
This page explains how measurement results get from the TMF8829 sensor to your application, why the driver uses callbacks for data delivery, and how to assemble a complete frame when the payload is larger than the driver buffer.
Once a measurement is complete, the TMF8829 writes the result frame into an internal FIFO. Your application retrieves that frame by calling tmf8829_read_results (or tmf8829_read_histogram for histogram data). During this call the driver reads the FIFO in chunks, clock-corrects distance data, and delivers each chunk to your application through one of the registered callback functions set on the tmf8829_driver_t struct:
| Field | Callback type | When invoked |
|---|---|---|
| on_stream_header | tmf8829_on_stream_header_fn | Once per tmf8829_read_results / tmf8829_read_histogram call, before any payload. Carries the fused pre-header (5 bytes) + frame header (16 bytes) = 21 bytes total. |
| on_result | tmf8829_on_result_fn | One or more times per tmf8829_read_results call, once for each FIFO chunk. The final chunk ends with the 12-byte frame footer (including the 2-byte EOF marker). |
| on_histogram | tmf8829_on_histogram_fn | One or more times per tmf8829_read_histogram call, once for each FIFO chunk. The final chunk also ends with the 12-byte frame footer. |
All callbacks are optional. Set any field to NULL to ignore that data path.
The TMF8829 stores result frames in a sensor-side FIFO (TMF8829_FIFO_SIZE = 8192 bytes). A single measurement frame — especially at high resolutions such as 48×32 — can be several kilobytes in size. Allocating a contiguous host buffer large enough for the worst-case frame would waste RAM on constrained microcontrollers.
Instead the driver re-uses the small, caller-supplied drv->buffer as a staging area and calls the registered callback once for each chunk read from the FIFO. This keeps the required RAM at TMF8829_MIN_BUFFER_SIZE (256 bytes minimum) regardless of the measurement resolution. A larger buffer reduces the number of bus transactions and callback invocations, but is never mandatory.
This is the most important thing to understand about the data path.
The total payload of one result frame equals the number of measurement zones multiplied by the bytes per zone (which depends on the active result format). For large configurations (e.g. 48×32 at full format) this can far exceed drv->buffer_len. The driver therefore loops, reading drv->buffer_len bytes (or fewer for the final chunk) and invoking on_result each iteration:
Your on_result implementation must therefore accumulate all chunks to reconstruct the full frame. The callback receives a pointer to drv->buffer (overwritten each iteration) and the chunk length. Copy the data out before the function returns.
A buffer of at least the full payload size reduces on_result to a single call per measurement.
The steps below show the minimum code required to have on_result fire. Error handling is omitted for clarity.
If you also want the 21-byte stream header, implement on_stream_header and register it on the driver instance. The callback fires once per tmf8829_read_results call, before any on_result invocations, so you can use it to initialise your accumulation buffer:
Poll the interrupt status register and call tmf8829_read_results when the result-ready bit is set:
Reset the accumulator (my_frame_len = 0) before calling tmf8829_read_results, not inside on_result, so the first chunk is not accidentally appended to a previous incomplete frame.
The following table shows the complete on-wire frame structure and which callback delivers each section:
| Section | Size (bytes) | Callback | Notes |
|---|---|---|---|
| Pre-header | 5 (TMF8829_PRE_HEADER_SIZE) | on_stream_header | Device tick, FIFO status |
| Frame header | 16 (TMF8829_FRAME_HEADER_SIZE) | on_stream_header | Frame id, payload length, focal-plane layout |
| Measurement data | pixel_size × num_zones | on_result (one or more chunks) | Distance + optional signal/noise per zone |
| Frame footer | 12 (TMF8829_FRAME_FOOTER_SIZE) | on_result (appended to final chunk) | Calibration metadata; last 2 bytes are the EOF marker 0xE0F7 |
The on_stream_header buffer is therefore always exactly 21 bytes. The total on_result data across all chunks is pixel_size × num_zones + 12 bytes.