[core] Refactor SerialClass as API library
This commit is contained in:
34
cores/common/arduino/libraries/api/Serial/Serial.cpp
Normal file
34
cores/common/arduino/libraries/api/Serial/Serial.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2023-05-23. */
|
||||
|
||||
#include "Serial.h"
|
||||
|
||||
SerialClass::SerialClass(uint32_t port) {
|
||||
this->port = port;
|
||||
this->buf = NULL;
|
||||
this->data = NULL;
|
||||
}
|
||||
|
||||
#if LT_AUTO_DOWNLOAD_REBOOT && defined(LT_UART_ADR_PATTERN)
|
||||
static uint8_t adrState = 0;
|
||||
static uint8_t adrCmd[] = {LT_UART_ADR_PATTERN};
|
||||
|
||||
void SerialClass::adrParse(uint8_t c) {
|
||||
adrState = (adrState + 1) * (c == adrCmd[adrState]);
|
||||
if (adrState == sizeof(adrCmd) / sizeof(uint8_t)) {
|
||||
LT_I("Auto download mode: rebooting");
|
||||
LT.restartDownloadMode();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
int SerialClass::available() {
|
||||
return this->buf && this->buf->available();
|
||||
}
|
||||
|
||||
int SerialClass::peek() {
|
||||
return this->buf ? this->buf->peek() : -1;
|
||||
}
|
||||
|
||||
int SerialClass::read() {
|
||||
return this->buf ? this->buf->read_char() : -1;
|
||||
}
|
||||
46
cores/common/arduino/libraries/api/Serial/Serial.h
Normal file
46
cores/common/arduino/libraries/api/Serial/Serial.h
Normal file
@@ -0,0 +1,46 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-06-23. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <api/HardwareSerial.h>
|
||||
#include <api/RingBuffer.h>
|
||||
|
||||
using namespace arduino;
|
||||
|
||||
class SerialClass : public HardwareSerial {
|
||||
private:
|
||||
uint32_t port;
|
||||
RingBuffer *buf;
|
||||
|
||||
public:
|
||||
void *data;
|
||||
|
||||
public:
|
||||
SerialClass(uint32_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;
|
||||
}
|
||||
|
||||
public:
|
||||
#if LT_AUTO_DOWNLOAD_REBOOT
|
||||
static void adrParse(uint8_t c);
|
||||
#endif
|
||||
|
||||
using Print::write;
|
||||
};
|
||||
|
||||
#define HAS_SERIAL_CLASS 1
|
||||
@@ -42,6 +42,7 @@ using std::min;
|
||||
#if defined(__cplusplus) && LT_ARD_HAS_SERIAL
|
||||
#include <Serial.h>
|
||||
|
||||
#if HAS_SERIAL_CLASS
|
||||
#if HAS_SERIAL0
|
||||
extern SerialClass Serial0;
|
||||
#endif
|
||||
@@ -51,6 +52,7 @@ extern SerialClass Serial1;
|
||||
#if HAS_SERIAL2
|
||||
extern SerialClass Serial2;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#define SerialN(x) Serial##x
|
||||
#define SerialM(x) SerialN(x)
|
||||
|
||||
@@ -21,6 +21,8 @@ extern "C" {
|
||||
#define PIN_SWD (1 << 10)
|
||||
#define PIN_UART (1 << 11)
|
||||
|
||||
#define PIN_INVALID 255
|
||||
|
||||
typedef struct {
|
||||
/**
|
||||
* @brief GPIO name in the family SDK.
|
||||
|
||||
Reference in New Issue
Block a user