mbed library sources. Supersedes mbed-src.
Fork of mbed-dev by
targets/TARGET_NUVOTON/TARGET_M451/device/StdDriver/m451_scuart.c@152:9a67f0b066fc, 2016-12-15 (annotated)
- Committer:
- <>
- Date:
- Thu Dec 15 11:48:27 2016 +0000
- Revision:
- 152:9a67f0b066fc
- Parent:
- 149:156823d33999
This updates the lib to the mbed lib v131
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
<> | 149:156823d33999 | 1 | /**************************************************************************//** |
<> | 149:156823d33999 | 2 | * @file scuart.c |
<> | 149:156823d33999 | 3 | * @version V3.00 |
<> | 149:156823d33999 | 4 | * $Revision: 8 $ |
<> | 149:156823d33999 | 5 | * $Date: 15/08/11 10:26a $ |
<> | 149:156823d33999 | 6 | * @brief M451 series Smartcard UART mode (SCUART) driver source file |
<> | 149:156823d33999 | 7 | * |
<> | 149:156823d33999 | 8 | * @note |
<> | 149:156823d33999 | 9 | * Copyright (C) 2013~2015 Nuvoton Technology Corp. All rights reserved. |
<> | 149:156823d33999 | 10 | *****************************************************************************/ |
<> | 149:156823d33999 | 11 | #include "M451Series.h" |
<> | 149:156823d33999 | 12 | |
<> | 149:156823d33999 | 13 | /** @addtogroup Standard_Driver Standard Driver |
<> | 149:156823d33999 | 14 | @{ |
<> | 149:156823d33999 | 15 | */ |
<> | 149:156823d33999 | 16 | |
<> | 149:156823d33999 | 17 | /** @addtogroup SCUART_Driver SCUART Driver |
<> | 149:156823d33999 | 18 | @{ |
<> | 149:156823d33999 | 19 | */ |
<> | 149:156823d33999 | 20 | |
<> | 149:156823d33999 | 21 | |
<> | 149:156823d33999 | 22 | /** @addtogroup SCUART_EXPORTED_FUNCTIONS SCUART Exported Functions |
<> | 149:156823d33999 | 23 | @{ |
<> | 149:156823d33999 | 24 | */ |
<> | 149:156823d33999 | 25 | |
<> | 149:156823d33999 | 26 | /** |
<> | 149:156823d33999 | 27 | * @brief Disable smartcard uart interface. |
<> | 149:156823d33999 | 28 | * @param sc The pointer of smartcard module. |
<> | 149:156823d33999 | 29 | * @return None |
<> | 149:156823d33999 | 30 | * @details The function is used to disable smartcard interface UART mode. |
<> | 149:156823d33999 | 31 | */ |
<> | 149:156823d33999 | 32 | void SCUART_Close(SC_T* sc) |
<> | 149:156823d33999 | 33 | { |
<> | 149:156823d33999 | 34 | sc->INTEN = 0; |
<> | 149:156823d33999 | 35 | sc->UARTCTL = 0; |
<> | 149:156823d33999 | 36 | sc->CTL = 0; |
<> | 149:156823d33999 | 37 | |
<> | 149:156823d33999 | 38 | } |
<> | 149:156823d33999 | 39 | |
<> | 149:156823d33999 | 40 | /// @cond HIDDEN_SYMBOLS |
<> | 149:156823d33999 | 41 | /** |
<> | 149:156823d33999 | 42 | * @brief This function returns module clock of specified SC interface |
<> | 149:156823d33999 | 43 | * @param[in] sc The pointer of smartcard module. |
<> | 149:156823d33999 | 44 | * @return Module clock of specified SC interface |
<> | 149:156823d33999 | 45 | */ |
<> | 149:156823d33999 | 46 | static uint32_t SCUART_GetClock(SC_T *sc) |
<> | 149:156823d33999 | 47 | { |
<> | 149:156823d33999 | 48 | uint32_t u32ClkSrc, u32Num, u32Clk; |
<> | 149:156823d33999 | 49 | |
<> | 149:156823d33999 | 50 | if(sc == SC0) |
<> | 149:156823d33999 | 51 | u32Num = 0; |
<> | 149:156823d33999 | 52 | #if 0 /* M451 series has only one SC interface */ |
<> | 149:156823d33999 | 53 | else if(sc == SC1) |
<> | 149:156823d33999 | 54 | u32Num = 1; |
<> | 149:156823d33999 | 55 | else if(sc == SC2) |
<> | 149:156823d33999 | 56 | u32Num = 2; |
<> | 149:156823d33999 | 57 | else if(sc == SC3) |
<> | 149:156823d33999 | 58 | u32Num = 3; |
<> | 149:156823d33999 | 59 | else if(sc == SC4) |
<> | 149:156823d33999 | 60 | u32Num = 4; |
<> | 149:156823d33999 | 61 | else if(sc == SC5) |
<> | 149:156823d33999 | 62 | u32Intf = 5; |
<> | 149:156823d33999 | 63 | #endif |
<> | 149:156823d33999 | 64 | else |
<> | 149:156823d33999 | 65 | return FALSE; |
<> | 149:156823d33999 | 66 | |
<> | 149:156823d33999 | 67 | u32ClkSrc = (CLK->CLKSEL3 >> (2 * u32Num)) & CLK_CLKSEL3_SC0SEL_Msk; |
<> | 149:156823d33999 | 68 | |
<> | 149:156823d33999 | 69 | // Get smartcard module clock |
<> | 149:156823d33999 | 70 | if(u32ClkSrc == 0) |
<> | 149:156823d33999 | 71 | u32Clk = __HXT; |
<> | 149:156823d33999 | 72 | else if(u32ClkSrc == 1) |
<> | 149:156823d33999 | 73 | u32Clk = CLK_GetPLLClockFreq(); |
<> | 149:156823d33999 | 74 | else if(u32ClkSrc == 2) { |
<> | 149:156823d33999 | 75 | SystemCoreClockUpdate(); |
<> | 149:156823d33999 | 76 | if(CLK->CLKSEL0 & CLK_CLKSEL0_PCLK0SEL_Msk) |
<> | 149:156823d33999 | 77 | u32Clk = SystemCoreClock / 2; |
<> | 149:156823d33999 | 78 | else |
<> | 149:156823d33999 | 79 | u32Clk = SystemCoreClock; |
<> | 149:156823d33999 | 80 | } else |
<> | 149:156823d33999 | 81 | u32Clk = __HIRC; |
<> | 149:156823d33999 | 82 | |
<> | 149:156823d33999 | 83 | #if 0 /* M451 series has only one SC interface */ |
<> | 149:156823d33999 | 84 | if(u32Num < 4) { |
<> | 149:156823d33999 | 85 | u32Clk /= (((CLK->CLKDIV1 >> (8 * u32Num)) & CLK_CLKDIV1_SC0DIV_Msk) + 1); |
<> | 149:156823d33999 | 86 | } else { |
<> | 149:156823d33999 | 87 | u32Clk /= (((CLK->CLKDIV2 >> (8 * (u32Num - 4))) & CLK_CLKDIV2_SC4DIV_Msk) + 1); |
<> | 149:156823d33999 | 88 | } |
<> | 149:156823d33999 | 89 | #else |
<> | 149:156823d33999 | 90 | u32Clk /= (((CLK->CLKDIV1 >> (8 * u32Num)) & CLK_CLKDIV1_SC0DIV_Msk) + 1); |
<> | 149:156823d33999 | 91 | #endif |
<> | 149:156823d33999 | 92 | |
<> | 149:156823d33999 | 93 | return u32Clk; |
<> | 149:156823d33999 | 94 | } |
<> | 149:156823d33999 | 95 | /// @endcond HIDDEN_SYMBOLS |
<> | 149:156823d33999 | 96 | |
<> | 149:156823d33999 | 97 | /** |
<> | 149:156823d33999 | 98 | * @brief Enable smartcard uart interface. |
<> | 149:156823d33999 | 99 | * @param[in] sc The pointer of smartcard module. |
<> | 149:156823d33999 | 100 | * @param[in] u32baudrate Target baudrate of smartcard module. |
<> | 149:156823d33999 | 101 | * @return Actual baudrate of smartcard mode. |
<> | 149:156823d33999 | 102 | * @details This function use to enable smartcard module UART mode and set baudrate. |
<> | 149:156823d33999 | 103 | * @note This function configures character width to 8 bits, 1 stop bit, and no parity. |
<> | 149:156823d33999 | 104 | * And can use \ref SCUART_SetLineConfig function to update these settings. |
<> | 149:156823d33999 | 105 | */ |
<> | 149:156823d33999 | 106 | uint32_t SCUART_Open(SC_T* sc, uint32_t u32baudrate) |
<> | 149:156823d33999 | 107 | { |
<> | 149:156823d33999 | 108 | uint32_t u32Clk = SCUART_GetClock(sc), u32Div; |
<> | 149:156823d33999 | 109 | |
<> | 149:156823d33999 | 110 | // Calculate divider for target baudrate |
<> | 149:156823d33999 | 111 | u32Div = (u32Clk + (u32baudrate >> 1) - 1) / u32baudrate - 1; |
<> | 149:156823d33999 | 112 | |
<> | 149:156823d33999 | 113 | sc->CTL = SC_CTL_SCEN_Msk | SC_CTL_NSB_Msk; // Enable smartcard interface and stop bit = 1 |
<> | 149:156823d33999 | 114 | sc->UARTCTL = SCUART_CHAR_LEN_8 | SCUART_PARITY_NONE | SC_UARTCTL_UARTEN_Msk; // Enable UART mode, disable parity and 8 bit per character |
<> | 149:156823d33999 | 115 | sc->ETUCTL = u32Div; |
<> | 149:156823d33999 | 116 | |
<> | 149:156823d33999 | 117 | return(u32Clk / (u32Div+1)); |
<> | 149:156823d33999 | 118 | } |
<> | 149:156823d33999 | 119 | |
<> | 149:156823d33999 | 120 | /** |
<> | 149:156823d33999 | 121 | * @brief Read data from smartcard UART interface. |
<> | 149:156823d33999 | 122 | * @param[in] sc The pointer of smartcard module. |
<> | 149:156823d33999 | 123 | * @param[in] pu8RxBuf The buffer to store receive the data. |
<> | 149:156823d33999 | 124 | * @param[in] u32ReadBytes Target number of characters to receive. |
<> | 149:156823d33999 | 125 | * @return Actual character number reads to buffer. |
<> | 149:156823d33999 | 126 | * @details The function is used to read Rx data from RX FIFO. |
<> | 149:156823d33999 | 127 | * @note This function does not block and return immediately if there's no data available. |
<> | 149:156823d33999 | 128 | */ |
<> | 149:156823d33999 | 129 | uint32_t SCUART_Read(SC_T* sc, uint8_t *pu8RxBuf, uint32_t u32ReadBytes) |
<> | 149:156823d33999 | 130 | { |
<> | 149:156823d33999 | 131 | uint32_t u32Count; |
<> | 149:156823d33999 | 132 | |
<> | 149:156823d33999 | 133 | for(u32Count = 0; u32Count < u32ReadBytes; u32Count++) { |
<> | 149:156823d33999 | 134 | if(SCUART_GET_RX_EMPTY(sc)) { // no data available |
<> | 149:156823d33999 | 135 | break; |
<> | 149:156823d33999 | 136 | } |
<> | 149:156823d33999 | 137 | pu8RxBuf[u32Count] = SCUART_READ(sc); // get data from FIFO |
<> | 149:156823d33999 | 138 | } |
<> | 149:156823d33999 | 139 | |
<> | 149:156823d33999 | 140 | return u32Count; |
<> | 149:156823d33999 | 141 | } |
<> | 149:156823d33999 | 142 | |
<> | 149:156823d33999 | 143 | /** |
<> | 149:156823d33999 | 144 | * @brief This function use to config smartcard UART mode line setting. |
<> | 149:156823d33999 | 145 | * @param[in] sc The pointer of smartcard module. |
<> | 149:156823d33999 | 146 | * @param[in] u32Baudrate Target baudrate of smartcard module. If this value is 0, UART baudrate will not change. |
<> | 149:156823d33999 | 147 | * @param[in] u32DataWidth The data length, could be: |
<> | 149:156823d33999 | 148 | * - \ref SCUART_CHAR_LEN_5 |
<> | 149:156823d33999 | 149 | * - \ref SCUART_CHAR_LEN_6 |
<> | 149:156823d33999 | 150 | * - \ref SCUART_CHAR_LEN_7 |
<> | 149:156823d33999 | 151 | * - \ref SCUART_CHAR_LEN_8 |
<> | 149:156823d33999 | 152 | * @param[in] u32Parity The parity setting, could be: |
<> | 149:156823d33999 | 153 | * - \ref SCUART_PARITY_NONE |
<> | 149:156823d33999 | 154 | * - \ref SCUART_PARITY_ODD |
<> | 149:156823d33999 | 155 | * - \ref SCUART_PARITY_EVEN |
<> | 149:156823d33999 | 156 | * @param[in] u32StopBits The stop bit length, could be: |
<> | 149:156823d33999 | 157 | * - \ref SCUART_STOP_BIT_1 |
<> | 149:156823d33999 | 158 | * - \ref SCUART_STOP_BIT_2 |
<> | 149:156823d33999 | 159 | * @return Actual baudrate of smartcard. |
<> | 149:156823d33999 | 160 | * @details Smartcard UART mode is operated in LIN data frame. |
<> | 149:156823d33999 | 161 | */ |
<> | 149:156823d33999 | 162 | uint32_t SCUART_SetLineConfig(SC_T* sc, uint32_t u32Baudrate, uint32_t u32DataWidth, uint32_t u32Parity, uint32_t u32StopBits) |
<> | 149:156823d33999 | 163 | { |
<> | 149:156823d33999 | 164 | |
<> | 149:156823d33999 | 165 | uint32_t u32Clk = SCUART_GetClock(sc), u32Div; |
<> | 149:156823d33999 | 166 | |
<> | 149:156823d33999 | 167 | if(u32Baudrate == 0) { // keep original baudrate setting |
<> | 149:156823d33999 | 168 | u32Div = sc->ETUCTL & SC_ETUCTL_ETURDIV_Msk; |
<> | 149:156823d33999 | 169 | } else { |
<> | 149:156823d33999 | 170 | // Calculate divider for target baudrate |
<> | 149:156823d33999 | 171 | u32Div = (u32Clk + (u32Baudrate >> 1) - 1) / u32Baudrate - 1; |
<> | 149:156823d33999 | 172 | sc->ETUCTL = u32Div; |
<> | 149:156823d33999 | 173 | } |
<> | 149:156823d33999 | 174 | |
<> | 149:156823d33999 | 175 | sc->CTL = u32StopBits | SC_CTL_SCEN_Msk; // Set stop bit |
<> | 149:156823d33999 | 176 | sc->UARTCTL = u32Parity | u32DataWidth | SC_UARTCTL_UARTEN_Msk; // Set character width and parity |
<> | 149:156823d33999 | 177 | |
<> | 149:156823d33999 | 178 | return(u32Clk / (u32Div+1)); |
<> | 149:156823d33999 | 179 | } |
<> | 149:156823d33999 | 180 | |
<> | 149:156823d33999 | 181 | /** |
<> | 149:156823d33999 | 182 | * @brief This function use to set receive timeout count. |
<> | 149:156823d33999 | 183 | * @param[in] sc The pointer of smartcard module. |
<> | 149:156823d33999 | 184 | * @param[in] u32TOC Rx timeout counter, using baudrate as counter unit. Valid range are 0~0x1FF, |
<> | 149:156823d33999 | 185 | * set this value to 0 will disable timeout counter. |
<> | 149:156823d33999 | 186 | * @return None |
<> | 149:156823d33999 | 187 | * @details The time-out counter resets and starts counting whenever the RX buffer received a |
<> | 149:156823d33999 | 188 | * new data word. Once the counter decrease to 1 and no new data is received or CPU |
<> | 149:156823d33999 | 189 | * does not read any data from FIFO, a receiver time-out interrupt will be generated. |
<> | 149:156823d33999 | 190 | */ |
<> | 149:156823d33999 | 191 | void SCUART_SetTimeoutCnt(SC_T* sc, uint32_t u32TOC) |
<> | 149:156823d33999 | 192 | { |
<> | 149:156823d33999 | 193 | sc->RXTOUT = u32TOC; |
<> | 149:156823d33999 | 194 | } |
<> | 149:156823d33999 | 195 | |
<> | 149:156823d33999 | 196 | |
<> | 149:156823d33999 | 197 | /** |
<> | 149:156823d33999 | 198 | * @brief Write data to smartcard UART interface. |
<> | 149:156823d33999 | 199 | * @param[in] sc The pointer of smartcard module. |
<> | 149:156823d33999 | 200 | * @param[in] pu8TxBuf The buffer containing data to send to transmit FIFO. |
<> | 149:156823d33999 | 201 | * @param[in] u32WriteBytes Number of data to send. |
<> | 149:156823d33999 | 202 | * @return None |
<> | 149:156823d33999 | 203 | * @details This function is to write data into transmit FIFO to send data out. |
<> | 149:156823d33999 | 204 | * @note This function blocks until all data write into FIFO. |
<> | 149:156823d33999 | 205 | */ |
<> | 149:156823d33999 | 206 | void SCUART_Write(SC_T* sc, uint8_t *pu8TxBuf, uint32_t u32WriteBytes) |
<> | 149:156823d33999 | 207 | { |
<> | 149:156823d33999 | 208 | uint32_t u32Count; |
<> | 149:156823d33999 | 209 | |
<> | 149:156823d33999 | 210 | for(u32Count = 0; u32Count != u32WriteBytes; u32Count++) { |
<> | 149:156823d33999 | 211 | while(SCUART_GET_TX_FULL(sc)); // Wait 'til FIFO not full |
<> | 149:156823d33999 | 212 | sc->DAT = pu8TxBuf[u32Count]; // Write 1 byte to FIFO |
<> | 149:156823d33999 | 213 | } |
<> | 149:156823d33999 | 214 | } |
<> | 149:156823d33999 | 215 | |
<> | 149:156823d33999 | 216 | |
<> | 149:156823d33999 | 217 | /*@}*/ /* end of group SCUART_EXPORTED_FUNCTIONS */ |
<> | 149:156823d33999 | 218 | |
<> | 149:156823d33999 | 219 | /*@}*/ /* end of group SCUART_Driver */ |
<> | 149:156823d33999 | 220 | |
<> | 149:156823d33999 | 221 | /*@}*/ /* end of group Standard_Driver */ |
<> | 149:156823d33999 | 222 | |
<> | 149:156823d33999 | 223 | /*** (C) COPYRIGHT 2013~2015 Nuvoton Technology Corp. ***/ |