Skip to content

Commit d543705

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 814ad18 commit d543705

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
@@ -49,15 +49,73 @@ Camera::Camera() : byte_swap(false), yuv_to_gray(false), vdev(NULL) {
4949
}
5050
}
5151

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

57-
if (!this->vdev || !device_is_ready(this->vdev)) {
88+
// start the clock
89+
int ret;
90+
91+
if (!this->vdev) {
5892
return false;
5993
}
6094

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

loader/fixups.c

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

84-
#if defined(CONFIG_BOARD_ARDUINO_GIGA_R1) && defined(CONFIG_VIDEO)
85-
#include <zephyr/kernel.h>
86-
#include <zephyr/device.h>
87-
#include <zephyr/drivers/clock_control.h>
88-
#include <zephyr/logging/log.h>
89-
90-
int camera_ext_clock_enable(void) {
91-
int ret;
92-
uint32_t rate;
93-
const struct device *cam_ext_clk_dev = DEVICE_DT_GET(DT_NODELABEL(pwmclock));
94-
95-
if (!device_is_ready(cam_ext_clk_dev)) {
96-
return -ENODEV;
97-
}
98-
99-
ret = clock_control_on(cam_ext_clk_dev, (clock_control_subsys_t)0);
100-
if (ret < 0) {
101-
return ret;
102-
}
103-
104-
ret = clock_control_get_rate(cam_ext_clk_dev, (clock_control_subsys_t)0, &rate);
105-
if (ret < 0) {
106-
return ret;
107-
}
108-
109-
return 0;
110-
}
111-
112-
SYS_INIT(camera_ext_clock_enable, POST_KERNEL, CONFIG_CLOCK_CONTROL_PWM_INIT_PRIORITY);
113-
114-
#endif
115-
11684
#if defined(CONFIG_SHARED_MULTI_HEAP)
11785
#include <zephyr/kernel.h>
11886
#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)