BLDC control on ATAVRMC303 with ATxMega128A1
usart_driver.c File Reference

XMEGA USART driver source file. More...

#include "usart_driver.h"
Include dependency graph for usart_driver.c:

Go to the source code of this file.

Functions

void USART_DataRegEmpty (USART_data_t *usart_data)
 Data Register Empty Interrupt Service Routine. More...
 
void USART_InterruptDriver_DreInterruptLevel_Set (USART_data_t *usart_data, USART_DREINTLVL_t dreIntLevel)
 Set USART DRE interrupt level. More...
 
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. More...
 
uint16_t USART_NineBits_GetChar (USART_t *usart)
 Get received data (9 bit character). More...
 
void USART_NineBits_PutChar (USART_t *usart, uint16_t data)
 Put data (9 bit character). More...
 
uint8_t USART_RXBuffer_GetByte (USART_data_t *usart_data)
 Get received data (5-8 bit character). More...
 
bool USART_RXBufferData_Available (USART_data_t *usart_data)
 Test if there is data in the receive software buffer. More...
 
bool USART_RXComplete (USART_data_t *usart_data)
 RX Complete Interrupt Service Routine. More...
 
bool USART_TXBuffer_FreeSpace (USART_data_t *usart_data)
 Test if there is data in the transmitter software buffer. More...
 
bool USART_TXBuffer_PutByte (USART_data_t *usart_data, uint8_t data)
 Put data (5-8 bit character). More...
 

Detailed Description

XMEGA USART driver source file.

This file contains the function implementations the XMEGA interrupt and polled 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.

Some functions use the following construct: "some_register = ... | (some_parameter ? SOME_BIT_bm : 0) | ..." Although the use of the ternary operator ( if ? then : else ) is discouraged, in some occasions the operator makes it possible to write pretty clean and neat code. In this driver, the construct is used to set or not set a configuration bit based on a boolean input parameter, such as the "some_parameter" in the example above.

Application note:
AVR1307: Using the XMEGA USART
Documentation
For comprehensive code documentation, supported compilers, compiler settings and supported devices see readme.html
Author
Atmel Corporation: http://www.atmel.com
Support email: avr@a.nosp@m.tmel.nosp@m..com
Revision
1694
Date
2008-07-29 14:21:58 +0200 (ti, 29 jul 2008)


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.

Definition in file usart_driver.c.

Function Documentation

◆ USART_DataRegEmpty()

void USART_DataRegEmpty ( USART_data_t usart_data)

Data Register Empty Interrupt Service Routine.

Data Register Empty Interrupt Service Routine. Transmits one byte from TX software buffer. Disables DRE interrupt if buffer is empty. Argument is pointer to USART (USART_data_t).

Parameters
usart_dataThe USART_data_t struct instance.

Definition at line 260 of file usart_driver.c.

References Usart_and_buffer::buffer, USART_Buffer::TX, USART_Buffer::TX_Head, USART_Buffer::TX_Tail, Usart_and_buffer::usart, and USART_TX_BUFFER_MASK.

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 }
volatile uint8_t TX_Tail
Definition: usart_driver.h:98
#define USART_TX_BUFFER_MASK
Definition: usart_driver.h:73
USART_Buffer_t buffer
Definition: usart_driver.h:114
volatile uint8_t TX[USART_TX_BUFFER_SIZE]
Definition: usart_driver.h:90
volatile uint8_t TX_Head
Definition: usart_driver.h:96

◆ USART_InterruptDriver_DreInterruptLevel_Set()

void USART_InterruptDriver_DreInterruptLevel_Set ( USART_data_t usart_data,
USART_DREINTLVL_t  dreIntLevel 
)

Set USART DRE interrupt level.

Set the interrupt level on Data Register interrupt.

Note
Changing the DRE interrupt level in the interrupt driver while it is running will not change the DRE interrupt level in the USART before the DRE interrupt have been disabled and enabled again.
Parameters
usart_dataThe USART_data_t struct instance
dreIntLevelInterrupt level of the DRE interrupt.

Definition at line 106 of file usart_driver.c.

References Usart_and_buffer::dreIntLevel.

108 {
109  usart_data->dreIntLevel = dreIntLevel;
110 }
USART_DREINTLVL_t dreIntLevel
Definition: usart_driver.h:112

◆ 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.

Initializes receive and transmit buffer and selects what USART module to use, and stores the data register empty interrupt level.

Parameters
usart_dataThe USART_data_t struct instance.
usartThe USART module.
dreIntLevelData register empty interrupt level.

Definition at line 81 of file usart_driver.c.

References Usart_and_buffer::buffer, Usart_and_buffer::dreIntLevel, USART_Buffer::RX_Head, USART_Buffer::RX_Tail, USART_Buffer::TX_Head, USART_Buffer::TX_Tail, and Usart_and_buffer::usart.

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 }
volatile uint8_t TX_Tail
Definition: usart_driver.h:98
USART_Buffer_t buffer
Definition: usart_driver.h:114
volatile uint8_t RX_Head
Definition: usart_driver.h:92
volatile uint8_t RX_Tail
Definition: usart_driver.h:94
volatile uint8_t TX_Head
Definition: usart_driver.h:96
USART_DREINTLVL_t dreIntLevel
Definition: usart_driver.h:112

◆ USART_NineBits_GetChar()

uint16_t USART_NineBits_GetChar ( USART_t *  usart)

Get received data (9 bit character).

This function reads out the received 9 bit character (uint16_t). Use the function USART_IsRXComplete to check if anything is received.

Parameters
usartThe USART module.
Return values
Receiveddata.

Definition at line 313 of file usart_driver.c.

314 {
315  if(usart->CTRLB & USART_RXB8_bm) {
316  return(0x0100 | usart->DATA);
317  }else {
318  return(usart->DATA);
319  }
320 }

◆ USART_NineBits_PutChar()

void USART_NineBits_PutChar ( USART_t *  usart,
uint16_t  data 
)

Put data (9 bit character).

Use the function USART_IsTXDataRegisterEmpty before using this function to put 9 bit character to the TX register.

Parameters
usartThe USART module.
dataThe data to send.

Definition at line 292 of file usart_driver.c.

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 }

◆ USART_RXBuffer_GetByte()

uint8_t USART_RXBuffer_GetByte ( USART_data_t usart_data)

Get received data (5-8 bit character).

The function USART_RXBufferData_Available should be used before this function is used to ensure that data is available.

Returns data from RX software buffer.

Parameters
usart_dataThe USART_data_t struct instance.
Returns
Received data.

Definition at line 204 of file usart_driver.c.

References Usart_and_buffer::buffer, USART_Buffer::RX, USART_Buffer::RX_Tail, and USART_RX_BUFFER_MASK.

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 }
volatile uint8_t RX[USART_RX_BUFFER_SIZE]
Definition: usart_driver.h:88
#define USART_RX_BUFFER_MASK
Definition: usart_driver.h:71
USART_Buffer_t buffer
Definition: usart_driver.h:114
volatile uint8_t RX_Tail
Definition: usart_driver.h:94

◆ USART_RXBufferData_Available()

bool USART_RXBufferData_Available ( USART_data_t usart_data)

Test if there is data in the receive software buffer.

This function can be used to test if there is data in the receive software buffer.

Parameters
usart_dataThe USART_data_t struct instance
Return values
trueThere is data in the receive buffer.
falseThe receive buffer is empty.

Definition at line 181 of file usart_driver.c.

References Usart_and_buffer::buffer, USART_Buffer::RX_Head, and USART_Buffer::RX_Tail.

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 }
USART_Buffer_t buffer
Definition: usart_driver.h:114
volatile uint8_t RX_Head
Definition: usart_driver.h:92
volatile uint8_t RX_Tail
Definition: usart_driver.h:94

◆ USART_RXComplete()

bool USART_RXComplete ( USART_data_t usart_data)

RX Complete Interrupt Service Routine.

RX Complete Interrupt Service Routine. Stores received data in RX software buffer.

Parameters
usart_dataThe USART_data_t struct instance.

Definition at line 227 of file usart_driver.c.

References Usart_and_buffer::buffer, USART_Buffer::RX, USART_Buffer::RX_Head, USART_Buffer::RX_Tail, Usart_and_buffer::usart, and USART_RX_BUFFER_MASK.

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 }
volatile uint8_t RX[USART_RX_BUFFER_SIZE]
Definition: usart_driver.h:88
#define USART_RX_BUFFER_MASK
Definition: usart_driver.h:71
USART_Buffer_t buffer
Definition: usart_driver.h:114
volatile uint8_t RX_Head
Definition: usart_driver.h:92
volatile uint8_t RX_Tail
Definition: usart_driver.h:94

◆ USART_TXBuffer_FreeSpace()

bool USART_TXBuffer_FreeSpace ( USART_data_t usart_data)

Test if there is data in the transmitter software buffer.

This function can be used to test if there is free space in the transmitter software buffer.

Parameters
usart_dataThe USART_data_t struct instance.
Return values
trueThere is data in the receive buffer.
falseThe receive buffer is empty.

Definition at line 123 of file usart_driver.c.

References Usart_and_buffer::buffer, USART_Buffer::TX_Head, USART_Buffer::TX_Tail, and USART_TX_BUFFER_MASK.

Referenced by USART_TXBuffer_PutByte().

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 }
volatile uint8_t TX_Tail
Definition: usart_driver.h:98
#define USART_TX_BUFFER_MASK
Definition: usart_driver.h:73
USART_Buffer_t buffer
Definition: usart_driver.h:114
volatile uint8_t TX_Head
Definition: usart_driver.h:96

◆ USART_TXBuffer_PutByte()

bool USART_TXBuffer_PutByte ( USART_data_t usart_data,
uint8_t  data 
)

Put data (5-8 bit character).

Stores data byte in TX software buffer and enables DRE interrupt if there is free space in the TX software buffer.

Parameters
usart_dataThe USART_data_t struct instance.
dataThe data to send.

Definition at line 143 of file usart_driver.c.

References Usart_and_buffer::buffer, Usart_and_buffer::dreIntLevel, USART_Buffer::TX, USART_Buffer::TX_Head, Usart_and_buffer::usart, USART_TX_BUFFER_MASK, and USART_TXBuffer_FreeSpace().

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 }
bool USART_TXBuffer_FreeSpace(USART_data_t *usart_data)
Test if there is data in the transmitter software buffer.
Definition: usart_driver.c:123
#define USART_TX_BUFFER_MASK
Definition: usart_driver.h:73
USART_Buffer_t buffer
Definition: usart_driver.h:114
volatile uint8_t TX[USART_TX_BUFFER_SIZE]
Definition: usart_driver.h:90
volatile uint8_t TX_Head
Definition: usart_driver.h:96
USART_DREINTLVL_t dreIntLevel
Definition: usart_driver.h:112
Here is the call graph for this function: