From ae15878a8a6703a2e1a72e1365e0d34d9ad10cc2 Mon Sep 17 00:00:00 2001 From: Ibrahim Abdalkader Date: Fri, 24 Jan 2025 14:38:12 +0100 Subject: [PATCH] drivers: video: video_stm32_dcmi: Use video buffers for DCMI buffer. Instead of reserving a static (possibly unaligned) buffer for DCMI, this patch reserves and holds one of the video buffers to use as the main DCMI buffer. This buffer will be aligned (using the alignment specified in the config) and will either be allocated from `video_common` pool or a shared multi-heap (if enabled). Signed-off-by: Ibrahim Abdalkader --- drivers/video/video_stm32_dcmi.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/drivers/video/video_stm32_dcmi.c b/drivers/video/video_stm32_dcmi.c index 599d1a5d5a1b..f18d2a28c3b2 100644 --- a/drivers/video/video_stm32_dcmi.c +++ b/drivers/video/video_stm32_dcmi.c @@ -22,7 +22,9 @@ LOG_MODULE_REGISTER(video_stm32_dcmi, CONFIG_VIDEO_LOG_LEVEL); -K_HEAP_DEFINE(video_stm32_buffer_pool, CONFIG_VIDEO_BUFFER_POOL_SZ_MAX); +#if CONFIG_VIDEO_BUFFER_POOL_NUM_MAX < 2 +#error "The minimum required number of buffers for video_stm32 is 2" +#endif typedef void (*irq_config_func_t)(const struct device *dev); @@ -43,7 +45,7 @@ struct video_stm32_dcmi_data { uint32_t height; uint32_t width; uint32_t pitch; - uint8_t *buffer; + struct video_buffer *vbuf; }; struct video_stm32_dcmi_config { @@ -75,7 +77,7 @@ void HAL_DCMI_FrameEventCallback(DCMI_HandleTypeDef *hdcmi) } vbuf->timestamp = k_uptime_get_32(); - memcpy(vbuf->buffer, dev_data->buffer, vbuf->bytesused); + memcpy(vbuf->buffer, dev_data->vbuf->buffer, vbuf->bytesused); k_fifo_put(&dev_data->fifo_out, vbuf); @@ -244,16 +246,16 @@ static int video_stm32_dcmi_stream_start(const struct device *dev) { struct video_stm32_dcmi_data *data = dev->data; const struct video_stm32_dcmi_config *config = dev->config; - size_t buffer_size = data->pitch * data->height; - data->buffer = k_heap_alloc(&video_stm32_buffer_pool, buffer_size, K_NO_WAIT); - if (data->buffer == NULL) { - LOG_ERR("Failed to allocate DCMI buffer for image. Size %d bytes", buffer_size); + data->vbuf = k_fifo_get(&data->fifo_in, K_NO_WAIT); + + if (data->vbuf == NULL) { + LOG_ERR("Failed to dequeue a DCMI buffer."); return -ENOMEM; } int err = HAL_DCMI_Start_DMA(&data->hdcmi, DCMI_MODE_CONTINUOUS, - (uint32_t)data->buffer, buffer_size / 4); + (uint32_t)data->vbuf->buffer, data->vbuf->bytesused / 4); if (err != HAL_OK) { LOG_ERR("Failed to start DCMI DMA"); return -EIO; @@ -276,8 +278,8 @@ static int video_stm32_dcmi_stream_stop(const struct device *dev) return -EIO; } - /* Release the buffer allocated in stream_start */ - k_heap_free(&video_stm32_buffer_pool, data->buffer); + /* Release the video buffer allocated in stream_start */ + k_fifo_put(&data->fifo_in, data->vbuf); err = HAL_DCMI_Stop(&data->hdcmi); if (err != HAL_OK) {