Skip to content

Commit 294d563

Browse files
authored
Merge pull request #8928 from tannewt/imx_fixes
Numerous iMX fixes and SDK update
2 parents f2bfb15 + 4d1f558 commit 294d563

File tree

12 files changed

+53
-88
lines changed

12 files changed

+53
-88
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
138138
continue;
139139
}
140140

141+
if (reserved_i2c[mcu_i2c_scl_list[j].bank_idx - 1]) {
142+
continue;
143+
}
144+
141145
self->sda = &mcu_i2c_sda_list[i];
142146
self->scl = &mcu_i2c_scl_list[j];
143147

@@ -151,6 +155,9 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
151155
self->i2c = mcu_i2c_banks[self->sda->bank_idx - 1];
152156
}
153157

158+
159+
reserved_i2c[self->sda->bank_idx - 1] = true;
160+
154161
config_periph_pin(self->sda);
155162
config_periph_pin(self->scl);
156163

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525
* THE SOFTWARE.
2626
*/
2727

28-
#ifndef MICROPY_INCLUDED_MIMXRT10XX_COMMON_HAL_BUSIO_I2C_H
29-
#define MICROPY_INCLUDED_MIMXRT10XX_COMMON_HAL_BUSIO_I2C_H
28+
#pragma once
3029

3130
#include "common-hal/microcontroller/Pin.h"
3231
#include "fsl_common.h"
@@ -42,5 +41,3 @@ typedef struct {
4241
} busio_i2c_obj_t;
4342

4443
void i2c_reset(void);
45-
46-
#endif // MICROPY_INCLUDED_MIMXRT10XX_COMMON_HAL_BUSIO_I2C_H

ports/mimxrt10xx/common-hal/busio/SPI.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525
* THE SOFTWARE.
2626
*/
2727

28-
#ifndef MICROPY_INCLUDED_MIMXRT10XX_COMMON_HAL_BUSIO_SPI_H
29-
#define MICROPY_INCLUDED_MIMXRT10XX_COMMON_HAL_BUSIO_SPI_H
28+
#pragma once
3029

3130
#include "common-hal/microcontroller/Pin.h"
3231
#include "fsl_common.h"
@@ -44,5 +43,3 @@ typedef struct {
4443
} busio_spi_obj_t;
4544

4645
void spi_reset(void);
47-
48-
#endif // MICROPY_INCLUDED_MIMXRT10XX_COMMON_HAL_BUSIO_SPI_H

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

Lines changed: 28 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -83,24 +83,6 @@ static void config_periph_pin(const mcu_periph_obj_t *periph) {
8383
| IOMUXC_SW_PAD_CTL_PAD_SRE(0));
8484
}
8585

86-
STATIC void LPUART_UserCallback(LPUART_Type *base, lpuart_handle_t *handle, status_t status, void *user_data) {
87-
busio_uart_obj_t *self = (busio_uart_obj_t *)user_data;
88-
89-
if (status == kStatus_LPUART_RxIdle) {
90-
self->rx_ongoing = false;
91-
}
92-
}
93-
94-
void uart_reset(void) {
95-
for (uint i = 0; i < MP_ARRAY_SIZE(mcu_uart_banks); i++) {
96-
if (never_reset_uart[i]) {
97-
continue;
98-
}
99-
reserved_uart[i] = false;
100-
LPUART_Deinit(mcu_uart_banks[i]);
101-
}
102-
}
103-
10486
void common_hal_busio_uart_never_reset(busio_uart_obj_t *self) {
10587
never_reset_uart[self->index] = true;
10688
common_hal_never_reset_pin(self->tx);
@@ -348,6 +330,8 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
348330

349331
if (self->rx != NULL) {
350332
if (receiver_buffer == NULL) {
333+
// One byte is used internally so add one to make sure we have enough space.
334+
receiver_buffer_size += 1;
351335
self->ringbuf = gc_alloc(receiver_buffer_size, false);
352336
} else {
353337
self->ringbuf = receiver_buffer;
@@ -359,7 +343,8 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
359343
m_malloc_fail(receiver_buffer_size);
360344
}
361345

362-
LPUART_TransferCreateHandle(self->uart, &self->handle, LPUART_UserCallback, self);
346+
// Use the internal ring buffer implementation.
347+
LPUART_TransferCreateHandle(self->uart, &self->handle, NULL, NULL);
363348
// Pass actual allocated size; the LPUART routines are cognizant that
364349
// the capacity is one less than the size.
365350
LPUART_TransferStartRingBuffer(self->uart, &self->handle, self->ringbuf, receiver_buffer_size);
@@ -378,12 +363,13 @@ void common_hal_busio_uart_deinit(busio_uart_obj_t *self) {
378363
return;
379364
}
380365

381-
reserved_uart[self->index] = false;
382-
never_reset_uart[self->index] = false;
383-
366+
LPUART_TransferStopRingBuffer(self->uart, &self->handle);
384367
LPUART_Deinit(self->uart);
385368
gc_free(self->ringbuf);
386369

370+
reserved_uart[self->index] = false;
371+
never_reset_uart[self->index] = false;
372+
387373
common_hal_reset_pin(self->rx);
388374
common_hal_reset_pin(self->tx);
389375
common_hal_reset_pin(self->cts);
@@ -395,7 +381,6 @@ void common_hal_busio_uart_deinit(busio_uart_obj_t *self) {
395381
self->cts = NULL;
396382
self->rts = NULL;
397383
self->rs485_dir = NULL;
398-
399384
}
400385

401386
// Read characters.
@@ -409,18 +394,24 @@ size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t
409394
return 0;
410395
}
411396

412-
lpuart_transfer_t xfer = {
413-
.data = data,
414-
.dataSize = len,
415-
};
416-
417-
self->rx_ongoing = true;
418-
LPUART_TransferReceiveNonBlocking(self->uart, &self->handle, &xfer, NULL);
419-
397+
size_t received = 0;
420398
uint64_t start_ticks = supervisor_ticks_ms64();
421-
422399
// Wait for all bytes received or timeout
423-
while (self->rx_ongoing && (supervisor_ticks_ms64() - start_ticks < self->timeout_ms)) {
400+
while (received < len && (supervisor_ticks_ms64() - start_ticks < self->timeout_ms)) {
401+
lpuart_transfer_t xfer = {
402+
.data = data + received
403+
};
404+
size_t remaining = len - received;
405+
xfer.dataSize = MIN(remaining, LPUART_TransferGetRxRingBufferLength(self->uart, &self->handle));
406+
// Only request as much as has already been received. Otherwise, we need to deal with
407+
// callbacks.
408+
size_t additional_read = 0;
409+
LPUART_TransferReceiveNonBlocking(self->uart, &self->handle, &xfer, &additional_read);
410+
received += additional_read;
411+
// Break early when we're done to skip background tasks.
412+
if (received == len) {
413+
break;
414+
}
424415
RUN_BACKGROUND_TASKS;
425416

426417
// Allow user to break out of a timeout with a KeyboardInterrupt.
@@ -429,30 +420,12 @@ size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t
429420
}
430421
}
431422

432-
// if we timed out, stop the transfer
433-
if (self->rx_ongoing) {
434-
uint32_t recvd = 0;
435-
LPUART_TransferGetReceiveCount(self->uart, &self->handle, &recvd);
436-
LPUART_TransferAbortReceive(self->uart, &self->handle);
437-
if (recvd == 0) {
438-
*errcode = EAGAIN;
439-
return MP_STREAM_ERROR;
440-
}
441-
return recvd;
442-
}
443-
444-
// No data left, we got it all
445-
if (self->handle.rxData == NULL) {
446-
return len;
423+
if (received == 0) {
424+
*errcode = EAGAIN;
425+
return MP_STREAM_ERROR;
447426
}
448427

449-
// The only place we can reliably tell how many bytes have been received is from the current
450-
// wp in the handle (because the abort nukes rxDataSize, and reading it before abort is a race.)
451-
if (self->handle.rxData > data) {
452-
return self->handle.rxData - data;
453-
} else {
454-
return len;
455-
}
428+
return received;
456429
}
457430

458431
// Write characters.

ports/mimxrt10xx/common-hal/busio/UART.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525
* THE SOFTWARE.
2626
*/
2727

28-
#ifndef MICROPY_INCLUDED_MIMXRT10XX_COMMON_HAL_BUSIO_UART_H
29-
#define MICROPY_INCLUDED_MIMXRT10XX_COMMON_HAL_BUSIO_UART_H
28+
#pragma once
3029

3130
#include "common-hal/microcontroller/Pin.h"
3231

@@ -43,7 +42,6 @@ typedef struct {
4342
uint8_t *ringbuf;
4443
uint32_t baudrate;
4544
uint32_t timeout_ms;
46-
bool rx_ongoing;
4745
uint8_t character_bits;
4846
uint8_t index;
4947
const mcu_pin_obj_t *rx;
@@ -54,6 +52,3 @@ typedef struct {
5452
bool rs485_invert;
5553

5654
} busio_uart_obj_t;
57-
58-
void uart_reset(void);
59-
#endif // MICROPY_INCLUDED_MIMXRT10XX_COMMON_HAL_BUSIO_UART_H

ports/mimxrt10xx/common-hal/digitalio/DigitalInOut.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525
* THE SOFTWARE.
2626
*/
2727

28-
#ifndef MICROPY_INCLUDED_MIMXRT10XX_COMMON_HAL_DIGITALIO_DIGITALINOUT_H
29-
#define MICROPY_INCLUDED_MIMXRT10XX_COMMON_HAL_DIGITALIO_DIGITALINOUT_H
28+
#pragma once
3029

3130
#include "common-hal/microcontroller/Pin.h"
3231
#include "shared-bindings/digitalio/Pull.h"
@@ -41,5 +40,3 @@ typedef struct {
4140
} digitalio_digitalinout_obj_t;
4241

4342
void pin_config(const mcu_pin_obj_t *pin, bool open_drain, digitalio_pull_t pull);
44-
45-
#endif // MICROPY_INCLUDED_MIMXRT10XX_COMMON_HAL_DIGITALIO_DIGITALINOUT_H

ports/mimxrt10xx/common-hal/microcontroller/Pin.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
#include "shared-bindings/microcontroller/Pin.h"
3030
#include "shared-bindings/microcontroller/__init__.h"
3131

32+
#include "sdk/drivers/igpio/fsl_gpio.h"
33+
3234
#include "py/gc.h"
3335

3436
STATIC bool claimed_pins[PAD_COUNT];
@@ -91,6 +93,11 @@ void common_hal_reset_pin(const mcu_pin_obj_t *pin) {
9193
never_reset_pins[pin->mux_idx] = false;
9294
claimed_pins[pin->mux_idx] = false;
9395

96+
// Make sure this pin's GPIO is set to input. Otherwise, output values could interfere
97+
// with the ADC.
98+
const gpio_pin_config_t config = { kGPIO_DigitalInput, 0, kGPIO_NoIntmode };
99+
GPIO_PinInit(pin->gpio, pin->number, &config);
100+
94101
// This should never be true, but protect against it anyway.
95102
if (pin->mux_reg == 0) {
96103
return;

ports/mimxrt10xx/common-hal/pwmio/PWMOut.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,6 @@ pwmout_result_t common_hal_pwmio_pwmout_construct(pwmio_pwmout_obj_t *self,
213213

214214
/*
215215
* pwmConfig.enableDebugMode = false;
216-
* pwmConfig.enableWait = false;
217216
* pwmConfig.reloadSelect = kPWM_LocalReload;
218217
* pwmConfig.faultFilterCount = 0;
219218
* pwmConfig.faultFilterPeriod = 0;
@@ -228,7 +227,6 @@ pwmout_result_t common_hal_pwmio_pwmout_construct(pwmio_pwmout_obj_t *self,
228227
PWM_GetDefaultConfig(&pwmConfig);
229228

230229
pwmConfig.reloadLogic = kPWM_ReloadPwmFullCycle;
231-
pwmConfig.enableWait = true;
232230
pwmConfig.enableDebugMode = true;
233231

234232
pwmConfig.prescale = self->prescaler;

ports/mimxrt10xx/sdk

Submodule sdk updated 4267 files

ports/mimxrt10xx/supervisor/port.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "common-hal/microcontroller/Pin.h"
4444
#include "common-hal/pwmio/PWMOut.h"
4545
#include "common-hal/rtc/RTC.h"
46+
#include "common-hal/busio/I2C.h"
4647
#include "common-hal/busio/SPI.h"
4748
#include "shared-bindings/microcontroller/__init__.h"
4849

@@ -447,6 +448,7 @@ safe_mode_t port_init(void) {
447448

448449
void reset_port(void) {
449450
#if CIRCUITPY_BUSIO
451+
i2c_reset();
450452
spi_reset();
451453
#endif
452454

0 commit comments

Comments
 (0)