]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32L1/serial_api.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_STM / TARGET_STM32L1 / serial_api.c
1 /* mbed Microcontroller Library
2  *******************************************************************************
3  * Copyright (c) 2014, STMicroelectronics
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
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.
17  *
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  *******************************************************************************
29  */
30 #include "mbed_assert.h"
31 #include "serial_api.h"
32
33 #if DEVICE_SERIAL
34
35 #include "cmsis.h"
36 #include "pinmap.h"
37 #include <string.h>
38 #include "PeripheralPins.h"
39
40 #define UART_NUM (5)
41
42 static uint32_t serial_irq_ids[UART_NUM] = {0, 0, 0, 0, 0};
43
44 static uart_irq_handler irq_handler;
45
46 UART_HandleTypeDef UartHandle;
47
48 int stdio_uart_inited = 0;
49 serial_t stdio_uart;
50
51 static void init_uart(serial_t *obj)
52 {
53     UartHandle.Instance = (USART_TypeDef *)(obj->uart);
54
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;
60
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;
65     } else {
66         UartHandle.Init.Mode = UART_MODE_TX_RX;
67     }
68
69     HAL_UART_Init(&UartHandle);
70 }
71
72 void serial_init(serial_t *obj, PinName tx, PinName rx)
73 {
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);
77
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);
81
82     // Enable UART clock
83     if (obj->uart == UART_1) {
84         __USART1_CLK_ENABLE();
85         obj->index = 0;
86     }
87
88     if (obj->uart == UART_2) {
89         __USART2_CLK_ENABLE();
90         obj->index = 1;
91     }
92
93     if (obj->uart == UART_3) {
94         __USART3_CLK_ENABLE();
95         obj->index = 2;
96     }
97
98 #if defined(USART4_BASE)
99     if (obj->uart == UART_4) {
100         __UART4_CLK_ENABLE();
101         obj->index = 3;
102     }
103 #endif
104 #if defined(USART5_BASE)
105     if (obj->uart == UART_5) {
106         __UART5_CLK_ENABLE();
107         obj->index = 4;
108     }
109 #endif
110
111     // Configure the UART pins
112     pinmap_pinout(tx, PinMap_UART_TX);
113     pinmap_pinout(rx, PinMap_UART_RX);
114     if (tx != NC) {
115         pin_mode(tx, PullUp);
116     }
117     if (rx != NC) {
118         pin_mode(rx, PullUp);
119     }
120
121     // Configure UART
122     obj->baudrate = 9600;
123     obj->databits = UART_WORDLENGTH_8B;
124     obj->stopbits = UART_STOPBITS_1;
125     obj->parity   = UART_PARITY_NONE;
126     obj->pin_tx   = tx;
127     obj->pin_rx   = rx;
128
129     init_uart(obj);
130
131     // For stdio management
132     if (obj->uart == STDIO_UART) {
133         stdio_uart_inited = 1;
134         memcpy(&stdio_uart, obj, sizeof(serial_t));
135     }
136 }
137
138 void serial_free(serial_t *obj)
139 {
140     // Reset UART and disable clock
141     if (obj->uart == UART_1) {
142         __USART1_FORCE_RESET();
143         __USART1_RELEASE_RESET();
144         __USART1_CLK_DISABLE();
145     }
146
147     if (obj->uart == UART_2) {
148         __USART2_FORCE_RESET();
149         __USART2_RELEASE_RESET();
150         __USART2_CLK_DISABLE();
151     }
152
153     if (obj->uart == UART_3) {
154         __USART3_FORCE_RESET();
155         __USART3_RELEASE_RESET();
156         __USART3_CLK_DISABLE();
157     }
158
159 #if defined(USART4_BASE)
160     if (obj->uart == UART_4) {
161         __UART4_FORCE_RESET();
162         __UART4_RELEASE_RESET();
163         __UART4_CLK_DISABLE();
164     }
165 #endif
166 #if defined(USART5_BASE)
167     if (obj->uart == UART_5) {
168         __UART5_FORCE_RESET();
169         __UART5_RELEASE_RESET();
170         __UART5_CLK_DISABLE();
171     }
172 #endif
173
174     // Configure GPIOs
175     pin_function(obj->pin_tx, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
176     pin_function(obj->pin_rx, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
177
178     serial_irq_ids[obj->index] = 0;
179 }
180
181 void serial_baud(serial_t *obj, int baudrate)
182 {
183     obj->baudrate = baudrate;
184     init_uart(obj);
185 }
186
187 void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits)
188 {
189     if (data_bits == 9) {
190         obj->databits = UART_WORDLENGTH_9B;
191     } else {
192         obj->databits = UART_WORDLENGTH_8B;
193     }
194
195     switch (parity) {
196         case ParityOdd:
197         case ParityForced0:
198             obj->parity = UART_PARITY_ODD;
199             break;
200         case ParityEven:
201         case ParityForced1:
202             obj->parity = UART_PARITY_EVEN;
203             break;
204         default: // ParityNone
205             obj->parity = UART_PARITY_NONE;
206             break;
207     }
208
209     if (stop_bits == 2) {
210         obj->stopbits = UART_STOPBITS_2;
211     } else {
212         obj->stopbits = UART_STOPBITS_1;
213     }
214
215     init_uart(obj);
216 }
217
218 /******************************************************************************
219  * INTERRUPTS HANDLING
220  ******************************************************************************/
221
222 static void uart_irq(UARTName name, int id)
223 {
224     UartHandle.Instance = (USART_TypeDef *)name;
225     if (serial_irq_ids[id] != 0) {
226         if (__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_TC) != RESET) {
227             irq_handler(serial_irq_ids[id], TxIrq);
228             __HAL_UART_CLEAR_FLAG(&UartHandle, UART_FLAG_TC);
229         }
230         if (__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_RXNE) != RESET) {
231             irq_handler(serial_irq_ids[id], RxIrq);
232             __HAL_UART_CLEAR_FLAG(&UartHandle, UART_FLAG_RXNE);
233         }
234     }
235 }
236
237 static void uart1_irq(void)
238 {
239     uart_irq(UART_1, 0);
240 }
241
242 static void uart2_irq(void)
243 {
244     uart_irq(UART_2, 1);
245 }
246
247 static void uart3_irq(void)
248 {
249     uart_irq(UART_3, 2);
250 }
251
252 #if defined(USART4_BASE)
253 static void uart4_irq(void)
254 {
255     uart_irq(UART_4, 3);
256 }
257 #endif
258
259 #if defined(USART5_BASE)
260 static void uart5_irq(void)
261 {
262     uart_irq(UART_5, 4);
263 }
264 #endif
265
266 void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id)
267 {
268     irq_handler = handler;
269     serial_irq_ids[obj->index] = id;
270 }
271
272 void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
273 {
274     IRQn_Type irq_n = (IRQn_Type)0;
275     uint32_t vector = 0;
276
277     UartHandle.Instance = (USART_TypeDef *)(obj->uart);
278
279     if (obj->uart == UART_1) {
280         irq_n = USART1_IRQn;
281         vector = (uint32_t)&uart1_irq;
282     }
283
284     if (obj->uart == UART_2) {
285         irq_n = USART2_IRQn;
286         vector = (uint32_t)&uart2_irq;
287     }
288
289     if (obj->uart == UART_3) {
290         irq_n = USART3_IRQn;
291         vector = (uint32_t)&uart3_irq;
292     }
293
294 #if defined(USART4_BASE)
295     if (obj->uart == UART_4) {
296         irq_n = UART4_IRQn;
297         vector = (uint32_t)&uart4_irq;
298     }
299 #endif
300
301 #if defined(USART5_BASE)
302     if (obj->uart == UART_5) {
303         irq_n = UART5_IRQn;
304         vector = (uint32_t)&uart5_irq;
305     }
306 #endif
307
308     if (enable) {
309
310         if (irq == RxIrq) {
311             __HAL_UART_ENABLE_IT(&UartHandle, UART_IT_RXNE);
312         } else { // TxIrq
313             __HAL_UART_ENABLE_IT(&UartHandle, UART_IT_TC);
314         }
315
316         NVIC_SetVector(irq_n, vector);
317         NVIC_EnableIRQ(irq_n);
318
319     } else { // disable
320
321         int all_disabled = 0;
322
323         if (irq == RxIrq) {
324             __HAL_UART_DISABLE_IT(&UartHandle, UART_IT_RXNE);
325             // Check if TxIrq is disabled too
326             if ((UartHandle.Instance->CR1 & USART_CR1_TXEIE) == 0) all_disabled = 1;
327         } else { // TxIrq
328             __HAL_UART_DISABLE_IT(&UartHandle, UART_IT_TXE);
329             // Check if RxIrq is disabled too
330             if ((UartHandle.Instance->CR1 & USART_CR1_RXNEIE) == 0) all_disabled = 1;
331         }
332
333         if (all_disabled) NVIC_DisableIRQ(irq_n);
334
335     }
336 }
337
338 /******************************************************************************
339  * READ/WRITE
340  ******************************************************************************/
341
342 int serial_getc(serial_t *obj)
343 {
344     USART_TypeDef *uart = (USART_TypeDef *)(obj->uart);
345     while (!serial_readable(obj));
346     return (int)(uart->DR & 0xFF);
347 }
348
349 void serial_putc(serial_t *obj, int c)
350 {
351     USART_TypeDef *uart = (USART_TypeDef *)(obj->uart);
352     while (!serial_writable(obj));
353     uart->DR = (uint32_t)(c & 0xFF);
354 }
355
356 int serial_readable(serial_t *obj)
357 {
358     int status;
359     UartHandle.Instance = (USART_TypeDef *)(obj->uart);
360     // Check if data is received
361     status = ((__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_RXNE) != RESET) ? 1 : 0);
362     return status;
363 }
364
365 int serial_writable(serial_t *obj)
366 {
367     int status;
368     UartHandle.Instance = (USART_TypeDef *)(obj->uart);
369     // Check if data is transmitted
370     status = ((__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_TXE) != RESET) ? 1 : 0);
371     return status;
372 }
373
374 void serial_clear(serial_t *obj)
375 {
376     UartHandle.Instance = (USART_TypeDef *)(obj->uart);
377     __HAL_UART_CLEAR_FLAG(&UartHandle, UART_FLAG_TXE);
378     __HAL_UART_CLEAR_FLAG(&UartHandle, UART_FLAG_RXNE);
379 }
380
381 void serial_pinout_tx(PinName tx)
382 {
383     pinmap_pinout(tx, PinMap_UART_TX);
384 }
385
386 void serial_break_set(serial_t *obj)
387 {
388     UartHandle.Instance = (USART_TypeDef *)(obj->uart);
389     HAL_LIN_SendBreak(&UartHandle);
390 }
391
392 void serial_break_clear(serial_t *obj)
393 {
394 }
395
396 #endif