root / freemodbus / port / portserial.c @ ab2834f1
History | View | Annotate | Download (3.6 KB)
1 | 20e5429c | Tom Mullins | /*
|
---|---|---|---|
2 | * FreeModbus Libary: ATMega168 Port
|
||
3 | * Copyright (C) 2006 Christian Walter <wolti@sil.at>
|
||
4 | * - Initial version and ATmega168 support
|
||
5 | * Modfications Copyright (C) 2006 Tran Minh Hoang:
|
||
6 | * - ATmega8, ATmega16, ATmega32 support
|
||
7 | * - RS485 support for DS75176
|
||
8 | *
|
||
9 | * This library is free software; you can redistribute it and/or
|
||
10 | * modify it under the terms of the GNU Lesser General Public
|
||
11 | * License as published by the Free Software Foundation; either
|
||
12 | * version 2.1 of the License, or (at your option) any later version.
|
||
13 | *
|
||
14 | * This library is distributed in the hope that it will be useful,
|
||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||
17 | * Lesser General Public License for more details.
|
||
18 | *
|
||
19 | * You should have received a copy of the GNU Lesser General Public
|
||
20 | * License along with this library; if not, write to the Free Software
|
||
21 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||
22 | *
|
||
23 | * File: $Id: portserial.c,v 1.6 2006/09/17 16:45:53 wolti Exp $
|
||
24 | */
|
||
25 | |||
26 | #include <avr/io.h> |
||
27 | #include <avr/interrupt.h> |
||
28 | #include <avr/signal.h> |
||
29 | |||
30 | #include "port.h" |
||
31 | |||
32 | /* ----------------------- Modbus includes ----------------------------------*/
|
||
33 | #include "mb.h" |
||
34 | #include "mbport.h" |
||
35 | |||
36 | #define UART_BAUD_RATE 9600 |
||
37 | #define UART_BAUD_CALC(UART_BAUD_RATE,F_OSC) \
|
||
38 | ( ( F_OSC ) / ( ( UART_BAUD_RATE ) * 16UL ) - 1 ) |
||
39 | |||
40 | //#define UART_UCSRB UCSR0B
|
||
41 | |||
42 | void
|
||
43 | vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable ) |
||
44 | { |
||
45 | #ifdef RTS_ENABLE
|
||
46 | UCSRB |= _BV( TXEN ) | _BV(TXCIE); |
||
47 | #else
|
||
48 | UCSRB |= _BV( TXEN ); |
||
49 | #endif
|
||
50 | |||
51 | if( xRxEnable )
|
||
52 | { |
||
53 | UCSRB |= _BV( RXEN ) | _BV( RXCIE ); |
||
54 | } |
||
55 | else
|
||
56 | { |
||
57 | UCSRB &= ~( _BV( RXEN ) | _BV( RXCIE ) ); |
||
58 | } |
||
59 | |||
60 | if( xTxEnable )
|
||
61 | { |
||
62 | UCSRB |= _BV( TXEN ) | _BV( UDRE ); |
||
63 | #ifdef RTS_ENABLE
|
||
64 | RTS_HIGH; |
||
65 | #endif
|
||
66 | } |
||
67 | else
|
||
68 | { |
||
69 | UCSRB &= ~( _BV( UDRE ) ); |
||
70 | } |
||
71 | } |
||
72 | |||
73 | BOOL |
||
74 | xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity ) |
||
75 | { |
||
76 | UCHAR ucUCSRC = 0;
|
||
77 | |||
78 | /* prevent compiler warning. */
|
||
79 | (void)ucPORT;
|
||
80 | |||
81 | UBRR = UART_BAUD_CALC( ulBaudRate, F_CPU ); |
||
82 | |||
83 | switch ( eParity )
|
||
84 | { |
||
85 | case MB_PAR_EVEN:
|
||
86 | ucUCSRC |= _BV( UPM1 ); |
||
87 | break;
|
||
88 | case MB_PAR_ODD:
|
||
89 | ucUCSRC |= _BV( UPM1 ) | _BV( UPM0 ); |
||
90 | break;
|
||
91 | case MB_PAR_NONE:
|
||
92 | break;
|
||
93 | } |
||
94 | |||
95 | switch ( ucDataBits )
|
||
96 | { |
||
97 | case 8: |
||
98 | ucUCSRC |= _BV( UCSZ0 ) | _BV( UCSZ1 ); |
||
99 | break;
|
||
100 | case 7: |
||
101 | ucUCSRC |= _BV( UCSZ1 ); |
||
102 | break;
|
||
103 | } |
||
104 | |||
105 | #if defined (__AVR_ATmega168__)
|
||
106 | UCSRC |= ucUCSRC; |
||
107 | #elif defined (__AVR_ATmega169__)
|
||
108 | UCSRC |= ucUCSRC; |
||
109 | #elif defined (__AVR_ATmega8__)
|
||
110 | UCSRC = _BV( URSEL ) | ucUCSRC; |
||
111 | #elif defined (__AVR_ATmega16__)
|
||
112 | UCSRC = _BV( URSEL ) | ucUCSRC; |
||
113 | #elif defined (__AVR_ATmega32__)
|
||
114 | UCSRC = _BV( URSEL ) | ucUCSRC; |
||
115 | #elif defined (__AVR_ATmega128__)
|
||
116 | UCSRC |= ucUCSRC; |
||
117 | ab2834f1 | Tom Mullins | #elif defined (__AVR_ATtiny1634__)
|
118 | UCSRC |= ucUCSRC; |
||
119 | 20e5429c | Tom Mullins | #endif
|
120 | |||
121 | vMBPortSerialEnable( FALSE, FALSE ); |
||
122 | |||
123 | #ifdef RTS_ENABLE
|
||
124 | RTS_INIT; |
||
125 | #endif
|
||
126 | return TRUE;
|
||
127 | } |
||
128 | |||
129 | BOOL |
||
130 | xMBPortSerialPutByte( CHAR ucByte ) |
||
131 | { |
||
132 | UDR = ucByte; |
||
133 | return TRUE;
|
||
134 | } |
||
135 | |||
136 | BOOL |
||
137 | xMBPortSerialGetByte( CHAR * pucByte ) |
||
138 | { |
||
139 | *pucByte = UDR; |
||
140 | return TRUE;
|
||
141 | } |
||
142 | |||
143 | SIGNAL( SIG_USART_DATA ) |
||
144 | { |
||
145 | pxMBFrameCBTransmitterEmpty( ); |
||
146 | } |
||
147 | |||
148 | SIGNAL( SIG_USART_RECV ) |
||
149 | { |
||
150 | pxMBFrameCBByteReceived( ); |
||
151 | } |
||
152 | |||
153 | #ifdef RTS_ENABLE
|
||
154 | SIGNAL( SIG_UART_TRANS ) |
||
155 | { |
||
156 | RTS_LOW; |
||
157 | } |
||
158 | #endif
|
||
159 |