mbed library sources. Supersedes mbed-src. Edited target satm32f446 for user USART3 pins

Dependents:   IGLOO_board

Fork of mbed-dev by mbed official

Committer:
ua1arn
Date:
Mon Jul 30 12:31:10 2018 +0000
Revision:
188:3f10722804f9
Parent:
184:08ed48f1de7f
before add multi-configuration USB descriptors

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AnnaBridge 184:08ed48f1de7f 1 /* mbed Microcontroller Library
AnnaBridge 184:08ed48f1de7f 2 * (C)Copyright TOSHIBA ELECTRONIC DEVICES & STORAGE CORPORATION 2017 All rights reserved
AnnaBridge 184:08ed48f1de7f 3 *
AnnaBridge 184:08ed48f1de7f 4 * Licensed under the Apache License, Version 2.0 (the "License");
AnnaBridge 184:08ed48f1de7f 5 * you may not use this file except in compliance with the License.
AnnaBridge 184:08ed48f1de7f 6 * You may obtain a copy of the License at
AnnaBridge 184:08ed48f1de7f 7 *
AnnaBridge 184:08ed48f1de7f 8 * http://www.apache.org/licenses/LICENSE-2.0
AnnaBridge 184:08ed48f1de7f 9 *
AnnaBridge 184:08ed48f1de7f 10 * Unless required by applicable law or agreed to in writing, software
AnnaBridge 184:08ed48f1de7f 11 * distributed under the License is distributed on an "AS IS" BASIS,
AnnaBridge 184:08ed48f1de7f 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
AnnaBridge 184:08ed48f1de7f 13 * See the License for the specific language governing permissions and
AnnaBridge 184:08ed48f1de7f 14 * limitations under the License.
AnnaBridge 184:08ed48f1de7f 15 */
AnnaBridge 184:08ed48f1de7f 16 #include <string.h>
AnnaBridge 184:08ed48f1de7f 17 #include "serial_api.h"
AnnaBridge 184:08ed48f1de7f 18 #include "PeripheralNames.h"
AnnaBridge 184:08ed48f1de7f 19 #include "pinmap.h"
AnnaBridge 184:08ed48f1de7f 20 #include "tmpm46b_uart.h"
AnnaBridge 184:08ed48f1de7f 21 #include "tmpm46b_fuart.h"
AnnaBridge 184:08ed48f1de7f 22
AnnaBridge 184:08ed48f1de7f 23 #define UART_NUM 6
AnnaBridge 184:08ed48f1de7f 24 #define FUART_INT_BITS 0x07FF
AnnaBridge 184:08ed48f1de7f 25
AnnaBridge 184:08ed48f1de7f 26 static const PinMap PinMap_UART_TX[] = {
AnnaBridge 184:08ed48f1de7f 27 {PE2, SERIAL_0, PIN_DATA(1, 1)},
AnnaBridge 184:08ed48f1de7f 28 {PE5, SERIAL_1, PIN_DATA(1, 1)},
AnnaBridge 184:08ed48f1de7f 29 {PL2, SERIAL_2, PIN_DATA(5, 1)},
AnnaBridge 184:08ed48f1de7f 30 {PB0, SERIAL_3, PIN_DATA(3, 1)},
AnnaBridge 184:08ed48f1de7f 31 {PF1, SERIAL_4, PIN_DATA(3, 1)},
AnnaBridge 184:08ed48f1de7f 32 {PA6, SERIAL_5, PIN_DATA(2, 1)},
AnnaBridge 184:08ed48f1de7f 33 {NC, NC, 0}
AnnaBridge 184:08ed48f1de7f 34 };
AnnaBridge 184:08ed48f1de7f 35
AnnaBridge 184:08ed48f1de7f 36 static const PinMap PinMap_UART_RX[] = {
AnnaBridge 184:08ed48f1de7f 37 {PE1, SERIAL_0, PIN_DATA(1, 0)},
AnnaBridge 184:08ed48f1de7f 38 {PE6, SERIAL_1, PIN_DATA(1, 0)},
AnnaBridge 184:08ed48f1de7f 39 {PL1, SERIAL_2, PIN_DATA(5, 0)},
AnnaBridge 184:08ed48f1de7f 40 {PB1, SERIAL_3, PIN_DATA(3, 0)},
AnnaBridge 184:08ed48f1de7f 41 {PF2, SERIAL_4, PIN_DATA(3, 0)},
AnnaBridge 184:08ed48f1de7f 42 {PA5, SERIAL_5, PIN_DATA(2, 0)},
AnnaBridge 184:08ed48f1de7f 43 {NC, NC, 0}
AnnaBridge 184:08ed48f1de7f 44 };
AnnaBridge 184:08ed48f1de7f 45
AnnaBridge 184:08ed48f1de7f 46 static const PinMap PinMap_UART_CTS[] = {
AnnaBridge 184:08ed48f1de7f 47 {PE3, SERIAL_0, PIN_DATA(4, 0)},
AnnaBridge 184:08ed48f1de7f 48 {PE4, SERIAL_1, PIN_DATA(4, 0)},
AnnaBridge 184:08ed48f1de7f 49 {PL3, SERIAL_2, PIN_DATA(6, 0)},
AnnaBridge 184:08ed48f1de7f 50 {PA7, SERIAL_3, PIN_DATA(4, 0)},
AnnaBridge 184:08ed48f1de7f 51 {PF0, SERIAL_4, PIN_DATA(3, 0)},
AnnaBridge 184:08ed48f1de7f 52 {PA7, SERIAL_5, PIN_DATA(2, 0)},
AnnaBridge 184:08ed48f1de7f 53 {NC, NC, 0}
AnnaBridge 184:08ed48f1de7f 54 };
AnnaBridge 184:08ed48f1de7f 55
AnnaBridge 184:08ed48f1de7f 56 static const PinMap PinMap_UART_RTS[] = {
AnnaBridge 184:08ed48f1de7f 57 {PF3, SERIAL_4, PIN_DATA(3, 1)},
AnnaBridge 184:08ed48f1de7f 58 {PA4, SERIAL_5, PIN_DATA(2, 1)},
AnnaBridge 184:08ed48f1de7f 59 {NC, NC, 0}
AnnaBridge 184:08ed48f1de7f 60 };
AnnaBridge 184:08ed48f1de7f 61
AnnaBridge 184:08ed48f1de7f 62
AnnaBridge 184:08ed48f1de7f 63 static uint32_t serial_irq_ids[UART_NUM] = {0};
AnnaBridge 184:08ed48f1de7f 64 static uart_irq_handler irq_handler;
AnnaBridge 184:08ed48f1de7f 65 void serial_init_UART_configure(int uartname, serial_t *obj, PinName tx, PinName rx);
AnnaBridge 184:08ed48f1de7f 66
AnnaBridge 184:08ed48f1de7f 67 int stdio_uart_inited = 0;
AnnaBridge 184:08ed48f1de7f 68 serial_t stdio_uart;
AnnaBridge 184:08ed48f1de7f 69
AnnaBridge 184:08ed48f1de7f 70 void serial_init(serial_t *obj, PinName tx, PinName rx)
AnnaBridge 184:08ed48f1de7f 71 {
AnnaBridge 184:08ed48f1de7f 72 int is_stdio_uart = 0;
AnnaBridge 184:08ed48f1de7f 73
AnnaBridge 184:08ed48f1de7f 74 UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
AnnaBridge 184:08ed48f1de7f 75 UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);
AnnaBridge 184:08ed48f1de7f 76 UARTName uart_name = (UARTName)pinmap_merge(uart_tx, uart_rx);
AnnaBridge 184:08ed48f1de7f 77 MBED_ASSERT((int)uart_name != NC);
AnnaBridge 184:08ed48f1de7f 78
AnnaBridge 184:08ed48f1de7f 79 obj->index = uart_name;
AnnaBridge 184:08ed48f1de7f 80 // Initialize UART instance
AnnaBridge 184:08ed48f1de7f 81 switch (uart_name) {
AnnaBridge 184:08ed48f1de7f 82 case SERIAL_0:
AnnaBridge 184:08ed48f1de7f 83 obj->UARTx = UART0;
AnnaBridge 184:08ed48f1de7f 84 serial_init_UART_configure(SERIAL_0, obj, tx, rx);
AnnaBridge 184:08ed48f1de7f 85 break;
AnnaBridge 184:08ed48f1de7f 86 case SERIAL_1:
AnnaBridge 184:08ed48f1de7f 87 obj->UARTx = UART1;
AnnaBridge 184:08ed48f1de7f 88 serial_init_UART_configure(SERIAL_1, obj, tx, rx);
AnnaBridge 184:08ed48f1de7f 89 break;
AnnaBridge 184:08ed48f1de7f 90 case SERIAL_2:
AnnaBridge 184:08ed48f1de7f 91 obj->UARTx = UART2;
AnnaBridge 184:08ed48f1de7f 92 serial_init_UART_configure(SERIAL_2, obj, tx, rx);
AnnaBridge 184:08ed48f1de7f 93 break;
AnnaBridge 184:08ed48f1de7f 94 case SERIAL_3:
AnnaBridge 184:08ed48f1de7f 95 obj->UARTx = UART3;
AnnaBridge 184:08ed48f1de7f 96 serial_init_UART_configure(SERIAL_3, obj, tx, rx);
AnnaBridge 184:08ed48f1de7f 97 break;
AnnaBridge 184:08ed48f1de7f 98 case SERIAL_4:
AnnaBridge 184:08ed48f1de7f 99 obj->FUART = FUART0;
AnnaBridge 184:08ed48f1de7f 100 serial_init_UART_configure(SERIAL_4, obj, tx, rx);
AnnaBridge 184:08ed48f1de7f 101 break;
AnnaBridge 184:08ed48f1de7f 102 case SERIAL_5:
AnnaBridge 184:08ed48f1de7f 103 obj->FUART = FUART1;
AnnaBridge 184:08ed48f1de7f 104 serial_init_UART_configure(SERIAL_5, obj, tx, rx);
AnnaBridge 184:08ed48f1de7f 105 break;
AnnaBridge 184:08ed48f1de7f 106 default:
AnnaBridge 184:08ed48f1de7f 107 break;
AnnaBridge 184:08ed48f1de7f 108 }
AnnaBridge 184:08ed48f1de7f 109
AnnaBridge 184:08ed48f1de7f 110 is_stdio_uart = (uart_name == STDIO_UART) ? (1) : (0);
AnnaBridge 184:08ed48f1de7f 111
AnnaBridge 184:08ed48f1de7f 112 if (is_stdio_uart) {
AnnaBridge 184:08ed48f1de7f 113 stdio_uart_inited = 1;
AnnaBridge 184:08ed48f1de7f 114 memcpy(&stdio_uart, obj, sizeof(serial_t));
AnnaBridge 184:08ed48f1de7f 115 }
AnnaBridge 184:08ed48f1de7f 116 }
AnnaBridge 184:08ed48f1de7f 117
AnnaBridge 184:08ed48f1de7f 118 void serial_init_UART_configure(int uartname, serial_t *obj, PinName tx, PinName rx)
AnnaBridge 184:08ed48f1de7f 119 {
AnnaBridge 184:08ed48f1de7f 120 if (uartname <= SERIAL_3) {
AnnaBridge 184:08ed48f1de7f 121 obj->uart_config.BaudRate = 9600U;
AnnaBridge 184:08ed48f1de7f 122 obj->uart_config.DataBits = UART_DATA_BITS_8;
AnnaBridge 184:08ed48f1de7f 123 obj->uart_config.StopBits = UART_STOP_BITS_1;
AnnaBridge 184:08ed48f1de7f 124 obj->uart_config.Parity = UART_NO_PARITY;
AnnaBridge 184:08ed48f1de7f 125 obj->uart_config.FlowCtrl = UART_NONE_FLOW_CTRL;
AnnaBridge 184:08ed48f1de7f 126
AnnaBridge 184:08ed48f1de7f 127 if (tx != NC && rx != NC) {
AnnaBridge 184:08ed48f1de7f 128 obj->uart_config.Mode = UART_ENABLE_RX | UART_ENABLE_TX;
AnnaBridge 184:08ed48f1de7f 129 } else if (tx != NC) {
AnnaBridge 184:08ed48f1de7f 130 obj->uart_config.Mode = UART_ENABLE_TX;
AnnaBridge 184:08ed48f1de7f 131 } else if (rx != NC) {
AnnaBridge 184:08ed48f1de7f 132 obj->uart_config.Mode = UART_ENABLE_RX;
AnnaBridge 184:08ed48f1de7f 133 }
AnnaBridge 184:08ed48f1de7f 134
AnnaBridge 184:08ed48f1de7f 135 // Pinout the chosen uart
AnnaBridge 184:08ed48f1de7f 136 pinmap_pinout(tx, PinMap_UART_TX);
AnnaBridge 184:08ed48f1de7f 137 pinmap_pinout(rx, PinMap_UART_RX);
AnnaBridge 184:08ed48f1de7f 138
AnnaBridge 184:08ed48f1de7f 139 UART_Enable(obj->UARTx);
AnnaBridge 184:08ed48f1de7f 140 UART_SetIdleMode(obj->UARTx, ENABLE);
AnnaBridge 184:08ed48f1de7f 141 UART_Init(obj->UARTx, &obj->uart_config);
AnnaBridge 184:08ed48f1de7f 142 } else {
AnnaBridge 184:08ed48f1de7f 143 obj->fuart_config.BaudRate = 9600U;
AnnaBridge 184:08ed48f1de7f 144 obj->fuart_config.DataBits = FUART_DATA_BITS_8;
AnnaBridge 184:08ed48f1de7f 145 obj->fuart_config.StopBits = FUART_STOP_BITS_1;
AnnaBridge 184:08ed48f1de7f 146 obj->fuart_config.Parity = FUART_NO_PARITY;
AnnaBridge 184:08ed48f1de7f 147 obj->fuart_config.FlowCtrl = FUART_NONE_FLOW_CTRL;
AnnaBridge 184:08ed48f1de7f 148
AnnaBridge 184:08ed48f1de7f 149 if (tx != NC && rx != NC) {
AnnaBridge 184:08ed48f1de7f 150 obj->fuart_config.Mode = FUART_ENABLE_TX | FUART_ENABLE_RX;
AnnaBridge 184:08ed48f1de7f 151 } else if (tx != NC) {
AnnaBridge 184:08ed48f1de7f 152 obj->fuart_config.Mode = FUART_ENABLE_TX;
AnnaBridge 184:08ed48f1de7f 153 } else if (rx != NC) {
AnnaBridge 184:08ed48f1de7f 154 obj->fuart_config.Mode = FUART_ENABLE_RX;
AnnaBridge 184:08ed48f1de7f 155 }
AnnaBridge 184:08ed48f1de7f 156
AnnaBridge 184:08ed48f1de7f 157 // pin-out the chosen UART
AnnaBridge 184:08ed48f1de7f 158 pinmap_pinout(tx, PinMap_UART_TX);
AnnaBridge 184:08ed48f1de7f 159 pinmap_pinout(rx, PinMap_UART_RX);
AnnaBridge 184:08ed48f1de7f 160
AnnaBridge 184:08ed48f1de7f 161 FUART_Init(obj->FUART, &obj->fuart_config);
AnnaBridge 184:08ed48f1de7f 162 FUART_Enable(obj->FUART);
AnnaBridge 184:08ed48f1de7f 163 }
AnnaBridge 184:08ed48f1de7f 164 }
AnnaBridge 184:08ed48f1de7f 165
AnnaBridge 184:08ed48f1de7f 166 void serial_free(serial_t *obj)
AnnaBridge 184:08ed48f1de7f 167 {
AnnaBridge 184:08ed48f1de7f 168 switch (obj->index) {
AnnaBridge 184:08ed48f1de7f 169 case SERIAL_0:
AnnaBridge 184:08ed48f1de7f 170 case SERIAL_1:
AnnaBridge 184:08ed48f1de7f 171 case SERIAL_2:
AnnaBridge 184:08ed48f1de7f 172 case SERIAL_3:
AnnaBridge 184:08ed48f1de7f 173 // Disable UART
AnnaBridge 184:08ed48f1de7f 174 UART_Disable(obj->UARTx);
AnnaBridge 184:08ed48f1de7f 175 UART_SWReset(obj->UARTx);
AnnaBridge 184:08ed48f1de7f 176 // set information of object to invalid
AnnaBridge 184:08ed48f1de7f 177 obj->uart_config.BaudRate = 0;
AnnaBridge 184:08ed48f1de7f 178 obj->uart_config.DataBits = 0;
AnnaBridge 184:08ed48f1de7f 179 obj->uart_config.StopBits = 0;
AnnaBridge 184:08ed48f1de7f 180 obj->uart_config.Parity = 0;
AnnaBridge 184:08ed48f1de7f 181 obj->uart_config.Mode = 0;
AnnaBridge 184:08ed48f1de7f 182 obj->uart_config.FlowCtrl = 0;
AnnaBridge 184:08ed48f1de7f 183 break;
AnnaBridge 184:08ed48f1de7f 184 case SERIAL_4:
AnnaBridge 184:08ed48f1de7f 185 case SERIAL_5:
AnnaBridge 184:08ed48f1de7f 186 // Disable UART
AnnaBridge 184:08ed48f1de7f 187 FUART_Disable(obj->FUART);
AnnaBridge 184:08ed48f1de7f 188 // set information of object to invalid
AnnaBridge 184:08ed48f1de7f 189 obj->fuart_config.BaudRate = 0;
AnnaBridge 184:08ed48f1de7f 190 obj->fuart_config.DataBits = 0;
AnnaBridge 184:08ed48f1de7f 191 obj->fuart_config.StopBits = 0;
AnnaBridge 184:08ed48f1de7f 192 obj->fuart_config.Parity = 0;
AnnaBridge 184:08ed48f1de7f 193 obj->fuart_config.Mode = 0;
AnnaBridge 184:08ed48f1de7f 194 obj->fuart_config.FlowCtrl = 0;
AnnaBridge 184:08ed48f1de7f 195 break;
AnnaBridge 184:08ed48f1de7f 196 }
AnnaBridge 184:08ed48f1de7f 197 }
AnnaBridge 184:08ed48f1de7f 198
AnnaBridge 184:08ed48f1de7f 199 // serial_baud
AnnaBridge 184:08ed48f1de7f 200 void serial_baud(serial_t *obj, int baudrate)
AnnaBridge 184:08ed48f1de7f 201 {
AnnaBridge 184:08ed48f1de7f 202 switch (obj->index) {
AnnaBridge 184:08ed48f1de7f 203 case SERIAL_0:
AnnaBridge 184:08ed48f1de7f 204 case SERIAL_1:
AnnaBridge 184:08ed48f1de7f 205 case SERIAL_2:
AnnaBridge 184:08ed48f1de7f 206 case SERIAL_3:
AnnaBridge 184:08ed48f1de7f 207 obj->uart_config.BaudRate = baudrate;
AnnaBridge 184:08ed48f1de7f 208 UART_Init(obj->UARTx,&obj->uart_config);
AnnaBridge 184:08ed48f1de7f 209 break;
AnnaBridge 184:08ed48f1de7f 210 case SERIAL_4:
AnnaBridge 184:08ed48f1de7f 211 case SERIAL_5:
AnnaBridge 184:08ed48f1de7f 212 FUART_Disable(obj->FUART);
AnnaBridge 184:08ed48f1de7f 213 obj->fuart_config.BaudRate = baudrate;
AnnaBridge 184:08ed48f1de7f 214 FUART_Init(obj->FUART,&obj->fuart_config);
AnnaBridge 184:08ed48f1de7f 215 FUART_Enable(obj->FUART);
AnnaBridge 184:08ed48f1de7f 216 break;
AnnaBridge 184:08ed48f1de7f 217 }
AnnaBridge 184:08ed48f1de7f 218 }
AnnaBridge 184:08ed48f1de7f 219
AnnaBridge 184:08ed48f1de7f 220 void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits)
AnnaBridge 184:08ed48f1de7f 221 {
AnnaBridge 184:08ed48f1de7f 222 // 0: 1 stop bits, 1: 2 stop bits
AnnaBridge 184:08ed48f1de7f 223 MBED_ASSERT((stop_bits == 1) || (stop_bits == 2));
AnnaBridge 184:08ed48f1de7f 224 MBED_ASSERT((parity == ParityNone) || (parity == ParityOdd) || (parity == ParityEven) ||
AnnaBridge 184:08ed48f1de7f 225 (parity == ParityForced1) || (parity == ParityForced0));
AnnaBridge 184:08ed48f1de7f 226 // 0: 7 data bits ... 2: 9 data bits
AnnaBridge 184:08ed48f1de7f 227 switch (obj->index) {
AnnaBridge 184:08ed48f1de7f 228 case SERIAL_0:
AnnaBridge 184:08ed48f1de7f 229 case SERIAL_1:
AnnaBridge 184:08ed48f1de7f 230 case SERIAL_2:
AnnaBridge 184:08ed48f1de7f 231 case SERIAL_3:
AnnaBridge 184:08ed48f1de7f 232 MBED_ASSERT((data_bits > 6) && (data_bits < 10)); // 0: 7 data bits ... 2: 9 data bits
AnnaBridge 184:08ed48f1de7f 233 obj->uart_config.DataBits = data_bits;
AnnaBridge 184:08ed48f1de7f 234 obj->uart_config.StopBits = stop_bits;
AnnaBridge 184:08ed48f1de7f 235 obj->uart_config.Parity = parity;
AnnaBridge 184:08ed48f1de7f 236 UART_Init(obj->UARTx,&obj->uart_config);
AnnaBridge 184:08ed48f1de7f 237 break;
AnnaBridge 184:08ed48f1de7f 238 case SERIAL_4:
AnnaBridge 184:08ed48f1de7f 239 case SERIAL_5:
AnnaBridge 184:08ed48f1de7f 240 FUART_Disable(obj->FUART);
AnnaBridge 184:08ed48f1de7f 241 MBED_ASSERT((data_bits > 4) && (data_bits < 9)); // 0: 5 data bits ... 2: 8 data bits
AnnaBridge 184:08ed48f1de7f 242 obj->fuart_config.DataBits = data_bits;
AnnaBridge 184:08ed48f1de7f 243 obj->fuart_config.StopBits = stop_bits;
AnnaBridge 184:08ed48f1de7f 244 obj->fuart_config.Parity = parity;
AnnaBridge 184:08ed48f1de7f 245 FUART_Init(obj->FUART,&obj->fuart_config);
AnnaBridge 184:08ed48f1de7f 246 FUART_Enable(obj->FUART);
AnnaBridge 184:08ed48f1de7f 247 break;
AnnaBridge 184:08ed48f1de7f 248 }
AnnaBridge 184:08ed48f1de7f 249 }
AnnaBridge 184:08ed48f1de7f 250
AnnaBridge 184:08ed48f1de7f 251 void INTTX0_IRQHandler(void)
AnnaBridge 184:08ed48f1de7f 252 {
AnnaBridge 184:08ed48f1de7f 253 irq_handler(serial_irq_ids[SERIAL_0], TxIrq);
AnnaBridge 184:08ed48f1de7f 254 }
AnnaBridge 184:08ed48f1de7f 255
AnnaBridge 184:08ed48f1de7f 256 void INTRX0_IRQHandler(void)
AnnaBridge 184:08ed48f1de7f 257 {
AnnaBridge 184:08ed48f1de7f 258 irq_handler(serial_irq_ids[SERIAL_0], RxIrq);
AnnaBridge 184:08ed48f1de7f 259 }
AnnaBridge 184:08ed48f1de7f 260
AnnaBridge 184:08ed48f1de7f 261 void INTTX1_IRQHandler(void)
AnnaBridge 184:08ed48f1de7f 262 {
AnnaBridge 184:08ed48f1de7f 263 irq_handler(serial_irq_ids[SERIAL_1], TxIrq);
AnnaBridge 184:08ed48f1de7f 264 }
AnnaBridge 184:08ed48f1de7f 265
AnnaBridge 184:08ed48f1de7f 266 void INTRX1_IRQHandler(void)
AnnaBridge 184:08ed48f1de7f 267 {
AnnaBridge 184:08ed48f1de7f 268 irq_handler(serial_irq_ids[SERIAL_1], RxIrq);
AnnaBridge 184:08ed48f1de7f 269 }
AnnaBridge 184:08ed48f1de7f 270
AnnaBridge 184:08ed48f1de7f 271 void INTTX2_IRQHandler(void)
AnnaBridge 184:08ed48f1de7f 272 {
AnnaBridge 184:08ed48f1de7f 273 irq_handler(serial_irq_ids[SERIAL_2], TxIrq);
AnnaBridge 184:08ed48f1de7f 274 }
AnnaBridge 184:08ed48f1de7f 275
AnnaBridge 184:08ed48f1de7f 276 void INTRX2_IRQHandler(void)
AnnaBridge 184:08ed48f1de7f 277 {
AnnaBridge 184:08ed48f1de7f 278 irq_handler(serial_irq_ids[SERIAL_2], RxIrq);
AnnaBridge 184:08ed48f1de7f 279 }
AnnaBridge 184:08ed48f1de7f 280
AnnaBridge 184:08ed48f1de7f 281 void INTTX3_IRQHandler(void)
AnnaBridge 184:08ed48f1de7f 282 {
AnnaBridge 184:08ed48f1de7f 283 irq_handler(serial_irq_ids[SERIAL_3], TxIrq);
AnnaBridge 184:08ed48f1de7f 284 }
AnnaBridge 184:08ed48f1de7f 285
AnnaBridge 184:08ed48f1de7f 286 void INTRX3_IRQHandler(void)
AnnaBridge 184:08ed48f1de7f 287 {
AnnaBridge 184:08ed48f1de7f 288 irq_handler(serial_irq_ids[SERIAL_3], RxIrq);
AnnaBridge 184:08ed48f1de7f 289 }
AnnaBridge 184:08ed48f1de7f 290
AnnaBridge 184:08ed48f1de7f 291 void INTUART0_IRQHandler(void)
AnnaBridge 184:08ed48f1de7f 292 {
AnnaBridge 184:08ed48f1de7f 293 FUART_INTStatus fuart_int;
AnnaBridge 184:08ed48f1de7f 294 fuart_int = FUART_GetMaskedINTStatus(FUART0);
AnnaBridge 184:08ed48f1de7f 295 if (fuart_int.Bit.TxFIFO == 1) {
AnnaBridge 184:08ed48f1de7f 296 irq_handler(serial_irq_ids[SERIAL_4], TxIrq);
AnnaBridge 184:08ed48f1de7f 297 }
AnnaBridge 184:08ed48f1de7f 298 if (fuart_int.Bit.RxFIFO == 1) {
AnnaBridge 184:08ed48f1de7f 299 irq_handler(serial_irq_ids[SERIAL_4], RxIrq);
AnnaBridge 184:08ed48f1de7f 300 }
AnnaBridge 184:08ed48f1de7f 301 }
AnnaBridge 184:08ed48f1de7f 302
AnnaBridge 184:08ed48f1de7f 303 void INTUART1_IRQHandler(void)
AnnaBridge 184:08ed48f1de7f 304 {
AnnaBridge 184:08ed48f1de7f 305 FUART_INTStatus fuart_int;
AnnaBridge 184:08ed48f1de7f 306 fuart_int = FUART_GetMaskedINTStatus(FUART1);
AnnaBridge 184:08ed48f1de7f 307 if (fuart_int.Bit.TxFIFO == 1) {
AnnaBridge 184:08ed48f1de7f 308 irq_handler(serial_irq_ids[SERIAL_5], TxIrq);
AnnaBridge 184:08ed48f1de7f 309 }
AnnaBridge 184:08ed48f1de7f 310 if (fuart_int.Bit.RxFIFO == 1) {
AnnaBridge 184:08ed48f1de7f 311 irq_handler(serial_irq_ids[SERIAL_5], RxIrq);
AnnaBridge 184:08ed48f1de7f 312 }
AnnaBridge 184:08ed48f1de7f 313 }
AnnaBridge 184:08ed48f1de7f 314
AnnaBridge 184:08ed48f1de7f 315 void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id)
AnnaBridge 184:08ed48f1de7f 316 {
AnnaBridge 184:08ed48f1de7f 317 irq_handler = handler;
AnnaBridge 184:08ed48f1de7f 318 serial_irq_ids[obj->index] = id;
AnnaBridge 184:08ed48f1de7f 319 }
AnnaBridge 184:08ed48f1de7f 320
AnnaBridge 184:08ed48f1de7f 321 void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
AnnaBridge 184:08ed48f1de7f 322 {
AnnaBridge 184:08ed48f1de7f 323 IRQn_Type irq_n = (IRQn_Type)0;
AnnaBridge 184:08ed48f1de7f 324 uint32_t int_mask = 0;
AnnaBridge 184:08ed48f1de7f 325
AnnaBridge 184:08ed48f1de7f 326 switch (obj->index) {
AnnaBridge 184:08ed48f1de7f 327 case SERIAL_0:
AnnaBridge 184:08ed48f1de7f 328 if (irq == RxIrq) {
AnnaBridge 184:08ed48f1de7f 329 irq_n = INTRX0_IRQn;
AnnaBridge 184:08ed48f1de7f 330 } else {
AnnaBridge 184:08ed48f1de7f 331 irq_n = INTTX0_IRQn;
AnnaBridge 184:08ed48f1de7f 332 }
AnnaBridge 184:08ed48f1de7f 333 break;
AnnaBridge 184:08ed48f1de7f 334 case SERIAL_1:
AnnaBridge 184:08ed48f1de7f 335 if (irq == RxIrq) {
AnnaBridge 184:08ed48f1de7f 336 irq_n = INTRX1_IRQn;
AnnaBridge 184:08ed48f1de7f 337 } else {
AnnaBridge 184:08ed48f1de7f 338 irq_n = INTTX1_IRQn;
AnnaBridge 184:08ed48f1de7f 339 }
AnnaBridge 184:08ed48f1de7f 340 break;
AnnaBridge 184:08ed48f1de7f 341 case SERIAL_2:
AnnaBridge 184:08ed48f1de7f 342 if (irq == RxIrq) {
AnnaBridge 184:08ed48f1de7f 343 irq_n = INTRX2_IRQn;
AnnaBridge 184:08ed48f1de7f 344 } else {
AnnaBridge 184:08ed48f1de7f 345 irq_n = INTTX2_IRQn;
AnnaBridge 184:08ed48f1de7f 346 }
AnnaBridge 184:08ed48f1de7f 347 break;
AnnaBridge 184:08ed48f1de7f 348 case SERIAL_3:
AnnaBridge 184:08ed48f1de7f 349 if (irq == RxIrq) {
AnnaBridge 184:08ed48f1de7f 350 irq_n = INTRX3_IRQn;
AnnaBridge 184:08ed48f1de7f 351 } else {
AnnaBridge 184:08ed48f1de7f 352 irq_n = INTTX3_IRQn;
AnnaBridge 184:08ed48f1de7f 353 }
AnnaBridge 184:08ed48f1de7f 354 break;
AnnaBridge 184:08ed48f1de7f 355 case SERIAL_4:
AnnaBridge 184:08ed48f1de7f 356 irq_n = INTUART0_IRQn;
AnnaBridge 184:08ed48f1de7f 357 break;
AnnaBridge 184:08ed48f1de7f 358 case SERIAL_5:
AnnaBridge 184:08ed48f1de7f 359 irq_n = INTUART1_IRQn;
AnnaBridge 184:08ed48f1de7f 360 break;
AnnaBridge 184:08ed48f1de7f 361 }
AnnaBridge 184:08ed48f1de7f 362
AnnaBridge 184:08ed48f1de7f 363 if ((obj->index == SERIAL_4) || (obj->index == SERIAL_5)) {
AnnaBridge 184:08ed48f1de7f 364 // Get interrupt mask
AnnaBridge 184:08ed48f1de7f 365 int_mask = obj->FUART->IMSC & FUART_INT_BITS;
AnnaBridge 184:08ed48f1de7f 366
AnnaBridge 184:08ed48f1de7f 367 // Set interrupt mask
AnnaBridge 184:08ed48f1de7f 368 if (irq == RxIrq) {
AnnaBridge 184:08ed48f1de7f 369 int_mask |= FUART_RX_FIFO_INT_MASK;
AnnaBridge 184:08ed48f1de7f 370 } else {
AnnaBridge 184:08ed48f1de7f 371 int_mask |= FUART_TX_FIFO_INT_MASK;
AnnaBridge 184:08ed48f1de7f 372 }
AnnaBridge 184:08ed48f1de7f 373 FUART_SetINTMask(obj->FUART, int_mask);
AnnaBridge 184:08ed48f1de7f 374 }
AnnaBridge 184:08ed48f1de7f 375
AnnaBridge 184:08ed48f1de7f 376 if (enable) {
AnnaBridge 184:08ed48f1de7f 377 NVIC_EnableIRQ(irq_n);
AnnaBridge 184:08ed48f1de7f 378 } else {
AnnaBridge 184:08ed48f1de7f 379 NVIC_DisableIRQ(irq_n);
AnnaBridge 184:08ed48f1de7f 380 }
AnnaBridge 184:08ed48f1de7f 381 }
AnnaBridge 184:08ed48f1de7f 382
AnnaBridge 184:08ed48f1de7f 383 int serial_getc(serial_t *obj)
AnnaBridge 184:08ed48f1de7f 384 {
AnnaBridge 184:08ed48f1de7f 385 int data = 0;
AnnaBridge 184:08ed48f1de7f 386
AnnaBridge 184:08ed48f1de7f 387 // Wait until Rx buffer is full
AnnaBridge 184:08ed48f1de7f 388 while (!serial_readable(obj)) {
AnnaBridge 184:08ed48f1de7f 389 // Do nothing
AnnaBridge 184:08ed48f1de7f 390 }
AnnaBridge 184:08ed48f1de7f 391 switch (obj->index) {
AnnaBridge 184:08ed48f1de7f 392 case SERIAL_0:
AnnaBridge 184:08ed48f1de7f 393 case SERIAL_1:
AnnaBridge 184:08ed48f1de7f 394 case SERIAL_2:
AnnaBridge 184:08ed48f1de7f 395 case SERIAL_3:
AnnaBridge 184:08ed48f1de7f 396 data = (int) UART_GetRxData(obj->UARTx);
AnnaBridge 184:08ed48f1de7f 397 break;
AnnaBridge 184:08ed48f1de7f 398 case SERIAL_4:
AnnaBridge 184:08ed48f1de7f 399 case SERIAL_5:
AnnaBridge 184:08ed48f1de7f 400 data = (int) FUART_GetRxData(obj->FUART);
AnnaBridge 184:08ed48f1de7f 401 break;
AnnaBridge 184:08ed48f1de7f 402 default:
AnnaBridge 184:08ed48f1de7f 403 break;
AnnaBridge 184:08ed48f1de7f 404 }
AnnaBridge 184:08ed48f1de7f 405 return data;
AnnaBridge 184:08ed48f1de7f 406 }
AnnaBridge 184:08ed48f1de7f 407
AnnaBridge 184:08ed48f1de7f 408 void serial_putc(serial_t *obj, int c)
AnnaBridge 184:08ed48f1de7f 409 {
AnnaBridge 184:08ed48f1de7f 410 // Wait until Tx buffer is empty
AnnaBridge 184:08ed48f1de7f 411 while (!serial_writable(obj)) {
AnnaBridge 184:08ed48f1de7f 412 // Do nothing
AnnaBridge 184:08ed48f1de7f 413 }
AnnaBridge 184:08ed48f1de7f 414
AnnaBridge 184:08ed48f1de7f 415 switch (obj->index) {
AnnaBridge 184:08ed48f1de7f 416 case SERIAL_0:
AnnaBridge 184:08ed48f1de7f 417 case SERIAL_1:
AnnaBridge 184:08ed48f1de7f 418 case SERIAL_2:
AnnaBridge 184:08ed48f1de7f 419 case SERIAL_3:
AnnaBridge 184:08ed48f1de7f 420 UART_SetTxData(obj->UARTx,(uint32_t)c);
AnnaBridge 184:08ed48f1de7f 421 break;
AnnaBridge 184:08ed48f1de7f 422 case SERIAL_4:
AnnaBridge 184:08ed48f1de7f 423 case SERIAL_5:
AnnaBridge 184:08ed48f1de7f 424 FUART_SetTxData(obj->FUART,(uint32_t)c);
AnnaBridge 184:08ed48f1de7f 425 break;
AnnaBridge 184:08ed48f1de7f 426 }
AnnaBridge 184:08ed48f1de7f 427 }
AnnaBridge 184:08ed48f1de7f 428
AnnaBridge 184:08ed48f1de7f 429 int serial_readable(serial_t *obj)
AnnaBridge 184:08ed48f1de7f 430 {
AnnaBridge 184:08ed48f1de7f 431 int ret = 0;
AnnaBridge 184:08ed48f1de7f 432
AnnaBridge 184:08ed48f1de7f 433 switch (obj->index) {
AnnaBridge 184:08ed48f1de7f 434 case SERIAL_0:
AnnaBridge 184:08ed48f1de7f 435 case SERIAL_1:
AnnaBridge 184:08ed48f1de7f 436 case SERIAL_2:
AnnaBridge 184:08ed48f1de7f 437 case SERIAL_3:
AnnaBridge 184:08ed48f1de7f 438 if(UART_GetBufState(obj->UARTx, UART_RX) == DONE) {
AnnaBridge 184:08ed48f1de7f 439 ret = 1;
AnnaBridge 184:08ed48f1de7f 440 }
AnnaBridge 184:08ed48f1de7f 441 break;
AnnaBridge 184:08ed48f1de7f 442 case SERIAL_4:
AnnaBridge 184:08ed48f1de7f 443 case SERIAL_5:
AnnaBridge 184:08ed48f1de7f 444 if (FUART_GetStorageStatus(obj->FUART, FUART_RX) == FUART_STORAGE_FULL) {
AnnaBridge 184:08ed48f1de7f 445 ret = 1;
AnnaBridge 184:08ed48f1de7f 446 }
AnnaBridge 184:08ed48f1de7f 447 break;
AnnaBridge 184:08ed48f1de7f 448 }
AnnaBridge 184:08ed48f1de7f 449 return ret;
AnnaBridge 184:08ed48f1de7f 450 }
AnnaBridge 184:08ed48f1de7f 451
AnnaBridge 184:08ed48f1de7f 452 int serial_writable(serial_t *obj)
AnnaBridge 184:08ed48f1de7f 453 {
AnnaBridge 184:08ed48f1de7f 454 int ret = 0;
AnnaBridge 184:08ed48f1de7f 455
AnnaBridge 184:08ed48f1de7f 456 switch (obj->index) {
AnnaBridge 184:08ed48f1de7f 457 case SERIAL_0:
AnnaBridge 184:08ed48f1de7f 458 case SERIAL_1:
AnnaBridge 184:08ed48f1de7f 459 case SERIAL_2:
AnnaBridge 184:08ed48f1de7f 460 case SERIAL_3:
AnnaBridge 184:08ed48f1de7f 461 if(UART_GetBufState(obj->UARTx, UART_TX) == DONE) {
AnnaBridge 184:08ed48f1de7f 462 ret = 1;
AnnaBridge 184:08ed48f1de7f 463 }
AnnaBridge 184:08ed48f1de7f 464 break;
AnnaBridge 184:08ed48f1de7f 465 case SERIAL_4:
AnnaBridge 184:08ed48f1de7f 466 case SERIAL_5:
AnnaBridge 184:08ed48f1de7f 467 if (FUART_GetStorageStatus(obj->FUART, FUART_TX) == FUART_STORAGE_EMPTY) {
AnnaBridge 184:08ed48f1de7f 468 ret = 1;
AnnaBridge 184:08ed48f1de7f 469 }
AnnaBridge 184:08ed48f1de7f 470 break;
AnnaBridge 184:08ed48f1de7f 471 }
AnnaBridge 184:08ed48f1de7f 472 return ret;
AnnaBridge 184:08ed48f1de7f 473 }
AnnaBridge 184:08ed48f1de7f 474
AnnaBridge 184:08ed48f1de7f 475 void serial_clear(serial_t *obj)
AnnaBridge 184:08ed48f1de7f 476 {
AnnaBridge 184:08ed48f1de7f 477 switch (obj->index) {
AnnaBridge 184:08ed48f1de7f 478 case SERIAL_0:
AnnaBridge 184:08ed48f1de7f 479 case SERIAL_1:
AnnaBridge 184:08ed48f1de7f 480 case SERIAL_2:
AnnaBridge 184:08ed48f1de7f 481 case SERIAL_3:
AnnaBridge 184:08ed48f1de7f 482 UART_GetRxData(obj->UARTx);
AnnaBridge 184:08ed48f1de7f 483 break;
AnnaBridge 184:08ed48f1de7f 484 case SERIAL_4:
AnnaBridge 184:08ed48f1de7f 485 case SERIAL_5:
AnnaBridge 184:08ed48f1de7f 486 FUART_GetRxData(obj->FUART);
AnnaBridge 184:08ed48f1de7f 487 break;
AnnaBridge 184:08ed48f1de7f 488 }
AnnaBridge 184:08ed48f1de7f 489 }
AnnaBridge 184:08ed48f1de7f 490
AnnaBridge 184:08ed48f1de7f 491 void serial_pinout_tx(PinName tx)
AnnaBridge 184:08ed48f1de7f 492 {
AnnaBridge 184:08ed48f1de7f 493 // pin out the chosen UART
AnnaBridge 184:08ed48f1de7f 494 pinmap_pinout(tx, PinMap_UART_TX);
AnnaBridge 184:08ed48f1de7f 495 }
AnnaBridge 184:08ed48f1de7f 496
AnnaBridge 184:08ed48f1de7f 497 // Set flow control, Just support CTS
AnnaBridge 184:08ed48f1de7f 498 void serial_set_flow_control(serial_t *obj, FlowControl type, PinName rxflow, PinName txflow)
AnnaBridge 184:08ed48f1de7f 499 {
AnnaBridge 184:08ed48f1de7f 500 UARTName uart_cts = (UARTName)pinmap_peripheral(txflow, PinMap_UART_CTS);
AnnaBridge 184:08ed48f1de7f 501 UARTName uart_rts = (UARTName)pinmap_peripheral(rxflow, PinMap_UART_RTS);
AnnaBridge 184:08ed48f1de7f 502 UARTName uart_name = (UARTName)pinmap_merge(uart_cts, uart_rts);
AnnaBridge 184:08ed48f1de7f 503
AnnaBridge 184:08ed48f1de7f 504 switch (obj->index) {
AnnaBridge 184:08ed48f1de7f 505 case SERIAL_0:
AnnaBridge 184:08ed48f1de7f 506 case SERIAL_1:
AnnaBridge 184:08ed48f1de7f 507 case SERIAL_2:
AnnaBridge 184:08ed48f1de7f 508 case SERIAL_3:
AnnaBridge 184:08ed48f1de7f 509 if (FlowControlCTS == type) {
AnnaBridge 184:08ed48f1de7f 510 MBED_ASSERT(uart_cts != (UARTName) NC);
AnnaBridge 184:08ed48f1de7f 511
AnnaBridge 184:08ed48f1de7f 512 // Enable the pin for CTS function
AnnaBridge 184:08ed48f1de7f 513 pinmap_pinout(txflow, PinMap_UART_CTS);
AnnaBridge 184:08ed48f1de7f 514
AnnaBridge 184:08ed48f1de7f 515 // Support CTS hardware control flow only
AnnaBridge 184:08ed48f1de7f 516 obj->UARTx->MOD0 |= 1 << 6;
AnnaBridge 184:08ed48f1de7f 517 } else {
AnnaBridge 184:08ed48f1de7f 518 // Disable hardware flow control
AnnaBridge 184:08ed48f1de7f 519 obj->UARTx->MOD0 &= !(1 << 6);
AnnaBridge 184:08ed48f1de7f 520 }
AnnaBridge 184:08ed48f1de7f 521 break;
AnnaBridge 184:08ed48f1de7f 522 case SERIAL_4:
AnnaBridge 184:08ed48f1de7f 523 case SERIAL_5:
AnnaBridge 184:08ed48f1de7f 524 FUART_Disable(obj->FUART);
AnnaBridge 184:08ed48f1de7f 525 if (type == FlowControlCTS) {
AnnaBridge 184:08ed48f1de7f 526 MBED_ASSERT(uart_cts != (UARTName) NC);
AnnaBridge 184:08ed48f1de7f 527
AnnaBridge 184:08ed48f1de7f 528 // Enable CTS hardware flow control
AnnaBridge 184:08ed48f1de7f 529 obj->FUART->CR |= FUART_CTS_FLOW_CTRL;
AnnaBridge 184:08ed48f1de7f 530
AnnaBridge 184:08ed48f1de7f 531 // Enable the pin for CTS and RTS function
AnnaBridge 184:08ed48f1de7f 532 pinmap_pinout(txflow, PinMap_UART_CTS);
AnnaBridge 184:08ed48f1de7f 533 } else if (type == FlowControlRTS) {
AnnaBridge 184:08ed48f1de7f 534 MBED_ASSERT(uart_rts != (UARTName) NC);
AnnaBridge 184:08ed48f1de7f 535
AnnaBridge 184:08ed48f1de7f 536 // Enable RTS hardware flow control
AnnaBridge 184:08ed48f1de7f 537 obj->FUART->CR |= FUART_RTS_FLOW_CTRL;
AnnaBridge 184:08ed48f1de7f 538
AnnaBridge 184:08ed48f1de7f 539 // Enable the pin for RTS function
AnnaBridge 184:08ed48f1de7f 540 pinmap_pinout(rxflow, PinMap_UART_RTS);
AnnaBridge 184:08ed48f1de7f 541 } else if (type == FlowControlRTSCTS) {
AnnaBridge 184:08ed48f1de7f 542 MBED_ASSERT(uart_name != (UARTName) NC);
AnnaBridge 184:08ed48f1de7f 543
AnnaBridge 184:08ed48f1de7f 544 // Enable CTS and RTS hardware flow control
AnnaBridge 184:08ed48f1de7f 545 obj->FUART->CR |= FUART_CTS_FLOW_CTRL | FUART_RTS_FLOW_CTRL;
AnnaBridge 184:08ed48f1de7f 546
AnnaBridge 184:08ed48f1de7f 547 // Enable the pin for CTS and RTS function
AnnaBridge 184:08ed48f1de7f 548 pinmap_pinout(txflow, PinMap_UART_CTS);
AnnaBridge 184:08ed48f1de7f 549 pinmap_pinout(rxflow, PinMap_UART_RTS);
AnnaBridge 184:08ed48f1de7f 550 } else {
AnnaBridge 184:08ed48f1de7f 551 // Disable CTS and RTS hardware flow control
AnnaBridge 184:08ed48f1de7f 552 obj->FUART->CR &= (uint32_t) 0xFFFF0FFF;
AnnaBridge 184:08ed48f1de7f 553 }
AnnaBridge 184:08ed48f1de7f 554 FUART_Enable(obj->FUART);
AnnaBridge 184:08ed48f1de7f 555 break;
AnnaBridge 184:08ed48f1de7f 556 }
AnnaBridge 184:08ed48f1de7f 557 }
AnnaBridge 184:08ed48f1de7f 558
AnnaBridge 184:08ed48f1de7f 559 // Pause transmission
AnnaBridge 184:08ed48f1de7f 560 void serial_break_set(serial_t *obj)
AnnaBridge 184:08ed48f1de7f 561 {
AnnaBridge 184:08ed48f1de7f 562 if (obj->index == SERIAL_4 || obj->index == SERIAL_5) {
AnnaBridge 184:08ed48f1de7f 563 FUART_SetSendBreak(obj->FUART, ENABLE);
AnnaBridge 184:08ed48f1de7f 564 }
AnnaBridge 184:08ed48f1de7f 565 }
AnnaBridge 184:08ed48f1de7f 566
AnnaBridge 184:08ed48f1de7f 567 // Switch to normal transmission
AnnaBridge 184:08ed48f1de7f 568 void serial_break_clear(serial_t *obj)
AnnaBridge 184:08ed48f1de7f 569 {
AnnaBridge 184:08ed48f1de7f 570 if (obj->index == SERIAL_4 || obj->index == SERIAL_5) {
AnnaBridge 184:08ed48f1de7f 571 FUART_SetSendBreak(obj->FUART, DISABLE);
AnnaBridge 184:08ed48f1de7f 572 }
AnnaBridge 184:08ed48f1de7f 573 }