forked
targets/TARGET_Realtek/TARGET_AMEBA/serial_api.c@167:e84263d55307, 2017-06-21 (annotated)
- Committer:
- AnnaBridge
- Date:
- Wed Jun 21 17:46:44 2017 +0100
- Revision:
- 167:e84263d55307
This updates the lib to the mbed lib v 145
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AnnaBridge | 167:e84263d55307 | 1 | /* mbed Microcontroller Library |
AnnaBridge | 167:e84263d55307 | 2 | * Copyright (c) 2013-2016 Realtek Semiconductor Corp. |
AnnaBridge | 167:e84263d55307 | 3 | * |
AnnaBridge | 167:e84263d55307 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
AnnaBridge | 167:e84263d55307 | 5 | * you may not use this file except in compliance with the License. |
AnnaBridge | 167:e84263d55307 | 6 | * You may obtain a copy of the License at |
AnnaBridge | 167:e84263d55307 | 7 | * |
AnnaBridge | 167:e84263d55307 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
AnnaBridge | 167:e84263d55307 | 9 | * |
AnnaBridge | 167:e84263d55307 | 10 | * Unless required by applicable law or agreed to in writing, software |
AnnaBridge | 167:e84263d55307 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
AnnaBridge | 167:e84263d55307 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
AnnaBridge | 167:e84263d55307 | 13 | * See the License for the specific language governing permissions and |
AnnaBridge | 167:e84263d55307 | 14 | * limitations under the License. |
AnnaBridge | 167:e84263d55307 | 15 | */ |
AnnaBridge | 167:e84263d55307 | 16 | |
AnnaBridge | 167:e84263d55307 | 17 | #include "rtl8195a.h" |
AnnaBridge | 167:e84263d55307 | 18 | #include "objects.h" |
AnnaBridge | 167:e84263d55307 | 19 | #include "serial_api.h" |
AnnaBridge | 167:e84263d55307 | 20 | #if CONFIG_UART_EN |
AnnaBridge | 167:e84263d55307 | 21 | |
AnnaBridge | 167:e84263d55307 | 22 | #include "pinmap.h" |
AnnaBridge | 167:e84263d55307 | 23 | #include <string.h> |
AnnaBridge | 167:e84263d55307 | 24 | |
AnnaBridge | 167:e84263d55307 | 25 | static const PinMap PinMap_UART_TX[] = { |
AnnaBridge | 167:e84263d55307 | 26 | {PC_3, RTL_PIN_PERI(UART0, 0, S0), RTL_PIN_FUNC(UART0, S0)}, |
AnnaBridge | 167:e84263d55307 | 27 | {PE_0, RTL_PIN_PERI(UART0, 0, S1), RTL_PIN_FUNC(UART0, S1)}, |
AnnaBridge | 167:e84263d55307 | 28 | {PA_7, RTL_PIN_PERI(UART0, 0, S2), RTL_PIN_FUNC(UART0, S2)}, |
AnnaBridge | 167:e84263d55307 | 29 | {PD_3, RTL_PIN_PERI(UART1, 1, S0), RTL_PIN_FUNC(UART1, S0)}, |
AnnaBridge | 167:e84263d55307 | 30 | {PE_4, RTL_PIN_PERI(UART1, 1, S1), RTL_PIN_FUNC(UART1, S1)}, |
AnnaBridge | 167:e84263d55307 | 31 | {PB_5, RTL_PIN_PERI(UART1, 1, S2), RTL_PIN_FUNC(UART1, S2)}, |
AnnaBridge | 167:e84263d55307 | 32 | {PA_4, RTL_PIN_PERI(UART2, 2, S0), RTL_PIN_FUNC(UART2, S0)}, |
AnnaBridge | 167:e84263d55307 | 33 | {PC_9, RTL_PIN_PERI(UART2, 2, S1), RTL_PIN_FUNC(UART2, S1)}, |
AnnaBridge | 167:e84263d55307 | 34 | {PD_7, RTL_PIN_PERI(UART2, 2, S2), RTL_PIN_FUNC(UART2, S2)}, |
AnnaBridge | 167:e84263d55307 | 35 | {PB_0, RTL_PIN_PERI(LOG_UART, 3, S0), RTL_PIN_FUNC(LOG_UART, S0)}, |
AnnaBridge | 167:e84263d55307 | 36 | {NC, NC, 0} |
AnnaBridge | 167:e84263d55307 | 37 | }; |
AnnaBridge | 167:e84263d55307 | 38 | |
AnnaBridge | 167:e84263d55307 | 39 | static const PinMap PinMap_UART_RX[] = { |
AnnaBridge | 167:e84263d55307 | 40 | {PC_0, RTL_PIN_PERI(UART0, 0, S0), RTL_PIN_FUNC(UART0, S0)}, |
AnnaBridge | 167:e84263d55307 | 41 | {PE_3, RTL_PIN_PERI(UART0, 0, S1), RTL_PIN_FUNC(UART0, S1)}, |
AnnaBridge | 167:e84263d55307 | 42 | {PA_6, RTL_PIN_PERI(UART0, 0, S2), RTL_PIN_FUNC(UART0, S2)}, |
AnnaBridge | 167:e84263d55307 | 43 | {PD_0, RTL_PIN_PERI(UART1, 1, S0), RTL_PIN_FUNC(UART1, S0)}, |
AnnaBridge | 167:e84263d55307 | 44 | {PE_7, RTL_PIN_PERI(UART1, 1, S1), RTL_PIN_FUNC(UART1, S1)}, |
AnnaBridge | 167:e84263d55307 | 45 | {PB_4, RTL_PIN_PERI(UART1, 1, S2), RTL_PIN_FUNC(UART1, S2)}, |
AnnaBridge | 167:e84263d55307 | 46 | {PA_0, RTL_PIN_PERI(UART2, 2, S0), RTL_PIN_FUNC(UART2, S0)}, |
AnnaBridge | 167:e84263d55307 | 47 | {PC_6, RTL_PIN_PERI(UART2, 2, S1), RTL_PIN_FUNC(UART2, S1)}, |
AnnaBridge | 167:e84263d55307 | 48 | {PD_4, RTL_PIN_PERI(UART2, 2, S2), RTL_PIN_FUNC(UART2, S2)}, |
AnnaBridge | 167:e84263d55307 | 49 | {PB_1, RTL_PIN_PERI(LOG_UART, 3, S0), RTL_PIN_FUNC(LOG_UART, S0)}, |
AnnaBridge | 167:e84263d55307 | 50 | {NC, NC, 0} |
AnnaBridge | 167:e84263d55307 | 51 | }; |
AnnaBridge | 167:e84263d55307 | 52 | |
AnnaBridge | 167:e84263d55307 | 53 | #define UART_NUM (3) |
AnnaBridge | 167:e84263d55307 | 54 | #define SERIAL_TX_IRQ_EN 0x01 |
AnnaBridge | 167:e84263d55307 | 55 | #define SERIAL_RX_IRQ_EN 0x02 |
AnnaBridge | 167:e84263d55307 | 56 | #define SERIAL_TX_DMA_EN 0x01 |
AnnaBridge | 167:e84263d55307 | 57 | #define SERIAL_RX_DMA_EN 0x02 |
AnnaBridge | 167:e84263d55307 | 58 | |
AnnaBridge | 167:e84263d55307 | 59 | static uint32_t serial_irq_ids[UART_NUM] = {0, 0, 0}; |
AnnaBridge | 167:e84263d55307 | 60 | |
AnnaBridge | 167:e84263d55307 | 61 | static uart_irq_handler irq_handler[UART_NUM]; |
AnnaBridge | 167:e84263d55307 | 62 | static uint32_t serial_irq_en[UART_NUM]={0, 0, 0}; |
AnnaBridge | 167:e84263d55307 | 63 | |
AnnaBridge | 167:e84263d55307 | 64 | #ifdef CONFIG_GDMA_EN |
AnnaBridge | 167:e84263d55307 | 65 | static uint32_t serial_dma_en[UART_NUM] = {0, 0, 0}; |
AnnaBridge | 167:e84263d55307 | 66 | static HAL_GDMA_OP UartGdmaOp; |
AnnaBridge | 167:e84263d55307 | 67 | #endif |
AnnaBridge | 167:e84263d55307 | 68 | |
AnnaBridge | 167:e84263d55307 | 69 | #ifdef CONFIG_MBED_ENABLED |
AnnaBridge | 167:e84263d55307 | 70 | #include "log_uart_api.h" |
AnnaBridge | 167:e84263d55307 | 71 | int stdio_uart_inited = 0; |
AnnaBridge | 167:e84263d55307 | 72 | serial_t stdio_uart; |
AnnaBridge | 167:e84263d55307 | 73 | log_uart_t stdio_uart_log; |
AnnaBridge | 167:e84263d55307 | 74 | #endif |
AnnaBridge | 167:e84263d55307 | 75 | |
AnnaBridge | 167:e84263d55307 | 76 | static void SerialTxDoneCallBack(VOID *pAdapter); |
AnnaBridge | 167:e84263d55307 | 77 | static void SerialRxDoneCallBack(VOID *pAdapter); |
AnnaBridge | 167:e84263d55307 | 78 | |
AnnaBridge | 167:e84263d55307 | 79 | void serial_init(serial_t *obj, PinName tx, PinName rx) |
AnnaBridge | 167:e84263d55307 | 80 | { |
AnnaBridge | 167:e84263d55307 | 81 | uint32_t uart_tx, uart_rx; |
AnnaBridge | 167:e84263d55307 | 82 | uint32_t uart_sel; |
AnnaBridge | 167:e84263d55307 | 83 | uint8_t uart_idx; |
AnnaBridge | 167:e84263d55307 | 84 | PHAL_RUART_OP pHalRuartOp; |
AnnaBridge | 167:e84263d55307 | 85 | PHAL_RUART_ADAPTER pHalRuartAdapter; |
AnnaBridge | 167:e84263d55307 | 86 | #ifdef CONFIG_GDMA_EN |
AnnaBridge | 167:e84263d55307 | 87 | PUART_DMA_CONFIG pHalRuartDmaCfg; |
AnnaBridge | 167:e84263d55307 | 88 | PHAL_GDMA_OP pHalGdmaOp=&UartGdmaOp; |
AnnaBridge | 167:e84263d55307 | 89 | #endif |
AnnaBridge | 167:e84263d55307 | 90 | |
AnnaBridge | 167:e84263d55307 | 91 | // Determine the UART to use (UART0, UART1, or UART3) |
AnnaBridge | 167:e84263d55307 | 92 | uart_tx = pinmap_peripheral(tx, PinMap_UART_TX); |
AnnaBridge | 167:e84263d55307 | 93 | uart_rx = pinmap_peripheral(rx, PinMap_UART_RX); |
AnnaBridge | 167:e84263d55307 | 94 | |
AnnaBridge | 167:e84263d55307 | 95 | uart_sel = pinmap_merge(uart_tx, uart_rx); |
AnnaBridge | 167:e84263d55307 | 96 | uart_idx = RTL_GET_PERI_IDX(uart_sel); |
AnnaBridge | 167:e84263d55307 | 97 | if (unlikely(uart_idx == (uint8_t)NC)) { |
AnnaBridge | 167:e84263d55307 | 98 | DBG_UART_ERR("%s: Cannot find matched UART\n", __FUNCTION__); |
AnnaBridge | 167:e84263d55307 | 99 | return; |
AnnaBridge | 167:e84263d55307 | 100 | } |
AnnaBridge | 167:e84263d55307 | 101 | #ifdef CONFIG_MBED_ENABLED |
AnnaBridge | 167:e84263d55307 | 102 | else if(uart_idx == UART_3){ |
AnnaBridge | 167:e84263d55307 | 103 | obj->index = UART_3; |
AnnaBridge | 167:e84263d55307 | 104 | goto init_stdio; |
AnnaBridge | 167:e84263d55307 | 105 | } |
AnnaBridge | 167:e84263d55307 | 106 | #endif |
AnnaBridge | 167:e84263d55307 | 107 | |
AnnaBridge | 167:e84263d55307 | 108 | pHalRuartOp = &(obj->hal_uart_op); |
AnnaBridge | 167:e84263d55307 | 109 | pHalRuartAdapter = &(obj->hal_uart_adp); |
AnnaBridge | 167:e84263d55307 | 110 | |
AnnaBridge | 167:e84263d55307 | 111 | if ((NULL == pHalRuartOp) || (NULL == pHalRuartAdapter)) { |
AnnaBridge | 167:e84263d55307 | 112 | DBG_UART_ERR("%s: Allocate Adapter Failed\n", __FUNCTION__); |
AnnaBridge | 167:e84263d55307 | 113 | return; |
AnnaBridge | 167:e84263d55307 | 114 | } |
AnnaBridge | 167:e84263d55307 | 115 | |
AnnaBridge | 167:e84263d55307 | 116 | HalRuartOpInit((VOID*)pHalRuartOp); |
AnnaBridge | 167:e84263d55307 | 117 | |
AnnaBridge | 167:e84263d55307 | 118 | #ifdef CONFIG_GDMA_EN |
AnnaBridge | 167:e84263d55307 | 119 | HalGdmaOpInit((VOID*)pHalGdmaOp); |
AnnaBridge | 167:e84263d55307 | 120 | pHalRuartDmaCfg = &obj->uart_gdma_cfg; |
AnnaBridge | 167:e84263d55307 | 121 | pHalRuartDmaCfg->pHalGdmaOp = pHalGdmaOp; |
AnnaBridge | 167:e84263d55307 | 122 | pHalRuartDmaCfg->pTxHalGdmaAdapter = &obj->uart_gdma_adp_tx; |
AnnaBridge | 167:e84263d55307 | 123 | pHalRuartDmaCfg->pRxHalGdmaAdapter = &obj->uart_gdma_adp_rx; |
AnnaBridge | 167:e84263d55307 | 124 | pHalRuartDmaCfg->pTxDmaBlkList = &obj->gdma_multiblk_list_tx; |
AnnaBridge | 167:e84263d55307 | 125 | pHalRuartDmaCfg->pRxDmaBlkList = &obj->gdma_multiblk_list_rx; |
AnnaBridge | 167:e84263d55307 | 126 | _memset((void*)(pHalRuartDmaCfg->pTxHalGdmaAdapter), 0, sizeof(HAL_GDMA_ADAPTER)); |
AnnaBridge | 167:e84263d55307 | 127 | _memset((void*)(pHalRuartDmaCfg->pRxHalGdmaAdapter), 0, sizeof(HAL_GDMA_ADAPTER)); |
AnnaBridge | 167:e84263d55307 | 128 | _memset((void*)(pHalRuartDmaCfg->pTxDmaBlkList), 0, sizeof(UART_DMA_MULTIBLK)); |
AnnaBridge | 167:e84263d55307 | 129 | _memset((void*)(pHalRuartDmaCfg->pRxDmaBlkList), 0, sizeof(UART_DMA_MULTIBLK)); |
AnnaBridge | 167:e84263d55307 | 130 | #endif |
AnnaBridge | 167:e84263d55307 | 131 | |
AnnaBridge | 167:e84263d55307 | 132 | pHalRuartOp->HalRuartAdapterLoadDef(pHalRuartAdapter, uart_idx); |
AnnaBridge | 167:e84263d55307 | 133 | pHalRuartAdapter->PinmuxSelect = RTL_GET_PERI_SEL(uart_sel); |
AnnaBridge | 167:e84263d55307 | 134 | pHalRuartAdapter->BaudRate = 9600; |
AnnaBridge | 167:e84263d55307 | 135 | pHalRuartAdapter->IrqHandle.Priority = 6; |
AnnaBridge | 167:e84263d55307 | 136 | |
AnnaBridge | 167:e84263d55307 | 137 | if (HalRuartInit(pHalRuartAdapter) != HAL_OK) { |
AnnaBridge | 167:e84263d55307 | 138 | DBG_UART_ERR("serial_init Err!\n"); |
AnnaBridge | 167:e84263d55307 | 139 | return; |
AnnaBridge | 167:e84263d55307 | 140 | } |
AnnaBridge | 167:e84263d55307 | 141 | pHalRuartOp->HalRuartRegIrq(pHalRuartAdapter); |
AnnaBridge | 167:e84263d55307 | 142 | pHalRuartOp->HalRuartIntEnable(pHalRuartAdapter); |
AnnaBridge | 167:e84263d55307 | 143 | |
AnnaBridge | 167:e84263d55307 | 144 | #ifdef CONFIG_MBED_ENABLED |
AnnaBridge | 167:e84263d55307 | 145 | init_stdio: |
AnnaBridge | 167:e84263d55307 | 146 | // For stdio management |
AnnaBridge | 167:e84263d55307 | 147 | if (uart_idx == STDIO_UART) { |
AnnaBridge | 167:e84263d55307 | 148 | // default setting to 38400 |
AnnaBridge | 167:e84263d55307 | 149 | if (stdio_uart_inited) return; |
AnnaBridge | 167:e84263d55307 | 150 | log_uart_init(&stdio_uart_log, 38400, 8, ParityNone, 1); |
AnnaBridge | 167:e84263d55307 | 151 | stdio_uart_inited = 1; |
AnnaBridge | 167:e84263d55307 | 152 | memcpy(&stdio_uart, obj, sizeof(serial_t)); |
AnnaBridge | 167:e84263d55307 | 153 | } |
AnnaBridge | 167:e84263d55307 | 154 | #endif |
AnnaBridge | 167:e84263d55307 | 155 | } |
AnnaBridge | 167:e84263d55307 | 156 | |
AnnaBridge | 167:e84263d55307 | 157 | void serial_free(serial_t *obj) |
AnnaBridge | 167:e84263d55307 | 158 | { |
AnnaBridge | 167:e84263d55307 | 159 | PHAL_RUART_ADAPTER pHalRuartAdapter; |
AnnaBridge | 167:e84263d55307 | 160 | #ifdef CONFIG_GDMA_EN |
AnnaBridge | 167:e84263d55307 | 161 | u8 uart_idx; |
AnnaBridge | 167:e84263d55307 | 162 | PUART_DMA_CONFIG pHalRuartDmaCfg; |
AnnaBridge | 167:e84263d55307 | 163 | #endif |
AnnaBridge | 167:e84263d55307 | 164 | #ifdef CONFIG_MBED_ENABLED |
AnnaBridge | 167:e84263d55307 | 165 | if(obj->index == UART_3){ |
AnnaBridge | 167:e84263d55307 | 166 | log_uart_free(&stdio_uart_log); |
AnnaBridge | 167:e84263d55307 | 167 | return; |
AnnaBridge | 167:e84263d55307 | 168 | } |
AnnaBridge | 167:e84263d55307 | 169 | #endif |
AnnaBridge | 167:e84263d55307 | 170 | pHalRuartAdapter = &(obj->hal_uart_adp); |
AnnaBridge | 167:e84263d55307 | 171 | |
AnnaBridge | 167:e84263d55307 | 172 | HalRuartDeInit(pHalRuartAdapter); |
AnnaBridge | 167:e84263d55307 | 173 | |
AnnaBridge | 167:e84263d55307 | 174 | #ifdef CONFIG_GDMA_EN |
AnnaBridge | 167:e84263d55307 | 175 | uart_idx = pHalRuartAdapter->UartIndex; |
AnnaBridge | 167:e84263d55307 | 176 | pHalRuartDmaCfg = &obj->uart_gdma_cfg; |
AnnaBridge | 167:e84263d55307 | 177 | if (serial_dma_en[uart_idx] & SERIAL_RX_DMA_EN) { |
AnnaBridge | 167:e84263d55307 | 178 | HalRuartRxGdmaDeInit(pHalRuartDmaCfg); |
AnnaBridge | 167:e84263d55307 | 179 | serial_dma_en[uart_idx] &= ~SERIAL_RX_DMA_EN; |
AnnaBridge | 167:e84263d55307 | 180 | } |
AnnaBridge | 167:e84263d55307 | 181 | |
AnnaBridge | 167:e84263d55307 | 182 | if (serial_dma_en[uart_idx] & SERIAL_TX_DMA_EN) { |
AnnaBridge | 167:e84263d55307 | 183 | HalRuartTxGdmaDeInit(pHalRuartDmaCfg); |
AnnaBridge | 167:e84263d55307 | 184 | serial_dma_en[uart_idx] &= ~SERIAL_TX_DMA_EN; |
AnnaBridge | 167:e84263d55307 | 185 | } |
AnnaBridge | 167:e84263d55307 | 186 | #endif |
AnnaBridge | 167:e84263d55307 | 187 | } |
AnnaBridge | 167:e84263d55307 | 188 | |
AnnaBridge | 167:e84263d55307 | 189 | void serial_baud(serial_t *obj, int baudrate) |
AnnaBridge | 167:e84263d55307 | 190 | { |
AnnaBridge | 167:e84263d55307 | 191 | #ifdef CONFIG_MBED_ENABLED |
AnnaBridge | 167:e84263d55307 | 192 | if(obj->index == UART_3){ |
AnnaBridge | 167:e84263d55307 | 193 | return; |
AnnaBridge | 167:e84263d55307 | 194 | } |
AnnaBridge | 167:e84263d55307 | 195 | #endif |
AnnaBridge | 167:e84263d55307 | 196 | PHAL_RUART_ADAPTER pHalRuartAdapter; |
AnnaBridge | 167:e84263d55307 | 197 | |
AnnaBridge | 167:e84263d55307 | 198 | pHalRuartAdapter = &(obj->hal_uart_adp); |
AnnaBridge | 167:e84263d55307 | 199 | |
AnnaBridge | 167:e84263d55307 | 200 | pHalRuartAdapter->BaudRate = baudrate; |
AnnaBridge | 167:e84263d55307 | 201 | HalRuartSetBaudRate((VOID*)pHalRuartAdapter); |
AnnaBridge | 167:e84263d55307 | 202 | } |
AnnaBridge | 167:e84263d55307 | 203 | |
AnnaBridge | 167:e84263d55307 | 204 | void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) |
AnnaBridge | 167:e84263d55307 | 205 | { |
AnnaBridge | 167:e84263d55307 | 206 | #ifdef CONFIG_MBED_ENABLED |
AnnaBridge | 167:e84263d55307 | 207 | if(obj->index == UART_3){ |
AnnaBridge | 167:e84263d55307 | 208 | log_uart_format(&stdio_uart_log, data_bits, parity, stop_bits); |
AnnaBridge | 167:e84263d55307 | 209 | return; |
AnnaBridge | 167:e84263d55307 | 210 | } |
AnnaBridge | 167:e84263d55307 | 211 | #endif |
AnnaBridge | 167:e84263d55307 | 212 | PHAL_RUART_ADAPTER pHalRuartAdapter; |
AnnaBridge | 167:e84263d55307 | 213 | pHalRuartAdapter = &(obj->hal_uart_adp); |
AnnaBridge | 167:e84263d55307 | 214 | |
AnnaBridge | 167:e84263d55307 | 215 | if (data_bits == 8) { |
AnnaBridge | 167:e84263d55307 | 216 | pHalRuartAdapter->WordLen = RUART_WLS_8BITS; |
AnnaBridge | 167:e84263d55307 | 217 | } else { |
AnnaBridge | 167:e84263d55307 | 218 | pHalRuartAdapter->WordLen = RUART_WLS_7BITS; |
AnnaBridge | 167:e84263d55307 | 219 | } |
AnnaBridge | 167:e84263d55307 | 220 | |
AnnaBridge | 167:e84263d55307 | 221 | |
AnnaBridge | 167:e84263d55307 | 222 | switch (parity) { |
AnnaBridge | 167:e84263d55307 | 223 | case ParityOdd: |
AnnaBridge | 167:e84263d55307 | 224 | case ParityForced0: |
AnnaBridge | 167:e84263d55307 | 225 | pHalRuartAdapter->Parity = RUART_PARITY_ENABLE; |
AnnaBridge | 167:e84263d55307 | 226 | pHalRuartAdapter->ParityType = RUART_ODD_PARITY; |
AnnaBridge | 167:e84263d55307 | 227 | break; |
AnnaBridge | 167:e84263d55307 | 228 | case ParityEven: |
AnnaBridge | 167:e84263d55307 | 229 | case ParityForced1: |
AnnaBridge | 167:e84263d55307 | 230 | pHalRuartAdapter->Parity = RUART_PARITY_ENABLE; |
AnnaBridge | 167:e84263d55307 | 231 | pHalRuartAdapter->ParityType = RUART_EVEN_PARITY; |
AnnaBridge | 167:e84263d55307 | 232 | break; |
AnnaBridge | 167:e84263d55307 | 233 | default: // ParityNone |
AnnaBridge | 167:e84263d55307 | 234 | pHalRuartAdapter->Parity = RUART_PARITY_DISABLE; |
AnnaBridge | 167:e84263d55307 | 235 | break; |
AnnaBridge | 167:e84263d55307 | 236 | } |
AnnaBridge | 167:e84263d55307 | 237 | |
AnnaBridge | 167:e84263d55307 | 238 | if (stop_bits == 2) { |
AnnaBridge | 167:e84263d55307 | 239 | pHalRuartAdapter->StopBit = RUART_STOP_BIT_2; |
AnnaBridge | 167:e84263d55307 | 240 | } else { |
AnnaBridge | 167:e84263d55307 | 241 | pHalRuartAdapter->StopBit = RUART_STOP_BIT_1; |
AnnaBridge | 167:e84263d55307 | 242 | } |
AnnaBridge | 167:e84263d55307 | 243 | |
AnnaBridge | 167:e84263d55307 | 244 | HalRuartInit(pHalRuartAdapter); |
AnnaBridge | 167:e84263d55307 | 245 | } |
AnnaBridge | 167:e84263d55307 | 246 | |
AnnaBridge | 167:e84263d55307 | 247 | /****************************************************************************** |
AnnaBridge | 167:e84263d55307 | 248 | * INTERRUPTS HANDLING |
AnnaBridge | 167:e84263d55307 | 249 | ******************************************************************************/ |
AnnaBridge | 167:e84263d55307 | 250 | static void SerialTxDoneCallBack(VOID *pAdapter) |
AnnaBridge | 167:e84263d55307 | 251 | { |
AnnaBridge | 167:e84263d55307 | 252 | PHAL_RUART_ADAPTER pHalRuartAdapter = pAdapter; |
AnnaBridge | 167:e84263d55307 | 253 | u8 uart_idx = pHalRuartAdapter->UartIndex; |
AnnaBridge | 167:e84263d55307 | 254 | |
AnnaBridge | 167:e84263d55307 | 255 | // Mask UART TX FIFO empty |
AnnaBridge | 167:e84263d55307 | 256 | pHalRuartAdapter->Interrupts &= ~RUART_IER_ETBEI; |
AnnaBridge | 167:e84263d55307 | 257 | HalRuartSetIMRRtl8195a (pHalRuartAdapter); |
AnnaBridge | 167:e84263d55307 | 258 | |
AnnaBridge | 167:e84263d55307 | 259 | if (irq_handler[uart_idx] != NULL) { |
AnnaBridge | 167:e84263d55307 | 260 | irq_handler[uart_idx](serial_irq_ids[uart_idx], TxIrq); |
AnnaBridge | 167:e84263d55307 | 261 | } |
AnnaBridge | 167:e84263d55307 | 262 | } |
AnnaBridge | 167:e84263d55307 | 263 | |
AnnaBridge | 167:e84263d55307 | 264 | static void SerialRxDoneCallBack(VOID *pAdapter) |
AnnaBridge | 167:e84263d55307 | 265 | { |
AnnaBridge | 167:e84263d55307 | 266 | PHAL_RUART_ADAPTER pHalRuartAdapter = pAdapter; |
AnnaBridge | 167:e84263d55307 | 267 | u8 uart_idx = pHalRuartAdapter->UartIndex; |
AnnaBridge | 167:e84263d55307 | 268 | |
AnnaBridge | 167:e84263d55307 | 269 | if (irq_handler[uart_idx] != NULL) { |
AnnaBridge | 167:e84263d55307 | 270 | irq_handler[uart_idx](serial_irq_ids[uart_idx], RxIrq); |
AnnaBridge | 167:e84263d55307 | 271 | } |
AnnaBridge | 167:e84263d55307 | 272 | } |
AnnaBridge | 167:e84263d55307 | 273 | |
AnnaBridge | 167:e84263d55307 | 274 | void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id) |
AnnaBridge | 167:e84263d55307 | 275 | { |
AnnaBridge | 167:e84263d55307 | 276 | PHAL_RUART_ADAPTER pHalRuartAdapter; |
AnnaBridge | 167:e84263d55307 | 277 | u8 uart_idx; |
AnnaBridge | 167:e84263d55307 | 278 | |
AnnaBridge | 167:e84263d55307 | 279 | pHalRuartAdapter = &(obj->hal_uart_adp); |
AnnaBridge | 167:e84263d55307 | 280 | uart_idx = pHalRuartAdapter->UartIndex; |
AnnaBridge | 167:e84263d55307 | 281 | |
AnnaBridge | 167:e84263d55307 | 282 | irq_handler[uart_idx] = handler; |
AnnaBridge | 167:e84263d55307 | 283 | serial_irq_ids[uart_idx] = id; |
AnnaBridge | 167:e84263d55307 | 284 | |
AnnaBridge | 167:e84263d55307 | 285 | pHalRuartAdapter->TxTDCallback = SerialTxDoneCallBack; |
AnnaBridge | 167:e84263d55307 | 286 | pHalRuartAdapter->TxTDCbPara = (void*)pHalRuartAdapter; |
AnnaBridge | 167:e84263d55307 | 287 | pHalRuartAdapter->RxDRCallback = SerialRxDoneCallBack; |
AnnaBridge | 167:e84263d55307 | 288 | pHalRuartAdapter->RxDRCbPara = (void*)pHalRuartAdapter; |
AnnaBridge | 167:e84263d55307 | 289 | } |
AnnaBridge | 167:e84263d55307 | 290 | |
AnnaBridge | 167:e84263d55307 | 291 | |
AnnaBridge | 167:e84263d55307 | 292 | void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) |
AnnaBridge | 167:e84263d55307 | 293 | { |
AnnaBridge | 167:e84263d55307 | 294 | PHAL_RUART_ADAPTER pHalRuartAdapter; |
AnnaBridge | 167:e84263d55307 | 295 | PHAL_RUART_OP pHalRuartOp; |
AnnaBridge | 167:e84263d55307 | 296 | u8 uart_idx; |
AnnaBridge | 167:e84263d55307 | 297 | |
AnnaBridge | 167:e84263d55307 | 298 | pHalRuartAdapter = &(obj->hal_uart_adp); |
AnnaBridge | 167:e84263d55307 | 299 | pHalRuartOp = &(obj->hal_uart_op); |
AnnaBridge | 167:e84263d55307 | 300 | uart_idx = pHalRuartAdapter->UartIndex; |
AnnaBridge | 167:e84263d55307 | 301 | |
AnnaBridge | 167:e84263d55307 | 302 | if (enable) { |
AnnaBridge | 167:e84263d55307 | 303 | if (irq == RxIrq) { |
AnnaBridge | 167:e84263d55307 | 304 | pHalRuartAdapter->Interrupts |= RUART_IER_ERBI | RUART_IER_ELSI; |
AnnaBridge | 167:e84263d55307 | 305 | serial_irq_en[uart_idx] |= SERIAL_RX_IRQ_EN; |
AnnaBridge | 167:e84263d55307 | 306 | HalRuartSetIMRRtl8195a (pHalRuartAdapter); |
AnnaBridge | 167:e84263d55307 | 307 | } else { |
AnnaBridge | 167:e84263d55307 | 308 | serial_irq_en[uart_idx] |= SERIAL_TX_IRQ_EN; |
AnnaBridge | 167:e84263d55307 | 309 | } |
AnnaBridge | 167:e84263d55307 | 310 | pHalRuartOp->HalRuartRegIrq(pHalRuartAdapter); |
AnnaBridge | 167:e84263d55307 | 311 | pHalRuartOp->HalRuartIntEnable(pHalRuartAdapter); |
AnnaBridge | 167:e84263d55307 | 312 | } else { // disable |
AnnaBridge | 167:e84263d55307 | 313 | if (irq == RxIrq) { |
AnnaBridge | 167:e84263d55307 | 314 | pHalRuartAdapter->Interrupts &= ~(RUART_IER_ERBI | RUART_IER_ELSI); |
AnnaBridge | 167:e84263d55307 | 315 | serial_irq_en[uart_idx] &= ~SERIAL_RX_IRQ_EN; |
AnnaBridge | 167:e84263d55307 | 316 | } else { |
AnnaBridge | 167:e84263d55307 | 317 | pHalRuartAdapter->Interrupts &= ~RUART_IER_ETBEI; |
AnnaBridge | 167:e84263d55307 | 318 | serial_irq_en[uart_idx] &= ~SERIAL_TX_IRQ_EN; |
AnnaBridge | 167:e84263d55307 | 319 | } |
AnnaBridge | 167:e84263d55307 | 320 | HalRuartSetIMRRtl8195a (pHalRuartAdapter); |
AnnaBridge | 167:e84263d55307 | 321 | if (pHalRuartAdapter->Interrupts == 0) { |
AnnaBridge | 167:e84263d55307 | 322 | InterruptUnRegister(&pHalRuartAdapter->IrqHandle); |
AnnaBridge | 167:e84263d55307 | 323 | InterruptDis(&pHalRuartAdapter->IrqHandle); |
AnnaBridge | 167:e84263d55307 | 324 | } |
AnnaBridge | 167:e84263d55307 | 325 | } |
AnnaBridge | 167:e84263d55307 | 326 | } |
AnnaBridge | 167:e84263d55307 | 327 | |
AnnaBridge | 167:e84263d55307 | 328 | /****************************************************************************** |
AnnaBridge | 167:e84263d55307 | 329 | * READ/WRITE |
AnnaBridge | 167:e84263d55307 | 330 | ******************************************************************************/ |
AnnaBridge | 167:e84263d55307 | 331 | |
AnnaBridge | 167:e84263d55307 | 332 | int serial_getc(serial_t *obj) |
AnnaBridge | 167:e84263d55307 | 333 | { |
AnnaBridge | 167:e84263d55307 | 334 | #ifdef CONFIG_MBED_ENABLED |
AnnaBridge | 167:e84263d55307 | 335 | if(obj->index == UART_3){ |
AnnaBridge | 167:e84263d55307 | 336 | return log_uart_getc(&stdio_uart_log); |
AnnaBridge | 167:e84263d55307 | 337 | } |
AnnaBridge | 167:e84263d55307 | 338 | #endif |
AnnaBridge | 167:e84263d55307 | 339 | PHAL_RUART_ADAPTER pHalRuartAdapter=(PHAL_RUART_ADAPTER)&(obj->hal_uart_adp); |
AnnaBridge | 167:e84263d55307 | 340 | u8 uart_idx = pHalRuartAdapter->UartIndex; |
AnnaBridge | 167:e84263d55307 | 341 | |
AnnaBridge | 167:e84263d55307 | 342 | while (!serial_readable(obj)); |
AnnaBridge | 167:e84263d55307 | 343 | return (int)((HAL_RUART_READ32(uart_idx, RUART_REV_BUF_REG_OFF)) & 0xFF); |
AnnaBridge | 167:e84263d55307 | 344 | } |
AnnaBridge | 167:e84263d55307 | 345 | |
AnnaBridge | 167:e84263d55307 | 346 | void serial_putc(serial_t *obj, int c) |
AnnaBridge | 167:e84263d55307 | 347 | { |
AnnaBridge | 167:e84263d55307 | 348 | #ifdef CONFIG_MBED_ENABLED |
AnnaBridge | 167:e84263d55307 | 349 | if(obj->index == UART_3){ |
AnnaBridge | 167:e84263d55307 | 350 | log_uart_putc(&stdio_uart_log, (char)c); |
AnnaBridge | 167:e84263d55307 | 351 | return; |
AnnaBridge | 167:e84263d55307 | 352 | } |
AnnaBridge | 167:e84263d55307 | 353 | #endif |
AnnaBridge | 167:e84263d55307 | 354 | PHAL_RUART_ADAPTER pHalRuartAdapter=(PHAL_RUART_ADAPTER)&(obj->hal_uart_adp); |
AnnaBridge | 167:e84263d55307 | 355 | u8 uart_idx = pHalRuartAdapter->UartIndex; |
AnnaBridge | 167:e84263d55307 | 356 | |
AnnaBridge | 167:e84263d55307 | 357 | while (!serial_writable(obj)); |
AnnaBridge | 167:e84263d55307 | 358 | HAL_RUART_WRITE32(uart_idx, RUART_TRAN_HOLD_REG_OFF, (c & 0xFF)); |
AnnaBridge | 167:e84263d55307 | 359 | |
AnnaBridge | 167:e84263d55307 | 360 | if (serial_irq_en[uart_idx] & SERIAL_TX_IRQ_EN) { |
AnnaBridge | 167:e84263d55307 | 361 | // UnMask TX FIFO empty IRQ |
AnnaBridge | 167:e84263d55307 | 362 | pHalRuartAdapter->Interrupts |= RUART_IER_ETBEI; |
AnnaBridge | 167:e84263d55307 | 363 | HalRuartSetIMRRtl8195a (pHalRuartAdapter); |
AnnaBridge | 167:e84263d55307 | 364 | } |
AnnaBridge | 167:e84263d55307 | 365 | } |
AnnaBridge | 167:e84263d55307 | 366 | |
AnnaBridge | 167:e84263d55307 | 367 | int serial_readable(serial_t *obj) |
AnnaBridge | 167:e84263d55307 | 368 | { |
AnnaBridge | 167:e84263d55307 | 369 | #ifdef CONFIG_MBED_ENABLED |
AnnaBridge | 167:e84263d55307 | 370 | if(obj->index == UART_3){ |
AnnaBridge | 167:e84263d55307 | 371 | return log_uart_readable(&stdio_uart_log); |
AnnaBridge | 167:e84263d55307 | 372 | } |
AnnaBridge | 167:e84263d55307 | 373 | #endif |
AnnaBridge | 167:e84263d55307 | 374 | |
AnnaBridge | 167:e84263d55307 | 375 | PHAL_RUART_ADAPTER pHalRuartAdapter=(PHAL_RUART_ADAPTER)&(obj->hal_uart_adp); |
AnnaBridge | 167:e84263d55307 | 376 | u8 uart_idx = pHalRuartAdapter->UartIndex; |
AnnaBridge | 167:e84263d55307 | 377 | |
AnnaBridge | 167:e84263d55307 | 378 | if ((HAL_RUART_READ32(uart_idx, RUART_LINE_STATUS_REG_OFF)) & RUART_LINE_STATUS_REG_DR) { |
AnnaBridge | 167:e84263d55307 | 379 | return 1; |
AnnaBridge | 167:e84263d55307 | 380 | } else { |
AnnaBridge | 167:e84263d55307 | 381 | return 0; |
AnnaBridge | 167:e84263d55307 | 382 | } |
AnnaBridge | 167:e84263d55307 | 383 | } |
AnnaBridge | 167:e84263d55307 | 384 | |
AnnaBridge | 167:e84263d55307 | 385 | int serial_writable(serial_t *obj) |
AnnaBridge | 167:e84263d55307 | 386 | { |
AnnaBridge | 167:e84263d55307 | 387 | #ifdef CONFIG_MBED_ENABLED |
AnnaBridge | 167:e84263d55307 | 388 | if(obj->index == UART_3){ |
AnnaBridge | 167:e84263d55307 | 389 | return log_uart_writable(&stdio_uart_log); |
AnnaBridge | 167:e84263d55307 | 390 | } |
AnnaBridge | 167:e84263d55307 | 391 | #endif |
AnnaBridge | 167:e84263d55307 | 392 | |
AnnaBridge | 167:e84263d55307 | 393 | PHAL_RUART_ADAPTER pHalRuartAdapter=(PHAL_RUART_ADAPTER)&(obj->hal_uart_adp); |
AnnaBridge | 167:e84263d55307 | 394 | u8 uart_idx = pHalRuartAdapter->UartIndex; |
AnnaBridge | 167:e84263d55307 | 395 | |
AnnaBridge | 167:e84263d55307 | 396 | if (HAL_RUART_READ32(uart_idx, RUART_LINE_STATUS_REG_OFF) & |
AnnaBridge | 167:e84263d55307 | 397 | (RUART_LINE_STATUS_REG_THRE)) { |
AnnaBridge | 167:e84263d55307 | 398 | return 1; |
AnnaBridge | 167:e84263d55307 | 399 | } else { |
AnnaBridge | 167:e84263d55307 | 400 | return 0; |
AnnaBridge | 167:e84263d55307 | 401 | } |
AnnaBridge | 167:e84263d55307 | 402 | } |
AnnaBridge | 167:e84263d55307 | 403 | |
AnnaBridge | 167:e84263d55307 | 404 | void serial_clear(serial_t *obj) |
AnnaBridge | 167:e84263d55307 | 405 | { |
AnnaBridge | 167:e84263d55307 | 406 | #ifdef CONFIG_MBED_ENABLED |
AnnaBridge | 167:e84263d55307 | 407 | if(obj->index == UART_3){ |
AnnaBridge | 167:e84263d55307 | 408 | log_uart_clear(&stdio_uart_log); |
AnnaBridge | 167:e84263d55307 | 409 | return; |
AnnaBridge | 167:e84263d55307 | 410 | } |
AnnaBridge | 167:e84263d55307 | 411 | #endif |
AnnaBridge | 167:e84263d55307 | 412 | |
AnnaBridge | 167:e84263d55307 | 413 | PHAL_RUART_ADAPTER pHalRuartAdapter; |
AnnaBridge | 167:e84263d55307 | 414 | |
AnnaBridge | 167:e84263d55307 | 415 | pHalRuartAdapter = &(obj->hal_uart_adp); |
AnnaBridge | 167:e84263d55307 | 416 | HalRuartResetTRxFifo((VOID *)pHalRuartAdapter); |
AnnaBridge | 167:e84263d55307 | 417 | } |
AnnaBridge | 167:e84263d55307 | 418 | |
AnnaBridge | 167:e84263d55307 | 419 | void serial_break_set(serial_t *obj) |
AnnaBridge | 167:e84263d55307 | 420 | { |
AnnaBridge | 167:e84263d55307 | 421 | #ifdef CONFIG_MBED_ENABLED |
AnnaBridge | 167:e84263d55307 | 422 | if(obj->index == UART_3){ |
AnnaBridge | 167:e84263d55307 | 423 | log_uart_break_set(&stdio_uart_log); |
AnnaBridge | 167:e84263d55307 | 424 | return; |
AnnaBridge | 167:e84263d55307 | 425 | } |
AnnaBridge | 167:e84263d55307 | 426 | #endif |
AnnaBridge | 167:e84263d55307 | 427 | |
AnnaBridge | 167:e84263d55307 | 428 | PHAL_RUART_ADAPTER pHalRuartAdapter=(PHAL_RUART_ADAPTER)&(obj->hal_uart_adp); |
AnnaBridge | 167:e84263d55307 | 429 | u8 uart_idx = pHalRuartAdapter->UartIndex; |
AnnaBridge | 167:e84263d55307 | 430 | u32 RegValue; |
AnnaBridge | 167:e84263d55307 | 431 | |
AnnaBridge | 167:e84263d55307 | 432 | RegValue = HAL_RUART_READ32(uart_idx, RUART_LINE_CTL_REG_OFF); |
AnnaBridge | 167:e84263d55307 | 433 | RegValue |= BIT_UART_LCR_BREAK_CTRL; |
AnnaBridge | 167:e84263d55307 | 434 | HAL_RUART_WRITE32(uart_idx, RUART_LINE_CTL_REG_OFF, RegValue); |
AnnaBridge | 167:e84263d55307 | 435 | } |
AnnaBridge | 167:e84263d55307 | 436 | |
AnnaBridge | 167:e84263d55307 | 437 | void serial_break_clear(serial_t *obj) |
AnnaBridge | 167:e84263d55307 | 438 | { |
AnnaBridge | 167:e84263d55307 | 439 | #ifdef CONFIG_MBED_ENABLED |
AnnaBridge | 167:e84263d55307 | 440 | if(obj->index == UART_3){ |
AnnaBridge | 167:e84263d55307 | 441 | log_uart_break_clear(&stdio_uart_log); |
AnnaBridge | 167:e84263d55307 | 442 | return; |
AnnaBridge | 167:e84263d55307 | 443 | } |
AnnaBridge | 167:e84263d55307 | 444 | #endif |
AnnaBridge | 167:e84263d55307 | 445 | |
AnnaBridge | 167:e84263d55307 | 446 | PHAL_RUART_ADAPTER pHalRuartAdapter=(PHAL_RUART_ADAPTER)&(obj->hal_uart_adp); |
AnnaBridge | 167:e84263d55307 | 447 | u8 uart_idx = pHalRuartAdapter->UartIndex; |
AnnaBridge | 167:e84263d55307 | 448 | u32 RegValue; |
AnnaBridge | 167:e84263d55307 | 449 | |
AnnaBridge | 167:e84263d55307 | 450 | RegValue = HAL_RUART_READ32(uart_idx, RUART_LINE_CTL_REG_OFF); |
AnnaBridge | 167:e84263d55307 | 451 | RegValue &= ~(BIT_UART_LCR_BREAK_CTRL); |
AnnaBridge | 167:e84263d55307 | 452 | HAL_RUART_WRITE32(uart_idx, RUART_LINE_CTL_REG_OFF, RegValue); |
AnnaBridge | 167:e84263d55307 | 453 | } |
AnnaBridge | 167:e84263d55307 | 454 | |
AnnaBridge | 167:e84263d55307 | 455 | void serial_pinout_tx(PinName tx) |
AnnaBridge | 167:e84263d55307 | 456 | { |
AnnaBridge | 167:e84263d55307 | 457 | pinmap_pinout(tx, PinMap_UART_TX); |
AnnaBridge | 167:e84263d55307 | 458 | } |
AnnaBridge | 167:e84263d55307 | 459 | |
AnnaBridge | 167:e84263d55307 | 460 | |
AnnaBridge | 167:e84263d55307 | 461 | #if DEVICE_SERIAL_ASYNCH |
AnnaBridge | 167:e84263d55307 | 462 | #endif |
AnnaBridge | 167:e84263d55307 | 463 | #endif //#if CONFIG_UART_EN |
AnnaBridge | 167:e84263d55307 | 464 |