Revision 205
| trunk/cardbox/serial.c (revision 205) | ||
|---|---|---|
| 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 |
// Disables the UART receiver |
|
| 59 |
void serial_disable_rx (void) {
|
|
| 60 |
UCSR0B &= ~(1<<RXEN0); |
|
| 61 |
} |
|
| 62 |
|
|
| 63 |
// Enables the UART receiver |
|
| 64 |
void serial_enable_rx(void) {
|
|
| 65 |
UCSR0B |= (1<<RXEN0); |
|
| 66 |
} |
|
| 67 |
|
|
| 68 |
// uart_putchar - Low-level function which puts a value into the |
|
| 69 |
// Tx buffer for transmission. Automatically injects |
|
| 70 |
// a \r before all \n's |
|
| 71 |
int serial_putchar(char c) |
|
| 72 |
{
|
|
| 73 |
|
|
| 74 |
if (c == '\n') |
|
| 75 |
putchar('\r');
|
|
| 76 |
// Wait until buffer is clear for sending |
|
| 77 |
loop_until_bit_is_set(UCSR0A, UDRE0); |
|
| 78 |
//while ( !(UCSR0A & (1<<UDRE0))); |
|
| 79 |
|
|
| 80 |
// Load buffer with your character |
|
| 81 |
UDR0 = c; |
|
| 82 |
return 0; |
|
| 83 |
} |
|
| 84 |
|
|
| 85 |
// uart_getchar - Low-level function which waits for the Rx buffer |
|
| 86 |
// to be filled, and then reads one character out of it. |
|
| 87 |
// Note that this function blocks on read - it will wait until |
|
| 88 |
// something fills the Rx buffer. |
|
| 89 |
int serial_getchar(void) |
|
| 90 |
{
|
|
| 91 |
char c; |
|
| 92 |
// Wait for the receive buffer to be filled |
|
| 93 |
loop_until_bit_is_set(UCSR0A, RXC0); |
|
| 94 |
|
|
| 95 |
// Read the receive buffer |
|
| 96 |
c = UDR0; |
|
| 97 |
return c; |
|
| 98 |
} |
|
| 99 |
|
|
| 100 |
// uart_getchar_nb - Low-level function which checks if the Rx buffer |
|
| 101 |
// is filled, and then reads one character out of it. |
|
| 102 |
// This is a non blocking version uart_getchar |
|
| 103 |
int serial_getchar_nb(void) |
|
| 104 |
{
|
|
| 105 |
char c; |
|
| 106 |
// Wait for the receive buffer to be filled |
|
| 107 |
//loop_until_bit_is_set(UCSR0A, RXC0); |
|
| 108 |
if (UCSR0A & _BV(RXC0)){
|
|
| 109 |
// Read the receive buffer |
|
| 110 |
c = UDR0; |
|
| 111 |
return c; |
|
| 112 |
} |
|
| 113 |
return 0; |
|
| 114 |
|
|
| 115 |
} |
|
| 116 |
|
|
| 117 |
|
|
| trunk/cardbox/serial.h (revision 205) | ||
|---|---|---|
| 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.h - Contains definitions and function prototypes for the RS232 serial port |
|
| 21 |
*/ |
|
| 22 |
|
|
| 23 |
#ifndef _SERIAL_H |
|
| 24 |
#define _SERIAL_H |
|
| 25 |
|
|
| 26 |
// Tested baud rates |
|
| 27 |
#define BAUD9600 51 |
|
| 28 |
#define BAUD115200 8 //Warning--3.5% error |
|
| 29 |
|
|
| 30 |
// Untested baud rates that might be right --aaron |
|
| 31 |
#define BAUD1M 0 |
|
| 32 |
#define BAUD500K 1 |
|
| 33 |
#define BAUD250K 3 |
|
| 34 |
#define BAUD230400 3 //Warning--8.5% error |
|
| 35 |
#define BAUD76800 12 |
|
| 36 |
#define BAUD57600 16 //Warning--2.1% error |
|
| 37 |
#define BAUD38400 25 |
|
| 38 |
#define BAUD28800 34 |
|
| 39 |
#define BAUD19200 51 |
|
| 40 |
#define BAUD14400 68 |
|
| 41 |
#define BAUD4800 207 |
|
| 42 |
#define BAUD2400 416 //Might not work, since it needs some high bits set |
|
| 43 |
|
|
| 44 |
// Function descriptions are available in serial.c |
|
| 45 |
|
|
| 46 |
//serial (serial0) is the ttl serial (wireless) |
|
| 47 |
void serial_init( unsigned int ubrr); |
|
| 48 |
void serial_disable_rx(void); |
|
| 49 |
void serial_enable_rx(void); |
|
| 50 |
int serial_putchar(char c); |
|
| 51 |
int serial_getchar(void); |
|
| 52 |
int serial_getchar_nb(void); |
|
| 53 |
|
|
| 54 |
//serial1 is the rs-232 port (db9 connector) (except on robot0 where both are serial0) |
|
| 55 |
void serial1_init( unsigned int ubrr); |
|
| 56 |
int serial1_putchar(char c); |
|
| 57 |
int serial1_getchar(void); |
|
| 58 |
int serial1_getchar_nb(void); |
|
| 59 |
|
|
| 60 |
|
|
| 61 |
|
|
| 62 |
#endif |
|
Also available in: Unified diff