summaryrefslogtreecommitdiff
path: root/device/lib/pic16/libio/usart/uopen.c
blob: f1caabd81f3a4c68235699b44e87ba9446074df3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*-------------------------------------------------------------------------
   uopen - initialize USART module

   Copyright (C) 2004, Vangelis Rokas <vrokas AT otenet.gr>

   This library is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 2, or (at your option) any
   later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License 
   along with this library; see the file COPYING. If not, write to the
   Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
   MA 02110-1301, USA.

   As a special exception, if you link this library with other files,
   some of which are compiled with SDCC, to produce an executable,
   this library does not by itself cause the resulting executable to
   be covered by the GNU General Public License. This exception does
   not however invalidate any other reasons why the executable file
   might be covered by the GNU General Public License.
-------------------------------------------------------------------------*/

/*
 * Devices implemented:
 *	PIC18F[24][45][28]
 */


#include <pic18fregs.h>
#include <usart.h>

// USART Status Structure
extern union USART USART_Status;

void
usart_open (unsigned char config, sdcc_spbrg_t spbrg) __wparam
{
  TXSTA = 0;           // Reset USART registers to POR state
  RCSTA = 0;

  if (config & 0x01)
    TXSTAbits.SYNC = 1;

  if (config & 0x02)
    {
      TXSTAbits.TX9 = 1;
      RCSTAbits.RX9 = 1;
    }

  if (config & 0x04)
    TXSTAbits.CSRC = 1;

  if (config & 0x08)
    RCSTAbits.CREN = 1;
  else
    RCSTAbits.SREN = 1;

  if (config & 0x10)
    {
      TXSTAbits.BRGH = 1;
#if !__SDCC_NO_SPBRGH
      BAUDCONbits.BRG16 = 1;
    }
  else
    {
      BAUDCONbits.BRG16 = 0;
#endif  /* !__SDCC_NO_SPBRGH */
    }

  /* TX interrupts */

  PIR1bits.TXIF = 0;

  if (config & 0x40)
    PIE1bits.RCIE = 1;
  else
    PIE1bits.RCIE = 0;

  /* RX interrupts */
  PIR1bits.RCIF = 0;

  if (config & 0x80)
    PIE1bits.TXIE = 1;
  else
    PIE1bits.TXIE = 0;

#if !__SDCC_NO_SPBRGH
  SPBRGH = (spbrg >> 8);
#endif  /* !__SDCC_NO_SPBRGH */
  SPBRG = spbrg;

#if (__SDCC_USART_STYLE == 1812200)
  /* Configure RX/TX pins as digital pins. */
  ADCON1bits.PCFG5 = 1;
  ADCON1bits.PCFG6 = 1;

  /* Configure RX/TX pins as inputs. */
  TRISBbits.TRISB1 = 1;
  TRISBbits.TRISB4 = 1;
#elif (__SDCC_USART_STYLE == 1812300)
  /* Configure RX/TX pins as inputs. */
  TRISAbits.TRISA2 = 1;
  TRISAbits.TRISA3 = 1;
#elif (__SDCC_USART_STYLE == 1813502)
  /* Configure RX pin as digital pin. */
  ANSELHbits.ANS11 = 0;

  /* Configure RX/TX pins as inputs. */
  TRISBbits.TRISB5 = 1;
  TRISBbits.TRISB7 = 1;
#elif (__SDCC_USART_STYLE == 1822200)
  /* Configure RX/TX pins. */
  TRISCbits.TRISC6 = 0;
  TRISCbits.TRISC7 = 1;
#elif (__SDCC_USART_STYLE == 1822210) \
   || (__SDCC_USART_STYLE == 1865850)
  /* Configure RX/TX pins. */
  TRISCbits.TRISC6 = 1;
  TRISCbits.TRISC7 = 1;
#elif (__SDCC_USART_STYLE == 1824500)
  /* Configure RX/TX pins. */
  TRISCbits.TRISC6 = 0;
  TRISCbits.TRISC7 = 1;
#elif (__SDCC_USART_STYLE == 1824501) \
   || (__SDCC_USART_STYLE == 1865200)
  /* Configure RX1/TX1 pins. */
  TRISCbits.TRISC6 = 0;
  TRISCbits.TRISC7 = 1;
#else /* other devices */
#error Invalid USART style.
#endif /* other devices */

  TXSTAbits.TXEN = 1;
  RCSTAbits.SPEN = 1;
}