1 /* mbed Microcontroller Library
2 * Copyright (c) 2006-2013 ARM Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include "serial_api.h"
20 // math.h required for floating point operations for baud rate calculation
22 #include "mbed_assert.h"
28 #include "fsl_uart_hal.h"
29 #include "fsl_clock_manager.h"
30 #include "fsl_uart_features.h"
31 #include "PeripheralPins.h"
34 putchar/getchar 9 and 10 bits support
42 static uint32_t serial_irq_ids
[UART_NUM
] = {0};
43 static uart_irq_handler irq_handler
;
45 int stdio_uart_inited
= 0;
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
);
54 uint32_t uartSourceClock
= CLOCK_SYS_GetUartFreq(obj
->index
);
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
);
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
]);
68 pinmap_pinout(tx
, PinMap_UART_TX
);
69 pinmap_pinout(rx
, PinMap_UART_RX
);
78 if (obj
->index
== STDIO_UART
) {
79 stdio_uart_inited
= 1;
80 memcpy(&stdio_uart
, obj
, sizeof(serial_t
));
82 while(!UART_HAL_IsTxDataRegEmpty(uart_addrs
[obj
->index
]));
85 void serial_free(serial_t
*obj
) {
86 serial_irq_ids
[obj
->index
] = 0;
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
);
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
);
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) {
109 irq_handler(serial_irq_ids
[index
], TxIrq
);
112 irq_handler(serial_irq_ids
[index
], RxIrq
);
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
);
122 uart_irq(UART_HAL_IsTxDataRegEmpty(UART1_BASE
), UART_HAL_IsRxDataRegFull(UART1_BASE
), 1);
126 uart_irq(UART_HAL_IsTxDataRegEmpty(UART2_BASE
), UART_HAL_IsRxDataRegFull(UART2_BASE
), 2);
132 uart_irq(UART_HAL_IsTxDataRegEmpty(UART3_BASE
), UART_HAL_IsRxDataRegFull(UART3_BASE
), 3);
136 uart_irq(UART_HAL_IsTxDataRegEmpty(UART4_BASE
), UART_HAL_IsRxDataRegFull(UART4_BASE
), 4);
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
;
145 void serial_irq_set(serial_t
*obj
, SerialIrq irq
, uint32_t enable
) {
146 IRQn_Type irq_n
= (IRQn_Type
)0;
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;
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;
158 uint32_t uart_addrs
[] = UART_BASE_ADDRS
;
161 case RxIrq
: UART_HAL_SetRxDataRegFullIntCmd(uart_addrs
[obj
->index
], true); break;
162 case TxIrq
: UART_HAL_SetTxDataRegEmptyIntCmd(uart_addrs
[obj
->index
], true); break;
164 NVIC_SetVector(irq_n
, vector
);
165 NVIC_EnableIRQ(irq_n
);
168 int all_disabled
= 0;
169 SerialIrq other_irq
= (irq
== RxIrq
) ? (TxIrq
) : (RxIrq
);
171 case RxIrq
: UART_HAL_SetRxDataRegFullIntCmd(uart_addrs
[obj
->index
], false); break;
172 case TxIrq
: UART_HAL_SetTxDataRegEmptyIntCmd(uart_addrs
[obj
->index
], false); break;
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;
179 NVIC_DisableIRQ(irq_n
);
183 int serial_getc(serial_t
*obj
) {
184 while (!serial_readable(obj
));
186 uint32_t uart_addrs
[] = UART_BASE_ADDRS
;
187 UART_HAL_Getchar(uart_addrs
[obj
->index
], &data
);
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
);
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
]);
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
);
210 return UART_HAL_IsTxDataRegEmpty(uart_address
[obj
->index
]);
213 void serial_clear(serial_t
*obj
) {
216 void serial_pinout_tx(PinName tx
) {
217 pinmap_pinout(tx
, PinMap_UART_TX
);
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);
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);