Skip to content

Commit a023f92

Browse files
committed
address review
1 parent 3ed7698 commit a023f92

File tree

11 files changed

+41
-20
lines changed

11 files changed

+41
-20
lines changed

ports/atmel-samd/common-hal/busio/I2C.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
5353
uint32_t sda_pinmux, scl_pinmux;
5454

5555
// Ensure the object starts in its deinit state.
56-
self->sda_pin = NO_PIN;
56+
common_hal_busio_i2c_mark_deinit(self);
57+
5758
Sercom *sercom = samd_i2c_get_sercom(scl, sda, &sercom_index, &sda_pinmux, &scl_pinmux);
5859
if (sercom == NULL) {
5960
raise_ValueError_invalid_pins();
@@ -108,7 +109,6 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
108109
mp_arg_error_invalid(MP_QSTR_frequency);
109110
}
110111

111-
112112
if (i2c_m_sync_enable(&self->i2c_desc) != ERR_NONE) {
113113
common_hal_busio_i2c_deinit(self);
114114
mp_raise_OSError(MP_EIO);
@@ -141,8 +141,6 @@ void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) {
141141

142142
reset_pin_number(self->sda_pin);
143143
reset_pin_number(self->scl_pin);
144-
self->sda_pin = NO_PIN;
145-
self->scl_pin = NO_PIN;
146144
common_hal_busio_i2c_mark_deinit(self);
147145
}
148146

ports/broadcom/common-hal/busio/I2C.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,14 @@ static bool i2c_in_use[NUM_I2C];
2727

2828
void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
2929
const mcu_pin_obj_t *scl, const mcu_pin_obj_t *sda, uint32_t frequency, uint32_t timeout) {
30+
31+
// Ensure the object starts in its deinit state.
32+
common_hal_busio_i2c_mark_deinit(self);
33+
3034
size_t instance_index = NUM_I2C;
3135
uint8_t scl_alt = 0;
3236
uint8_t sda_alt = 0;
37+
3338
for (scl_alt = 0; scl_alt < 6; scl_alt++) {
3439
if (scl->functions[scl_alt].type != PIN_FUNCTION_I2C ||
3540
i2c_in_use[scl->functions[scl_alt].index] ||
@@ -82,8 +87,6 @@ void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) {
8287

8388
common_hal_reset_pin(self->sda_pin);
8489
common_hal_reset_pin(self->scl_pin);
85-
self->sda_pin = NULL;
86-
self->scl_pin = NULL;
8790
common_hal_busio_i2c_mark_deinit(self);
8891
}
8992

ports/cxd56/common-hal/busio/I2C.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
void common_hal_busio_i2c_construct(busio_i2c_obj_t *self, const mcu_pin_obj_t *scl,
1818
const mcu_pin_obj_t *sda, uint32_t frequency, uint32_t timeout) {
19+
20+
// Ensure the object starts in its deinit state.
21+
common_hal_busio_i2c_mark_deinit(self);
22+
1923
if (frequency != I2C_SPEED_STANDARD && frequency != I2C_SPEED_FAST) {
2024
mp_arg_error_invalid(MP_QSTR_frequency);
2125
}

ports/espressif/common-hal/busio/I2C.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
1818
const mcu_pin_obj_t *scl, const mcu_pin_obj_t *sda, uint32_t frequency, uint32_t timeout_us) {
19+
20+
// Ensure the object starts in its deinit state.
21+
common_hal_busio_i2c_mark_deinit(self);
22+
1923
// Pins 45 and 46 are "strapping" pins that impact start up behavior. They usually need to
2024
// be pulled-down so pulling them up for I2C is a bad idea. To make this hard, we don't
2125
// support I2C on these pins.
@@ -125,13 +129,11 @@ void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) {
125129

126130
common_hal_reset_pin(self->sda_pin);
127131
common_hal_reset_pin(self->scl_pin);
128-
self->sda_pin = NULL;
129-
self->scl_pin = NULL;
130132
common_hal_busio_i2c_mark_deinit(self);
131133
}
132134

133135
bool common_hal_busio_i2c_probe(busio_i2c_obj_t *self, uint8_t addr) {
134-
esp_err_t result = i2c_master_probe(self->handle, addr, 1);
136+
esp_err_t result = i2c_master_probe(self->handle, addr, 10);
135137
return result == ESP_OK;
136138
}
137139

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ void common_hal_espcamera_camera_construct(
7878
self->i2c = i2c;
7979

8080
// These pins might be NULL because they were not specified.
81+
// Note that common_hal_mcu_pin_number() returns NO_PIN (- =1) if pass NULL, but as a `uint8_t`,
82+
// so it becomes 255. The camera driver expects non-specified pins to be < 0.
83+
// So we have to set the pins to NO_PIN explicitly,
84+
// instead of relying on the return value from common_hal_mcu_pin_number().
8185
self->camera_config.pin_pwdn = powerdown_pin ? common_hal_mcu_pin_number(powerdown_pin) : NO_PIN;
8286
self->camera_config.pin_reset = reset_pin ? common_hal_mcu_pin_number(reset_pin) : NO_PIN;
8387
self->camera_config.pin_xclk = external_clock_pin ? common_hal_mcu_pin_number(external_clock_pin) : NO_PIN;

ports/mimxrt10xx/common-hal/busio/I2C.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ static void i2c_check_pin_config(const mcu_pin_obj_t *pin, uint32_t pull) {
6666
void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
6767
const mcu_pin_obj_t *scl, const mcu_pin_obj_t *sda, uint32_t frequency, uint32_t timeout) {
6868

69+
// Ensure the object starts in its deinit state.
70+
common_hal_busio_i2c_mark_deinit(self);
71+
6972
#if CIRCUITPY_REQUIRE_I2C_PULLUPS
7073
// Test that the pins are in a high state. (Hopefully indicating they are pulled up.)
7174
IOMUXC_SetPinMux(sda->mux_reg, IOMUXC_SW_MUX_CTL_PAD_MUX_MODE_ALT5, 0, 0, 0, 0);
@@ -162,8 +165,6 @@ void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) {
162165
common_hal_reset_pin(self->sda->pin);
163166
common_hal_reset_pin(self->scl->pin);
164167

165-
self->sda = NULL;
166-
self->scl = NULL;
167168
common_hal_busio_i2c_mark_deinit(self);
168169
}
169170

@@ -179,6 +180,9 @@ bool common_hal_busio_i2c_probe(busio_i2c_obj_t *self, uint8_t addr) {
179180
}
180181

181182
bool common_hal_busio_i2c_try_lock(busio_i2c_obj_t *self) {
183+
if (common_hal_busio_i2c_deinited(self)) {
184+
return false;
185+
}
182186
bool grabbed_lock = false;
183187
// CRITICAL_SECTION_ENTER()
184188
if (!self->has_lock) {

ports/nordic/common-hal/busio/I2C.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ static void twim_event_handler(nrfx_twim_evt_t const *p_event, void *p_context)
7474
}
7575

7676
void common_hal_busio_i2c_construct(busio_i2c_obj_t *self, const mcu_pin_obj_t *scl, const mcu_pin_obj_t *sda, uint32_t frequency, uint32_t timeout) {
77+
78+
// Ensure the object starts in its deinit state.
79+
common_hal_busio_i2c_mark_deinit(self);
80+
7781
if (scl->number == sda->number) {
7882
raise_ValueError_invalid_pins();
7983
}
@@ -155,8 +159,6 @@ void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) {
155159

156160
reset_pin_number(self->sda_pin_number);
157161
reset_pin_number(self->scl_pin_number);
158-
self->sda_pin_number = NO_PIN;
159-
self->scl_pin_number = NO_PIN;
160162

161163
self->twim_peripheral->in_use = false;
162164
common_hal_busio_i2c_mark_deinit(self);

ports/raspberrypi/common-hal/busio/I2C.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ static i2c_inst_t *i2c[2] = {i2c0, i2c1};
2626

2727
void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
2828
const mcu_pin_obj_t *scl, const mcu_pin_obj_t *sda, uint32_t frequency, uint32_t timeout) {
29+
30+
// Ensure object starts in its deinit state.
31+
common_hal_busio_i2c_mark_deinit(self);
32+
2933
self->peripheral = NULL;
3034
// I2C pins have a regular pattern. SCL is always odd and SDA is even. They match up in pairs
3135
// so we can divide by two to get the instance. This pattern repeats.
@@ -109,8 +113,6 @@ void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) {
109113

110114
reset_pin_number(self->sda_pin);
111115
reset_pin_number(self->scl_pin);
112-
self->sda_pin = NO_PIN;
113-
self->scl_pin = NO_PIN;
114116
common_hal_busio_i2c_mark_deinit(self);
115117
}
116118

ports/silabs/common-hal/busio/I2C.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
3939
const mcu_pin_obj_t *sda,
4040
uint32_t frequency, uint32_t timeout) {
4141

42+
// Ensure the object starts in its deinit state.
43+
common_hal_busio_i2c_mark_deinit(self);
44+
4245
if ((scl != NULL) && (sda != NULL)) {
4346
if (scl->function_list[ DEFAULT_I2C_PERIPHERAL == I2C1?
4447
FN_I2C1_SCL : FN_I2C0_SCL] == 1 &&
@@ -88,8 +91,6 @@ void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) {
8891
I2C_Reset(self->i2cspm);
8992
common_hal_reset_pin(self->sda);
9093
common_hal_reset_pin(self->scl);
91-
self->sda = NULL;
92-
self->scl = NULL;
9394
self->i2cspm = NULL;
9495
in_used = false;
9596
common_hal_busio_i2c_mark_deinit(self);

ports/stm/common-hal/busio/I2C.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ static void i2c_assign_irq(busio_i2c_obj_t *self, I2C_TypeDef *I2Cx);
5050
void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
5151
const mcu_pin_obj_t *scl, const mcu_pin_obj_t *sda, uint32_t frequency, uint32_t timeout) {
5252

53+
// Ensure the object starts in its deinit state.
54+
common_hal_busio_i2c_mark_deinit(self);
55+
5356
// Match pins to I2C objects
5457
I2C_TypeDef *I2Cx;
5558
uint8_t sda_len = MP_ARRAY_SIZE(mcu_i2c_sda_list);
@@ -168,8 +171,6 @@ void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) {
168171

169172
reset_pin_number(self->sda->pin->port, self->sda->pin->number);
170173
reset_pin_number(self->scl->pin->port, self->scl->pin->number);
171-
self->sda = NULL;
172-
self->scl = NULL;
173174
common_hal_busio_i2c_mark_deinit(self);
174175
}
175176

0 commit comments

Comments
 (0)