From 48764018099b93058206622bf33ce5e47c8540af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Thu, 23 Jun 2022 17:06:44 +0200 Subject: [PATCH] [beken-72xx] Implement Serial classes --- README.md | 5 +- arduino/beken-72xx/cores/arduino/Arduino.h | 17 +++++ .../beken-72xx/cores/arduino/SerialClass.cpp | 75 +++++++++++++++++++ .../beken-72xx/cores/arduino/SerialClass.h | 38 ++++++++++ 4 files changed, 133 insertions(+), 2 deletions(-) create mode 100644 arduino/beken-72xx/cores/arduino/SerialClass.cpp create mode 100644 arduino/beken-72xx/cores/arduino/SerialClass.h diff --git a/README.md b/README.md index facc630..fe0edcd 100644 --- a/README.md +++ b/README.md @@ -45,8 +45,9 @@ Note: this list will probably change with each functionality update. --------------------|----------------|------------- Core functions | ✔️ | ✔️ GPIO/PWM/IRQ | ✔️/✔️/✔️ | ❓/✔️/❌ -Analog input | ✔️ | ✔️ -UART I/O | ✔️ | ❌ +Analog input (ADC) | ✔️ | ✔️ +Serial | ✔️ | ✔️ +Serial (extra) | ❌ | 1, 2 Flash I/O | ✔️ | ✔️ **CORE LIBRARIES** | | SoftwareSerial | ❌ | ❌ diff --git a/arduino/beken-72xx/cores/arduino/Arduino.h b/arduino/beken-72xx/cores/arduino/Arduino.h index cbea0ee..571074f 100644 --- a/arduino/beken-72xx/cores/arduino/Arduino.h +++ b/arduino/beken-72xx/cores/arduino/Arduino.h @@ -13,3 +13,20 @@ #include "WVariant.h" // Include board variant #include "variant.h" + +#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 +extern SerialClass Serial1; +#endif +#ifdef PIN_SERIAL2_RX +extern SerialClass Serial2; +#define Serial Serial2 +#else +#define Serial Serial1 +#endif +#endif +#endif diff --git a/arduino/beken-72xx/cores/arduino/SerialClass.cpp b/arduino/beken-72xx/cores/arduino/SerialClass.cpp new file mode 100644 index 0000000..aa5d53e --- /dev/null +++ b/arduino/beken-72xx/cores/arduino/SerialClass.cpp @@ -0,0 +1,75 @@ +/* Copyright (c) Kuba Szczodrzyński 2022-06-23. */ + +#include "SerialClass.h" + +extern "C" { +#include + +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 +SerialClass Serial1(UART1_PORT); +#endif +#ifdef PIN_SERIAL2_RX +SerialClass Serial2(UART2_PORT); +#endif + +SerialClass::SerialClass(uint8_t port) { + this->port = port; + this->buf = NULL; +} + +static void callback(int port, void *param) { + RingBuffer *buf = (RingBuffer *)param; + buf->store_char(uart_read_byte(port)); +} + +void SerialClass::begin(unsigned long baudrate, uint16_t config) { + uint8_t dataWidth = ((config & SERIAL_DATA_MASK) >> 8) - 1; // 0x100..0x400 -> 0..3 + uint8_t parity = 3 - (config & SERIAL_PARITY_MASK); // 0x3..0x1 -> 0..2 + uint8_t stopBits = (config & SERIAL_STOP_BIT_MASK) == SERIAL_STOP_BIT_2; // 0x10..0x30 -> 0..1 + + bk_uart_config_t cfg = { + .baud_rate = baudrate, + .data_width = (uart_data_width_t)dataWidth, + .parity = (uart_parity_t)parity, + .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); +} + +void SerialClass::end() { + uart_rx_callback_set(port, NULL, NULL); + delete this->buf; +} + +int SerialClass::available() { + return buf->available(); +} + +int SerialClass::peek() { + return buf->peek(); +} + +int SerialClass::read() { + return buf->read_char(); +} + +void SerialClass::flush() { + uart_wait_tx_over(); +} + +size_t SerialClass::write(uint8_t c) { + bk_send_byte(port, c); + return 1; +} diff --git a/arduino/beken-72xx/cores/arduino/SerialClass.h b/arduino/beken-72xx/cores/arduino/SerialClass.h new file mode 100644 index 0000000..19ef39c --- /dev/null +++ b/arduino/beken-72xx/cores/arduino/SerialClass.h @@ -0,0 +1,38 @@ +/* Copyright (c) Kuba Szczodrzyński 2022-06-23. */ + +#pragma once + +#include +#include +#include + +using namespace arduino; + +class SerialClass : public HardwareSerial { + private: + uint8_t port; + RingBuffer *buf; + + public: + SerialClass(uint8_t port); + + 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 !!buf; + } + + using Print::write; +}; + +#define HAS_SERIAL_CLASS 1