Loading...
Searching...
No Matches
SerialUART.h
Go to the documentation of this file.
1
10#pragma once
11
12#include "api/HardwareSerial.h"
13#include "api/RingBuffer.h"
14#include "hardware/uart.h"
15#include "pico/critical_section.h"
16
23class SerialUARTClass : public arduino::HardwareSerial {
24public:
31 SerialUARTClass(uart_inst_t* uart, pin_size_t tx_pin, pin_size_t rx_pin)
32 : uart_(uart), tx_pin_(tx_pin), rx_pin_(rx_pin) {}
33
38 void begin(unsigned long baud) override { begin(baud, SERIAL_8N1); }
39
46 void begin(unsigned long baud, uint16_t config) override;
47
53 void end() override;
54
59 int available() override;
60
65 int peek() override;
66
71 int read() override;
72
76 void flush() override;
77
83 size_t write(uint8_t c) override;
84 using arduino::Print::write;
85
90 operator bool() override { return started_; }
91
93 void handleRxInterrupt();
94
95private:
96 uart_inst_t* uart_;
97 pin_size_t tx_pin_;
98 pin_size_t rx_pin_;
99 bool started_ = false;
100 arduino::RingBufferN<256> rx_buffer_;
101 critical_section_t rx_lock_ = {}; // guards rx_buffer_; set up by the first begin()
102};
103
SerialUARTClass Serial1
UART0 on D1 (TX) and D0 (RX).
SerialUARTClass Serial2
UART1 on D4 (TX) and D5 (RX).
Serial port on one RP2040 UART; sketches use Serial1 and Serial2.
Definition SerialUART.h:23
void begin(unsigned long baud) override
Starts the port with 8 data bits, no parity and 1 stop bit.
Definition SerialUART.h:38
void end() override
Waits for queued bytes to go out, then stops the port and frees its pins.
size_t write(uint8_t c) override
Sends one byte, waiting while the hardware FIFO is full.
SerialUARTClass(uart_inst_t *uart, pin_size_t tx_pin, pin_size_t rx_pin)
Binds the port to a UART and its pins; nothing is set up until begin().
Definition SerialUART.h:31
int peek() override
Next received byte, left in the buffer.
void begin(unsigned long baud, uint16_t config) override
Starts the port with the given frame format.
void flush() override
Waits until every queued byte has been sent.
int read() override
Takes the next received byte from the buffer.
int available() override
Number of received bytes waiting to be read.
uint8_t pin_size_t
Type for Arduino pin numbers (uint8_t).
Definition Common.h:112