Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1915 - 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
 
40
#include <avr/io.h>
41
#include <avr/interrupt.h>
42
#include <avr/pgmspace.h>
43
#include <string.h>
44
#include <stdbool.h>
45
#include "uart1.h"
46
#include "../main.h"
47
#include "../bluetooth/bluetooth.h"
48
#include "../tracking/tracking.h"
49
//
50
//  constants and macros
51
//
52
#if defined HWVERSION1_3W || defined HWVERSION3_9
53
 
54
// size of RX/TX buffers
55
#define UART_RX_BUFFER_MASK ( UART_RX_BUFFER_SIZE - 1)
56
#define UART_TX_BUFFER_MASK ( UART_TX_BUFFER_SIZE - 1)
57
 
58
#if ( UART_RX_BUFFER_SIZE & UART_RX_BUFFER_MASK )
59
#error RX buffer size is not a power of 2
60
#endif
61
 
62
#if ( UART_TX_BUFFER_SIZE & UART_TX_BUFFER_MASK )
63
#error TX buffer size is not a power of 2
64
#endif
65
 
66
 
67
// ATmega with two USART
68
 
69
#define ATMEGA_USART1
70
 
71
#define UART1_STATUS   UCSR1A
72
#define UART1_CONTROL  UCSR1B
73
#define UART1_DATA     UDR1
74
#define UART1_UDRIE    UDRIE1
75
 
76
 
77
 
78
//
79
//  module global variables
80
//
81
 
82
uint8_t receiveNMEA = false;
83
 
84
 
85
#if defined( ATMEGA_USART1 )
86
static volatile unsigned char UART1_TxBuf[UART_TX_BUFFER_SIZE];
87
static volatile unsigned char UART1_RxBuf[UART_RX_BUFFER_SIZE];
88
static volatile unsigned char UART1_TxHead;
89
static volatile unsigned char UART1_TxTail;
90
static volatile unsigned char UART1_RxHead;
91
static volatile unsigned char UART1_RxTail;
92
static volatile unsigned char UART1_LastRxError;
93
volatile uint16_t UART1_RxError;
94
#endif
95
 
96
 
97
void SetBaudUart1(uint8_t Baudrate)
98
{
99
  switch (Baudrate)
100
  {
101
  case Baud_2400: uart1_init( UART_BAUD_SELECT(2400,F_CPU) );  break;
102
  case Baud_4800: uart1_init( UART_BAUD_SELECT(4800,F_CPU) );  break;
103
//  case Baud_9600: uart1_init( UART_BAUD_SELECT(9600,F_CPU) );  break;
104
  case Baud_9600: uart1_init( 129);  break;
105
  case Baud_19200: uart1_init( UART_BAUD_SELECT(19200,F_CPU) );  break;
106
  case Baud_38400: uart1_init( UART_BAUD_SELECT(38400,F_CPU) );  break;
107
  case Baud_57600: uart1_init( UART_BAUD_SELECT(57600,F_CPU) );  break;
108
//  case Baud_57600: uart1_init( 21);  break;
109
//  case Baud_115200: uart1_init( UART_BAUD_SELECT(115200,F_CPU) );  break;
110
  case Baud_115200: uart1_init( 10 );  break;
111
  }
112
}
113
 
114
//
115
// these functions are only for ATmegas with two USART
116
//
117
 
118
 
119
#if defined( ATMEGA_USART1 )
120
 
121
//--------------------------------------------------------------
122
// Function: UART1 Receive Complete interrupt
123
// Purpose:  called when the UART1 has received a character
124
//--------------------------------------------------------------
125
ISR(USART1_RX_vect)
126
{
127
        unsigned char tmphead;
128
        unsigned char data;
129
        unsigned char usr;
130
        unsigned char lastRxError;
131
 
132
 
133
        // read UART status register and UART data register
134
        usr  = UART1_STATUS;
135
        data = UART1_DATA;
136
 
137
        lastRxError = (usr & (_BV(FE1)|_BV(DOR1)) );
138
 
139
        // calculate buffer index
140
        tmphead = ( UART1_RxHead + 1) & UART_RX_BUFFER_MASK;
141
 
142
        if ( tmphead == UART1_RxTail )
143
        {
144
                // error: receive buffer overflow
145
                lastRxError = UART_BUFFER_OVERFLOW >> 8;
146
                UART1_RxError++;
147
        }
148
        else
149
        {
150
                // store new index
151
                UART1_RxHead = tmphead;
152
                // store received data in buffer
153
                UART1_RxBuf[tmphead] = data;
154
        }
155
        UART1_LastRxError = lastRxError;
156
 
157
 
158
 
159
//        if (receiveNMEA==true)
160
//          {
161
//            if (bt_receiveNMEA()) Tracking_NMEA();
162
//            else receiveNMEA = false;
163
//
164
//          }
165
 
166
}
167
 
168
 
169
//--------------------------------------------------------------
170
// Function: UART1 Data Register Empty interrupt
171
// Purpose:  called when the UART1 is ready to transmit the next byte
172
//--------------------------------------------------------------
173
ISR(USART1_UDRE_vect)
174
{
175
        unsigned char tmptail;
176
 
177
 
178
        if ( UART1_TxHead != UART1_TxTail)
179
        {
180
                // calculate and store new buffer index
181
                tmptail = (UART1_TxTail + 1) & UART_TX_BUFFER_MASK;
182
                UART1_TxTail = tmptail;
183
                // get one byte from buffer and write it to UART
184
                UART1_DATA = UART1_TxBuf[tmptail];  // start transmission
185
        }
186
        else
187
        {
188
                // tx buffer empty, disable UDRE interrupt
189
                UART1_CONTROL &= ~_BV(UART1_UDRIE);
190
        }
191
}
192
 
193
 
194
//--------------------------------------------------------------
195
// Function: uart1_init()
196
// Purpose:  initialize UART1 and set baudrate
197
// Input:       baudrate using macro UART_BAUD_SELECT()
198
// Returns:  none
199
//--------------------------------------------------------------
200
void uart1_init(unsigned int baudrate)
201
{
202
        UART1_TxHead = 0;
203
        UART1_TxTail = 0;
204
        UART1_RxHead = 0;
205
        UART1_RxTail = 0;
206
        UART1_RxError = 0;
207
 
208
        // Set baud rate
209
        if ( baudrate & 0x8000 )
210
        {
211
                UART1_STATUS = (1<<U2X1);  //Enable 2x speed
212
                baudrate &= ~0x8000;
213
        }
214
        UBRR1H = (unsigned char)(baudrate>>8);
215
        UBRR1L = (unsigned char) baudrate;
216
 
217
        // Enable USART receiver and transmitter and receive complete interrupt
218
        UART1_CONTROL = _BV(RXCIE1)|(1<<RXEN1)|(1<<TXEN1);
219
 
220
        // Set frame format: asynchronous, 8data, no parity, 1stop bit
221
#ifdef URSEL1
222
        UCSR1C = (1<<URSEL1)|(3<<UCSZ10);
223
#else
224
        UCSR1C = (3<<UCSZ10);
225
//      UCSR1C = (1<<UCSZ11) | (1<<UCSZ10); // 8data Bit
226
#endif
227
 
228
 
229
 
230
}
231
 
232
 
233
//--------------------------------------------------------------
234
// Function: uart1_getc()
235
// Purpose:  return byte from ringbuffer
236
// Returns:  lower byte:  received byte from ringbuffer
237
//           higher byte: last receive error
238
//--------------------------------------------------------------
239
unsigned int uart1_getc(void)
240
{
241
        unsigned char tmptail;
242
        unsigned char data;
243
 
244
 
245
        if ( UART1_RxHead == UART1_RxTail )
246
        {
247
                return UART_NO_DATA;   // no data available
248
        }
249
 
250
        // calculate /store buffer index
251
        tmptail = (UART1_RxTail + 1) & UART_RX_BUFFER_MASK;
252
        UART1_RxTail = tmptail;
253
 
254
        // get data from receive buffer
255
        data = UART1_RxBuf[tmptail];
256
 
257
        return (UART1_LastRxError << 8) + data;
258
 
259
}/* uart1_getc */
260
 
261
 
262
//--------------------------------------------------------------
263
// Function: uart1_putc()
264
// Purpose:  write byte to ringbuffer for transmitting via UART
265
// Input:    byte to be transmitted
266
// Returns:  1 on succes, 0 if remote not ready
267
//--------------------------------------------------------------
268
int uart1_putc(unsigned char data)
269
{
270
        unsigned char tmphead;
271
 
272
 
273
        tmphead = (UART1_TxHead + 1) & UART_TX_BUFFER_MASK;
274
 
275
        while ( tmphead == UART1_TxTail )
276
        {;}  // wait for free space in buffer
277
 
278
        UART1_TxBuf[tmphead] = data;
279
        UART1_TxHead = tmphead;
280
 
281
        // enable UDRE interrupt
282
        UART1_CONTROL |= _BV(UART1_UDRIE);
283
        return (UART1_LastRxError << 8) + data;
284
}
285
 
286
 
287
//--------------------------------------------------------------
288
// Function: uart1_puts()
289
// Purpose:  transmit string to UART1
290
// Input:    string to be transmitted
291
// Returns:  none
292
//--------------------------------------------------------------
293
void uart1_puts(const char *s )
294
{
295
        while (*s)
296
                uart1_putc(*s++);
297
}
298
 
299
 
300
//--------------------------------------------------------------
301
// Function: uart1_puts_p()
302
// Purpose:  transmit string from program memory to UART1
303
// Input:    program memory string to be transmitted
304
// Returns:  none
305
//--------------------------------------------------------------
306
void uart1_puts_p(const char *progmem_s )
307
{
308
        register char c;
309
        while ( (c = pgm_read_byte(progmem_s++)) )
310
                uart1_putc(c);
311
}
312
 
313
 
314
//--------------------------------------------------------------
315
// Function: uart1_available()
316
// Purpose:  Determine the number of bytes waiting in the receive buffer
317
// Input:    None
318
// Returns:  Integer number of bytes in the receive buffer
319
//--------------------------------------------------------------
320
uint16_t uart1_available(void)
321
{
322
        return (UART_RX_BUFFER_MASK + UART1_RxHead - UART1_RxTail) % UART_RX_BUFFER_MASK;
323
}
324
 
325
 
326
 
327
//--------------------------------------------------------------
328
// Function: uart1_flush()
329
// Purpose:  Flush bytes waiting the receive buffer.  Acutally ignores them.
330
// Input:    None
331
// Returns:  None
332
//--------------------------------------------------------------
333
void uart1_flush(void)
334
{
335
        UART1_RxHead = UART1_RxTail;
336
}
337
 
338
 
339
/*************************************************************************
340
Function: utoa()
341
Purpose:  convert unsigned integer to ascii
342
Input:    string_buffer, string_buffer_size, unsigned integer value
343
Returns:  first ascii character
344
**************************************************************************/
345
char *utoa1(char* buffer, const unsigned int size, unsigned int value)
346
{
347
  char *p;
348
 
349
  // p points to last char
350
  p = buffer+size-1;
351
 
352
  // Set terminating Zero
353
  *p='\0';
354
 
355
  do
356
  {
357
    *--p = value%10 + '0';
358
    value = value/10;
359
  } while (value!=0 && p>buffer);
360
 
361
  return p;
362
}/* utoa */
363
 
364
 
365
 
366
 
367
 
368
 
369
#endif
370
#endif
371