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