]> git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/serial_api.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[tmk_keyboard.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_Freescale / TARGET_KPSDK_MCUS / serial_api.c
1 /* mbed Microcontroller Library
2 * Copyright (c) 2006-2013 ARM Limited
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include "serial_api.h"
17
18 #if DEVICE_SERIAL
19
20 // math.h required for floating point operations for baud rate calculation
21 #include <math.h>
22 #include "mbed_assert.h"
23
24 #include <string.h>
25
26 #include "cmsis.h"
27 #include "pinmap.h"
28 #include "fsl_uart_hal.h"
29 #include "fsl_clock_manager.h"
30 #include "fsl_uart_features.h"
31 #include "PeripheralPins.h"
32
33 /* TODO:
34 putchar/getchar 9 and 10 bits support
35 */
36 #ifndef UART3_BASE
37 #define UART_NUM 3
38 #else
39 #define UART_NUM 5
40 #endif
41
42 static uint32_t serial_irq_ids[UART_NUM] = {0};
43 static uart_irq_handler irq_handler;
44
45 int stdio_uart_inited = 0;
46 serial_t stdio_uart;
47
48 void serial_init(serial_t *obj, PinName tx, PinName rx) {
49 uint32_t uart_tx = pinmap_peripheral(tx, PinMap_UART_TX);
50 uint32_t uart_rx = pinmap_peripheral(rx, PinMap_UART_RX);
51 obj->index = pinmap_merge(uart_tx, uart_rx);
52 MBED_ASSERT((int)obj->index != NC);
53
54 uint32_t uartSourceClock = CLOCK_SYS_GetUartFreq(obj->index);
55
56 CLOCK_SYS_EnableUartClock(obj->index);
57 uint32_t uart_addrs[] = UART_BASE_ADDRS;
58 UART_HAL_Init(uart_addrs[obj->index]);
59 UART_HAL_SetBaudRate(uart_addrs[obj->index], uartSourceClock, 9600);
60 UART_HAL_SetParityMode(uart_addrs[obj->index], kUartParityDisabled);
61 #if FSL_FEATURE_UART_HAS_STOP_BIT_CONFIG_SUPPORT
62 UART_HAL_SetStopBitCount(uart_addrs[obj->index], kUartOneStopBit);
63 #endif
64 UART_HAL_SetBitCountPerChar(uart_addrs[obj->index], kUart8BitsPerChar);
65 UART_HAL_EnableTransmitter(uart_addrs[obj->index]);
66 UART_HAL_EnableReceiver(uart_addrs[obj->index]);
67
68 pinmap_pinout(tx, PinMap_UART_TX);
69 pinmap_pinout(rx, PinMap_UART_RX);
70
71 if (tx != NC) {
72 pin_mode(tx, PullUp);
73 }
74 if (rx != NC) {
75 pin_mode(rx, PullUp);
76 }
77
78 if (obj->index == STDIO_UART) {
79 stdio_uart_inited = 1;
80 memcpy(&stdio_uart, obj, sizeof(serial_t));
81 }
82 while(!UART_HAL_IsTxDataRegEmpty(uart_addrs[obj->index]));
83 }
84
85 void serial_free(serial_t *obj) {
86 serial_irq_ids[obj->index] = 0;
87 }
88
89 void serial_baud(serial_t *obj, int baudrate) {
90 uint32_t uart_addrs[] = UART_BASE_ADDRS;
91 UART_HAL_SetBaudRate(uart_addrs[obj->index], CLOCK_SYS_GetUartFreq(obj->index), (uint32_t)baudrate);
92 }
93
94 void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) {
95 uint32_t uart_addrs[] = UART_BASE_ADDRS;
96 UART_HAL_SetBitCountPerChar(uart_addrs[obj->index], (uart_bit_count_per_char_t)data_bits);
97 UART_HAL_SetParityMode(uart_addrs[obj->index], (uart_parity_mode_t)parity);
98 #if FSL_FEATURE_UART_HAS_STOP_BIT_CONFIG_SUPPORT
99 UART_HAL_SetStopBitCount(uart_addrs[obj->index], (uart_stop_bit_count_t)--stop_bits);
100 #endif
101 }
102
103 /******************************************************************************
104 * INTERRUPTS HANDLING
105 ******************************************************************************/
106 static inline void uart_irq(uint32_t transmit_empty, uint32_t receive_full, uint32_t index) {
107 if (serial_irq_ids[index] != 0) {
108 if (transmit_empty)
109 irq_handler(serial_irq_ids[index], TxIrq);
110
111 if (receive_full)
112 irq_handler(serial_irq_ids[index], RxIrq);
113 }
114 }
115
116 void uart0_irq() {
117 uart_irq(UART_HAL_IsTxDataRegEmpty(UART0_BASE), UART_HAL_IsRxDataRegFull(UART0_BASE), 0);
118 if (UART_HAL_GetStatusFlag(UART0_BASE, kUartRxOverrun))
119 UART_HAL_ClearStatusFlag(UART0_BASE, kUartRxOverrun);
120 }
121 void uart1_irq() {
122 uart_irq(UART_HAL_IsTxDataRegEmpty(UART1_BASE), UART_HAL_IsRxDataRegFull(UART1_BASE), 1);
123 }
124
125 void uart2_irq() {
126 uart_irq(UART_HAL_IsTxDataRegEmpty(UART2_BASE), UART_HAL_IsRxDataRegFull(UART2_BASE), 2);
127 }
128
129 #if (UART_NUM > 3)
130
131 void uart3_irq() {
132 uart_irq(UART_HAL_IsTxDataRegEmpty(UART3_BASE), UART_HAL_IsRxDataRegFull(UART3_BASE), 3);
133 }
134
135 void uart4_irq() {
136 uart_irq(UART_HAL_IsTxDataRegEmpty(UART4_BASE), UART_HAL_IsRxDataRegFull(UART4_BASE), 4);
137 }
138 #endif
139
140 void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id) {
141 irq_handler = handler;
142 serial_irq_ids[obj->index] = id;
143 }
144
145 void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) {
146 IRQn_Type irq_n = (IRQn_Type)0;
147 uint32_t vector = 0;
148
149 switch (obj->index) {
150 case 0: irq_n=UART0_RX_TX_IRQn; vector = (uint32_t)&uart0_irq; break;
151 case 1: irq_n=UART1_RX_TX_IRQn; vector = (uint32_t)&uart1_irq; break;
152 case 2: irq_n=UART2_RX_TX_IRQn; vector = (uint32_t)&uart2_irq; break;
153 #if (UART_NUM > 3)
154 case 3: irq_n=UART3_RX_TX_IRQn; vector = (uint32_t)&uart3_irq; break;
155 case 4: irq_n=UART4_RX_TX_IRQn; vector = (uint32_t)&uart4_irq; break;
156 #endif
157 }
158 uint32_t uart_addrs[] = UART_BASE_ADDRS;
159 if (enable) {
160 switch (irq) {
161 case RxIrq: UART_HAL_SetRxDataRegFullIntCmd(uart_addrs[obj->index], true); break;
162 case TxIrq: UART_HAL_SetTxDataRegEmptyIntCmd(uart_addrs[obj->index], true); break;
163 }
164 NVIC_SetVector(irq_n, vector);
165 NVIC_EnableIRQ(irq_n);
166
167 } else { // disable
168 int all_disabled = 0;
169 SerialIrq other_irq = (irq == RxIrq) ? (TxIrq) : (RxIrq);
170 switch (irq) {
171 case RxIrq: UART_HAL_SetRxDataRegFullIntCmd(uart_addrs[obj->index], false); break;
172 case TxIrq: UART_HAL_SetTxDataRegEmptyIntCmd(uart_addrs[obj->index], false); break;
173 }
174 switch (other_irq) {
175 case RxIrq: all_disabled = UART_HAL_GetRxDataRegFullIntCmd(uart_addrs[obj->index]) == 0; break;
176 case TxIrq: all_disabled = UART_HAL_GetTxDataRegEmptyIntCmd(uart_addrs[obj->index]) == 0; break;
177 }
178 if (all_disabled)
179 NVIC_DisableIRQ(irq_n);
180 }
181 }
182
183 int serial_getc(serial_t *obj) {
184 while (!serial_readable(obj));
185 uint8_t data;
186 uint32_t uart_addrs[] = UART_BASE_ADDRS;
187 UART_HAL_Getchar(uart_addrs[obj->index], &data);
188
189 return data;
190 }
191
192 void serial_putc(serial_t *obj, int c) {
193 while (!serial_writable(obj));
194 uint32_t uart_addrs[] = UART_BASE_ADDRS;
195 UART_HAL_Putchar(uart_addrs[obj->index], (uint8_t)c);
196 }
197
198 int serial_readable(serial_t *obj) {
199 uint32_t uart_address[] = UART_BASE_ADDRS;
200 if (UART_HAL_GetStatusFlag(uart_address[obj->index], kUartRxOverrun))
201 UART_HAL_ClearStatusFlag(uart_address[obj->index], kUartRxOverrun);
202 return UART_HAL_IsRxDataRegFull(uart_address[obj->index]);
203 }
204
205 int serial_writable(serial_t *obj) {
206 uint32_t uart_address[] = UART_BASE_ADDRS;
207 if (UART_HAL_GetStatusFlag(uart_address[obj->index], kUartRxOverrun))
208 UART_HAL_ClearStatusFlag(uart_address[obj->index], kUartRxOverrun);
209
210 return UART_HAL_IsTxDataRegEmpty(uart_address[obj->index]);
211 }
212
213 void serial_clear(serial_t *obj) {
214 }
215
216 void serial_pinout_tx(PinName tx) {
217 pinmap_pinout(tx, PinMap_UART_TX);
218 }
219
220 void serial_break_set(serial_t *obj) {
221 uint32_t uart_address[] = UART_BASE_ADDRS;
222 UART_HAL_SetBreakCharCmd(uart_address[obj->index], true);
223 }
224
225 void serial_break_clear(serial_t *obj) {
226 uint32_t uart_address[] = UART_BASE_ADDRS;
227 UART_HAL_SetBreakCharCmd(uart_address[obj->index], false);
228 }
229
230 #endif
Imprint / Impressum