Merge pull request #4772 from EmergReanimator/atmel-samd

[resolves #4771] DEBUG UART supported on ATMSAME5x
This commit is contained in:
Jeff Epler 2021-05-28 20:05:06 -06:00 committed by GitHub
commit b0dd0e344c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 11 deletions

View File

@ -45,6 +45,8 @@
#include "samd/sercom.h" #include "samd/sercom.h"
#include "common-hal/busio/SPI.h" // for never_reset_sercom
#define UART_DEBUG(...) (void)0 #define UART_DEBUG(...) (void)0
// #define UART_DEBUG(...) mp_printf(&mp_plat_print __VA_OPT__(,) __VA_ARGS__) // #define UART_DEBUG(...) mp_printf(&mp_plat_print __VA_OPT__(,) __VA_ARGS__)
@ -152,17 +154,23 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
if (rx && receiver_buffer_size > 0) { if (rx && receiver_buffer_size > 0) {
self->buffer_length = receiver_buffer_size; self->buffer_length = receiver_buffer_size;
if (NULL != receiver_buffer) {
self->buffer = receiver_buffer;
}
else {
// Initially allocate the UART's buffer in the long-lived part of the // Initially allocate the UART's buffer in the long-lived part of the
// heap. UARTs are generally long-lived objects, but the "make long- // heap. UARTs are generally long-lived objects, but the "make long-
// lived" machinery is incapable of moving internal pointers like // lived" machinery is incapable of moving internal pointers like
// self->buffer, so do it manually. (However, as long as internal // self->buffer, so do it manually. (However, as long as internal
// pointers like this are NOT moved, allocating the buffer // pointers like this are NOT moved, allocating the buffer
// in the long-lived pool is not strictly necessary) // in the long-lived pool is not strictly necessary)
self->buffer = (uint8_t *)gc_alloc(self->buffer_length * sizeof(uint8_t), false, true); self->buffer = (uint8_t *)gc_alloc(self->buffer_length * sizeof(uint8_t), false, true);
if (self->buffer == NULL) { if (self->buffer == NULL) {
common_hal_busio_uart_deinit(self); common_hal_busio_uart_deinit(self);
mp_raise_msg_varg(&mp_type_MemoryError, translate("Failed to allocate RX buffer of %d bytes"), self->buffer_length * sizeof(uint8_t)); mp_raise_msg_varg(&mp_type_MemoryError, translate("Failed to allocate RX buffer of %d bytes"), self->buffer_length * sizeof(uint8_t));
} }
}
} else { } else {
self->buffer_length = 0; self->buffer_length = 0;
self->buffer = NULL; self->buffer = NULL;
@ -246,6 +254,21 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
usart_async_enable(usart_desc_p); usart_async_enable(usart_desc_p);
} }
void common_hal_busio_uart_never_reset(busio_uart_obj_t *self) {
for (size_t i = 0; i < MP_ARRAY_SIZE(sercom_insts); i++) {
const Sercom *sercom = sercom_insts[i];
Sercom *hw = (Sercom *)(self->usart_desc.device.hw);
// Reserve pins for active UART only
if (sercom == hw) {
never_reset_sercom(hw);
never_reset_pin_number(self->rx_pin);
never_reset_pin_number(self->tx_pin);
}
}
return;
}
bool common_hal_busio_uart_deinited(busio_uart_obj_t *self) { bool common_hal_busio_uart_deinited(busio_uart_obj_t *self) {
return self->rx_pin == NO_PIN && self->tx_pin == NO_PIN; return self->rx_pin == NO_PIN && self->tx_pin == NO_PIN;
} }

View File

@ -67,7 +67,7 @@ void serial_early_init(void) {
const mcu_pin_obj_t *tx = MP_OBJ_TO_PTR(DEBUG_UART_TX); const mcu_pin_obj_t *tx = MP_OBJ_TO_PTR(DEBUG_UART_TX);
common_hal_busio_uart_construct(&debug_uart, tx, rx, NULL, NULL, NULL, common_hal_busio_uart_construct(&debug_uart, tx, rx, NULL, NULL, NULL,
false, 115200, 8, UART_PARITY_NONE, 1, 1.0f, 64, false, 115200, 8, BUSIO_UART_PARITY_NONE, 1, 1.0f, 64,
buf_array, true); buf_array, true);
common_hal_busio_uart_never_reset(&debug_uart); common_hal_busio_uart_never_reset(&debug_uart);
#endif #endif