Skip to content

Commit 9f84301

Browse files
committed
Merge remote-tracking branch 'origin/main'
2 parents c19bcea + 68386fa commit 9f84301

File tree

4 files changed

+41
-11
lines changed

4 files changed

+41
-11
lines changed

ports/espressif/bindings/esp32_camera/Camera.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@
4646
//| self,
4747
//| *,
4848
//| data_pins: List[microcontroller.Pin],
49-
//| pixel_clock: microcontroller.Pin,
50-
//| vsync: microcontroller.Pin,
51-
//| href: microcontroller.Pin,
49+
//| pixel_clock_pin: microcontroller.Pin,
50+
//| vsync_pin: microcontroller.Pin,
51+
//| href_pin: microcontroller.Pin,
5252
//| i2c: busio.I2C,
5353
//| external_clock_pin: microcontroller.Pin,
5454
//| external_clock_frequency: int,
@@ -79,12 +79,12 @@
7979
//| that case.
8080
//|
8181
//| :param data_pins: The 8 data data_pins used for image data transfer from the camera module, least significant bit first
82-
//| :param pixel_clock: The pixel clock output from the camera module
83-
//| :param vsync: The vertical sync pulse output from the camera module
84-
//| :param href: The horizontal reference output from the camera module
82+
//| :param pixel_clock_pin: The pixel clock output from the camera module
83+
//| :param vsync_pin: The vertical sync pulse output from the camera module
84+
//| :param href_pin: The horizontal reference output from the camera module
8585
//| :param i2c: The I2C bus connected to the camera module
86-
//| :param external_clock_frequency: The frequency generated on the external clock pin
8786
//| :param external_clock_pin: The pin on which to generate the external clock
87+
//| :param external_clock_frequency: The frequency generated on the external clock pin
8888
//| :param powerdown_pin: The powerdown input to the camera module
8989
//| :param reset_pin: The reset input to the camera module
9090
//| :param pixel_format: The pixel format of the captured image
@@ -125,7 +125,7 @@ STATIC mp_obj_t esp32_camera_camera_make_new(const mp_obj_type_t *type, size_t n
125125
const mcu_pin_obj_t *pixel_clock_pin = validate_obj_is_free_pin(args[ARG_pixel_clock_pin].u_obj);
126126
const mcu_pin_obj_t *vsync_pin = validate_obj_is_free_pin(args[ARG_vsync_pin].u_obj);
127127
const mcu_pin_obj_t *href_pin = validate_obj_is_free_pin(args[ARG_href_pin].u_obj);
128-
const busio_i2c_obj_t *i2c = MP_OBJ_TO_PTR(mp_arg_validate_type(args[ARG_i2c].u_obj, &busio_i2c_type, MP_QSTR_i2c));
128+
busio_i2c_obj_t *i2c = MP_OBJ_TO_PTR(mp_arg_validate_type(args[ARG_i2c].u_obj, &busio_i2c_type, MP_QSTR_i2c));
129129
const mcu_pin_obj_t *external_clock_pin = validate_obj_is_free_pin(args[ARG_external_clock_pin].u_obj);
130130
const mcu_pin_obj_t *powerdown_pin = validate_obj_is_free_pin_or_none(args[ARG_powerdown_pin].u_obj);
131131
const mcu_pin_obj_t *reset_pin = validate_obj_is_free_pin_or_none(args[ARG_reset_pin].u_obj);

ports/espressif/bindings/esp32_camera/Camera.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ extern void common_hal_esp32_camera_camera_construct(
4545
const mcu_pin_obj_t *href_pin,
4646
const mcu_pin_obj_t *powerdown_pin,
4747
const mcu_pin_obj_t *reset_pin,
48-
const busio_i2c_obj_t *i2c,
48+
busio_i2c_obj_t *i2c,
4949
mp_int_t external_clock_frequency,
5050
pixformat_t pixel_format,
5151
framesize_t frame_size,

ports/espressif/common-hal/esp32_camera/Camera.c

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,15 @@
2424
* THE SOFTWARE.
2525
*/
2626

27+
#include "py/mperrno.h"
2728
#include "py/runtime.h"
2829

2930
#include "bindings/esp32_camera/Camera.h"
3031
#include "bindings/espidf/__init__.h"
3132
#include "common-hal/esp32_camera/Camera.h"
33+
#include "shared-bindings/busio/I2C.h"
3234
#include "shared-bindings/microcontroller/Pin.h"
35+
#include "shared-bindings/util.h"
3336
#include "common-hal/microcontroller/Pin.h"
3437

3538
#include "esp32-camera/driver/private_include/cam_hal.h"
@@ -38,6 +41,19 @@
3841
#error esp32_camera only works on boards configured with spiram, disable it in mpconfigboard.mk
3942
#endif
4043

44+
static void i2c_lock(esp32_camera_camera_obj_t *self) {
45+
if (common_hal_busio_i2c_deinited(self->i2c)) {
46+
raise_deinited_error();
47+
}
48+
if (!common_hal_busio_i2c_try_lock(self->i2c)) {
49+
mp_raise_OSError(MP_EWOULDBLOCK);
50+
}
51+
}
52+
53+
static void i2c_unlock(esp32_camera_camera_obj_t *self) {
54+
common_hal_busio_i2c_unlock(self->i2c);
55+
}
56+
4157
static void maybe_claim_pin(const mcu_pin_obj_t *pin) {
4258
if (pin) {
4359
claim_pin(pin);
@@ -53,7 +69,7 @@ void common_hal_esp32_camera_camera_construct(
5369
const mcu_pin_obj_t *href_pin,
5470
const mcu_pin_obj_t *powerdown_pin,
5571
const mcu_pin_obj_t *reset_pin,
56-
const busio_i2c_obj_t *i2c,
72+
busio_i2c_obj_t *i2c,
5773
mp_int_t external_clock_frequency,
5874
pixformat_t pixel_format,
5975
framesize_t frame_size,
@@ -78,6 +94,8 @@ void common_hal_esp32_camera_camera_construct(
7894

7995
common_hal_pwmio_pwmout_construct(&self->pwm, external_clock_pin, 1, external_clock_frequency, true);
8096

97+
self->i2c = i2c;
98+
8199
self->camera_config.pin_pwdn = common_hal_mcu_pin_number(powerdown_pin);
82100
self->camera_config.pin_reset = common_hal_mcu_pin_number(reset_pin);
83101
self->camera_config.pin_xclk = common_hal_mcu_pin_number(external_clock_pin);
@@ -112,7 +130,11 @@ void common_hal_esp32_camera_camera_construct(
112130

113131
self->camera_config.sccb_i2c_port = i2c->i2c_num;
114132

115-
CHECK_ESP_RESULT(esp_camera_init(&self->camera_config));
133+
i2c_lock(self);
134+
esp_err_t result = esp_camera_init(&self->camera_config);
135+
i2c_unlock(self);
136+
137+
CHECK_ESP_RESULT(result);
116138
}
117139

118140
extern void common_hal_esp32_camera_camera_deinit(esp32_camera_camera_obj_t *self) {
@@ -165,7 +187,9 @@ camera_fb_t *common_hal_esp32_camera_camera_take(esp32_camera_camera_obj_t *self
165187

166188
#define SENSOR_GET(type, name, status_field_name, setter_function_name) \
167189
type common_hal_esp32_camera_camera_get_##name(esp32_camera_camera_obj_t * self) { \
190+
i2c_lock(self); \
168191
sensor_t *sensor = esp_camera_sensor_get(); \
192+
i2c_unlock(self); \
169193
if (!sensor->setter_function_name) { \
170194
mp_raise_AttributeError(translate("no such attribute")); \
171195
} \
@@ -174,7 +198,9 @@ camera_fb_t *common_hal_esp32_camera_camera_take(esp32_camera_camera_obj_t *self
174198

175199
#define SENSOR_SET(type, name, setter_function_name) \
176200
void common_hal_esp32_camera_camera_set_##name(esp32_camera_camera_obj_t * self, type value) { \
201+
i2c_lock(self); \
177202
sensor_t *sensor = esp_camera_sensor_get(); \
203+
i2c_unlock(self); \
178204
if (!sensor->setter_function_name) { \
179205
mp_raise_AttributeError(translate("no such attribute")); \
180206
} \
@@ -203,6 +229,7 @@ void common_hal_esp32_camera_camera_reconfigure(esp32_camera_camera_obj_t *self,
203229
frame_size = sensor_info->max_size;
204230
}
205231

232+
i2c_lock(self);
206233
cam_deinit();
207234
self->camera_config.pixel_format = pixel_format;
208235
self->camera_config.frame_size = frame_size;
@@ -212,6 +239,7 @@ void common_hal_esp32_camera_camera_reconfigure(esp32_camera_camera_obj_t *self,
212239
sensor->set_framesize(sensor, self->camera_config.frame_size);
213240
cam_init(&self->camera_config);
214241
cam_config(&self->camera_config, frame_size, sensor_info->pid);
242+
i2c_unlock(self);
215243
cam_start();
216244
}
217245

ports/espressif/common-hal/esp32_camera/Camera.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@
2929
#include "py/obj.h"
3030
#include "esp_camera.h"
3131
#include "shared-bindings/pwmio/PWMOut.h"
32+
#include "common-hal/busio/I2C.h"
3233

3334
typedef struct esp32_camera_camera_obj {
3435
mp_obj_base_t base;
3536
camera_config_t camera_config;
3637
camera_fb_t *buffer_to_return;
3738
pwmio_pwmout_obj_t pwm;
39+
busio_i2c_obj_t *i2c;
3840
} esp32_camera_obj_t;

0 commit comments

Comments
 (0)