mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Tue May 27 08:30:06 2014 +0100
Revision:
215:83cf97a28428
Parent:
174:8bb9f3a33240
Child:
227:7bd0639b8911
Synchronized with git revision a8b7f5df2cb5d7a76fda549a15b91fdaba6e2986

Full URL: https://github.com/mbedmicro/mbed/commit/a8b7f5df2cb5d7a76fda549a15b91fdaba6e2986/

[KLXX] Added RPC + RTOS support (KL05Z)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 76:aeb1df146756 1 /* mbed Microcontroller Library
mbed_official 76:aeb1df146756 2 *******************************************************************************
mbed_official 76:aeb1df146756 3 * Copyright (c) 2014, STMicroelectronics
mbed_official 76:aeb1df146756 4 * All rights reserved.
mbed_official 76:aeb1df146756 5 *
mbed_official 76:aeb1df146756 6 * Redistribution and use in source and binary forms, with or without
mbed_official 76:aeb1df146756 7 * modification, are permitted provided that the following conditions are met:
mbed_official 76:aeb1df146756 8 *
mbed_official 76:aeb1df146756 9 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 76:aeb1df146756 10 * this list of conditions and the following disclaimer.
mbed_official 76:aeb1df146756 11 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 76:aeb1df146756 12 * this list of conditions and the following disclaimer in the documentation
mbed_official 76:aeb1df146756 13 * and/or other materials provided with the distribution.
mbed_official 76:aeb1df146756 14 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 76:aeb1df146756 15 * may be used to endorse or promote products derived from this software
mbed_official 76:aeb1df146756 16 * without specific prior written permission.
mbed_official 76:aeb1df146756 17 *
mbed_official 76:aeb1df146756 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 76:aeb1df146756 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 76:aeb1df146756 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 76:aeb1df146756 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 76:aeb1df146756 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 76:aeb1df146756 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 76:aeb1df146756 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 76:aeb1df146756 25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 76:aeb1df146756 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 76:aeb1df146756 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 76:aeb1df146756 28 *******************************************************************************
mbed_official 76:aeb1df146756 29 */
mbed_official 76:aeb1df146756 30 #include "serial_api.h"
mbed_official 174:8bb9f3a33240 31
mbed_official 174:8bb9f3a33240 32 #if DEVICE_SERIAL
mbed_official 174:8bb9f3a33240 33
mbed_official 76:aeb1df146756 34 #include "cmsis.h"
mbed_official 76:aeb1df146756 35 #include "pinmap.h"
mbed_official 76:aeb1df146756 36 #include "error.h"
mbed_official 76:aeb1df146756 37 #include <string.h>
mbed_official 76:aeb1df146756 38
mbed_official 76:aeb1df146756 39 static const PinMap PinMap_UART_TX[] = {
mbed_official 129:0182c99221bc 40 {PA_2, UART_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_USART2)},
mbed_official 76:aeb1df146756 41 {PA_9, UART_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_USART1)},
mbed_official 118:b44c45162f28 42 {PB_6, UART_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_USART1)},
mbed_official 118:b44c45162f28 43 {PB_10, UART_3, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_USART3)},
mbed_official 174:8bb9f3a33240 44 // {PC_10, UART_3, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_USART3)},
mbed_official 129:0182c99221bc 45 {PC_10, UART_4, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_UART4)},
mbed_official 129:0182c99221bc 46 {PC_12, UART_5, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_UART5)},
mbed_official 76:aeb1df146756 47 {NC, NC, 0}
mbed_official 76:aeb1df146756 48 };
mbed_official 76:aeb1df146756 49
mbed_official 76:aeb1df146756 50 static const PinMap PinMap_UART_RX[] = {
mbed_official 129:0182c99221bc 51 {PA_3, UART_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_USART2)},
mbed_official 76:aeb1df146756 52 {PA_10, UART_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_USART1)},
mbed_official 129:0182c99221bc 53 {PB_7, UART_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_USART1)},
mbed_official 118:b44c45162f28 54 {PB_11, UART_3, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_USART3)},
mbed_official 174:8bb9f3a33240 55 // {PC_11, UART_3, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_USART3)},
mbed_official 118:b44c45162f28 56 {PC_11, UART_4, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_UART4)},
mbed_official 129:0182c99221bc 57 {PD_2, UART_5, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_UART5)},
mbed_official 76:aeb1df146756 58 {NC, NC, 0}
mbed_official 76:aeb1df146756 59 };
mbed_official 76:aeb1df146756 60
mbed_official 118:b44c45162f28 61 #define UART_NUM (5)
mbed_official 76:aeb1df146756 62
mbed_official 76:aeb1df146756 63 static uint32_t serial_irq_ids[UART_NUM] = {0};
mbed_official 76:aeb1df146756 64
mbed_official 76:aeb1df146756 65 static uart_irq_handler irq_handler;
mbed_official 76:aeb1df146756 66
mbed_official 76:aeb1df146756 67 int stdio_uart_inited = 0;
mbed_official 76:aeb1df146756 68 serial_t stdio_uart;
mbed_official 76:aeb1df146756 69
mbed_official 76:aeb1df146756 70 static void init_usart(serial_t *obj) {
mbed_official 76:aeb1df146756 71 USART_TypeDef *usart = (USART_TypeDef *)(obj->uart);
mbed_official 76:aeb1df146756 72 USART_InitTypeDef USART_InitStructure;
mbed_official 174:8bb9f3a33240 73
mbed_official 76:aeb1df146756 74 USART_Cmd(usart, DISABLE);
mbed_official 76:aeb1df146756 75
mbed_official 129:0182c99221bc 76 USART_InitStructure.USART_BaudRate = obj->baudrate;
mbed_official 129:0182c99221bc 77 USART_InitStructure.USART_WordLength = obj->databits;
mbed_official 129:0182c99221bc 78 USART_InitStructure.USART_StopBits = obj->stopbits;
mbed_official 129:0182c99221bc 79 USART_InitStructure.USART_Parity = obj->parity;
mbed_official 76:aeb1df146756 80 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
mbed_official 129:0182c99221bc 81 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
mbed_official 76:aeb1df146756 82 USART_Init(usart, &USART_InitStructure);
mbed_official 174:8bb9f3a33240 83
mbed_official 76:aeb1df146756 84 USART_Cmd(usart, ENABLE);
mbed_official 76:aeb1df146756 85 }
mbed_official 76:aeb1df146756 86
mbed_official 174:8bb9f3a33240 87 void serial_init(serial_t *obj, PinName tx, PinName rx) {
mbed_official 76:aeb1df146756 88 // Determine the UART to use (UART_1, UART_2, ...)
mbed_official 76:aeb1df146756 89 UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
mbed_official 76:aeb1df146756 90 UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);
mbed_official 174:8bb9f3a33240 91
mbed_official 76:aeb1df146756 92 // Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object
mbed_official 76:aeb1df146756 93 obj->uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
mbed_official 76:aeb1df146756 94
mbed_official 76:aeb1df146756 95 if (obj->uart == (UARTName)NC) {
mbed_official 76:aeb1df146756 96 error("Serial pinout mapping failed");
mbed_official 76:aeb1df146756 97 }
mbed_official 76:aeb1df146756 98
mbed_official 76:aeb1df146756 99 // Enable USART clock
mbed_official 76:aeb1df146756 100 if (obj->uart == UART_1) {
mbed_official 174:8bb9f3a33240 101 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
mbed_official 215:83cf97a28428 102 obj->index = 0;
mbed_official 76:aeb1df146756 103 }
mbed_official 76:aeb1df146756 104 if (obj->uart == UART_2) {
mbed_official 174:8bb9f3a33240 105 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
mbed_official 215:83cf97a28428 106 obj->index = 1;
mbed_official 76:aeb1df146756 107 }
mbed_official 129:0182c99221bc 108 if (obj->uart == UART_3) {
mbed_official 174:8bb9f3a33240 109 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
mbed_official 215:83cf97a28428 110 obj->index = 2;
mbed_official 118:b44c45162f28 111 }
mbed_official 129:0182c99221bc 112 if (obj->uart == UART_4) {
mbed_official 174:8bb9f3a33240 113 RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);
mbed_official 215:83cf97a28428 114 obj->index = 3;
mbed_official 118:b44c45162f28 115 }
mbed_official 129:0182c99221bc 116 if (obj->uart == UART_5) {
mbed_official 174:8bb9f3a33240 117 RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE);
mbed_official 215:83cf97a28428 118 obj->index = 4;
mbed_official 118:b44c45162f28 119 }
mbed_official 129:0182c99221bc 120
mbed_official 76:aeb1df146756 121 // Configure the UART pins
mbed_official 76:aeb1df146756 122 pinmap_pinout(tx, PinMap_UART_TX);
mbed_official 76:aeb1df146756 123 pinmap_pinout(rx, PinMap_UART_RX);
mbed_official 76:aeb1df146756 124 pin_mode(tx, PullUp);
mbed_official 76:aeb1df146756 125 pin_mode(rx, PullUp);
mbed_official 76:aeb1df146756 126
mbed_official 76:aeb1df146756 127 // Configure UART
mbed_official 76:aeb1df146756 128 obj->baudrate = 9600;
mbed_official 76:aeb1df146756 129 obj->databits = USART_WordLength_8b;
mbed_official 76:aeb1df146756 130 obj->stopbits = USART_StopBits_1;
mbed_official 174:8bb9f3a33240 131 obj->parity = USART_Parity_No;
mbed_official 76:aeb1df146756 132
mbed_official 215:83cf97a28428 133 obj->pin_tx = tx;
mbed_official 215:83cf97a28428 134 obj->pin_rx = rx;
mbed_official 76:aeb1df146756 135
mbed_official 215:83cf97a28428 136 init_usart(obj);
mbed_official 174:8bb9f3a33240 137
mbed_official 76:aeb1df146756 138 // For stdio management
mbed_official 76:aeb1df146756 139 if (obj->uart == STDIO_UART) {
mbed_official 76:aeb1df146756 140 stdio_uart_inited = 1;
mbed_official 76:aeb1df146756 141 memcpy(&stdio_uart, obj, sizeof(serial_t));
mbed_official 174:8bb9f3a33240 142 }
mbed_official 76:aeb1df146756 143 }
mbed_official 76:aeb1df146756 144
mbed_official 76:aeb1df146756 145 void serial_free(serial_t *obj) {
mbed_official 215:83cf97a28428 146 // Reset UART and disable clock
mbed_official 215:83cf97a28428 147 if (obj->uart == UART_1) {
mbed_official 215:83cf97a28428 148 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);
mbed_official 215:83cf97a28428 149 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE);
mbed_official 215:83cf97a28428 150 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, DISABLE);
mbed_official 215:83cf97a28428 151 }
mbed_official 215:83cf97a28428 152 if (obj->uart == UART_2) {
mbed_official 215:83cf97a28428 153 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, ENABLE);
mbed_official 215:83cf97a28428 154 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, DISABLE);
mbed_official 215:83cf97a28428 155 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, DISABLE);
mbed_official 215:83cf97a28428 156 }
mbed_official 215:83cf97a28428 157 if (obj->uart == UART_3) {
mbed_official 215:83cf97a28428 158 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, ENABLE);
mbed_official 215:83cf97a28428 159 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, DISABLE);
mbed_official 215:83cf97a28428 160 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, DISABLE);
mbed_official 215:83cf97a28428 161 }
mbed_official 215:83cf97a28428 162 if (obj->uart == UART_4) {
mbed_official 215:83cf97a28428 163 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, ENABLE);
mbed_official 215:83cf97a28428 164 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, DISABLE);
mbed_official 215:83cf97a28428 165 RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, DISABLE);
mbed_official 215:83cf97a28428 166 }
mbed_official 215:83cf97a28428 167 if (obj->uart == UART_5) {
mbed_official 215:83cf97a28428 168 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, ENABLE);
mbed_official 215:83cf97a28428 169 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, DISABLE);
mbed_official 215:83cf97a28428 170 RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, DISABLE);
mbed_official 215:83cf97a28428 171 }
mbed_official 215:83cf97a28428 172
mbed_official 215:83cf97a28428 173 // Configure GPIOs
mbed_official 215:83cf97a28428 174 pin_function(obj->pin_tx, STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF));
mbed_official 215:83cf97a28428 175 pin_function(obj->pin_rx, STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF));
mbed_official 215:83cf97a28428 176
mbed_official 76:aeb1df146756 177 serial_irq_ids[obj->index] = 0;
mbed_official 76:aeb1df146756 178 }
mbed_official 76:aeb1df146756 179
mbed_official 76:aeb1df146756 180 void serial_baud(serial_t *obj, int baudrate) {
mbed_official 76:aeb1df146756 181 obj->baudrate = baudrate;
mbed_official 76:aeb1df146756 182 init_usart(obj);
mbed_official 76:aeb1df146756 183 }
mbed_official 76:aeb1df146756 184
mbed_official 76:aeb1df146756 185 void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) {
mbed_official 76:aeb1df146756 186 if (data_bits == 8) {
mbed_official 76:aeb1df146756 187 obj->databits = USART_WordLength_8b;
mbed_official 174:8bb9f3a33240 188 } else {
mbed_official 76:aeb1df146756 189 obj->databits = USART_WordLength_9b;
mbed_official 76:aeb1df146756 190 }
mbed_official 76:aeb1df146756 191
mbed_official 76:aeb1df146756 192 switch (parity) {
mbed_official 174:8bb9f3a33240 193 case ParityOdd:
mbed_official 174:8bb9f3a33240 194 case ParityForced0:
mbed_official 174:8bb9f3a33240 195 obj->parity = USART_Parity_Odd;
mbed_official 174:8bb9f3a33240 196 break;
mbed_official 174:8bb9f3a33240 197 case ParityEven:
mbed_official 174:8bb9f3a33240 198 case ParityForced1:
mbed_official 174:8bb9f3a33240 199 obj->parity = USART_Parity_Even;
mbed_official 174:8bb9f3a33240 200 break;
mbed_official 174:8bb9f3a33240 201 default: // ParityNone
mbed_official 174:8bb9f3a33240 202 obj->parity = USART_Parity_No;
mbed_official 174:8bb9f3a33240 203 break;
mbed_official 76:aeb1df146756 204 }
mbed_official 174:8bb9f3a33240 205
mbed_official 76:aeb1df146756 206 if (stop_bits == 2) {
mbed_official 76:aeb1df146756 207 obj->stopbits = USART_StopBits_2;
mbed_official 174:8bb9f3a33240 208 } else {
mbed_official 76:aeb1df146756 209 obj->stopbits = USART_StopBits_1;
mbed_official 76:aeb1df146756 210 }
mbed_official 76:aeb1df146756 211
mbed_official 76:aeb1df146756 212 init_usart(obj);
mbed_official 76:aeb1df146756 213 }
mbed_official 76:aeb1df146756 214
mbed_official 76:aeb1df146756 215 /******************************************************************************
mbed_official 76:aeb1df146756 216 * INTERRUPTS HANDLING
mbed_official 76:aeb1df146756 217 ******************************************************************************/
mbed_official 76:aeb1df146756 218
mbed_official 76:aeb1df146756 219 // not api
mbed_official 76:aeb1df146756 220 static void uart_irq(USART_TypeDef* usart, int id) {
mbed_official 76:aeb1df146756 221 if (serial_irq_ids[id] != 0) {
mbed_official 76:aeb1df146756 222 if (USART_GetITStatus(usart, USART_IT_TC) != RESET) {
mbed_official 76:aeb1df146756 223 irq_handler(serial_irq_ids[id], TxIrq);
mbed_official 76:aeb1df146756 224 USART_ClearITPendingBit(usart, USART_IT_TC);
mbed_official 76:aeb1df146756 225 }
mbed_official 76:aeb1df146756 226 if (USART_GetITStatus(usart, USART_IT_RXNE) != RESET) {
mbed_official 76:aeb1df146756 227 irq_handler(serial_irq_ids[id], RxIrq);
mbed_official 76:aeb1df146756 228 USART_ClearITPendingBit(usart, USART_IT_RXNE);
mbed_official 76:aeb1df146756 229 }
mbed_official 76:aeb1df146756 230 }
mbed_official 76:aeb1df146756 231 }
mbed_official 76:aeb1df146756 232
mbed_official 174:8bb9f3a33240 233 static void uart1_irq(void) {
mbed_official 174:8bb9f3a33240 234 uart_irq((USART_TypeDef*)UART_1, 0);
mbed_official 174:8bb9f3a33240 235 }
mbed_official 174:8bb9f3a33240 236 static void uart2_irq(void) {
mbed_official 174:8bb9f3a33240 237 uart_irq((USART_TypeDef*)UART_2, 1);
mbed_official 174:8bb9f3a33240 238 }
mbed_official 174:8bb9f3a33240 239 static void uart3_irq(void) {
mbed_official 174:8bb9f3a33240 240 uart_irq((USART_TypeDef*)UART_3, 2);
mbed_official 174:8bb9f3a33240 241 }
mbed_official 174:8bb9f3a33240 242 static void uart4_irq(void) {
mbed_official 174:8bb9f3a33240 243 uart_irq((USART_TypeDef*)UART_4, 3);
mbed_official 174:8bb9f3a33240 244 }
mbed_official 174:8bb9f3a33240 245 static void uart5_irq(void) {
mbed_official 174:8bb9f3a33240 246 uart_irq((USART_TypeDef*)UART_5, 4);
mbed_official 174:8bb9f3a33240 247 }
mbed_official 76:aeb1df146756 248
mbed_official 76:aeb1df146756 249 void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id) {
mbed_official 76:aeb1df146756 250 irq_handler = handler;
mbed_official 76:aeb1df146756 251 serial_irq_ids[obj->index] = id;
mbed_official 76:aeb1df146756 252 }
mbed_official 76:aeb1df146756 253
mbed_official 76:aeb1df146756 254 void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) {
mbed_official 76:aeb1df146756 255 IRQn_Type irq_n = (IRQn_Type)0;
mbed_official 76:aeb1df146756 256 uint32_t vector = 0;
mbed_official 76:aeb1df146756 257 USART_TypeDef *usart = (USART_TypeDef *)(obj->uart);
mbed_official 76:aeb1df146756 258
mbed_official 76:aeb1df146756 259 if (obj->uart == UART_1) {
mbed_official 174:8bb9f3a33240 260 irq_n = USART1_IRQn;
mbed_official 174:8bb9f3a33240 261 vector = (uint32_t)&uart1_irq;
mbed_official 76:aeb1df146756 262 }
mbed_official 174:8bb9f3a33240 263
mbed_official 76:aeb1df146756 264 if (obj->uart == UART_2) {
mbed_official 174:8bb9f3a33240 265 irq_n = USART2_IRQn;
mbed_official 174:8bb9f3a33240 266 vector = (uint32_t)&uart2_irq;
mbed_official 76:aeb1df146756 267 }
mbed_official 129:0182c99221bc 268
mbed_official 118:b44c45162f28 269 if (obj->uart == UART_3) {
mbed_official 174:8bb9f3a33240 270 irq_n = USART3_IRQn;
mbed_official 174:8bb9f3a33240 271 vector = (uint32_t)&uart3_irq;
mbed_official 118:b44c45162f28 272 }
mbed_official 129:0182c99221bc 273
mbed_official 118:b44c45162f28 274 if (obj->uart == UART_4) {
mbed_official 174:8bb9f3a33240 275 irq_n = UART4_IRQn;
mbed_official 174:8bb9f3a33240 276 vector = (uint32_t)&uart4_irq;
mbed_official 118:b44c45162f28 277 }
mbed_official 129:0182c99221bc 278
mbed_official 118:b44c45162f28 279 if (obj->uart == UART_5) {
mbed_official 174:8bb9f3a33240 280 irq_n = UART5_IRQn;
mbed_official 174:8bb9f3a33240 281 vector = (uint32_t)&uart5_irq;
mbed_official 118:b44c45162f28 282 }
mbed_official 174:8bb9f3a33240 283
mbed_official 76:aeb1df146756 284 if (enable) {
mbed_official 174:8bb9f3a33240 285
mbed_official 76:aeb1df146756 286 if (irq == RxIrq) {
mbed_official 76:aeb1df146756 287 USART_ITConfig(usart, USART_IT_RXNE, ENABLE);
mbed_official 174:8bb9f3a33240 288 } else { // TxIrq
mbed_official 76:aeb1df146756 289 USART_ITConfig(usart, USART_IT_TC, ENABLE);
mbed_official 174:8bb9f3a33240 290 }
mbed_official 174:8bb9f3a33240 291
mbed_official 76:aeb1df146756 292 NVIC_SetVector(irq_n, vector);
mbed_official 76:aeb1df146756 293 NVIC_EnableIRQ(irq_n);
mbed_official 174:8bb9f3a33240 294
mbed_official 76:aeb1df146756 295 } else { // disable
mbed_official 174:8bb9f3a33240 296
mbed_official 76:aeb1df146756 297 int all_disabled = 0;
mbed_official 174:8bb9f3a33240 298
mbed_official 76:aeb1df146756 299 if (irq == RxIrq) {
mbed_official 76:aeb1df146756 300 USART_ITConfig(usart, USART_IT_RXNE, DISABLE);
mbed_official 76:aeb1df146756 301 // Check if TxIrq is disabled too
mbed_official 76:aeb1df146756 302 if ((usart->CR1 & USART_CR1_TXEIE) == 0) all_disabled = 1;
mbed_official 174:8bb9f3a33240 303 } else { // TxIrq
mbed_official 76:aeb1df146756 304 USART_ITConfig(usart, USART_IT_TXE, DISABLE);
mbed_official 76:aeb1df146756 305 // Check if RxIrq is disabled too
mbed_official 174:8bb9f3a33240 306 if ((usart->CR1 & USART_CR1_RXNEIE) == 0) all_disabled = 1;
mbed_official 76:aeb1df146756 307 }
mbed_official 174:8bb9f3a33240 308
mbed_official 76:aeb1df146756 309 if (all_disabled) NVIC_DisableIRQ(irq_n);
mbed_official 174:8bb9f3a33240 310
mbed_official 174:8bb9f3a33240 311 }
mbed_official 76:aeb1df146756 312 }
mbed_official 76:aeb1df146756 313
mbed_official 76:aeb1df146756 314 /******************************************************************************
mbed_official 76:aeb1df146756 315 * READ/WRITE
mbed_official 76:aeb1df146756 316 ******************************************************************************/
mbed_official 76:aeb1df146756 317
mbed_official 76:aeb1df146756 318 int serial_getc(serial_t *obj) {
mbed_official 76:aeb1df146756 319 USART_TypeDef *usart = (USART_TypeDef *)(obj->uart);
mbed_official 76:aeb1df146756 320 while (!serial_readable(obj));
mbed_official 76:aeb1df146756 321 return (int)(USART_ReceiveData(usart));
mbed_official 76:aeb1df146756 322 }
mbed_official 76:aeb1df146756 323
mbed_official 76:aeb1df146756 324 void serial_putc(serial_t *obj, int c) {
mbed_official 76:aeb1df146756 325 USART_TypeDef *usart = (USART_TypeDef *)(obj->uart);
mbed_official 76:aeb1df146756 326 while (!serial_writable(obj));
mbed_official 76:aeb1df146756 327 USART_SendData(usart, (uint16_t)c);
mbed_official 76:aeb1df146756 328 }
mbed_official 76:aeb1df146756 329
mbed_official 76:aeb1df146756 330 int serial_readable(serial_t *obj) {
mbed_official 76:aeb1df146756 331 int status;
mbed_official 76:aeb1df146756 332 USART_TypeDef *usart = (USART_TypeDef *)(obj->uart);
mbed_official 76:aeb1df146756 333 // Check if data is received
mbed_official 76:aeb1df146756 334 status = ((USART_GetFlagStatus(usart, USART_FLAG_RXNE) != RESET) ? 1 : 0);
mbed_official 76:aeb1df146756 335 return status;
mbed_official 76:aeb1df146756 336 }
mbed_official 76:aeb1df146756 337
mbed_official 76:aeb1df146756 338 int serial_writable(serial_t *obj) {
mbed_official 76:aeb1df146756 339 int status;
mbed_official 76:aeb1df146756 340 USART_TypeDef *usart = (USART_TypeDef *)(obj->uart);
mbed_official 76:aeb1df146756 341 // Check if data is transmitted
mbed_official 76:aeb1df146756 342 status = ((USART_GetFlagStatus(usart, USART_FLAG_TXE) != RESET) ? 1 : 0);
mbed_official 76:aeb1df146756 343 return status;
mbed_official 76:aeb1df146756 344 }
mbed_official 76:aeb1df146756 345
mbed_official 76:aeb1df146756 346 void serial_clear(serial_t *obj) {
mbed_official 76:aeb1df146756 347 USART_TypeDef *usart = (USART_TypeDef *)(obj->uart);
mbed_official 76:aeb1df146756 348 USART_ClearFlag(usart, USART_FLAG_TXE);
mbed_official 76:aeb1df146756 349 USART_ClearFlag(usart, USART_FLAG_RXNE);
mbed_official 76:aeb1df146756 350 }
mbed_official 76:aeb1df146756 351
mbed_official 76:aeb1df146756 352 void serial_pinout_tx(PinName tx) {
mbed_official 76:aeb1df146756 353 pinmap_pinout(tx, PinMap_UART_TX);
mbed_official 76:aeb1df146756 354 }
mbed_official 76:aeb1df146756 355
mbed_official 76:aeb1df146756 356 void serial_break_set(serial_t *obj) {
mbed_official 76:aeb1df146756 357 USART_TypeDef *usart = (USART_TypeDef *)(obj->uart);
mbed_official 76:aeb1df146756 358 USART_SendBreak(usart);
mbed_official 76:aeb1df146756 359 }
mbed_official 76:aeb1df146756 360
mbed_official 76:aeb1df146756 361 void serial_break_clear(serial_t *obj) {
mbed_official 76:aeb1df146756 362 }
mbed_official 174:8bb9f3a33240 363
mbed_official 174:8bb9f3a33240 364 #endif