mbed
Fork of mbed-dev by
Diff: targets/TARGET_STM/TARGET_STM32L0/serial_device.c
- Revision:
- 181:96ed750bd169
- Parent:
- 178:d650f5d4c87a
- Child:
- 182:57724642e740
--- a/targets/TARGET_STM/TARGET_STM32L0/serial_device.c Thu Dec 07 14:01:42 2017 +0000 +++ b/targets/TARGET_STM/TARGET_STM32L0/serial_device.c Wed Jan 17 15:23:54 2018 +0000 @@ -1,6 +1,6 @@ /* mbed Microcontroller Library ******************************************************************************* - * Copyright (c) 2015, STMicroelectronics + * Copyright (c) 2017, STMicroelectronics * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -27,171 +27,24 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ -#include "mbed_assert.h" -#include "serial_api.h" -#include "serial_api_hal.h" #if DEVICE_SERIAL -#include "cmsis.h" -#include "pinmap.h" -#include <string.h> -#include "PeripheralPins.h" -#include "mbed_error.h" +#include "serial_api_hal.h" -#define UART_NUM (5) +#if defined (TARGET_STM32L011K4) || defined (TARGET_STM32L031K6) + #define UART_NUM (2) +#elif defined (TARGET_STM32L053x8) + #define UART_NUM (3) +#else + #define UART_NUM (5) +#endif -static uint32_t serial_irq_ids[UART_NUM] = {0}; +uint32_t serial_irq_ids[UART_NUM] = {0}; UART_HandleTypeDef uart_handlers[UART_NUM]; static uart_irq_handler irq_handler; -int stdio_uart_inited = 0; -serial_t stdio_uart; - -void serial_init(serial_t *obj, PinName tx, PinName rx) -{ - struct serial_s *obj_s = SERIAL_S(obj); - - // Determine the UART to use (UART_1, UART_2, ...) - UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX); - UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX); - - // Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object - obj_s->uart = (UARTName)pinmap_merge(uart_tx, uart_rx); - MBED_ASSERT(obj_s->uart != (UARTName)NC); - - // Enable UART clock -#if defined(USART1_BASE) - if (obj_s->uart == UART_1) { - __HAL_RCC_USART1_FORCE_RESET(); - __HAL_RCC_USART1_RELEASE_RESET(); - __HAL_RCC_USART1_CLK_ENABLE(); - obj_s->index = 0; - } -#endif - - if (obj_s->uart == UART_2) { - __HAL_RCC_USART2_FORCE_RESET(); - __HAL_RCC_USART2_RELEASE_RESET(); - __HAL_RCC_USART2_CLK_ENABLE(); - obj_s->index = 1; - } - - if (obj_s->uart == LPUART_1) { - __HAL_RCC_LPUART1_FORCE_RESET(); - __HAL_RCC_LPUART1_RELEASE_RESET(); - __HAL_RCC_LPUART1_CLK_ENABLE(); - obj_s->index = 2; - } - -#if defined(USART4_BASE) - if (obj_s->uart == UART_4) { - __HAL_RCC_USART4_FORCE_RESET(); - __HAL_RCC_USART4_RELEASE_RESET(); - __HAL_RCC_USART4_CLK_ENABLE(); - obj_s->index = 3; - } -#endif - -#if defined(USART5_BASE) - if (obj_s->uart == UART_5) { - __HAL_RCC_USART5_FORCE_RESET(); - __HAL_RCC_USART5_RELEASE_RESET(); - __HAL_RCC_USART5_CLK_ENABLE(); - obj_s->index = 4; - } -#endif - - // Configure the UART pins - pinmap_pinout(tx, PinMap_UART_TX); - pinmap_pinout(rx, PinMap_UART_RX); - - if (tx != NC) { - pin_mode(tx, PullUp); - } - if (rx != NC) { - pin_mode(rx, PullUp); - } - - // Configure UART - obj_s->baudrate = 9600; - obj_s->databits = UART_WORDLENGTH_8B; - obj_s->stopbits = UART_STOPBITS_1; - obj_s->parity = UART_PARITY_NONE; - -#if DEVICE_SERIAL_FC - obj_s->hw_flow_ctl = UART_HWCONTROL_NONE; -#endif - - obj_s->pin_tx = tx; - obj_s->pin_rx = rx; - - init_uart(obj); - - // For stdio management - if (obj_s->uart == STDIO_UART) { - stdio_uart_inited = 1; - memcpy(&stdio_uart, obj, sizeof(serial_t)); - } -} - -void serial_free(serial_t *obj) -{ - struct serial_s *obj_s = SERIAL_S(obj); - - // Reset UART and disable clock -#if defined(USART1_BASE) - if (obj_s->uart == UART_1) { - __HAL_RCC_USART1_FORCE_RESET(); - __HAL_RCC_USART1_RELEASE_RESET(); - __HAL_RCC_USART1_CLK_DISABLE(); - } -#endif - - if (obj_s->uart == UART_2) { - __HAL_RCC_USART2_FORCE_RESET(); - __HAL_RCC_USART2_RELEASE_RESET(); - __HAL_RCC_USART2_CLK_DISABLE(); - } - - if (obj_s->uart == LPUART_1) { - __HAL_RCC_LPUART1_FORCE_RESET(); - __HAL_RCC_LPUART1_RELEASE_RESET(); - __HAL_RCC_LPUART1_CLK_DISABLE(); - } - -#if defined(USART4_BASE) - if (obj_s->uart == UART_4) { - __HAL_RCC_USART4_FORCE_RESET(); - __HAL_RCC_USART4_RELEASE_RESET(); - __HAL_RCC_USART4_CLK_DISABLE(); - } -#endif - -#if defined(USART5_BASE) - if (obj_s->uart == UART_5) { - __HAL_RCC_USART5_FORCE_RESET(); - __HAL_RCC_USART5_RELEASE_RESET(); - __HAL_RCC_USART5_CLK_DISABLE(); - } -#endif - - // Configure GPIOs - pin_function(obj_s->pin_tx, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); - pin_function(obj_s->pin_rx, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); - - serial_irq_ids[obj_s->index] = 0; -} - -void serial_baud(serial_t *obj, int baudrate) -{ - struct serial_s *obj_s = SERIAL_S(obj); - - obj_s->baudrate = baudrate; - init_uart(obj); -} - /****************************************************************************** * INTERRUPTS HANDLING ******************************************************************************/ @@ -761,7 +614,7 @@ } } -#endif +#endif /* DEVICE_SERIAL_ASYNCH */ #if DEVICE_SERIAL_FC @@ -820,6 +673,6 @@ init_uart(obj); } -#endif +#endif /* DEVICE_SERIAL_FC */ -#endif +#endif /* DEVICE_SERIAL */