Skip to content

Commit 6cb0382

Browse files
committed
camera: try to defer init of video objects
This change, removes the automatic starting of the PWM clock on the GIGA, at startup. Instead it starts the clock if/when the sketch calls the Camera::begin method. But to make this work, we also need to not start up the video objects, until after the MCLK has been started. We can do that with marking them as zephyr,deferred-init
1 parent a26551e commit 6cb0382

File tree

4 files changed

+65
-33
lines changed

4 files changed

+65
-33
lines changed

libraries/Camera/src/camera.cpp

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,73 @@ Camera::Camera() : vdev(NULL), byte_swap(false), yuv_to_gray(false) {
4545
}
4646
}
4747

48+
#if defined(CONFIG_VIDEO)
49+
#include <zephyr/kernel.h>
50+
#include <zephyr/device.h>
51+
#include <zephyr/drivers/clock_control.h>
52+
#include <zephyr/logging/log.h>
53+
54+
int camera_ext_clock_enable(void) {
55+
int ret;
56+
uint32_t rate;
57+
const struct device *cam_ext_clk_dev = DEVICE_DT_GET(DT_NODELABEL(pwmclock));
58+
59+
if (!device_is_ready(cam_ext_clk_dev)) {
60+
return -ENODEV;
61+
}
62+
63+
ret = clock_control_on(cam_ext_clk_dev, (clock_control_subsys_t)0);
64+
if (ret < 0) {
65+
return ret;
66+
}
67+
68+
ret = clock_control_get_rate(cam_ext_clk_dev, (clock_control_subsys_t)0, &rate);
69+
if (ret < 0) {
70+
return ret;
71+
}
72+
73+
return 0;
74+
}
75+
#else
76+
#ERROR "CONFIG_VIDEO is not defined for this variant"
77+
#endif
78+
4879
bool Camera::begin(uint32_t width, uint32_t height, uint32_t pixformat, bool byte_swap) {
4980
#if DT_HAS_CHOSEN(zephyr_camera)
5081
this->vdev = DEVICE_DT_GET(DT_CHOSEN(zephyr_camera));
5182
#endif
5283

53-
if (!this->vdev || !device_is_ready(this->vdev)) {
84+
// start the clock
85+
int ret;
86+
87+
if (!this->vdev) {
5488
return false;
5589
}
5690

91+
camera_ext_clock_enable();
92+
delay(50);
93+
94+
if (!device_is_ready(this->vdev)) {
95+
// device probably has zephyr,deferred-init
96+
// On GIGA and Portenta H7 and probably others starts DCIM object
97+
if ((ret = device_init(this->vdev)) < 0) {
98+
printk("device_init camera(%p) failed:%d\n", this->vdev, ret);
99+
return false;
100+
}
101+
}
102+
103+
// Now see if the actual camera is defined in choosen. And see if it is ready
104+
#if DT_HAS_CHOSEN(zephyr_camera_sensor)
105+
const struct device *camera_sensor = DEVICE_DT_GET(DT_CHOSEN(zephyr_camera_sensor));
106+
if (!device_is_ready(camera_sensor)) {
107+
if ((ret = device_init(camera_sensor)) < 0) {
108+
printk("device_init camera sensor(%p) failed:%d\n", camera_sensor, ret);
109+
return false;
110+
}
111+
}
112+
113+
#endif
114+
57115
switch (pixformat) {
58116
case CAMERA_RGB565:
59117
this->byte_swap = byte_swap;

loader/fixups.c

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -72,38 +72,6 @@ static void zephyr_input_callback(struct input_event *evt, void *user_data) {
7272
INPUT_CALLBACK_DEFINE(NULL, zephyr_input_callback, NULL);
7373
#endif
7474

75-
#if defined(CONFIG_BOARD_ARDUINO_GIGA_R1) && defined(CONFIG_VIDEO)
76-
#include <zephyr/kernel.h>
77-
#include <zephyr/device.h>
78-
#include <zephyr/drivers/clock_control.h>
79-
#include <zephyr/logging/log.h>
80-
81-
int camera_ext_clock_enable(void) {
82-
int ret;
83-
uint32_t rate;
84-
const struct device *cam_ext_clk_dev = DEVICE_DT_GET(DT_NODELABEL(pwmclock));
85-
86-
if (!device_is_ready(cam_ext_clk_dev)) {
87-
return -ENODEV;
88-
}
89-
90-
ret = clock_control_on(cam_ext_clk_dev, (clock_control_subsys_t)0);
91-
if (ret < 0) {
92-
return ret;
93-
}
94-
95-
ret = clock_control_get_rate(cam_ext_clk_dev, (clock_control_subsys_t)0, &rate);
96-
if (ret < 0) {
97-
return ret;
98-
}
99-
100-
return 0;
101-
}
102-
103-
SYS_INIT(camera_ext_clock_enable, POST_KERNEL, CONFIG_CLOCK_CONTROL_PWM_INIT_PRIORITY);
104-
105-
#endif
106-
10775
#if defined(CONFIG_SHARED_MULTI_HEAP)
10876
#include <zephyr/kernel.h>
10977
#include <zephyr/devicetree.h>

variants/arduino_giga_r1_stm32h747xx_m7/arduino_giga_r1_stm32h747xx_m7.overlay

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
gc2145: gc2145@3c {
3333
compatible = "galaxycore,gc2145";
3434
reg = <0x3c>;
35+
zephyr,deferred-init;
3536
reset-gpios = <&gpiod 4 GPIO_ACTIVE_LOW>;
3637
pwdn-gpios = <&gpioa 1 GPIO_ACTIVE_LOW>;
3738

@@ -148,6 +149,7 @@
148149

149150
&dcmi {
150151
status = "okay";
152+
zephyr,deferred-init;
151153
/* ext-sdram = <&sdram1>; */
152154
pinctrl-0 = <&dcmi_hsync_ph8 &dcmi_pixclk_pa6 &dcmi_vsync_pi5
153155
&dcmi_d0_ph9 &dcmi_d1_ph10 &dcmi_d2_ph11 &dcmi_d3_pg11
@@ -320,6 +322,7 @@
320322
/{
321323
chosen {
322324
zephyr,camera = &dcmi;
325+
zephyr,camera-sensor = &gc2145;
323326
/* zephyr,console = &board_cdc_acm_uart; */
324327
};
325328

variants/arduino_portenta_h7_stm32h747xx_m7/arduino_portenta_h7_stm32h747xx_m7.overlay

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
gc2145: gc2145@3c {
2121
compatible = "galaxycore,gc2145";
2222
reg = <0x3c>;
23+
zephyr,deferred-init;
2324
status = "okay";
2425

2526
reset-gpios = <&gpioe 3 GPIO_ACTIVE_LOW>;
@@ -132,6 +133,7 @@
132133

133134
&dcmi {
134135
status = "okay";
136+
zephyr,deferred-init;
135137
/* ext-sdram = <&sdram1>; */
136138
pinctrl-0 = <&dcmi_hsync_pa4 &dcmi_pixclk_pa6 &dcmi_vsync_pi5
137139
&dcmi_d0_ph9 &dcmi_d1_ph10 &dcmi_d2_ph11 &dcmi_d3_ph12
@@ -245,6 +247,7 @@
245247
/ {
246248
chosen {
247249
zephyr,camera = &dcmi;
250+
zephyr,camera-sensor = &gc2145;
248251
zephyr,console = &usart6;
249252
zephyr,shell-uart = &usart6;
250253
zephyr,cdc-acm-uart0 = &usart6;

0 commit comments

Comments
 (0)