Revision 139
Added LGPL to all source code except the hardware schematics.
serial.c | ||
---|---|---|
1 |
/* |
|
2 |
serial.c - Functions for using the RS232 serial port |
|
3 |
|
|
4 |
author: Robotics Club, Colony Project |
|
5 |
|
|
6 |
much code taken from FWR's library, author: Tom Lauwers |
|
7 |
|
|
8 |
|
|
9 |
general note - serial -> uart |
|
10 |
serial1 -> uart1 |
|
11 |
this is done for clarity purposes |
|
12 |
*/ |
|
13 |
|
|
14 |
|
|
15 |
#include <avr/io.h> |
|
16 |
#include <stdio.h> |
|
17 |
#include "serial.h" |
|
18 |
|
|
19 |
//setup uart0 (serial) |
|
20 |
void serial_init(unsigned int ubrr) |
|
21 |
{ |
|
22 |
DDRD |= (1<<PD1); |
|
23 |
DDRD &= ~(1<<PD0); |
|
24 |
|
|
25 |
UDR0 = 0; |
|
26 |
|
|
27 |
/*Set baud rate - baud rates under 4800bps unsupported */ |
|
28 |
UBRR0H = 0x00; |
|
29 |
UBRR0L = (unsigned char)ubrr; |
|
30 |
|
|
31 |
//UCSR0A |= (1<<U2X0); |
|
32 |
|
|
33 |
/*Enable receiver and transmitter */ |
|
34 |
UCSR0B |= (1<<RXEN0)|(1<<TXEN0); |
|
35 |
|
|
36 |
/* Set frame format: 8data, 1stop bit, asynchronous normal mode */ |
|
37 |
UCSR0C |= (1<<UCSZ00) | (1<<UCSZ01); |
|
38 |
} |
|
39 |
|
|
40 |
// uart_putchar - Low-level function which puts a value into the |
|
41 |
// Tx buffer for transmission. Automatically injects |
|
42 |
// a \r before all \n's |
|
43 |
int serial_putchar(char c) |
|
44 |
{ |
|
45 |
|
|
46 |
if (c == '\n') |
|
47 |
putchar('\r'); |
|
48 |
// Wait until buffer is clear for sending |
|
49 |
loop_until_bit_is_set(UCSR0A, UDRE0); |
|
50 |
//while ( !(UCSR0A & (1<<UDRE0))); |
|
51 |
|
|
52 |
// Load buffer with your character |
|
53 |
UDR0 = c; |
|
54 |
return 0; |
|
55 |
} |
|
56 |
|
|
57 |
// uart_getchar - Low-level function which waits for the Rx buffer |
|
58 |
// to be filled, and then reads one character out of it. |
|
59 |
// Note that this function blocks on read - it will wait until |
|
60 |
// something fills the Rx buffer. |
|
61 |
int serial_getchar(void) |
|
62 |
{ |
|
63 |
char c; |
|
64 |
// Wait for the receive buffer to be filled |
|
65 |
loop_until_bit_is_set(UCSR0A, RXC0); |
|
66 |
|
|
67 |
// Read the receive buffer |
|
68 |
c = UDR0; |
|
69 |
return c; |
|
70 |
} |
|
71 |
|
|
72 |
// uart_getchar_nb - Low-level function which checks if the Rx buffer |
|
73 |
// is filled, and then reads one character out of it. |
|
74 |
// This is a non blocking version uart_getchar |
|
75 |
int serial_getchar_nb(void) |
|
76 |
{ |
|
77 |
char c; |
|
78 |
// Wait for the receive buffer to be filled |
|
79 |
//loop_until_bit_is_set(UCSR0A, RXC0); |
|
80 |
if (UCSR0A & _BV(RXC0)){ |
|
81 |
// Read the receive buffer |
|
82 |
c = UDR0; |
|
83 |
return c; |
|
84 |
} |
|
85 |
return 0; |
|
86 |
|
|
87 |
} |
|
88 |
|
|
89 |
|
|
1 |
/******** |
|
2 |
* This file is part of Tooltron. |
|
3 |
* |
|
4 |
* Tooltron is free software: you can redistribute it and/or modify |
|
5 |
* it under the terms of the Lesser GNU General Public License as published by |
|
6 |
* the Free Software Foundation, either version 3 of the License, or |
|
7 |
* (at your option) any later version. |
|
8 |
* |
|
9 |
* Tooltron is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* Lesser GNU General Public License for more details. |
|
13 |
* You should have received a copy of the Lesser GNU General Public License |
|
14 |
* along with Tooltron. If not, see <http://www.gnu.org/licenses/>. |
|
15 |
* |
|
16 |
* Copyright 2009 Kevin Woo <kwoo@2ndt.com> |
|
17 |
* |
|
18 |
********/ |
|
19 |
/* |
|
20 |
serial.c - Functions for using the RS232 serial port |
|
21 |
|
|
22 |
author: Robotics Club, Colony Project |
|
23 |
|
|
24 |
much code taken from FWR's library, author: Tom Lauwers |
|
25 |
|
|
26 |
|
|
27 |
general note - serial -> uart |
|
28 |
serial1 -> uart1 |
|
29 |
this is done for clarity purposes |
|
30 |
*/ |
|
31 |
|
|
32 |
|
|
33 |
#include <avr/io.h> |
|
34 |
#include <stdio.h> |
|
35 |
#include "serial.h" |
|
36 |
|
|
37 |
//setup uart0 (serial) |
|
38 |
void serial_init(unsigned int ubrr) |
|
39 |
{ |
|
40 |
DDRD |= (1<<PD1); |
|
41 |
DDRD &= ~(1<<PD0); |
|
42 |
|
|
43 |
UDR0 = 0; |
|
44 |
|
|
45 |
/*Set baud rate - baud rates under 4800bps unsupported */ |
|
46 |
UBRR0H = 0x00; |
|
47 |
UBRR0L = (unsigned char)ubrr; |
|
48 |
|
|
49 |
//UCSR0A |= (1<<U2X0); |
|
50 |
|
|
51 |
/*Enable receiver and transmitter */ |
|
52 |
UCSR0B |= (1<<RXEN0)|(1<<TXEN0); |
|
53 |
|
|
54 |
/* Set frame format: 8data, 1stop bit, asynchronous normal mode */ |
|
55 |
UCSR0C |= (1<<UCSZ00) | (1<<UCSZ01); |
|
56 |
} |
|
57 |
|
|
58 |
// uart_putchar - Low-level function which puts a value into the |
|
59 |
// Tx buffer for transmission. Automatically injects |
|
60 |
// a \r before all \n's |
|
61 |
int serial_putchar(char c) |
|
62 |
{ |
|
63 |
|
|
64 |
if (c == '\n') |
|
65 |
putchar('\r'); |
|
66 |
// Wait until buffer is clear for sending |
|
67 |
loop_until_bit_is_set(UCSR0A, UDRE0); |
|
68 |
//while ( !(UCSR0A & (1<<UDRE0))); |
|
69 |
|
|
70 |
// Load buffer with your character |
|
71 |
UDR0 = c; |
|
72 |
return 0; |
|
73 |
} |
|
74 |
|
|
75 |
// uart_getchar - Low-level function which waits for the Rx buffer |
|
76 |
// to be filled, and then reads one character out of it. |
|
77 |
// Note that this function blocks on read - it will wait until |
|
78 |
// something fills the Rx buffer. |
|
79 |
int serial_getchar(void) |
|
80 |
{ |
|
81 |
char c; |
|
82 |
// Wait for the receive buffer to be filled |
|
83 |
loop_until_bit_is_set(UCSR0A, RXC0); |
|
84 |
|
|
85 |
// Read the receive buffer |
|
86 |
c = UDR0; |
|
87 |
return c; |
|
88 |
} |
|
89 |
|
|
90 |
// uart_getchar_nb - Low-level function which checks if the Rx buffer |
|
91 |
// is filled, and then reads one character out of it. |
|
92 |
// This is a non blocking version uart_getchar |
|
93 |
int serial_getchar_nb(void) |
|
94 |
{ |
|
95 |
char c; |
|
96 |
// Wait for the receive buffer to be filled |
|
97 |
//loop_until_bit_is_set(UCSR0A, RXC0); |
|
98 |
if (UCSR0A & _BV(RXC0)){ |
|
99 |
// Read the receive buffer |
|
100 |
c = UDR0; |
|
101 |
return c; |
|
102 |
} |
|
103 |
return 0; |
|
104 |
|
|
105 |
} |
|
106 |
|
|
107 |
|
Also available in: Unified diff