[realtek-ambz] Refactor Serial class
This commit is contained in:
@@ -14,19 +14,21 @@
|
||||
// Include board variant
|
||||
#include "variant.h"
|
||||
|
||||
// Define available serial ports
|
||||
#ifdef __cplusplus
|
||||
#include "SerialClass.h"
|
||||
#ifdef HAS_SERIAL_CLASS
|
||||
// declare instances of available Serial* components
|
||||
// map Serial to Serial2 if available, else to Serial1
|
||||
#ifdef PIN_SERIAL1_RX
|
||||
#ifdef HAS_SERIAL_CLASS // failsafe for circular inclusion
|
||||
|
||||
#ifdef PIN_SERIAL1_TX
|
||||
extern SerialClass Serial1;
|
||||
#endif
|
||||
#ifdef PIN_SERIAL2_RX
|
||||
|
||||
#ifdef PIN_SERIAL2_TX
|
||||
extern SerialClass Serial2;
|
||||
#define Serial Serial2
|
||||
#else
|
||||
#define Serial Serial1
|
||||
#endif
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@@ -3,17 +3,19 @@
|
||||
#include "SerialClass.h"
|
||||
|
||||
extern "C" {
|
||||
|
||||
#include <uart_pub.h>
|
||||
|
||||
extern void bk_send_byte(uint8_t uport, uint8_t data);
|
||||
extern void uart_hw_set_change(uint8_t uport, bk_uart_config_t *uart_config);
|
||||
extern int uart_rx_callback_set(int uport, uart_callback callback, void *param);
|
||||
}
|
||||
|
||||
#ifdef PIN_SERIAL1_RX
|
||||
} // extern "C"
|
||||
|
||||
#ifdef PIN_SERIAL1_TX
|
||||
SerialClass Serial1(UART1_PORT);
|
||||
#endif
|
||||
#ifdef PIN_SERIAL2_RX
|
||||
#ifdef PIN_SERIAL2_TX
|
||||
SerialClass Serial2(UART2_PORT);
|
||||
#endif
|
||||
|
||||
@@ -39,11 +41,13 @@ void SerialClass::begin(unsigned long baudrate, uint16_t config) {
|
||||
.stop_bits = (uart_stop_bits_t)stopBits,
|
||||
.flow_control = FLOW_CTRL_DISABLED,
|
||||
};
|
||||
|
||||
if (this->buf) {
|
||||
this->buf->clear();
|
||||
} else {
|
||||
this->buf = new RingBuffer();
|
||||
}
|
||||
|
||||
uart_hw_set_change(port, &cfg);
|
||||
uart_rx_callback_set(port, callback, this->buf);
|
||||
}
|
||||
|
||||
@@ -16,7 +16,27 @@
|
||||
// Include board variant
|
||||
#include "variant.h"
|
||||
|
||||
// Define available serial ports
|
||||
#ifdef __cplusplus
|
||||
#include "LOGUARTClass.h"
|
||||
extern LOGUARTClass Serial;
|
||||
#include "SerialClass.h"
|
||||
#ifdef HAS_SERIAL_CLASS // failsafe for circular inclusion
|
||||
|
||||
#ifdef PIN_SERIAL0_TX
|
||||
extern SerialClass Serial0;
|
||||
#endif
|
||||
|
||||
#ifdef PIN_SERIAL1_TX
|
||||
extern SerialClass Serial1;
|
||||
#endif
|
||||
|
||||
#ifdef PIN_SERIAL2_RX
|
||||
extern SerialClass Serial2;
|
||||
#define Serial Serial2
|
||||
#elif PIN_SERIAL0_TX
|
||||
#define Serial Serial0
|
||||
#else
|
||||
#define Serial Serial1
|
||||
#endif
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@@ -1,98 +0,0 @@
|
||||
/*
|
||||
Copyright (c) 2011 Arduino. All right reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
See the GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#include "LOGUARTClass.h"
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#define LOG_UART_MODIFIABLE_BAUD_RATE 1
|
||||
|
||||
RingBuffer rx_buffer0;
|
||||
|
||||
LOGUARTClass::LOGUARTClass(int dwIrq, RingBuffer *pRx_buffer) {
|
||||
_rx_buffer = pRx_buffer;
|
||||
_dwIrq = dwIrq;
|
||||
}
|
||||
|
||||
void IrqHandler(void) {
|
||||
uint8_t data = 0;
|
||||
BOOL PullMode = _FALSE;
|
||||
uint32_t IrqEn = DiagGetIsrEnReg();
|
||||
|
||||
DiagSetIsrEnReg(0);
|
||||
|
||||
data = DiagGetChar(PullMode);
|
||||
if (data > 0)
|
||||
rx_buffer0.store_char(data);
|
||||
|
||||
DiagSetIsrEnReg(IrqEn);
|
||||
}
|
||||
|
||||
void LOGUARTClass::begin(const uint32_t dwBaudRate) {
|
||||
DIAG_UartReInit((IRQ_FUN)IrqHandler);
|
||||
NVIC_SetPriority(UART_LOG_IRQ, 10);
|
||||
LOGUART_SetBaud(dwBaudRate);
|
||||
}
|
||||
|
||||
void LOGUARTClass::end(void) {
|
||||
// clear any received data
|
||||
_rx_buffer->_iHead = _rx_buffer->_iTail;
|
||||
}
|
||||
|
||||
int LOGUARTClass::available(void) {
|
||||
return (uint32_t)(SERIAL_BUFFER_SIZE + _rx_buffer->_iHead - _rx_buffer->_iTail) % SERIAL_BUFFER_SIZE;
|
||||
}
|
||||
|
||||
int LOGUARTClass::peek(void) {
|
||||
|
||||
if (_rx_buffer->_iHead == _rx_buffer->_iTail)
|
||||
return -1;
|
||||
|
||||
return _rx_buffer->_aucBuffer[_rx_buffer->_iTail];
|
||||
}
|
||||
|
||||
int LOGUARTClass::read(void) {
|
||||
// if the head isn't ahead of the tail, we don't have any characters
|
||||
if (_rx_buffer->_iHead == _rx_buffer->_iTail)
|
||||
return -1;
|
||||
|
||||
uint8_t uc = _rx_buffer->_aucBuffer[_rx_buffer->_iTail];
|
||||
_rx_buffer->_iTail = (unsigned int)(_rx_buffer->_iTail + 1) % SERIAL_BUFFER_SIZE;
|
||||
return uc;
|
||||
}
|
||||
|
||||
void LOGUARTClass::flush(void) {
|
||||
// TODO:
|
||||
// while ( serial_writable(&(this->sobj)) != 1 );
|
||||
/*
|
||||
// Wait for transmission to complete
|
||||
while ((_pUart->UART_SR & UART_SR_TXRDY) != UART_SR_TXRDY)
|
||||
;
|
||||
*/
|
||||
}
|
||||
|
||||
size_t LOGUARTClass::write(const uint8_t uc_data) {
|
||||
DiagPutChar(uc_data);
|
||||
return 1;
|
||||
}
|
||||
|
||||
LOGUARTClass Serial(UART_LOG_IRQ, &rx_buffer0);
|
||||
|
||||
bool Serial_available() {
|
||||
return Serial.available() > 0;
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
/*
|
||||
Copyright (c) 2011 Arduino. All right reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
See the GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <api/HardwareSerial.h>
|
||||
#include <api/RingBuffer.h>
|
||||
|
||||
using namespace arduino;
|
||||
|
||||
// TODO this class begs to be rewritten :(
|
||||
|
||||
class LOGUARTClass : public HardwareSerial {
|
||||
public:
|
||||
LOGUARTClass(int dwIrq, RingBuffer *pRx_buffer);
|
||||
|
||||
void begin(const uint32_t dwBaudRate);
|
||||
|
||||
inline void begin(const uint32_t dwBaudRate, uint16_t config) {
|
||||
begin(dwBaudRate); // TODO implement this properly
|
||||
}
|
||||
|
||||
void end(void);
|
||||
int available(void);
|
||||
int peek(void);
|
||||
int read(void);
|
||||
void flush(void);
|
||||
size_t write(const uint8_t c);
|
||||
|
||||
using Print::write; // pull in write(str) and write(buf, size) from Print
|
||||
|
||||
operator bool() {
|
||||
return true; // UART always active
|
||||
}
|
||||
|
||||
protected:
|
||||
RingBuffer *_rx_buffer;
|
||||
|
||||
int _dwIrq;
|
||||
|
||||
private:
|
||||
friend bool Serial_available();
|
||||
};
|
||||
98
arduino/realtek-ambz/cores/arduino/SerialClass.cpp
Normal file
98
arduino/realtek-ambz/cores/arduino/SerialClass.cpp
Normal file
@@ -0,0 +1,98 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-07-03. */
|
||||
|
||||
#include "SerialClass.h"
|
||||
|
||||
#ifdef PIN_SERIAL0_TX
|
||||
SerialClass Serial0(UART0_DEV, UART0_IRQ, PIN_SERIAL0_RX, PIN_SERIAL0_TX);
|
||||
#endif
|
||||
#ifdef PIN_SERIAL1_TX
|
||||
SerialClass Serial1(UART1_DEV, UART1_IRQ, PIN_SERIAL1_RX, PIN_SERIAL1_TX);
|
||||
#endif
|
||||
#ifdef PIN_SERIAL2_TX
|
||||
SerialClass Serial2(UART2_DEV, UART_LOG_IRQ, PIN_SERIAL2_RX, PIN_SERIAL2_TX);
|
||||
#endif
|
||||
|
||||
SerialClass::SerialClass(UART_TypeDef *uart, IRQn irq, pin_size_t rx, pin_size_t tx) {
|
||||
data.uart = uart;
|
||||
data.buf = NULL;
|
||||
this->irq = irq;
|
||||
this->rx = rx;
|
||||
this->tx = tx;
|
||||
}
|
||||
|
||||
static uint32_t callback(void *param) {
|
||||
SerialData *data = (SerialData *)param;
|
||||
|
||||
uint32_t intcr = data->uart->DLH_INTCR;
|
||||
data->uart->DLH_INTCR = 0;
|
||||
|
||||
uint8_t c;
|
||||
UART_CharGet(data->uart, &c);
|
||||
if (c)
|
||||
data->buf->store_char(c);
|
||||
|
||||
data->uart->DLH_INTCR = intcr;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void SerialClass::begin(unsigned long baudrate, uint16_t config) {
|
||||
// RUART_WLS_7BITS / RUART_WLS_8BITS
|
||||
uint8_t dataWidth = (config & SERIAL_DATA_MASK) == SERIAL_DATA_8;
|
||||
// RUART_PARITY_DISABLE / RUART_PARITY_ENABLE
|
||||
uint8_t parity = (config & SERIAL_PARITY_MASK) != SERIAL_PARITY_NONE;
|
||||
// RUART_ODD_PARITY / RUART_EVEN_PARITY
|
||||
uint8_t parityType = (config & SERIAL_PARITY_MASK) == SERIAL_PARITY_EVEN;
|
||||
// RUART_STOP_BIT_1 / RUART_STOP_BIT_2
|
||||
uint8_t stopBits = (config & SERIAL_STOP_BIT_MASK) == SERIAL_STOP_BIT_2;
|
||||
|
||||
UART_InitTypeDef cfg;
|
||||
UART_StructInit(&cfg);
|
||||
cfg.WordLen = dataWidth;
|
||||
cfg.Parity = parity;
|
||||
cfg.ParityType = parityType;
|
||||
cfg.StopBit = stopBits;
|
||||
UART_Init(data.uart, &cfg);
|
||||
UART_SetBaud(data.uart, baudrate);
|
||||
|
||||
if (data.buf) {
|
||||
data.buf->clear();
|
||||
} else {
|
||||
data.buf = new RingBuffer();
|
||||
}
|
||||
|
||||
Pinmux_Config(pinInfo(this->rx)->gpio, PINMUX_FUNCTION_UART);
|
||||
Pinmux_Config(pinInfo(this->tx)->gpio, PINMUX_FUNCTION_UART);
|
||||
|
||||
VECTOR_IrqUnRegister(this->irq);
|
||||
VECTOR_IrqRegister(callback, this->irq, (uint32_t)&data, 10);
|
||||
}
|
||||
|
||||
void SerialClass::end() {
|
||||
if (data.uart == UART2_DEV) {
|
||||
// restore command line mode
|
||||
DIAG_UartReInit((IRQ_FUN)UartLogIrqHandle);
|
||||
}
|
||||
delete data.buf;
|
||||
}
|
||||
|
||||
int SerialClass::available() {
|
||||
return data.buf->available();
|
||||
}
|
||||
|
||||
int SerialClass::peek() {
|
||||
return data.buf->peek();
|
||||
}
|
||||
|
||||
int SerialClass::read() {
|
||||
return data.buf->read_char();
|
||||
}
|
||||
|
||||
void SerialClass::flush() {
|
||||
UART_WaitBusy(data.uart, 10);
|
||||
}
|
||||
|
||||
size_t SerialClass::write(uint8_t c) {
|
||||
while (UART_Writable(data.uart) == 0) {}
|
||||
UART_CharPut(data.uart, c);
|
||||
return 1;
|
||||
}
|
||||
44
arduino/realtek-ambz/cores/arduino/SerialClass.h
Normal file
44
arduino/realtek-ambz/cores/arduino/SerialClass.h
Normal file
@@ -0,0 +1,44 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-07-03. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <api/HardwareSerial.h>
|
||||
#include <api/RingBuffer.h>
|
||||
|
||||
using namespace arduino;
|
||||
|
||||
typedef struct {
|
||||
UART_TypeDef *uart;
|
||||
RingBuffer *buf;
|
||||
} SerialData;
|
||||
|
||||
class SerialClass : public HardwareSerial {
|
||||
private:
|
||||
// data accessible to IRQ handler
|
||||
SerialData data;
|
||||
IRQn irq;
|
||||
pin_size_t rx;
|
||||
pin_size_t tx;
|
||||
|
||||
public:
|
||||
SerialClass(UART_TypeDef *uart, IRQn irq, pin_size_t rx, pin_size_t tx);
|
||||
|
||||
inline void begin(unsigned long baudrate) {
|
||||
begin(baudrate, SERIAL_8N1);
|
||||
}
|
||||
|
||||
void begin(unsigned long baudrate, uint16_t config);
|
||||
void end();
|
||||
int available();
|
||||
int peek();
|
||||
int read();
|
||||
void flush();
|
||||
size_t write(uint8_t c);
|
||||
|
||||
operator bool() {
|
||||
return !!data.buf;
|
||||
}
|
||||
|
||||
using Print::write;
|
||||
};
|
||||
@@ -7,10 +7,17 @@
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
extern void LOGUART_PutChar(char c);
|
||||
#define LOG_UART_REG_BASE 0x40003000
|
||||
#define UART0_REG_BASE 0x40040000
|
||||
#define UART1_REG_BASE 0x40040400
|
||||
#define UART2_REG_BASE LOG_UART_REG_BASE
|
||||
|
||||
extern uint32_t UART_Writable(void *UARTx);
|
||||
extern void UART_CharPut(void *UARTx, uint8_t TxData);
|
||||
|
||||
void putchar_(char c) {
|
||||
LOGUART_PutChar(c);
|
||||
while (UART_Writable(LOG_UART_REG_BASE) == 0) {}
|
||||
UART_CharPut(LOG_UART_REG_BASE, c);
|
||||
}
|
||||
|
||||
WRAP_PRINTF(rtl_printf);
|
||||
|
||||
Reference in New Issue
Block a user