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