-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusart_driver.h
294 lines (246 loc) · 9.84 KB
/
usart_driver.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
/* This file has been prepared for Doxygen automatic documentation generation.*/
/*! \file *********************************************************************
*
* \brief XMEGA USART driver header file.
*
* This file contains the function prototypes and enumerator definitions
* for various configuration parameters for the XMEGA USART driver.
*
* The driver is not intended for size and/or speed critical code, since
* most functions are just a few lines of code, and the function call
* overhead would decrease code performance. The driver is intended for
* rapid prototyping and documentation purposes for getting started with
* the XMEGA ADC module.
*
* For size and/or speed critical code, it is recommended to copy the
* function contents directly into your application instead of making
* a function call.
*
* \par Application note:
* AVR1307: Using the XMEGA USART
*
* \par Documentation
* For comprehensive code documentation, supported compilers, compiler
* settings and supported devices see readme.html
*
* \author
* Atmel Corporation: http://www.atmel.com \n
* Support email: [email protected]
*
* $Revision: 481 $
* $Date: 2007-03-06 10:12:53 +0100 (ty, 06 mar 2007) $ \n
*
* Copyright (c) 2008, Atmel Corporation All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The name of ATMEL may not be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND
* SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
#ifndef USART_DRIVER_H
#define USART_DRIVER_H
#include "avr_compiler.h"
/* USART buffer defines. */
/* \brief Receive buffer size: 2,4,8,16,32,64,128 or 256 bytes. */
#define USART_RX_BUFFER_SIZE 64
/* \brief Transmit buffer size: 2,4,8,16,32,64,128 or 256 bytes */
#define USART_TX_BUFFER_SIZE 64
/* \brief Receive buffer mask. */
#define USART_RX_BUFFER_MASK ( USART_RX_BUFFER_SIZE - 1 )
/* \brief Transmit buffer mask. */
#define USART_TX_BUFFER_MASK ( USART_TX_BUFFER_SIZE - 1 )
#if ( USART_RX_BUFFER_SIZE & USART_RX_BUFFER_MASK )
#error RX buffer size is not a power of 2
#endif
#if ( USART_TX_BUFFER_SIZE & USART_TX_BUFFER_MASK )
#error TX buffer size is not a power of 2
#endif
/* \brief USART transmit and receive ring buffer. */
typedef struct USART_Buffer
{
/* \brief Receive buffer. */
volatile uint8_t RX[USART_RX_BUFFER_SIZE];
/* \brief Transmit buffer. */
volatile uint8_t TX[USART_TX_BUFFER_SIZE];
/* \brief Receive buffer head. */
volatile uint8_t RX_Head;
/* \brief Receive buffer tail. */
volatile uint8_t RX_Tail;
/* \brief Transmit buffer head. */
volatile uint8_t TX_Head;
/* \brief Transmit buffer tail. */
volatile uint8_t TX_Tail;
} USART_Buffer_t;
/*! \brief Struct used when interrupt driven driver is used.
*
* Struct containing pointer to a usart, a buffer and a location to store Data
* register interrupt level temporary.
*/
typedef struct Usart_and_buffer
{
/* \brief Pointer to USART module to use. */
USART_t * usart;
/* \brief Data register empty interrupt level. */
USART_DREINTLVL_t dreIntLevel;
/* \brief Data buffer. */
USART_Buffer_t buffer;
} USART_data_t;
/* Macros. */
/*! \brief Macro that sets the USART frame format.
*
* Sets the frame format, Frame Size, parity mode and number of stop bits.
*
* \param _usart Pointer to the USART module
* \param _charSize The character size. Use USART_CHSIZE_t type.
* \param _parityMode The parity Mode. Use USART_PMODE_t type.
* \param _twoStopBits Enable two stop bit mode. Use bool type.
*/
#define USART_Format_Set(_usart, _charSize, _parityMode, _twoStopBits) \
(_usart)->CTRLC = (uint8_t) _charSize | _parityMode | \
(_twoStopBits ? USART_SBMODE_bm : 0)
/*! \brief Set USART baud rate.
*
* Sets the USART's baud rate register.
*
* UBRR_Value : Value written to UBRR
* ScaleFactor : Time Base Generator Scale Factor
*
* Equation for calculation of BSEL value in asynchronous normal speed mode:
* If ScaleFactor >= 0
* BSEL = ((I/O clock frequency)/(2^(ScaleFactor)*16*Baudrate))-1
* If ScaleFactor < 0
* BSEL = (1/(2^(ScaleFactor)*16))*(((I/O clock frequency)/Baudrate)-1)
*
* \note See XMEGA manual for equations for calculation of BSEL value in other
* modes.
*
* \param _usart Pointer to the USART module.
* \param _bselValue Value to write to BSEL part of Baud control register.
* Use uint16_t type.
* \param _bScaleFactor USART baud rate scale factor.
* Use uint8_t type
*/
#define USART_Baudrate_Set(_usart, _bselValue, _bScaleFactor) \
(_usart)->BAUDCTRLA =(uint8_t)_bselValue; \
(_usart)->BAUDCTRLB =(_bScaleFactor << USART_BSCALE0_bp)|(_bselValue >> 8)
/*! \brief Enable USART receiver.
*
* \param _usart Pointer to the USART module
*/
#define USART_Rx_Enable(_usart) ((_usart)->CTRLB |= USART_RXEN_bm)
/*! \brief Disable USART receiver.
*
* \param _usart Pointer to the USART module.
*/
#define USART_Rx_Disable(_usart) ((_usart)->CTRLB &= ~USART_RXEN_bm)
/*! \brief Enable USART transmitter.
*
* \param _usart Pointer to the USART module.
*/
#define USART_Tx_Enable(_usart) ((_usart)->CTRLB |= USART_TXEN_bm)
/*! \brief Disable USART transmitter.
*
* \param _usart Pointer to the USART module.
*/
#define USART_Tx_Disable(_usart) ((_usart)->CTRLB &= ~USART_TXEN_bm)
/*! \brief Set USART RXD interrupt level.
*
* Sets the interrupt level on RX Complete interrupt.
*
* \param _usart Pointer to the USART module.
* \param _rxdIntLevel Interrupt level of the RXD interrupt.
* Use USART_RXCINTLVL_t type.
*/
#define USART_RxdInterruptLevel_Set(_usart, _rxdIntLevel) \
((_usart)->CTRLA = ((_usart)->CTRLA & ~USART_RXCINTLVL_gm) | _rxdIntLevel)
/*! \brief Set USART TXD interrupt level.
*
* Sets the interrupt level on TX Complete interrupt.
*
* \param _usart Pointer to the USART module.
* \param _txdIntLevel Interrupt level of the TXD interrupt.
* Use USART_TXCINTLVL_t type.
*/
#define USART_TxdInterruptLevel_Set(_usart, _txdIntLevel) \
(_usart)->CTRLA = ((_usart)->CTRLA & ~USART_TXCINTLVL_gm) | _txdIntLevel
/*! \brief Set USART DRE interrupt level.
*
* Sets the interrupt level on Data Register interrupt.
*
* \param _usart Pointer to the USART module.
* \param _dreIntLevel Interrupt level of the DRE interrupt.
* Use USART_DREINTLVL_t type.
*/
#define USART_DreInterruptLevel_Set(_usart, _dreIntLevel) \
(_usart)->CTRLA = ((_usart)->CTRLA & ~USART_DREINTLVL_gm) | _dreIntLevel
/*! \brief Set the mode the USART run in.
*
* Set the mode the USART run in. The default mode is asynchronous mode.
*
* \param _usart Pointer to the USART module register section.
* \param _usartMode Selects the USART mode. Use USART_CMODE_t type.
*
* USART modes:
* - 0x0 : Asynchronous mode.
* - 0x1 : Synchronous mode.
* - 0x2 : IrDA mode.
* - 0x3 : Master SPI mode.
*/
#define USART_SetMode(_usart, _usartMode) \
((_usart)->CTRLC = ((_usart)->CTRLC & (~USART_CMODE_gm)) | _usartMode)
/*! \brief Check if data register empty flag is set.
*
* \param _usart The USART module.
*/
#define USART_IsTXDataRegisterEmpty(_usart) (((_usart)->STATUS & USART_DREIF_bm) != 0)
/*! \brief Put data (5-8 bit character).
*
* Use the macro USART_IsTXDataRegisterEmpty before using this function to
* put data to the TX register.
*
* \param _usart The USART module.
* \param _data The data to send.
*/
#define USART_PutChar(_usart, _data) ((_usart)->DATA = _data)
/*! \brief Checks if the RX complete interrupt flag is set.
*
* Checks if the RX complete interrupt flag is set.
*
* \param _usart The USART module.
*/
#define USART_IsRXComplete(_usart) (((_usart)->STATUS & USART_RXCIF_bm) != 0)
/*! \brief Get received data (5-8 bit character).
*
* This macro reads out the RX register.
* Use the macro USART_RX_Complete to check if anything is received.
*
* \param _usart The USART module.
*
* \retval Received data.
*/
#define USART_GetChar(_usart) ((_usart)->DATA)
/* Functions for interrupt driven driver. */
void USART_InterruptDriver_Initialize(USART_data_t * usart_data, USART_t * usart, USART_DREINTLVL_t dreIntLevel);
bool USART_TXBuffer_PutByte(USART_data_t * usart_data, uint8_t data);
void USART_DataRegEmpty(USART_data_t * usart_data);
void USART_init(USART_t * usart);
#endif