Skip to content

Commit 5028804

Browse files
committed
Remove fixed pointers and check UART return
1 parent 69b3d47 commit 5028804

File tree

4 files changed

+12
-8
lines changed

4 files changed

+12
-8
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,10 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
9090
#endif
9191

9292

93-
self->semaphore_handle = xSemaphoreCreateBinaryStatic(&self->semaphore);
94-
xSemaphoreGive(self->semaphore_handle);
93+
if (xSemaphoreCreateBinaryStatic(&self->semaphore) != &self->semaphore) {
94+
mp_raise_RuntimeError(translate("Unable to create lock"));
95+
}
96+
xSemaphoreGive(&self->semaphore);
9597
self->sda_pin = sda;
9698
self->scl_pin = scl;
9799
self->i2c_num = I2C_NUM_MAX;
@@ -161,7 +163,7 @@ bool common_hal_busio_i2c_probe(busio_i2c_obj_t *self, uint8_t addr) {
161163
}
162164

163165
bool common_hal_busio_i2c_try_lock(busio_i2c_obj_t *self) {
164-
self->has_lock = xSemaphoreTake(self->semaphore_handle, 0) == pdTRUE;
166+
self->has_lock = xSemaphoreTake(&self->semaphore, 0) == pdTRUE;
165167
return self->has_lock;
166168
}
167169

@@ -170,7 +172,7 @@ bool common_hal_busio_i2c_has_lock(busio_i2c_obj_t *self) {
170172
}
171173

172174
void common_hal_busio_i2c_unlock(busio_i2c_obj_t *self) {
173-
xSemaphoreGive(self->semaphore_handle);
175+
xSemaphoreGive(&self->semaphore);
174176
self->has_lock = false;
175177
}
176178

ports/esp32s2/common-hal/busio/I2C.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ typedef struct {
4040
const mcu_pin_obj_t* sda_pin;
4141
i2c_port_t i2c_num;
4242
StaticSemaphore_t semaphore;
43-
SemaphoreHandle_t semaphore_handle;
4443
bool has_lock;
4544
} busio_i2c_obj_t;
4645

ports/esp32s2/common-hal/busio/SPI.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,6 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
209209

210210
hal->io_mode = SPI_LL_IO_MODE_NORMAL;
211211

212-
// This must be set after spi_hal_init.
213-
hal->timing_conf = &self->timing_conf;
214-
215212
common_hal_busio_spi_configure(self, 250000, 0, 0, 8);
216213
}
217214

@@ -261,6 +258,7 @@ bool common_hal_busio_spi_configure(busio_spi_obj_t *self,
261258
self->phase = phase;
262259
self->bits = bits;
263260
self->target_frequency = baudrate;
261+
self->hal_context.timing_conf = &self->timing_conf;
264262
esp_err_t result = spi_hal_get_clock_conf(&self->hal_context,
265263
self->target_frequency,
266264
128 /* duty_cycle */,
@@ -315,6 +313,8 @@ bool common_hal_busio_spi_transfer(busio_spi_obj_t *self, const uint8_t *data_ou
315313
spi_hal_context_t* hal = &self->hal_context;
316314
hal->send_buffer = NULL;
317315
hal->rcv_buffer = NULL;
316+
// Reset timing_conf in case we've moved since the last time we used it.
317+
hal->timing_conf = &self->timing_conf;
318318
// This rounds up.
319319
size_t dma_count = (len + LLDESC_MAX_NUM_PER_DESC - 1) / LLDESC_MAX_NUM_PER_DESC;
320320
for (size_t i = 0; i < dma_count; i++) {

ports/esp32s2/common-hal/busio/UART.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,9 @@ size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t
249249
while (supervisor_ticks_ms64() - start_ticks <= self->timeout_ms) {
250250
// Read as many chars as we can right now, up to len.
251251
size_t num_read = uart_read_bytes(self->uart_num, data, len, 0);
252+
if (num_read < 0) {
253+
break;
254+
}
252255

253256
// Advance pointer in data buffer, and decrease how many chars left to read.
254257
data += num_read;

0 commit comments

Comments
 (0)