avruart
uart.h
Go to the documentation of this file.
1 /*
2  * uartavr interrupt driven serial communication for 8bit avrs
3  * Copyright © 2016 Christian Rapp
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of the organization nor the
17  * names of its contributors may be used to endorse or promote products
18  * derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY ''AS IS'' AND ANY EXPRESS OR IMPLIED
21  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23  * DISCLAIMED. IN NO EVENT SHALL yourname BE LIABLE FOR ANY
24  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING 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 #ifndef UART_H
33 #define UART_H
34 
35 /*
36  * Make sure we have defined F_CPU before we include anything. This is very
37  * important and you must change this value according to your needs. The easiest
38  * way to do this is to use this in a Makefile
39  * -DF_CPU=1000000
40  */
41 #ifndef F_CPU
42 #define F_CPU 16000000UL /* 16MHz µc */
43 #endif /* ifndef F_CPU */
44 #ifndef BAUD
45 #define BAUD 9600L
46 #endif /* ifndef BAUD */
47 
48 /*
49  * Some basic includes
50  */
51 #include <inttypes.h>
52 #include <stdlib.h>
53 #ifdef PRINTF
54 #include <stdio.h>
55 #endif
56 
57 #include <avr/io.h>
58 #include <util/atomic.h>
59 #include <util/setbaud.h>
60 
85 #define UARTAVR_VERSION_MAJOR 0
86 
89 #define UARTAVR_VERSION_MINOR 3
90 
93 #define UARTAVR_VERSION_PATCH 0
94 
102 #define CR "\n\r"
103 
108 #define CR_PRINTF "\r"
109 
113 #define BUFFSIZE 64
114 
123 struct DirBuff {
124  char buff[BUFFSIZE];
126  char *start_ptr;
127  char *end_ptr;
128  char *inpos_ptr;
129  char *outpos_ptr;
130  size_t items;
131  uint8_t full;
133  void (*rx_callback)(void);
135  void (*tx_callback)(void);
136  void (*buff_empty)(void);
137 };
138 
142 enum DIR_BUFFS {
145 };
146 
150 struct CBuffer {
151  struct DirBuff rx_buff;
152  struct DirBuff tx_buff;
153 } cb;
155 /* TODO: I am not sure if this instance of CBuffer needs to be volatile. But if I
156 * do this I get lots of compiler warnings.
157 */
158 
164 struct UARTcfg {
165  uint8_t tx;
166  uint8_t rx;
167  void (*rx_callback)(void);
169  void (*tx_callback)(void);
171  void (*buff_empty)(void);
173 };
174 
181 void cb_init(void);
182 
189 void get_direction_buffer(enum DIR_BUFFS dir, struct DirBuff **dbuff);
190 
199 uint8_t cb_pop(char *c, enum DIR_BUFFS dir);
208 uint8_t cp_push(char c, enum DIR_BUFFS dir);
209 
215 void init_uart_cfg(struct UARTcfg *cfg);
216 
230 void init_UART(const struct UARTcfg *cfg);
231 
232 #ifdef LIB_DEBUG
233 void put_noi_UART(char c);
234 void puts_noi_UART(const char *s);
235 #endif /* LIB_DEBUG */
236 
242 void put_UART(const char c);
243 
253 void puts_UART(const char *s);
254 
262 uint8_t get_UART(char *s);
274 uint8_t gets_UART(char *s);
275 
276 #ifdef PRINTF
277 
293 int puts_printf_UART(char c, FILE *stream);
294 #pragma GCC diagnostic push
295 #pragma GCC diagnostic ignored "-Wunused-variable"
296 
305 static FILE uartavr_stdout =
306  FDEV_SETUP_STREAM(puts_printf_UART, NULL, _FDEV_SETUP_WRITE);
307 #pragma GCC diagnostic pop
308 #endif
309 
310 #endif /* ifndef UART_H */
void(* buff_empty)(void)
Definition: uart.h:136
uint8_t full
Definition: uart.h:131
size_t items
Definition: uart.h:130
Definition: uart.h:144
Definition: uart.h:143
uint8_t gets_UART(char *s)
Get all data from the circular buffer.
Definition: uart.c:229
struct CBuffer cb
uint8_t cp_push(char c, enum DIR_BUFFS dir)
Put one byte in the circular buffer.
void(* rx_callback)(void)
Definition: uart.h:133
char buff[BUFFSIZE]
Definition: uart.h:124
void get_direction_buffer(enum DIR_BUFFS dir, struct DirBuff **dbuff)
Get a direction buffer struct from CBuffer.
Definition: uart.c:50
char * start_ptr
Definition: uart.h:126
void put_UART(const char c)
Send a single character.
Definition: uart.c:202
void cb_init(void)
Initializes the circular buffer structure.
Definition: uart.c:34
char * outpos_ptr
Definition: uart.h:129
#define BUFFSIZE
The buffer size for the RX and TX buffers.
Definition: uart.h:113
char * inpos_ptr
Definition: uart.h:128
void init_UART(const struct UARTcfg *cfg)
Initialize the UART on the microcontroller.
Definition: uart.c:151
uint8_t cb_pop(char *c, enum DIR_BUFFS dir)
Get one byte from the circular buffer.
Definition: uart.c:63
uint8_t tx
Definition: uart.h:165
A struct to configure the UART.
Definition: uart.h:164
This holds the circular buffers.
Definition: uart.h:150
void puts_UART(const char *s)
Write a string to the UART buffer.
Definition: uart.c:208
void(* tx_callback)(void)
Definition: uart.h:135
char * end_ptr
Definition: uart.h:127
Presenting a circular buffer.
Definition: uart.h:123
DIR_BUFFS
Identifier for direction buffer.
Definition: uart.h:142
void init_uart_cfg(struct UARTcfg *cfg)
Init a cfg struct with the default values.
Definition: uart.c:139
int puts_printf_UART(char c, FILE *stream)
The FILE stream method for uartavr_stdout.
Definition: uart.c:247
uint8_t rx
Definition: uart.h:166
uint8_t get_UART(char *s)
Retrieve one char from the buffer.
Definition: uart.c:221