-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUART.h
345 lines (261 loc) · 9.8 KB
/
UART.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/****************************************************************************
*
* Copyright (c) 2006 Dave Hylands <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* Alternatively, this software may be distributed under the terms of BSD
* license.
*
* See README and COPYING for more details.
*
****************************************************************************/
/**
*
* @file UART.h
*
* @brief This file implements the interrupt drive UART class.
*
****************************************************************************/
#if !defined( UART_H )
#define UART_H ///< Include Guard
// ---- Include Files -------------------------------------------------------
#include <inttypes.h>
#include <avr/io.h>
#include "CBUF.h"
#include "Config.h"
#if defined( __AVR_LIBC_VERSION__ )
# include <stdio.h>
#endif
#if defined( __cplusplus )
extern "C"
{
#endif
//---------------------------------------------------------------------------
//
// To use this header file the following #defines should be made in Config.h
// Replace the x by a 0 or a 1.
//
// CFG_USE_UARTx 0 or 1 Controls if anything relating to that UART is generated
//
// CFG_UARTx_RX_BUFFER_SIZE How many bytes to put in the Rx buffer.
// Setting the buffer size to zero, means that
// no buffers will be alllocated and a callback
// must be provided to deal with each received
// character.
//
// CFG_UARTx_TX_BUFFER_SIZE How many bytes to put in the Tx buffer.
// Setting this to zero will cause polled routines
// to be used.
//
// CFG_UARTx_ECHO_INPUT causes data received by UARTx_GetChar to be
// echoed. This is useful if the uart is connected
// to s termnal program and being used to prompt
// the user for input.
//
// CFG_UARTx_CR_TO_LF Translate received CRs to LFs. This allows
// the input to be compatible with fgets
//
// CFG_UARTx_LF_TO_CRLF Translate sent LFs to CRLFs. Useful when
// using a terminal program.
//
// API Provided:
//
// int UARTx_GetChar( void );
// int UARTx_IsCharAvailable();
// void UARTx_SetRxHandler( void (*rxHandler)( uint8_t ch ));
// int UARTx_PutChar( char ch );
// void UARTx_PutStr( const char *s );
// void UARTx_Write( const void *data, uint8_t len );
//
// Note: To allow the code to be compatible with single UART processors
// CFG_USE_UART can be defined instead of CFG_USE_UARTx and then
// The x (the uart number) can be dropped.
//
//---------------------------------------------------------------------------
#if defined( CFG_USE_UART )
# if defined( CFG_USE_UART0 ) || defined( CFG_USE_UART1 )
# error CFG_USE_UART is incompatible with CFG_USE_UART0 or CFG_USE_UART1
# endif
# if !defined( CFG_UART_RX_BUFFER_SIZE )
# error CFG_UART_RX_BUFFER_SIZE isnt defined.
# endif
# if !defined( CFG_UART_TX_BUFFER_SIZE )
# error CFG_UART_TX_BUFFER_SIZE isnt declared.
# endif
# define CFG_USE_UART0 CFG_USE_UART
# define CFG_UART0_RX_BUFFER_SIZE CFG_UART_RX_BUFFER_SIZE
# define CFG_UART0_TX_BUFFER_SIZE CFG_UART_TX_BUFFER_SIZE
# define CFG_UART0_LF_TO_CRLF CFG_UART_LF_TO_CRLF
# define CFG_UART0_ECHO_INPUT CFG_UART_ECHO_INPUT
# define CFG_UART0_CR_TO_LF CFG_UART_CR_TO_LF
# define UART_GetChar UART0_GetChar
# define UART_IsCharAvailable UART0_IsCharAvailable
# define UART_SetRxHandler UART0_SetRxHandler
# define UART_PutChar UART0_PutChar
# define UART_PutStr UART0_PutStr
# define UART_Write UART0_Write
# define UART_GetCharStdio UART0_GetCharStdio
# define UART_PutCharStdio UART0_PutCharStdio
#endif // CFG_USE_UART
#if !defined( CFG_USE_UART0 ) && !defined( CFG_USE_UART1 )
# error Expecting one of CFG_USE_UART0 or CFG_USE_UART1 to be defined
#endif
#if CFG_USE_UART0
// These defines allow this file to work with AVRs which only have one UART
// (i.e ATMega8, 16, 32)
#if !defined( UCSR0A )
#define UCSR0A UCSRA
#define UCSR0B UCSRB
#define UCSR0C UCSRC
#define UBRR0H UBRRH
#define UBRR0L UBRRL
#define UDR0 UDR
#define UDRIE0 UDRIE
#define UDRE0 UDRE
#endif
#if defined( SIG_UART_RECV )
# define SIG_USART0_RECV SIG_UART_RECV
# define SIG_USART0_DATA SIG_UART_DATA
#elif defined( SIG_USART_RECV )
# define SIG_USART0_RECV SIG_USART_RECV
# define SIG_USART0_DATA SIG_USART_DATA
#elif !defined( SIG_USART0_RECV )
# define SIG_USART0_RECV SIG_USART_RECV
# define SIG_USART0_DATA SIG_USART_DATA
#endif
//---------------------------------------------------------------------------
#if !defined( CFG_UART0_RX_BUFFER_SIZE )
# error CFG_UART0_RX_BUFFER_SIZE isnt defined.
#endif
#if (( CFG_UART0_RX_BUFFER_SIZE > 0 ) && (( CFG_UART0_RX_BUFFER_SIZE & ( CFG_UART0_RX_BUFFER_SIZE - 1 )) != 0 ))
# error CFG_UART0_RX_BUFFER_SIZE isnt a power of 2.
#endif
#if ( CFG_UART0_RX_BUFFER_SIZE > 128 )
typedef uint16_t UART0_RxIndex_t;
#else
typedef uint8_t UART0_RxIndex_t;
#endif
#if ( CFG_UART0_RX_BUFFER_SIZE > 0 )
typedef struct
{
UART0_RxIndex_t m_getIdx;
UART0_RxIndex_t m_putIdx;
uint8_t m_entry[ CFG_UART0_RX_BUFFER_SIZE ];
} UART0_RxBuffer_t;
#if defined( __AVR_LIBC_VERSION__ )
int UART0_GetCharStdio( FILE *fs );
#else
#define UART0_GetCharStdio UART0_GetChar
#endif
int UART0_GetChar( void );
#define gUart0RxBuf_SIZE sizeof( gUart0RxBuf.m_entry )
extern volatile UART0_RxBuffer_t gUart0RxBuf;
#define UART0_IsCharAvailable() (!CBUF_IsEmpty( gUart0RxBuf ))
#else
void UART0_SetRxHandler( void (*rxHandler)( uint8_t ch ));
#endif
//---------------------------------------------------------------------------
#if !defined( CFG_UART0_TX_BUFFER_SIZE )
# error CFG_UART0_TX_BUFFER_SIZE isnt declared.
#endif
#if (( CFG_UART0_TX_BUFFER_SIZE & ( CFG_UART0_TX_BUFFER_SIZE - 1 )) != 0 )
# error CFG_UART0_TX_BUFFER_SIZE isnt a power of 2.
#endif
#if ( CFG_UART0_TX_BUFFER_SIZE > 128 )
typedef uint16_t UART0_TxIndex_t;
#else
typedef uint8_t UART0_TxIndex_t;
#endif
#if ( CFG_UART0_TX_BUFFER_SIZE > 0 )
typedef struct
{
UART0_TxIndex_t m_getIdx;
UART0_TxIndex_t m_putIdx;
uint8_t m_entry[ CFG_UART0_TX_BUFFER_SIZE ];
} UART0_TxBuffer_t;
#define gUart0TxBuf_SIZE sizeof( gUart0TxBuf.m_entry )
extern volatile UART0_TxBuffer_t gUart0TxBuf;
#endif
//---------------------------------------------------------------------------
#if defined( __AVR_LIBC_VERSION__ )
int UART0_PutCharStdio( char ch, FILE *fs );
#else
#define UART0_PutCharStdio UART0_PutChar
#endif
int UART0_PutChar( char ch );
void UART0_PutStr( const char *s );
void UART0_Write( const void *data, uint8_t len );
#endif // CFG_USE_UART0
//===========================================================================
#if CFG_USE_UART1
//---------------------------------------------------------------------------
#if !defined( CFG_UART1_RX_BUFFER_SIZE )
# error CFG_UART1_RX_BUFFER_SIZE isnt defined.
#endif
#if (( CFG_UART1_RX_BUFFER_SIZE > 0 ) && (( CFG_UART1_RX_BUFFER_SIZE & ( CFG_UART1_RX_BUFFER_SIZE - 1 )) != 0 ))
# error CFG_UART1_RX_BUFFER_SIZE isnt a power of 2.
#endif
#if ( CFG_UART1_RX_BUFFER_SIZE > 128 )
typedef uint16_t UART1_RxIndex_t;
#else
typedef uint8_t UART1_RxIndex_t;
#endif
#if ( CFG_UART1_RX_BUFFER_SIZE > 0 )
typedef struct
{
UART1_RxIndex_t m_getIdx;
UART1_RxIndex_t m_putIdx;
uint8_t m_entry[ CFG_UART1_RX_BUFFER_SIZE ];
} UART1_RxBuffer_t;
#if defined( __AVR_LIBC_VERSION__ )
int UART1_GetCharStdio( FILE *fs );
#else
#define UART1_GetCharStdio UART1_GetChar
#endif
int UART1_GetChar( void );
#define gUart1RxBuf_SIZE sizeof( gUart1RxBuf.m_entry )
extern volatile UART1_RxBuffer_t gUart1RxBuf;
#define UART1_IsCharAvailable() (!CBUF_IsEmpty( gUart1RxBuf ))
#else
void UART1_SetRxHandler( void (*rxHandler)( uint8_t ch ));
#endif
//---------------------------------------------------------------------------
#if !defined( CFG_UART1_TX_BUFFER_SIZE )
# error CFG_UART1_TX_BUFFER_SIZE isnt declared.
#endif
#if (( CFG_UART1_TX_BUFFER_SIZE & ( CFG_UART1_TX_BUFFER_SIZE - 1 )) != 0 )
# error CFG_UART1_TX_BUFFER_SIZE isnt a power of 2.
#endif
#if ( CFG_UART1_TX_BUFFER_SIZE > 128 )
typedef uint16_t UART1_TxIndex_t;
#else
typedef uint8_t UART1_TxIndex_t;
#endif
#if ( CFG_UART1_TX_BUFFER_SIZE > 0 )
typedef struct
{
UART1_TxIndex_t m_getIdx;
UART1_TxIndex_t m_putIdx;
uint8_t m_entry[ CFG_UART1_TX_BUFFER_SIZE ];
} UART1_TxBuffer_t;
#define gUart1TxBuf_SIZE sizeof( gUart1TxBuf.m_entry )
extern volatile UART1_TxBuffer_t gUart1TxBuf;
#endif
//---------------------------------------------------------------------------
#if defined( __AVR_LIBC_VERSION__ )
int UART1_PutCharStdio( char ch, FILE *fs );
#else
#define UART1_PutCharStdio UART1_PutChar
#endif
int UART1_PutChar( char ch );
void UART1_PutStr( const char *s );
void UART1_Write( const void *data, uint8_t len );
#endif // CFG_USE_UART1
#if defined( __cplusplus )
}
#endif
/** @} */
#endif // UART_H