Statistics
| Branch: | Revision:

root / toolbox / freemodbus / port / portserial.c @ 036b4f9a

History | View | Annotate | Download (3.67 KB)

1
/*
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

    
29
#include "port.h"
30

    
31
/* ----------------------- Modbus includes ----------------------------------*/
32
#include "mb.h"
33
#include "mbport.h"
34

    
35
#ifdef __AVR_ATtiny1634__
36
#define UART_BAUD_CALC(UART_BAUD_RATE,F_OSC) \
37
    ( ( F_OSC ) / ( ( UART_BAUD_RATE ) * 8UL ) - 1 )
38
#else
39
#define UART_BAUD_CALC(UART_BAUD_RATE,F_OSC) \
40
    ( ( F_OSC ) / ( ( UART_BAUD_RATE ) * 16UL ) - 1 )
41
#endif
42

    
43
void
44
vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable )
45
{
46
#ifdef RTS_ENABLE
47
    UCSRB |= _BV( TXEN ) | _BV(TXCIE);
48
#else
49
    UCSRB |= _BV( TXEN );
50
#endif
51

    
52
    if( xRxEnable )
53
    {
54
        UCSRB |= _BV( RXEN ) | _BV( RXCIE );
55
    }
56
    else
57
    {
58
        UCSRB &= ~( _BV( RXEN ) | _BV( RXCIE ) );
59
    }
60

    
61
    if( xTxEnable )
62
    {
63
        UCSRB |= _BV( TXEN ) | _BV( UDRE );
64
#ifdef RTS_ENABLE
65
        RTS_HIGH;
66
#endif
67
    }
68
    else
69
    {
70
        UCSRB &= ~( _BV( UDRE ) );
71
    }
72
}
73

    
74
BOOL
75
xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity )
76
{
77
    UCHAR ucUCSRC = 0;
78

    
79
    /* prevent compiler warning. */
80
    (void)ucPORT;
81
        
82
    UBRR = UART_BAUD_CALC( ulBaudRate, F_CPU );
83

    
84
    switch ( eParity )
85
    {
86
        case MB_PAR_EVEN:
87
            ucUCSRC |= _BV( UPM1 );
88
            break;
89
        case MB_PAR_ODD:
90
            ucUCSRC |= _BV( UPM1 ) | _BV( UPM0 );
91
            break;
92
        case MB_PAR_NONE:
93
            break;
94
    }
95

    
96
    switch ( ucDataBits )
97
    {
98
        case 8:
99
            ucUCSRC |= _BV( UCSZ0 ) | _BV( UCSZ1 );
100
            break;
101
        case 7:
102
            ucUCSRC |= _BV( UCSZ1 );
103
            break;
104
    }
105

    
106
#if defined (__AVR_ATmega168__)
107
    UCSRC |= ucUCSRC;
108
#elif defined (__AVR_ATmega169__)
109
    UCSRC |= ucUCSRC;
110
#elif defined (__AVR_ATmega8__)
111
    UCSRC = _BV( URSEL ) | ucUCSRC;
112
#elif defined (__AVR_ATmega16__)
113
    UCSRC = _BV( URSEL ) | ucUCSRC;
114
#elif defined (__AVR_ATmega32__)
115
    UCSRC = _BV( URSEL ) | ucUCSRC;
116
#elif defined (__AVR_ATmega128__)
117
    UCSRC |= ucUCSRC;
118
#elif defined (__AVR_ATtiny1634__)
119
    UCSRA |= _BV(U2X);
120
    UCSRC |= ucUCSRC;
121
#endif
122

    
123
    vMBPortSerialEnable( FALSE, FALSE );
124

    
125
#ifdef RTS_ENABLE
126
    RTS_INIT;
127
#endif
128
    return TRUE;
129
}
130

    
131
BOOL
132
xMBPortSerialPutByte( CHAR ucByte )
133
{
134
    UDR = ucByte;
135
    return TRUE;
136
}
137

    
138
BOOL
139
xMBPortSerialGetByte( CHAR * pucByte )
140
{
141
    *pucByte = UDR;
142
    return TRUE;
143
}
144

    
145
SIGNAL( SIG_USART_DATA )
146
{
147
    pxMBFrameCBTransmitterEmpty(  );
148
}
149

    
150
SIGNAL( SIG_USART_RECV )
151
{
152
    pxMBFrameCBByteReceived(  );
153
}
154

    
155
#ifdef RTS_ENABLE
156
SIGNAL( SIG_UART_TRANS )
157
{
158
    RTS_LOW;
159
}
160
#endif
161