Skip to content

Commit 073ad40

Browse files
author
Alain Volmat
committed
video: stm32-dcmi: correct get/set fmt handling
This commit mainly correct the get/set format handling and how DCMI format is stored within the driver. struct video_format within the data structure is used to store the format. Reworked way to handle get format to avoid calling the sensor set_fmt whenever performing the get_fmt. Slightly adjusted code to as much as possible reuse return values provided by functions. Signed-off-by: Alain Volmat <alain.volmat@foss.st.com>
1 parent 1edadce commit 073ad40

File tree

1 file changed

+39
-42
lines changed

1 file changed

+39
-42
lines changed

drivers/video/video_stm32_dcmi.c

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,6 @@ struct video_stm32_dcmi_data {
4343
struct video_format fmt;
4444
struct k_fifo fifo_in;
4545
struct k_fifo fifo_out;
46-
uint32_t pixel_format;
47-
uint32_t height;
48-
uint32_t width;
49-
uint32_t pitch;
5046
struct video_buffer *vbuf;
5147
};
5248

@@ -127,7 +123,7 @@ static int stm32_dma_init(const struct device *dev)
127123

128124
/*
129125
* DMA configuration
130-
* Due to use of QSPI HAL API in current driver,
126+
* Due to use of DMA HAL API in current driver,
131127
* both HAL and Zephyr DMA drivers should be configured.
132128
* The required configuration for Zephyr DMA driver should only provide
133129
* the minimum information to inform the DMA slot will be in used and
@@ -179,18 +175,22 @@ static int stm32_dcmi_enable_clock(const struct device *dev)
179175
{
180176
const struct video_stm32_dcmi_config *config = dev->config;
181177
const struct device *dcmi_clock = DEVICE_DT_GET(STM32_CLOCK_CONTROL_NODE);
182-
int err;
183178

184179
if (!device_is_ready(dcmi_clock)) {
185180
LOG_ERR("clock control device not ready");
186181
return -ENODEV;
187182
}
188183

189184
/* Turn on DCMI peripheral clock */
190-
err = clock_control_on(dcmi_clock, (clock_control_subsys_t *) &config->pclken);
191-
if (err < 0) {
192-
LOG_ERR("Failed to enable DCMI clock. Error %d", err);
193-
return err;
185+
return clock_control_on(dcmi_clock, (clock_control_subsys_t *)&config->pclken);
186+
}
187+
188+
static inline int video_stm32_dcmi_is_fmt_valid(uint32_t pixelformat, uint32_t pitch,
189+
uint32_t height)
190+
{
191+
if (video_bits_per_pixel(pixelformat) / BITS_PER_BYTE == 0 ||
192+
pitch * height > CONFIG_VIDEO_BUFFER_POOL_SZ_MAX) {
193+
return -EINVAL;
194194
}
195195

196196
return 0;
@@ -202,25 +202,24 @@ static int video_stm32_dcmi_set_fmt(const struct device *dev,
202202
{
203203
const struct video_stm32_dcmi_config *config = dev->config;
204204
struct video_stm32_dcmi_data *data = dev->data;
205-
unsigned int bpp = video_bits_per_pixel(fmt->pixelformat) / BITS_PER_BYTE;
205+
int ret;
206206

207-
if (bpp == 0 || (ep != VIDEO_EP_OUT && ep != VIDEO_EP_ALL)) {
207+
if (ep != VIDEO_EP_OUT && ep != VIDEO_EP_ALL) {
208208
return -EINVAL;
209209
}
210210

211-
if ((fmt->pitch * fmt->height) > CONFIG_VIDEO_BUFFER_POOL_SZ_MAX) {
212-
return -EINVAL;
211+
ret = video_stm32_dcmi_is_fmt_valid(fmt->pixelformat, fmt->pitch, fmt->height);
212+
if (ret < 0) {
213+
return ret;
213214
}
214215

215-
data->pixel_format = fmt->pixelformat;
216-
data->pitch = fmt->pitch;
217-
data->height = fmt->height;
218-
data->width = fmt->width;
219-
220-
if (video_set_format(config->sensor_dev, ep, fmt)) {
221-
return -EIO;
216+
ret = video_set_format(config->sensor_dev, ep, fmt);
217+
if (ret < 0) {
218+
return ret;
222219
}
223220

221+
data->fmt = *fmt;
222+
224223
return 0;
225224
}
226225

@@ -230,33 +229,38 @@ static int video_stm32_dcmi_get_fmt(const struct device *dev,
230229
{
231230
struct video_stm32_dcmi_data *data = dev->data;
232231
const struct video_stm32_dcmi_config *config = dev->config;
232+
int ret;
233233

234234
if (fmt == NULL || (ep != VIDEO_EP_OUT && ep != VIDEO_EP_ALL)) {
235235
return -EINVAL;
236236
}
237237

238-
if (!video_get_format(config->sensor_dev, ep, fmt)) {
239-
/* align DCMI with sensor fmt */
240-
return video_stm32_dcmi_set_fmt(dev, ep, fmt);
238+
/* Align DCMI format with the one provided by the sensor */
239+
ret = video_get_format(config->sensor_dev, ep, fmt);
240+
if (ret < 0) {
241+
return ret;
242+
}
243+
244+
ret = video_stm32_dcmi_is_fmt_valid(fmt->pixelformat, fmt->pitch, fmt->height);
245+
if (ret < 0) {
246+
return ret;
241247
}
242248

243-
fmt->pixelformat = data->pixel_format;
244-
fmt->height = data->height;
245-
fmt->width = data->width;
246-
fmt->pitch = data->pitch;
249+
data->fmt = *fmt;
247250

248251
return 0;
249252
}
250253

251254
static int video_stm32_dcmi_set_stream(const struct device *dev, bool enable)
252255
{
253-
int err;
254256
struct video_stm32_dcmi_data *data = dev->data;
255257
const struct video_stm32_dcmi_config *config = dev->config;
258+
int err;
256259

257260
if (!enable) {
258-
if (video_stream_stop(config->sensor_dev)) {
259-
return -EIO;
261+
err = video_stream_stop(config->sensor_dev);
262+
if (err < 0) {
263+
return err;
260264
}
261265

262266
err = HAL_DCMI_Stop(&data->hdcmi);
@@ -285,19 +289,15 @@ static int video_stm32_dcmi_set_stream(const struct device *dev, bool enable)
285289
return -EIO;
286290
}
287291

288-
if (video_stream_start(config->sensor_dev)) {
289-
return -EIO;
290-
}
291-
292-
return 0;
292+
return video_stream_start(config->sensor_dev);
293293
}
294294

295295
static int video_stm32_dcmi_enqueue(const struct device *dev,
296296
enum video_endpoint_id ep,
297297
struct video_buffer *vbuf)
298298
{
299299
struct video_stm32_dcmi_data *data = dev->data;
300-
const uint32_t buffer_size = data->pitch * data->height;
300+
const uint32_t buffer_size = data->fmt.pitch * data->fmt.height;
301301

302302
if (ep != VIDEO_EP_OUT && ep != VIDEO_EP_ALL) {
303303
return -EINVAL;
@@ -339,7 +339,6 @@ static int video_stm32_dcmi_get_caps(const struct device *dev,
339339
struct video_caps *caps)
340340
{
341341
const struct video_stm32_dcmi_config *config = dev->config;
342-
int ret = -ENODEV;
343342

344343
if (ep != VIDEO_EP_OUT && ep != VIDEO_EP_ALL) {
345344
return -EINVAL;
@@ -349,9 +348,7 @@ static int video_stm32_dcmi_get_caps(const struct device *dev,
349348
caps->min_line_count = caps->max_line_count = LINE_COUNT_HEIGHT;
350349

351350
/* Forward the message to the sensor device */
352-
ret = video_get_caps(config->sensor_dev, ep, caps);
353-
354-
return ret;
351+
return video_get_caps(config->sensor_dev, ep, caps);
355352
}
356353

357354
static DEVICE_API(video, video_stm32_dcmi_driver_api) = {
@@ -479,7 +476,7 @@ static int video_stm32_dcmi_init(const struct device *dev)
479476
err = stm32_dcmi_enable_clock(dev);
480477
if (err < 0) {
481478
LOG_ERR("Clock enabling failed.");
482-
return -EIO;
479+
return err;
483480
}
484481

485482
data->dev = dev;

0 commit comments

Comments
 (0)