JM9 XRCU Board 0.1.2
Libraries API Reference
Loading...
Searching...
No Matches
HardwareSerial.h
1/*
2 HardwareSerial.h - Hardware serial library for Wiring
3 Copyright (c) 2006 Nicholas Zambetti. All right reserved.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19 Modified 28 September 2010 by Mark Sproul
20 Modified 14 August 2012 by Alarus
21 Modified 3 December 2013 by Matthijs Kooijman
22*/
23
24#ifndef HardwareSerial_h
25#define HardwareSerial_h
26
27#include <inttypes.h>
28
29#include "Stream.h"
30#include "uart.h"
31
32// Define constants and variables for buffering incoming serial data. We're
33// using a ring buffer (I think), in which head is the index of the location
34// to which to write the next incoming character and tail is the index of the
35// location from which to read.
36// NOTE: a "power of 2" buffer size is recommended to dramatically
37// optimize all the modulo operations for ring buffers.
38// WARNING: When buffer sizes are increased to > 256, the buffer index
39// variables are automatically increased in size, but the extra
40// atomicity guards needed for that are not implemented. This will
41// often work, but occasionally a race condition can occur that makes
42// Serial behave erratically. See https://github.com/arduino/Arduino/issues/2405
43#if !defined(SERIAL_TX_BUFFER_SIZE)
44 #define SERIAL_TX_BUFFER_SIZE 64
45#endif
46#if !defined(SERIAL_RX_BUFFER_SIZE)
47 #define SERIAL_RX_BUFFER_SIZE 64
48#endif
49#if (SERIAL_TX_BUFFER_SIZE>256)
50 typedef uint16_t tx_buffer_index_t;
51#else
52 typedef uint8_t tx_buffer_index_t;
53#endif
54#if (SERIAL_RX_BUFFER_SIZE>256)
55 typedef uint16_t rx_buffer_index_t;
56#else
57 typedef uint8_t rx_buffer_index_t;
58#endif
59
60// A bool should be enough for this
61// But it brings an build error due to ambiguous
62// call of overloaded HardwareSerial(int, int)
63// So defining a dedicated type
64typedef enum {
65 HALF_DUPLEX_DISABLED,
66 HALF_DUPLEX_ENABLED
67} HalfDuplexMode_t;
68
69// Define config for Serial.begin(baud, config);
70// below configs are not supported by STM32
71//#define SERIAL_5N1 0x00
72//#define SERIAL_5N2 0x08
73//#define SERIAL_5E1 0x20
74//#define SERIAL_5E2 0x28
75//#define SERIAL_5O1 0x30
76//#define SERIAL_5O2 0x38
77//#define SERIAL_6N1 0x02
78//#define SERIAL_6N2 0x0A
79
80#ifdef UART_WORDLENGTH_7B
81 #define SERIAL_7N1 0x04
82 #define SERIAL_7N2 0x0C
83 #define SERIAL_6E1 0x22
84 #define SERIAL_6E2 0x2A
85 #define SERIAL_6O1 0x32
86 #define SERIAL_6O2 0x3A
87#endif
88#define SERIAL_8N1 0x06
89#define SERIAL_8N2 0x0E
90#define SERIAL_7E1 0x24
91#define SERIAL_8E1 0x26
92#define SERIAL_7E2 0x2C
93#define SERIAL_8E2 0x2E
94#define SERIAL_7O1 0x34
95#define SERIAL_8O1 0x36
96#define SERIAL_7O2 0x3C
97#define SERIAL_8O2 0x3E
98
99class HardwareSerial : public Stream {
100 protected:
101 // Has any byte been written to the UART since begin()
102 bool _written;
103
104 // Don't put any members after these buffers, since only the first
105 // 32 bytes of this struct can be accessed quickly using the ldd
106 // instruction.
107 unsigned char _rx_buffer[SERIAL_RX_BUFFER_SIZE];
108 unsigned char _tx_buffer[SERIAL_TX_BUFFER_SIZE];
109
110 serial_t _serial;
111
112 public:
113 HardwareSerial(uint32_t _rx, uint32_t _tx, uint32_t _rts = NUM_DIGITAL_PINS, uint32_t _cts = NUM_DIGITAL_PINS);
114 HardwareSerial(PinName _rx, PinName _tx, PinName _rts = NC, PinName _cts = NC);
115 HardwareSerial(void *peripheral, HalfDuplexMode_t halfDuplex = HALF_DUPLEX_DISABLED);
116 HardwareSerial(uint32_t _rxtx);
117 HardwareSerial(PinName _rxtx);
118 void begin(unsigned long baud)
119 {
120 begin(baud, SERIAL_8N1);
121 }
122 void begin(unsigned long, uint8_t);
123 void end();
124 virtual int available(void);
125 virtual int peek(void);
126 virtual int read(void);
127 int availableForWrite(void);
128 virtual void flush();
129 void flush(uint32_t timeout);
130 virtual size_t write(uint8_t);
131 inline size_t write(unsigned long n)
132 {
133 return write((uint8_t)n);
134 }
135 inline size_t write(long n)
136 {
137 return write((uint8_t)n);
138 }
139 inline size_t write(unsigned int n)
140 {
141 return write((uint8_t)n);
142 }
143 inline size_t write(int n)
144 {
145 return write((uint8_t)n);
146 }
147 size_t write(const uint8_t *buffer, size_t size);
148 using Print::write; // pull in write(str) from Print
149 operator bool()
150 {
151 return true;
152 }
153
154 void setRx(uint32_t _rx);
155 void setTx(uint32_t _tx);
156 void setRx(PinName _rx);
157 void setTx(PinName _tx);
158
159 // Enable HW flow control on RTS, CTS or both
160 void setRts(uint32_t _rts);
161 void setCts(uint32_t _cts);
162 void setRtsCts(uint32_t _rts, uint32_t _cts);
163 void setRts(PinName _rts);
164 void setCts(PinName _cts);
165 void setRtsCts(PinName _rts, PinName _cts);
166
167 // Enable half-duplex mode by setting the Rx pin to NC
168 // This needs to be done before the call to begin()
169 void setHalfDuplex(void);
170 bool isHalfDuplex(void) const;
171 void enableHalfDuplexRx(void);
172
173 friend class STM32LowPower;
174
175 // Interrupt handlers
176 static void _rx_complete_irq(serial_t *obj);
177 static int _tx_complete_irq(serial_t *obj);
178
179#if defined(HAL_UART_MODULE_ENABLED) && !defined(HAL_UART_MODULE_ONLY)
180 // Could be used to mix Arduino API and STM32Cube HAL API (ex: DMA). Use at your own risk.
181 UART_HandleTypeDef *getHandle(void)
182 {
183 return &(_serial.handle);
184 }
185#endif // HAL_UART_MODULE_ENABLED && !HAL_UART_MODULE_ONLY
186
187 private:
188 bool _rx_enabled;
189 uint8_t _config;
190 unsigned long _baud;
191 void init(PinName _rx, PinName _tx, PinName _rts = NC, PinName _cts = NC);
192 void configForLowPower(void);
193};
194
195#if defined(USART1)
196 extern HardwareSerial Serial1;
197#endif
198#if defined(USART2)
199 extern HardwareSerial Serial2;
200#endif
201#if defined(USART3)
202 extern HardwareSerial Serial3;
203#endif
204#if defined(UART4) || defined(USART4)
205 extern HardwareSerial Serial4;
206#endif
207#if defined(UART5) || defined(USART5)
208 extern HardwareSerial Serial5;
209#endif
210#if defined(USART6)
211 extern HardwareSerial Serial6;
212#endif
213#if defined(UART7) || defined(USART7)
214 extern HardwareSerial Serial7;
215#endif
216#if defined(UART8) || defined(USART8)
217 extern HardwareSerial Serial8;
218#endif
219#if defined(UART9)
220 extern HardwareSerial Serial9;
221#endif
222#if defined(UART10) || defined(USART10)
223 extern HardwareSerial Serial10;
224#endif
225#if defined(LPUART1)
226 extern HardwareSerial SerialLP1;
227#endif
228#if defined(LPUART2)
229 extern HardwareSerial SerialLP2;
230#endif
231#endif
Definition HardwareSerial.h:99