Contiki 3.x
pic32_uart.c
Go to the documentation of this file.
1 /*
2  * Contiki PIC32 Port project
3  *
4  * Copyright (c) 2012,
5  * Scuola Superiore Sant'Anna (http://www.sssup.it) and
6  * Consorzio Nazionale Interuniversitario per le Telecomunicazioni
7  * (http://www.cnit.it).
8  *
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in the
18  * documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the Institute nor the names of its contributors
20  * may be used to endorse or promote products derived from this software
21  * without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  */
36 
37 /**
38  * \addtogroup pic32 PIC32 Contiki Port
39  *
40  * @{
41  */
42 
43 /**
44  * \file pic32_uart.c
45  * \brief UART Interface for PIC32MX (pic32mx795f512l)
46  * \author Giovanni Pellerano <giovanni.pellerano@evilaliv3.org>
47  * \date 2012-03-21
48  */
49 
50 /*
51  * PIC32MX795F512L - Specific Functions
52  *
53  * All the functions in this part of the file are specific for the
54  * pic32mx795f512l that is characterized by registers' name that differ from
55  * the 3xx and 4xx families of the pic32mx.
56  */
57 
58 #define __UART_CODE_TEST__ 0
59 
60 #if __UART_CODE_TEST__
61 #define __USE_UART__ 1
62 #define __USE_UART_PORT1A__ 1
63 #define __USE_UART_PORT1B__ 1
64 #define __USE_UART_PORT2A__ 1
65 #define __USE_UART_PORT2B__ 1
66 #define __USE_UART_PORT3A__ 1
67 #define __USE_UART_PORT3B__ 1
68 #endif /* __UART_CODE_TEST__ */
69 
70 #ifdef __USE_UART__
71 
72 #include <pic32_uart.h>
73 #include <pic32_clock.h>
74 #include <pic32_irq.h>
75 
76 #include <p32xxxx.h>
77 
78 #include "contiki.h"
79 
80 #include "dev/leds.h"
81 
82 /*---------------------------------------------------------------------------*/
83 #define UART_PORT_INIT_XA(XX, YY, ZZ) \
84  int8_t \
85  pic32_uart##XX##A_init(uint32_t baudrate, uint16_t byte_format) \
86  { \
87  /* Disable Interrupts: RX, TX, ERR */ \
88  IEC##ZZ##CLR = _IEC##ZZ##_U##XX##AEIE_MASK | _IEC##ZZ##_U##XX##ATXIE_MASK | _IEC##ZZ##_U##XX##ARXIE_MASK; \
89  IFS##ZZ##CLR = _IFS##ZZ##_U##XX##AEIF_MASK | _IFS##ZZ##_U##XX##ATXIF_MASK | _IFS##ZZ##_U##XX##ARXIF_MASK; \
90  \
91  /* Clear thant Set Pri and Sub priority */ \
92  IPC##YY##CLR = _IPC##YY##_U##XX##AIP_MASK | _IPC##YY##_U##XX##AIS_MASK; \
93  IPC##YY##SET = (6 << _IPC##YY##_U##XX##AIP_POSITION) | (0 << _IPC##YY##_U##XX##AIS_POSITION); \
94  \
95  /* Mode Register Reset (this also stops UART) */ \
96  U##XX##AMODE = 0; \
97  \
98  /* Use BRGH = 1: 4 divisor */ \
99  U##XX##AMODESET = _U##XX##AMODE_BRGH_MASK; \
100  U##XX##ABRG = pic32_clock_calculate_brg(4, baudrate); \
101  \
102  U##XX##AMODESET = byte_format & 0x07; /* Number of bit, Parity and Stop bits */ \
103  \
104  /* Status bits */ \
105  U##XX##ASTA = 0; \
106  U##XX##ASTASET = _U##XX##ASTA_URXEN_MASK | _U##XX##ASTA_UTXEN_MASK; /* Enable RX and TX */ \
107  \
108  IEC##ZZ##SET = _IEC##ZZ##_U##XX##ARXIE_MASK; \
109  \
110  /* Enable UART port */ \
111  U##XX##AMODESET = _U##XX##AMODE_UARTEN_MASK; \
112  \
113  return UART_NO_ERROR; \
114  }
115 /*---------------------------------------------------------------------------*/
116 #define UART_PORT_INIT_XB(XX, YY, ZZ) \
117  int8_t \
118  pic32_uart##XX##B_init(uint32_t baudrate, uint16_t byte_format) \
119  { \
120  /* Disable Interrupts: RX, TX, ERR */ \
121  IEC##ZZ##CLR = _IEC##ZZ##_U##XX##BEIE_MASK | _IEC##ZZ##_U##XX##BTXIE_MASK | _IEC##ZZ##_U##XX##BRXIE_MASK; \
122  IFS##ZZ##CLR = _IFS##ZZ##_U##XX##BEIF_MASK | _IFS##ZZ##_U##XX##BTXIF_MASK | _IFS##ZZ##_U##XX##BRXIF_MASK; \
123  \
124  /* Clear thant Set Pri and Sub priority */ \
125  IPC##YY##CLR = _IPC##YY##_U##XX##BIP_MASK | _IPC##YY##_U##XX##BIS_MASK; \
126  IPC##YY##SET = (6 << _IPC##YY##_U##XX##BIP_POSITION) | (0 << _IPC##YY##_U##XX##BIS_POSITION); \
127  \
128  /* Mode Register Reset (this also stops UART) */ \
129  U##XX##BMODE = 0; \
130  \
131  /* Use BRGH = 1: 4 divisor */ \
132  U##XX##BMODESET = _U##XX##BMODE_BRGH_MASK; \
133  U##XX##BBRG = pic32_clock_calculate_brg(4, baudrate); \
134  \
135  U##XX##BMODESET = byte_format & 0x07; /* Number of bit, Parity and Stop bits */ \
136  \
137  /* Status bits */ \
138  U##XX##BSTA = 0; \
139  U##XX##BSTASET = _U##XX##BSTA_URXEN_MASK | _U##XX##BSTA_UTXEN_MASK; /* Enable RX and TX */ \
140  \
141  IEC##ZZ##SET = _IEC##ZZ##_U##XX##BRXIE_MASK; \
142  \
143  /* Enable UART port */ \
144  U##XX##BMODESET = _U##XX##BMODE_UARTEN_MASK; \
145  \
146  return UART_NO_ERROR; \
147  }
148 /*---------------------------------------------------------------------------*/
149 #define UART_PORT(XX, YY) \
150  \
151  int8_t \
152  pic32_uart##XX##_write(uint8_t data) \
153  { \
154  volatile uint8_t wait; \
155  \
156  do { \
157  wait = U##XX##STAbits.UTXBF; \
158  } while(wait); \
159  \
160  U##XX##TXREG = data; \
161  \
162  return UART_NO_ERROR; \
163  }
164 /*---------------------------------------------------------------------------*/
165 
166 #ifdef __USE_UART_PORT1A__
167 UART_PORT(1A, 0)
168 UART_PORT_INIT_XA(1, 6, 0)
169 #endif /* __USE_UART_PORT1A__ */
170 
171 #ifdef __USE_UART_PORT1B__
172 UART_PORT(1B, 2)
173 UART_PORT_INIT_XB(1, 12, 2)
174 #endif /* __USE_UART_PORT1B__ */
175 
176 #ifdef __USE_UART_PORT2A__
177 UART_PORT(2A, 1)
178 UART_PORT_INIT_XA(2, 7, 1)
179 #endif /* __USE_UART_PORT2A__ */
180 
181 #ifdef __USE_UART_PORT2B__
182 UART_PORT(2B, 2)
183 UART_PORT_INIT_XB(2, 12, 2)
184 #endif /* __USE_UART_PORT2B__ */
185 
186 #ifdef __USE_UART_PORT3A__
187 UART_PORT(3A, 1)
188 UART_PORT_INIT_XA(3, 8, 1)
189 #endif /* __USE_UART_PORT3A__ */
190 
191 #ifdef __USE_UART_PORT3B__
192 UART_PORT(3B, 2)
193 UART_PORT_INIT_XB(3, 12, 2)
194 #endif /* __USE_UART_PORT3B__ */
195 
196 #endif /* __USE_UART__ */
197 
198 /** @} */
UART Interface for PIC32MX (pic32mx795f512l)
INTERRUPT interface for PIC32MX (pic32mx795f512l)
CLOCK interface for PIC32MX (pic32mx795f512l)