Skip to content

Commit f3c090b

Browse files
iabdalkaderfacchinm
authored andcommitted
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 <[email protected]>
1 parent 43fd683 commit f3c090b

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

drivers/video/video_stm32_dcmi.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222

2323
LOG_MODULE_REGISTER(video_stm32_dcmi, CONFIG_VIDEO_LOG_LEVEL);
2424

25-
K_HEAP_DEFINE(video_stm32_buffer_pool, CONFIG_VIDEO_BUFFER_POOL_SZ_MAX);
25+
#if CONFIG_VIDEO_BUFFER_POOL_NUM_MAX < 2
26+
#error "The minimum required number of buffers for video_stm32 is 2"
27+
#endif
2628

2729
typedef void (*irq_config_func_t)(const struct device *dev);
2830

@@ -43,7 +45,7 @@ struct video_stm32_dcmi_data {
4345
uint32_t height;
4446
uint32_t width;
4547
uint32_t pitch;
46-
uint8_t *buffer;
48+
struct video_buffer *vbuf;
4749
};
4850

4951
struct video_stm32_dcmi_config {
@@ -75,7 +77,7 @@ void HAL_DCMI_FrameEventCallback(DCMI_HandleTypeDef *hdcmi)
7577
}
7678

7779
vbuf->timestamp = k_uptime_get_32();
78-
memcpy(vbuf->buffer, dev_data->buffer, vbuf->bytesused);
80+
memcpy(vbuf->buffer, dev_data->vbuf->buffer, vbuf->bytesused);
7981

8082
k_fifo_put(&dev_data->fifo_out, vbuf);
8183

@@ -244,16 +246,16 @@ static int video_stm32_dcmi_stream_start(const struct device *dev)
244246
{
245247
struct video_stm32_dcmi_data *data = dev->data;
246248
const struct video_stm32_dcmi_config *config = dev->config;
247-
size_t buffer_size = data->pitch * data->height;
248249

249-
data->buffer = k_heap_alloc(&video_stm32_buffer_pool, buffer_size, K_NO_WAIT);
250-
if (data->buffer == NULL) {
251-
LOG_ERR("Failed to allocate DCMI buffer for image. Size %d bytes", buffer_size);
250+
data->vbuf = k_fifo_get(&data->fifo_in, K_NO_WAIT);
251+
252+
if (data->vbuf == NULL) {
253+
LOG_ERR("Failed to dequeue a DCMI buffer.");
252254
return -ENOMEM;
253255
}
254256

255257
int err = HAL_DCMI_Start_DMA(&data->hdcmi, DCMI_MODE_CONTINUOUS,
256-
(uint32_t)data->buffer, buffer_size / 4);
258+
(uint32_t)data->vbuf->buffer, data->vbuf->bytesused / 4);
257259
if (err != HAL_OK) {
258260
LOG_ERR("Failed to start DCMI DMA");
259261
return -EIO;
@@ -276,8 +278,8 @@ static int video_stm32_dcmi_stream_stop(const struct device *dev)
276278
return -EIO;
277279
}
278280

279-
/* Release the buffer allocated in stream_start */
280-
k_heap_free(&video_stm32_buffer_pool, data->buffer);
281+
/* Release the video buffer allocated in stream_start */
282+
k_fifo_put(&data->fifo_in, data->vbuf);
281283

282284
err = HAL_DCMI_Stop(&data->hdcmi);
283285
if (err != HAL_OK) {

0 commit comments

Comments
 (0)