Repostiory containing DAPLink source code with Reset Pin workaround for HANI_IOT board.
Upstream: https://github.com/ARMmbed/DAPLink
source/hic_hal/nxp/lpc4322/uart.c@0:01f31e923fe2, 2020-04-07 (annotated)
- Committer:
- Pawel Zarembski
- Date:
- Tue Apr 07 12:55:42 2020 +0200
- Revision:
- 0:01f31e923fe2
hani: DAPLink with reset workaround
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Pawel Zarembski |
0:01f31e923fe2 | 1 | /** |
Pawel Zarembski |
0:01f31e923fe2 | 2 | * @file uart.c |
Pawel Zarembski |
0:01f31e923fe2 | 3 | * @brief |
Pawel Zarembski |
0:01f31e923fe2 | 4 | * |
Pawel Zarembski |
0:01f31e923fe2 | 5 | * DAPLink Interface Firmware |
Pawel Zarembski |
0:01f31e923fe2 | 6 | * Copyright (c) 2009-2016, ARM Limited, All Rights Reserved |
Pawel Zarembski |
0:01f31e923fe2 | 7 | * SPDX-License-Identifier: Apache-2.0 |
Pawel Zarembski |
0:01f31e923fe2 | 8 | * |
Pawel Zarembski |
0:01f31e923fe2 | 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
Pawel Zarembski |
0:01f31e923fe2 | 10 | * not use this file except in compliance with the License. |
Pawel Zarembski |
0:01f31e923fe2 | 11 | * You may obtain a copy of the License at |
Pawel Zarembski |
0:01f31e923fe2 | 12 | * |
Pawel Zarembski |
0:01f31e923fe2 | 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
Pawel Zarembski |
0:01f31e923fe2 | 14 | * |
Pawel Zarembski |
0:01f31e923fe2 | 15 | * Unless required by applicable law or agreed to in writing, software |
Pawel Zarembski |
0:01f31e923fe2 | 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
Pawel Zarembski |
0:01f31e923fe2 | 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Pawel Zarembski |
0:01f31e923fe2 | 18 | * See the License for the specific language governing permissions and |
Pawel Zarembski |
0:01f31e923fe2 | 19 | * limitations under the License. |
Pawel Zarembski |
0:01f31e923fe2 | 20 | */ |
Pawel Zarembski |
0:01f31e923fe2 | 21 | |
Pawel Zarembski |
0:01f31e923fe2 | 22 | #include "LPC43xx.h" |
Pawel Zarembski |
0:01f31e923fe2 | 23 | #include "uart.h" |
Pawel Zarembski |
0:01f31e923fe2 | 24 | #include "lpc43xx_cgu.h" |
Pawel Zarembski |
0:01f31e923fe2 | 25 | #include "lpc43xx_scu.h" |
Pawel Zarembski |
0:01f31e923fe2 | 26 | #include "util.h" |
Pawel Zarembski |
0:01f31e923fe2 | 27 | #include "circ_buf.h" |
Pawel Zarembski |
0:01f31e923fe2 | 28 | #include "settings.h" // for config_get_overflow_detect |
Pawel Zarembski |
0:01f31e923fe2 | 29 | |
Pawel Zarembski |
0:01f31e923fe2 | 30 | static uint32_t baudrate; |
Pawel Zarembski |
0:01f31e923fe2 | 31 | static uint32_t dll; |
Pawel Zarembski |
0:01f31e923fe2 | 32 | static uint32_t tx_in_progress; |
Pawel Zarembski |
0:01f31e923fe2 | 33 | |
Pawel Zarembski |
0:01f31e923fe2 | 34 | extern uint32_t SystemCoreClock; |
Pawel Zarembski |
0:01f31e923fe2 | 35 | |
Pawel Zarembski |
0:01f31e923fe2 | 36 | #define RX_OVRF_MSG "<DAPLink:Overflow>\n" |
Pawel Zarembski |
0:01f31e923fe2 | 37 | #define RX_OVRF_MSG_SIZE (sizeof(RX_OVRF_MSG) - 1) |
Pawel Zarembski |
0:01f31e923fe2 | 38 | #define BUFFER_SIZE (512) |
Pawel Zarembski |
0:01f31e923fe2 | 39 | |
Pawel Zarembski |
0:01f31e923fe2 | 40 | circ_buf_t write_buffer; |
Pawel Zarembski |
0:01f31e923fe2 | 41 | uint8_t write_buffer_data[BUFFER_SIZE]; |
Pawel Zarembski |
0:01f31e923fe2 | 42 | circ_buf_t read_buffer; |
Pawel Zarembski |
0:01f31e923fe2 | 43 | uint8_t read_buffer_data[BUFFER_SIZE]; |
Pawel Zarembski |
0:01f31e923fe2 | 44 | |
Pawel Zarembski |
0:01f31e923fe2 | 45 | static int32_t reset(void); |
Pawel Zarembski |
0:01f31e923fe2 | 46 | |
Pawel Zarembski |
0:01f31e923fe2 | 47 | #define UART_IRQn USART0_IRQn |
Pawel Zarembski |
0:01f31e923fe2 | 48 | #define LPC_USART LPC_USART0 |
Pawel Zarembski |
0:01f31e923fe2 | 49 | #define UART_IRQHandler UART0_IRQHandler |
Pawel Zarembski |
0:01f31e923fe2 | 50 | |
Pawel Zarembski |
0:01f31e923fe2 | 51 | // UART Control Pin P2_2: GPIO5[2] |
Pawel Zarembski |
0:01f31e923fe2 | 52 | #define PORT_UARTCTRL 5 |
Pawel Zarembski |
0:01f31e923fe2 | 53 | #define PIN_UARTCTRL_IN_BIT 2 |
Pawel Zarembski |
0:01f31e923fe2 | 54 | #define PIN_UARTCTRL (1<<PIN_UARTCTRL_IN_BIT) |
Pawel Zarembski |
0:01f31e923fe2 | 55 | |
Pawel Zarembski |
0:01f31e923fe2 | 56 | |
Pawel Zarembski |
0:01f31e923fe2 | 57 | int32_t uart_initialize(void) |
Pawel Zarembski |
0:01f31e923fe2 | 58 | { |
Pawel Zarembski |
0:01f31e923fe2 | 59 | NVIC_DisableIRQ(UART_IRQn); |
Pawel Zarembski |
0:01f31e923fe2 | 60 | |
Pawel Zarembski |
0:01f31e923fe2 | 61 | // The baudrate calculations require the UART to be clocked as SystemCoreClock |
Pawel Zarembski |
0:01f31e923fe2 | 62 | CGU_EntityConnect(CGU_CLKSRC_PLL1, CGU_BASE_UART0); |
Pawel Zarembski |
0:01f31e923fe2 | 63 | CGU_EnableEntity(CGU_BASE_UART0, ENABLE); |
Pawel Zarembski |
0:01f31e923fe2 | 64 | scu_pinmux(2, 0, UART_RX_TX, FUNC1); /* P2_0: U0_TXD */ |
Pawel Zarembski |
0:01f31e923fe2 | 65 | scu_pinmux(2, 1, UART_RX_TX, FUNC1); /* P2_1: U0_RXD */ |
Pawel Zarembski |
0:01f31e923fe2 | 66 | |
Pawel Zarembski |
0:01f31e923fe2 | 67 | scu_pinmux(2, 2, GPIO_NOPULL, FUNC4); /* UARTCTRL: GPIO5[2] */ |
Pawel Zarembski |
0:01f31e923fe2 | 68 | // Control target's UART RX: |
Pawel Zarembski |
0:01f31e923fe2 | 69 | // UARTCTRL high: The LPC1549 gets uart input from the LPC4322 |
Pawel Zarembski |
0:01f31e923fe2 | 70 | // UARTCTRL low: The LPC1549 gets uart input from the ISP_RX on the pinlist |
Pawel Zarembski |
0:01f31e923fe2 | 71 | LPC_GPIO_PORT->CLR[PORT_UARTCTRL] = PIN_UARTCTRL; |
Pawel Zarembski |
0:01f31e923fe2 | 72 | LPC_GPIO_PORT->DIR[PORT_UARTCTRL] |= (PIN_UARTCTRL); |
Pawel Zarembski |
0:01f31e923fe2 | 73 | // enable FIFOs (trigger level 1) and clear them |
Pawel Zarembski |
0:01f31e923fe2 | 74 | LPC_USART->FCR = 0x87; |
Pawel Zarembski |
0:01f31e923fe2 | 75 | // Transmit Enable |
Pawel Zarembski |
0:01f31e923fe2 | 76 | LPC_USART->TER = 0x01; |
Pawel Zarembski |
0:01f31e923fe2 | 77 | // reset uart |
Pawel Zarembski |
0:01f31e923fe2 | 78 | reset(); |
Pawel Zarembski |
0:01f31e923fe2 | 79 | // enable rx and tx interrupt |
Pawel Zarembski |
0:01f31e923fe2 | 80 | LPC_USART->IER |= (1 << 0) | (1 << 1); |
Pawel Zarembski |
0:01f31e923fe2 | 81 | NVIC_EnableIRQ(UART_IRQn); |
Pawel Zarembski |
0:01f31e923fe2 | 82 | return 1; |
Pawel Zarembski |
0:01f31e923fe2 | 83 | } |
Pawel Zarembski |
0:01f31e923fe2 | 84 | |
Pawel Zarembski |
0:01f31e923fe2 | 85 | int32_t uart_uninitialize(void) |
Pawel Zarembski |
0:01f31e923fe2 | 86 | { |
Pawel Zarembski |
0:01f31e923fe2 | 87 | // disable interrupt |
Pawel Zarembski |
0:01f31e923fe2 | 88 | LPC_USART->IER &= ~(0x7); |
Pawel Zarembski |
0:01f31e923fe2 | 89 | NVIC_DisableIRQ(UART_IRQn); |
Pawel Zarembski |
0:01f31e923fe2 | 90 | // reset uart |
Pawel Zarembski |
0:01f31e923fe2 | 91 | reset(); |
Pawel Zarembski |
0:01f31e923fe2 | 92 | return 1; |
Pawel Zarembski |
0:01f31e923fe2 | 93 | } |
Pawel Zarembski |
0:01f31e923fe2 | 94 | |
Pawel Zarembski |
0:01f31e923fe2 | 95 | int32_t uart_reset(void) |
Pawel Zarembski |
0:01f31e923fe2 | 96 | { |
Pawel Zarembski |
0:01f31e923fe2 | 97 | // disable interrupt |
Pawel Zarembski |
0:01f31e923fe2 | 98 | NVIC_DisableIRQ(UART_IRQn); |
Pawel Zarembski |
0:01f31e923fe2 | 99 | // reset uart |
Pawel Zarembski |
0:01f31e923fe2 | 100 | reset(); |
Pawel Zarembski |
0:01f31e923fe2 | 101 | // enable interrupt |
Pawel Zarembski |
0:01f31e923fe2 | 102 | NVIC_EnableIRQ(UART_IRQn); |
Pawel Zarembski |
0:01f31e923fe2 | 103 | return 1; |
Pawel Zarembski |
0:01f31e923fe2 | 104 | } |
Pawel Zarembski |
0:01f31e923fe2 | 105 | |
Pawel Zarembski |
0:01f31e923fe2 | 106 | int32_t uart_set_configuration(UART_Configuration *config) |
Pawel Zarembski |
0:01f31e923fe2 | 107 | { |
Pawel Zarembski |
0:01f31e923fe2 | 108 | uint8_t DivAddVal = 0; |
Pawel Zarembski |
0:01f31e923fe2 | 109 | uint8_t MulVal = 1; |
Pawel Zarembski |
0:01f31e923fe2 | 110 | uint8_t mv, data_bits = 8, parity, stop_bits = 0; |
Pawel Zarembski |
0:01f31e923fe2 | 111 | // disable interrupt |
Pawel Zarembski |
0:01f31e923fe2 | 112 | NVIC_DisableIRQ(UART_IRQn); |
Pawel Zarembski |
0:01f31e923fe2 | 113 | // reset uart |
Pawel Zarembski |
0:01f31e923fe2 | 114 | reset(); |
Pawel Zarembski |
0:01f31e923fe2 | 115 | baudrate = config->Baudrate; |
Pawel Zarembski |
0:01f31e923fe2 | 116 | // Compute baud rate dividers |
Pawel Zarembski |
0:01f31e923fe2 | 117 | mv = 15; |
Pawel Zarembski |
0:01f31e923fe2 | 118 | dll = util_div_round_down(SystemCoreClock, 16 * config->Baudrate); |
Pawel Zarembski |
0:01f31e923fe2 | 119 | DivAddVal = util_div_round(SystemCoreClock * mv, dll * config->Baudrate * 16) - mv; |
Pawel Zarembski |
0:01f31e923fe2 | 120 | // set LCR[DLAB] to enable writing to divider registers |
Pawel Zarembski |
0:01f31e923fe2 | 121 | LPC_USART->LCR |= (1 << 7); |
Pawel Zarembski |
0:01f31e923fe2 | 122 | // set divider values |
Pawel Zarembski |
0:01f31e923fe2 | 123 | LPC_USART->DLM = (dll >> 8) & 0xFF; |
Pawel Zarembski |
0:01f31e923fe2 | 124 | LPC_USART->DLL = (dll >> 0) & 0xFF; |
Pawel Zarembski |
0:01f31e923fe2 | 125 | LPC_USART->FDR = (uint32_t) DivAddVal << 0 |
Pawel Zarembski |
0:01f31e923fe2 | 126 | | (uint32_t) MulVal << 4; |
Pawel Zarembski |
0:01f31e923fe2 | 127 | // clear LCR[DLAB] |
Pawel Zarembski |
0:01f31e923fe2 | 128 | LPC_USART->LCR &= ~(1 << 7); |
Pawel Zarembski |
0:01f31e923fe2 | 129 | |
Pawel Zarembski |
0:01f31e923fe2 | 130 | // set data bits, stop bits, parity |
Pawel Zarembski |
0:01f31e923fe2 | 131 | if ((config->DataBits < 5) || (config->DataBits > 8)) { |
Pawel Zarembski |
0:01f31e923fe2 | 132 | data_bits = 8; |
Pawel Zarembski |
0:01f31e923fe2 | 133 | } |
Pawel Zarembski |
0:01f31e923fe2 | 134 | |
Pawel Zarembski |
0:01f31e923fe2 | 135 | data_bits -= 5; |
Pawel Zarembski |
0:01f31e923fe2 | 136 | |
Pawel Zarembski |
0:01f31e923fe2 | 137 | if (config->StopBits != 1 && config->StopBits != 2) { |
Pawel Zarembski |
0:01f31e923fe2 | 138 | stop_bits = 1; |
Pawel Zarembski |
0:01f31e923fe2 | 139 | } |
Pawel Zarembski |
0:01f31e923fe2 | 140 | |
Pawel Zarembski |
0:01f31e923fe2 | 141 | stop_bits -= 1; |
Pawel Zarembski |
0:01f31e923fe2 | 142 | |
Pawel Zarembski |
0:01f31e923fe2 | 143 | switch (config->Parity) { |
Pawel Zarembski |
0:01f31e923fe2 | 144 | case UART_PARITY_ODD: |
Pawel Zarembski |
0:01f31e923fe2 | 145 | parity = 0x01; |
Pawel Zarembski |
0:01f31e923fe2 | 146 | break; // Parity Odd |
Pawel Zarembski |
0:01f31e923fe2 | 147 | |
Pawel Zarembski |
0:01f31e923fe2 | 148 | case UART_PARITY_EVEN: |
Pawel Zarembski |
0:01f31e923fe2 | 149 | parity = 0x03; |
Pawel Zarembski |
0:01f31e923fe2 | 150 | break; // Parity Even |
Pawel Zarembski |
0:01f31e923fe2 | 151 | |
Pawel Zarembski |
0:01f31e923fe2 | 152 | case UART_PARITY_MARK: |
Pawel Zarembski |
0:01f31e923fe2 | 153 | parity = 0x05; |
Pawel Zarembski |
0:01f31e923fe2 | 154 | break; // Parity Mark |
Pawel Zarembski |
0:01f31e923fe2 | 155 | |
Pawel Zarembski |
0:01f31e923fe2 | 156 | case UART_PARITY_SPACE: |
Pawel Zarembski |
0:01f31e923fe2 | 157 | parity = 0x07; |
Pawel Zarembski |
0:01f31e923fe2 | 158 | break; // Parity Space |
Pawel Zarembski |
0:01f31e923fe2 | 159 | |
Pawel Zarembski |
0:01f31e923fe2 | 160 | case UART_PARITY_NONE: // Parity None |
Pawel Zarembski |
0:01f31e923fe2 | 161 | default: |
Pawel Zarembski |
0:01f31e923fe2 | 162 | parity = 0x00; |
Pawel Zarembski |
0:01f31e923fe2 | 163 | break; |
Pawel Zarembski |
0:01f31e923fe2 | 164 | } |
Pawel Zarembski |
0:01f31e923fe2 | 165 | |
Pawel Zarembski |
0:01f31e923fe2 | 166 | LPC_USART->LCR = (data_bits << 0) |
Pawel Zarembski |
0:01f31e923fe2 | 167 | | (stop_bits << 2) |
Pawel Zarembski |
0:01f31e923fe2 | 168 | | (parity << 3); |
Pawel Zarembski |
0:01f31e923fe2 | 169 | // Enable UART interrupt |
Pawel Zarembski |
0:01f31e923fe2 | 170 | NVIC_EnableIRQ(UART_IRQn); |
Pawel Zarembski |
0:01f31e923fe2 | 171 | return 1; |
Pawel Zarembski |
0:01f31e923fe2 | 172 | } |
Pawel Zarembski |
0:01f31e923fe2 | 173 | |
Pawel Zarembski |
0:01f31e923fe2 | 174 | int32_t uart_get_configuration(UART_Configuration *config) |
Pawel Zarembski |
0:01f31e923fe2 | 175 | { |
Pawel Zarembski |
0:01f31e923fe2 | 176 | float br; |
Pawel Zarembski |
0:01f31e923fe2 | 177 | uint32_t lcr; |
Pawel Zarembski |
0:01f31e923fe2 | 178 | // line control parameter |
Pawel Zarembski |
0:01f31e923fe2 | 179 | lcr = LPC_USART->LCR; |
Pawel Zarembski |
0:01f31e923fe2 | 180 | // baudrate |
Pawel Zarembski |
0:01f31e923fe2 | 181 | br = SystemCoreClock / (dll * 16); |
Pawel Zarembski |
0:01f31e923fe2 | 182 | |
Pawel Zarembski |
0:01f31e923fe2 | 183 | // If inside +/- 2% tolerance |
Pawel Zarembski |
0:01f31e923fe2 | 184 | if (((br * 100) <= (baudrate * 102)) && ((br * 100) >= (baudrate * 98))) { |
Pawel Zarembski |
0:01f31e923fe2 | 185 | config->Baudrate = baudrate; |
Pawel Zarembski |
0:01f31e923fe2 | 186 | } else { |
Pawel Zarembski |
0:01f31e923fe2 | 187 | config->Baudrate = br; |
Pawel Zarembski |
0:01f31e923fe2 | 188 | } |
Pawel Zarembski |
0:01f31e923fe2 | 189 | |
Pawel Zarembski |
0:01f31e923fe2 | 190 | // get data bits |
Pawel Zarembski |
0:01f31e923fe2 | 191 | switch ((lcr >> 0) & 3) { |
Pawel Zarembski |
0:01f31e923fe2 | 192 | case 0: |
Pawel Zarembski |
0:01f31e923fe2 | 193 | config->DataBits = UART_DATA_BITS_5; |
Pawel Zarembski |
0:01f31e923fe2 | 194 | break; |
Pawel Zarembski |
0:01f31e923fe2 | 195 | |
Pawel Zarembski |
0:01f31e923fe2 | 196 | case 1: |
Pawel Zarembski |
0:01f31e923fe2 | 197 | config->DataBits = UART_DATA_BITS_6; |
Pawel Zarembski |
0:01f31e923fe2 | 198 | break; |
Pawel Zarembski |
0:01f31e923fe2 | 199 | |
Pawel Zarembski |
0:01f31e923fe2 | 200 | case 2: |
Pawel Zarembski |
0:01f31e923fe2 | 201 | config->DataBits = UART_DATA_BITS_7; |
Pawel Zarembski |
0:01f31e923fe2 | 202 | break; |
Pawel Zarembski |
0:01f31e923fe2 | 203 | |
Pawel Zarembski |
0:01f31e923fe2 | 204 | case 3: |
Pawel Zarembski |
0:01f31e923fe2 | 205 | config->DataBits = UART_DATA_BITS_8; |
Pawel Zarembski |
0:01f31e923fe2 | 206 | break; |
Pawel Zarembski |
0:01f31e923fe2 | 207 | |
Pawel Zarembski |
0:01f31e923fe2 | 208 | default: |
Pawel Zarembski |
0:01f31e923fe2 | 209 | return 0; |
Pawel Zarembski |
0:01f31e923fe2 | 210 | } |
Pawel Zarembski |
0:01f31e923fe2 | 211 | |
Pawel Zarembski |
0:01f31e923fe2 | 212 | // get parity |
Pawel Zarembski |
0:01f31e923fe2 | 213 | switch ((lcr >> 3) & 7) { |
Pawel Zarembski |
0:01f31e923fe2 | 214 | case 0: |
Pawel Zarembski |
0:01f31e923fe2 | 215 | case 2: |
Pawel Zarembski |
0:01f31e923fe2 | 216 | case 4: |
Pawel Zarembski |
0:01f31e923fe2 | 217 | case 6: |
Pawel Zarembski |
0:01f31e923fe2 | 218 | config->Parity = UART_PARITY_NONE; |
Pawel Zarembski |
0:01f31e923fe2 | 219 | break; |
Pawel Zarembski |
0:01f31e923fe2 | 220 | |
Pawel Zarembski |
0:01f31e923fe2 | 221 | case 1: |
Pawel Zarembski |
0:01f31e923fe2 | 222 | config->Parity = UART_PARITY_ODD; |
Pawel Zarembski |
0:01f31e923fe2 | 223 | break; |
Pawel Zarembski |
0:01f31e923fe2 | 224 | |
Pawel Zarembski |
0:01f31e923fe2 | 225 | case 3: |
Pawel Zarembski |
0:01f31e923fe2 | 226 | config->Parity = UART_PARITY_MARK; |
Pawel Zarembski |
0:01f31e923fe2 | 227 | break; |
Pawel Zarembski |
0:01f31e923fe2 | 228 | |
Pawel Zarembski |
0:01f31e923fe2 | 229 | case 5: |
Pawel Zarembski |
0:01f31e923fe2 | 230 | config->Parity = UART_PARITY_EVEN; |
Pawel Zarembski |
0:01f31e923fe2 | 231 | break; |
Pawel Zarembski |
0:01f31e923fe2 | 232 | |
Pawel Zarembski |
0:01f31e923fe2 | 233 | case 7: |
Pawel Zarembski |
0:01f31e923fe2 | 234 | config->Parity = UART_PARITY_SPACE; |
Pawel Zarembski |
0:01f31e923fe2 | 235 | break; |
Pawel Zarembski |
0:01f31e923fe2 | 236 | |
Pawel Zarembski |
0:01f31e923fe2 | 237 | default: |
Pawel Zarembski |
0:01f31e923fe2 | 238 | return 0; |
Pawel Zarembski |
0:01f31e923fe2 | 239 | } |
Pawel Zarembski |
0:01f31e923fe2 | 240 | |
Pawel Zarembski |
0:01f31e923fe2 | 241 | // get stop bits |
Pawel Zarembski |
0:01f31e923fe2 | 242 | switch ((lcr >> 2) & 1) { |
Pawel Zarembski |
0:01f31e923fe2 | 243 | case 0: |
Pawel Zarembski |
0:01f31e923fe2 | 244 | config->StopBits = UART_STOP_BITS_1; |
Pawel Zarembski |
0:01f31e923fe2 | 245 | break; |
Pawel Zarembski |
0:01f31e923fe2 | 246 | |
Pawel Zarembski |
0:01f31e923fe2 | 247 | case 1: |
Pawel Zarembski |
0:01f31e923fe2 | 248 | config->StopBits = UART_STOP_BITS_2; |
Pawel Zarembski |
0:01f31e923fe2 | 249 | break; |
Pawel Zarembski |
0:01f31e923fe2 | 250 | |
Pawel Zarembski |
0:01f31e923fe2 | 251 | default: |
Pawel Zarembski |
0:01f31e923fe2 | 252 | return 0; |
Pawel Zarembski |
0:01f31e923fe2 | 253 | } |
Pawel Zarembski |
0:01f31e923fe2 | 254 | |
Pawel Zarembski |
0:01f31e923fe2 | 255 | // get flow control |
Pawel Zarembski |
0:01f31e923fe2 | 256 | config->FlowControl = UART_FLOW_CONTROL_NONE; |
Pawel Zarembski |
0:01f31e923fe2 | 257 | return 1; |
Pawel Zarembski |
0:01f31e923fe2 | 258 | } |
Pawel Zarembski |
0:01f31e923fe2 | 259 | |
Pawel Zarembski |
0:01f31e923fe2 | 260 | int32_t uart_write_free(void) |
Pawel Zarembski |
0:01f31e923fe2 | 261 | { |
Pawel Zarembski |
0:01f31e923fe2 | 262 | return circ_buf_count_free(&write_buffer); |
Pawel Zarembski |
0:01f31e923fe2 | 263 | } |
Pawel Zarembski |
0:01f31e923fe2 | 264 | |
Pawel Zarembski |
0:01f31e923fe2 | 265 | int32_t uart_write_data(uint8_t *data, uint16_t size) |
Pawel Zarembski |
0:01f31e923fe2 | 266 | { |
Pawel Zarembski |
0:01f31e923fe2 | 267 | uint32_t cnt; |
Pawel Zarembski |
0:01f31e923fe2 | 268 | |
Pawel Zarembski |
0:01f31e923fe2 | 269 | cnt = circ_buf_write(&write_buffer, data, size); |
Pawel Zarembski |
0:01f31e923fe2 | 270 | |
Pawel Zarembski |
0:01f31e923fe2 | 271 | // Make sure that the target LPC1549 can receive the output |
Pawel Zarembski |
0:01f31e923fe2 | 272 | LPC_GPIO_PORT->SET[PORT_UARTCTRL] = PIN_UARTCTRL; |
Pawel Zarembski |
0:01f31e923fe2 | 273 | |
Pawel Zarembski |
0:01f31e923fe2 | 274 | // enable THRE interrupt |
Pawel Zarembski |
0:01f31e923fe2 | 275 | LPC_USART->IER |= (1 << 1); |
Pawel Zarembski |
0:01f31e923fe2 | 276 | |
Pawel Zarembski |
0:01f31e923fe2 | 277 | if (!tx_in_progress) { |
Pawel Zarembski |
0:01f31e923fe2 | 278 | // force THRE interrupt to start |
Pawel Zarembski |
0:01f31e923fe2 | 279 | NVIC_SetPendingIRQ(UART_IRQn); |
Pawel Zarembski |
0:01f31e923fe2 | 280 | } |
Pawel Zarembski |
0:01f31e923fe2 | 281 | |
Pawel Zarembski |
0:01f31e923fe2 | 282 | return cnt; |
Pawel Zarembski |
0:01f31e923fe2 | 283 | } |
Pawel Zarembski |
0:01f31e923fe2 | 284 | |
Pawel Zarembski |
0:01f31e923fe2 | 285 | |
Pawel Zarembski |
0:01f31e923fe2 | 286 | int32_t uart_read_data(uint8_t *data, uint16_t size) |
Pawel Zarembski |
0:01f31e923fe2 | 287 | { |
Pawel Zarembski |
0:01f31e923fe2 | 288 | return circ_buf_read(&read_buffer, data, size); |
Pawel Zarembski |
0:01f31e923fe2 | 289 | } |
Pawel Zarembski |
0:01f31e923fe2 | 290 | |
Pawel Zarembski |
0:01f31e923fe2 | 291 | void uart_enable_flow_control(bool enabled) |
Pawel Zarembski |
0:01f31e923fe2 | 292 | { |
Pawel Zarembski |
0:01f31e923fe2 | 293 | // Flow control not implemented for this platform |
Pawel Zarembski |
0:01f31e923fe2 | 294 | } |
Pawel Zarembski |
0:01f31e923fe2 | 295 | |
Pawel Zarembski |
0:01f31e923fe2 | 296 | void UART_IRQHandler(void) |
Pawel Zarembski |
0:01f31e923fe2 | 297 | { |
Pawel Zarembski |
0:01f31e923fe2 | 298 | uint32_t iir; |
Pawel Zarembski |
0:01f31e923fe2 | 299 | // read interrupt status |
Pawel Zarembski |
0:01f31e923fe2 | 300 | iir = LPC_USART->IIR; |
Pawel Zarembski |
0:01f31e923fe2 | 301 | |
Pawel Zarembski |
0:01f31e923fe2 | 302 | // handle character to transmit |
Pawel Zarembski |
0:01f31e923fe2 | 303 | if (circ_buf_count_used(&write_buffer) > 0) { |
Pawel Zarembski |
0:01f31e923fe2 | 304 | // if THR is empty |
Pawel Zarembski |
0:01f31e923fe2 | 305 | if (LPC_USART->LSR & (1 << 5)) { |
Pawel Zarembski |
0:01f31e923fe2 | 306 | LPC_USART->THR = circ_buf_pop(&write_buffer); |
Pawel Zarembski |
0:01f31e923fe2 | 307 | tx_in_progress = 1; |
Pawel Zarembski |
0:01f31e923fe2 | 308 | } |
Pawel Zarembski |
0:01f31e923fe2 | 309 | |
Pawel Zarembski |
0:01f31e923fe2 | 310 | } else if (tx_in_progress) { |
Pawel Zarembski |
0:01f31e923fe2 | 311 | tx_in_progress = 0; |
Pawel Zarembski |
0:01f31e923fe2 | 312 | // Turn back input for the target LPC1549 to it's pinlist |
Pawel Zarembski |
0:01f31e923fe2 | 313 | LPC_GPIO_PORT->CLR[PORT_UARTCTRL] = PIN_UARTCTRL; |
Pawel Zarembski |
0:01f31e923fe2 | 314 | // disable THRE interrupt |
Pawel Zarembski |
0:01f31e923fe2 | 315 | LPC_USART->IER &= ~(1 << 1); |
Pawel Zarembski |
0:01f31e923fe2 | 316 | } |
Pawel Zarembski |
0:01f31e923fe2 | 317 | |
Pawel Zarembski |
0:01f31e923fe2 | 318 | // handle received character |
Pawel Zarembski |
0:01f31e923fe2 | 319 | if (((iir & 0x0E) == 0x04) || // Rx interrupt (RDA) |
Pawel Zarembski |
0:01f31e923fe2 | 320 | ((iir & 0x0E) == 0x0C)) { // Rx interrupt (CTI) |
Pawel Zarembski |
0:01f31e923fe2 | 321 | while (LPC_USART->LSR & 0x01) { |
Pawel Zarembski |
0:01f31e923fe2 | 322 | uint32_t free; |
Pawel Zarembski |
0:01f31e923fe2 | 323 | uint8_t data; |
Pawel Zarembski |
0:01f31e923fe2 | 324 | |
Pawel Zarembski |
0:01f31e923fe2 | 325 | data = LPC_USART->RBR; |
Pawel Zarembski |
0:01f31e923fe2 | 326 | free = circ_buf_count_free(&read_buffer); |
Pawel Zarembski |
0:01f31e923fe2 | 327 | if (free > RX_OVRF_MSG_SIZE) { |
Pawel Zarembski |
0:01f31e923fe2 | 328 | circ_buf_push(&read_buffer, data); |
Pawel Zarembski |
0:01f31e923fe2 | 329 | } else if (config_get_overflow_detect()) { |
Pawel Zarembski |
0:01f31e923fe2 | 330 | if (RX_OVRF_MSG_SIZE == free) { |
Pawel Zarembski |
0:01f31e923fe2 | 331 | circ_buf_write(&read_buffer, (uint8_t*)RX_OVRF_MSG, RX_OVRF_MSG_SIZE); |
Pawel Zarembski |
0:01f31e923fe2 | 332 | } else { |
Pawel Zarembski |
0:01f31e923fe2 | 333 | // Drop newest |
Pawel Zarembski |
0:01f31e923fe2 | 334 | } |
Pawel Zarembski |
0:01f31e923fe2 | 335 | } else { |
Pawel Zarembski |
0:01f31e923fe2 | 336 | // Drop oldest |
Pawel Zarembski |
0:01f31e923fe2 | 337 | circ_buf_pop(&read_buffer); |
Pawel Zarembski |
0:01f31e923fe2 | 338 | circ_buf_push(&read_buffer, data); |
Pawel Zarembski |
0:01f31e923fe2 | 339 | } |
Pawel Zarembski |
0:01f31e923fe2 | 340 | } |
Pawel Zarembski |
0:01f31e923fe2 | 341 | } |
Pawel Zarembski |
0:01f31e923fe2 | 342 | |
Pawel Zarembski |
0:01f31e923fe2 | 343 | LPC_USART->LSR; |
Pawel Zarembski |
0:01f31e923fe2 | 344 | } |
Pawel Zarembski |
0:01f31e923fe2 | 345 | |
Pawel Zarembski |
0:01f31e923fe2 | 346 | static int32_t reset(void) |
Pawel Zarembski |
0:01f31e923fe2 | 347 | { |
Pawel Zarembski |
0:01f31e923fe2 | 348 | // Reset FIFOs |
Pawel Zarembski |
0:01f31e923fe2 | 349 | LPC_USART->FCR = 0x06; |
Pawel Zarembski |
0:01f31e923fe2 | 350 | baudrate = 0; |
Pawel Zarembski |
0:01f31e923fe2 | 351 | dll = 0; |
Pawel Zarembski |
0:01f31e923fe2 | 352 | tx_in_progress = 0; |
Pawel Zarembski |
0:01f31e923fe2 | 353 | |
Pawel Zarembski |
0:01f31e923fe2 | 354 | circ_buf_init(&write_buffer, write_buffer_data, sizeof(write_buffer_data)); |
Pawel Zarembski |
0:01f31e923fe2 | 355 | circ_buf_init(&read_buffer, read_buffer_data, sizeof(read_buffer_data)); |
Pawel Zarembski |
0:01f31e923fe2 | 356 | |
Pawel Zarembski |
0:01f31e923fe2 | 357 | // Ensure a clean start, no data in either TX or RX FIFO |
Pawel Zarembski |
0:01f31e923fe2 | 358 | while ((LPC_USART->LSR & ((1 << 5) | (1 << 6))) != ((1 << 5) | (1 << 6))); |
Pawel Zarembski |
0:01f31e923fe2 | 359 | |
Pawel Zarembski |
0:01f31e923fe2 | 360 | while (LPC_USART->LSR & 0x01) { |
Pawel Zarembski |
0:01f31e923fe2 | 361 | LPC_USART->RBR; // Dump data from RX FIFO |
Pawel Zarembski |
0:01f31e923fe2 | 362 | } |
Pawel Zarembski |
0:01f31e923fe2 | 363 | |
Pawel Zarembski |
0:01f31e923fe2 | 364 | return 1; |
Pawel Zarembski |
0:01f31e923fe2 | 365 | } |