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