mbed library sources

Dependents:   FRDM-KL46Z_LCD_Test FRDM-KL46Z_LCD_Test FRDM-KL46Z_Plantilla FRDM-KL46Z_Plantilla ... more

Committer:
ebrus
Date:
Thu Jul 28 15:56:34 2016 +0000
Revision:
0:6bc4ac881c8e
1;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ebrus 0:6bc4ac881c8e 1 /* mbed Microcontroller Library
ebrus 0:6bc4ac881c8e 2 *******************************************************************************
ebrus 0:6bc4ac881c8e 3 * Copyright (c) 2014, STMicroelectronics
ebrus 0:6bc4ac881c8e 4 * All rights reserved.
ebrus 0:6bc4ac881c8e 5 *
ebrus 0:6bc4ac881c8e 6 * Redistribution and use in source and binary forms, with or without
ebrus 0:6bc4ac881c8e 7 * modification, are permitted provided that the following conditions are met:
ebrus 0:6bc4ac881c8e 8 *
ebrus 0:6bc4ac881c8e 9 * 1. Redistributions of source code must retain the above copyright notice,
ebrus 0:6bc4ac881c8e 10 * this list of conditions and the following disclaimer.
ebrus 0:6bc4ac881c8e 11 * 2. Redistributions in binary form must reproduce the above copyright notice,
ebrus 0:6bc4ac881c8e 12 * this list of conditions and the following disclaimer in the documentation
ebrus 0:6bc4ac881c8e 13 * and/or other materials provided with the distribution.
ebrus 0:6bc4ac881c8e 14 * 3. Neither the name of STMicroelectronics nor the names of its contributors
ebrus 0:6bc4ac881c8e 15 * may be used to endorse or promote products derived from this software
ebrus 0:6bc4ac881c8e 16 * without specific prior written permission.
ebrus 0:6bc4ac881c8e 17 *
ebrus 0:6bc4ac881c8e 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
ebrus 0:6bc4ac881c8e 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
ebrus 0:6bc4ac881c8e 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
ebrus 0:6bc4ac881c8e 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
ebrus 0:6bc4ac881c8e 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
ebrus 0:6bc4ac881c8e 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
ebrus 0:6bc4ac881c8e 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
ebrus 0:6bc4ac881c8e 25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
ebrus 0:6bc4ac881c8e 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
ebrus 0:6bc4ac881c8e 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
ebrus 0:6bc4ac881c8e 28 *******************************************************************************
ebrus 0:6bc4ac881c8e 29 */
ebrus 0:6bc4ac881c8e 30 #include "mbed_assert.h"
ebrus 0:6bc4ac881c8e 31 #include "serial_api.h"
ebrus 0:6bc4ac881c8e 32
ebrus 0:6bc4ac881c8e 33 #if DEVICE_SERIAL
ebrus 0:6bc4ac881c8e 34
ebrus 0:6bc4ac881c8e 35 #include "cmsis.h"
ebrus 0:6bc4ac881c8e 36 #include "pinmap.h"
ebrus 0:6bc4ac881c8e 37 #include <string.h>
ebrus 0:6bc4ac881c8e 38
ebrus 0:6bc4ac881c8e 39 static const PinMap PinMap_UART_TX[] = {
ebrus 0:6bc4ac881c8e 40 {PA_2, UART_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF7_USART2)},
ebrus 0:6bc4ac881c8e 41 {PA_9, UART_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF7_USART1)},
ebrus 0:6bc4ac881c8e 42 {PA_14, UART_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF7_USART2)},
ebrus 0:6bc4ac881c8e 43 {PB_3, UART_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF7_USART2)},
ebrus 0:6bc4ac881c8e 44 {PB_6, UART_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF7_USART1)},
ebrus 0:6bc4ac881c8e 45 {PB_9, UART_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF7_USART3)},
ebrus 0:6bc4ac881c8e 46 {PB_10, UART_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF7_USART3)},
ebrus 0:6bc4ac881c8e 47 {PC_4, UART_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF7_USART1)},
ebrus 0:6bc4ac881c8e 48 {PC_10, UART_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF7_USART3)},
ebrus 0:6bc4ac881c8e 49 {NC, NC, 0}
ebrus 0:6bc4ac881c8e 50 };
ebrus 0:6bc4ac881c8e 51
ebrus 0:6bc4ac881c8e 52 static const PinMap PinMap_UART_RX[] = {
ebrus 0:6bc4ac881c8e 53 {PA_3, UART_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF7_USART2)},
ebrus 0:6bc4ac881c8e 54 {PA_10, UART_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF7_USART1)},
ebrus 0:6bc4ac881c8e 55 {PA_15, UART_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF7_USART2)},
ebrus 0:6bc4ac881c8e 56 {PB_4, UART_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF7_USART2)},
ebrus 0:6bc4ac881c8e 57 {PB_7, UART_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF7_USART1)},
ebrus 0:6bc4ac881c8e 58 {PB_8, UART_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF7_USART3)},
ebrus 0:6bc4ac881c8e 59 {PB_11, UART_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF7_USART3)},
ebrus 0:6bc4ac881c8e 60 {PC_5, UART_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF7_USART1)},
ebrus 0:6bc4ac881c8e 61 {PC_11, UART_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF7_USART3)},
ebrus 0:6bc4ac881c8e 62 {NC, NC, 0}
ebrus 0:6bc4ac881c8e 63 };
ebrus 0:6bc4ac881c8e 64
ebrus 0:6bc4ac881c8e 65 #define UART_NUM (3)
ebrus 0:6bc4ac881c8e 66
ebrus 0:6bc4ac881c8e 67 static uint32_t serial_irq_ids[UART_NUM] = {0, 0, 0};
ebrus 0:6bc4ac881c8e 68
ebrus 0:6bc4ac881c8e 69 static uart_irq_handler irq_handler;
ebrus 0:6bc4ac881c8e 70
ebrus 0:6bc4ac881c8e 71 UART_HandleTypeDef UartHandle;
ebrus 0:6bc4ac881c8e 72
ebrus 0:6bc4ac881c8e 73 int stdio_uart_inited = 0;
ebrus 0:6bc4ac881c8e 74 serial_t stdio_uart;
ebrus 0:6bc4ac881c8e 75
ebrus 0:6bc4ac881c8e 76 static void init_uart(serial_t *obj)
ebrus 0:6bc4ac881c8e 77 {
ebrus 0:6bc4ac881c8e 78 UartHandle.Instance = (USART_TypeDef *)(obj->uart);
ebrus 0:6bc4ac881c8e 79
ebrus 0:6bc4ac881c8e 80 UartHandle.Init.BaudRate = obj->baudrate;
ebrus 0:6bc4ac881c8e 81 UartHandle.Init.WordLength = obj->databits;
ebrus 0:6bc4ac881c8e 82 UartHandle.Init.StopBits = obj->stopbits;
ebrus 0:6bc4ac881c8e 83 UartHandle.Init.Parity = obj->parity;
ebrus 0:6bc4ac881c8e 84 UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
ebrus 0:6bc4ac881c8e 85
ebrus 0:6bc4ac881c8e 86 if (obj->pin_rx == NC) {
ebrus 0:6bc4ac881c8e 87 UartHandle.Init.Mode = UART_MODE_TX;
ebrus 0:6bc4ac881c8e 88 } else if (obj->pin_tx == NC) {
ebrus 0:6bc4ac881c8e 89 UartHandle.Init.Mode = UART_MODE_RX;
ebrus 0:6bc4ac881c8e 90 } else {
ebrus 0:6bc4ac881c8e 91 UartHandle.Init.Mode = UART_MODE_TX_RX;
ebrus 0:6bc4ac881c8e 92 }
ebrus 0:6bc4ac881c8e 93
ebrus 0:6bc4ac881c8e 94 // Disable the reception overrun detection
ebrus 0:6bc4ac881c8e 95 UartHandle.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_RXOVERRUNDISABLE_INIT;
ebrus 0:6bc4ac881c8e 96 UartHandle.AdvancedInit.OverrunDisable = UART_ADVFEATURE_OVERRUN_DISABLE;
ebrus 0:6bc4ac881c8e 97
ebrus 0:6bc4ac881c8e 98 HAL_UART_Init(&UartHandle);
ebrus 0:6bc4ac881c8e 99 }
ebrus 0:6bc4ac881c8e 100
ebrus 0:6bc4ac881c8e 101 void serial_init(serial_t *obj, PinName tx, PinName rx)
ebrus 0:6bc4ac881c8e 102 {
ebrus 0:6bc4ac881c8e 103 // Determine the UART to use (UART_1, UART_2, ...)
ebrus 0:6bc4ac881c8e 104 UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
ebrus 0:6bc4ac881c8e 105 UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);
ebrus 0:6bc4ac881c8e 106
ebrus 0:6bc4ac881c8e 107 // Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object
ebrus 0:6bc4ac881c8e 108 obj->uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
ebrus 0:6bc4ac881c8e 109 MBED_ASSERT(obj->uart != (UARTName)NC);
ebrus 0:6bc4ac881c8e 110
ebrus 0:6bc4ac881c8e 111 // Enable USART clock + switch to SystemClock
ebrus 0:6bc4ac881c8e 112 if (obj->uart == UART_1) {
ebrus 0:6bc4ac881c8e 113 __USART1_CLK_ENABLE();
ebrus 0:6bc4ac881c8e 114 __HAL_RCC_USART1_CONFIG(RCC_USART1CLKSOURCE_SYSCLK);
ebrus 0:6bc4ac881c8e 115 obj->index = 0;
ebrus 0:6bc4ac881c8e 116 }
ebrus 0:6bc4ac881c8e 117 if (obj->uart == UART_2) {
ebrus 0:6bc4ac881c8e 118 __USART2_CLK_ENABLE();
ebrus 0:6bc4ac881c8e 119 __HAL_RCC_USART2_CONFIG(RCC_USART2CLKSOURCE_SYSCLK);
ebrus 0:6bc4ac881c8e 120 obj->index = 1;
ebrus 0:6bc4ac881c8e 121 }
ebrus 0:6bc4ac881c8e 122 if (obj->uart == UART_3) {
ebrus 0:6bc4ac881c8e 123 __USART3_CLK_ENABLE();
ebrus 0:6bc4ac881c8e 124 __HAL_RCC_USART3_CONFIG(RCC_USART3CLKSOURCE_SYSCLK);
ebrus 0:6bc4ac881c8e 125 obj->index = 2;
ebrus 0:6bc4ac881c8e 126 }
ebrus 0:6bc4ac881c8e 127
ebrus 0:6bc4ac881c8e 128 // Configure the UART pins
ebrus 0:6bc4ac881c8e 129 pinmap_pinout(tx, PinMap_UART_TX);
ebrus 0:6bc4ac881c8e 130 pinmap_pinout(rx, PinMap_UART_RX);
ebrus 0:6bc4ac881c8e 131 pin_mode(tx, PullUp);
ebrus 0:6bc4ac881c8e 132 pin_mode(rx, PullUp);
ebrus 0:6bc4ac881c8e 133
ebrus 0:6bc4ac881c8e 134 // Configure UART
ebrus 0:6bc4ac881c8e 135 obj->baudrate = 9600;
ebrus 0:6bc4ac881c8e 136 obj->databits = UART_WORDLENGTH_8B;
ebrus 0:6bc4ac881c8e 137 obj->stopbits = UART_STOPBITS_1;
ebrus 0:6bc4ac881c8e 138 obj->parity = UART_PARITY_NONE;
ebrus 0:6bc4ac881c8e 139
ebrus 0:6bc4ac881c8e 140 obj->pin_tx = tx;
ebrus 0:6bc4ac881c8e 141 obj->pin_rx = rx;
ebrus 0:6bc4ac881c8e 142
ebrus 0:6bc4ac881c8e 143 init_uart(obj);
ebrus 0:6bc4ac881c8e 144
ebrus 0:6bc4ac881c8e 145 // For stdio management
ebrus 0:6bc4ac881c8e 146 if (obj->uart == STDIO_UART) {
ebrus 0:6bc4ac881c8e 147 stdio_uart_inited = 1;
ebrus 0:6bc4ac881c8e 148 memcpy(&stdio_uart, obj, sizeof(serial_t));
ebrus 0:6bc4ac881c8e 149 }
ebrus 0:6bc4ac881c8e 150 }
ebrus 0:6bc4ac881c8e 151
ebrus 0:6bc4ac881c8e 152 void serial_free(serial_t *obj)
ebrus 0:6bc4ac881c8e 153 {
ebrus 0:6bc4ac881c8e 154 // Reset UART and disable clock
ebrus 0:6bc4ac881c8e 155 if (obj->uart == UART_1) {
ebrus 0:6bc4ac881c8e 156 __USART1_FORCE_RESET();
ebrus 0:6bc4ac881c8e 157 __USART1_RELEASE_RESET();
ebrus 0:6bc4ac881c8e 158 __USART1_CLK_DISABLE();
ebrus 0:6bc4ac881c8e 159 }
ebrus 0:6bc4ac881c8e 160 if (obj->uart == UART_2) {
ebrus 0:6bc4ac881c8e 161 __USART2_FORCE_RESET();
ebrus 0:6bc4ac881c8e 162 __USART2_RELEASE_RESET();
ebrus 0:6bc4ac881c8e 163 __USART2_CLK_DISABLE();
ebrus 0:6bc4ac881c8e 164 }
ebrus 0:6bc4ac881c8e 165 if (obj->uart == UART_3) {
ebrus 0:6bc4ac881c8e 166 __USART3_FORCE_RESET();
ebrus 0:6bc4ac881c8e 167 __USART3_RELEASE_RESET();
ebrus 0:6bc4ac881c8e 168 __USART3_CLK_DISABLE();
ebrus 0:6bc4ac881c8e 169 }
ebrus 0:6bc4ac881c8e 170
ebrus 0:6bc4ac881c8e 171 // Configure GPIOs
ebrus 0:6bc4ac881c8e 172 pin_function(obj->pin_tx, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
ebrus 0:6bc4ac881c8e 173 pin_function(obj->pin_rx, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
ebrus 0:6bc4ac881c8e 174
ebrus 0:6bc4ac881c8e 175 serial_irq_ids[obj->index] = 0;
ebrus 0:6bc4ac881c8e 176 }
ebrus 0:6bc4ac881c8e 177
ebrus 0:6bc4ac881c8e 178 void serial_baud(serial_t *obj, int baudrate)
ebrus 0:6bc4ac881c8e 179 {
ebrus 0:6bc4ac881c8e 180 obj->baudrate = baudrate;
ebrus 0:6bc4ac881c8e 181 init_uart(obj);
ebrus 0:6bc4ac881c8e 182 }
ebrus 0:6bc4ac881c8e 183
ebrus 0:6bc4ac881c8e 184 void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits)
ebrus 0:6bc4ac881c8e 185 {
ebrus 0:6bc4ac881c8e 186 if (data_bits == 9) {
ebrus 0:6bc4ac881c8e 187 obj->databits = UART_WORDLENGTH_9B;
ebrus 0:6bc4ac881c8e 188 } else {
ebrus 0:6bc4ac881c8e 189 obj->databits = UART_WORDLENGTH_8B;
ebrus 0:6bc4ac881c8e 190 }
ebrus 0:6bc4ac881c8e 191
ebrus 0:6bc4ac881c8e 192 switch (parity) {
ebrus 0:6bc4ac881c8e 193 case ParityOdd:
ebrus 0:6bc4ac881c8e 194 case ParityForced0:
ebrus 0:6bc4ac881c8e 195 obj->parity = UART_PARITY_ODD;
ebrus 0:6bc4ac881c8e 196 break;
ebrus 0:6bc4ac881c8e 197 case ParityEven:
ebrus 0:6bc4ac881c8e 198 case ParityForced1:
ebrus 0:6bc4ac881c8e 199 obj->parity = UART_PARITY_EVEN;
ebrus 0:6bc4ac881c8e 200 break;
ebrus 0:6bc4ac881c8e 201 default: // ParityNone
ebrus 0:6bc4ac881c8e 202 obj->parity = UART_PARITY_NONE;
ebrus 0:6bc4ac881c8e 203 break;
ebrus 0:6bc4ac881c8e 204 }
ebrus 0:6bc4ac881c8e 205
ebrus 0:6bc4ac881c8e 206 if (stop_bits == 2) {
ebrus 0:6bc4ac881c8e 207 obj->stopbits = UART_STOPBITS_2;
ebrus 0:6bc4ac881c8e 208 } else {
ebrus 0:6bc4ac881c8e 209 obj->stopbits = UART_STOPBITS_1;
ebrus 0:6bc4ac881c8e 210 }
ebrus 0:6bc4ac881c8e 211
ebrus 0:6bc4ac881c8e 212 init_uart(obj);
ebrus 0:6bc4ac881c8e 213 }
ebrus 0:6bc4ac881c8e 214
ebrus 0:6bc4ac881c8e 215 /******************************************************************************
ebrus 0:6bc4ac881c8e 216 * INTERRUPTS HANDLING
ebrus 0:6bc4ac881c8e 217 ******************************************************************************/
ebrus 0:6bc4ac881c8e 218
ebrus 0:6bc4ac881c8e 219 static void uart_irq(UARTName name, int id)
ebrus 0:6bc4ac881c8e 220 {
ebrus 0:6bc4ac881c8e 221 UartHandle.Instance = (USART_TypeDef *)name;
ebrus 0:6bc4ac881c8e 222 if (serial_irq_ids[id] != 0) {
ebrus 0:6bc4ac881c8e 223 if (__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_TC) != RESET) {
ebrus 0:6bc4ac881c8e 224 irq_handler(serial_irq_ids[id], TxIrq);
ebrus 0:6bc4ac881c8e 225 __HAL_UART_CLEAR_IT(&UartHandle, UART_FLAG_TC);
ebrus 0:6bc4ac881c8e 226 }
ebrus 0:6bc4ac881c8e 227 if (__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_RXNE) != RESET) {
ebrus 0:6bc4ac881c8e 228 irq_handler(serial_irq_ids[id], RxIrq);
ebrus 0:6bc4ac881c8e 229 volatile uint32_t tmpval = UartHandle.Instance->RDR; // Clear RXNE bit
ebrus 0:6bc4ac881c8e 230 }
ebrus 0:6bc4ac881c8e 231 }
ebrus 0:6bc4ac881c8e 232 }
ebrus 0:6bc4ac881c8e 233
ebrus 0:6bc4ac881c8e 234 static void uart1_irq(void)
ebrus 0:6bc4ac881c8e 235 {
ebrus 0:6bc4ac881c8e 236 uart_irq(UART_1, 0);
ebrus 0:6bc4ac881c8e 237 }
ebrus 0:6bc4ac881c8e 238
ebrus 0:6bc4ac881c8e 239 static void uart2_irq(void)
ebrus 0:6bc4ac881c8e 240 {
ebrus 0:6bc4ac881c8e 241 uart_irq(UART_2, 1);
ebrus 0:6bc4ac881c8e 242 }
ebrus 0:6bc4ac881c8e 243
ebrus 0:6bc4ac881c8e 244 static void uart3_irq(void)
ebrus 0:6bc4ac881c8e 245 {
ebrus 0:6bc4ac881c8e 246 uart_irq(UART_3, 2);
ebrus 0:6bc4ac881c8e 247 }
ebrus 0:6bc4ac881c8e 248
ebrus 0:6bc4ac881c8e 249 void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id)
ebrus 0:6bc4ac881c8e 250 {
ebrus 0:6bc4ac881c8e 251 irq_handler = handler;
ebrus 0:6bc4ac881c8e 252 serial_irq_ids[obj->index] = id;
ebrus 0:6bc4ac881c8e 253 }
ebrus 0:6bc4ac881c8e 254
ebrus 0:6bc4ac881c8e 255 void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
ebrus 0:6bc4ac881c8e 256 {
ebrus 0:6bc4ac881c8e 257 IRQn_Type irq_n = (IRQn_Type)0;
ebrus 0:6bc4ac881c8e 258 uint32_t vector = 0;
ebrus 0:6bc4ac881c8e 259
ebrus 0:6bc4ac881c8e 260 UartHandle.Instance = (USART_TypeDef *)(obj->uart);
ebrus 0:6bc4ac881c8e 261
ebrus 0:6bc4ac881c8e 262 if (obj->uart == UART_1) {
ebrus 0:6bc4ac881c8e 263 irq_n = USART1_IRQn;
ebrus 0:6bc4ac881c8e 264 vector = (uint32_t)&uart1_irq;
ebrus 0:6bc4ac881c8e 265 }
ebrus 0:6bc4ac881c8e 266
ebrus 0:6bc4ac881c8e 267 if (obj->uart == UART_2) {
ebrus 0:6bc4ac881c8e 268 irq_n = USART2_IRQn;
ebrus 0:6bc4ac881c8e 269 vector = (uint32_t)&uart2_irq;
ebrus 0:6bc4ac881c8e 270 }
ebrus 0:6bc4ac881c8e 271
ebrus 0:6bc4ac881c8e 272 if (obj->uart == UART_3) {
ebrus 0:6bc4ac881c8e 273 irq_n = USART3_IRQn;
ebrus 0:6bc4ac881c8e 274 vector = (uint32_t)&uart3_irq;
ebrus 0:6bc4ac881c8e 275 }
ebrus 0:6bc4ac881c8e 276
ebrus 0:6bc4ac881c8e 277 if (enable) {
ebrus 0:6bc4ac881c8e 278
ebrus 0:6bc4ac881c8e 279 if (irq == RxIrq) {
ebrus 0:6bc4ac881c8e 280 __HAL_UART_ENABLE_IT(&UartHandle, UART_IT_RXNE);
ebrus 0:6bc4ac881c8e 281 } else { // TxIrq
ebrus 0:6bc4ac881c8e 282 __HAL_UART_ENABLE_IT(&UartHandle, UART_IT_TC);
ebrus 0:6bc4ac881c8e 283 }
ebrus 0:6bc4ac881c8e 284
ebrus 0:6bc4ac881c8e 285 NVIC_SetVector(irq_n, vector);
ebrus 0:6bc4ac881c8e 286 NVIC_EnableIRQ(irq_n);
ebrus 0:6bc4ac881c8e 287
ebrus 0:6bc4ac881c8e 288 } else { // disable
ebrus 0:6bc4ac881c8e 289
ebrus 0:6bc4ac881c8e 290 int all_disabled = 0;
ebrus 0:6bc4ac881c8e 291
ebrus 0:6bc4ac881c8e 292 if (irq == RxIrq) {
ebrus 0:6bc4ac881c8e 293 __HAL_UART_DISABLE_IT(&UartHandle, UART_IT_RXNE);
ebrus 0:6bc4ac881c8e 294 // Check if TxIrq is disabled too
ebrus 0:6bc4ac881c8e 295 if ((UartHandle.Instance->CR1 & USART_CR1_TCIE) == 0) all_disabled = 1;
ebrus 0:6bc4ac881c8e 296 } else { // TxIrq
ebrus 0:6bc4ac881c8e 297 __HAL_UART_DISABLE_IT(&UartHandle, UART_IT_TC);
ebrus 0:6bc4ac881c8e 298 // Check if RxIrq is disabled too
ebrus 0:6bc4ac881c8e 299 if ((UartHandle.Instance->CR1 & USART_CR1_RXNEIE) == 0) all_disabled = 1;
ebrus 0:6bc4ac881c8e 300 }
ebrus 0:6bc4ac881c8e 301
ebrus 0:6bc4ac881c8e 302 if (all_disabled) NVIC_DisableIRQ(irq_n);
ebrus 0:6bc4ac881c8e 303
ebrus 0:6bc4ac881c8e 304 }
ebrus 0:6bc4ac881c8e 305 }
ebrus 0:6bc4ac881c8e 306
ebrus 0:6bc4ac881c8e 307 /******************************************************************************
ebrus 0:6bc4ac881c8e 308 * READ/WRITE
ebrus 0:6bc4ac881c8e 309 ******************************************************************************/
ebrus 0:6bc4ac881c8e 310
ebrus 0:6bc4ac881c8e 311 int serial_getc(serial_t *obj)
ebrus 0:6bc4ac881c8e 312 {
ebrus 0:6bc4ac881c8e 313 USART_TypeDef *uart = (USART_TypeDef *)(obj->uart);
ebrus 0:6bc4ac881c8e 314 while (!serial_readable(obj));
ebrus 0:6bc4ac881c8e 315 if (obj->databits == UART_WORDLENGTH_8B) {
ebrus 0:6bc4ac881c8e 316 return (int)(uart->RDR & (uint8_t)0xFF);
ebrus 0:6bc4ac881c8e 317 } else {
ebrus 0:6bc4ac881c8e 318 return (int)(uart->RDR & (uint16_t)0x1FF);
ebrus 0:6bc4ac881c8e 319 }
ebrus 0:6bc4ac881c8e 320 }
ebrus 0:6bc4ac881c8e 321
ebrus 0:6bc4ac881c8e 322 void serial_putc(serial_t *obj, int c)
ebrus 0:6bc4ac881c8e 323 {
ebrus 0:6bc4ac881c8e 324 USART_TypeDef *uart = (USART_TypeDef *)(obj->uart);
ebrus 0:6bc4ac881c8e 325 while (!serial_writable(obj));
ebrus 0:6bc4ac881c8e 326 if (obj->databits == UART_WORDLENGTH_8B) {
ebrus 0:6bc4ac881c8e 327 uart->TDR = (uint8_t)(c & (uint8_t)0xFF);
ebrus 0:6bc4ac881c8e 328 } else {
ebrus 0:6bc4ac881c8e 329 uart->TDR = (uint16_t)(c & (uint16_t)0x1FF);
ebrus 0:6bc4ac881c8e 330 }
ebrus 0:6bc4ac881c8e 331 }
ebrus 0:6bc4ac881c8e 332
ebrus 0:6bc4ac881c8e 333 int serial_readable(serial_t *obj)
ebrus 0:6bc4ac881c8e 334 {
ebrus 0:6bc4ac881c8e 335 int status;
ebrus 0:6bc4ac881c8e 336 UartHandle.Instance = (USART_TypeDef *)(obj->uart);
ebrus 0:6bc4ac881c8e 337 // Check if data is received
ebrus 0:6bc4ac881c8e 338 status = ((__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_RXNE) != RESET) ? 1 : 0);
ebrus 0:6bc4ac881c8e 339 return status;
ebrus 0:6bc4ac881c8e 340 }
ebrus 0:6bc4ac881c8e 341
ebrus 0:6bc4ac881c8e 342 int serial_writable(serial_t *obj)
ebrus 0:6bc4ac881c8e 343 {
ebrus 0:6bc4ac881c8e 344 int status;
ebrus 0:6bc4ac881c8e 345 UartHandle.Instance = (USART_TypeDef *)(obj->uart);
ebrus 0:6bc4ac881c8e 346 // Check if data is transmitted
ebrus 0:6bc4ac881c8e 347 status = ((__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_TXE) != RESET) ? 1 : 0);
ebrus 0:6bc4ac881c8e 348 return status;
ebrus 0:6bc4ac881c8e 349 }
ebrus 0:6bc4ac881c8e 350
ebrus 0:6bc4ac881c8e 351 void serial_clear(serial_t *obj)
ebrus 0:6bc4ac881c8e 352 {
ebrus 0:6bc4ac881c8e 353 UartHandle.Instance = (USART_TypeDef *)(obj->uart);
ebrus 0:6bc4ac881c8e 354 __HAL_UART_CLEAR_IT(&UartHandle, UART_FLAG_TC);
ebrus 0:6bc4ac881c8e 355 __HAL_UART_SEND_REQ(&UartHandle, UART_RXDATA_FLUSH_REQUEST);
ebrus 0:6bc4ac881c8e 356 }
ebrus 0:6bc4ac881c8e 357
ebrus 0:6bc4ac881c8e 358 void serial_pinout_tx(PinName tx)
ebrus 0:6bc4ac881c8e 359 {
ebrus 0:6bc4ac881c8e 360 pinmap_pinout(tx, PinMap_UART_TX);
ebrus 0:6bc4ac881c8e 361 }
ebrus 0:6bc4ac881c8e 362
ebrus 0:6bc4ac881c8e 363 void serial_break_set(serial_t *obj)
ebrus 0:6bc4ac881c8e 364 {
ebrus 0:6bc4ac881c8e 365 UartHandle.Instance = (USART_TypeDef *)(obj->uart);
ebrus 0:6bc4ac881c8e 366 HAL_LIN_SendBreak(&UartHandle);
ebrus 0:6bc4ac881c8e 367 }
ebrus 0:6bc4ac881c8e 368
ebrus 0:6bc4ac881c8e 369 void serial_break_clear(serial_t *obj)
ebrus 0:6bc4ac881c8e 370 {
ebrus 0:6bc4ac881c8e 371 }
ebrus 0:6bc4ac881c8e 372
ebrus 0:6bc4ac881c8e 373 #endif