root / trunk / cardbox / rs485_int.c @ 206
History | View | Annotate | Download (1.96 KB)
1 | 139 | kwoo | /********
|
---|---|---|---|
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 | 8 | kwoo | /** @file uart.c
|
20 | *
|
||
21 | * @brief Implements UART functionality in hardware
|
||
22 | *
|
||
23 | * @author Kevin Woo (kwoo)
|
||
24 | */
|
||
25 | |||
26 | #include "uart.h" |
||
27 | 143 | bneuman | #include <util/delay.h> |
28 | 109 | kwoo | #include <stdint.h> |
29 | 8 | kwoo | |
30 | 109 | kwoo | uint8_t received_byte; //Byte received
|
31 | uint8_t byte_ready; //New byte has been received
|
||
32 | 8 | kwoo | |
33 | 109 | kwoo | void init_uart(uint16_t baud) {
|
34 | // Set baud rate
|
||
35 | UBRRH = (uint8_t)(baud>>8);
|
||
36 | UBRRL = (uint8_t)baud; |
||
37 | |||
38 | // Enable RX/TX and RX/TX Interrupt
|
||
39 | 9 | kwoo | UCSRB = _BV(RXCIE) | _BV(RXEN) | _BV(TXCIE) | _BV(TXEN); |
40 | 8 | kwoo | |
41 | 109 | kwoo | // Enable the TXEN pin as output
|
42 | DDRD |= TX_EN; |
||
43 | |||
44 | // Initialize receive variables
|
||
45 | byte_ready = 0;
|
||
46 | received_byte = 0x0;
|
||
47 | 8 | kwoo | } |
48 | |||
49 | 109 | kwoo | int8_t uart_get_byte(uint8_t *output_byte) { |
50 | 8 | kwoo | if (byte_ready) {
|
51 | 109 | kwoo | byte_ready = 0;
|
52 | 8 | kwoo | *output_byte = received_byte; |
53 | return 0; |
||
54 | } else {
|
||
55 | return -1; |
||
56 | } |
||
57 | } |
||
58 | |||
59 | 109 | kwoo | void uart_send_byte(uint8_t data) {
|
60 | 13 | kwoo | //Waits until current transmit is done
|
61 | 109 | kwoo | while (!(UCSRA & _BV(UDRE)));
|
62 | 13 | kwoo | |
63 | 109 | kwoo | // Enable writes and send
|
64 | uart_toggle_transmit(UART_TX_ON); |
||
65 | UDR = data; |
||
66 | 9 | kwoo | return;
|
67 | } |
||
68 | |||
69 | 109 | kwoo | void uart_toggle_transmit(uint8_t state) {
|
70 | 13 | kwoo | if (state == UART_TX_ON) {
|
71 | 109 | kwoo | PORTD |= TX_EN; |
72 | 13 | kwoo | } else {
|
73 | 109 | kwoo | PORTD &= ~TX_EN; |
74 | 13 | kwoo | } |
75 | } |
||
76 | |||
77 | 109 | kwoo | ISR(USART_RX_vect) { |
78 | received_byte = UDR; |
||
79 | 117 | kwoo | byte_ready = 1;
|
80 | 8 | kwoo | } |
81 | 9 | kwoo | |
82 | 109 | kwoo | ISR(USART_TX_vect) { |
83 | // Re-enable reads
|
||
84 | uart_toggle_transmit(UART_TX_OFF); |
||
85 | 9 | kwoo | } |