Skip to content

Generic MIPI-CSI2 camera framework - #22753

Open
jlaitine wants to merge 17 commits into
apache:masterfrom
bittwist-fi:generic_csi2_camera
Open

jlaitine wants to merge 17 commits into
apache:masterfrom
bittwist-fi:generic_csi2_camera

Conversation

@jlaitine

@jlaitine jlaitine commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR improves the CSI2 camera support in NuttX. I earlier developed CSI2 camera support for another architecture,
and now there was IMX335 added on stm32n6 platfrom. I ported some of my earlier work on top of the current
master in this PR.

This gives the following:

  • MIPI-CSI2 camera drivers are usable in other architectures than just stm32n6; I have tested these versions also on imx9, which I intend to upstream later.
  • There are common helpers for i2c command interface for cameras, which can be reused by all the CSI camera drivers
  • Added separate analog and digital gain controls int v4l2 ioctl interface, this is similar to what linux has. The issue with having only one gain control is, that it hides from the user what is actually being controlled. All CSI cameras don't have digital gain, and many have separate controls for digital and analog already. The imx335 is actually a bit different than most cameras, having just one gain control register which first adjusts analog gain and then digital.
  • Add support in v4l2 for hardware level double buffering for imgdata hardware / drivers which support this. It is directly supported on both imx9 and stm32n6. The benefit from this is, that when one buffer gets ready, the next one starts filling up automatically; the user is not bound to update the buffer pointer during the vertical blanking period, which is very tight timing if the FPS is high ( like > 100 ).

Specifically for STM32N6 this gives:

  • Direct support for other CSI cameras as well, I added imx219 as an example, I will add more camera drivers later if there is need for those
  • Support for greyscale cameras
  • Bugfix for streaming stop (previous driver might have tried an i2c write to sensor from interrupt handler), streaming stop is deferred to LPWORK queue.
  • Added PIPE2 support for DCMIPP; I am using this to scale the image from PIPE1 directly into neuralart NPU's memory, but it can also be used for other purposes if two resolutions are needed simultaneously.

This PR grew quite large, when I just pulled in all the pieces which are somehow related. I can try to split it into smaller pieces if wanted; on the other hand it currently touches just one arch (stm32n6).

Some things to note:

  • I have used v4l2 fourcc formats directly for CSI cameras, as I didn't see a need to add every format again to imgdata/imgsensor enumerations. They share the same format field, but the numbering doesn't overlap (all the fourcc codes are > than the largest enumeration), so it is safe. The alternatives for this are: 1) just add more enumerations and switch cases to map the enums to fourcc and back or 2) switch all the formats to fourcc directly. I didn't like 1), and 2) is too large/intrusive for now, so I was thinking that this is a good middle ground. All the existing cameras work as is, and MIPI-CSI cameras just use fourcc formats directly. I am open to change this approach, of course, if it is not accepted.

Impact

Currently impacts only STM32N6 and IMX335 camera, since there are no other MIPI-CSI2 camera drivers.

Testing

On master this is tested on stm32n6570-dk:lvglcam. Test that the camera picture works on the display / functionality is unchanged. Plug in imx219 camera (arducam module) instaed, and observe that image comes to the display clean. On imx219 the image comes out stretched, but this is due to lvglcam app selecting the target resolution according to camera highest mode which is 16:9; I also tested changing lvglcam to request for 4:3 resolution always, and picture is clean / good. DCMIPP pipe2 support is not yet tested on master branch.

I have also run the same code with more camera modules and the sensor + v4l2 changes with another arch. Also I have been running the PIPE2 + NeuralART NPU outside of the repo with this DCMIPP driver, I just need to figure out how to bring in the NPU support into NuttX cleanly.

Add common sensor code to support MIPI-CSI2 camera sensors. This also
adds a new defconfig CONFIG_VIDEO_MIPI_CSI, which is selected by
the actual sensor configuration.

Signed-off-by: Jukka Laitinen <jukka.laitinen@bittwist.fi>
Add support for MIPI-CSI2 camera sensors into v4l2 and imgsensor.
The MIPI-CSI2 sensors need support for more image formats than what
are now defined in imgsensor. Instead of expanding the list of
supported types, just always use V4L2 fourcc types directly for all
MIPI-CSI2 sensors.

Assisted-by: GitHub Copilot:claude-sonnet-5.5
Signed-off-by: Jukka Laitinen <jukka.laitinen@bittwist.fi>
Add more sensor controls, needed mainly by CSI-2 cameras. Add
separate anlog and digital gain controls. Most CSI sensors use
analog gain. After analog gain is exhausted, typically adding digital
gain only adds noise. The analog and digital gains are also separate
nowdays in linux v4l2 userspace interface.

V4L2_CID_ANALOGUE_GAIN : Set analogue gain
V4L2_CID_DIGITAL_GAIN : Set digital gain
V4L2_CID_TEST_PATTERN : Set sensor to output test pattern
V4L2_CID_BLACK_LEVEL : Query sensor black level

Assisted-by: GitHub Copilot:claude-sonnet-5.5
Signed-off-by: Jukka Laitinen <jukka.laitinen@bittwist.fi>
The underlying imagedata driver can support buffering directly in hardware.
For example, the STM32N6 DCMIPP can be given the buffer of the next frame
while the current one is being written.

This adds support to v4l2_cap for initially passing more than one buffer
to the driver, before streaming start. On the image completion, the reception
of the next one is then already in progress, and the v4l2 passes the address
of the next of that one to the driver.

This is implemented by keeping track of how many buffers are "inflight",
that is, already passed to the underlying driver. It works in both RING and
FIFO modes. In FIFO mode the application holds a dequeued buffer, so at a
frame completion there may be no free buffer; the capture then continues
while a frame is in flight, and a buffer queued with VIDIOC_QBUF is passed
to the driver at once.

Assisted-by: GitHub Copilot:claude-opus-5.5
Signed-off-by: Jukka Laitinen <jukka.laitinen@bittwist.fi>
In RING mode with max_inflight > 1 the oldest completed buffer was taken
out of the dequeueable range as soon as it was handed to the driver
again. The driver does not write it until the frames in flight before it
have completed, so the frame in it is still valid for that time. With
only max_inflight buffers this made every frame undequeueable.

Keep such a buffer dequeueable until capture_done() reaches it. VIDIOC_DQBUF
returns a copy of its v4l2_buffer and leaves the container in the ring as
the pending in-flight entry; VIDIOC_QBUF of that buffer only releases the
application's claim. The sequence number is now set at completion, as the
buffer may carry an older frame when it is handed to the driver.

Assisted-by: GitHub Copilot:claude-sonnet-5.5
Signed-off-by: Jukka Laitinen <jukka.laitinen@bittwist.fi>
V4L2_PIX_FMT_GREY is 8bpp, just return width*height.

Signed-off-by: Jukka Laitinen <jukka.laitinen@bittwist.fi>
Bind the image sensor that the DCMIPP registers in front of the raw
sensor with csi_sensor_bind(): one RGB565 mode of the sensor frame size,
which the DCMIPP scales to any smaller size (CONFIG_VIDEO_MIPI_CSI_SCALING).
The mode is validated through the raw sensor, whose CSI-2 link parameters
are reported by get_mipi_csi_config().  The driver name and the frame
interval are those of the raw sensor.

The capture framework for DCMIPP and CSI sensors use V4L2 fourcc types
directly, not the IMGSENSOR/DATA_PIX_FMT.

Assisted-by: GitHub Copilot:claude-sonnet-5.5
Signed-off-by: Jukka Laitinen <jukka.laitinen@bittwist.fi>
Bind the IMX335 with csi_sensor_bind() instead of implementing the
imgsensor operations directly: two SRGGB10 modes (2592x1944 and 2/2 binned
1296x972) with selectable frame rates, the CSI-2 parameters in
get_mipi_csi_config() and the register access through the shared little
endian CCI helpers.  The sensor needs CONFIG_VIDEO_MIPI_CSI, which the
STM32 DCMIPP now selects.

The raw frame format that the DCMIPP requests from the sensor is a V4L2
fourcc now, as the csi_sensor modes are matched by fourcc.

Support separate analogue and digital gain controls. This sensor has only
one gain control register, but many other sensors have separate registers
for analogue and digital gain.

Assisted-by: GitHub Copilot:claude-opus-5.5
Signed-off-by: Jukka Laitinen <jukka.laitinen@bittwist.fi>

fix imx335 add separate analog and digital gain controls

Signed-off-by: Jukka Laitinen <jukka.laitinen@bittwist.fi>
Assisted-by: GitHub Copilot:claude-opus-5.5
Signed-off-by: Jukka Laitinen <jukka.laitinen@bittwist.fi>
Don't call IMGSENSOR_STOP_CAPTURE from interrupt handler, it needs I2C access.
Postpone the call to LPWORK item, and select LPWORK for DCMIPP always.

Signed-off-by: Jukka Laitinen <jukka.laitinen@bittwist.fi>
Just treat monochrome data as 8-bit luminance, skip the
demosaic.

Signed-off-by: Jukka Laitinen <jukka.laitinen@bittwist.fi>
Give the pipe the buffer of the next frame while the current one is being
written, so that the refill does not depend on the interrupt latency in
the vertical blanking.

The address and the capture request registers are shadowed and loaded at
the frame start. At the frame start interrupt the address of the frame
after it is programmed, or the capture request is cleared if there is no
buffer for it; either has the whole frame period. A buffer given later
during the frame is requested at once. A frame that finds no request is
not captured, so the DCMIPP never writes a buffer handed back to the
application.

The cache is invalidated over the exact buffer when it is given and when
its frame is complete, instead of a clean and invalidate of the whole
cache. Without CONFIG_STM32_DCMIPP_PIPE1_CPU, for frames that only go to
another bus master, a buffer is invalidated only the first time it is
given.

Assisted-by: GitHub Copilot:claude-opus-5.5
Signed-off-by: Jukka Laitinen <jukka.laitinen@bittwist.fi>
Support output pixel format of 8-bit grayscale (V4L2_PIX_FMT_GREY).
Asking for RGB565 output from DCMIPP, which is currently the only option,
packs the luma into each color component (5,6,5 bits). This works, but
causes banding; the quality is really bad due to reduced range.

Pipe1 can also output 8-bit grayscale directly, which preserves more bits
in the output luminance, and can be used as an input for VENC as well.

Signed-off-by: Jukka Laitinen <jukka.laitinen@bittwist.fi>
With CONFIG_STM32_DCMIPP_PIPE2 the DCMIPP registers its aux pipe with
capture_register() as a second V4L2 capture device, next to the pipe1
device. Pipe2 takes the output of the ISP of pipe1, so it has frames only
while pipe1 captures, and scales them to any smaller size as RGB888
(V4L2_PIX_FMT_RGB24 or BGR24), or as Y8 from a monochrome sensor. An
application can get a small RGB frame, e.g. for a neural network, without
scaling the frames on the CPU.

The buffers are requested frame by frame as for pipe1. The application
gives USERPTR buffers, as v4l2_cap sizes MMAP ones for 16-bit pixels.

Unless CONFIG_STM32_DCMIPP_PIPE2_CPU, the cache is invalidated over a
buffer once, when it is given, and left alone after the frames: the
buffer is for another DMA master, or the application invalidates it
before the CPU reads it.

The DCMIPP and the CSI-2 host are reset by the first of the capture
devices that is opened, not by each.

Assisted-by: GitHub Copilot:claude-opus-5.5
Signed-off-by: Jukka Laitinen <jukka.laitinen@bittwist.fi>
The automatic exposure of the DCMIPP drove V4L2_CID_GAIN, whose units
are sensor specific, with the number of gain steps that double the
gain given by the board.  Drive V4L2_CID_ANALOGUE_GAIN and
V4L2_CID_DIGITAL_GAIN instead, both linear Q16.16, so that any sensor
with these controls works without board tuning.

The exposure time is raised first, then the analogue gain, and the
digital gain only when the analogue gain is at its maximum, as it adds
more noise.  The digital gain is optional.  The values the sensor took
are read back, so a half step rounded away by the sensor is replaced by
the full step.

The gainsteps field of struct stm32_dcmipp_config_s is removed.

Assisted-by: GitHub Copilot:claude-opus-5.5
Add IMX219 MIPI-CSI2 color camera sensor driver.

Signed-off-by: Jukka Laitinen <jukka.laitinen@bittwist.fi>
Add initialization of imx219 imgsensor in stm32_camera.c and
enable the sensor in defconfig.

The stm32_camera.c just probes all the sensors which are configured
in, and gets the one that was found into use.

Signed-off-by: Jukka Laitinen <jukka.laitinen@bittwist.fi>
@jlaitine

jlaitine commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

@raiden00pl , I added the functionality which I presented to you earlier + some more in this same PR. Other reviewers, please just let me know if this is too large to digest.

AI has been used in creating some of the commits, and for re-basing (several times) and bugfixing these. Some commits are originally hand-written but later edited by help of AI. All are human-architected. All testing has been done by hand, and the code has been inspected by human (well, me).

@acassis acassis left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jlaitine please include a Documentation to explain the Generic MIPI-CSI2 camera framework. I think currently NuttX is missing even the basic documentation about it: https://nuttx.apache.org/docs/latest/os/video.html

@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants