Contiki 3.x
uart.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014, Eistec AB.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holder nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  *
29  * This file is part of the Mulle platform port of the Contiki operating system.
30  *
31  */
32 
33 /**
34  * \file
35  * Provide common UART routines for MK60DZ10.
36  *
37  * \author Tony Persson <tony.persson@rubico.com>
38  * \author Joakim Gebart <joakim.gebart@eistec.se>
39  */
40 
41 #ifndef CPU_ARM_K60_UART_H_
42 #define CPU_ARM_K60_UART_H_
43 
44 #include <stdint.h>
45 #include "K60.h"
46 
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50 
51 void uart_module_enable(const unsigned int uart_num);
52 void uart_init(const unsigned int uart_num, uint32_t module_clk_hz, const uint32_t baud);
53 void uart_putchar(const unsigned int uart_num, const char ch);
54 void uart_putstring(const unsigned int uart_num, const char *str);
55 void uart_enable_rx_interrupt(const unsigned int uart_num);
56 void uart_set_rx_callback(const unsigned int uart_num, int (*callback)(unsigned char));
57 
58 /*
59  * Baud rate generator is driven by the System clock (UART0, UART1) or bus
60  * clock (UART2-4) divided by SBR in UARTx_BDL, UARTx_BDH (13 bits)
61  * The receiver needs to sample the input at 16 times the line baud rate.
62  *
63  * From the reference manual:
64  * UART baud rate = UART module clock / (16 * (SBR[12:0] + BRFD))
65  *
66  * So if we want 115200 baud, we need to have a UART clock of (after dividing)
67  * 115200*16=1843200
68  * If we are running a system clock of 96 MHz we will need to divide the clock
69  * by 96000000/1843200=52.083333
70  * We set the clock divisor SBR to 52 and the BRFA fine adjust to 3 (BRFD = 0.09375)
71  * This yields a baud rate of 115176.9646.
72  * Alternatively, we can run at BRFA = 2 yielding a baud rate of 115246.0984
73  */
74 /**
75  * UART module SBR parameter based on module frequency f and desired baud rate b.
76  */
77 #define UART_SBR(f, b) ((f) / (b * 16))
78 
79 /*
80  * The constant numbers will be computed compile time by most (all?) compilers.
81  * The suffix ull on 64ull is in order to avoid overflows in the variable in
82  * the compiler when computing the number. Without ull suffix the number will
83  * be truncated to a 32 bit integer before the division yielding the wrong
84  * fine adjust value.
85  */
86 /*
87  * The below calculation will yield a fine adjust value rounded to the nearest
88  * configurable fraction.
89  */
90 /**
91  * UART module fine adjust parameter based on module frequency f and desired baud rate b.
92  */
93 /**
94  * \todo Verify proper rounding on UART1 fine adjust calculation
95  *
96  * \todo Verify the UART1 fine-adjust calculations if F_SYS*32 > 2^32 <=> F_SYS > 2^27 (== 134217728)
97  */
98 #define UART_BRFA(f, b) ((((4 * (f)) / (b) + 1) / 2) % 32)
99 
100 #ifdef __cplusplus
101 } /* extern "C" */
102 #endif
103 
104 #endif /* CPU_ARM_K60_UART_H_ */
void uart_init(const unsigned int uart_num, uint32_t module_clk_hz, const uint32_t baud)
Initialize UART.
Definition: uart.c:154
void uart_module_enable(const unsigned int uart_num)
Enable the clock gate to an UART module.
Definition: uart.c:115
K60 hardware register header wrapper.