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