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