NiboRoboLib 3.6 - NIBO burger Library
usart.h
gehe zur Dokumentation dieser Datei
1 /* BSD-License:
2 
3 Copyright (c) 2007 by Nils Springob, nicai-systems, Germany
4 
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without modification,
8 are permitted provided that the following conditions are met:
9 
10  * Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12  * Redistributions in binary form must reproduce the above copyright notice,
13  this list of conditions and the following disclaimer in the documentation
14  and/or other materials provided with the distribution.
15  * Neither the name nicai-systems nor the names of its contributors may be
16  used to endorse or promote products derived from this software without
17  specific prior written permission.
18 
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 */
32 
46 #ifndef NIBOBURGER_USART_H_
47 #define NIBOBURGER_USART_H_
48 
49 #include <stdint.h>
50 #include <avr/pgmspace.h>
51 
52 #ifdef __cplusplus
53 extern "C" {
54 #endif
55 
56 
60 #define USART_TXBUF_SIZE 16
61 
62 
66 #define USART_RXBUF_SIZE 8
67 
68 
69 #ifndef DOXYGEN
70 extern uint8_t usart_txbuf[USART_TXBUF_SIZE];
71 extern uint8_t usart_rxbuf[USART_RXBUF_SIZE];
72 
73 extern volatile uint8_t usart_txbuf_begin;
74 extern volatile uint8_t usart_txbuf_end;
75 
76 extern volatile uint8_t usart_rxbuf_begin;
77 extern volatile uint8_t usart_rxbuf_end;
78 #endif
79 
80 
86 void usart_setbaudrate(uint16_t baud);
87 
88 
92 void usart_enable();
93 
94 
98 void usart_disable();
99 
100 
105 char usart_getchar();
106 
107 
112 char usart_putchar(char c);
113 
114 
122 void usart_write(const char * s);
123 
124 
132 void usart_write_P(PGM_P s);
133 
134 
143 void usart_send(const char * s, unsigned int len);
144 
145 
154 void usart_send_P(PGM_P s, unsigned int len);
155 
156 
157 
161 static inline char usart_rxempty() {
162  return usart_rxbuf_begin==usart_rxbuf_end;
163 }
164 
165 
169 static inline char usart_txempty() {
170  return usart_txbuf_begin==usart_txbuf_end;
171 }
172 
173 
177 static inline char usart_rxfull() {
178  return usart_rxbuf_end==USART_RXBUF_SIZE;
179 }
180 
181 
185 static inline char usart_txfull() {
186  return usart_txbuf_end==USART_TXBUF_SIZE;
187 }
188 
189 
193 static inline char usart_rxavail() {
194  // fn is isr save (order important)!
195  int s=(int)usart_rxbuf_end-(int)usart_rxbuf_begin;
196  if (usart_rxfull()) return USART_RXBUF_SIZE;
197  return (s<0)?(USART_RXBUF_SIZE+s):s;
198 }
199 
200 
204 static inline char usart_txfree() {
205  // fn is isr save (order important)!
206  int s=(int)usart_txbuf_end-(int)usart_txbuf_begin;
207  if (usart_txfull()) return 0;
208  return (s<0)?(-s):(USART_TXBUF_SIZE-s);
209 }
210 
211 
212 #ifdef __cplusplus
213 } // extern "C"
214 #endif
215 
216 #endif // NIBOBURGER_USART_H_
void usart_send_P(PGM_P s, unsigned int len)
void usart_send(const char *s, unsigned int len)
#define USART_TXBUF_SIZE
Definition: usart.h:60
void usart_write(const char *s)
char usart_putchar(char c)
void usart_setbaudrate(uint16_t baud)
char usart_getchar()
void usart_enable()
void usart_write_P(PGM_P s)
void usart_disable()
#define USART_RXBUF_SIZE
Definition: usart.h:66