Skip to content

Commit

Permalink
drivers: video: video_stm32_dcmi: Use shared multi-heap if enabled.
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
iabdalkader committed Jan 23, 2025
1 parent ddff91f commit aa2a9d8
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions drivers/video/video_stm32_dcmi.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,17 @@

LOG_MODULE_REGISTER(video_stm32_dcmi, CONFIG_VIDEO_LOG_LEVEL);

#if defined(CONFIG_VIDEO_BUFFER_USE_SHARED_MULTI_HEAP)
#include <zephyr/multi_heap/shared_multi_heap.h>
#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);

Expand Down Expand Up @@ -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);

Check warning on line 259 in drivers/video/video_stm32_dcmi.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

LONG_LINE

drivers/video/video_stm32_dcmi.c:259 line length of 102 exceeds 100 columns
if (data->buffer == NULL) {
LOG_ERR("Failed to allocate DCMI buffer for image. Size %d bytes", buffer_size);
return -ENOMEM;
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit aa2a9d8

Please sign in to comment.