Hardware-agnostic API for Image Sensors.
This library provides a unified interface for controlling image sensors on microcontrollers.
It abstracts sensor-specific details and exposes a simplified API for applications (img_sensor.h), while defining low-level primitives for sensor drivers implementation (ll_img_sensor.h).
| API | img_sensor.h - High-level API |
ll_img_sensor.h - Low-level / sensor-oriented API |
|---|---|---|
| Target | Application code | Sensor driver implementations |
| Expo/Gain | exposure_time_us in microseconds, gain_analog / gain_digital (float ratios) |
exposure_time in line count, gain_analog / gain_digital in sensor units |
| Frame size | width / height in pixels |
selection_t with x_start/y_start/x_end/y_end coordinates |
| Frame rate | frame_rate in fps |
line_length + frame_length (pixel clock / line units) |
| Stream | Single stream | Multi-stream support (all functions take stream_id) |
| Nature | Generic abstraction layer (implementation in img_sensor.c) |
Low-level API contract that sensor drivers must implement |
mcu-img-sensor/
├── .clang-format # Code formatting rules (LLVM-based)
├── CMakeLists.txt # Build configuration
├── CHANGELOG.md # Version history
├── LICENSE.md # BSD-3-Clause license
├── img_sensor_type.h # Common type definitions (errors, enums, structs)
├── img_sensor.h # High-level API — application-friendly interface
├── img_sensor.c # High-level API implementation (wrappers & conversions)
├── ll_img_sensor.h # Low-level API — driver vtable and sensor-native types
└── README.md # This file
#include "img_sensor.h"
#include "my_sensor_driver.h"
// caller-allocated context (no dynamic memory allocation in driver)
static img_sensor_ctx_t sensor;
static my_sensor_private_t sensor_data;
my_sensor_new(&sensor, &sensor_data);
// Initialize with hardware config and IO control functions
img_sensor_hw_config_t hw_config = {
.comm.i2c.address = 0x20,
.comm.i2c.address_size = 2,
.comm.bus = BUS_TYPE_I2C,
.comm.bus_handler = &hi2c1,
.ext_clock = 12000000U,
};
img_sensor_io_t io = {
.read = interface_device_read,
.write = interface_device_write,
.delay = interface_device_delay,
.control_pin = interface_device_control_pin
};
img_sensor_init(&sensor, &hw_config, &io);
// Optionally, adjust settings
img_sensor_set_frame_size(&sensor, 640, 480);
img_sensor_set_frame_rate(&sensor, 60);
// Start streaming
img_sensor_start(&sensor);Available functions: See img_sensor.h
img_sensor_init()/deinit()img_sensor_get_capabilities()img_sensor_get/set_frame_size()img_sensor_get/set_pixel_depth(),img_sensor_get_pixel_pattern()img_sensor_get/set_frame_rate()img_sensor_start()/stop()img_sensor_get/set_exposure_gain()
┌──────────────────────┐
│ Application │
└──────────┬───────────┘
▼
┌─────────────────────────────────┐
│ img_sensor.h (High Level API) │
│ + Generic Implementation │
└────────────────┬────────────────┘
│ #include "ll_img_sensor.h"
▼
┌────────────────────────┐
│ ll_img_sensor.h │
│ Low-Level API Contract │
└───────────┬────────────┘
┌───────────────┼────────────────────┐
▼ ▼ ▼
┌──────────┐ ┌──────────┐ ┌──────────┐
│ VD56G3 │ │ IMX335 │ │ Sensor │
│ Driver │ │ Driver │ ... │ Driver │
└──────────┘ └──────────┘ └──────────┘
The low-level API defines the contract that drivers must implement to be exposed through the standard high-level API. It uses a vtable pattern where each driver populates an img_sensor_ctx_t structure with function pointers.
All functions are defined in ll_img_sensor.h
Core Functions [Mandatory]
init()- Power on, configure sensordeinit()- Power downstart()- Start streamingstop()- Stop streaming
Getters on Sensor Capabilities [Mandatory]
get_capabilities()- Report global sensor capabilities (list of available streams)get_stream_capabilities()- Report per-stream capabilities (frame size, timing, gain ranges)get_pixel_pattern()- Report the pixel nature (Bayer pattern, monochrome, etc.)get_pixel_depth()- Report bits per pixel (8, 10, 12, etc.)
Setters on Sensor Pixel Format [Optional, depend on sensor capabilities]
set_pixel_depth()- Set pixel resolution (8, 10, 12, etc.)
Controls on Exposure & Gain [Recommended for ISP Auto Exposure support]
get/set_exposure()- Gain (register values) + exposure (line counts)
Control on Frame & Timing [Recommended to enable continuous control over image size and framerate]
get/set_selection()- Crop window (x/y start/end, binning, subsampling)get/set_line_length()- Horizontal blanking (timing units)get/set_frame_length()- Vertical blanking (line units)
Conversion Callbacks [Required for High-Level API if any of the two above groups of functions are used]
analog_gain_to_reg()- Used by high-level APIset_exposure_gain()(float → sensor units)analog_gain_from_reg()- Used by high-level APIget_exposure_gain()(sensor units → float)digital_gain_to_reg()- Used by high-level APIset_exposure_gain()(float → sensor units)digital_gain_from_reg()- Used by high-level APIget_exposure_gain()(sensor units → float)us_to_lines()- Used by high-level APIsset_exposure_gain(),set_frame_rate()(µs → line count)lines_to_us()- Used by high-level APIsget_exposure_gain(),get_frame_rate()(line count → µs)
Additional controls [Optional]
get/set_ctrl()- Generic control function for future extensions (e.g. test pattern, HDR mode, etc.)
Streaming specific control [Optional]
get/set_stream_ctrl()- MIPI virtual channel, data type
Each sensor driver provides a new() function that populates the img_sensor_ctx_t structure with:
- Function pointers — the driver's implementation of each API operation (vtable)
- Conversion helpers (
conv.*) — unit-conversion callbacks - Private data (
private_data) — opaque, driver-specific context (allocated by the caller)
The remaining fields are set later by init():
- I/O callbacks (
img_sensor_io_t) — platform-provided read/write/delay/pin control - Hardware configuration (
img_sensor_hw_config_t) — bus, clock, MIPI lanes - State (
ll_img_sensor_state_t) — tracks the sensor state
// Driver registration
my_sensor_new(&ctx, &private_data); // Populates vtable
// High-level API uses vtable
img_sensor_init(&ctx, &hw_cfg, &io); // → calls ctx->init()All functions follow a strict pattern:
- Return value =
img_sensor_error_t(always) - Inputs = by value (scalars) or
constpointer (structs) - Outputs = by pointer
Conversion callbacks:
- Set
*rounded = truewhen value is clamped or approximated - Return error if value is completely out of range
No dynamic allocation — The driver never calls malloc() or free().
All persistent memory is caller-allocated:
// Application allocates everything
static img_sensor_ctx_t ctx;
static my_sensor_private_t priv_data;
// Driver just receives pointers
my_sensor_new(&ctx, &priv_data); // ctx->private_data = &priv_dataDriver maintains state in ctx->state (type: ll_img_sensor_state_t).
State machine:
NONE ────init()───┐
▲ (poweron)
| (boot)
(poweroff) |
| ▼
└─ deinit()── READY ◄─────────┐
| |
start() stop()
| |
▼ |
STREAM ─────────┘
Driver responsibilities:
- Update
ctx->stateat each transition - Cache sensor configuration in
private_data(line length, frame length, mode registers, etc.) - Validate operations against current state
- Create your driver source and header (e.g.,
my_sensor.c+my_sensor.h) - Include
"ll_img_sensor.h"and implement the required functions following the defined signatures - Implement a
my_sensor_new()function that populates theimg_sensor_ctx_tvtable and binds the caller-allocated private data - Document any sensor-specific behavior (e.g. optical-flow, ISL metadata) in the header comments
Naming conventions:
- Prefix driver-specific functions:
my_sensor_* - Static functions for vtable implementations
- Public
my_sensor_new()for registration
I/O Access:
- Use provided
ctx->io.read()/ctx->io.write()callbacks - Never access hardware directly (allows platform abstraction)
- Use
ctx->io.delay()for timing-sensitive operations
Coding style:
- Based on LLVM style with
IndentWidth: 4 - Enforces brace insertion (
InsertBraces: true) per MISRA-C:2012 rule 15.6 - Column limit: 120 characters
- See
.clang-formatfor full configuration