| BLDC control on ATAVRMC303 with ATxMega128A1

usart_driver.h
Go to the documentation of this file.
1 /* This file has been prepared for Doxygen automatic documentation generation.*/
59 #ifndef USART_DRIVER_H
60 #define USART_DRIVER_H
61
62 #include "avr_compiler.h"
63
64 /* USART buffer defines. */
65
66 /* \brief Receive buffer size: 2,4,8,16,32,64,128 or 256 bytes. */
67 #define USART_RX_BUFFER_SIZE 4
68 /* \brief Transmit buffer size: 2,4,8,16,32,64,128 or 256 bytes */
69 #define USART_TX_BUFFER_SIZE 4
70 /* \brief Receive buffer mask. */
71 #define USART_RX_BUFFER_MASK ( USART_RX_BUFFER_SIZE - 1 )
72 /* \brief Transmit buffer mask. */
73 #define USART_TX_BUFFER_MASK ( USART_TX_BUFFER_SIZE - 1 )
74
75
76 #if ( USART_RX_BUFFER_SIZE & USART_RX_BUFFER_MASK )
77 #error RX buffer size is not a power of 2
78 #endif
79 #if ( USART_TX_BUFFER_SIZE & USART_TX_BUFFER_MASK )
80 #error TX buffer size is not a power of 2
81 #endif
82
83
84 /* \brief USART transmit and receive ring buffer. */
85 typedef struct USART_Buffer
86 {
87 /* \brief Receive buffer. */
88 volatile uint8_t RX[USART_RX_BUFFER_SIZE];
89 /* \brief Transmit buffer. */
90 volatile uint8_t TX[USART_TX_BUFFER_SIZE];
91 /* \brief Receive buffer head. */
92 volatile uint8_t RX_Head;
93 /* \brief Receive buffer tail. */
94 volatile uint8_t RX_Tail;
95 /* \brief Transmit buffer head. */
96 volatile uint8_t TX_Head;
97 /* \brief Transmit buffer tail. */
98 volatile uint8_t TX_Tail;
99 } USART_Buffer_t;
100
101
107 typedef struct Usart_and_buffer
108 {
109 /* \brief Pointer to USART module to use. */
110 USART_t * usart;
111 /* \brief Data register empty interrupt level. */
112 USART_DREINTLVL_t dreIntLevel;
113 /* \brief Data buffer. */
114 USART_Buffer_t buffer;
115 } USART_data_t;
116
117
118 /* Macros. */
119
129 #define USART_Format_Set(_usart, _charSize, _parityMode, _twoStopBits) \
130 (_usart)->CTRLC = (uint8_t) _charSize | _parityMode | \
131 (_twoStopBits ? USART_SBMODE_bm : 0)
132
133
156 #define USART_Baudrate_Set(_usart, _bselValue, _bScaleFactor) \
157 (_usart)->BAUDCTRLA =(uint8_t)_bselValue; \
158 (_usart)->BAUDCTRLB =(_bScaleFactor << USART_BSCALE0_bp)|(_bselValue >> 8)
159
160
165 #define USART_Rx_Enable(_usart) ((_usart)->CTRLB |= USART_RXEN_bm)
166
167
172 #define USART_Rx_Disable(_usart) ((_usart)->CTRLB &= ~USART_RXEN_bm)
173
174
179 #define USART_Tx_Enable(_usart) ((_usart)->CTRLB |= USART_TXEN_bm)
180
181
186 #define USART_Tx_Disable(_usart) ((_usart)->CTRLB &= ~USART_TXEN_bm)
187
188
197 #define USART_RxdInterruptLevel_Set(_usart, _rxdIntLevel) \
198 ((_usart)->CTRLA = ((_usart)->CTRLA & ~USART_RXCINTLVL_gm) | _rxdIntLevel)
199
200
209 #define USART_TxdInterruptLevel_Set(_usart, _txdIntLevel) \
210 (_usart)->CTRLA = ((_usart)->CTRLA & ~USART_TXCINTLVL_gm) | _txdIntLevel
211
212
213
222 #define USART_DreInterruptLevel_Set(_usart, _dreIntLevel) \
223 (_usart)->CTRLA = ((_usart)->CTRLA & ~USART_DREINTLVL_gm) | _dreIntLevel
224
225
239 #define USART_SetMode(_usart, _usartMode) \
240 ((_usart)->CTRLC = ((_usart)->CTRLC & (~USART_CMODE_gm)) | _usartMode)
241
242
243
248 #define USART_IsTXDataRegisterEmpty(_usart) (((_usart)->STATUS & USART_DREIF_bm) != 0)
249
250
251
260 #define USART_PutChar(_usart, _data) ((_usart)->DATA = _data)
261
262
263
270 #define USART_IsRXComplete(_usart) (((_usart)->STATUS & USART_RXCIF_bm) != 0)
271
272
273
274
284 #define USART_GetChar(_usart) ((_usart)->DATA)
285
286
287 /* Functions for interrupt driven driver. */
288 void USART_InterruptDriver_Initialize(USART_data_t * usart_data,
289 USART_t * usart,
290 USART_DREINTLVL_t dreIntLevel );
291
292 void USART_InterruptDriver_DreInterruptLevel_Set(USART_data_t * usart_data,
293 USART_DREINTLVL_t dreIntLevel);
294
295 bool USART_TXBuffer_FreeSpace(USART_data_t * usart_data);
296 bool USART_TXBuffer_PutByte(USART_data_t * usart_data, uint8_t data);
297 bool USART_RXBufferData_Available(USART_data_t * usart_data);
298 uint8_t USART_RXBuffer_GetByte(USART_data_t * usart_data);
299 bool USART_RXComplete(USART_data_t * usart_data);
300 void USART_DataRegEmpty(USART_data_t * usart_data);
301
302 /* Functions for polled driver. */
303 void USART_NineBits_PutChar(USART_t * usart, uint16_t data);
304 uint16_t USART_NineBits_GetChar(USART_t * usart);
305
306 #endif
Usart_and_buffer::usart
USART_t * usart
Definition: usart_driver.h:110
USART_TXBuffer_FreeSpace
bool USART_TXBuffer_FreeSpace(USART_data_t *usart_data)
Test if there is data in the transmitter software buffer.
Definition: usart_driver.c:123
USART_RXComplete
bool USART_RXComplete(USART_data_t *usart_data)
RX Complete Interrupt Service Routine.
Definition: usart_driver.c:227
Usart_and_buffer
Struct used when interrupt driven driver is used.
Definition: usart_driver.h:107
USART_RXBuffer_GetByte
uint8_t USART_RXBuffer_GetByte(USART_data_t *usart_data)
Get received data (5-8 bit character).
Definition: usart_driver.c:204
USART_TXBuffer_PutByte
bool USART_TXBuffer_PutByte(USART_data_t *usart_data, uint8_t data)
Put data (5-8 bit character).
Definition: usart_driver.c:143
avr_compiler.h
This file implements some macros that makes the IAR C-compiler and avr-gcc work with the same code ba...
USART_Buffer::RX
volatile uint8_t RX[USART_RX_BUFFER_SIZE]
Definition: usart_driver.h:88
USART_Buffer::TX_Tail
volatile uint8_t TX_Tail
Definition: usart_driver.h:98
USART_RXBufferData_Available
bool USART_RXBufferData_Available(USART_data_t *usart_data)
Test if there is data in the receive software buffer.
Definition: usart_driver.c:181
USART_InterruptDriver_Initialize
void USART_InterruptDriver_Initialize(USART_data_t *usart_data, USART_t *usart, USART_DREINTLVL_t dreIntLevel)
Initializes buffer and selects what USART module to use.
Definition: usart_driver.c:81
USART_Buffer_t
struct USART_Buffer USART_Buffer_t
Usart_and_buffer::buffer
USART_Buffer_t buffer
Definition: usart_driver.h:114
USART_Buffer
Definition: usart_driver.h:85
USART_NineBits_PutChar
void USART_NineBits_PutChar(USART_t *usart, uint16_t data)
Put data (9 bit character).
Definition: usart_driver.c:292
USART_Buffer::RX_Head
volatile uint8_t RX_Head
Definition: usart_driver.h:92
USART_InterruptDriver_DreInterruptLevel_Set
void USART_InterruptDriver_DreInterruptLevel_Set(USART_data_t *usart_data, USART_DREINTLVL_t dreIntLevel)
Set USART DRE interrupt level.
Definition: usart_driver.c:106
USART_RX_BUFFER_SIZE
#define USART_RX_BUFFER_SIZE
Definition: usart_driver.h:67
USART_data_t
struct Usart_and_buffer USART_data_t
Struct used when interrupt driven driver is used.
USART_NineBits_GetChar
uint16_t USART_NineBits_GetChar(USART_t *usart)
Get received data (9 bit character).
Definition: usart_driver.c:313
USART_Buffer::TX
volatile uint8_t TX[USART_TX_BUFFER_SIZE]
Definition: usart_driver.h:90
USART_Buffer::RX_Tail
volatile uint8_t RX_Tail
Definition: usart_driver.h:94
USART_TX_BUFFER_SIZE
#define USART_TX_BUFFER_SIZE
Definition: usart_driver.h:69
USART_DataRegEmpty
void USART_DataRegEmpty(USART_data_t *usart_data)
Data Register Empty Interrupt Service Routine.
Definition: usart_driver.c:260
USART_Buffer::TX_Head
volatile uint8_t TX_Head
Definition: usart_driver.h:96
Usart_and_buffer::dreIntLevel
USART_DREINTLVL_t dreIntLevel
Definition: usart_driver.h:112