Revision 185
Renamed uart to rs485_poll because it is more descriptive of what it actually does. Some function name changes
trunk/bootloader/uart.h | ||
---|---|---|
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 |
/** @file rs485_sw.h |
|
20 |
* |
|
21 |
* @brief Implements rs485 using the uart hardware module. |
|
22 |
* |
|
23 |
* See rs485_sw.c for more information |
|
24 |
* |
|
25 |
* @author Kevin Woo (kwoo) |
|
26 |
*/ |
|
27 |
|
|
28 |
#ifndef _RS485_SW_H |
|
29 |
#define _RS485_SW_H |
|
30 |
|
|
31 |
#include <avr/io.h> |
|
32 |
#include <avr/interrupt.h> |
|
33 |
#include <stdint.h> |
|
34 |
|
|
35 |
#define RS485_TX_OFF 0 |
|
36 |
#define RS485_TX_ON 1 |
|
37 |
|
|
38 |
/** @brief RX Pin for the RS485 */ |
|
39 |
#define RX _BV(PORTD0) |
|
40 |
/** @brief TX Pin for the RS485 */ |
|
41 |
#define TX _BV(PORTD1) |
|
42 |
/** @brief TX Enable Pin for the RS485 */ |
|
43 |
#define TX_EN _BV(PORTD5) |
|
44 |
|
|
45 |
void uart_init(uint16_t baud); |
|
46 |
int8_t uart_get_byte(uint8_t *output_byte); |
|
47 |
void uart_send_byte(uint8_t data); |
|
48 |
static void uart_toggle_transmit(uint8_t state); |
|
49 |
#endif |
trunk/bootloader/rs485_sw.c | ||
---|---|---|
1 |
/** |
|
2 |
* @file rs485_sw.c |
|
3 |
* @brief RS485 implementation using UART hardware module and no interrupts |
|
4 |
* |
|
5 |
* This uses the AVR's UART hardware module but does polling rather than |
|
6 |
* interrupts to handle transmission and receiving. It assumes the use |
|
7 |
* of an external UART<->RS485 converter with the RXEN and TXEN pins tied |
|
8 |
* together. |
|
9 |
* |
|
10 |
* @author Kevin Woo <kwoo@2ndt.com> |
|
11 |
**/ |
|
12 |
|
|
13 |
/** |
|
14 |
* @brief Initializes the uart |
|
15 |
* @param baud The baudrate. Use the definitions in rs485_sw.h |
|
16 |
*/ |
|
17 |
void rs485_init(uint16_t baud) { |
|
18 |
// Set baud rate |
|
19 |
UBRRH = (uint8_t)(baud>>8); |
|
20 |
UBRRL = (uint8_t)baud; |
|
21 |
|
|
22 |
// Enable RX/TX |
|
23 |
UCSRB = _BV(RXEN) | _BV(TXEN); |
|
24 |
|
|
25 |
// Enable the TXEN pin as output |
|
26 |
DDRD |= TX_EN; |
|
27 |
rs485_toggle_transmit(RS485_TX_OFF); |
|
28 |
} |
|
29 |
|
|
30 |
/** |
|
31 |
* @brief Non-blocking receive |
|
32 |
* |
|
33 |
* Receives a byte from the UART if one exists. If none exist the function |
|
34 |
* will return an error code immediately. If there is a byte to receive, |
|
35 |
* it will be placed in a passed in pointer and return. |
|
36 |
* |
|
37 |
* @pre The RS485 TXEN line is not enabled |
|
38 |
* @param output_byte Byte to store the received message |
|
39 |
* @return 0 on success, negative if there is nothing to receive |
|
40 |
*/ |
|
41 |
int8_t rs485_get_byte(uint8_t *output_byte) { |
|
42 |
if (UCSRA & _BV(RXC)) { |
|
43 |
*output_byte = UDR; |
|
44 |
return 0; |
|
45 |
} else { |
|
46 |
return -1; |
|
47 |
} |
|
48 |
} |
|
49 |
|
|
50 |
/** |
|
51 |
* @brief Blocking send |
|
52 |
* |
|
53 |
* Sends the byte in data onto the network. The function will block until |
|
54 |
* the transmit is complete so that the transmit enable line can be toggled |
|
55 |
* safely. It will wait until any current transmission completes before sending |
|
56 |
* and will wait until it completes before returning. The RS485 TX enable line |
|
57 |
* is automatically toggled by the function and should not be done so |
|
58 |
* outside the function. |
|
59 |
* |
|
60 |
* @param data The byte to send |
|
61 |
*/ |
|
62 |
void rs485_send_byte(uint8_t data) { |
|
63 |
//Waits until current transmit is done |
|
64 |
while (!(UCSRA & _BV(UDRE))); |
|
65 |
|
|
66 |
// Enable writes and send |
|
67 |
rs485_toggle_transmit(RS485_TX_ON); |
|
68 |
UDR = data; |
|
69 |
|
|
70 |
// Waits until the transmit is done |
|
71 |
while(!(UCSRA & _BV(TXC))); |
|
72 |
rs485_toggle_transmit(RS485_TX_OFF); |
|
73 |
UCSRA |= _BV(TXC); |
|
74 |
|
|
75 |
return; |
|
76 |
} |
|
77 |
|
|
78 |
/** |
|
79 |
* @brief Toggles the RS485 TXEN line |
|
80 |
* |
|
81 |
* This function should not be used outside of this file |
|
82 |
* |
|
83 |
* @param state Whether to turn the pin on or off |
|
84 |
*/ |
|
85 |
void rs485_toggle_transmit(uint8_t state) { |
|
86 |
if (state == RS485_TX_ON) { |
|
87 |
PORTD |= TX_EN; |
|
88 |
} else { |
|
89 |
PORTD &= ~TX_EN; |
|
90 |
} |
|
91 |
} |
trunk/bootloader/rs485_poll.h | ||
---|---|---|
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 |
* @file rs485_poll.h |
|
21 |
* @brief Implements rs485 using the uart hardware module. |
|
22 |
* |
|
23 |
* See rs485_sw.c for more information |
|
24 |
* |
|
25 |
* @author Kevin Woo (kwoo) |
|
26 |
*/ |
|
27 |
|
|
28 |
#ifndef _RS485_SW_H |
|
29 |
#define _RS485_SW_H |
|
30 |
|
|
31 |
#include <avr/io.h> |
|
32 |
#include <avr/interrupt.h> |
|
33 |
#include <stdint.h> |
|
34 |
|
|
35 |
#define RS485_TX_OFF 0 |
|
36 |
#define RS485_TX_ON 1 |
|
37 |
|
|
38 |
/** @brief RX Pin for the RS485 */ |
|
39 |
#define RX _BV(PORTD0) |
|
40 |
/** @brief TX Pin for the RS485 */ |
|
41 |
#define TX _BV(PORTD1) |
|
42 |
/** @brief TX Enable Pin for the RS485 */ |
|
43 |
#define TX_EN _BV(PORTD5) |
|
44 |
|
|
45 |
void rs485_init(uint16_t baud); |
|
46 |
int8_t rs485_get_byte(uint8_t *output_byte); |
|
47 |
void rs485_send_byte(uint8_t data); |
|
48 |
static void rs485_toggle_transmit(uint8_t state); |
|
49 |
#endif |
trunk/bootloader/rs485_poll.c | ||
---|---|---|
1 |
/** |
|
2 |
* @file rs485_poll.c |
|
3 |
* @brief RS485 implementation using UART hardware module and polling |
|
4 |
* |
|
5 |
* This uses the AVR's UART hardware module but does polling rather than |
|
6 |
* interrupts to handle transmission and receiving. It assumes the use |
|
7 |
* of an external UART<->RS485 converter with the RXEN and TXEN pins tied |
|
8 |
* together. |
|
9 |
* |
|
10 |
* @author Kevin Woo <kwoo@2ndt.com> |
|
11 |
**/ |
|
12 |
|
|
13 |
/** |
|
14 |
* @brief Initializes the uart |
|
15 |
* @param baud The baudrate. Use the definitions in rs485_sw.h |
|
16 |
*/ |
|
17 |
void rs485_init(uint16_t baud) { |
|
18 |
// Set baud rate |
|
19 |
UBRRH = (uint8_t)(baud>>8); |
|
20 |
UBRRL = (uint8_t)baud; |
|
21 |
|
|
22 |
// Enable RX/TX |
|
23 |
UCSRB = _BV(RXEN) | _BV(TXEN); |
|
24 |
|
|
25 |
// Enable the TXEN pin as output |
|
26 |
DDRD |= TX_EN; |
|
27 |
rs485_toggle_transmit(RS485_TX_OFF); |
|
28 |
} |
|
29 |
|
|
30 |
/** |
|
31 |
* @brief Non-blocking receive |
|
32 |
* |
|
33 |
* Receives a byte from the UART if one exists. If none exist the function |
|
34 |
* will return an error code immediately. If there is a byte to receive, |
|
35 |
* it will be placed in a passed in pointer and return. |
|
36 |
* |
|
37 |
* @pre The RS485 TXEN line is not enabled |
|
38 |
* @param output_byte Byte to store the received message |
|
39 |
* @return 0 on success, negative if there is nothing to receive |
|
40 |
*/ |
|
41 |
int8_t rs485_get_byte(uint8_t *output_byte) { |
|
42 |
if (UCSRA & _BV(RXC)) { |
|
43 |
*output_byte = UDR; |
|
44 |
return 0; |
|
45 |
} else { |
|
46 |
return -1; |
|
47 |
} |
|
48 |
} |
|
49 |
|
|
50 |
/** |
|
51 |
* @brief Blocking send |
|
52 |
* |
|
53 |
* Sends the byte in data onto the network. The function will block until |
|
54 |
* the transmit is complete so that the transmit enable line can be toggled |
|
55 |
* safely. It will wait until any current transmission completes before sending |
|
56 |
* and will wait until it completes before returning. The RS485 TX enable line |
|
57 |
* is automatically toggled by the function and should not be done so |
|
58 |
* outside the function. |
|
59 |
* |
|
60 |
* @param data The byte to send |
|
61 |
*/ |
|
62 |
void rs485_send_byte(uint8_t data) { |
|
63 |
//Waits until current transmit is done |
|
64 |
while (!(UCSRA & _BV(UDRE))); |
|
65 |
|
|
66 |
// Enable writes and send |
|
67 |
rs485_toggle_transmit(RS485_TX_ON); |
|
68 |
UDR = data; |
|
69 |
|
|
70 |
// Waits until the transmit is done |
|
71 |
while(!(UCSRA & _BV(TXC))); |
|
72 |
rs485_toggle_transmit(RS485_TX_OFF); |
|
73 |
UCSRA |= _BV(TXC); |
|
74 |
|
|
75 |
return; |
|
76 |
} |
|
77 |
|
|
78 |
/** |
|
79 |
* @brief Toggles the RS485 TXEN line |
|
80 |
* |
|
81 |
* This function should not be used outside of this file |
|
82 |
* |
|
83 |
* @param state Whether to turn the pin on or off |
|
84 |
*/ |
|
85 |
void rs485_toggle_transmit(uint8_t state) { |
|
86 |
if (state == RS485_TX_ON) { |
|
87 |
PORTD |= TX_EN; |
|
88 |
} else { |
|
89 |
PORTD &= ~TX_EN; |
|
90 |
} |
|
91 |
} |
Also available in: Unified diff