Imported Upstream version 2.9.0
[debian/cc1111] / device / lib / pic16 / libio / usart / uopen.c
1
2 /*
3  * uopen - initialize USART module
4  *
5  * written by Vangelis Rokas, 2004 <vrokas AT otenet.gr>
6  *
7  * Devices implemented:
8  *      PIC18F[24][45][28]
9  *
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Library General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  *
25  *
26  * $Id: uopen.c 5195 2008-06-21 21:36:29Z tecodev $
27  */
28
29
30 #include <pic18fregs.h>
31
32 #include <usart.h>
33
34 // USART Status Structure
35 extern union USART USART_Status;
36
37 void usart_open(unsigned char config, unsigned int spbrg) __wparam
38 {
39   TXSTA = 0;           // Reset USART registers to POR state
40   RCSTA = 0;
41
42   if(config&0x01)TXSTAbits.SYNC = 1;
43
44   if(config&0x02) {
45     TXSTAbits.TX9 = 1;
46     RCSTAbits.RX9 = 1;
47   }
48
49   if(config&0x04)TXSTAbits.CSRC = 1;
50
51   if(config&0x08)RCSTAbits.CREN = 1;
52   else RCSTAbits.SREN = 1;
53
54   if(config&0x10)TXSTAbits.BRGH = 1;
55   else TXSTAbits.BRGH = 0;
56
57   /* TX interrupts */
58 #if defined(pic18f66j60) || defined(pic18f66j65) || \
59         defined(pic18f67j60) || defined(pic18f86j60) || \
60         defined(pic18f86j65) || defined(pic18f87j60) || \
61         defined(pic18f96j60) || defined(pic18f96j65) || \
62         defined(pic18f97j60)
63
64   PIR1bits.TXIF_PIR1 = 0;
65
66 #else   /* all other devices */
67
68   PIR1bits.TXIF = 0;
69
70 #endif
71
72   if(config&0x40)PIE1bits.RCIE = 1;
73   else PIE1bits.RCIE = 0;
74
75   /* RX interrupts */
76   PIR1bits.RCIF = 0;
77
78 #if defined(pic18f66j60) || defined(pic18f66j65) || \
79         defined(pic18f67j60) || defined(pic18f86j60) || \
80         defined(pic18f86j65) || defined(pic18f87j60) || \
81         defined(pic18f96j60) || defined(pic18f96j65) || \
82         defined(pic18f97j60)
83
84   if(config&0x80)PIE1bits.TXIE_PIE1 = 1;
85   else PIE1bits.TXIE_PIE1 = 0;
86
87 #else   /* all other devices */
88
89   if(config&0x80)PIE1bits.TXIE = 1;
90   else PIE1bits.TXIE = 0;
91
92 #endif
93
94   SPBRG = (char)spbrg;
95
96   TXSTAbits.TXEN = 1;
97   RCSTAbits.SPEN = 1;
98 }