]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32L0/serial_api.c
Merge commit '4d116a04e94cf0d19317d5b44e4fa9f34a3e5594'
[qmk_firmware.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_STM / TARGET_STM32L0 / 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     if (obj->uart == LPUART_1) {
56         UartHandle.Init.BaudRate = obj->baudrate >> 1;
57     } else {
58         UartHandle.Init.BaudRate = obj->baudrate;
59     }
60     UartHandle.Init.WordLength = obj->databits;
61     UartHandle.Init.StopBits   = obj->stopbits;
62     UartHandle.Init.Parity     = obj->parity;
63     UartHandle.Init.HwFlowCtl  = UART_HWCONTROL_NONE;
64
65     if (obj->pin_rx == NC) {
66         UartHandle.Init.Mode = UART_MODE_TX;
67     } else if (obj->pin_tx == NC) {
68         UartHandle.Init.Mode = UART_MODE_RX;
69     } else {
70         UartHandle.Init.Mode = UART_MODE_TX_RX;
71     }
72
73     // Disable the reception overrun detection
74     UartHandle.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_RXOVERRUNDISABLE_INIT;
75     UartHandle.AdvancedInit.OverrunDisable = UART_ADVFEATURE_OVERRUN_DISABLE;
76
77     HAL_UART_Init(&UartHandle);
78 }
79
80 void serial_init(serial_t *obj, PinName tx, PinName rx)
81 {
82     // Determine the UART to use (UART_1, UART_2, ...)
83     UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
84     UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);
85
86     // Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object
87     obj->uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
88     MBED_ASSERT(obj->uart != (UARTName)NC);
89
90     // Enable UART clock
91     if (obj->uart == UART_1) {
92         __HAL_RCC_USART1_CLK_ENABLE();
93         obj->index = 0;
94     }
95
96     if (obj->uart == UART_2) {
97         __HAL_RCC_USART2_CLK_ENABLE();
98         obj->index = 1;
99     }
100
101     if (obj->uart == LPUART_1) {
102         __HAL_RCC_LPUART1_CLK_ENABLE();
103         obj->index = 2;
104     }
105
106 #if defined(USART4_BASE)
107     if (obj->uart == UART_4) {
108         __HAL_RCC_USART4_CLK_ENABLE();
109         obj->index = 3;
110     }
111 #endif
112
113 #if defined(USART5_BASE)
114     if (obj->uart == UART_5) {
115         __HAL_RCC_USART5_CLK_ENABLE();
116         obj->index = 4;
117     }
118 #endif
119
120     // Configure the UART pins
121     pinmap_pinout(tx, PinMap_UART_TX);
122     pinmap_pinout(rx, PinMap_UART_RX);
123     if (tx != NC) {
124         pin_mode(tx, PullUp);
125     }
126     if (rx != NC) {
127         pin_mode(rx, PullUp);
128     }
129
130     // Configure UART
131     obj->baudrate = 9600;
132     obj->databits = UART_WORDLENGTH_8B;
133     obj->stopbits = UART_STOPBITS_1;
134     obj->parity   = UART_PARITY_NONE;
135     obj->pin_tx   = tx;
136     obj->pin_rx   = rx;
137
138     init_uart(obj);
139
140     // For stdio management
141     if (obj->uart == STDIO_UART) {
142         stdio_uart_inited = 1;
143         memcpy(&stdio_uart, obj, sizeof(serial_t));
144     }
145 }
146
147 void serial_free(serial_t *obj)
148 {
149     // Reset UART and disable clock
150     if (obj->uart == UART_1) {
151         __HAL_RCC_USART1_FORCE_RESET();
152         __HAL_RCC_USART1_RELEASE_RESET();
153         __HAL_RCC_USART1_CLK_DISABLE();
154     }
155
156     if (obj->uart == UART_2) {
157         __HAL_RCC_USART2_FORCE_RESET();
158         __HAL_RCC_USART2_RELEASE_RESET();
159         __HAL_RCC_USART2_CLK_DISABLE();
160     }
161
162     if (obj->uart == LPUART_1) {
163         __HAL_RCC_LPUART1_FORCE_RESET();
164         __HAL_RCC_LPUART1_RELEASE_RESET();
165         __HAL_RCC_LPUART1_CLK_DISABLE();
166     }
167
168 #if defined(USART4_BASE)
169     if (obj->uart == UART_4) {
170         __HAL_RCC_USART4_FORCE_RESET();
171         __HAL_RCC_USART4_RELEASE_RESET();
172         __HAL_RCC_USART4_CLK_DISABLE();
173     }
174 #endif
175
176 #if defined(USART5_BASE)
177     if (obj->uart == UART_5) {
178         __HAL_RCC_USART5_FORCE_RESET();
179         __HAL_RCC_USART5_RELEASE_RESET();
180         __HAL_RCC_USART5_CLK_DISABLE();
181     }
182 #endif
183
184     // Configure GPIOs
185     pin_function(obj->pin_tx, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
186     pin_function(obj->pin_rx, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
187
188     serial_irq_ids[obj->index] = 0;
189 }
190
191 void serial_baud(serial_t *obj, int baudrate)
192 {
193     obj->baudrate = baudrate;
194     init_uart(obj);
195 }
196
197 void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits)
198 {
199     if (data_bits == 9) {
200         obj->databits = UART_WORDLENGTH_9B;
201     } else {
202         obj->databits = UART_WORDLENGTH_8B;
203     }
204
205     switch (parity) {
206         case ParityOdd:
207         case ParityForced0:
208             obj->parity = UART_PARITY_ODD;
209             break;
210         case ParityEven:
211         case ParityForced1:
212             obj->parity = UART_PARITY_EVEN;
213             break;
214         default: // ParityNone
215             obj->parity = UART_PARITY_NONE;
216             break;
217     }
218
219     if (stop_bits == 2) {
220         obj->stopbits = UART_STOPBITS_2;
221     } else {
222         obj->stopbits = UART_STOPBITS_1;
223     }
224
225     init_uart(obj);
226 }
227
228 /******************************************************************************
229  * INTERRUPTS HANDLING
230  ******************************************************************************/
231
232 static void uart_irq(UARTName name, int id)
233 {
234     UartHandle.Instance = (USART_TypeDef *)name;
235     if (serial_irq_ids[id] != 0) {
236         if (__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_TC) != RESET) {
237             irq_handler(serial_irq_ids[id], TxIrq);
238             __HAL_UART_CLEAR_IT(&UartHandle, UART_CLEAR_TCF);
239         }
240         if (__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_RXNE) != RESET) {
241             irq_handler(serial_irq_ids[id], RxIrq);
242             volatile uint32_t tmpval = UartHandle.Instance->RDR; // Clear RXNE bit
243         }
244     }
245 }
246
247 static void uart1_irq(void)
248 {
249     uart_irq(UART_1, 0);
250 }
251
252 static void uart2_irq(void)
253 {
254     uart_irq(UART_2, 1);
255 }
256
257 static void lpuart1_irq(void)
258 {
259     uart_irq(LPUART_1, 2);
260 }
261
262 #if defined(USART4_BASE)
263 static void uart4_irq(void)
264 {
265     uart_irq(UART_4, 3);
266 }
267 #endif
268
269 #if defined(USART5_BASE)
270 static void uart5_irq(void)
271 {
272     uart_irq(UART_5, 4);
273 }
274 #endif
275
276 void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id)
277 {
278     irq_handler = handler;
279     serial_irq_ids[obj->index] = id;
280 }
281
282 void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
283 {
284     IRQn_Type irq_n = (IRQn_Type)0;
285     uint32_t vector = 0;
286
287     UartHandle.Instance = (USART_TypeDef *)(obj->uart);
288
289     if (obj->uart == UART_1) {
290         irq_n = USART1_IRQn;
291         vector = (uint32_t)&uart1_irq;
292     }
293
294     if (obj->uart == UART_2) {
295         irq_n = USART2_IRQn;
296         vector = (uint32_t)&uart2_irq;
297     }
298
299     if (obj->uart == LPUART_1) {
300         irq_n = RNG_LPUART1_IRQn;
301         vector = (uint32_t)&lpuart1_irq;
302     }
303
304 #if defined(USART4_BASE)
305     if (obj->uart == UART_4) {
306         irq_n = USART4_5_IRQn;
307         vector = (uint32_t)&uart4_irq;
308     }
309 #endif
310
311 #if defined(USART5_BASE)
312     if (obj->uart == UART_5) {
313         irq_n = USART4_5_IRQn;
314         vector = (uint32_t)&uart5_irq;
315     }
316 #endif
317
318     if (enable) {
319
320         if (irq == RxIrq) {
321             __HAL_UART_ENABLE_IT(&UartHandle, UART_IT_RXNE);
322         } else { // TxIrq
323             __HAL_UART_ENABLE_IT(&UartHandle, UART_IT_TC);
324         }
325
326         NVIC_SetVector(irq_n, vector);
327         NVIC_EnableIRQ(irq_n);
328
329     } else { // disable
330
331         int all_disabled = 0;
332
333         if (irq == RxIrq) {
334             __HAL_UART_DISABLE_IT(&UartHandle, UART_IT_RXNE);
335             // Check if TxIrq is disabled too
336             if ((UartHandle.Instance->CR1 & USART_CR1_TCIE) == 0) all_disabled = 1;
337         } else { // TxIrq
338             __HAL_UART_DISABLE_IT(&UartHandle, UART_IT_TC);
339             // Check if RxIrq is disabled too
340             if ((UartHandle.Instance->CR1 & USART_CR1_RXNEIE) == 0) all_disabled = 1;
341         }
342
343         if (all_disabled) NVIC_DisableIRQ(irq_n);
344
345     }
346 }
347
348 /******************************************************************************
349  * READ/WRITE
350  ******************************************************************************/
351
352 int serial_getc(serial_t *obj)
353 {
354     USART_TypeDef *uart = (USART_TypeDef *)(obj->uart);
355     while (!serial_readable(obj));
356     return (int)(uart->RDR & (uint32_t)0xFF);
357 }
358
359 void serial_putc(serial_t *obj, int c)
360 {
361     USART_TypeDef *uart = (USART_TypeDef *)(obj->uart);
362     while (!serial_writable(obj));
363     uart->TDR = (uint32_t)(c & (uint32_t)0xFF);
364 }
365
366 int serial_readable(serial_t *obj)
367 {
368     int status;
369     UartHandle.Instance = (USART_TypeDef *)(obj->uart);
370     // Check if data is received
371     status = ((__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_RXNE) != RESET) ? 1 : 0);
372     return status;
373 }
374
375 int serial_writable(serial_t *obj)
376 {
377     int status;
378     UartHandle.Instance = (USART_TypeDef *)(obj->uart);
379     // Check if data is transmitted
380     status = ((__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_TXE) != RESET) ? 1 : 0);
381     return status;
382 }
383
384 void serial_clear(serial_t *obj)
385 {
386     UartHandle.Instance = (USART_TypeDef *)(obj->uart);
387     __HAL_UART_CLEAR_IT(&UartHandle, UART_CLEAR_TCF);
388     __HAL_UART_SEND_REQ(&UartHandle, UART_RXDATA_FLUSH_REQUEST);
389 }
390
391 void serial_pinout_tx(PinName tx)
392 {
393     pinmap_pinout(tx, PinMap_UART_TX);
394 }
395
396 void serial_break_set(serial_t *obj)
397 {
398     UartHandle.Instance = (USART_TypeDef *)(obj->uart);
399     __HAL_UART_SEND_REQ(&UartHandle, UART_SENDBREAK_REQUEST);
400 }
401
402 void serial_break_clear(serial_t *obj)
403 {
404 }
405
406 #endif