Xmega Application Note | |||||
00001 /* This file has been prepared for Doxygen automatic documentation generation.*/ 00068 #include "usart_driver.h" 00069 00070 00071 00081 void USART_InterruptDriver_Initialize(USART_data_t * usart_data, 00082 USART_t * usart, 00083 USART_DREINTLVL_t dreIntLevel) 00084 { 00085 usart_data->usart = usart; 00086 usart_data->dreIntLevel = dreIntLevel; 00087 00088 usart_data->buffer.RX_Tail = 0; 00089 usart_data->buffer.RX_Head = 0; 00090 usart_data->buffer.TX_Tail = 0; 00091 usart_data->buffer.TX_Head = 0; 00092 } 00093 00094 00106 void USART_InterruptDriver_DreInterruptLevel_Set(USART_data_t * usart_data, 00107 USART_DREINTLVL_t dreIntLevel) 00108 { 00109 usart_data->dreIntLevel = dreIntLevel; 00110 } 00111 00112 00123 bool USART_TXBuffer_FreeSpace(USART_data_t * usart_data) 00124 { 00125 /* Make copies to make sure that volatile access is specified. */ 00126 uint8_t tempHead = (usart_data->buffer.TX_Head + 1) & USART_TX_BUFFER_MASK; 00127 uint8_t tempTail = usart_data->buffer.TX_Tail; 00128 00129 /* There are data left in the buffer unless Head and Tail are equal. */ 00130 return (tempHead != tempTail); 00131 } 00132 00133 00134 00143 bool USART_TXBuffer_PutByte(USART_data_t * usart_data, uint8_t data) 00144 { 00145 uint8_t tempCTRLA; 00146 uint8_t tempTX_Head; 00147 bool TXBuffer_FreeSpace; 00148 USART_Buffer_t * TXbufPtr; 00149 00150 TXbufPtr = &usart_data->buffer; 00151 TXBuffer_FreeSpace = USART_TXBuffer_FreeSpace(usart_data); 00152 00153 00154 if(TXBuffer_FreeSpace) 00155 { 00156 tempTX_Head = TXbufPtr->TX_Head; 00157 TXbufPtr->TX[tempTX_Head]= data; 00158 /* Advance buffer head. */ 00159 TXbufPtr->TX_Head = (tempTX_Head + 1) & USART_TX_BUFFER_MASK; 00160 00161 /* Enable DRE interrupt. */ 00162 tempCTRLA = usart_data->usart->CTRLA; 00163 tempCTRLA = (tempCTRLA & ~USART_DREINTLVL_gm) | usart_data->dreIntLevel; 00164 usart_data->usart->CTRLA = tempCTRLA; 00165 } 00166 return TXBuffer_FreeSpace; 00167 } 00168 00169 00170 00181 bool USART_RXBufferData_Available(USART_data_t * usart_data) 00182 { 00183 /* Make copies to make sure that volatile access is specified. */ 00184 uint8_t tempHead = usart_data->buffer.RX_Head; 00185 uint8_t tempTail = usart_data->buffer.RX_Tail; 00186 00187 /* There are data left in the buffer unless Head and Tail are equal. */ 00188 return (tempHead != tempTail); 00189 } 00190 00191 00192 00204 uint8_t USART_RXBuffer_GetByte(USART_data_t * usart_data) 00205 { 00206 USART_Buffer_t * bufPtr; 00207 uint8_t ans; 00208 00209 bufPtr = &usart_data->buffer; 00210 ans = (bufPtr->RX[bufPtr->RX_Tail]); 00211 00212 /* Advance buffer tail. */ 00213 bufPtr->RX_Tail = (bufPtr->RX_Tail + 1) & USART_RX_BUFFER_MASK; 00214 00215 return ans; 00216 } 00217 00218 00219 00227 bool USART_RXComplete(USART_data_t * usart_data) 00228 { 00229 USART_Buffer_t * bufPtr; 00230 bool ans; 00231 00232 bufPtr = &usart_data->buffer; 00233 /* Advance buffer head. */ 00234 uint8_t tempRX_Head = (bufPtr->RX_Head + 1) & USART_RX_BUFFER_MASK; 00235 00236 /* Check for overflow. */ 00237 uint8_t tempRX_Tail = bufPtr->RX_Tail; 00238 uint8_t data = usart_data->usart->DATA; 00239 00240 if (tempRX_Head == tempRX_Tail) { 00241 ans = false; 00242 }else{ 00243 ans = true; 00244 usart_data->buffer.RX[usart_data->buffer.RX_Head] = data; 00245 usart_data->buffer.RX_Head = tempRX_Head; 00246 } 00247 return ans; 00248 } 00249 00250 00251 00260 void USART_DataRegEmpty(USART_data_t * usart_data) 00261 { 00262 USART_Buffer_t * bufPtr; 00263 bufPtr = &usart_data->buffer; 00264 00265 /* Check if all data is transmitted. */ 00266 uint8_t tempTX_Tail = usart_data->buffer.TX_Tail; 00267 if (bufPtr->TX_Head == tempTX_Tail){ 00268 /* Disable DRE interrupts. */ 00269 uint8_t tempCTRLA = usart_data->usart->CTRLA; 00270 tempCTRLA = (tempCTRLA & ~USART_DREINTLVL_gm) | USART_DREINTLVL_OFF_gc; 00271 usart_data->usart->CTRLA = tempCTRLA; 00272 00273 }else{ 00274 /* Start transmitting. */ 00275 uint8_t data = bufPtr->TX[usart_data->buffer.TX_Tail]; 00276 usart_data->usart->DATA = data; 00277 00278 /* Advance buffer tail. */ 00279 bufPtr->TX_Tail = (bufPtr->TX_Tail + 1) & USART_TX_BUFFER_MASK; 00280 } 00281 } 00282 00283 00292 void USART_NineBits_PutChar(USART_t * usart, uint16_t data) 00293 { 00294 if(data & 0x0100) { 00295 usart->CTRLB |= USART_TXB8_bm; 00296 }else { 00297 usart->CTRLB &= ~USART_TXB8_bm; 00298 } 00299 00300 usart->DATA = (data & 0x00FF); 00301 } 00302 00303 00313 uint16_t USART_NineBits_GetChar(USART_t * usart) 00314 { 00315 if(usart->CTRLB & USART_RXB8_bm) { 00316 return(0x0100 | usart->DATA); 00317 }else { 00318 return(usart->DATA); 00319 } 00320 }
Generated on Wed Nov 5 10:23:27 2008 for AVRxxxx Application note title by ![]() |