Revision 186
| trunk/common/rs485_poll.h (revision 186) | ||
|---|---|---|
| 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 |
#endif |
|
| trunk/common/tooltron.h (revision 186) | ||
|---|---|---|
| 76 | 76 |
|
| 77 | 77 |
// Jump table functions for locations that are in the bootloader |
| 78 | 78 |
#ifdef TOOLBOX |
| 79 |
void (*init_uart)(uint16_t baud) = JT_INIT_UART; |
|
| 79 |
void (*uart_init)(uint16_t baud) = JT_UART_INIT; |
|
| 80 | 80 |
int8_t (*uart_get_byte)(uint8_t *output_byte) = JT_UART_GET_BYTE; |
| 81 | 81 |
void (*uart_send_byte)(uint8_t data) = JT_UART_SEND_BYTE; |
| 82 | 82 |
void (*uart_toggle_transmit)(uint8_t state) = JT_UART_TOGGLE_TRANSMIT; |
| trunk/common/rs485_poll.c (revision 186) | ||
|---|---|---|
| 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 |
#include <rs485_poll.h> |
|
| 14 |
|
|
| 15 |
static void rs485_toggle_transmit(uint8_t state); |
|
| 16 |
|
|
| 17 |
/** |
|
| 18 |
* @brief Initializes the uart |
|
| 19 |
* @param baud The baudrate. Use the definitions in rs485_sw.h |
|
| 20 |
*/ |
|
| 21 |
void rs485_init(uint16_t baud) {
|
|
| 22 |
// Set baud rate |
|
| 23 |
UBRRH = (uint8_t)(baud>>8); |
|
| 24 |
UBRRL = (uint8_t)baud; |
|
| 25 |
|
|
| 26 |
// Enable RX/TX |
|
| 27 |
UCSRB = _BV(RXEN) | _BV(TXEN); |
|
| 28 |
|
|
| 29 |
// Enable the TXEN pin as output |
|
| 30 |
DDRD |= TX_EN; |
|
| 31 |
rs485_toggle_transmit(RS485_TX_OFF); |
|
| 32 |
} |
|
| 33 |
|
|
| 34 |
/** |
|
| 35 |
* @brief Non-blocking receive |
|
| 36 |
* |
|
| 37 |
* Receives a byte from the UART if one exists. If none exist the function |
|
| 38 |
* will return an error code immediately. If there is a byte to receive, |
|
| 39 |
* it will be placed in a passed in pointer and return. |
|
| 40 |
* |
|
| 41 |
* @pre The RS485 TXEN line is not enabled |
|
| 42 |
* @param output_byte Byte to store the received message |
|
| 43 |
* @return 0 on success, negative if there is nothing to receive |
|
| 44 |
*/ |
|
| 45 |
int8_t rs485_get_byte(uint8_t *output_byte) {
|
|
| 46 |
if (UCSRA & _BV(RXC)) {
|
|
| 47 |
*output_byte = UDR; |
|
| 48 |
return 0; |
|
| 49 |
} else {
|
|
| 50 |
return -1; |
|
| 51 |
} |
|
| 52 |
} |
|
| 53 |
|
|
| 54 |
/** |
|
| 55 |
* @brief Blocking send |
|
| 56 |
* |
|
| 57 |
* Sends the byte in data onto the network. The function will block until |
|
| 58 |
* the transmit is complete so that the transmit enable line can be toggled |
|
| 59 |
* safely. It will wait until any current transmission completes before sending |
|
| 60 |
* and will wait until it completes before returning. The RS485 TX enable line |
|
| 61 |
* is automatically toggled by the function and should not be done so |
|
| 62 |
* outside the function. |
|
| 63 |
* |
|
| 64 |
* @param data The byte to send |
|
| 65 |
*/ |
|
| 66 |
void rs485_send_byte(uint8_t data) {
|
|
| 67 |
//Waits until current transmit is done |
|
| 68 |
while (!(UCSRA & _BV(UDRE))); |
|
| 69 |
|
|
| 70 |
// Enable writes and send |
|
| 71 |
rs485_toggle_transmit(RS485_TX_ON); |
|
| 72 |
UDR = data; |
|
| 73 |
|
|
| 74 |
// Waits until the transmit is done |
|
| 75 |
while(!(UCSRA & _BV(TXC))); |
|
| 76 |
rs485_toggle_transmit(RS485_TX_OFF); |
|
| 77 |
UCSRA |= _BV(TXC); |
|
| 78 |
|
|
| 79 |
return; |
|
| 80 |
} |
|
| 81 |
|
|
| 82 |
/** |
|
| 83 |
* @brief Toggles the RS485 TXEN line |
|
| 84 |
* |
|
| 85 |
* This function should not be used outside of this file |
|
| 86 |
* |
|
| 87 |
* @param state Whether to turn the pin on or off |
|
| 88 |
*/ |
|
| 89 |
static void rs485_toggle_transmit(uint8_t state) {
|
|
| 90 |
if (state == RS485_TX_ON) {
|
|
| 91 |
PORTD |= TX_EN; |
|
| 92 |
} else {
|
|
| 93 |
PORTD &= ~TX_EN; |
|
| 94 |
} |
|
| 95 |
} |
|
| trunk/bootloader/rs485_poll.h (revision 186) | ||
|---|---|---|
| 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 (revision 186) | ||
|---|---|---|
| 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 |
} |
|
| trunk/bootloader/bootloader.c (revision 186) | ||
|---|---|---|
| 2 | 2 |
#include <avr/boot.h> |
| 3 | 3 |
#include <avr/wdt.h> |
| 4 | 4 |
#include <tooltron.h> |
| 5 |
#include "rs485_sw.h" |
|
| 5 |
#include <rs485_poll.h> |
|
| 6 | 6 |
|
| 7 | 7 |
#define ADDR 18 |
| 8 | 8 |
|
| trunk/bootloader/Makefile (revision 186) | ||
|---|---|---|
| 86 | 86 |
# Each directory must be seperated by a space. |
| 87 | 87 |
# Use forward slashes for directory separators. |
| 88 | 88 |
# For a directory that has spaces, enclose it in quotes. |
| 89 |
#EXTRAINCDIRS = C:\WinAVR\include\fwr |
|
| 89 |
EXTRAINCDIRS = |
|
| 90 | 90 |
|
| 91 | 91 |
|
| 92 | 92 |
# Compiler flag to set the C Standard level. |
| ... | ... | |
| 102 | 102 |
|
| 103 | 103 |
|
| 104 | 104 |
# Place -I options here |
| 105 |
CINCS = -I ../common/ |
|
| 105 |
CINCS = |
|
| 106 | 106 |
|
| 107 | 107 |
|
| 108 | 108 |
|
Also available in: Unified diff