Prepare for SDCC 4.5.0 release.
[sdcc.git] / sdcc / device / lib / pic16 / libio / usart / uopen.c
blobf1caabd81f3a4c68235699b44e87ba9446074df3
1 /*-------------------------------------------------------------------------
2 uopen - initialize USART module
4 Copyright (C) 2004, Vangelis Rokas <vrokas AT otenet.gr>
6 This library is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
9 later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this library; see the file COPYING. If not, write to the
18 Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
19 MA 02110-1301, USA.
21 As a special exception, if you link this library with other files,
22 some of which are compiled with SDCC, to produce an executable,
23 this library does not by itself cause the resulting executable to
24 be covered by the GNU General Public License. This exception does
25 not however invalidate any other reasons why the executable file
26 might be covered by the GNU General Public License.
27 -------------------------------------------------------------------------*/
30 * Devices implemented:
31 * PIC18F[24][45][28]
35 #include <pic18fregs.h>
36 #include <usart.h>
38 // USART Status Structure
39 extern union USART USART_Status;
41 void
42 usart_open (unsigned char config, sdcc_spbrg_t spbrg) __wparam
44 TXSTA = 0; // Reset USART registers to POR state
45 RCSTA = 0;
47 if (config & 0x01)
48 TXSTAbits.SYNC = 1;
50 if (config & 0x02)
52 TXSTAbits.TX9 = 1;
53 RCSTAbits.RX9 = 1;
56 if (config & 0x04)
57 TXSTAbits.CSRC = 1;
59 if (config & 0x08)
60 RCSTAbits.CREN = 1;
61 else
62 RCSTAbits.SREN = 1;
64 if (config & 0x10)
66 TXSTAbits.BRGH = 1;
67 #if !__SDCC_NO_SPBRGH
68 BAUDCONbits.BRG16 = 1;
70 else
72 BAUDCONbits.BRG16 = 0;
73 #endif /* !__SDCC_NO_SPBRGH */
76 /* TX interrupts */
78 PIR1bits.TXIF = 0;
80 if (config & 0x40)
81 PIE1bits.RCIE = 1;
82 else
83 PIE1bits.RCIE = 0;
85 /* RX interrupts */
86 PIR1bits.RCIF = 0;
88 if (config & 0x80)
89 PIE1bits.TXIE = 1;
90 else
91 PIE1bits.TXIE = 0;
93 #if !__SDCC_NO_SPBRGH
94 SPBRGH = (spbrg >> 8);
95 #endif /* !__SDCC_NO_SPBRGH */
96 SPBRG = spbrg;
98 #if (__SDCC_USART_STYLE == 1812200)
99 /* Configure RX/TX pins as digital pins. */
100 ADCON1bits.PCFG5 = 1;
101 ADCON1bits.PCFG6 = 1;
103 /* Configure RX/TX pins as inputs. */
104 TRISBbits.TRISB1 = 1;
105 TRISBbits.TRISB4 = 1;
106 #elif (__SDCC_USART_STYLE == 1812300)
107 /* Configure RX/TX pins as inputs. */
108 TRISAbits.TRISA2 = 1;
109 TRISAbits.TRISA3 = 1;
110 #elif (__SDCC_USART_STYLE == 1813502)
111 /* Configure RX pin as digital pin. */
112 ANSELHbits.ANS11 = 0;
114 /* Configure RX/TX pins as inputs. */
115 TRISBbits.TRISB5 = 1;
116 TRISBbits.TRISB7 = 1;
117 #elif (__SDCC_USART_STYLE == 1822200)
118 /* Configure RX/TX pins. */
119 TRISCbits.TRISC6 = 0;
120 TRISCbits.TRISC7 = 1;
121 #elif (__SDCC_USART_STYLE == 1822210) \
122 || (__SDCC_USART_STYLE == 1865850)
123 /* Configure RX/TX pins. */
124 TRISCbits.TRISC6 = 1;
125 TRISCbits.TRISC7 = 1;
126 #elif (__SDCC_USART_STYLE == 1824500)
127 /* Configure RX/TX pins. */
128 TRISCbits.TRISC6 = 0;
129 TRISCbits.TRISC7 = 1;
130 #elif (__SDCC_USART_STYLE == 1824501) \
131 || (__SDCC_USART_STYLE == 1865200)
132 /* Configure RX1/TX1 pins. */
133 TRISCbits.TRISC6 = 0;
134 TRISCbits.TRISC7 = 1;
135 #else /* other devices */
136 #error Invalid USART style.
137 #endif /* other devices */
139 TXSTAbits.TXEN = 1;
140 RCSTAbits.SPEN = 1;