Skip to content

Commit

Permalink
drivers: video: video_stm32_dcmi: Use video buffers for DCMI buffer.
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
iabdalkader authored and kartben committed Jan 28, 2025
1 parent e30c108 commit b58671f
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions drivers/video/video_stm32_dcmi.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

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

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

0 comments on commit b58671f

Please sign in to comment.