24
24
* THE SOFTWARE.
25
25
*/
26
26
27
+ #include "py/mperrno.h"
27
28
#include "py/runtime.h"
28
29
29
30
#include "bindings/esp32_camera/Camera.h"
30
31
#include "bindings/espidf/__init__.h"
31
32
#include "common-hal/esp32_camera/Camera.h"
33
+ #include "shared-bindings/busio/I2C.h"
32
34
#include "shared-bindings/microcontroller/Pin.h"
35
+ #include "shared-bindings/util.h"
33
36
#include "common-hal/microcontroller/Pin.h"
34
37
35
38
#include "esp32-camera/driver/private_include/cam_hal.h"
38
41
#error esp32_camera only works on boards configured with spiram, disable it in mpconfigboard.mk
39
42
#endif
40
43
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
+
41
57
static void maybe_claim_pin (const mcu_pin_obj_t * pin ) {
42
58
if (pin ) {
43
59
claim_pin (pin );
@@ -53,7 +69,7 @@ void common_hal_esp32_camera_camera_construct(
53
69
const mcu_pin_obj_t * href_pin ,
54
70
const mcu_pin_obj_t * powerdown_pin ,
55
71
const mcu_pin_obj_t * reset_pin ,
56
- const busio_i2c_obj_t * i2c ,
72
+ busio_i2c_obj_t * i2c ,
57
73
mp_int_t external_clock_frequency ,
58
74
pixformat_t pixel_format ,
59
75
framesize_t frame_size ,
@@ -78,6 +94,8 @@ void common_hal_esp32_camera_camera_construct(
78
94
79
95
common_hal_pwmio_pwmout_construct (& self -> pwm , external_clock_pin , 1 , external_clock_frequency , true);
80
96
97
+ self -> i2c = i2c ;
98
+
81
99
self -> camera_config .pin_pwdn = common_hal_mcu_pin_number (powerdown_pin );
82
100
self -> camera_config .pin_reset = common_hal_mcu_pin_number (reset_pin );
83
101
self -> camera_config .pin_xclk = common_hal_mcu_pin_number (external_clock_pin );
@@ -112,7 +130,11 @@ void common_hal_esp32_camera_camera_construct(
112
130
113
131
self -> camera_config .sccb_i2c_port = i2c -> i2c_num ;
114
132
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 );
116
138
}
117
139
118
140
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
165
187
166
188
#define SENSOR_GET (type , name , status_field_name , setter_function_name ) \
167
189
type common_hal_esp32_camera_camera_get_##name(esp32_camera_camera_obj_t * self) { \
190
+ i2c_lock(self); \
168
191
sensor_t *sensor = esp_camera_sensor_get(); \
192
+ i2c_unlock(self); \
169
193
if (!sensor->setter_function_name) { \
170
194
mp_raise_AttributeError(translate("no such attribute")); \
171
195
} \
@@ -174,7 +198,9 @@ camera_fb_t *common_hal_esp32_camera_camera_take(esp32_camera_camera_obj_t *self
174
198
175
199
#define SENSOR_SET (type , name , setter_function_name ) \
176
200
void common_hal_esp32_camera_camera_set_##name(esp32_camera_camera_obj_t * self, type value) { \
201
+ i2c_lock(self); \
177
202
sensor_t *sensor = esp_camera_sensor_get(); \
203
+ i2c_unlock(self); \
178
204
if (!sensor->setter_function_name) { \
179
205
mp_raise_AttributeError(translate("no such attribute")); \
180
206
} \
@@ -203,6 +229,7 @@ void common_hal_esp32_camera_camera_reconfigure(esp32_camera_camera_obj_t *self,
203
229
frame_size = sensor_info -> max_size ;
204
230
}
205
231
232
+ i2c_lock (self );
206
233
cam_deinit ();
207
234
self -> camera_config .pixel_format = pixel_format ;
208
235
self -> camera_config .frame_size = frame_size ;
@@ -212,6 +239,7 @@ void common_hal_esp32_camera_camera_reconfigure(esp32_camera_camera_obj_t *self,
212
239
sensor -> set_framesize (sensor , self -> camera_config .frame_size );
213
240
cam_init (& self -> camera_config );
214
241
cam_config (& self -> camera_config , frame_size , sensor_info -> pid );
242
+ i2c_unlock (self );
215
243
cam_start ();
216
244
}
217
245
0 commit comments