mbed library sources. Supersedes mbed-src.
Fork of mbed-dev by
targets/TARGET_NUVOTON/TARGET_NUC472/device/StdDriver/nuc472_uart.c@149:156823d33999, 2016-10-28 (annotated)
- Committer:
- <>
- Date:
- Fri Oct 28 11:17:30 2016 +0100
- Revision:
- 149:156823d33999
- Parent:
- targets/cmsis/TARGET_NUVOTON/TARGET_NUC472/StdDriver/nuc472_uart.c@144:ef7eb2e8f9f7
This updates the lib to the mbed lib v128
NOTE: This release includes a restructuring of the file and directory locations and thus some
include paths in your code may need updating accordingly.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
<> | 144:ef7eb2e8f9f7 | 1 | /**************************************************************************//** |
<> | 144:ef7eb2e8f9f7 | 2 | * @file uart.c |
<> | 144:ef7eb2e8f9f7 | 3 | * @version V1.00 |
<> | 144:ef7eb2e8f9f7 | 4 | * $Revision: 13 $ |
<> | 144:ef7eb2e8f9f7 | 5 | * $Date: 14/10/03 1:55p $ |
<> | 144:ef7eb2e8f9f7 | 6 | * @brief NUC472/NUC442 UART driver source file |
<> | 144:ef7eb2e8f9f7 | 7 | * |
<> | 144:ef7eb2e8f9f7 | 8 | * @note |
<> | 144:ef7eb2e8f9f7 | 9 | * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved. |
<> | 144:ef7eb2e8f9f7 | 10 | *****************************************************************************/ |
<> | 144:ef7eb2e8f9f7 | 11 | |
<> | 144:ef7eb2e8f9f7 | 12 | #include <stdio.h> |
<> | 144:ef7eb2e8f9f7 | 13 | #include "NUC472_442.h" |
<> | 144:ef7eb2e8f9f7 | 14 | |
<> | 144:ef7eb2e8f9f7 | 15 | /*---------------------------------------------------------------------------------------------------------*/ |
<> | 144:ef7eb2e8f9f7 | 16 | /* Includes of local headers */ |
<> | 144:ef7eb2e8f9f7 | 17 | /*---------------------------------------------------------------------------------------------------------*/ |
<> | 144:ef7eb2e8f9f7 | 18 | |
<> | 144:ef7eb2e8f9f7 | 19 | |
<> | 144:ef7eb2e8f9f7 | 20 | /** @addtogroup NUC472_442_Device_Driver NUC472/NUC442 Device Driver |
<> | 144:ef7eb2e8f9f7 | 21 | @{ |
<> | 144:ef7eb2e8f9f7 | 22 | */ |
<> | 144:ef7eb2e8f9f7 | 23 | |
<> | 144:ef7eb2e8f9f7 | 24 | /** @addtogroup NUC472_442_UART_Driver UART Driver |
<> | 144:ef7eb2e8f9f7 | 25 | @{ |
<> | 144:ef7eb2e8f9f7 | 26 | */ |
<> | 144:ef7eb2e8f9f7 | 27 | |
<> | 144:ef7eb2e8f9f7 | 28 | |
<> | 144:ef7eb2e8f9f7 | 29 | /** @addtogroup NUC472_442_UART_EXPORTED_FUNCTIONS UART Exported Functions |
<> | 144:ef7eb2e8f9f7 | 30 | @{ |
<> | 144:ef7eb2e8f9f7 | 31 | */ |
<> | 144:ef7eb2e8f9f7 | 32 | |
<> | 144:ef7eb2e8f9f7 | 33 | |
<> | 144:ef7eb2e8f9f7 | 34 | /** |
<> | 144:ef7eb2e8f9f7 | 35 | * @brief The function is used to clear UART specified interrupt flag. |
<> | 144:ef7eb2e8f9f7 | 36 | * |
<> | 144:ef7eb2e8f9f7 | 37 | * @param[in] uart The base address of UART module. |
<> | 144:ef7eb2e8f9f7 | 38 | * @param[in] u32InterruptFlag The specified interrupt of UART module.. |
<> | 144:ef7eb2e8f9f7 | 39 | * |
<> | 144:ef7eb2e8f9f7 | 40 | * @return None |
<> | 144:ef7eb2e8f9f7 | 41 | */ |
<> | 144:ef7eb2e8f9f7 | 42 | void UART_ClearIntFlag(UART_T* uart , uint32_t u32InterruptFlag) |
<> | 144:ef7eb2e8f9f7 | 43 | { |
<> | 144:ef7eb2e8f9f7 | 44 | if(u32InterruptFlag & UART_INTSTS_RLSINT_Msk) { /* clear Receive Line Status Interrupt */ |
<> | 144:ef7eb2e8f9f7 | 45 | uart->FIFOSTS |= UART_FIFOSTS_BIF_Msk | UART_FIFOSTS_FEF_Msk | UART_FIFOSTS_PEF_Msk; |
<> | 144:ef7eb2e8f9f7 | 46 | uart->FIFOSTS |= UART_FIFOSTS_ADDRDETF_Msk; |
<> | 144:ef7eb2e8f9f7 | 47 | } |
<> | 144:ef7eb2e8f9f7 | 48 | |
<> | 144:ef7eb2e8f9f7 | 49 | if(u32InterruptFlag & UART_INTSTS_MODEMINT_Msk) /* clear Modem Interrupt */ |
<> | 144:ef7eb2e8f9f7 | 50 | uart->MODEMSTS |= UART_MODEMSTS_CTSDETF_Msk; |
<> | 144:ef7eb2e8f9f7 | 51 | |
<> | 144:ef7eb2e8f9f7 | 52 | if(u32InterruptFlag & UART_INTSTS_BUFERRINT_Msk) { /* clear Buffer Error Interrupt */ |
<> | 144:ef7eb2e8f9f7 | 53 | uart->FIFOSTS |= UART_FIFOSTS_RXOVIF_Msk | UART_FIFOSTS_TXOVIF_Msk; |
<> | 144:ef7eb2e8f9f7 | 54 | } |
<> | 144:ef7eb2e8f9f7 | 55 | |
<> | 144:ef7eb2e8f9f7 | 56 | if(u32InterruptFlag & UART_INTSTS_RXTOINT_Msk) /* clear Modem Interrupt */ |
<> | 144:ef7eb2e8f9f7 | 57 | uart->INTSTS |= UART_INTSTS_RXTOIF_Msk; |
<> | 144:ef7eb2e8f9f7 | 58 | |
<> | 144:ef7eb2e8f9f7 | 59 | } |
<> | 144:ef7eb2e8f9f7 | 60 | |
<> | 144:ef7eb2e8f9f7 | 61 | |
<> | 144:ef7eb2e8f9f7 | 62 | /** |
<> | 144:ef7eb2e8f9f7 | 63 | * @brief The function is used to disable UART. |
<> | 144:ef7eb2e8f9f7 | 64 | * |
<> | 144:ef7eb2e8f9f7 | 65 | * @param[in] uart The base address of UART module. |
<> | 144:ef7eb2e8f9f7 | 66 | * |
<> | 144:ef7eb2e8f9f7 | 67 | * @return None |
<> | 144:ef7eb2e8f9f7 | 68 | */ |
<> | 144:ef7eb2e8f9f7 | 69 | void UART_Close(UART_T* uart) |
<> | 144:ef7eb2e8f9f7 | 70 | { |
<> | 144:ef7eb2e8f9f7 | 71 | uart->INTEN = 0; |
<> | 144:ef7eb2e8f9f7 | 72 | } |
<> | 144:ef7eb2e8f9f7 | 73 | |
<> | 144:ef7eb2e8f9f7 | 74 | |
<> | 144:ef7eb2e8f9f7 | 75 | /** |
<> | 144:ef7eb2e8f9f7 | 76 | * @brief The function is used to disable UART auto flow control. |
<> | 144:ef7eb2e8f9f7 | 77 | * |
<> | 144:ef7eb2e8f9f7 | 78 | * @param[in] uart The base address of UART module. |
<> | 144:ef7eb2e8f9f7 | 79 | * |
<> | 144:ef7eb2e8f9f7 | 80 | * @return None |
<> | 144:ef7eb2e8f9f7 | 81 | */ |
<> | 144:ef7eb2e8f9f7 | 82 | void UART_DisableFlowCtrl(UART_T* uart) |
<> | 144:ef7eb2e8f9f7 | 83 | { |
<> | 144:ef7eb2e8f9f7 | 84 | uart->INTEN &= ~(UART_INTEN_ATORTSEN_Msk | UART_INTEN_ATOCTSEN_Msk); |
<> | 144:ef7eb2e8f9f7 | 85 | } |
<> | 144:ef7eb2e8f9f7 | 86 | |
<> | 144:ef7eb2e8f9f7 | 87 | |
<> | 144:ef7eb2e8f9f7 | 88 | /** |
<> | 144:ef7eb2e8f9f7 | 89 | * @brief The function is used to disable UART specified interrupt and disable NVIC UART IRQ. |
<> | 144:ef7eb2e8f9f7 | 90 | * |
<> | 144:ef7eb2e8f9f7 | 91 | * @param[in] uart The base address of UART module. |
<> | 144:ef7eb2e8f9f7 | 92 | * @param[in] u32InterruptFlag The specified interrupt of UART module. |
<> | 144:ef7eb2e8f9f7 | 93 | * - \ref UART_INTEN_TOCNTEN_Msk : Rx Time Out interrupt |
<> | 144:ef7eb2e8f9f7 | 94 | * - \ref UART_INTEN_WKCTSIEN_Msk : Wakeup interrupt |
<> | 144:ef7eb2e8f9f7 | 95 | * - \ref UART_INTEN_BUFERRIEN_Msk : Buffer Error interrupt |
<> | 144:ef7eb2e8f9f7 | 96 | * - \ref UART_INTEN_RXTOIEN_Msk : Rx time-out interrupt |
<> | 144:ef7eb2e8f9f7 | 97 | * - \ref UART_INTEN_MODEMIEN_Msk : Modem interrupt |
<> | 144:ef7eb2e8f9f7 | 98 | * - \ref UART_INTEN_RLSIEN_Msk : Rx Line status interrupt |
<> | 144:ef7eb2e8f9f7 | 99 | * - \ref UART_INTEN_THREIEN_Msk : Tx empty interrupt |
<> | 144:ef7eb2e8f9f7 | 100 | * - \ref UART_INTEN_RDAIEN_Msk : Rx ready interrupt |
<> | 144:ef7eb2e8f9f7 | 101 | * |
<> | 144:ef7eb2e8f9f7 | 102 | * @return None |
<> | 144:ef7eb2e8f9f7 | 103 | */ |
<> | 144:ef7eb2e8f9f7 | 104 | void UART_DisableInt(UART_T* uart, uint32_t u32InterruptFlag ) |
<> | 144:ef7eb2e8f9f7 | 105 | { |
<> | 144:ef7eb2e8f9f7 | 106 | uart->INTEN &= ~ u32InterruptFlag; |
<> | 144:ef7eb2e8f9f7 | 107 | } |
<> | 144:ef7eb2e8f9f7 | 108 | |
<> | 144:ef7eb2e8f9f7 | 109 | |
<> | 144:ef7eb2e8f9f7 | 110 | |
<> | 144:ef7eb2e8f9f7 | 111 | /** |
<> | 144:ef7eb2e8f9f7 | 112 | * @brief The function is used to Enable UART auto flow control. |
<> | 144:ef7eb2e8f9f7 | 113 | * |
<> | 144:ef7eb2e8f9f7 | 114 | * @param[in] uart The base address of UART module. |
<> | 144:ef7eb2e8f9f7 | 115 | * |
<> | 144:ef7eb2e8f9f7 | 116 | * @return None |
<> | 144:ef7eb2e8f9f7 | 117 | */ |
<> | 144:ef7eb2e8f9f7 | 118 | void UART_EnableFlowCtrl(UART_T* uart ) |
<> | 144:ef7eb2e8f9f7 | 119 | { |
<> | 144:ef7eb2e8f9f7 | 120 | uart->MODEM |= UART_MODEM_RTSACTLV_Msk; |
<> | 144:ef7eb2e8f9f7 | 121 | uart->MODEM &= UART_MODEM_RTS_Msk; |
<> | 144:ef7eb2e8f9f7 | 122 | uart->MODEMSTS |= UART_MODEMSTS_CTSACTLV_Msk; |
<> | 144:ef7eb2e8f9f7 | 123 | uart->INTEN |= UART_INTEN_ATORTSEN_Msk | UART_INTEN_ATOCTSEN_Msk; |
<> | 144:ef7eb2e8f9f7 | 124 | } |
<> | 144:ef7eb2e8f9f7 | 125 | |
<> | 144:ef7eb2e8f9f7 | 126 | |
<> | 144:ef7eb2e8f9f7 | 127 | /** |
<> | 144:ef7eb2e8f9f7 | 128 | * @brief The function is used to enable UART specified interrupt and disable NVIC UART IRQ. |
<> | 144:ef7eb2e8f9f7 | 129 | * |
<> | 144:ef7eb2e8f9f7 | 130 | * @param[in] uart The base address of UART module. |
<> | 144:ef7eb2e8f9f7 | 131 | * @param[in] u32InterruptFlag The specified interrupt of UART module: |
<> | 144:ef7eb2e8f9f7 | 132 | * - \ref UART_INTEN_TOCNTEN_Msk : Rx Time Out interrupt |
<> | 144:ef7eb2e8f9f7 | 133 | * - \ref UART_INTEN_WKCTSIEN_Msk : Wakeup interrupt |
<> | 144:ef7eb2e8f9f7 | 134 | * - \ref UART_INTEN_BUFERRIEN_Msk : Buffer Error interrupt |
<> | 144:ef7eb2e8f9f7 | 135 | * - \ref UART_INTEN_RXTOIEN_Msk : Rx time-out interrupt |
<> | 144:ef7eb2e8f9f7 | 136 | * - \ref UART_INTEN_MODEMIEN_Msk : Modem interrupt |
<> | 144:ef7eb2e8f9f7 | 137 | * - \ref UART_INTEN_RLSIEN_Msk : Rx Line status interrupt |
<> | 144:ef7eb2e8f9f7 | 138 | * - \ref UART_INTEN_THREIEN_Msk : Tx empty interrupt |
<> | 144:ef7eb2e8f9f7 | 139 | * - \ref UART_INTEN_RDAIEN_Msk : Rx ready interrupt |
<> | 144:ef7eb2e8f9f7 | 140 | * |
<> | 144:ef7eb2e8f9f7 | 141 | * @return None |
<> | 144:ef7eb2e8f9f7 | 142 | */ |
<> | 144:ef7eb2e8f9f7 | 143 | void UART_EnableInt(UART_T* uart, uint32_t u32InterruptFlag ) |
<> | 144:ef7eb2e8f9f7 | 144 | { |
<> | 144:ef7eb2e8f9f7 | 145 | uart->INTEN |= u32InterruptFlag; |
<> | 144:ef7eb2e8f9f7 | 146 | } |
<> | 144:ef7eb2e8f9f7 | 147 | |
<> | 144:ef7eb2e8f9f7 | 148 | |
<> | 144:ef7eb2e8f9f7 | 149 | /** |
<> | 144:ef7eb2e8f9f7 | 150 | * @brief This function use to enable UART function and set baud-rate. |
<> | 144:ef7eb2e8f9f7 | 151 | * |
<> | 144:ef7eb2e8f9f7 | 152 | * @param[in] uart The base address of UART module. |
<> | 144:ef7eb2e8f9f7 | 153 | * @param[in] u32baudrate The baudrate of UART module. |
<> | 144:ef7eb2e8f9f7 | 154 | * |
<> | 144:ef7eb2e8f9f7 | 155 | * @return None |
<> | 144:ef7eb2e8f9f7 | 156 | */ |
<> | 144:ef7eb2e8f9f7 | 157 | void UART_Open(UART_T* uart, uint32_t u32baudrate) |
<> | 144:ef7eb2e8f9f7 | 158 | { |
<> | 144:ef7eb2e8f9f7 | 159 | uint8_t u8UartClkSrcSel; |
<> | 144:ef7eb2e8f9f7 | 160 | uint32_t u32ClkTbl[4] = {__HXT, 0, __HIRC, __HIRC}; |
<> | 144:ef7eb2e8f9f7 | 161 | uint32_t u32Clk; |
<> | 144:ef7eb2e8f9f7 | 162 | uint32_t u32Baud_Div; |
<> | 144:ef7eb2e8f9f7 | 163 | |
<> | 144:ef7eb2e8f9f7 | 164 | u32ClkTbl[1] = CLK_GetPLLClockFreq();; |
<> | 144:ef7eb2e8f9f7 | 165 | |
<> | 144:ef7eb2e8f9f7 | 166 | u8UartClkSrcSel = (CLK->CLKSEL1 & CLK_CLKSEL1_UARTSEL_Msk) >> CLK_CLKSEL1_UARTSEL_Pos; |
<> | 144:ef7eb2e8f9f7 | 167 | |
<> | 144:ef7eb2e8f9f7 | 168 | uart->FUNCSEL = UART_FUNCSEL_UART; |
<> | 144:ef7eb2e8f9f7 | 169 | uart->LINE = UART_WORD_LEN_8 | UART_PARITY_NONE | UART_STOP_BIT_1; |
<> | 144:ef7eb2e8f9f7 | 170 | uart->FIFO = UART_FIFO_RFITL_1BYTE | UART_FIFO_RTSTRGLV_1BYTE; |
<> | 144:ef7eb2e8f9f7 | 171 | |
<> | 144:ef7eb2e8f9f7 | 172 | u32Clk = (u32ClkTbl[u8UartClkSrcSel]) / (((CLK->CLKDIV0 & CLK_CLKDIV0_UARTDIV_Msk) >> CLK_CLKDIV0_UARTDIV_Pos) + 1); |
<> | 144:ef7eb2e8f9f7 | 173 | |
<> | 144:ef7eb2e8f9f7 | 174 | if(u32baudrate != 0) { |
<> | 144:ef7eb2e8f9f7 | 175 | u32Baud_Div = UART_BAUD_MODE2_DIVIDER(u32Clk, u32baudrate); |
<> | 144:ef7eb2e8f9f7 | 176 | |
<> | 144:ef7eb2e8f9f7 | 177 | if(u32Baud_Div > 0xFFFF) |
<> | 144:ef7eb2e8f9f7 | 178 | uart->BAUD = (UART_BAUD_MODE0 | UART_BAUD_MODE0_DIVIDER(u32Clk, u32baudrate)); |
<> | 144:ef7eb2e8f9f7 | 179 | else |
<> | 144:ef7eb2e8f9f7 | 180 | uart->BAUD = (UART_BAUD_MODE2 | u32Baud_Div); |
<> | 144:ef7eb2e8f9f7 | 181 | } |
<> | 144:ef7eb2e8f9f7 | 182 | } |
<> | 144:ef7eb2e8f9f7 | 183 | |
<> | 144:ef7eb2e8f9f7 | 184 | |
<> | 144:ef7eb2e8f9f7 | 185 | /** |
<> | 144:ef7eb2e8f9f7 | 186 | * @brief The function is used to read Rx data from RX FIFO and the data will be stored in pu8RxBuf. |
<> | 144:ef7eb2e8f9f7 | 187 | * |
<> | 144:ef7eb2e8f9f7 | 188 | * @param[in] uart The base address of UART module. |
<> | 144:ef7eb2e8f9f7 | 189 | * @param[out] pu8RxBuf The buffer to receive the data of receive FIFO. |
<> | 144:ef7eb2e8f9f7 | 190 | * @param[in] u32ReadBytes The the read bytes number of data. |
<> | 144:ef7eb2e8f9f7 | 191 | * |
<> | 144:ef7eb2e8f9f7 | 192 | * @return u32Count: Receive byte count |
<> | 144:ef7eb2e8f9f7 | 193 | * |
<> | 144:ef7eb2e8f9f7 | 194 | */ |
<> | 144:ef7eb2e8f9f7 | 195 | uint32_t UART_Read(UART_T* uart, uint8_t *pu8RxBuf, uint32_t u32ReadBytes) |
<> | 144:ef7eb2e8f9f7 | 196 | { |
<> | 144:ef7eb2e8f9f7 | 197 | uint32_t u32Count; |
<> | 144:ef7eb2e8f9f7 | 198 | |
<> | 144:ef7eb2e8f9f7 | 199 | for(u32Count=0; u32Count < u32ReadBytes; u32Count++) { |
<> | 144:ef7eb2e8f9f7 | 200 | if(uart->FIFOSTS & UART_FIFOSTS_RXEMPTY_Msk) { /* Check RX empty => failed */ |
<> | 144:ef7eb2e8f9f7 | 201 | return u32Count; |
<> | 144:ef7eb2e8f9f7 | 202 | } |
<> | 144:ef7eb2e8f9f7 | 203 | pu8RxBuf[u32Count] = uart->DAT; /* Get Data from UART RX */ |
<> | 144:ef7eb2e8f9f7 | 204 | } |
<> | 144:ef7eb2e8f9f7 | 205 | |
<> | 144:ef7eb2e8f9f7 | 206 | return u32Count; |
<> | 144:ef7eb2e8f9f7 | 207 | } |
<> | 144:ef7eb2e8f9f7 | 208 | |
<> | 144:ef7eb2e8f9f7 | 209 | |
<> | 144:ef7eb2e8f9f7 | 210 | /** |
<> | 144:ef7eb2e8f9f7 | 211 | * @brief This function use to config UART line setting. |
<> | 144:ef7eb2e8f9f7 | 212 | * |
<> | 144:ef7eb2e8f9f7 | 213 | * @param[in] uart The base address of UART module. |
<> | 144:ef7eb2e8f9f7 | 214 | * @param[in] u32baudrate The register value of baudrate of UART module. |
<> | 144:ef7eb2e8f9f7 | 215 | * if u32baudrate = 0, UART baudrate will not change. |
<> | 144:ef7eb2e8f9f7 | 216 | * @param[in] u32data_width The data length of UART module. [ \ref UART_WORD_LEN_5 / \ref UART_WORD_LEN_6 / \ref UART_WORD_LEN_7 / \ref UART_WORD_LEN_8] |
<> | 144:ef7eb2e8f9f7 | 217 | * @param[in] u32parity The parity setting (odd/even/none) of UART module. [ \ref UART_PARITY_NONE / \ref UART_PARITY_ODD / |
<> | 144:ef7eb2e8f9f7 | 218 | * \ref UART_PARITY_EVEN / \ref UART_PARITY_MARK / \ref UART_PARITY_SPACE] |
<> | 144:ef7eb2e8f9f7 | 219 | * @param[in] u32stop_bits The stop bit length (1/1.5/2 bit) of UART module. [ \ref UART_STOP_BIT_1 / \ref UART_STOP_BIT_1_5 / \ref UART_STOP_BIT_2] |
<> | 144:ef7eb2e8f9f7 | 220 | * |
<> | 144:ef7eb2e8f9f7 | 221 | * @return None |
<> | 144:ef7eb2e8f9f7 | 222 | */ |
<> | 144:ef7eb2e8f9f7 | 223 | void UART_SetLine_Config(UART_T* uart, uint32_t u32baudrate, uint32_t u32data_width, uint32_t u32parity, uint32_t u32stop_bits) |
<> | 144:ef7eb2e8f9f7 | 224 | { |
<> | 144:ef7eb2e8f9f7 | 225 | uint8_t u8UartClkSrcSel; |
<> | 144:ef7eb2e8f9f7 | 226 | uint32_t u32ClkTbl[4] = {__HXT, 0, __HIRC, __HIRC}; |
<> | 144:ef7eb2e8f9f7 | 227 | uint32_t u32Clk; |
<> | 144:ef7eb2e8f9f7 | 228 | uint32_t u32Baud_Div = 0; |
<> | 144:ef7eb2e8f9f7 | 229 | |
<> | 144:ef7eb2e8f9f7 | 230 | u32ClkTbl[1] = CLK_GetPLLClockFreq(); |
<> | 144:ef7eb2e8f9f7 | 231 | |
<> | 144:ef7eb2e8f9f7 | 232 | u8UartClkSrcSel = (CLK->CLKSEL1 & CLK_CLKSEL1_UARTSEL_Msk) >> CLK_CLKSEL1_UARTSEL_Pos; |
<> | 144:ef7eb2e8f9f7 | 233 | |
<> | 144:ef7eb2e8f9f7 | 234 | u32Clk = (u32ClkTbl[u8UartClkSrcSel]) / (((CLK->CLKDIV0 & CLK_CLKDIV0_UARTDIV_Msk) >> CLK_CLKDIV0_UARTDIV_Pos) + 1); |
<> | 144:ef7eb2e8f9f7 | 235 | |
<> | 144:ef7eb2e8f9f7 | 236 | if(u32baudrate != 0) { |
<> | 144:ef7eb2e8f9f7 | 237 | u32Baud_Div = UART_BAUD_MODE2_DIVIDER(u32Clk, u32baudrate); |
<> | 144:ef7eb2e8f9f7 | 238 | |
<> | 144:ef7eb2e8f9f7 | 239 | if(u32Baud_Div > 0xFFFF) |
<> | 144:ef7eb2e8f9f7 | 240 | uart->BAUD = (UART_BAUD_MODE0 | UART_BAUD_MODE0_DIVIDER(u32Clk, u32baudrate)); |
<> | 144:ef7eb2e8f9f7 | 241 | else |
<> | 144:ef7eb2e8f9f7 | 242 | uart->BAUD = (UART_BAUD_MODE2 | u32Baud_Div); |
<> | 144:ef7eb2e8f9f7 | 243 | } |
<> | 144:ef7eb2e8f9f7 | 244 | |
<> | 144:ef7eb2e8f9f7 | 245 | uart->LINE = u32data_width | u32parity | u32stop_bits; |
<> | 144:ef7eb2e8f9f7 | 246 | } |
<> | 144:ef7eb2e8f9f7 | 247 | |
<> | 144:ef7eb2e8f9f7 | 248 | |
<> | 144:ef7eb2e8f9f7 | 249 | /** |
<> | 144:ef7eb2e8f9f7 | 250 | * @brief This function use to set Rx timeout count. |
<> | 144:ef7eb2e8f9f7 | 251 | * |
<> | 144:ef7eb2e8f9f7 | 252 | * @param[in] uart The base address of UART module. |
<> | 144:ef7eb2e8f9f7 | 253 | * @param[in] u32TOC Rx timeout counter. |
<> | 144:ef7eb2e8f9f7 | 254 | * |
<> | 144:ef7eb2e8f9f7 | 255 | * @return None |
<> | 144:ef7eb2e8f9f7 | 256 | */ |
<> | 144:ef7eb2e8f9f7 | 257 | void UART_SetTimeoutCnt(UART_T* uart, uint32_t u32TOC) |
<> | 144:ef7eb2e8f9f7 | 258 | { |
<> | 144:ef7eb2e8f9f7 | 259 | uart->TOUT = (uart->TOUT & ~UART_TOUT_TOIC_Msk)| (u32TOC); |
<> | 144:ef7eb2e8f9f7 | 260 | uart->INTEN |= UART_INTEN_TOCNTEN_Msk; |
<> | 144:ef7eb2e8f9f7 | 261 | } |
<> | 144:ef7eb2e8f9f7 | 262 | |
<> | 144:ef7eb2e8f9f7 | 263 | |
<> | 144:ef7eb2e8f9f7 | 264 | /** |
<> | 144:ef7eb2e8f9f7 | 265 | * @brief The function is used to configure IrDA relative settings. It consists of TX or RX mode and baudrate. |
<> | 144:ef7eb2e8f9f7 | 266 | * |
<> | 144:ef7eb2e8f9f7 | 267 | * @param[in] uart The base address of UART module. |
<> | 144:ef7eb2e8f9f7 | 268 | * @param[in] u32Buadrate The baudrate of UART module. |
<> | 144:ef7eb2e8f9f7 | 269 | * @param[in] u32Direction The direction(transmit:1/receive:0) of UART module in IrDA mode. |
<> | 144:ef7eb2e8f9f7 | 270 | * |
<> | 144:ef7eb2e8f9f7 | 271 | * @return None |
<> | 144:ef7eb2e8f9f7 | 272 | */ |
<> | 144:ef7eb2e8f9f7 | 273 | void UART_SelectIrDAMode(UART_T* uart, uint32_t u32Buadrate, uint32_t u32Direction) |
<> | 144:ef7eb2e8f9f7 | 274 | { |
<> | 144:ef7eb2e8f9f7 | 275 | uart->BAUD = UART_BAUD_MODE0 | UART_BAUD_MODE0_DIVIDER(12000000, 57600); |
<> | 144:ef7eb2e8f9f7 | 276 | |
<> | 144:ef7eb2e8f9f7 | 277 | uart->IRDA &= ~UART_IRDA_TXINV_Msk; |
<> | 144:ef7eb2e8f9f7 | 278 | uart->IRDA |= UART_IRDA_RXINV_Msk; |
<> | 144:ef7eb2e8f9f7 | 279 | uart->IRDA = u32Direction ? uart->IRDA | UART_IRDA_TXEN_Msk : uart->IRDA &~ UART_IRDA_TXEN_Msk; |
<> | 144:ef7eb2e8f9f7 | 280 | uart->FUNCSEL = (0x2 << UART_FUNCSEL_FUNCSEL_Pos); |
<> | 144:ef7eb2e8f9f7 | 281 | } |
<> | 144:ef7eb2e8f9f7 | 282 | |
<> | 144:ef7eb2e8f9f7 | 283 | |
<> | 144:ef7eb2e8f9f7 | 284 | /** |
<> | 144:ef7eb2e8f9f7 | 285 | * @brief The function is used to set RS485 relative setting. |
<> | 144:ef7eb2e8f9f7 | 286 | * |
<> | 144:ef7eb2e8f9f7 | 287 | * @param[in] uart The base address of UART module. |
<> | 144:ef7eb2e8f9f7 | 288 | * @param[in] u32Mode The operation mode( \ref UART_ALTCTL_RS485NMM_Msk / \ref UART_ALTCTL_RS485AUD_Msk / \ref UART_ALTCTL_RS485AAD_Msk). |
<> | 144:ef7eb2e8f9f7 | 289 | * @param[in] u32Addr The RS485 address. |
<> | 144:ef7eb2e8f9f7 | 290 | * |
<> | 144:ef7eb2e8f9f7 | 291 | * @return None |
<> | 144:ef7eb2e8f9f7 | 292 | */ |
<> | 144:ef7eb2e8f9f7 | 293 | void UART_SelectRS485Mode(UART_T* uart, uint32_t u32Mode, uint32_t u32Addr) |
<> | 144:ef7eb2e8f9f7 | 294 | { |
<> | 144:ef7eb2e8f9f7 | 295 | uart->FUNCSEL = UART_FUNCSEL_RS485; |
<> | 144:ef7eb2e8f9f7 | 296 | uart->ALTCTL = 0; |
<> | 144:ef7eb2e8f9f7 | 297 | uart->ALTCTL |= u32Mode | (u32Addr << UART_ALTCTL_ADDRMV_Pos); |
<> | 144:ef7eb2e8f9f7 | 298 | } |
<> | 144:ef7eb2e8f9f7 | 299 | |
<> | 144:ef7eb2e8f9f7 | 300 | |
<> | 144:ef7eb2e8f9f7 | 301 | /** |
<> | 144:ef7eb2e8f9f7 | 302 | * @brief The function is to write data into TX buffer to transmit data by UART. |
<> | 144:ef7eb2e8f9f7 | 303 | * |
<> | 144:ef7eb2e8f9f7 | 304 | * @param[in] uart The base address of UART module. |
<> | 144:ef7eb2e8f9f7 | 305 | * @param[in] pu8TxBuf The buffer to send the data to UART transmission FIFO. |
<> | 144:ef7eb2e8f9f7 | 306 | * @param[in] u32WriteBytes The byte number of data. |
<> | 144:ef7eb2e8f9f7 | 307 | * |
<> | 144:ef7eb2e8f9f7 | 308 | * @return u32Count: transfer byte count |
<> | 144:ef7eb2e8f9f7 | 309 | */ |
<> | 144:ef7eb2e8f9f7 | 310 | uint32_t UART_Write(UART_T* uart,uint8_t *pu8TxBuf, uint32_t u32WriteBytes) |
<> | 144:ef7eb2e8f9f7 | 311 | { |
<> | 144:ef7eb2e8f9f7 | 312 | uint32_t u32Count; |
<> | 144:ef7eb2e8f9f7 | 313 | |
<> | 144:ef7eb2e8f9f7 | 314 | for(u32Count=0; u32Count != u32WriteBytes; u32Count++) { |
<> | 144:ef7eb2e8f9f7 | 315 | if(uart->FIFOSTS & UART_FIFOSTS_TXFULL_Msk) { /* Wait Tx empty and Time-out manner */ |
<> | 144:ef7eb2e8f9f7 | 316 | return u32Count; |
<> | 144:ef7eb2e8f9f7 | 317 | } |
<> | 144:ef7eb2e8f9f7 | 318 | uart->DAT = pu8TxBuf[u32Count]; /* Send UART Data from buffer */ |
<> | 144:ef7eb2e8f9f7 | 319 | } |
<> | 144:ef7eb2e8f9f7 | 320 | |
<> | 144:ef7eb2e8f9f7 | 321 | return u32Count; |
<> | 144:ef7eb2e8f9f7 | 322 | } |
<> | 144:ef7eb2e8f9f7 | 323 | |
<> | 144:ef7eb2e8f9f7 | 324 | |
<> | 144:ef7eb2e8f9f7 | 325 | /*@}*/ /* end of group NUC472_442_UART_EXPORTED_FUNCTIONS */ |
<> | 144:ef7eb2e8f9f7 | 326 | |
<> | 144:ef7eb2e8f9f7 | 327 | /*@}*/ /* end of group NUC472_442_UART_Driver */ |
<> | 144:ef7eb2e8f9f7 | 328 | |
<> | 144:ef7eb2e8f9f7 | 329 | /*@}*/ /* end of group NUC472_442_Device_Driver */ |
<> | 144:ef7eb2e8f9f7 | 330 | |
<> | 144:ef7eb2e8f9f7 | 331 | /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/ |
<> | 144:ef7eb2e8f9f7 | 332 | |
<> | 144:ef7eb2e8f9f7 | 333 |