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