Statistics
| Branch: | Revision:

root / toolbox / freemodbus / port / portserial.c @ 07718da3

History | View | Annotate | Download (3.69 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
#include <avr/signal.h>
29

    
30
#include "port.h"
31

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

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

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

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

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

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

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

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

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

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

    
124
    vMBPortSerialEnable( FALSE, FALSE );
125

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

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

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

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

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

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