1 /* mbed Microcontroller Library
2 *******************************************************************************
3 * Copyright (c) 2014, STMicroelectronics
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * 3. Neither the name of STMicroelectronics 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.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER 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 OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *******************************************************************************
30 #include "mbed_assert.h"
31 #include "serial_api.h"
38 #include "PeripheralPins.h"
42 static uint32_t serial_irq_ids
[UART_NUM
] = {0, 0, 0, 0, 0, 0, 0, 0};
44 static uart_irq_handler irq_handler
;
46 UART_HandleTypeDef UartHandle
;
48 int stdio_uart_inited
= 0;
51 static void init_uart(serial_t
*obj
)
53 UartHandle
.Instance
= (USART_TypeDef
*)(obj
->uart
);
55 UartHandle
.Init
.BaudRate
= obj
->baudrate
;
56 UartHandle
.Init
.WordLength
= obj
->databits
;
57 UartHandle
.Init
.StopBits
= obj
->stopbits
;
58 UartHandle
.Init
.Parity
= obj
->parity
;
59 UartHandle
.Init
.HwFlowCtl
= UART_HWCONTROL_NONE
;
61 if (obj
->pin_rx
== NC
) {
62 UartHandle
.Init
.Mode
= UART_MODE_TX
;
63 } else if (obj
->pin_tx
== NC
) {
64 UartHandle
.Init
.Mode
= UART_MODE_RX
;
66 UartHandle
.Init
.Mode
= UART_MODE_TX_RX
;
69 HAL_UART_Init(&UartHandle
);
72 void serial_init(serial_t
*obj
, PinName tx
, PinName rx
)
74 // Determine the UART to use (UART_1, UART_2, ...)
75 UARTName uart_tx
= (UARTName
)pinmap_peripheral(tx
, PinMap_UART_TX
);
76 UARTName uart_rx
= (UARTName
)pinmap_peripheral(rx
, PinMap_UART_RX
);
78 // Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object
79 obj
->uart
= (UARTName
)pinmap_merge(uart_tx
, uart_rx
);
80 MBED_ASSERT(obj
->uart
!= (UARTName
)NC
);
85 __USART1_CLK_ENABLE();
89 __USART2_CLK_ENABLE();
92 #if defined(USART3_BASE)
94 __USART3_CLK_ENABLE();
98 #if defined(UART4_BASE)
100 __UART4_CLK_ENABLE();
104 #if defined(UART5_BASE)
106 __UART5_CLK_ENABLE();
111 __USART6_CLK_ENABLE();
114 #if defined(UART7_BASE)
116 __UART7_CLK_ENABLE();
120 #if defined(UART8_BASE)
122 __UART8_CLK_ENABLE();
128 // Configure the UART pins
129 pinmap_pinout(tx
, PinMap_UART_TX
);
130 pinmap_pinout(rx
, PinMap_UART_RX
);
132 pin_mode(tx
, PullUp
);
135 pin_mode(rx
, PullUp
);
139 obj
->baudrate
= 9600;
140 obj
->databits
= UART_WORDLENGTH_8B
;
141 obj
->stopbits
= UART_STOPBITS_1
;
142 obj
->parity
= UART_PARITY_NONE
;
149 // For stdio management
150 if (obj
->uart
== STDIO_UART
) {
151 stdio_uart_inited
= 1;
152 memcpy(&stdio_uart
, obj
, sizeof(serial_t
));
156 void serial_free(serial_t
*obj
)
158 // Reset UART and disable clock
161 __USART1_FORCE_RESET();
162 __USART1_RELEASE_RESET();
163 __USART1_CLK_DISABLE();
166 __USART2_FORCE_RESET();
167 __USART2_RELEASE_RESET();
168 __USART2_CLK_DISABLE();
170 #if defined(USART3_BASE)
172 __USART3_FORCE_RESET();
173 __USART3_RELEASE_RESET();
174 __USART3_CLK_DISABLE();
177 #if defined(UART4_BASE)
179 __UART4_FORCE_RESET();
180 __UART4_RELEASE_RESET();
181 __UART4_CLK_DISABLE();
184 #if defined(UART5_BASE)
186 __UART5_FORCE_RESET();
187 __UART5_RELEASE_RESET();
188 __UART5_CLK_DISABLE();
192 __USART6_FORCE_RESET();
193 __USART6_RELEASE_RESET();
194 __USART6_CLK_DISABLE();
196 #if defined(UART7_BASE)
198 __UART7_FORCE_RESET();
199 __UART7_RELEASE_RESET();
200 __UART7_CLK_DISABLE();
203 #if defined(UART8_BASE)
205 __UART8_FORCE_RESET();
206 __UART8_RELEASE_RESET();
207 __UART8_CLK_DISABLE();
212 pin_function(obj
->pin_tx
, STM_PIN_DATA(STM_MODE_INPUT
, GPIO_NOPULL
, 0));
213 pin_function(obj
->pin_rx
, STM_PIN_DATA(STM_MODE_INPUT
, GPIO_NOPULL
, 0));
215 serial_irq_ids
[obj
->index
] = 0;
218 void serial_baud(serial_t
*obj
, int baudrate
)
220 obj
->baudrate
= baudrate
;
224 void serial_format(serial_t
*obj
, int data_bits
, SerialParity parity
, int stop_bits
)
226 if (data_bits
== 9) {
227 obj
->databits
= UART_WORDLENGTH_9B
;
229 obj
->databits
= UART_WORDLENGTH_8B
;
235 obj
->parity
= UART_PARITY_ODD
;
239 obj
->parity
= UART_PARITY_EVEN
;
241 default: // ParityNone
242 obj
->parity
= UART_PARITY_NONE
;
246 if (stop_bits
== 2) {
247 obj
->stopbits
= UART_STOPBITS_2
;
249 obj
->stopbits
= UART_STOPBITS_1
;
255 /******************************************************************************
256 * INTERRUPTS HANDLING
257 ******************************************************************************/
259 static void uart_irq(UARTName name
, int id
)
261 UartHandle
.Instance
= (USART_TypeDef
*)name
;
262 if (serial_irq_ids
[id
] != 0) {
263 if (__HAL_UART_GET_FLAG(&UartHandle
, UART_FLAG_TC
) != RESET
) {
264 irq_handler(serial_irq_ids
[id
], TxIrq
);
265 __HAL_UART_CLEAR_FLAG(&UartHandle
, UART_FLAG_TC
);
267 if (__HAL_UART_GET_FLAG(&UartHandle
, UART_FLAG_RXNE
) != RESET
) {
268 irq_handler(serial_irq_ids
[id
], RxIrq
);
269 __HAL_UART_CLEAR_FLAG(&UartHandle
, UART_FLAG_RXNE
);
274 static void uart1_irq(void)
279 static void uart2_irq(void)
284 #if defined(USART3_BASE)
285 static void uart3_irq(void)
291 #if defined(UART4_BASE)
292 static void uart4_irq(void)
298 #if defined(UART5_BASE)
299 static void uart5_irq(void)
305 static void uart6_irq(void)
310 #if defined(UART7_BASE)
311 static void uart7_irq(void)
317 #if defined(UART8_BASE)
318 static void uart8_irq(void)
324 void serial_irq_handler(serial_t
*obj
, uart_irq_handler handler
, uint32_t id
)
326 irq_handler
= handler
;
327 serial_irq_ids
[obj
->index
] = id
;
330 void serial_irq_set(serial_t
*obj
, SerialIrq irq
, uint32_t enable
)
332 IRQn_Type irq_n
= (IRQn_Type
)0;
335 UartHandle
.Instance
= (USART_TypeDef
*)(obj
->uart
);
340 vector
= (uint32_t)&uart1_irq
;
345 vector
= (uint32_t)&uart2_irq
;
347 #if defined(USART3_BASE)
350 vector
= (uint32_t)&uart3_irq
;
353 #if defined(UART4_BASE)
356 vector
= (uint32_t)&uart4_irq
;
359 #if defined(UART5_BASE)
362 vector
= (uint32_t)&uart5_irq
;
367 vector
= (uint32_t)&uart6_irq
;
369 #if defined(UART7_BASE)
372 vector
= (uint32_t)&uart7_irq
;
375 #if defined(UART8_BASE)
378 vector
= (uint32_t)&uart8_irq
;
386 __HAL_UART_ENABLE_IT(&UartHandle
, UART_IT_RXNE
);
388 __HAL_UART_ENABLE_IT(&UartHandle
, UART_IT_TC
);
391 NVIC_SetVector(irq_n
, vector
);
392 NVIC_EnableIRQ(irq_n
);
396 int all_disabled
= 0;
399 __HAL_UART_DISABLE_IT(&UartHandle
, UART_IT_RXNE
);
400 // Check if TxIrq is disabled too
401 if ((UartHandle
.Instance
->CR1
& USART_CR1_TXEIE
) == 0) all_disabled
= 1;
403 __HAL_UART_DISABLE_IT(&UartHandle
, UART_IT_TXE
);
404 // Check if RxIrq is disabled too
405 if ((UartHandle
.Instance
->CR1
& USART_CR1_RXNEIE
) == 0) all_disabled
= 1;
408 if (all_disabled
) NVIC_DisableIRQ(irq_n
);
413 /******************************************************************************
415 ******************************************************************************/
417 int serial_getc(serial_t
*obj
)
419 USART_TypeDef
*uart
= (USART_TypeDef
*)(obj
->uart
);
420 while (!serial_readable(obj
));
421 return (int)(uart
->DR
& 0x1FF);
424 void serial_putc(serial_t
*obj
, int c
)
426 USART_TypeDef
*uart
= (USART_TypeDef
*)(obj
->uart
);
427 while (!serial_writable(obj
));
428 uart
->DR
= (uint32_t)(c
& 0x1FF);
431 int serial_readable(serial_t
*obj
)
434 UartHandle
.Instance
= (USART_TypeDef
*)(obj
->uart
);
435 // Check if data is received
436 status
= ((__HAL_UART_GET_FLAG(&UartHandle
, UART_FLAG_RXNE
) != RESET
) ? 1 : 0);
440 int serial_writable(serial_t
*obj
)
443 UartHandle
.Instance
= (USART_TypeDef
*)(obj
->uart
);
444 // Check if data is transmitted
445 status
= ((__HAL_UART_GET_FLAG(&UartHandle
, UART_FLAG_TXE
) != RESET
) ? 1 : 0);
449 void serial_clear(serial_t
*obj
)
451 UartHandle
.Instance
= (USART_TypeDef
*)(obj
->uart
);
452 __HAL_UART_CLEAR_FLAG(&UartHandle
, UART_FLAG_TXE
);
453 __HAL_UART_CLEAR_FLAG(&UartHandle
, UART_FLAG_RXNE
);
456 void serial_pinout_tx(PinName tx
)
458 pinmap_pinout(tx
, PinMap_UART_TX
);
461 void serial_break_set(serial_t
*obj
)
463 UartHandle
.Instance
= (USART_TypeDef
*)(obj
->uart
);
464 HAL_LIN_SendBreak(&UartHandle
);
467 void serial_break_clear(serial_t
*obj
)