Contiki 3.x
uart.c
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 #include <mc1322x.h>
37 #include <stdint.h>
38 
39 #define MOD 9999
40 #define CLK 24000000
41 #define DIV 16 /* uart->CON.XTIM = 0 is 16x oversample (datasheet is incorrect) */
42 
43 void uart_setbaud(volatile struct UART_struct * uart, uint32_t baud) {
44  uint64_t inc;
45 
46  /* baud rate eqn from reference manual */
47  /* multiply by an additional 10 to do a fixed point round later */
48  inc = ((uint64_t) baud * DIV * MOD * 10 / CLK ) - 10 ;
49  /* add 5 and divide by 10 to get a rounding */
50  inc = (inc + 5) / 10;
51 
52  /* UART must be disabled to set the baudrate */
53  uart->CONbits.TXE = 0;
54  uart->CONbits.RXE = 0;
55 
56  uart->BR = ( (uint16_t)inc << 16 ) | MOD;
57 
58  uart->CONbits.XTIM = 0;
59  uart->CONbits.TXE = 1;
60  uart->CONbits.RXE = 1;
61 }
62 
63 void uart_flowctl(volatile struct UART_struct * uart, uint8_t on) {
64  if (on) {
65  if( uart == UART1 ) {
66  /* CTS and RTS directions */
67  GPIO->PAD_DIR_SET.U1CTS = 1;
68  GPIO->PAD_DIR_RESET.U1RTS = 1;
69  /* function select to uart */
70  GPIO->FUNC_SEL.U1CTS = 1;
71  GPIO->FUNC_SEL.U1RTS = 1;
72  } else {
73  /* UART 2 */
74  /* CTS and RTS directions */
75  GPIO->PAD_DIR_SET.U2CTS = 1;
76  GPIO->PAD_DIR_RESET.U2RTS = 1;
77  /* function select to uart */
78  GPIO->FUNC_SEL.U2CTS = 1;
79  GPIO->FUNC_SEL.U2RTS = 1;
80  }
81  /* enable flow control */
82  uart->CONbits.FCE = 1;
83  } else {
84  /* off */
85  /* disable flow control */
86  uart->CONbits.FCE = 0;
87  if( uart == UART1 ) {
88  /* CTS and RTS to inputs */
89  GPIO->PAD_DIR_RESET.U1CTS = 1;
90  GPIO->PAD_DIR_RESET.U1RTS = 1;
91  /* function select to gpio */
92  GPIO->FUNC_SEL.U1CTS = 3;
93  GPIO->FUNC_SEL.U1RTS = 3;
94  } else {
95  /* UART 2 */
96  /* CTS and RTS to inputs */
97  GPIO->PAD_DIR_RESET.U2CTS = 1;
98  GPIO->PAD_DIR_RESET.U2RTS = 1;
99  /* function select to gpio */
100  GPIO->FUNC_SEL.U2CTS = 3;
101  GPIO->FUNC_SEL.U2RTS = 3;
102  }
103  }
104 }
105 
106 void uart_init(volatile struct UART_struct * uart, uint32_t baud) {
107  /* enable the uart so we can set the gpio mode */
108  /* see Section 11.5.1.2 Alternate Modes */
109  /* you must enable the peripheral first BEFORE setting the function in GPIO_FUNC_SEL */
110  /* From the datasheet: "The peripheral function will control operation of the pad IF */
111  /* THE PERIPHERAL IS ENABLED. */
112  uart->CONbits = (struct UART_CON) {
113  .TXE = 1,
114  .RXE = 1,
115  };
116 
117  /* interrupt when there are this number or more bytes free in the TX buffer*/
118  uart->TXCON = 16;
119 
120  if( uart == UART1 ) {
121  /* TX and RX directions */
122  GPIO->PAD_DIR_SET.U1TX = 1;
123  GPIO->PAD_DIR_RESET.U1RX = 1;
124 
125  /* set func sel to UART */
126  GPIO->FUNC_SEL.U1TX = 1;
127  GPIO->FUNC_SEL.U1RX = 1;
128 
129 #if UART1_RX_BUFFERSIZE > 32
130  *UART1_UCON = (1 << 0) | (1 << 1) ; /* enable receive, transmit, and both interrupts */
131  *UART1_URXCON = 30; /* interrupt when fifo is nearly full */
132  u1_rx_head = 0; u1_rx_tail = 0;
133 #elif UART1_RX_BUFFERSIZE < 32 /* enable receive, transmit, flow control, disable rx interrupt */
134  *UART1_UCON = (1 << 0) | (1 << 1) | (1 << 12) | (1 << 14);
135  *UART1_UCTS = UART1_RX_BUFFERSIZE; /* drop cts when tx buffer at trigger level */
136  *GPIO_FUNC_SEL1 = ( (0x01 << (0*2)) | (0x01 << (1*2)) ); /* set GPIO17-16 to UART1 CTS and RTS */
137 #else
138  *UART1_UCON = (1 << 0) | (1 << 1) | (1 << 14); /* enable receive, transmit, disable rx interrupt */
139 #endif
140 
141  u1_tx_head = 0; u1_tx_tail = 0;
142 
143  /* tx and rx interrupts are enabled in the UART by default */
144  /* see status register bits 13 and 14 */
145  /* enable UART1 interrupts in the interrupt controller */
146  enable_irq(UART1);
147 
148  } else {
149  /* UART2 */
150  /* TX and RX directions */
151  GPIO->PAD_DIR_SET.U2TX = 1;
152  GPIO->PAD_DIR_RESET.U1RX = 1;
153 
154  /* set func sel to UART */
155  GPIO->FUNC_SEL.U2TX = 1;
156  GPIO->FUNC_SEL.U2RX = 1;
157 
158 #if UART2_RX_BUFFERSIZE > 32
159  *UART2_UCON = (1 << 0) | (1 << 1) ; /* enable receive, transmit, and both interrupts */
160  *UART2_URXCON = 30; /* interrupt when fifo is nearly full */
161  u2_rx_head = 0; u2_rx_tail = 0;
162 #elif UART2_RX_BUFFERSIZE < 32 /* enable receive, transmit, disable flow control, disable rx interrupt */
163  *UART2_UCON = (1 << 0) | (1 << 1) | (0 << 12) | (1 << 14);
164  *UART2_UCTS = UART2_RX_BUFFERSIZE; /* drop cts when tx buffer at trigger level */
165  *GPIO_FUNC_SEL1 = ( (0x01 << (0*2)) | (0x01 << (1*2)) ); /* set GPIO17-16 to UART2 CTS and RTS */
166 #else
167  *UART2_UCON = (1 << 0) | (1 << 1) | (1 << 14); /* enable receive, transmit, disable rx interrupt */
168 #endif
169 
170  u2_tx_head = 0; u2_tx_tail = 0;
171 
172  enable_irq(UART2);
173  }
174 
175  uart_setbaud(uart, baud);
176 
177 }
178 
void uart_init(const unsigned int uart_num, uint32_t module_clk_hz, const uint32_t baud)
Initialize UART.
Definition: uart.c:154
#define UART1
Peripheral UART1 base pointer.
Definition: MK60D10.h:8667
#define UART2
Peripheral UART2 base pointer.
Definition: MK60D10.h:8671