mbed library sources
Fork of mbed-src by
targets/hal/TARGET_NXP/TARGET_LPC15XX/serial_api.c@251:de9a1e4ffd79, 2014-07-08 (annotated)
- Committer:
- mbed_official
- Date:
- Tue Jul 08 14:15:07 2014 +0100
- Revision:
- 251:de9a1e4ffd79
- Parent:
- 250:a49055e7a707
- Child:
- 285:31249416b6f9
Synchronized with git revision b0ac1e3485a9aa1cb98d1ca68df9d1cdbcd6ce63
Full URL: https://github.com/mbedmicro/mbed/commit/b0ac1e3485a9aa1cb98d1ca68df9d1cdbcd6ce63/
Revert "error.h -> mbed_error.h"
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 103:9b881da47c92 | 1 | /* mbed Microcontroller Library |
mbed_official | 103:9b881da47c92 | 2 | * Copyright (c) 2006-2013 ARM Limited |
mbed_official | 103:9b881da47c92 | 3 | * |
mbed_official | 103:9b881da47c92 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mbed_official | 103:9b881da47c92 | 5 | * you may not use this file except in compliance with the License. |
mbed_official | 103:9b881da47c92 | 6 | * You may obtain a copy of the License at |
mbed_official | 103:9b881da47c92 | 7 | * |
mbed_official | 103:9b881da47c92 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbed_official | 103:9b881da47c92 | 9 | * |
mbed_official | 103:9b881da47c92 | 10 | * Unless required by applicable law or agreed to in writing, software |
mbed_official | 103:9b881da47c92 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mbed_official | 103:9b881da47c92 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbed_official | 103:9b881da47c92 | 13 | * See the License for the specific language governing permissions and |
mbed_official | 103:9b881da47c92 | 14 | * limitations under the License. |
mbed_official | 103:9b881da47c92 | 15 | */ |
mbed_official | 103:9b881da47c92 | 16 | // math.h required for floating point operations for baud rate calculation |
mbed_official | 227:7bd0639b8911 | 17 | #include "mbed_assert.h" |
mbed_official | 103:9b881da47c92 | 18 | #include <math.h> |
mbed_official | 103:9b881da47c92 | 19 | #include <string.h> |
mbed_official | 103:9b881da47c92 | 20 | |
mbed_official | 103:9b881da47c92 | 21 | #include "serial_api.h" |
mbed_official | 103:9b881da47c92 | 22 | #include "cmsis.h" |
mbed_official | 103:9b881da47c92 | 23 | #include "pinmap.h" |
mbed_official | 251:de9a1e4ffd79 | 24 | #include "error.h" |
mbed_official | 103:9b881da47c92 | 25 | |
mbed_official | 103:9b881da47c92 | 26 | /****************************************************************************** |
mbed_official | 103:9b881da47c92 | 27 | * INITIALIZATION |
mbed_official | 103:9b881da47c92 | 28 | ******************************************************************************/ |
mbed_official | 103:9b881da47c92 | 29 | #define UART_NUM 3 |
mbed_official | 103:9b881da47c92 | 30 | |
mbed_official | 103:9b881da47c92 | 31 | static const SWM_Map SWM_UART_TX[] = { |
mbed_official | 103:9b881da47c92 | 32 | {0, 0}, // Pin assign register0, 7:0bit |
mbed_official | 103:9b881da47c92 | 33 | {1, 8}, // Pin assign register1, 15:8bit |
mbed_official | 103:9b881da47c92 | 34 | {2, 16}, // Pin assign register2, 23:16bit |
mbed_official | 103:9b881da47c92 | 35 | }; |
mbed_official | 103:9b881da47c92 | 36 | |
mbed_official | 103:9b881da47c92 | 37 | static const SWM_Map SWM_UART_RX[] = { |
mbed_official | 103:9b881da47c92 | 38 | {0, 8}, |
mbed_official | 103:9b881da47c92 | 39 | {1, 16}, |
mbed_official | 103:9b881da47c92 | 40 | {2, 24}, |
mbed_official | 103:9b881da47c92 | 41 | }; |
mbed_official | 103:9b881da47c92 | 42 | |
mbed_official | 103:9b881da47c92 | 43 | static const SWM_Map SWM_UART_RTS[] = { |
mbed_official | 103:9b881da47c92 | 44 | {0, 16}, |
mbed_official | 103:9b881da47c92 | 45 | {1, 24}, |
mbed_official | 182:242346c42295 | 46 | {3, 0}, // not available |
mbed_official | 103:9b881da47c92 | 47 | }; |
mbed_official | 103:9b881da47c92 | 48 | |
mbed_official | 103:9b881da47c92 | 49 | static const SWM_Map SWM_UART_CTS[] = { |
mbed_official | 103:9b881da47c92 | 50 | {0, 24}, |
mbed_official | 103:9b881da47c92 | 51 | {2, 0}, |
mbed_official | 182:242346c42295 | 52 | {3, 8} // not available |
mbed_official | 103:9b881da47c92 | 53 | }; |
mbed_official | 103:9b881da47c92 | 54 | |
mbed_official | 103:9b881da47c92 | 55 | // bit flags for used UARTs |
mbed_official | 103:9b881da47c92 | 56 | static unsigned char uart_used = 0; |
mbed_official | 103:9b881da47c92 | 57 | static int get_available_uart(void) { |
mbed_official | 103:9b881da47c92 | 58 | int i; |
mbed_official | 103:9b881da47c92 | 59 | for (i=0; i<3; i++) { |
mbed_official | 103:9b881da47c92 | 60 | if ((uart_used & (1 << i)) == 0) |
mbed_official | 103:9b881da47c92 | 61 | return i; |
mbed_official | 103:9b881da47c92 | 62 | } |
mbed_official | 103:9b881da47c92 | 63 | return -1; |
mbed_official | 103:9b881da47c92 | 64 | } |
mbed_official | 103:9b881da47c92 | 65 | |
mbed_official | 103:9b881da47c92 | 66 | #define UART_EN (0x01<<0) |
mbed_official | 103:9b881da47c92 | 67 | |
mbed_official | 103:9b881da47c92 | 68 | #define CTS_DELTA (0x01<<5) |
mbed_official | 103:9b881da47c92 | 69 | #define RXBRK (0x01<<10) |
mbed_official | 103:9b881da47c92 | 70 | #define DELTA_RXBRK (0x01<<11) |
mbed_official | 103:9b881da47c92 | 71 | |
mbed_official | 103:9b881da47c92 | 72 | #define RXRDY (0x01<<0) |
mbed_official | 103:9b881da47c92 | 73 | #define TXRDY (0x01<<2) |
mbed_official | 103:9b881da47c92 | 74 | |
mbed_official | 103:9b881da47c92 | 75 | #define TXBRKEN (0x01<<1) |
mbed_official | 103:9b881da47c92 | 76 | #define CTSEN (0x01<<9) |
mbed_official | 103:9b881da47c92 | 77 | |
mbed_official | 103:9b881da47c92 | 78 | static uint32_t UARTSysClk; |
mbed_official | 103:9b881da47c92 | 79 | |
mbed_official | 103:9b881da47c92 | 80 | static uint32_t serial_irq_ids[UART_NUM] = {0}; |
mbed_official | 103:9b881da47c92 | 81 | static uart_irq_handler irq_handler; |
mbed_official | 103:9b881da47c92 | 82 | |
mbed_official | 103:9b881da47c92 | 83 | int stdio_uart_inited = 0; |
mbed_official | 103:9b881da47c92 | 84 | serial_t stdio_uart; |
mbed_official | 103:9b881da47c92 | 85 | |
mbed_official | 182:242346c42295 | 86 | static void switch_pin(const SWM_Map *swm, PinName pn) |
mbed_official | 182:242346c42295 | 87 | { |
mbed_official | 182:242346c42295 | 88 | uint32_t regVal; |
mbed_official | 182:242346c42295 | 89 | if (pn != NC) |
mbed_official | 182:242346c42295 | 90 | { |
mbed_official | 182:242346c42295 | 91 | // check if we have any function mapped to this pin already and remove it |
mbed_official | 234:37acebda271b | 92 | for (uint32_t n = 0; n < sizeof(LPC_SWM->PINASSIGN)/sizeof(*LPC_SWM->PINASSIGN); n ++) { |
mbed_official | 182:242346c42295 | 93 | regVal = LPC_SWM->PINASSIGN[n]; |
mbed_official | 234:37acebda271b | 94 | for (uint32_t j = 0; j <= 24; j += 8) { |
mbed_official | 234:37acebda271b | 95 | if (((regVal >> j) & 0xFF) == (uint32_t)pn) |
mbed_official | 182:242346c42295 | 96 | regVal |= (0xFF << j); |
mbed_official | 182:242346c42295 | 97 | } |
mbed_official | 182:242346c42295 | 98 | LPC_SWM->PINASSIGN[n] = regVal; |
mbed_official | 182:242346c42295 | 99 | } |
mbed_official | 182:242346c42295 | 100 | } |
mbed_official | 182:242346c42295 | 101 | // now map it |
mbed_official | 182:242346c42295 | 102 | regVal = LPC_SWM->PINASSIGN[swm->n] & ~(0xFF << swm->offset); |
mbed_official | 182:242346c42295 | 103 | LPC_SWM->PINASSIGN[swm->n] = regVal | (pn << swm->offset); |
mbed_official | 182:242346c42295 | 104 | } |
mbed_official | 182:242346c42295 | 105 | |
mbed_official | 103:9b881da47c92 | 106 | void serial_init(serial_t *obj, PinName tx, PinName rx) { |
mbed_official | 103:9b881da47c92 | 107 | int is_stdio_uart = 0; |
mbed_official | 234:37acebda271b | 108 | |
mbed_official | 103:9b881da47c92 | 109 | int uart_n = get_available_uart(); |
mbed_official | 103:9b881da47c92 | 110 | if (uart_n == -1) { |
mbed_official | 103:9b881da47c92 | 111 | error("No available UART"); |
mbed_official | 103:9b881da47c92 | 112 | } |
mbed_official | 103:9b881da47c92 | 113 | obj->index = uart_n; |
mbed_official | 103:9b881da47c92 | 114 | obj->uart = (LPC_USART0_Type *)(LPC_USART0_BASE + (0x4000 * uart_n)); |
mbed_official | 103:9b881da47c92 | 115 | uart_used |= (1 << uart_n); |
mbed_official | 103:9b881da47c92 | 116 | |
mbed_official | 182:242346c42295 | 117 | switch_pin(&SWM_UART_TX[uart_n], tx); |
mbed_official | 182:242346c42295 | 118 | switch_pin(&SWM_UART_RX[uart_n], rx); |
mbed_official | 103:9b881da47c92 | 119 | |
mbed_official | 103:9b881da47c92 | 120 | /* uart clock divided by 6 */ |
mbed_official | 103:9b881da47c92 | 121 | LPC_SYSCON->UARTCLKDIV =6; |
mbed_official | 103:9b881da47c92 | 122 | |
mbed_official | 103:9b881da47c92 | 123 | /* disable uart interrupts */ |
mbed_official | 103:9b881da47c92 | 124 | NVIC_DisableIRQ((IRQn_Type)(UART0_IRQn + uart_n)); |
mbed_official | 103:9b881da47c92 | 125 | |
mbed_official | 103:9b881da47c92 | 126 | /* Enable UART clock */ |
mbed_official | 103:9b881da47c92 | 127 | LPC_SYSCON->SYSAHBCLKCTRL1 |= (1 << (17 + uart_n)); |
mbed_official | 103:9b881da47c92 | 128 | |
mbed_official | 103:9b881da47c92 | 129 | /* Peripheral reset control to UART, a "1" bring it out of reset. */ |
mbed_official | 103:9b881da47c92 | 130 | LPC_SYSCON->PRESETCTRL1 |= (0x1 << (17 + uart_n)); |
mbed_official | 234:37acebda271b | 131 | LPC_SYSCON->PRESETCTRL1 &= ~(0x1 << (17 + uart_n)); |
mbed_official | 103:9b881da47c92 | 132 | |
mbed_official | 103:9b881da47c92 | 133 | UARTSysClk = SystemCoreClock / LPC_SYSCON->UARTCLKDIV; |
mbed_official | 103:9b881da47c92 | 134 | |
mbed_official | 103:9b881da47c92 | 135 | // set default baud rate and format |
mbed_official | 103:9b881da47c92 | 136 | serial_baud (obj, 9600); |
mbed_official | 103:9b881da47c92 | 137 | serial_format(obj, 8, ParityNone, 1); |
mbed_official | 103:9b881da47c92 | 138 | |
mbed_official | 103:9b881da47c92 | 139 | /* Clear all status bits. */ |
mbed_official | 103:9b881da47c92 | 140 | obj->uart->STAT = CTS_DELTA | DELTA_RXBRK; |
mbed_official | 103:9b881da47c92 | 141 | |
mbed_official | 103:9b881da47c92 | 142 | /* enable uart interrupts */ |
mbed_official | 103:9b881da47c92 | 143 | NVIC_EnableIRQ((IRQn_Type)(UART0_IRQn + uart_n)); |
mbed_official | 103:9b881da47c92 | 144 | |
mbed_official | 103:9b881da47c92 | 145 | /* Enable UART */ |
mbed_official | 103:9b881da47c92 | 146 | obj->uart->CFG |= UART_EN; |
mbed_official | 103:9b881da47c92 | 147 | |
mbed_official | 103:9b881da47c92 | 148 | is_stdio_uart = ((tx == USBTX) && (rx == USBRX)); |
mbed_official | 103:9b881da47c92 | 149 | |
mbed_official | 103:9b881da47c92 | 150 | if (is_stdio_uart) { |
mbed_official | 103:9b881da47c92 | 151 | stdio_uart_inited = 1; |
mbed_official | 103:9b881da47c92 | 152 | memcpy(&stdio_uart, obj, sizeof(serial_t)); |
mbed_official | 103:9b881da47c92 | 153 | } |
mbed_official | 103:9b881da47c92 | 154 | } |
mbed_official | 103:9b881da47c92 | 155 | |
mbed_official | 103:9b881da47c92 | 156 | void serial_free(serial_t *obj) { |
mbed_official | 103:9b881da47c92 | 157 | uart_used &= ~(1 << obj->index); |
mbed_official | 103:9b881da47c92 | 158 | serial_irq_ids[obj->index] = 0; |
mbed_official | 103:9b881da47c92 | 159 | } |
mbed_official | 103:9b881da47c92 | 160 | |
mbed_official | 103:9b881da47c92 | 161 | // serial_baud |
mbed_official | 103:9b881da47c92 | 162 | // set the baud rate, taking in to account the current SystemFrequency |
mbed_official | 103:9b881da47c92 | 163 | void serial_baud(serial_t *obj, int baudrate) { |
mbed_official | 103:9b881da47c92 | 164 | /* Integer divider: |
mbed_official | 103:9b881da47c92 | 165 | BRG = UARTSysClk/(Baudrate * 16) - 1 |
mbed_official | 103:9b881da47c92 | 166 | |
mbed_official | 103:9b881da47c92 | 167 | Frational divider: |
mbed_official | 103:9b881da47c92 | 168 | FRG = ((UARTSysClk / (Baudrate * 16 * (BRG + 1))) - 1) |
mbed_official | 103:9b881da47c92 | 169 | |
mbed_official | 103:9b881da47c92 | 170 | where |
mbed_official | 103:9b881da47c92 | 171 | FRG = (LPC_SYSCON->UARTFRDADD + 1) / (LPC_SYSCON->UARTFRDSUB + 1) |
mbed_official | 103:9b881da47c92 | 172 | |
mbed_official | 103:9b881da47c92 | 173 | (1) The easiest way is set SUB value to 256, -1 encoded, thus SUB |
mbed_official | 103:9b881da47c92 | 174 | register is 0xFF. |
mbed_official | 103:9b881da47c92 | 175 | (2) In ADD register value, depending on the value of UartSysClk, |
mbed_official | 103:9b881da47c92 | 176 | baudrate, BRG register value, and SUB register value, be careful |
mbed_official | 103:9b881da47c92 | 177 | about the order of multiplier and divider and make sure any |
mbed_official | 103:9b881da47c92 | 178 | multiplier doesn't exceed 32-bit boundary and any divider doesn't get |
mbed_official | 103:9b881da47c92 | 179 | down below one(integer 0). |
mbed_official | 103:9b881da47c92 | 180 | (3) ADD should be always less than SUB. |
mbed_official | 103:9b881da47c92 | 181 | */ |
mbed_official | 103:9b881da47c92 | 182 | obj->uart->BRG = UARTSysClk / 16 / baudrate - 1; |
mbed_official | 103:9b881da47c92 | 183 | |
mbed_official | 103:9b881da47c92 | 184 | // To use of the fractional baud rate generator, you must write 0xFF to the DIV |
mbed_official | 103:9b881da47c92 | 185 | // value to yield a denominator value of 256. All other values are not supported. |
mbed_official | 103:9b881da47c92 | 186 | LPC_SYSCON->FRGCTRL = 0xFF; |
mbed_official | 103:9b881da47c92 | 187 | |
mbed_official | 103:9b881da47c92 | 188 | LPC_SYSCON->FRGCTRL |= ( ( ((UARTSysClk / 16) * (0xFF + 1)) / |
mbed_official | 103:9b881da47c92 | 189 | (baudrate * (obj->uart->BRG + 1)) |
mbed_official | 103:9b881da47c92 | 190 | ) - (0xFF + 1) ) << 8; |
mbed_official | 103:9b881da47c92 | 191 | |
mbed_official | 103:9b881da47c92 | 192 | } |
mbed_official | 103:9b881da47c92 | 193 | |
mbed_official | 103:9b881da47c92 | 194 | void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) { |
mbed_official | 227:7bd0639b8911 | 195 | MBED_ASSERT((stop_bits == 1) || (stop_bits == 2)); // 0: 1 stop bits, 1: 2 stop bits |
mbed_official | 227:7bd0639b8911 | 196 | MBED_ASSERT((data_bits > 6) && (data_bits < 10)); // 0: 7 data bits ... 2: 9 data bits |
mbed_official | 227:7bd0639b8911 | 197 | MBED_ASSERT((parity == ParityNone) || (parity == ParityEven) || (parity == ParityOdd)); |
mbed_official | 227:7bd0639b8911 | 198 | |
mbed_official | 103:9b881da47c92 | 199 | stop_bits -= 1; |
mbed_official | 103:9b881da47c92 | 200 | data_bits -= 7; |
mbed_official | 103:9b881da47c92 | 201 | |
mbed_official | 103:9b881da47c92 | 202 | int paritysel; |
mbed_official | 103:9b881da47c92 | 203 | switch (parity) { |
mbed_official | 103:9b881da47c92 | 204 | case ParityNone: paritysel = 0; break; |
mbed_official | 103:9b881da47c92 | 205 | case ParityEven: paritysel = 2; break; |
mbed_official | 103:9b881da47c92 | 206 | case ParityOdd : paritysel = 3; break; |
mbed_official | 103:9b881da47c92 | 207 | default: |
mbed_official | 227:7bd0639b8911 | 208 | break; |
mbed_official | 103:9b881da47c92 | 209 | } |
mbed_official | 103:9b881da47c92 | 210 | |
mbed_official | 103:9b881da47c92 | 211 | obj->uart->CFG = (data_bits << 2) |
mbed_official | 103:9b881da47c92 | 212 | | (paritysel << 4) |
mbed_official | 103:9b881da47c92 | 213 | | (stop_bits << 6); |
mbed_official | 103:9b881da47c92 | 214 | } |
mbed_official | 103:9b881da47c92 | 215 | |
mbed_official | 103:9b881da47c92 | 216 | /****************************************************************************** |
mbed_official | 103:9b881da47c92 | 217 | * INTERRUPTS HANDLING |
mbed_official | 103:9b881da47c92 | 218 | ******************************************************************************/ |
mbed_official | 103:9b881da47c92 | 219 | static inline void uart_irq(uint32_t iir, uint32_t index) { |
mbed_official | 103:9b881da47c92 | 220 | // [Chapter 14] LPC17xx UART0/2/3: UARTn Interrupt Handling |
mbed_official | 103:9b881da47c92 | 221 | SerialIrq irq_type; |
mbed_official | 103:9b881da47c92 | 222 | switch (iir) { |
mbed_official | 103:9b881da47c92 | 223 | case 1: irq_type = TxIrq; break; |
mbed_official | 103:9b881da47c92 | 224 | case 2: irq_type = RxIrq; break; |
mbed_official | 103:9b881da47c92 | 225 | default: return; |
mbed_official | 103:9b881da47c92 | 226 | } |
mbed_official | 103:9b881da47c92 | 227 | |
mbed_official | 103:9b881da47c92 | 228 | if (serial_irq_ids[index] != 0) |
mbed_official | 103:9b881da47c92 | 229 | irq_handler(serial_irq_ids[index], irq_type); |
mbed_official | 103:9b881da47c92 | 230 | } |
mbed_official | 103:9b881da47c92 | 231 | |
mbed_official | 103:9b881da47c92 | 232 | void uart0_irq() {uart_irq((LPC_USART0->STAT & (1 << 2)) ? 2 : 1, 0);} |
mbed_official | 103:9b881da47c92 | 233 | void uart1_irq() {uart_irq((LPC_USART1->STAT & (1 << 2)) ? 2 : 1, 1);} |
mbed_official | 103:9b881da47c92 | 234 | void uart2_irq() {uart_irq((LPC_USART2->STAT & (1 << 2)) ? 2 : 1, 2);} |
mbed_official | 103:9b881da47c92 | 235 | |
mbed_official | 103:9b881da47c92 | 236 | void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id) { |
mbed_official | 103:9b881da47c92 | 237 | irq_handler = handler; |
mbed_official | 103:9b881da47c92 | 238 | serial_irq_ids[obj->index] = id; |
mbed_official | 103:9b881da47c92 | 239 | } |
mbed_official | 103:9b881da47c92 | 240 | |
mbed_official | 103:9b881da47c92 | 241 | void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) { |
mbed_official | 103:9b881da47c92 | 242 | IRQn_Type irq_n = (IRQn_Type)0; |
mbed_official | 103:9b881da47c92 | 243 | uint32_t vector = 0; |
mbed_official | 103:9b881da47c92 | 244 | switch ((int)obj->uart) { |
mbed_official | 103:9b881da47c92 | 245 | case LPC_USART0_BASE: irq_n=UART0_IRQn; vector = (uint32_t)&uart0_irq; break; |
mbed_official | 103:9b881da47c92 | 246 | case LPC_USART1_BASE: irq_n=UART1_IRQn; vector = (uint32_t)&uart1_irq; break; |
mbed_official | 103:9b881da47c92 | 247 | case LPC_USART2_BASE: irq_n=UART2_IRQn; vector = (uint32_t)&uart2_irq; break; |
mbed_official | 103:9b881da47c92 | 248 | } |
mbed_official | 103:9b881da47c92 | 249 | |
mbed_official | 103:9b881da47c92 | 250 | if (enable) { |
mbed_official | 103:9b881da47c92 | 251 | obj->uart->INTENSET = (1 << ((irq == RxIrq) ? 0 : 2)); |
mbed_official | 103:9b881da47c92 | 252 | NVIC_SetVector(irq_n, vector); |
mbed_official | 103:9b881da47c92 | 253 | NVIC_EnableIRQ(irq_n); |
mbed_official | 103:9b881da47c92 | 254 | } else { // disable |
mbed_official | 103:9b881da47c92 | 255 | int all_disabled = 0; |
mbed_official | 103:9b881da47c92 | 256 | SerialIrq other_irq = (irq == RxIrq) ? (TxIrq) : (RxIrq); |
mbed_official | 103:9b881da47c92 | 257 | obj->uart->INTENSET &= ~(1 << ((irq == RxIrq) ? 0 : 2)); |
mbed_official | 103:9b881da47c92 | 258 | all_disabled = (obj->uart->INTENSET & (1 << ((other_irq == RxIrq) ? 0 : 2))) == 0; |
mbed_official | 103:9b881da47c92 | 259 | if (all_disabled) |
mbed_official | 103:9b881da47c92 | 260 | NVIC_DisableIRQ(irq_n); |
mbed_official | 103:9b881da47c92 | 261 | } |
mbed_official | 103:9b881da47c92 | 262 | } |
mbed_official | 103:9b881da47c92 | 263 | |
mbed_official | 103:9b881da47c92 | 264 | /****************************************************************************** |
mbed_official | 103:9b881da47c92 | 265 | * READ/WRITE |
mbed_official | 103:9b881da47c92 | 266 | ******************************************************************************/ |
mbed_official | 103:9b881da47c92 | 267 | int serial_getc(serial_t *obj) { |
mbed_official | 103:9b881da47c92 | 268 | while (!serial_readable(obj)); |
mbed_official | 103:9b881da47c92 | 269 | return obj->uart->RXDATA; |
mbed_official | 103:9b881da47c92 | 270 | } |
mbed_official | 103:9b881da47c92 | 271 | |
mbed_official | 103:9b881da47c92 | 272 | void serial_putc(serial_t *obj, int c) { |
mbed_official | 103:9b881da47c92 | 273 | while (!serial_writable(obj)); |
mbed_official | 103:9b881da47c92 | 274 | obj->uart->TXDATA = c; |
mbed_official | 103:9b881da47c92 | 275 | } |
mbed_official | 103:9b881da47c92 | 276 | |
mbed_official | 103:9b881da47c92 | 277 | int serial_readable(serial_t *obj) { |
mbed_official | 103:9b881da47c92 | 278 | return obj->uart->STAT & RXRDY; |
mbed_official | 103:9b881da47c92 | 279 | } |
mbed_official | 103:9b881da47c92 | 280 | |
mbed_official | 103:9b881da47c92 | 281 | int serial_writable(serial_t *obj) { |
mbed_official | 103:9b881da47c92 | 282 | return obj->uart->STAT & TXRDY; |
mbed_official | 103:9b881da47c92 | 283 | } |
mbed_official | 103:9b881da47c92 | 284 | |
mbed_official | 103:9b881da47c92 | 285 | void serial_clear(serial_t *obj) { |
mbed_official | 103:9b881da47c92 | 286 | // [TODO] |
mbed_official | 103:9b881da47c92 | 287 | } |
mbed_official | 103:9b881da47c92 | 288 | |
mbed_official | 103:9b881da47c92 | 289 | void serial_pinout_tx(PinName tx) { |
mbed_official | 103:9b881da47c92 | 290 | |
mbed_official | 103:9b881da47c92 | 291 | } |
mbed_official | 103:9b881da47c92 | 292 | |
mbed_official | 103:9b881da47c92 | 293 | void serial_break_set(serial_t *obj) { |
mbed_official | 103:9b881da47c92 | 294 | obj->uart->CTRL |= TXBRKEN; |
mbed_official | 103:9b881da47c92 | 295 | } |
mbed_official | 103:9b881da47c92 | 296 | |
mbed_official | 103:9b881da47c92 | 297 | void serial_break_clear(serial_t *obj) { |
mbed_official | 103:9b881da47c92 | 298 | obj->uart->CTRL &= ~TXBRKEN; |
mbed_official | 103:9b881da47c92 | 299 | } |
mbed_official | 103:9b881da47c92 | 300 | |
mbed_official | 103:9b881da47c92 | 301 | void serial_set_flow_control(serial_t *obj, FlowControl type, PinName rxflow, PinName txflow) { |
mbed_official | 182:242346c42295 | 302 | if ((FlowControlNone == type || FlowControlRTS == type)) txflow = NC; |
mbed_official | 182:242346c42295 | 303 | if ((FlowControlNone == type || FlowControlCTS == type)) rxflow = NC; |
mbed_official | 182:242346c42295 | 304 | switch_pin(&SWM_UART_RTS[obj->index], rxflow); |
mbed_official | 182:242346c42295 | 305 | switch_pin(&SWM_UART_CTS[obj->index], txflow); |
mbed_official | 182:242346c42295 | 306 | if (txflow == NC) obj->uart->CFG &= ~CTSEN; |
mbed_official | 182:242346c42295 | 307 | else obj->uart->CFG |= CTSEN; |
mbed_official | 103:9b881da47c92 | 308 | } |
mbed_official | 103:9b881da47c92 | 309 |