aboutsummaryrefslogtreecommitdiff
path: root/keyboards/bioi/usart.h
diff options
context:
space:
mode:
Diffstat (limited to 'keyboards/bioi/usart.h')
-rw-r--r--keyboards/bioi/usart.h436
1 files changed, 436 insertions, 0 deletions
diff --git a/keyboards/bioi/usart.h b/keyboards/bioi/usart.h
new file mode 100644
index 000000000..cc59c7236
--- /dev/null
+++ b/keyboards/bioi/usart.h
@@ -0,0 +1,436 @@
1#ifndef USART_H
2#define USART_H
3
4/************************************************************************
5Title: Interrupt UART library with receive/transmit circular buffers
6Author: Andy Gock
7Software: AVR-GCC 4.1, AVR Libc 1.4
8Hardware: any AVR with built-in UART, tested on AT90S8515 & ATmega8 at 4 Mhz
9License: GNU General Public License
10Usage: see README.md and Doxygen manual
11
12Based on original library by Peter Fluery, Tim Sharpe, Nicholas Zambetti.
13
14https://github.com/andygock/avr-uart
15
16LICENSE:
17
18 Copyright (C) 2012 Andy Gock
19 Copyright (C) 2006 Peter Fleury
20
21 This program is free software; you can redistribute it and/or modify
22 it under the terms of the GNU General Public License as published by
23 the Free Software Foundation; either version 2 of the License, or
24 any later version.
25
26 This program is distributed in the hope that it will be useful,
27 but WITHOUT ANY WARRANTY; without even the implied warranty of
28 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 GNU General Public License for more details.
30
31************************************************************************/
32
33/************************************************************************
34uart_available, uart_flush, uart1_available, and uart1_flush functions
35were adapted from the Arduino HardwareSerial.h library by Tim Sharpe on
3611 Jan 2009. The license info for HardwareSerial.h is as follows:
37
38 HardwareSerial.h - Hardware serial library for Wiring
39 Copyright (c) 2006 Nicholas Zambetti. All right reserved.
40
41 This library is free software; you can redistribute it and/or
42 modify it under the terms of the GNU Lesser General Public
43 License as published by the Free Software Foundation; either
44 version 2.1 of the License, or (at your option) any later version.
45
46 This library is distributed in the hope that it will be useful,
47 but WITHOUT ANY WARRANTY; without even the implied warranty of
48 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
49 Lesser General Public License for more details.
50
51 You should have received a copy of the GNU Lesser General Public
52 License along with this library; if not, write to the Free Software
53 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
54************************************************************************/
55
56/**
57 * @defgroup avr-uart UART Library
58 * @code #include <uart.h> @endcode
59 *
60 * @brief Interrupt UART library using the built-in UART with transmit and receive circular buffers.
61 * @see README.md
62 *
63 * This library can be used to transmit and receive data through the built in UART.
64 *
65 * An interrupt is generated when the UART has finished transmitting or
66 * receiving a byte. The interrupt handling routines use circular buffers
67 * for buffering received and transmitted data.
68 *
69 * The UART_RXn_BUFFER_SIZE and UART_TXn_BUFFER_SIZE constants define
70 * the size of the circular buffers in bytes. Note that these constants must be a power of 2.
71 *
72 * You need to define these buffer sizes as a symbol in your compiler settings or in uart.h
73 *
74 * See README.md for more detailed information. Especially that relating to symbols: USARTn_ENABLED and USARTn_LARGE_BUFFER
75 *
76 * @author Andy Gock <[email protected]>
77 * @note Based on Atmel Application Note AVR306 and original library by Peter Fleury and Tim Sharpe.
78 */
79
80/**@{*/
81#include <stdint.h>
82#include <avr/io.h>
83
84#if (__GNUC__ * 100 + __GNUC_MINOR__) < 304
85#error "This library requires AVR-GCC 3.4 or later, update to newer AVR-GCC compiler !"
86#endif
87
88/*
89 * constants and macros
90 */
91
92/* Enable USART 1, 2, 3 as required */
93/* Can be defined in compiler symbol setup with -D option (preferred) */
94#ifndef USART0_ENABLED
95 #define USART0_ENABLED /**< Enable USART0 */
96#endif
97//#define USART1_ENABLED
98//#define USART2_ENABLED
99//#define USART3_ENABLED
100
101/* Set size of receive and transmit buffers */
102
103#ifndef UART_RX0_BUFFER_SIZE
104 #define UART_RX0_BUFFER_SIZE 128 /**< Size of the circular receive buffer, must be power of 2 */
105#endif
106#ifndef UART_RX1_BUFFER_SIZE
107 #define UART_RX1_BUFFER_SIZE 128 /**< Size of the circular receive buffer, must be power of 2 */
108#endif
109#ifndef UART_RX2_BUFFER_SIZE
110 #define UART_RX2_BUFFER_SIZE 128 /**< Size of the circular receive buffer, must be power of 2 */
111#endif
112#ifndef UART_RX3_BUFFER_SIZE
113 #define UART_RX3_BUFFER_SIZE 128 /**< Size of the circular receive buffer, must be power of 2 */
114#endif
115
116#ifndef UART_TX0_BUFFER_SIZE
117 #define UART_TX0_BUFFER_SIZE 128 /**< Size of the circular transmit buffer, must be power of 2 */
118#endif
119#ifndef UART_TX1_BUFFER_SIZE
120 #define UART_TX1_BUFFER_SIZE 128 /**< Size of the circular transmit buffer, must be power of 2 */
121#endif
122#ifndef UART_TX2_BUFFER_SIZE
123 #define UART_TX2_BUFFER_SIZE 128 /**< Size of the circular transmit buffer, must be power of 2 */
124#endif
125#ifndef UART_TX3_BUFFER_SIZE
126 #define UART_TX3_BUFFER_SIZE 128 /**< Size of the circular transmit buffer, must be power of 2 */
127#endif
128
129/* Check buffer sizes are not too large for 8-bit positioning */
130
131#if (UART_RX0_BUFFER_SIZE > 256 & !defined(USART0_LARGE_BUFFER))
132 #error "Buffer too large, please use -DUSART0_LARGE_BUFFER switch in compiler options"
133#endif
134
135#if (UART_RX1_BUFFER_SIZE > 256 & !defined(USART1_LARGE_BUFFER))
136 #error "Buffer too large, please use -DUSART1_LARGE_BUFFER switch in compiler options"
137#endif
138
139#if (UART_RX2_BUFFER_SIZE > 256 & !defined(USART2_LARGE_BUFFER))
140 #error "Buffer too large, please use -DUSART2_LARGE_BUFFER switch in compiler options"
141#endif
142
143#if (UART_RX3_BUFFER_SIZE > 256 & !defined(USART3_LARGE_BUFFER))
144 #error "Buffer too large, please use -DUSART3_LARGE_BUFFER switch in compiler options"
145#endif
146
147/* Check buffer sizes are not too large for *_LARGE_BUFFER operation (16-bit positioning) */
148
149#if (UART_RX0_BUFFER_SIZE > 32768)
150 #error "Buffer too large, maximum allowed is 32768 bytes"
151#endif
152
153#if (UART_RX1_BUFFER_SIZE > 32768)
154 #error "Buffer too large, maximum allowed is 32768 bytes"
155#endif
156
157#if (UART_RX2_BUFFER_SIZE > 32768)
158 #error "Buffer too large, maximum allowed is 32768 bytes"
159#endif
160
161#if (UART_RX3_BUFFER_SIZE > 32768)
162 #error "Buffer too large, maximum allowed is 32768 bytes"
163#endif
164
165/** @brief UART Baudrate Expression
166 * @param xtalCpu system clock in Mhz, e.g. 4000000L for 4Mhz
167 * @param baudRate baudrate in bps, e.g. 1200, 2400, 9600
168 */
169#define UART_BAUD_SELECT(baudRate,xtalCpu) (((xtalCpu)+8UL*(baudRate))/(16UL*(baudRate))-1UL)
170
171/** @brief UART Baudrate Expression for ATmega double speed mode
172 * @param xtalCpu system clock in Mhz, e.g. 4000000L for 4Mhz
173 * @param baudRate baudrate in bps, e.g. 1200, 2400, 9600
174 */
175#define UART_BAUD_SELECT_DOUBLE_SPEED(baudRate,xtalCpu) ((((xtalCpu)+4UL*(baudRate))/(8UL*(baudRate))-1)|0x8000)
176
177/* test if the size of the circular buffers fits into SRAM */
178
179#if defined(USART0_ENABLED) && ( (UART_RX0_BUFFER_SIZE+UART_TX0_BUFFER_SIZE) >= (RAMEND-0x60))
180 #error "size of UART_RX0_BUFFER_SIZE + UART_TX0_BUFFER_SIZE larger than size of SRAM"
181#endif
182
183#if defined(USART1_ENABLED) && ( (UART_RX1_BUFFER_SIZE+UART_TX1_BUFFER_SIZE) >= (RAMEND-0x60))
184 #error "size of UART_RX1_BUFFER_SIZE + UART_TX1_BUFFER_SIZE larger than size of SRAM"
185#endif
186
187#if defined(USART2_ENABLED) && ( (UART_RX2_BUFFER_SIZE+UART_RX2_BUFFER_SIZE) >= (RAMEND-0x60))
188 #error "size of UART_RX2_BUFFER_SIZE + UART_TX2_BUFFER_SIZE larger than size of SRAM"
189#endif
190
191#if defined(USART3_ENABLED) && ( (UART_RX3_BUFFER_SIZE+UART_RX3_BUFFER_SIZE) >= (RAMEND-0x60))
192 #error "size of UART_RX3_BUFFER_SIZE + UART_TX3_BUFFER_SIZE larger than size of SRAM"
193#endif
194
195/*
196** high byte error return code of uart_getc()
197*/
198#define UART_FRAME_ERROR 0x0800 /**< Framing Error by UART */
199#define UART_OVERRUN_ERROR 0x0400 /**< Overrun condition by UART */
200#define UART_BUFFER_OVERFLOW 0x0200 /**< receive ringbuffer overflow */
201#define UART_NO_DATA 0x0100 /**< no receive data available */
202
203/* Macros, to allow use of legacy names */
204
205/** @brief Macro to initialize USART0 (only available on selected ATmegas) @see uart0_init */
206#define uart_init(b) uart0_init(b)
207
208/** @brief Macro to get received byte of USART0 from ringbuffer. (only available on selected ATmega) @see uart0_getc */
209#define uart_getc() uart0_getc()
210
211/** @brief Macro to peek at next byte in USART0 ringbuffer */
212#define uart_peek() uart0_peek()
213
214/** @brief Macro to put byte to ringbuffer for transmitting via USART0 (only available on selected ATmega) @see uart0_putc */
215#define uart_putc(d) uart0_putc(d)
216
217/** @brief Macro to put string to ringbuffer for transmitting via USART0 (only available on selected ATmega) @see uart0_puts */
218#define uart_puts(s) uart0_puts(s)
219
220/** @brief Macro to put string from program memory to ringbuffer for transmitting via USART0 (only available on selected ATmega) @see uart0_puts_p */
221#define uart_puts_p(s) uart0_puts_p(s)
222
223/** @brief Macro to return number of bytes waiting in the receive buffer of USART0 @see uart0_available */
224#define uart_available() uart0_available()
225
226/** @brief Macro to flush bytes waiting in receive buffer of USART0 @see uart0_flush */
227#define uart_flush() uart0_flush()
228
229/*
230** function prototypes
231*/
232
233/**
234 @brief Initialize UART and set baudrate
235 @param baudrate Specify baudrate using macro UART_BAUD_SELECT()
236 @return none
237*/
238/*extern*/void uart0_init(uint16_t baudrate);
239
240
241/**
242 * @brief Get received byte from ringbuffer
243 *
244 * Returns in the lower byte the received character and in the
245 * higher byte the last receive error.
246 * UART_NO_DATA is returned when no data is available.
247 *
248 * @return lower byte: received byte from ringbuffer
249 * @return higher byte: last receive status
250 * - \b 0 successfully received data from UART
251 * - \b UART_NO_DATA
252 * <br>no receive data available
253 * - \b UART_BUFFER_OVERFLOW
254 * <br>Receive ringbuffer overflow.
255 * We are not reading the receive buffer fast enough,
256 * one or more received character have been dropped
257 * - \b UART_OVERRUN_ERROR
258 * <br>Overrun condition by UART.
259 * A character already present in the UART UDR register was
260 * not read by the interrupt handler before the next character arrived,
261 * one or more received characters have been dropped.
262 * - \b UART_FRAME_ERROR
263 * <br>Framing Error by UART
264 */
265/*extern*/uint16_t uart0_getc(void);
266
267/**
268 * @brief Peek at next byte in ringbuffer
269 *
270 * Returns the next byte (character) of incoming UART data without removing it from the
271 * internal ring buffer. That is, successive calls to uartN_peek() will return the same
272 * character, as will the next call to uartN_getc().
273 *
274 * UART_NO_DATA is returned when no data is available.
275 *
276 * @return lower byte: next byte in ringbuffer
277 * @return higher byte: last receive status
278 * - \b 0 successfully received data from UART
279 * - \b UART_NO_DATA
280 * <br>no receive data available
281 * - \b UART_BUFFER_OVERFLOW
282 * <br>Receive ringbuffer overflow.
283 * We are not reading the receive buffer fast enough,
284 * one or more received character have been dropped
285 * - \b UART_OVERRUN_ERROR
286 * <br>Overrun condition by UART.
287 * A character already present in the UART UDR register was
288 * not read by the interrupt handler before the next character arrived,
289 * one or more received characters have been dropped.
290 * - \b UART_FRAME_ERROR
291 * <br>Framing Error by UART
292 */
293/*extern*/uint16_t uart0_peek(void);
294
295/**
296 * @brief Put byte to ringbuffer for transmitting via UART
297 * @param data byte to be transmitted
298 * @return none
299 */
300/*extern*/void uart0_putc(uint8_t data);
301
302
303/**
304 * @brief Put string to ringbuffer for transmitting via UART
305 *
306 * The string is buffered by the uart library in a circular buffer
307 * and one character at a time is transmitted to the UART using interrupts.
308 * Blocks if it can not write the whole string into the circular buffer.
309 *
310 * @param s string to be transmitted
311 * @return none
312 */
313/*extern*/void uart0_puts(const char *s);
314
315
316/**
317 * @brief Put string from program memory to ringbuffer for transmitting via UART.
318 *
319 * The string is buffered by the uart library in a circular buffer
320 * and one character at a time is transmitted to the UART using interrupts.
321 * Blocks if it can not write the whole string into the circular buffer.
322 *
323 * @param s program memory string to be transmitted
324 * @return none
325 * @see uart0_puts_P
326 */
327/*extern*/void uart0_puts_p(const char *s);
328
329/**
330 * @brief Macro to automatically put a string constant into program memory
331 * \param __s string in program memory
332 */
333#define uart_puts_P(__s) uart0_puts_p(PSTR(__s))
334
335/** @brief Macro to automatically put a string constant into program memory */
336#define uart0_puts_P(__s) uart0_puts_p(PSTR(__s))
337
338/**
339 * @brief Return number of bytes waiting in the receive buffer
340 * @return bytes waiting in the receive buffer
341 */
342/*extern*/uint16_t uart0_available(void);
343
344/**
345 * @brief Flush bytes waiting in receive buffer
346 */
347/*extern*/void uart0_flush(void);
348
349
350/** @brief Initialize USART1 (only available on selected ATmegas) @see uart_init */
351/*extern*/void uart1_init(uint16_t baudrate);
352
353/** @brief Get received byte of USART1 from ringbuffer. (only available on selected ATmega) @see uart_getc */
354/*extern*/uint16_t uart1_getc(void);
355
356/** @brief Peek at next byte in USART1 ringbuffer */
357/*extern*/uint16_t uart1_peek(void);
358
359/** @brief Put byte to ringbuffer for transmitting via USART1 (only available on selected ATmega) @see uart_putc */
360/*extern*/void uart1_putc(uint8_t data);
361
362/** @brief Put string to ringbuffer for transmitting via USART1 (only available on selected ATmega) @see uart_puts */
363/*extern*/void uart1_puts(const char *s);
364
365/** @brief Put string from program memory to ringbuffer for transmitting via USART1 (only available on selected ATmega) @see uart_puts_p */
366/*extern*/void uart1_puts_p(const char *s);
367
368/** @brief Macro to automatically put a string constant into program memory of USART1 @see uart1_puts_p */
369#define uart1_puts_P(__s) uart1_puts_p(PSTR(__s))
370
371/** @brief Return number of bytes waiting in the receive buffer of USART1 */
372/*extern*/uint16_t uart1_available(void);
373
374/** @brief Flush bytes waiting in receive buffer of USART1 */
375/*extern*/void uart1_flush(void);
376
377
378/** @brief Initialize USART2 (only available on selected ATmegas) @see uart_init */
379/*extern*/void uart2_init(uint16_t baudrate);
380
381/** @brief Get received byte of USART2 from ringbuffer. (only available on selected ATmega) @see uart_getc */
382/*extern*/uint16_t uart2_getc(void);
383
384/** @brief Peek at next byte in USART2 ringbuffer */
385/*extern*/uint16_t uart2_peek(void);
386
387/** @brief Put byte to ringbuffer for transmitting via USART2 (only available on selected ATmega) @see uart_putc */
388/*extern*/void uart2_putc(uint8_t data);
389
390/** @brief Put string to ringbuffer for transmitting via USART2 (only available on selected ATmega) @see uart_puts */
391/*extern*/void uart2_puts(const char *s);
392
393/** @brief Put string from program memory to ringbuffer for transmitting via USART2 (only available on selected ATmega) @see uart_puts_p */
394/*extern*/void uart2_puts_p(const char *s);
395
396/** @brief Macro to automatically put a string constant into program memory of USART2 @see uart2_puts_p */
397#define uart2_puts_P(__s) uart2_puts_p(PSTR(__s))
398
399/** @brief Return number of bytes waiting in the receive buffer of USART2 */
400/*extern*/uint16_t uart2_available(void);
401
402/** @brief Flush bytes waiting in receive buffer of USART2 */
403/*extern*/void uart2_flush(void);
404
405
406/** @brief Initialize USART3 (only available on selected ATmegas) @see uart_init */
407/*extern*/void uart3_init(uint16_t baudrate);
408
409/** @brief Get received byte of USART3 from ringbuffer. (only available on selected ATmega) @see uart_getc */
410/*extern*/uint16_t uart3_getc(void);
411
412/** @brief Peek at next byte in USART3 ringbuffer */
413/*extern*/uint16_t uart3_peek(void);
414
415/** @brief Put byte to ringbuffer for transmitting via USART3 (only available on selected ATmega) @see uart_putc */
416/*extern*/void uart3_putc(uint8_t data);
417
418/** @brief Put string to ringbuffer for transmitting via USART3 (only available on selected ATmega) @see uart_puts */
419/*extern*/void uart3_puts(const char *s);
420
421/** @brief Put string from program memory to ringbuffer for transmitting via USART3 (only available on selected ATmega) @see uart_puts_p */
422/*extern*/void uart3_puts_p(const char *s);
423
424/** @brief Macro to automatically put a string constant into program memory of USART3 @see uart3_puts_p */
425#define uart3_puts_P(__s) uart3_puts_p(PSTR(__s))
426
427/** @brief Return number of bytes waiting in the receive buffer of USART3 */
428/*extern*/uint16_t uart3_available(void);
429
430/** @brief Flush bytes waiting in receive buffer of USART3 */
431/*extern*/void uart3_flush(void);
432
433/**@}*/
434
435#endif // UART_H
436