]> git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32F0/serial_api.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[tmk_keyboard.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_STM / TARGET_STM32F0 / 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 #if defined (TARGET_STM32F091RC)
41 #define UART_NUM (8)
42
43 static uint32_t serial_irq_ids[UART_NUM] = {0, 0, 0, 0, 0, 0, 0, 0};
44
45 #elif defined (TARGET_STM32F030R8) || defined (TARGET_STM32F051R8)
46 #define UART_NUM (2)
47
48 static uint32_t serial_irq_ids[UART_NUM] = {0, 0};
49
50 #else
51 #define UART_NUM (4)
52
53 static uint32_t serial_irq_ids[UART_NUM] = {0, 0, 0, 0};
54
55 #endif
56
57 static uart_irq_handler irq_handler;
58
59 UART_HandleTypeDef UartHandle;
60
61 int stdio_uart_inited = 0;
62 serial_t stdio_uart;
63
64 static void init_uart(serial_t *obj)
65 {
66 UartHandle.Instance = (USART_TypeDef *)(obj->uart);
67
68 UartHandle.Init.BaudRate = obj->baudrate;
69 UartHandle.Init.WordLength = obj->databits;
70 UartHandle.Init.StopBits = obj->stopbits;
71 UartHandle.Init.Parity = obj->parity;
72 UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
73
74 if (obj->pin_rx == NC) {
75 UartHandle.Init.Mode = UART_MODE_TX;
76 } else if (obj->pin_tx == NC) {
77 UartHandle.Init.Mode = UART_MODE_RX;
78 } else {
79 UartHandle.Init.Mode = UART_MODE_TX_RX;
80 }
81
82 // Disable the reception overrun detection
83 UartHandle.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_RXOVERRUNDISABLE_INIT;
84 UartHandle.AdvancedInit.OverrunDisable = UART_ADVFEATURE_OVERRUN_DISABLE;
85
86 HAL_UART_Init(&UartHandle);
87 }
88
89 void serial_init(serial_t *obj, PinName tx, PinName rx)
90 {
91 // Determine the UART to use (UART_1, UART_2, ...)
92 UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
93 UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);
94
95 // Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object
96 obj->uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
97 MBED_ASSERT(obj->uart != (UARTName)NC);
98
99 // Enable USART clock
100 if (obj->uart == UART_1) {
101 __USART1_CLK_ENABLE();
102 obj->index = 0;
103 }
104
105 if (obj->uart == UART_2) {
106 __USART2_CLK_ENABLE();
107 obj->index = 1;
108 }
109
110 #if defined USART3_BASE
111 if (obj->uart == UART_3) {
112 __USART3_CLK_ENABLE();
113 obj->index = 2;
114 }
115 #endif
116
117 #if defined USART4_BASE
118 if (obj->uart == UART_4) {
119 __USART4_CLK_ENABLE();
120 obj->index = 3;
121 }
122 #endif
123
124 #if defined USART5_BASE
125 if (obj->uart == UART_5) {
126 __USART5_CLK_ENABLE();
127 obj->index = 4;
128 }
129 #endif
130
131 #if defined USART6_BASE
132 if (obj->uart == UART_6) {
133 __USART6_CLK_ENABLE();
134 obj->index = 5;
135 }
136 #endif
137
138 #if defined USART7_BASE
139 if (obj->uart == UART_7) {
140 __USART7_CLK_ENABLE();
141 obj->index = 6;
142 }
143 #endif
144
145 #if defined USART8_BASE
146 if (obj->uart == UART_8) {
147 __USART8_CLK_ENABLE();
148 obj->index = 7;
149 }
150 #endif
151
152 // Configure the UART pins
153 pinmap_pinout(tx, PinMap_UART_TX);
154 pinmap_pinout(rx, PinMap_UART_RX);
155 if (tx != NC) {
156 pin_mode(tx, PullUp);
157 }
158 if (rx != NC) {
159 pin_mode(rx, PullUp);
160 }
161
162 // Configure UART
163 obj->baudrate = 9600;
164 obj->databits = UART_WORDLENGTH_8B;
165 obj->stopbits = UART_STOPBITS_1;
166 obj->parity = UART_PARITY_NONE;
167
168 obj->pin_tx = tx;
169 obj->pin_rx = rx;
170
171 init_uart(obj);
172
173 // For stdio management
174 if (obj->uart == STDIO_UART) {
175 stdio_uart_inited = 1;
176 memcpy(&stdio_uart, obj, sizeof(serial_t));
177 }
178 }
179
180 void serial_free(serial_t *obj)
181 {
182 // Reset UART and disable clock
183 if (obj->uart == UART_1) {
184 __USART1_FORCE_RESET();
185 __USART1_RELEASE_RESET();
186 __USART1_CLK_DISABLE();
187 }
188
189 if (obj->uart == UART_2) {
190 __USART2_FORCE_RESET();
191 __USART2_RELEASE_RESET();
192 __USART2_CLK_DISABLE();
193 }
194
195 #if defined USART3_BASE
196 if (obj->uart == UART_3) {
197 __USART3_FORCE_RESET();
198 __USART3_RELEASE_RESET();
199 __USART3_CLK_DISABLE();
200 }
201 #endif
202
203 #if defined USART4_BASE
204 if (obj->uart == UART_4) {
205 __USART4_FORCE_RESET();
206 __USART4_RELEASE_RESET();
207 __USART4_CLK_DISABLE();
208 }
209 #endif
210
211 #if defined USART5_BASE
212 if (obj->uart == UART_5) {
213 __USART5_FORCE_RESET();
214 __USART5_RELEASE_RESET();
215 __USART5_CLK_DISABLE();
216 }
217 #endif
218
219 #if defined USART6_BASE
220 if (obj->uart == UART_6) {
221 __USART6_FORCE_RESET();
222 __USART6_RELEASE_RESET();
223 __USART6_CLK_DISABLE();
224 }
225 #endif
226
227 #if defined USART7_BASE
228 if (obj->uart == UART_7) {
229 __USART7_FORCE_RESET();
230 __USART7_RELEASE_RESET();
231 __USART7_CLK_DISABLE();
232 }
233 #endif
234
235 #if defined USART8_BASE
236 if (obj->uart == UART_8) {
237 __USART8_FORCE_RESET();
238 __USART8_RELEASE_RESET();
239 __USART8_CLK_DISABLE();
240 }
241 #endif
242
243
244 // Configure GPIOs
245 pin_function(obj->pin_tx, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
246 pin_function(obj->pin_rx, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
247
248 serial_irq_ids[obj->index] = 0;
249 }
250
251 void serial_baud(serial_t *obj, int baudrate)
252 {
253 obj->baudrate = baudrate;
254 init_uart(obj);
255 }
256
257 void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits)
258 {
259 if (data_bits == 9) {
260 obj->databits = UART_WORDLENGTH_9B;
261 } else {
262 obj->databits = UART_WORDLENGTH_8B;
263 }
264
265 switch (parity) {
266 case ParityOdd:
267 case ParityForced0:
268 obj->parity = UART_PARITY_ODD;
269 break;
270 case ParityEven:
271 case ParityForced1:
272 obj->parity = UART_PARITY_EVEN;
273 break;
274 default: // ParityNone
275 obj->parity = UART_PARITY_NONE;
276 break;
277 }
278
279 if (stop_bits == 2) {
280 obj->stopbits = UART_STOPBITS_2;
281 } else {
282 obj->stopbits = UART_STOPBITS_1;
283 }
284
285 init_uart(obj);
286 }
287
288 /******************************************************************************
289 * INTERRUPTS HANDLING
290 ******************************************************************************/
291
292 static void uart_irq(UARTName name, int id)
293 {
294 UartHandle.Instance = (USART_TypeDef *)name;
295 if (serial_irq_ids[id] != 0) {
296 if (__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_TC) != RESET) {
297 irq_handler(serial_irq_ids[id], TxIrq);
298 __HAL_UART_CLEAR_IT(&UartHandle, UART_FLAG_TC);
299 }
300 if (__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_RXNE) != RESET) {
301 irq_handler(serial_irq_ids[id], RxIrq);
302 volatile uint32_t tmpval = UartHandle.Instance->RDR; // Clear RXNE bit
303 }
304 }
305 }
306
307 static void uart1_irq(void)
308 {
309 uart_irq(UART_1, 0);
310 }
311
312 static void uart2_irq(void)
313 {
314 uart_irq(UART_2, 1);
315 }
316
317 #if defined USART3_BASE
318 static void uart3_irq(void)
319 {
320 uart_irq(UART_3, 2);
321 }
322 #endif
323
324 #if defined USART4_BASE
325 static void uart4_irq(void)
326 {
327 uart_irq(UART_4, 3);
328 }
329 #endif
330
331 #if defined USART5_BASE
332 static void uart5_irq(void)
333 {
334 uart_irq(UART_5, 4);
335 }
336 #endif
337
338 #if defined USART6_BASE
339 static void uart6_irq(void)
340 {
341 uart_irq(UART_6, 5);
342 }
343 #endif
344
345 #if defined USART7_BASE
346 static void uart7_irq(void)
347 {
348 uart_irq(UART_7, 6);
349 }
350 #endif
351
352 #if defined USART8_BASE
353 static void uart8_irq(void)
354 {
355 uart_irq(UART_8, 7);
356 }
357 #endif
358
359 void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id)
360 {
361 irq_handler = handler;
362 serial_irq_ids[obj->index] = id;
363 }
364
365 void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
366 {
367 IRQn_Type irq_n = (IRQn_Type)0;
368 uint32_t vector = 0;
369
370 UartHandle.Instance = (USART_TypeDef *)(obj->uart);
371
372 if (obj->uart == UART_1) {
373 irq_n = USART1_IRQn;
374 vector = (uint32_t)&uart1_irq;
375 }
376
377 if (obj->uart == UART_2) {
378 irq_n = USART2_IRQn;
379 vector = (uint32_t)&uart2_irq;
380 }
381
382 #if defined (TARGET_STM32F091RC)
383 if (obj->uart == UART_3) {
384 irq_n = USART3_8_IRQn;
385 vector = (uint32_t)&uart3_irq;
386 }
387
388 if (obj->uart == UART_4) {
389 irq_n = USART3_8_IRQn;
390 vector = (uint32_t)&uart4_irq;
391 }
392
393 if (obj->uart == UART_5) {
394 irq_n = USART3_8_IRQn;
395 vector = (uint32_t)&uart5_irq;
396 }
397
398 if (obj->uart == UART_6) {
399 irq_n = USART3_8_IRQn;
400 vector = (uint32_t)&uart6_irq;
401 }
402
403 if (obj->uart == UART_7) {
404 irq_n = USART3_8_IRQn;
405 vector = (uint32_t)&uart7_irq;
406 }
407
408 if (obj->uart == UART_8) {
409 irq_n = USART3_8_IRQn;
410 vector = (uint32_t)&uart8_irq;
411 }
412
413 #elif defined (TARGET_STM32F030R8) || defined (TARGET_STM32F051R8)
414
415 #else
416 if (obj->uart == UART_3) {
417 irq_n = USART3_4_IRQn;
418 vector = (uint32_t)&uart3_irq;
419 }
420
421 if (obj->uart == UART_4) {
422 irq_n = USART3_4_IRQn;
423 vector = (uint32_t)&uart4_irq;
424 }
425 #endif
426
427 if (enable) {
428
429 if (irq == RxIrq) {
430 __HAL_UART_ENABLE_IT(&UartHandle, UART_IT_RXNE);
431 } else { // TxIrq
432 __HAL_UART_ENABLE_IT(&UartHandle, UART_IT_TC);
433 }
434
435 NVIC_SetVector(irq_n, vector);
436 NVIC_EnableIRQ(irq_n);
437
438 } else { // disable
439
440 int all_disabled = 0;
441
442 if (irq == RxIrq) {
443 __HAL_UART_DISABLE_IT(&UartHandle, UART_IT_RXNE);
444 // Check if TxIrq is disabled too
445 if ((UartHandle.Instance->CR1 & USART_CR1_TCIE) == 0) all_disabled = 1;
446 } else { // TxIrq
447 __HAL_UART_DISABLE_IT(&UartHandle, UART_IT_TC);
448 // Check if RxIrq is disabled too
449 if ((UartHandle.Instance->CR1 & USART_CR1_RXNEIE) == 0) all_disabled = 1;
450 }
451
452 if (all_disabled) NVIC_DisableIRQ(irq_n);
453
454 }
455 }
456
457 /******************************************************************************
458 * READ/WRITE
459 ******************************************************************************/
460
461 int serial_getc(serial_t *obj)
462 {
463 USART_TypeDef *uart = (USART_TypeDef *)(obj->uart);
464 while (!serial_readable(obj));
465 return (int)(uart->RDR & (uint16_t)0xFF);
466 }
467
468 void serial_putc(serial_t *obj, int c)
469 {
470 USART_TypeDef *uart = (USART_TypeDef *)(obj->uart);
471 while (!serial_writable(obj));
472 uart->TDR = (uint32_t)(c & (uint16_t)0xFF);
473 }
474
475 int serial_readable(serial_t *obj)
476 {
477 int status;
478 UartHandle.Instance = (USART_TypeDef *)(obj->uart);
479 // Check if data is received
480 status = ((__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_RXNE) != RESET) ? 1 : 0);
481 return status;
482 }
483
484 int serial_writable(serial_t *obj)
485 {
486 int status;
487 UartHandle.Instance = (USART_TypeDef *)(obj->uart);
488 // Check if data is transmitted
489 status = ((__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_TXE) != RESET) ? 1 : 0);
490 return status;
491 }
492
493 void serial_clear(serial_t *obj)
494 {
495 UartHandle.Instance = (USART_TypeDef *)(obj->uart);
496 __HAL_UART_CLEAR_IT(&UartHandle, UART_FLAG_TC);
497 __HAL_UART_SEND_REQ(&UartHandle, UART_RXDATA_FLUSH_REQUEST);
498 }
499
500 void serial_pinout_tx(PinName tx)
501 {
502 pinmap_pinout(tx, PinMap_UART_TX);
503 }
504
505 void serial_break_set(serial_t *obj)
506 {
507 // [TODO]
508 }
509
510 void serial_break_clear(serial_t *obj)
511 {
512 // [TODO]
513 }
514
515 #endif
Imprint / Impressum