| BLDC control on ATAVRMC303 with ATxMega128A1

usart_driver.c
Go to the documentation of this file.
1 /* This file has been prepared for Doxygen automatic documentation generation.*/
68 #include "usart_driver.h"
69
70
71
81 void USART_InterruptDriver_Initialize(USART_data_t * usart_data,
82 USART_t * usart,
83 USART_DREINTLVL_t dreIntLevel)
84 {
85 usart_data->usart = usart;
86 usart_data->dreIntLevel = dreIntLevel;
87
88 usart_data->buffer.RX_Tail = 0;
89 usart_data->buffer.RX_Head = 0;
90 usart_data->buffer.TX_Tail = 0;
91 usart_data->buffer.TX_Head = 0;
92 }
93
94
106 void USART_InterruptDriver_DreInterruptLevel_Set(USART_data_t * usart_data,
107 USART_DREINTLVL_t dreIntLevel)
108 {
109 usart_data->dreIntLevel = dreIntLevel;
110 }
111
112
123 bool USART_TXBuffer_FreeSpace(USART_data_t * usart_data)
124 {
125 /* Make copies to make sure that volatile access is specified. */
126 uint8_t tempHead = (usart_data->buffer.TX_Head + 1) & USART_TX_BUFFER_MASK;
127 uint8_t tempTail = usart_data->buffer.TX_Tail;
128
129 /* There are data left in the buffer unless Head and Tail are equal. */
130 return (tempHead != tempTail);
131 }
132
133
134
143 bool USART_TXBuffer_PutByte(USART_data_t * usart_data, uint8_t data)
144 {
145 uint8_t tempCTRLA;
146 uint8_t tempTX_Head;
147 bool TXBuffer_FreeSpace;
148 USART_Buffer_t * TXbufPtr;
149
150 TXbufPtr = &usart_data->buffer;
151 TXBuffer_FreeSpace = USART_TXBuffer_FreeSpace(usart_data);
152
153
154 if(TXBuffer_FreeSpace)
155 {
156 tempTX_Head = TXbufPtr->TX_Head;
157 TXbufPtr->TX[tempTX_Head]= data;
158 /* Advance buffer head. */
159 TXbufPtr->TX_Head = (tempTX_Head + 1) & USART_TX_BUFFER_MASK;
160
161 /* Enable DRE interrupt. */
162 tempCTRLA = usart_data->usart->CTRLA;
163 tempCTRLA = (tempCTRLA & ~USART_DREINTLVL_gm) | usart_data->dreIntLevel;
164 usart_data->usart->CTRLA = tempCTRLA;
165 }
166 return TXBuffer_FreeSpace;
167 }
168
169
170
181 bool USART_RXBufferData_Available(USART_data_t * usart_data)
182 {
183 /* Make copies to make sure that volatile access is specified. */
184 uint8_t tempHead = usart_data->buffer.RX_Head;
185 uint8_t tempTail = usart_data->buffer.RX_Tail;
186
187 /* There are data left in the buffer unless Head and Tail are equal. */
188 return (tempHead != tempTail);
189 }
190
191
192
204 uint8_t USART_RXBuffer_GetByte(USART_data_t * usart_data)
205 {
206 USART_Buffer_t * bufPtr;
207 uint8_t ans;
208
209 bufPtr = &usart_data->buffer;
210 ans = (bufPtr->RX[bufPtr->RX_Tail]);
211
212 /* Advance buffer tail. */
213 bufPtr->RX_Tail = (bufPtr->RX_Tail + 1) & USART_RX_BUFFER_MASK;
214
215 return ans;
216 }
217
218
219
227 bool USART_RXComplete(USART_data_t * usart_data)
228 {
229 USART_Buffer_t * bufPtr;
230 bool ans;
231
232 bufPtr = &usart_data->buffer;
233 /* Advance buffer head. */
234 uint8_t tempRX_Head = (bufPtr->RX_Head + 1) & USART_RX_BUFFER_MASK;
235
236 /* Check for overflow. */
237 uint8_t tempRX_Tail = bufPtr->RX_Tail;
238 uint8_t data = usart_data->usart->DATA;
239
240 if (tempRX_Head == tempRX_Tail) {
241 ans = false;
242 }else{
243 ans = true;
244 usart_data->buffer.RX[usart_data->buffer.RX_Head] = data;
245 usart_data->buffer.RX_Head = tempRX_Head;
246 }
247 return ans;
248 }
249
250
251
260 void USART_DataRegEmpty(USART_data_t * usart_data)
261 {
262 USART_Buffer_t * bufPtr;
263 bufPtr = &usart_data->buffer;
264
265 /* Check if all data is transmitted. */
266 uint8_t tempTX_Tail = usart_data->buffer.TX_Tail;
267 if (bufPtr->TX_Head == tempTX_Tail){
268 /* Disable DRE interrupts. */
269 uint8_t tempCTRLA = usart_data->usart->CTRLA;
270 tempCTRLA = (tempCTRLA & ~USART_DREINTLVL_gm) | USART_DREINTLVL_OFF_gc;
271 usart_data->usart->CTRLA = tempCTRLA;
272
273 }else{
274 /* Start transmitting. */
275 uint8_t data = bufPtr->TX[usart_data->buffer.TX_Tail];
276 usart_data->usart->DATA = data;
277
278 /* Advance buffer tail. */
279 bufPtr->TX_Tail = (bufPtr->TX_Tail + 1) & USART_TX_BUFFER_MASK;
280 }
281 }
282
283
292 void USART_NineBits_PutChar(USART_t * usart, uint16_t data)
293 {
294 if(data & 0x0100) {
295 usart->CTRLB |= USART_TXB8_bm;
296 }else {
297 usart->CTRLB &= ~USART_TXB8_bm;
298 }
299
300 usart->DATA = (data & 0x00FF);
301 }
302
303
313 uint16_t USART_NineBits_GetChar(USART_t * usart)
314 {
315 if(usart->CTRLB & USART_RXB8_bm) {
316 return(0x0100 | usart->DATA);
317 }else {
318 return(usart->DATA);
319 }
320 }
Usart_and_buffer::usart
USART_t * usart
Definition: usart_driver.h:110
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_and_buffer
Struct used when interrupt driven driver is used.
Definition: usart_driver.h:107
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::RX
volatile uint8_t RX[USART_RX_BUFFER_SIZE]
Definition: usart_driver.h:88
USART_RX_BUFFER_MASK
#define USART_RX_BUFFER_MASK
Definition: usart_driver.h:71
USART_Buffer::TX_Tail
volatile uint8_t TX_Tail
Definition: usart_driver.h:98
USART_NineBits_PutChar
void USART_NineBits_PutChar(USART_t *usart, uint16_t data)
Put data (9 bit character).
Definition: usart_driver.c:292
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_NineBits_GetChar
uint16_t USART_NineBits_GetChar(USART_t *usart)
Get received data (9 bit character).
Definition: usart_driver.c:313
USART_TX_BUFFER_MASK
#define USART_TX_BUFFER_MASK
Definition: usart_driver.h:73
Usart_and_buffer::buffer
USART_Buffer_t buffer
Definition: usart_driver.h:114
USART_Buffer
Definition: usart_driver.h:85
USART_Buffer::RX_Head
volatile uint8_t RX_Head
Definition: usart_driver.h:92
USART_DataRegEmpty
void USART_DataRegEmpty(USART_data_t *usart_data)
Data Register Empty Interrupt Service Routine.
Definition: usart_driver.c:260
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_driver.h
XMEGA USART driver header file.
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_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
USART_RXComplete
bool USART_RXComplete(USART_data_t *usart_data)
RX Complete Interrupt Service Routine.
Definition: usart_driver.c:227
USART_Buffer::TX_Head
volatile uint8_t TX_Head
Definition: usart_driver.h:96
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_and_buffer::dreIntLevel
USART_DREINTLVL_t dreIntLevel
Definition: usart_driver.h:112