Skip to content

Commit b0dd0e3

Browse files
authored
Merge pull request #4772 from EmergReanimator/atmel-samd
[resolves #4771] DEBUG UART supported on ATMSAME5x
2 parents a20fbd1 + 7805392 commit b0dd0e3

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

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

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545

4646
#include "samd/sercom.h"
4747

48+
#include "common-hal/busio/SPI.h" // for never_reset_sercom
49+
4850
#define UART_DEBUG(...) (void)0
4951
// #define UART_DEBUG(...) mp_printf(&mp_plat_print __VA_OPT__(,) __VA_ARGS__)
5052

@@ -152,16 +154,22 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
152154

153155
if (rx && receiver_buffer_size > 0) {
154156
self->buffer_length = receiver_buffer_size;
155-
// Initially allocate the UART's buffer in the long-lived part of the
156-
// heap. UARTs are generally long-lived objects, but the "make long-
157-
// lived" machinery is incapable of moving internal pointers like
158-
// self->buffer, so do it manually. (However, as long as internal
159-
// pointers like this are NOT moved, allocating the buffer
160-
// in the long-lived pool is not strictly necessary)
161-
self->buffer = (uint8_t *)gc_alloc(self->buffer_length * sizeof(uint8_t), false, true);
162-
if (self->buffer == NULL) {
163-
common_hal_busio_uart_deinit(self);
164-
mp_raise_msg_varg(&mp_type_MemoryError, translate("Failed to allocate RX buffer of %d bytes"), self->buffer_length * sizeof(uint8_t));
157+
if (NULL != receiver_buffer) {
158+
self->buffer = receiver_buffer;
159+
}
160+
else {
161+
// Initially allocate the UART's buffer in the long-lived part of the
162+
// heap. UARTs are generally long-lived objects, but the "make long-
163+
// lived" machinery is incapable of moving internal pointers like
164+
// self->buffer, so do it manually. (However, as long as internal
165+
// pointers like this are NOT moved, allocating the buffer
166+
// in the long-lived pool is not strictly necessary)
167+
168+
self->buffer = (uint8_t *)gc_alloc(self->buffer_length * sizeof(uint8_t), false, true);
169+
if (self->buffer == NULL) {
170+
common_hal_busio_uart_deinit(self);
171+
mp_raise_msg_varg(&mp_type_MemoryError, translate("Failed to allocate RX buffer of %d bytes"), self->buffer_length * sizeof(uint8_t));
172+
}
165173
}
166174
} else {
167175
self->buffer_length = 0;
@@ -246,6 +254,21 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
246254
usart_async_enable(usart_desc_p);
247255
}
248256

257+
void common_hal_busio_uart_never_reset(busio_uart_obj_t *self) {
258+
for (size_t i = 0; i < MP_ARRAY_SIZE(sercom_insts); i++) {
259+
const Sercom *sercom = sercom_insts[i];
260+
Sercom *hw = (Sercom *)(self->usart_desc.device.hw);
261+
262+
// Reserve pins for active UART only
263+
if (sercom == hw) {
264+
never_reset_sercom(hw);
265+
never_reset_pin_number(self->rx_pin);
266+
never_reset_pin_number(self->tx_pin);
267+
}
268+
}
269+
return;
270+
}
271+
249272
bool common_hal_busio_uart_deinited(busio_uart_obj_t *self) {
250273
return self->rx_pin == NO_PIN && self->tx_pin == NO_PIN;
251274
}

supervisor/shared/serial.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void serial_early_init(void) {
6767
const mcu_pin_obj_t *tx = MP_OBJ_TO_PTR(DEBUG_UART_TX);
6868

6969
common_hal_busio_uart_construct(&debug_uart, tx, rx, NULL, NULL, NULL,
70-
false, 115200, 8, UART_PARITY_NONE, 1, 1.0f, 64,
70+
false, 115200, 8, BUSIO_UART_PARITY_NONE, 1, 1.0f, 64,
7171
buf_array, true);
7272
common_hal_busio_uart_never_reset(&debug_uart);
7373
#endif

0 commit comments

Comments
 (0)