Statistics
| Branch: | Revision:

root / freemodbus / port / portserial.c @ 20e5429c

History | View | Annotate | Download (3.54 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
#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
#endif
118

    
119
    vMBPortSerialEnable( FALSE, FALSE );
120

    
121
#ifdef RTS_ENABLE
122
    RTS_INIT;
123
#endif
124
    return TRUE;
125
}
126

    
127
BOOL
128
xMBPortSerialPutByte( CHAR ucByte )
129
{
130
    UDR = ucByte;
131
    return TRUE;
132
}
133

    
134
BOOL
135
xMBPortSerialGetByte( CHAR * pucByte )
136
{
137
    *pucByte = UDR;
138
    return TRUE;
139
}
140

    
141
SIGNAL( SIG_USART_DATA )
142
{
143
    pxMBFrameCBTransmitterEmpty(  );
144
}
145

    
146
SIGNAL( SIG_USART_RECV )
147
{
148
    pxMBFrameCBByteReceived(  );
149
}
150

    
151
#ifdef RTS_ENABLE
152
SIGNAL( SIG_UART_TRANS )
153
{
154
    RTS_LOW;
155
}
156
#endif
157