Contiki 2.5
uart.h
1 /*
2  * Copyright (c) 2010, Mariano Alvira <mar@devl.org> and other contributors
3  * to the MC1322x project (http://mc1322x.devl.org)
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the Institute nor the names of its contributors
15  * may be used to endorse or promote products derived from this software
16  * without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * This file is part of libmc1322x: see http://mc1322x.devl.org
31  * for details.
32  *
33  *
34  */
35 
36 #ifndef UART_H
37 #define UART_H
38 
39 #include <stdint.h>
40 
41 /* Timer registers are all 16-bit wide with 16-bit access only */
42 #define UART1_BASE (0x80005000)
43 #define UART2_BASE (0x8000B000)
44 
45 struct UART_struct {
46  union {
47  uint32_t CON;
48  struct UART_CON {
49  uint32_t :16;
50  uint32_t TST:1;
51  uint32_t MRXR:1;
52  uint32_t MTXR:1;
53  uint32_t FCE:1;
54  uint32_t FCP:1;
55  uint32_t XTIM:1;
56  uint32_t :2;
57  uint32_t TXOENB:1;
58  uint32_t CONTX:1;
59  uint32_t SB:1;
60  uint32_t ST2:1;
61  uint32_t EP:1;
62  uint32_t PEN:1;
63  uint32_t RXE:1;
64  uint32_t TXE:1;
65  } CONbits;
66  };
67  union {
68  uint32_t STAT;
69  struct UART_STAT {
70  uint32_t :24;
71  uint32_t TXRDY:1;
72  uint32_t RXRDY:1;
73  uint32_t RUE:1;
74  uint32_t ROE:1;
75  uint32_t TOE:1;
76  uint32_t FE:1;
77  uint32_t PE:1;
78  uint32_t SE:1;
79  } USTATbits;
80  };
81  union {
82  uint32_t DATA;
83  struct UART_DATA {
84  uint32_t :24;
85  uint32_t DATA:8;
86  } DATAbits;
87  };
88  union {
89  uint32_t RXCON;
90  struct UART_URXCON {
91  uint32_t :26;
92  uint32_t LVL:6;
93  } RXCONbits;
94  };
95  union {
96  uint32_t TXCON;
97  struct UART_TXCON {
98  uint32_t :26;
99  uint32_t LVL:6;
100  } TXCONbits;
101  };
102  union {
103  uint32_t CTS;
104  struct UART_CTS {
105  uint32_t :27;
106  uint32_t LVL:5;
107  } CTSbits;
108  };
109  union {
110  uint32_t BR;
111  struct UART_BR {
112  uint32_t INC:16;
113  uint32_t MOD:16;
114  } BRbits;
115  };
116 };
117 
118 static volatile struct UART_struct * const UART1 = (void *) (UART1_BASE);
119 static volatile struct UART_struct * const UART2 = (void *) (UART2_BASE);
120 
121 /* Old uart definitions, for compatibility */
122 #ifndef REG_NO_COMPAT
123 
124 #define UCON (0)
125 /* UCON bits */
126 #define UCON_SAMP 10
127 #define UCON_SAMP_8X 0
128 #define UCON_SAMP_16X 1
129 
130 #define USTAT (0x04)
131 #define UDATA (0x08)
132 #define URXCON (0x0c)
133 #define UTXCON (0x10)
134 #define UCTS (0x14)
135 #define UBRCNT (0x18)
136 
137 #define UART1_UCON ((volatile uint32_t *) ( UART1_BASE + UCON ))
138 #define UART1_USTAT ((volatile uint32_t *) ( UART1_BASE + USTAT ))
139 #define UART1_UDATA ((volatile uint32_t *) ( UART1_BASE + UDATA ))
140 #define UART1_URXCON ((volatile uint32_t *) ( UART1_BASE + URXCON ))
141 #define UART1_UTXCON ((volatile uint32_t *) ( UART1_BASE + UTXCON ))
142 #define UART1_UCTS ((volatile uint32_t *) ( UART1_BASE + UCTS ))
143 #define UART1_UBRCNT ((volatile uint32_t *) ( UART1_BASE + UBRCNT ))
144 
145 #define UART2_UCON ((volatile uint32_t *) ( UART2_BASE + UCON ))
146 #define UART2_USTAT ((volatile uint32_t *) ( UART2_BASE + USTAT ))
147 #define UART2_UDATA ((volatile uint32_t *) ( UART2_BASE + UDATA ))
148 #define UART2_URXCON ((volatile uint32_t *) ( UART2_BASE + URXCON ))
149 #define UART2_UTXCON ((volatile uint32_t *) ( UART2_BASE + UTXCON ))
150 #define UART2_UCTS ((volatile uint32_t *) ( UART2_BASE + UCTS ))
151 #define UART2_UBRCNT ((volatile uint32_t *) ( UART2_BASE + UBRCNT ))
152 
153 #endif /* REG_NO_COMPAT */
154 
155 /* The mc1322x has a 32 byte hardware FIFO for transmitted characters.
156  * Currently it is always filled from a larger RAM buffer. It would be
157  * possible to eliminate that overhead by filling directly from a chain
158  * of data buffer pointers, but printf's would be not so easy.
159  */
160 #define UART1_TX_BUFFERSIZE 1024
161 extern volatile uint32_t u1_tx_head, u1_tx_tail;
162 void uart1_putc(char c);
163 
164 /* The mc1322x has a 32 byte hardware FIFO for received characters.
165  * If a larger rx buffersize is specified the FIFO will be extended into RAM.
166  * RAM transfers will occur on interrupt when the FIFO is nearly full.
167  * If a smaller buffersize is specified hardware flow control will be
168  * initiated at that FIFO level.
169  * Set to 32 for no flow control or RAM buffer.
170  */
171 #define UART1_RX_BUFFERSIZE 128
172 #if UART1_RX_BUFFERSIZE > 32
173 extern volatile uint32_t u1_rx_head, u1_rx_tail;
174 #define uart1_can_get() ((u1_rx_head!=u1_rx_tail) || (*UART1_URXCON > 0))
175 #else
176 #define uart1_can_get() (*UART1_URXCON > 0)
177 #endif
178 uint8_t uart1_getc(void);
179 
180 
181 #define UART2_TX_BUFFERSIZE 1024
182 extern volatile uint32_t u2_tx_head, u2_tx_tail;
183 void uart2_putc(char c);
184 
185 #define UART2_RX_BUFFERSIZE 128
186 #if UART2_RX_BUFFERSIZE > 32
187 extern volatile uint32_t u2_rx_head, u2_rx_tail;
188 #define uart2_can_get() ((u2_rx_head!=u2_rx_tail) || (*UART2_URXCON > 0))
189 #else
190 #define uart2_can_get() (*UART2_URXCON > 0)
191 #endif
192 uint8_t uart2_getc(void);
193 
194 #endif