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 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
|
/*************************************************************************
DESCRIPTION:
An interrupt is generated when the UART has finished transmitting or
receiving a byte. The interrupt handling routines use circular buffers
for buffering received and transmitted data.
The UART_RX_BUFFER_SIZE and UART_TX_BUFFER_SIZE variables define
the buffer size in bytes. Note that these variables must be a
power of 2.
USAGE:
Refere to the header file uart.h for a description of the routines.
See also example test_uart.c.
NOTES:
Based on Atmel Application Note AVR306
$Id$
*************************************************************************/
#include <avr/io.h>
#include <avr/interrupt.h>
//#include <avr/signal.h>
#include <avr/pgmspace.h>
#include <ctype.h>
#include "uart.h"
#include <util/delay.h>
// #include "sys_config.h"
// #include "timer.h"
// #include "delay.h"
/*
* constants and macros
*/
/* size of RX/TX buffers */
#define UART_RX_BUFFER_MASK ( UART_RX_BUFFER_SIZE - 1)
#define UART_TX_BUFFER_MASK ( UART_TX_BUFFER_SIZE - 1)
#if ( UART_RX_BUFFER_SIZE & UART_RX_BUFFER_MASK )
#error RX buffer size is not a power of 2
#endif
#if ( UART_TX_BUFFER_SIZE & UART_TX_BUFFER_MASK )
#error TX buffer size is not a power of 2
#endif
/* ATmega with two USART */
#define ATMEGA_USART0
#define ATMEGA_USART1
#define UART0_RECEIVE_INTERRUPT USART0_RX_vect
#define UART1_RECEIVE_INTERRUPT USART1_RX_vect
#define UART0_TRANSMIT_INTERRUPT USART0_UDRE_vect
#define UART1_TRANSMIT_INTERRUPT USART1_UDRE_vect
#define UART0_STATUS UCSR0A
#define UART0_CONTROL UCSR0B
#define UART0_DATA UDR0
#define UART0_UDRIE UDRIE0
#define UART1_STATUS UCSR1A
#define UART1_CONTROL UCSR1B
#define UART1_DATA UDR1
#define UART1_UDRIE UDRIE1
/*
* module global variables
*/
volatile unsigned char setRS485ToReceive;
volatile unsigned char uart_input_timeout_0;
volatile unsigned char uart_output_timeout_0;
volatile unsigned char uart_input_timeout_1;
volatile unsigned char uart_output_timeout_1;
static volatile unsigned char UART_0_TxBuf[UART_TX_BUFFER_SIZE];
static volatile unsigned char UART_0_RxBuf[UART_RX_BUFFER_SIZE];
static volatile unsigned char UART_0_TxHead;
static volatile unsigned char UART_0_TxTail;
static volatile unsigned char UART_0_RxHead;
static volatile unsigned char UART_0_RxTail;
static volatile unsigned char UART_0_LastRxError;
static volatile unsigned char UART_1_TxBuf[UART_TX_BUFFER_SIZE];
static volatile unsigned char UART_1_RxBuf[UART_RX_BUFFER_SIZE];
static volatile unsigned char UART_1_TxHead;
static volatile unsigned char UART_1_TxTail;
static volatile unsigned char UART_1_RxHead;
static volatile unsigned char UART_1_RxTail;
static volatile unsigned char UART_1_LastRxError;
ISR(UART1_RECEIVE_INTERRUPT)
/*************************************************************************
Function: UART Receive Complete interrupt
Purpose: called when the UART has received a character
**************************************************************************/
{
unsigned char tmphead;
unsigned char data;
unsigned char usr;
unsigned char lastRxError;
/* read UART status register and UART data register */
usr = UART1_STATUS;
data = UART1_DATA;
/* */
lastRxError = (usr & (_BV(FE1)|_BV(DOR1)) );
/* calculate buffer index */
tmphead = ( UART_1_RxHead + 1) & UART_RX_BUFFER_MASK;
if ( tmphead == UART_1_RxTail ) {
/* error: receive buffer overflow */
lastRxError = UART_BUFFER_OVERFLOW >> 8;
}else{
/* store new index */
UART_1_RxHead = tmphead;
/* store received data in buffer */
UART_1_RxBuf[tmphead] = data;
}
UART_1_LastRxError = lastRxError;
}
ISR(UART1_TRANSMIT_INTERRUPT)
/*************************************************************************
Function: UART Data Register Empty interrupt
Purpose: called when the UART is ready to transmit the next byte
**************************************************************************/
{
unsigned char tmptail;
if ( UART_1_TxHead != UART_1_TxTail) {
/* calculate and store new buffer index */
tmptail = (UART_1_TxTail + 1) & UART_TX_BUFFER_MASK;
UART_1_TxTail = tmptail;
/* get one byte from buffer and write it to UART */
UART1_DATA = UART_1_TxBuf[tmptail]; /* start transmission */
}else{
/* tx buffer empty, disable UDRE interrupt */
UART1_CONTROL &= ~_BV(UART1_UDRIE);
/* set RS 485 xvr to receive */
//sbi(UCSR1A,6); //clear TXC by writing one to its bit location!!!
//not needed
//UCSR1A |= _BV(6);
//setRS485ToReceive = 1;
}
}
#define usart_baudrate2setting(x) \
(uint16_t)((F_CPU / ((uint32_t)(x) * (uint32_t)16)) - 1)
/*************************************************************************
Function: uart_1_init()
Purpose: initialize UART1 and set baudrate
Input: baudrate
Returns: none
**************************************************************************/
void uart_1_init(unsigned long baudrate, unsigned long xtalCPU){
unsigned int calcul;
// UART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// UART Receiver: On
// UART Transmitter: On
// UART Baud rate: defined by UBRRHI_VALUE & UBRRLO_VALUE above
calcul = usart_baudrate2setting(baudrate);
UBRR1H = calcul >> 8;
UBRR1L = calcul & 0xFF;
UCSR1B=0x18 | 0xA0 ; // enable data, RX interrupts
UCSR1C=0x06;
}
/*************************************************************************
Function: uart_1_getc()
Purpose: return byte from ringbuffer
Returns: lower byte: received byte from ringbuffer
higher byte: last receive error
**************************************************************************/
unsigned int uart_1_getc(void)
{
unsigned char tmptail;
unsigned char data;
if ( UART_1_RxHead == UART_1_RxTail ) {
return UART_NO_DATA; /* no data available */
}
/* calculate /store buffer index */
tmptail = (UART_1_RxTail + 1) & UART_RX_BUFFER_MASK;
UART_1_RxTail = tmptail;
/* get data from receive buffer */
data = UART_1_RxBuf[tmptail];
return (UART_1_LastRxError << 8) + data;
}/* uart_getc */
/*************************************************************************
Function: uart_1_putc()
Purpose: write byte to ringbuffer for transmitting via UART
Input: byte to be transmitted
Returns: none
**************************************************************************/
void uart_1_putc(unsigned char data)
{
unsigned char tmphead;
tmphead = (UART_1_TxHead + 1) & UART_TX_BUFFER_MASK;
while ( tmphead == UART_1_TxTail ){
;/* wait for free space in buffer */
}
UART_1_TxBuf[tmphead] = data;
UART_1_TxHead = tmphead;
/* enable UDRE interrupt */
UART1_CONTROL |= _BV(UART1_UDRIE);
}/* uart_putc */
/*************************************************************************
Function: uart_puts()
Purpose: transmit string to UART
Input: string to be transmitted
Returns: none
**************************************************************************/
void uart_1_puts(const char *s )
{
while (*s)
uart_1_putc(*s++);
}/* uart_puts */
void uart_1_disable(void){
UCSR1B = 0x0;
}
void uart_1_send_char(unsigned char ch) //send one character via the serial port
{
while (!(UCSR1A & 0x20))
; //wait xmit ready
UDR1 = ch; //send the character
}
/////////////////////////////////////UART 0///////////////////////////////////////
ISR(UART0_RECEIVE_INTERRUPT)
/*************************************************************************
Function: UART Receive Complete interrupt
Purpose: called when the UART has received a character
**************************************************************************/
{
unsigned char tmphead;
unsigned char data;
unsigned char usr;
unsigned char lastRxError;
/* read UART status register and UART data register */
usr = UART0_STATUS;
data = UART0_DATA;
/* */
lastRxError = (usr & (_BV(FE0)|_BV(DOR0)) );
/* calculate buffer index */
tmphead = ( UART_0_RxHead + 1) & UART_RX_BUFFER_MASK;
if ( tmphead == UART_0_RxTail ) {
/* error: receive buffer overflow */
lastRxError = UART_BUFFER_OVERFLOW >> 8;
}else{
/* store new index */
UART_0_RxHead = tmphead;
/* store received data in buffer */
UART_0_RxBuf[tmphead] = data;
}
UART_0_LastRxError = lastRxError;
}
ISR(UART0_TRANSMIT_INTERRUPT)
/*************************************************************************
Function: UART Data Register Empty interrupt
Purpose: called when the UART is ready to transmit the next byte
**************************************************************************/
{
unsigned char tmptail;
if ( UART_0_TxHead != UART_0_TxTail) {
/* calculate and store new buffer index */
tmptail = (UART_0_TxTail + 1) & UART_TX_BUFFER_MASK;
UART_0_TxTail = tmptail;
/* get one byte from buffer and write it to UART */
UART0_DATA = UART_0_TxBuf[tmptail]; /* start transmission */
}else{
/* tx buffer empty, disable UDRE interrupt */
UART0_CONTROL &= ~_BV(UART0_UDRIE);
}
}
/*************************************************************************
Function: uart_0_init()
Purpose: initialize UART1 and set baudrate
Input: baudrate
Returns: none
**************************************************************************/
void uart_0_init(unsigned long baudrate, unsigned long xtalCPU){
unsigned int calcul;
// UART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// UART Receiver: On
// UART Transmitter: On
// UART Baud rate: defined by UBRRHI_VALUE & UBRRLO_VALUE above
calcul = usart_baudrate2setting(baudrate);
UBRR0H = calcul >> 8;
UBRR0L = calcul & 0xFF;
UCSR0B=0x18 | 0xA0 ; // enable data, RX interrupts
UCSR0C=0x06;
}
/*************************************************************************
Function: uart_0_getc()
Purpose: return byte from ringbuffer
Returns: lower byte: received byte from ringbuffer
higher byte: last receive error
**************************************************************************/
unsigned int uart_0_getc(void)
{
unsigned char tmptail;
unsigned char data;
if ( UART_0_RxHead == UART_0_RxTail ) {
return UART_NO_DATA; /* no data available */
}
/* calculate /store buffer index */
tmptail = (UART_0_RxTail + 1) & UART_RX_BUFFER_MASK;
UART_0_RxTail = tmptail;
/* get data from receive buffer */
data = UART_0_RxBuf[tmptail];
return (UART_0_LastRxError << 8) + data;
}/* uart_getc */
/*************************************************************************
Function: uart_0_putc()
Purpose: write byte to ringbuffer for transmitting via UART
Input: byte to be transmitted
Returns: none
**************************************************************************/
void uart_0_putc(unsigned char data)
{
unsigned char tmphead;
tmphead = (UART_0_TxHead + 1) & UART_TX_BUFFER_MASK;
while ( tmphead == UART_0_TxTail ){
;/* wait for free space in buffer */
}
UART_0_TxBuf[tmphead] = data;
UART_0_TxHead = tmphead;
/* enable UDRE interrupt */
UART0_CONTROL |= _BV(UART0_UDRIE);
}/* uart_putc */
/*************************************************************************
Function: uart_puts()
Purpose: transmit string to UART
Input: string to be transmitted
Returns: none
**************************************************************************/
void uart_0_puts(const char *s )
{
while (*s)
uart_0_putc(*s++);
}/* uart_puts */
void uart_0_disable(void){
UCSR0B = 0x0;
}
void uart_0_send_char(unsigned char ch) //send one character via the serial port
{
while (!(UCSR0A & 0x20))
; //wait xmit ready
UDR0 = ch; //send the character
}
|