Contiki 3.x
uart.c
1 /**
2  * Copyright (c) 2014, Analog Devices, Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted (subject to the limitations in the
6  * disclaimer below) provided that the following conditions are met:
7  *
8  * - Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * - 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
14  * distribution.
15  *
16  * - Neither the name of Analog Devices, Inc. nor the names of its
17  * contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
21  * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
22  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
23  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
29  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
31  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
32  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 /**
35  * \author Jim Paris <jim.paris@rigado.com>
36  */
37 
38 #include <aducrf101-include.h>
39 
40 static int (*uart_input_handler)(unsigned char c);
41 static int stdout_enabled;
42 
43 void
44 uart_init(int baud)
45 {
46  /* P1.0 is UARTRXD, P1.1 is UARTTXD */
47  pADI_GP1->GPCON &= ~(GP1CON_CON0_MSK | GP1CON_CON1_MSK);
48  pADI_GP1->GPCON |= GP1CON_CON0_UART0RXD | GP1CON_CON1_UART0TXD;
49 
50  /* Set P1.1 as output */
51  GP1OEN_OEN1_BBA = 1;
52 
53  /* Set baudrate */
54  int div = (F_CPU / 32) / baud;
55  pADI_UART->COMDIV = div;
56  pADI_UART->COMFBR = 0x8800 | ((((64 * F_CPU) / div) / baud) - 2048);
57  pADI_UART->COMIEN = 0;
58  pADI_UART->COMLCR = 3;
59 
60  /* Set up RX IRQ */
61  pADI_UART->COMIEN = COMIEN_ERBFI_EN;
63  __enable_irq();
64 
65  uart_input_handler = NULL;
66  stdout_enabled = 1;
67 }
68 /*---------------------------------------------------------------------------*/
69 void
70 uart_put(unsigned char x)
71 {
72  while(!(pADI_UART->COMLSR & COMLSR_THRE))
73  continue;
74  pADI_UART->COMTX = x;
75 }
76 /*---------------------------------------------------------------------------*/
77 void
78 UART_Int_Handler(void)
79 {
80  if(pADI_UART->COMIIR & COMIIR_STA_RXBUFFULL) {
81  unsigned char x = pADI_UART->COMRX;
82  if(uart_input_handler) {
83  uart_input_handler(x);
84  }
85  }
86 }
87 /*---------------------------------------------------------------------------*/
88 void
89 uart_set_input(int (*input)(unsigned char c))
90 {
91  uart_input_handler = input;
92 }
93 void
94 uart_enable_stdout(int enabled)
95 {
96  stdout_enabled = enabled;
97 }
98 /*---------------------------------------------------------------------------*/
99 /* Connect newlib's _write function to the UART. */
100 int
101 _write(int fd, const void *buf, size_t len)
102 {
103  if(stdout_enabled == 0) {
104  return -1;
105  }
106 
107  if(fd == 1 || fd == 2) {
108  int n = len;
109  const unsigned char *p = buf;
110  while(n--)
111  uart_put(*p++);
112  return len;
113  }
114  return -1;
115 }
116 /*---------------------------------------------------------------------------*/
117 #ifdef __ICCARM__
118 /* Connect IAR's __write function to the UART. */
119 size_t
120 __write(int fd, const unsigned char *buf, size_t count)
121 {
122  return _write(fd, buf, count);
123 }
124 #endif
#define F_CPU
CPU core frequency resulting from the chosen divisors and multipliers.
Definition: config-clocks.h:87
__STATIC_INLINE void NVIC_EnableIRQ(IRQn_Type IRQn)
Enable External Interrupt.
Definition: core_cm0.h:535
void uart_set_input(uint8_t uart, int(*input)(unsigned char c))
Assigns a callback to be called when the UART receives a byte.
Definition: uart.c:337
void uart_init(const unsigned int uart_num, uint32_t module_clk_hz, const uint32_t baud)
Initialize UART.
Definition: uart.c:154
#define NULL
The null pointer.