]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32F4/serial_api.c
Add a qwerty layer
[max/tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_STM / TARGET_STM32F4 / 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 (8)
41
42 static uint32_t serial_irq_ids[UART_NUM] = {0, 0, 0, 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 USART clock
83     switch (obj->uart) {
84         case UART_1:
85             __USART1_CLK_ENABLE();
86             obj->index = 0;
87             break;
88         case UART_2:
89             __USART2_CLK_ENABLE();
90             obj->index = 1;
91             break;
92 #if defined(USART3_BASE)
93         case UART_3:
94             __USART3_CLK_ENABLE();
95             obj->index = 2;
96             break;
97 #endif
98 #if defined(UART4_BASE)
99         case UART_4:
100             __UART4_CLK_ENABLE();
101             obj->index = 3;
102             break;
103 #endif
104 #if defined(UART5_BASE)
105         case UART_5:
106             __UART5_CLK_ENABLE();
107             obj->index = 4;
108             break;
109 #endif
110         case UART_6:
111             __USART6_CLK_ENABLE();
112             obj->index = 5;
113             break;
114 #if defined(UART7_BASE)
115         case UART_7:
116             __UART7_CLK_ENABLE();
117             obj->index = 6;
118             break;
119 #endif
120 #if defined(UART8_BASE)
121         case UART_8:
122             __UART8_CLK_ENABLE();
123             obj->index = 7;
124             break;
125 #endif
126     }
127
128     // Configure the UART pins
129     pinmap_pinout(tx, PinMap_UART_TX);
130     pinmap_pinout(rx, PinMap_UART_RX);
131     if (tx != NC) {
132         pin_mode(tx, PullUp);
133     }
134     if (rx != NC) {
135         pin_mode(rx, PullUp);
136     }
137
138     // Configure UART
139     obj->baudrate = 9600;
140     obj->databits = UART_WORDLENGTH_8B;
141     obj->stopbits = UART_STOPBITS_1;
142     obj->parity   = UART_PARITY_NONE;
143
144     obj->pin_tx = tx;
145     obj->pin_rx = rx;
146
147     init_uart(obj);
148
149     // For stdio management
150     if (obj->uart == STDIO_UART) {
151         stdio_uart_inited = 1;
152         memcpy(&stdio_uart, obj, sizeof(serial_t));
153     }
154 }
155
156 void serial_free(serial_t *obj)
157 {
158     // Reset UART and disable clock
159     switch (obj->uart) {
160         case UART_1:
161             __USART1_FORCE_RESET();
162             __USART1_RELEASE_RESET();
163             __USART1_CLK_DISABLE();
164             break;
165         case UART_2:
166             __USART2_FORCE_RESET();
167             __USART2_RELEASE_RESET();
168             __USART2_CLK_DISABLE();
169             break;
170 #if defined(USART3_BASE)
171         case UART_3:
172             __USART3_FORCE_RESET();
173             __USART3_RELEASE_RESET();
174             __USART3_CLK_DISABLE();
175             break;
176 #endif
177 #if defined(UART4_BASE)
178         case UART_4:
179             __UART4_FORCE_RESET();
180             __UART4_RELEASE_RESET();
181             __UART4_CLK_DISABLE();
182             break;
183 #endif
184 #if defined(UART5_BASE)
185         case UART_5:
186             __UART5_FORCE_RESET();
187             __UART5_RELEASE_RESET();
188             __UART5_CLK_DISABLE();
189             break;
190 #endif
191         case UART_6:
192             __USART6_FORCE_RESET();
193             __USART6_RELEASE_RESET();
194             __USART6_CLK_DISABLE();
195             break;
196 #if defined(UART7_BASE)
197         case UART_7:
198             __UART7_FORCE_RESET();
199             __UART7_RELEASE_RESET();
200             __UART7_CLK_DISABLE();
201             break;
202 #endif
203 #if defined(UART8_BASE)
204         case UART_8:
205             __UART8_FORCE_RESET();
206             __UART8_RELEASE_RESET();
207             __UART8_CLK_DISABLE();
208             break;
209 #endif
210     }
211     // Configure GPIOs
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));
214
215     serial_irq_ids[obj->index] = 0;
216 }
217
218 void serial_baud(serial_t *obj, int baudrate)
219 {
220     obj->baudrate = baudrate;
221     init_uart(obj);
222 }
223
224 void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits)
225 {
226     if (data_bits == 9) {
227         obj->databits = UART_WORDLENGTH_9B;
228     } else {
229         obj->databits = UART_WORDLENGTH_8B;
230     }
231
232     switch (parity) {
233         case ParityOdd:
234         case ParityForced0:
235             obj->parity = UART_PARITY_ODD;
236             break;
237         case ParityEven:
238         case ParityForced1:
239             obj->parity = UART_PARITY_EVEN;
240             break;
241         default: // ParityNone
242             obj->parity = UART_PARITY_NONE;
243             break;
244     }
245
246     if (stop_bits == 2) {
247         obj->stopbits = UART_STOPBITS_2;
248     } else {
249         obj->stopbits = UART_STOPBITS_1;
250     }
251
252     init_uart(obj);
253 }
254
255 /******************************************************************************
256  * INTERRUPTS HANDLING
257  ******************************************************************************/
258
259 static void uart_irq(UARTName name, int id)
260 {
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);
266         }
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);
270         }
271     }
272 }
273
274 static void uart1_irq(void)
275 {
276     uart_irq(UART_1, 0);
277 }
278
279 static void uart2_irq(void)
280 {
281     uart_irq(UART_2, 1);
282 }
283
284 #if defined(USART3_BASE)
285 static void uart3_irq(void) 
286 {
287     uart_irq(UART_3, 2);
288 }
289 #endif
290
291 #if defined(UART4_BASE)
292 static void uart4_irq(void) 
293 {
294     uart_irq(UART_4, 3);
295 }
296 #endif
297
298 #if defined(UART5_BASE)
299 static void uart5_irq(void) 
300 {
301     uart_irq(UART_5, 4);
302 }
303 #endif
304
305 static void uart6_irq(void)
306 {
307     uart_irq(UART_6, 5);
308 }
309
310 #if defined(UART7_BASE)
311 static void uart7_irq(void) 
312 {
313     uart_irq(UART_7, 6);
314 }
315 #endif
316
317 #if defined(UART8_BASE)
318 static void uart8_irq(void) 
319 {
320     uart_irq(UART_8, 7);
321 }
322 #endif
323
324 void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id)
325 {
326     irq_handler = handler;
327     serial_irq_ids[obj->index] = id;
328 }
329
330 void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
331 {
332     IRQn_Type irq_n = (IRQn_Type)0;
333     uint32_t vector = 0;
334
335     UartHandle.Instance = (USART_TypeDef *)(obj->uart);
336
337     switch (obj->uart) {
338         case UART_1:
339             irq_n = USART1_IRQn;
340             vector = (uint32_t)&uart1_irq;
341             break;
342
343         case UART_2:
344             irq_n = USART2_IRQn;
345             vector = (uint32_t)&uart2_irq;
346             break;
347 #if defined(USART3_BASE)
348         case UART_3:
349             irq_n = USART3_IRQn;
350             vector = (uint32_t)&uart3_irq;
351             break;
352 #endif
353 #if defined(UART4_BASE)
354         case UART_4:
355             irq_n = UART4_IRQn;
356             vector = (uint32_t)&uart4_irq;
357             break;
358 #endif
359 #if defined(UART5_BASE)
360         case UART_5:
361             irq_n = UART5_IRQn;
362             vector = (uint32_t)&uart5_irq;
363             break;
364 #endif
365         case UART_6:
366             irq_n = USART6_IRQn;
367             vector = (uint32_t)&uart6_irq;
368             break;
369 #if defined(UART7_BASE)
370         case UART_7:
371             irq_n = UART7_IRQn;
372             vector = (uint32_t)&uart7_irq;
373             break;
374 #endif
375 #if defined(UART8_BASE)
376         case UART_8:
377             irq_n = UART8_IRQn;
378             vector = (uint32_t)&uart8_irq;
379             break;
380 #endif
381     }
382
383     if (enable) {
384
385         if (irq == RxIrq) {
386             __HAL_UART_ENABLE_IT(&UartHandle, UART_IT_RXNE);
387         } else { // TxIrq
388             __HAL_UART_ENABLE_IT(&UartHandle, UART_IT_TC);
389         }
390
391         NVIC_SetVector(irq_n, vector);
392         NVIC_EnableIRQ(irq_n);
393
394     } else { // disable
395
396         int all_disabled = 0;
397
398         if (irq == RxIrq) {
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;
402         } else { // TxIrq
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;
406         }
407
408         if (all_disabled) NVIC_DisableIRQ(irq_n);
409
410     }
411 }
412
413 /******************************************************************************
414  * READ/WRITE
415  ******************************************************************************/
416
417 int serial_getc(serial_t *obj)
418 {
419     USART_TypeDef *uart = (USART_TypeDef *)(obj->uart);
420     while (!serial_readable(obj));
421     return (int)(uart->DR & 0x1FF);
422 }
423
424 void serial_putc(serial_t *obj, int c)
425 {
426     USART_TypeDef *uart = (USART_TypeDef *)(obj->uart);
427     while (!serial_writable(obj));
428     uart->DR = (uint32_t)(c & 0x1FF);
429 }
430
431 int serial_readable(serial_t *obj)
432 {
433     int status;
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);
437     return status;
438 }
439
440 int serial_writable(serial_t *obj)
441 {
442     int status;
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);
446     return status;
447 }
448
449 void serial_clear(serial_t *obj)
450 {
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);
454 }
455
456 void serial_pinout_tx(PinName tx)
457 {
458     pinmap_pinout(tx, PinMap_UART_TX);
459 }
460
461 void serial_break_set(serial_t *obj)
462 {
463     UartHandle.Instance = (USART_TypeDef *)(obj->uart);
464     HAL_LIN_SendBreak(&UartHandle);
465 }
466
467 void serial_break_clear(serial_t *obj)
468 {
469 }
470
471 #endif