TMF8829 Universal Driver 0.1.0
Loading...
Searching...
No Matches
tmf8829.h
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright (c) 2026 tmf8829_universal_driver contributors
5 * Derived from ams-OSRAM TMF8829 reference drivers (MIT); adapted for portable multi-instance use.
6 */
7
20
21#ifndef TMF8829_H
22#define TMF8829_H
23
24#ifdef __cplusplus
25extern "C" {
26#endif
27
28#include "tmf8829/tmf8829_version.h"
29
30#include "tmf8829_ops.h"
31#include "tmf8829_regs.h"
32#include "tmf8829_types.h"
33
38#ifndef TMF8829_ENABLE_CAP_DISCHARGE_US
39#define TMF8829_ENABLE_CAP_DISCHARGE_US 3000u
40#endif
45#ifndef TMF8829_ENABLE_RAMP_US
46#define TMF8829_ENABLE_RAMP_US 3000u
47#endif
52#ifndef TMF8829_CPU_READY_TIMEOUT_MS
53#define TMF8829_CPU_READY_TIMEOUT_MS 3u
54#endif
55
61#ifndef TMF8829_WAKEUP_CPU_READY_TIMEOUT_MS
62#define TMF8829_WAKEUP_CPU_READY_TIMEOUT_MS 3u
63#endif
64
72typedef void (*tmf8829_log_fn)(tmf8829_driver_t* drv, int level, const char* msg);
73
82typedef void (*tmf8829_on_stream_header_fn)(tmf8829_driver_t* drv, const uint8_t* data, uint16_t len);
83
91typedef void (*tmf8829_on_result_fn)(tmf8829_driver_t* drv, const uint8_t* data, uint16_t len);
92
100typedef void (*tmf8829_on_histogram_fn)(tmf8829_driver_t* drv, const uint8_t* data, uint16_t len);
101
108typedef void (*tmf8829_on_error_fn)(tmf8829_driver_t* drv, int err);
109
130typedef int (*tmf8829_fw_image_read_fn)(tmf8829_driver_t* drv, uint32_t offset, uint8_t* buf, uint32_t len, uint32_t* image_total_size);
131
204
214
221
235
244
253int tmf8829_is_cpu_ready(tmf8829_driver_t* drv, uint8_t timeout_ms);
254
264int tmf8829_get_and_clr_interrupts(tmf8829_driver_t* drv, uint8_t mask, uint8_t* out_pending);
265
270
275
280
285
290
295
300
309int tmf8829_is_device_wakeup(tmf8829_driver_t* drv, uint8_t* out_awake);
310
326
336int tmf8829_command(tmf8829_driver_t* drv, uint8_t cmd, uint16_t timeout_ms);
337
342
347
356
365
370
383int tmf8829_download_firmware(tmf8829_driver_t* drv, uint32_t image_start_addr, int use_fifo);
384
395
402
404uint16_t tmf8829_get_uint16(const uint8_t* data);
406uint32_t tmf8829_get_uint32(const uint8_t* data);
408void tmf8829_set_uint16(uint16_t value, uint8_t* data);
409
415uint8_t tmf8829_get_pixel_size(uint8_t layout);
416
422uint16_t tmf8829_correct_distance(const tmf8829_driver_t* drv, uint16_t distance);
423
431void tmf8829_correct_distance_data_segment(tmf8829_driver_t* drv, uint16_t size, uint8_t layout);
432
439
446
460int tmf8829_set_i2c_address(tmf8829_driver_t* drv, uint8_t new_addr);
461
462#ifdef __cplusplus
463}
464#endif
465
466#endif /* TMF8829_H */
One driver instance per physical TMF8829.
Definition tmf8829.h:139
tmf8829_on_histogram_fn on_histogram
Definition tmf8829.h:168
uint8_t chip_version[2]
Definition tmf8829.h:193
uint16_t _clk_corr_ratio_uq
Definition tmf8829.h:196
uint32_t _device_ticks[TMF8829_CLK_CORRECTION_PAIRS]
Definition tmf8829.h:201
uint8_t _clk_corr_enable
Definition tmf8829.h:197
uint8_t * buffer
Definition tmf8829.h:149
uint32_t _initialised
Definition tmf8829.h:202
uint8_t i2c_addr
Definition tmf8829.h:143
uint32_t device_serial_number
Definition tmf8829.h:189
tmf8829_log_fn log
Definition tmf8829.h:162
tmf8829_on_error_fn on_error
Definition tmf8829.h:170
void * user_ctx
Definition tmf8829.h:145
tmf8829_bus_t bus
Definition tmf8829.h:141
tmf8829_fw_image_read_fn fw_image_read
Definition tmf8829.h:172
uint8_t _clk_corr_idx
Definition tmf8829.h:198
const tmf8829_ops_t * ops
Definition tmf8829.h:147
tmf8829_on_stream_header_fn on_stream_header
Definition tmf8829.h:164
uint32_t _host_ticks[TMF8829_CLK_CORRECTION_PAIRS]
Definition tmf8829.h:200
uint16_t max_write_len
Definition tmf8829.h:158
uint32_t host_ticks_per_1000_us
Definition tmf8829.h:180
uint8_t _cyclic_running
Definition tmf8829.h:199
uint8_t config[TMF8829_CFG_PAGE_SIZE]
Definition tmf8829.h:186
uint16_t buffer_len
Definition tmf8829.h:151
uint8_t app_version[4]
Definition tmf8829.h:191
uint8_t log_level
Definition tmf8829.h:160
tmf8829_on_result_fn on_result
Definition tmf8829.h:166
int tmf8829_disable(tmf8829_driver_t *drv)
Drive the enable pin low (sensor off).
int(* tmf8829_fw_image_read_fn)(tmf8829_driver_t *drv, uint32_t offset, uint8_t *buf, uint32_t len, uint32_t *image_total_size)
Read-only access to a firmware image for tmf8829_download_firmware.
Definition tmf8829.h:130
int tmf8829_set_configuration(tmf8829_driver_t *drv)
Write tmf8829_driver_t::config to the device (load page + write page sequence).
int tmf8829_cmd_load_config_page(tmf8829_driver_t *drv)
TMF8829_CMD_LOAD_CONFIG_PAGE
int tmf8829_download_firmware(tmf8829_driver_t *drv, uint32_t image_start_addr, int use_fifo)
Stream firmware through tmf8829_driver_t::fw_image_read and start the RAM application.
int tmf8829_bootloader_i2c_off(tmf8829_driver_t *drv)
Bootloader: TMF8829_BL_CMD_STAT_I2C_OFF.
int tmf8829_disable_interrupts(tmf8829_driver_t *drv, uint8_t mask)
Read-modify-write TMF8829_REG_INT_ENAB to disable mask bits.
void(* tmf8829_on_stream_header_fn)(tmf8829_driver_t *drv, const uint8_t *data, uint16_t len)
Invoked once per FIFO read with the fused pre-header and frame header.
Definition tmf8829.h:82
int tmf8829_set_i2c_address(tmf8829_driver_t *drv, uint8_t new_addr)
Change the device's I2C slave address and update the driver instance.
void(* tmf8829_log_fn)(tmf8829_driver_t *drv, int level, const char *msg)
Optional host logging callback (not invoked unless the driver calls it).
Definition tmf8829.h:72
void(* tmf8829_on_error_fn)(tmf8829_driver_t *drv, int err)
Optional error reporter (driver may call when a recoverable issue occurs).
Definition tmf8829.h:108
int tmf8829_soft_reset(tmf8829_driver_t *drv)
Write TMF8829_RESET_SOFT_MASK to TMF8829_REG_RESET and reset clock-correction state.
int tmf8829_get_and_clr_interrupts(tmf8829_driver_t *drv, uint8_t mask, uint8_t *out_pending)
Read TMF8829_REG_INT_STATUS and write-1-to-clear bits in mask.
int tmf8829_read_histogram(tmf8829_driver_t *drv)
Read a histogram frame from the FIFO (same header path as results).
int tmf8829_cmd_write_page(tmf8829_driver_t *drv)
TMF8829_CMD_WRITE_PAGE using tmf8829_driver_t::config
void tmf8829_set_uint16(uint16_t value, uint8_t *data)
Write value to two bytes, little-endian.
int tmf8829_clr_and_enable_interrupts(tmf8829_driver_t *drv, uint8_t mask)
Read-modify-write TMF8829_REG_INT_ENAB to enable mask bits.
int tmf8829_read_device_info(tmf8829_driver_t *drv)
Read serial number, app version, and chip id registers into drv.
int tmf8829_get_configuration(tmf8829_driver_t *drv)
After tmf8829_cmd_load_config_page, read the configuration page into tmf8829_driver_t::config.
int tmf8829_bootloader_spi_off(tmf8829_driver_t *drv)
Bootloader: TMF8829_BL_CMD_STAT_SPI_OFF.
int tmf8829_enable(tmf8829_driver_t *drv)
Drive the enable pin high and wait for CPU ready.
int tmf8829_hard_reset(tmf8829_driver_t *drv)
Write TMF8829_RESET_HARD_MASK to TMF8829_REG_RESET and wait until CPU is ready.
int tmf8829_set_log_level(tmf8829_driver_t *drv, uint8_t level)
Update tmf8829_driver_t::log_level for optional tmf8829_driver_t::log use.
int tmf8829_power_up(tmf8829_driver_t *drv)
Assert power-on (TMF8829_ENABLE_PON_MASK) and wait TMF8829_ENABLE_RAMP_US.
uint16_t tmf8829_clk_correction_ratio_uq15(const tmf8829_driver_t *drv)
Current UQ15 ratio used by tmf8829_correct_distance; nominal unity is 32768.
int tmf8829_clk_correction_set(tmf8829_driver_t *drv, uint8_t enable)
Enable or disable collection of clock-correction samples in result reads.
void tmf8829_correct_distance_data_segment(tmf8829_driver_t *drv, uint16_t size, uint8_t layout)
Apply tmf8829_correct_distance to each distance field in tmf8829_driver_t::buffer.
uint32_t tmf8829_get_uint32(const uint8_t *data)
Little-endian uint32 from four bytes.
int tmf8829_wakeup(tmf8829_driver_t *drv)
Leave low-power: if CPU not ready, set RAM boot path + PON and poll readiness.
int tmf8829_is_cpu_ready(tmf8829_driver_t *drv, uint8_t timeout_ms)
Poll TMF8829_REG_ENABLE until TMF8829_ENABLE_CPU_READY_MASK is set or time elapses.
void(* tmf8829_on_result_fn)(tmf8829_driver_t *drv, const uint8_t *data, uint16_t len)
Invoked for each result payload chunk read from the FIFO.
Definition tmf8829.h:91
void(* tmf8829_on_histogram_fn)(tmf8829_driver_t *drv, const uint8_t *data, uint16_t len)
Invoked for each histogram payload chunk read from the FIFO.
Definition tmf8829.h:100
int tmf8829_command(tmf8829_driver_t *drv, uint8_t cmd, uint16_t timeout_ms)
Issue a raw application command byte and poll TMF8829_REG_CMD_STAT for acceptance.
int tmf8829_read_results(tmf8829_driver_t *drv)
Read a result frame from the FIFO (header via TMF8829_REG_FIFO_STATUS, payload via TMF8829_REG_FIFO).
int tmf8829_standby(tmf8829_driver_t *drv)
Request standby if the CPU is running (sets TMF8829_ENABLE_POFF_MASK).
int tmf8829_stop_measurement(tmf8829_driver_t *drv)
TMF8829_CMD_STOP
int tmf8829_is_device_wakeup(tmf8829_driver_t *drv, uint8_t *out_awake)
Non-blocking wakeup check: reads enable register and CPU-ready bit.
int tmf8829_init(tmf8829_driver_t *drv, const tmf8829_ops_t *ops)
Validate parameters, bind ops, and reset driver state.
uint16_t tmf8829_get_uint16(const uint8_t *data)
Little-endian uint16 from two bytes.
uint8_t tmf8829_get_pixel_size(uint8_t layout)
Bytes per result pixel for a given TMF8829_REG_CFG_RESULT_FORMAT layout byte.
int tmf8829_start_measurement(tmf8829_driver_t *drv)
TMF8829_CMD_MEASURE
uint16_t tmf8829_correct_distance(const tmf8829_driver_t *drv, uint16_t distance)
Scale a raw distance using tmf8829_driver_t::_clk_corr_ratio_uq (UQ15).
Platform callback table (tmf8829_ops_t) for bus, time, and GPIO.
Register map, command/status codes, interrupt masks, frame layout, and bootloader constants.
#define TMF8829_CFG_PAGE_SIZE
Definition tmf8829_regs.h:217
Public typedefs, errors, log levels, and compile-time limits.
#define TMF8829_CLK_CORRECTION_PAIRS
Ring buffer depth for (host_tick, device_tick) clock-correction pairs.
Definition tmf8829_types.h:105
enum tmf8829_bus tmf8829_bus_t
Physical bus used to reach this device instance.
struct tmf8829_driver tmf8829_driver_t
Definition tmf8829_types.h:33
struct tmf8829_ops tmf8829_ops_t
Definition tmf8829_types.h:34