Jan Wolfsberger / mbed-src-no-hal

Fork of mbed-src by Reinhold Schaefer

Committer:
mbed_official
Date:
Mon Oct 13 08:00:07 2014 +0100
Revision:
345:ed24f9926218
Parent:
341:28d1f895c6fe
Child:
429:4ddf7f7eabbb
Synchronized with git revision 342790b0c3be997d6ed3744962729c4d9c6a9a39

Full URL: https://github.com/mbedmicro/mbed/commit/342790b0c3be997d6ed3744962729c4d9c6a9a39/

HAL: Disco_F429ZI - extensions of interrupt vectors, pin names and pin functions

Who changed what in which revision?

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