NiboRoboLib 3.6 - NIBObee 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 
39 #ifndef NIBOBEE_USART_H_
40 #define NIBOBEE_USART_H_
41 
42 #include <stdint.h>
43 #include <avr/pgmspace.h>
44 
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48 
49 #define USART_TXBUF_SIZE 16
50 #define USART_RXBUF_SIZE 8
51 
55 void usart_setbaudrate(uint16_t baud);
56 
57 extern uint8_t usart_txbuf[USART_TXBUF_SIZE];
58 extern uint8_t usart_rxbuf[USART_RXBUF_SIZE];
59 
60 extern volatile uint8_t usart_txbuf_begin;
61 extern volatile uint8_t usart_txbuf_end;
62 
63 extern volatile uint8_t usart_rxbuf_begin;
64 extern volatile uint8_t usart_rxbuf_end;
65 
69 void usart_enable();
70 
74 void usart_disable();
75 
76 
81 char usart_getchar();
82 
83 
88 char usart_putchar(char c);
89 
90 
95 void usart_write(const char * s);
96 
97 
102 void usart_write_P(PGM_P s);
103 
104 
109 void usart_send(const char * s, unsigned int len);
110 
111 
116 void usart_send_P(PGM_P s, unsigned int len);
117 
118 
119 
123 static inline char usart_rxempty() {
124  return usart_rxbuf_begin==usart_rxbuf_end;
125 }
126 
127 
131 static inline char usart_txempty() {
132  return usart_txbuf_begin==usart_txbuf_end;
133 }
134 
135 
139 static inline char usart_rxfull() {
140  return usart_rxbuf_end==USART_RXBUF_SIZE;
141 }
142 
143 
147 static inline char usart_txfull() {
148  return usart_txbuf_end==USART_TXBUF_SIZE;
149 }
150 
151 
155 static inline char usart_rxavail() {
156  // fn is isr save (order important)!
157  int s=(int)usart_rxbuf_end-(int)usart_rxbuf_begin;
158  if (usart_rxfull()) return USART_RXBUF_SIZE;
159  return (s<0)?(USART_RXBUF_SIZE+s):s;
160 }
161 
162 
166 static inline char usart_txfree() {
167  // fn is isr save (order important)!
168  int s=(int)usart_txbuf_end-(int)usart_txbuf_begin;
169  if (usart_txfull()) return 0;
170  return (s<0)?(-s):(USART_TXBUF_SIZE-s);
171 }
172 
173 
174 #ifdef __cplusplus
175 } // extern "C"
176 #endif
177 
178 #endif // NIBOBEE_USART_H_
void usart_send_P(PGM_P s, unsigned int len)
void usart_send(const char *s, unsigned int len)
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()