[realtek-ambz2] Reuse Log UART for Serial

This commit is contained in:
Kuba Szczodrzyński
2023-05-31 12:37:34 +02:00
parent 1e49c3ff6f
commit e7f35c584b
4 changed files with 30 additions and 16 deletions

View File

@@ -68,9 +68,10 @@ void SerialClass::end() {
break;
}
delete DATA;
this->data = NULL;
this->buf = NULL;
this->baudrate = 0;
delete DATA;
}
void SerialClass::flush() {

View File

@@ -114,9 +114,10 @@ void SerialClass::end() {
DIAG_UartReInit((IRQ_FUN)UartLogIrqHandle);
}
delete DATA;
this->data = NULL;
this->buf = NULL;
this->baudrate = 0;
delete DATA;
}
void SerialClass::flush() {

View File

@@ -15,7 +15,7 @@ SerialClass Serial2(2, PIN_SERIAL2_RX, PIN_SERIAL2_TX);
static void callback(uint32_t param, uint32_t event) {
if (event != RxIrq)
return;
hal_uart_adapter_t *uart = &pdUART;
hal_uart_adapter_t *uart = pdUART;
uint8_t c;
while (hal_uart_rgetc(uart, (char *)&c)) {
@@ -33,14 +33,19 @@ void SerialClass::begin(unsigned long baudrate, uint16_t config) {
this->data = new SerialData();
this->buf = &BUF;
// TODO handle PIN_INVALID
hal_uart_init(&UART, this->tx, this->rx, NULL);
if (this->port == 2) {
DATA->uart = &log_uart;
} else {
UART = new hal_uart_adapter_t();
// TODO handle PIN_INVALID
hal_uart_init(UART, this->tx, this->rx, NULL);
}
if (this->rx != PIN_INVALID) {
hal_uart_enter_critical();
hal_uart_rxind_hook(&UART, callback, (uint32_t)this->data, RxIrq);
UART.base_addr->ier_b.erbi = 1;
UART.base_addr->ier_b.etbei = 0;
hal_uart_rxind_hook(UART, callback, (uint32_t)this->data, RxIrq);
UART->base_addr->ier_b.erbi = 1;
UART->base_addr->ier_b.etbei = 0;
hal_uart_exit_critical();
}
}
@@ -57,8 +62,8 @@ void SerialClass::configure(unsigned long baudrate, uint16_t config) {
uint8_t parity = (config & SERIAL_PARITY_MASK) ^ 0b11;
uint8_t stopBits = (config & SERIAL_STOP_BIT_MASK) == SERIAL_STOP_BIT_2 ? 2 : 1;
hal_uart_set_baudrate(&UART, baudrate);
hal_uart_set_format(&UART, dataWidth, parity, stopBits);
hal_uart_set_baudrate(UART, baudrate);
hal_uart_set_format(UART, dataWidth, parity, stopBits);
this->baudrate = baudrate;
this->config = config;
@@ -68,23 +73,30 @@ void SerialClass::end() {
if (!this->data)
return;
hal_uart_deinit(&UART);
if (this->port == 2) {
UART->base_addr->ier_b.erbi = 0;
hal_uart_rxind_hook(UART, NULL, 0, RxIrq);
} else {
hal_uart_deinit(UART);
delete UART;
}
delete DATA;
this->data = NULL;
this->buf = NULL;
this->baudrate = 0;
delete DATA;
}
void SerialClass::flush() {
if (!this->data)
return;
while (UART.base_addr->tflvr_b.tx_fifo_lv != 0) {}
while (UART->base_addr->tflvr_b.tx_fifo_lv != 0) {}
}
size_t SerialClass::write(uint8_t c) {
if (!this->data)
return 0;
while (!hal_uart_writeable(&UART)) {}
hal_uart_putc(&UART, c);
while (!hal_uart_writeable(UART)) {}
hal_uart_putc(UART, c);
return 1;
}

View File

@@ -6,7 +6,7 @@
#include <sdk_private.h>
typedef struct {
hal_uart_adapter_t uart;
hal_uart_adapter_t *uart;
RingBuffer buf;
} SerialData;