summaryrefslogtreecommitdiff
path: root/n/avr/rs232/rs232.c
blob: a3984f78694145517aa27b1bf5cd4580a0d14c39 (plain)
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
/* rs232.c */
/* n.avr.rs232 - AVR RS232 Module. {{{
 *
 * Copyright (C) 2004 Nicolas Schodet
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * Contact :
 *        Web: http://perso.efrei.fr/~schodet/
 *      Email: <contact@ni.fr.eu.org>
 * }}} */
#include "rs232.h"

#include <avr/io.h>

/* Tested AVR check. */
#if defined (__AVR_ATmega8__)
# define SELECTOR _BV (URSEL) 
#elif defined (__AVR_ATmega8535__)
# define SELECTOR _BV (URSEL) 
#elif defined (__AVR_ATmega128__)
# define UBRRH UBRR0H
# define UBRRL UBRR0L
# define UCSRA UCSR0A
# define UCSRB UCSR0B
# define UCSRC UCSR0C
# define UDR UDR0
# define SELECTOR 0
#else
# warning "rs232: not tested on this chip."
#endif

/* Unimplemented features. */
#if AC_RS232_INTERRUPT
# error "rs232: interrupts to be implemented."
#endif

#if AC_RS232_CHAR_SIZE != 8
# error "rs232: char size != 8 not implemented."
#endif

/* Baud rate error check. */
#define UBRR_VAL (AC_FREQ / 16 / AC_RS232_BAUDRATE - 1)
#define BAUDRATE (AC_FREQ / 16 / (UBRR_VAL + 1))

#if BAUDRATE - AC_RS232_BAUDRATE > 2
#warning "rs232: baud rate error > 2."
#elif BAUDRATE - AC_RS232_BAUDRATE > 1
#warning "rs232: baud rate error > 1."
#elif BAUDRATE - AC_RS232_BAUDRATE > 0
#warning "rs232: baud rate error > 0."
#endif

/* Parity. */
#define ODD (_BV (UPM1) | _BV (UPM0))
#define EVEN _BV (UPM1)
#define NONE 0
#define PARITY AC_RS232_PARITY

/* Stop bits. */
#if AC_RS232_STOP_BITS == 1
#define STOP_BITS 0
#elif AC_RS232_STOP_BITS == 2
#define STOP_BITS _BV (USBS)
#else
#error "rs232: bad stop bits value."
#endif

/* Character size. */
#define CHAR_SIZE (_BV (UCSZ1) | _BV (UCSZ0))

/* +AutoDec */
/* -AutoDec */

/* Initialise rs232. */
void
rs232_init (void)
{
    /* Set baud rate. */
    UBRRH = UBRR_VAL >> 8;
    UBRRL = UBRR_VAL & 0xff;
    UCSRA = 0;
    /* Set format and enable rs232. */
    UCSRC = SELECTOR | PARITY | STOP_BITS | CHAR_SIZE;
    UCSRB = _BV (RXEN) | _BV (TXEN);
}

/* Read a char. */
unsigned char
rs232_getc (void)
{
    loop_until_bit_is_set (UCSRA, RXC);
    return UDR;
}

/* Write a char. */
void
rs232_putc (unsigned char c)
{
    loop_until_bit_is_set (UCSRA, UDRE);
    UDR = c;
}