COSA
An Object-Oriented Platform for Arduino Programming
UART.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_UART_HH
22 #define COSA_UART_HH
23 
24 // Default reciever buffer size
25 #ifndef COSA_UART_RX_BUFFER_MAX
26 # if defined(BOARD_ATTINY)
27 # define COSA_UART_RX_BUFFER_MAX 16
28 # else
29 # define COSA_UART_RX_BUFFER_MAX 32
30 # endif
31 #endif
32 
33 // Default transmitter buffer size
34 #ifndef COSA_UART_TX_BUFFER_MAX
35 # if defined(BOARD_ATTINY)
36 # define COSA_UART_TX_BUFFER_MAX 16
37 # else
38 # define COSA_UART_TX_BUFFER_MAX 32
39 # endif
40 #endif
41 
42 #if defined(BOARD_ATTINY)
43 #include "Cosa/Soft/UART.hh"
44 // Default is serial output only
45 // extern Soft::UART uart;
46 extern Soft::UAT uart;
47 #else
48 
49 #include "Cosa/Types.h"
50 #include "Cosa/Serial.hh"
51 #include "Cosa/IOStream.hh"
52 #include "Cosa/Board.hh"
53 
59 class UART : public Serial {
60 public:
62  static const uint16_t RX_BUFFER_MAX = COSA_UART_RX_BUFFER_MAX;
63  static const uint16_t TX_BUFFER_MAX = COSA_UART_TX_BUFFER_MAX;
64 
72  Serial(),
73  m_port(port),
74  m_sfr(Board::UART(port)),
75  m_ibuf(ibuf),
76  m_obuf(obuf),
77  m_idle(true)
78  {
79  uart[port] = this;
80  }
81 
87  virtual int available()
88  {
89  return (m_ibuf->available());
90  }
91 
97  virtual int room()
98  {
99  return (m_obuf->room());
100  }
101 
109  virtual int putchar(char c);
110 
117  virtual int peekchar()
118  {
119  return (m_ibuf->peekchar());
120  }
121 
128  virtual int peekchar(char c)
129  {
130  return (m_ibuf->peekchar(c));
131  }
132 
139  virtual int getchar()
140  {
141  return (m_ibuf->getchar());
142  }
143 
150  virtual int flush();
151 
156  virtual void empty()
157  {
158  m_ibuf->empty();
159  }
160 
169  virtual bool begin(uint32_t baudrate = DEFAULT_BAUDRATE,
170  uint8_t format = DEFAULT_FORMAT);
171 
178  virtual bool end();
179 
184  virtual void powerup();
185 
190  virtual void powerdown();
191 
192 protected:
193  uint8_t m_port;
194  volatile uint8_t* const m_sfr;
197  bool m_idle;
198 
203  static UART* uart[Board::UART_MAX];
204 
209  volatile uint8_t* UCSRnA() const
210  {
211  return (m_sfr);
212  }
213 
218  volatile uint8_t* UCSRnB() const
219  {
220  return (m_sfr + 1);
221  }
222 
227  volatile uint8_t* UCSRnC() const
228  {
229  return (m_sfr + 2);
230  }
231 
236  volatile uint16_t* UBRRn() const
237  {
238  return ((volatile uint16_t*) (m_sfr + 4));
239  }
240 
245  volatile uint8_t* UDRn() const
246  {
247  return (m_sfr + 6);
248  }
249 
254  virtual void on_udre_interrupt();
255 
260  virtual void on_rx_interrupt();
261 
266  virtual void on_tx_interrupt() {}
267 
268  // Interrupt Service Routines are friends
269 #if defined(USART_UDRE_vect)
270  friend void USART_UDRE_vect(void);
271  friend void USART_RX_vect(void);
272  friend void USART_TX_vect(void);
273 #endif
274 #if defined(USART1_UDRE_vect)
275  friend void USART1_UDRE_vect(void);
276  friend void USART1_RX_vect(void);
277  friend void USART1_TX_vect(void);
278 #endif
279 #if defined(USART2_UDRE_vect)
280  friend void USART2_UDRE_vect(void);
281  friend void USART2_RX_vect(void);
282  friend void USART2_TX_vect(void);
283 #endif
284 #if defined(USART3_UDRE_vect)
285  friend void USART3_UDRE_vect(void);
286  friend void USART3_RX_vect(void);
287  friend void USART3_TX_vect(void);
288 #endif
289 };
290 
295 #if defined(USBCON)
296 #include "Cosa/CDC.hh"
297 #define uart cdc
298 #else
299 extern UART uart;
300 #endif
301 #endif
302 #endif
virtual int peekchar()
Definition: UART.hh:117
UART uart
#define USART_UDRE_vect
Definition: ATmega1284P.hh:278
#define COSA_UART_RX_BUFFER_MAX
Definition: UART.hh:29
virtual int flush()
Definition: UART.cpp:130
IOStream::Device * m_obuf
Output Buffer/Device.
Definition: UART.hh:196
void USART1_RX_vect(void)
void USART2_TX_vect(void)
volatile uint8_t * UCSRnA() const
Definition: UART.hh:209
volatile uint8_t * UCSRnC() const
Definition: UART.hh:227
virtual void powerup()
Definition: UART.cpp:150
virtual int room()
virtual void empty()
virtual void powerdown()
Definition: UART.cpp:179
virtual int peekchar(char c)
Definition: UART.hh:128
virtual bool begin(uint32_t baudrate=DEFAULT_BAUDRATE, uint8_t format=DEFAULT_FORMAT)
Definition: UART.cpp:45
void USART1_UDRE_vect(void)
bool m_idle
Flag idle mode.
Definition: UART.hh:197
virtual bool end()
Definition: UART.cpp:69
static UART * uart[Board::UART_MAX]
Definition: UART.hh:203
UART(uint8_t port, IOStream::Device *ibuf, IOStream::Device *obuf)
Definition: UART.hh:71
virtual int getchar()
Definition: UART.hh:139
virtual int room()
Definition: UART.hh:97
volatile uint16_t * UBRRn() const
Definition: UART.hh:236
virtual void on_udre_interrupt()
Definition: UART.cpp:208
#define COSA_UART_TX_BUFFER_MAX
Definition: UART.hh:38
void USART2_UDRE_vect(void)
void USART1_TX_vect(void)
static IOBuffer< UART::TX_BUFFER_MAX > obuf
Definition: UART.cpp:38
IOStream::Device * m_ibuf
Input Buffer/Device.
Definition: UART.hh:195
volatile uint8_t *const m_sfr
Special Function Register Pointer.
Definition: UART.hh:194
Definition: UART.hh:59
#define USART_RX_vect
Definition: ATmega1284P.hh:279
virtual int available()
virtual void on_tx_interrupt()
Definition: UART.hh:266
static const uint32_t DEFAULT_BAUDRATE
Definition: Serial.hh:43
Definition: Serial.hh:40
void USART3_RX_vect(void)
virtual void on_rx_interrupt()
Definition: UART.cpp:221
static const uint16_t TX_BUFFER_MAX
Definition: UART.hh:63
virtual void empty()
Definition: UART.hh:156
virtual int getchar()
void USART3_UDRE_vect(void)
static const uint16_t RX_BUFFER_MAX
Definition: UART.hh:62
virtual int peekchar()
static IOBuffer< UART::RX_BUFFER_MAX > ibuf
Definition: UART.cpp:37
#define USART_TX_vect
Definition: ATmega1284P.hh:280
uint8_t m_port
UART port index.
Definition: UART.hh:193
virtual int available()
Definition: UART.hh:87
volatile uint8_t * UCSRnB() const
Definition: UART.hh:218
static const uint8_t DEFAULT_FORMAT
Definition: Serial.hh:59
void USART3_TX_vect(void)
volatile uint8_t * UDRn() const
Definition: UART.hh:245
void USART2_RX_vect(void)
virtual int putchar(char c)
Definition: UART.cpp:95