Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2136 - 1
/************************************************************************
2
Title:    Interrupt UART library with receive/transmit circular buffers
3
Author:   Peter Fleury <pfleury@gmx.ch>   http://jump.to/fleury
4
File:     $Id: uart.h,v 1.8.2.1 2007/07/01 11:14:38 peter Exp $
5
Software: AVR-GCC 4.1, AVR Libc 1.4
6
Hardware: any AVR with built-in UART, tested on AT90S8515 & ATmega8 at 4 Mhz
7
License:  GNU General Public License
8
Usage:    see Doxygen manual
9
 
10
LICENSE:
11
    Copyright (C) 2006 Peter Fleury
12
 
13
    This program is free software; you can redistribute it and/or modify
14
    it under the terms of the GNU General Public License as published by
15
    the Free Software Foundation; either version 2 of the License, or
16
    any later version.
17
 
18
    This program is distributed in the hope that it will be useful,
19
    but WITHOUT ANY WARRANTY; without even the implied warranty of
20
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
    GNU General Public License for more details.
22
 
23
************************************************************************/
24
//############################################################################
25
//# HISTORY  uart1.h
26
//#
27
//# 06.08.2015 CB
28
//# - add: uint8_t receiveNMEA              // Flag zur Empfangssteuerung in ISR(USART1_RX_vect)
29
//# - chg: UART_RX_BUFFER_SIZE war 1024, darf aber nur max 256 sein
30
//#
31
//# 26.06.2014 OG
32
//# - del: receiveNMEA
33
//#
34
//# 30.03.2013 CB
35
//# - add: uart1_flush(void); in Headerdatei aufgenommen
36
//###########################################################################
37
//
38
//  @defgroup pfleury_uart UART Library
39
//  @code #include <uart.h> @endcode
40
//
41
//  @brief Interrupt UART library using the built-in UART with transmit and receive circular buffers.
42
//
43
//  This library can be used to transmit and receive data through the built in UART.
44
//
45
//  An interrupt is generated when the UART has finished transmitting or
46
//  receiving a byte. The interrupt handling routines use circular buffers
47
//  for buffering received and transmitted data.
48
//
49
//  The UART_RX_BUFFER_SIZE and UART_TX_BUFFER_SIZE constants define
50
//  the size of the circular buffers in bytes. Note that these constants must be a power of 2.
51
//  You may need to adapt this constants to your target and your application by adding
52
//  CDEFS += -DUART_RX_BUFFER_SIZE=nn -DUART_RX_BUFFER_SIZE=nn to your Makefile.
53
//
54
//  @note Based on Atmel Application Note AVR306
55
//  @author Peter Fleury pfleury@gmx.ch  http://jump.to/fleury
56
//
57
 
58
#ifndef UART_H
59
#define UART_H
60
 
61
#if (__GNUC__ * 100 + __GNUC_MINOR__) < 304
62
#error "This library requires AVR-GCC 3.4 or later, update to newer AVR-GCC compiler !"
63
#endif
64
 
65
 
66
// constants and macros
67
 
68
 
69
// @brief  UART Baudrate Expression
70
//  @param  xtalcpu  system clock in Mhz, e.g. 4000000L for 4Mhz
71
//  @param  baudrate baudrate in bps, e.g. 1200, 2400, 9600
72
//
73
#define UART_BAUD_SELECT(baudRate,xtalCpu) ((xtalCpu)/((baudRate)*16l)-1)
74
 
75
// @brief  UART Baudrate Expression for ATmega double speed mode
76
//  @param  xtalcpu  system clock in Mhz, e.g. 4000000L for 4Mhz
77
//  @param  baudrate baudrate in bps, e.g. 1200, 2400, 9600
78
//
79
#define UART_BAUD_SELECT_DOUBLE_SPEED(baudRate,xtalCpu) (((xtalCpu)/((baudRate)*8l)-1)|0x8000)
80
 
81
 
82
// Size of the circular receive buffer, must be power of 2
83
#ifndef UART_RX_BUFFER_SIZE
84
#define UART_RX_BUFFER_SIZE 256         /* 2,4,8,16,32,64,128 or 256 bytes */
85
#endif
86
 
87
// Size of the circular transmit buffer, must be power of 2
88
#ifndef UART_TX_BUFFER_SIZE
89
#define UART_TX_BUFFER_SIZE 64          /* 2,4,8,16,32,64,128 or 256 bytes */
90
#endif
91
 
92
// test if the size of the circular buffers fits into SRAM
93
#if ( (UART_RX_BUFFER_SIZE+UART_TX_BUFFER_SIZE) >= (RAMEND-0x60 ) )
94
#error "size of UART_RX_BUFFER_SIZE + UART_TX_BUFFER_SIZE larger than size of SRAM"
95
#endif
96
 
97
//global variable
98
extern volatile uint16_t UART1_RxError;
99
extern volatile uint8_t  UART1_receiveNMEA;             // Flag zur Empfangssteuerung in ISR(USART1_RX_vect)
100
 
101
 
102
// high byte error return code of uart_getc()
103
 
104
#define UART_FRAME_ERROR      0x0800              // Framing Error by UART
105
#define UART_OVERRUN_ERROR    0x0400              // Overrun condition by UART
106
#define UART_BUFFER_OVERFLOW  0x0200              // receive ringbuffer overflow
107
#define UART_NO_DATA          0x0100              // no receive data available
108
 
109
#define TRACKING_RSSI           1
110
#define TRACKING_GPS            2
111
#define TRACKING_MKCOCKPIT      3
112
#define TRACKING_NMEA           4
113
#define NMEA_receive            5               // Flag zur Empfangssteuerung in ISR(USART1_RX_vect)
114
//
115
// function prototypes
116
//
117
 
118
//
119
//   @brief   Initialize UART and set baudrate
120
//   @param   baudrate Specify baudrate using macro UART_BAUD_SELECT()
121
//   @return  none
122
//
123
extern void uart_init(unsigned int baudrate);
124
 
125
 
126
//
127
//  @brief   Get received byte from ringbuffer
128
//
129
// Returns in the lower byte the received character and in the
130
// higher byte the last receive error.
131
// UART_NO_DATA is returned when no data is available.
132
//
133
//  @param   void
134
//  @return  lower byte:  received byte from ringbuffer
135
//  @return  higher byte: last receive status
136
//           - \b 0 successfully received data from UART
137
//           - \b UART_NO_DATA
138
//             <br>no receive data available
139
//           - \b UART_BUFFER_OVERFLOW
140
//             <br>Receive ringbuffer overflow.
141
//             We are not reading the receive buffer fast enough,
142
//             one or more received character have been dropped
143
//           - \b UART_OVERRUN_ERROR
144
//             <br>Overrun condition by UART.
145
//             A character already present in the UART UDR register was
146
//             not read by the interrupt handler before the next character arrived,
147
//             one or more received characters have been dropped.
148
//           - \b UART_FRAME_ERROR
149
//             <br>Framing Error by UART
150
//
151
extern unsigned int uart_getc(void);
152
 
153
 
154
//
155
//  @brief   Put byte to ringbuffer for transmitting via UART
156
//  @param   data byte to be transmitted
157
//  @return  none
158
//
159
 
160
// @brief  Set Baudrate USART1
161
extern void SetBaudUart1(uint8_t Baudrate);
162
 
163
// @brief  Initialize USART1 (only available on selected ATmegas) @see uart_init
164
extern void uart1_init(unsigned int baudrate);
165
 
166
// @brief  Get received byte of USART1 from ringbuffer. (only available on selected ATmega) @see uart_getc
167
extern unsigned int uart1_getc(void);
168
 
169
// @brief  Put byte to ringbuffer for transmitting via USART1 (only available on selected ATmega) @see uart_putc
170
//extern void uart1_putc(unsigned char data);
171
extern int uart1_putc(unsigned char data);
172
 
173
// @brief  Put string to ringbuffer for transmitting via USART1 (only available on selected ATmega) @see uart_puts
174
extern void uart1_puts(const char *s );
175
 
176
// @brief  Put string from program memory to ringbuffer for transmitting via USART1 (only available on selected ATmega) @see uart_puts_p
177
extern void uart1_puts_p(const char *s );
178
 
179
// @brief  Macro to automatically put a string constant into program memory
180
#define uart1_puts_P(__s)       uart1_puts_p(PSTR(__s))
181
 
182
extern char *utoa1(char* buffer, const unsigned int size, unsigned int value);
183
 
184
extern uint16_t uart1_available(void);
185
 
186
extern int uart1_peek(void);
187
 
188
// @brief   Flush bytes waiting the receive buffer.  Acutally ignores them.
189
extern void uart1_flush(void);
190
 
191
 
192
#endif // UART_H
193
 
194