From aa2a9d81b10739d3bd685ab0d9f93e8ab7298ff5 Mon Sep 17 00:00:00 2001 From: Ibrahim Abdalkader Date: Thu, 23 Jan 2025 14:05:33 +0100 Subject: [PATCH] drivers: video: video_stm32_dcmi: Use shared multi-heap if enabled. The stm32 video driver currently allocates a static pool in SRAM which: a) Does not respect the alignment specified in `CONFIG_VIDEO_BUFFER_POOL_ALIGN`. b) Overflows the SRAM if multi-heap is enabled and a bigger buffer size is used. This patch allocates an aligned DCMI buffer from the shared multi-heap pool (if enabled), or from the static pool if not, similar to how it's done in `video_common.c`. Signed-off-by: Ibrahim Abdalkader --- drivers/video/video_stm32_dcmi.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/drivers/video/video_stm32_dcmi.c b/drivers/video/video_stm32_dcmi.c index 599d1a5d5a1b..33a9971febb0 100644 --- a/drivers/video/video_stm32_dcmi.c +++ b/drivers/video/video_stm32_dcmi.c @@ -22,7 +22,17 @@ LOG_MODULE_REGISTER(video_stm32_dcmi, CONFIG_VIDEO_LOG_LEVEL); +#if defined(CONFIG_VIDEO_BUFFER_USE_SHARED_MULTI_HEAP) +#include +#define VIDEO_STM32_HEAP_ALLOC(align, size, timeout) \ + shared_multi_heap_aligned_alloc(CONFIG_VIDEO_BUFFER_SMH_ATTRIBUTE, align, size) +#define VIDEO_STM32_HEAP_FREE(block) shared_multi_heap_free(block) +#else K_HEAP_DEFINE(video_stm32_buffer_pool, CONFIG_VIDEO_BUFFER_POOL_SZ_MAX); +#define VIDEO_STM32_HEAP_ALLOC(align, size, timeout) \ + k_heap_aligned_alloc(&video_stm32_buffer_pool, align, size, timeout); +#define VIDEO_STM32_HEAP_FREE(block) k_heap_free(&video_stm32_buffer_pool, block) +#endif typedef void (*irq_config_func_t)(const struct device *dev); @@ -246,7 +256,7 @@ static int video_stm32_dcmi_stream_start(const struct device *dev) 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); + data->buffer = VIDEO_STM32_HEAP_ALLOC(CONFIG_VIDEO_BUFFER_POOL_ALIGN, buffer_size, K_NO_WAIT); if (data->buffer == NULL) { LOG_ERR("Failed to allocate DCMI buffer for image. Size %d bytes", buffer_size); return -ENOMEM; @@ -277,7 +287,7 @@ static int video_stm32_dcmi_stream_stop(const struct device *dev) } /* Release the buffer allocated in stream_start */ - k_heap_free(&video_stm32_buffer_pool, data->buffer); + VIDEO_STM32_HEAP_FREE(data->buffer); err = HAL_DCMI_Stop(&data->hdcmi); if (err != HAL_OK) {