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