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