Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1053 - 1
/*************************************************************************
2
Title:    Interrupt UART library with receive/transmit circular buffers
3
Author:   Peter Fleury <pfleury@gmx.ch>   http://jump.to/fleury
4
File:     $Id: uart.c,v 1.6.2.2 2009/11/29 08:56:12 Peter Exp $
5
Software: AVR-GCC 4.1, AVR Libc 1.4.6 or higher
6
Hardware: any AVR with built-in UART,
7
License:  GNU General Public License
8
 
9
DESCRIPTION:
10
    An interrupt is generated when the UART has finished transmitting or
11
    receiving a byte. The interrupt handling routines use circular buffers
12
    for buffering received and transmitted data.
13
 
14
    The UART_RX_BUFFER_SIZE and UART_TX_BUFFER_SIZE variables define
15
    the buffer size in bytes. Note that these variables must be a
16
    power of 2.
17
 
18
USAGE:
19
    Refere to the header file uart.h for a description of the routines.
20
    See also example test_uart.c.
21
 
22
NOTES:
23
    Based on Atmel Application Note AVR306
24
 
25
LICENSE:
26
    Copyright (C) 2006 Peter Fleury
27
 
28
    This program is free software; you can redistribute it and/or modify
29
    it under the terms of the GNU General Public License as published by
30
    the Free Software Foundation; either version 2 of the License, or
31
    any later version.
32
 
33
    This program is distributed in the hope that it will be useful,
34
    but WITHOUT ANY WARRANTY; without even the implied warranty of
35
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36
    GNU General Public License for more details.
37
 
38
*************************************************************************/
39
#include <avr/io.h>
40
#include <avr/interrupt.h>
41
#include <avr/pgmspace.h>
42
#include "uart1.h"
43
#include "main.h"
44
 
45
/*
46
 *  constants and macros
47
 */
48
//#define __AVR_ATmega644P__
49
 
50
#if defined HWVERSION3_1 || defined HWVERSION1_3
51
 
52
/* size of RX/TX buffers */
53
#define UART_RX_BUFFER_MASK ( UART_RX_BUFFER_SIZE - 1)
54
#define UART_TX_BUFFER_MASK ( UART_TX_BUFFER_SIZE - 1)
55
 
56
#if ( UART_RX_BUFFER_SIZE & UART_RX_BUFFER_MASK )
57
#error RX buffer size is not a power of 2
58
#endif
59
#if ( UART_TX_BUFFER_SIZE & UART_TX_BUFFER_MASK )
60
#error TX buffer size is not a power of 2
61
#endif
62
 
63
 
64
 /* ATmega with two USART */
65
 
66
 #define ATMEGA_USART1
67
 #define UART1_RECEIVE_INTERRUPT   SIG_USART1_RECV
68
 #define UART1_TRANSMIT_INTERRUPT  SIG_USART1_DATA
69
 #define UART1_STATUS   UCSR1A
70
 #define UART1_CONTROL  UCSR1B
71
 #define UART1_DATA     UDR1
72
 #define UART1_UDRIE    UDRIE1
73
 
74
 
75
 
76
/*
77
 *  module global variables
78
 */
79
 
80
 
81
static volatile unsigned char UART1_TxBuf[UART_TX_BUFFER_SIZE];
82
static volatile unsigned char UART1_RxBuf[UART_RX_BUFFER_SIZE];
83
static volatile unsigned char UART1_TxHead;
84
static volatile unsigned char UART1_TxTail;
85
static volatile unsigned char UART1_RxHead;
86
static volatile unsigned char UART1_RxTail;
87
static volatile unsigned char UART1_LastRxError;
88
 
89
 
90
 
91
 
92
 
93
/*
94
 * these functions are only for ATmegas with two USART
95
 */
96
 
97
SIGNAL(UART1_RECEIVE_INTERRUPT)
98
/*************************************************************************
99
Function: UART1 Receive Complete interrupt
100
Purpose:  called when the UART1 has received a character
101
**************************************************************************/
102
{
103
    unsigned char tmphead;
104
    unsigned char data;
105
    unsigned char usr;
106
    unsigned char lastRxError;
107
 
108
 
109
    /* read UART status register and UART data register */
110
    usr  = UART1_STATUS;
111
    data = UART1_DATA;
112
 
113
    /* */
114
    lastRxError = (usr & (_BV(FE1)|_BV(DOR1)) );
115
 
116
    /* calculate buffer index */
117
    tmphead = ( UART1_RxHead + 1) & UART_RX_BUFFER_MASK;
118
 
119
    if ( tmphead == UART1_RxTail ) {
120
        /* error: receive buffer overflow */
121
        lastRxError = UART_BUFFER_OVERFLOW >> 8;
122
    }else{
123
        /* store new index */
124
        UART1_RxHead = tmphead;
125
        /* store received data in buffer */
126
        UART1_RxBuf[tmphead] = data;
127
    }
128
    UART1_LastRxError = lastRxError;  
129
}
130
 
131
 
132
SIGNAL(UART1_TRANSMIT_INTERRUPT)
133
/*************************************************************************
134
Function: UART1 Data Register Empty interrupt
135
Purpose:  called when the UART1 is ready to transmit the next byte
136
**************************************************************************/
137
{
138
    unsigned char tmptail;
139
 
140
 
141
    if ( UART1_TxHead != UART1_TxTail) {
142
        /* calculate and store new buffer index */
143
        tmptail = (UART1_TxTail + 1) & UART_TX_BUFFER_MASK;
144
        UART1_TxTail = tmptail;
145
        /* get one byte from buffer and write it to UART */
146
        UART1_DATA = UART1_TxBuf[tmptail];  /* start transmission */
147
    }else{
148
        /* tx buffer empty, disable UDRE interrupt */
149
        UART1_CONTROL &= ~_BV(UART1_UDRIE);
150
    }
151
}
152
 
153
 
154
/*************************************************************************
155
Function: uart1_init()
156
Purpose:  initialize UART1 and set baudrate
157
Input:    baudrate using macro UART_BAUD_SELECT()
158
Returns:  none
159
**************************************************************************/
160
void uart1_init(unsigned int baudrate)
161
{
162
    UART1_TxHead = 0;
163
    UART1_TxTail = 0;
164
    UART1_RxHead = 0;
165
    UART1_RxTail = 0;
166
 
167
 
168
    /* Set baud rate */
169
    if ( baudrate & 0x8000 )
170
    {
171
        UART1_STATUS = (1<<U2X1);  //Enable 2x speed 
172
      baudrate &= ~0x8000;
173
    }
174
    UBRR1H = (unsigned char)(baudrate>>8);
175
    UBRR1L = (unsigned char) baudrate;
176
 
177
    /* Enable USART receiver and transmitter and receive complete interrupt */
178
    UART1_CONTROL = _BV(RXCIE1)|(1<<RXEN1)|(1<<TXEN1);
179
 
180
    /* Set frame format: asynchronous, 8data, no parity, 1stop bit */  
181
    #ifdef URSEL1
182
    UCSR1C = (1<<URSEL1)|(3<<UCSZ10);
183
    #else
184
    UCSR1C = (3<<UCSZ10);
185
    #endif 
186
}/* uart_init */
187
 
188
 
189
/*************************************************************************
190
Function: uart1_getc()
191
Purpose:  return byte from ringbuffer  
192
Returns:  lower byte:  received byte from ringbuffer
193
          higher byte: last receive error
194
**************************************************************************/
195
unsigned int uart1_getc(void)
196
{    
197
    unsigned char tmptail;
198
    unsigned char data;
199
 
200
 
201
    if ( UART1_RxHead == UART1_RxTail ) {
202
        return UART_NO_DATA;   /* no data available */
203
    }
204
 
205
    /* calculate /store buffer index */
206
    tmptail = (UART1_RxTail + 1) & UART_RX_BUFFER_MASK;
207
    UART1_RxTail = tmptail;
208
 
209
    /* get data from receive buffer */
210
    data = UART1_RxBuf[tmptail];
211
 
212
    return (UART1_LastRxError << 8) + data;
213
 
214
}/* uart1_getc */
215
 
216
 
217
/*************************************************************************
218
Function: uart1_putc()
219
Purpose:  write byte to ringbuffer for transmitting via UART
220
Input:    byte to be transmitted
221
Returns:  none          
222
**************************************************************************/
223
void uart1_putc(unsigned char data)
224
{
225
    unsigned char tmphead;
226
 
227
 
228
    tmphead  = (UART1_TxHead + 1) & UART_TX_BUFFER_MASK;
229
 
230
    while ( tmphead == UART1_TxTail ){
231
        ;/* wait for free space in buffer */
232
    }
233
 
234
    UART1_TxBuf[tmphead] = data;
235
    UART1_TxHead = tmphead;
236
 
237
    /* enable UDRE interrupt */
238
    UART1_CONTROL    |= _BV(UART1_UDRIE);
239
 
240
}/* uart1_putc */
241
 
242
 
243
/*************************************************************************
244
Function: uart1_puts()
245
Purpose:  transmit string to UART1
246
Input:    string to be transmitted
247
Returns:  none          
248
**************************************************************************/
249
void uart1_puts(const char *s )
250
{
251
    while (*s)
252
      uart1_putc(*s++);
253
 
254
}/* uart1_puts */
255
 
256
 
257
/*************************************************************************
258
Function: uart1_puts_p()
259
Purpose:  transmit string from program memory to UART1
260
Input:    program memory string to be transmitted
261
Returns:  none
262
**************************************************************************/
263
void uart1_puts_p(const char *progmem_s )
264
{
265
    register char c;
266
 
267
    while ( (c = pgm_read_byte(progmem_s++)) )
268
      uart1_putc(c);
269
 
270
}/* uart1_puts_p */
271
 
272
#endif
273