nu_scuart.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /**************************************************************************//**
  2. * @file scuart.c
  3. * @brief NUC980 series Smartcard UART mode (SCUART) driver source file
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. * @copyright (C) 2018 Nuvoton Technology Corp. All rights reserved.
  7. *****************************************************************************/
  8. #include "nu_scuart.h"
  9. /** @addtogroup Standard_Driver Standard Driver
  10. @{
  11. */
  12. /** @addtogroup SCUART_Driver SCUART Driver
  13. @{
  14. */
  15. /** @addtogroup SCUART_EXPORTED_FUNCTIONS SCUART Exported Functions
  16. @{
  17. */
  18. /**
  19. * @brief Disable smartcard uart interface.
  20. * @param sc Smartcard module number
  21. * @return None
  22. * @details The function is used to disable smartcard interface UART mode.
  23. */
  24. void SCUART_Close(UINT sc)
  25. {
  26. if (sc == 0)
  27. {
  28. outpw(REG_SC0_INTEN, 0);
  29. outpw(REG_SC0_UARTCTL, 0);
  30. outpw(REG_SC0_CTL, 0);
  31. }
  32. else
  33. {
  34. outpw(REG_SC1_INTEN, 0);
  35. outpw(REG_SC1_UARTCTL, 0);
  36. outpw(REG_SC1_CTL, 0);
  37. }
  38. }
  39. /// @cond HIDDEN_SYMBOLS
  40. /**
  41. * @brief This function returns module clock of specified SC interface
  42. * @param[in] sc Smartcard module number
  43. * @return Module clock of specified SC interface
  44. */
  45. static uint32_t SCUART_GetClock(UINT sc)
  46. {
  47. uint32_t u32Div;
  48. if (sc == 0)
  49. u32Div = ((inpw(REG_CLK_DIVCTL6) >> 24) & 0xF) + 1;
  50. else
  51. u32Div = ((inpw(REG_CLK_DIVCTL6) >> 28) & 0xF) + 1;
  52. return 12000000 / u32Div;
  53. }
  54. /// @endcond HIDDEN_SYMBOLS
  55. /**
  56. * @brief Enable smartcard uart interface.
  57. * @param[in] sc Smartcard module number
  58. * @param[in] u32baudrate Target baudrate of smartcard module.
  59. * @return Actual baudrate of smartcard mode.
  60. * @details This function use to enable smartcard module UART mode and set baudrate.
  61. * @note This function configures character width to 8 bits, 1 stop bit, and no parity.
  62. * And can use \ref SCUART_SetLineConfig function to update these settings.
  63. */
  64. UINT SCUART_Open(UINT sc, UINT u32baudrate)
  65. {
  66. uint32_t u32Clk = SCUART_GetClock(sc), u32Div;
  67. // Calculate divider for target baudrate
  68. u32Div = (u32Clk + (u32baudrate >> 1) - 1) / u32baudrate - 1;
  69. if (sc == 0)
  70. {
  71. outpw(REG_SC0_CTL, SC_CTL_SCEN_Msk | SC_CTL_NSB_Msk); // Enable smartcard interface and stop bit = 1
  72. outpw(REG_SC0_UARTCTL, SCUART_CHAR_LEN_8 | SCUART_PARITY_NONE | SC_UARTCTL_UARTEN_Msk); // Enable UART mode, disable parity and 8 bit per character
  73. outpw(REG_SC0_ETUCTL, u32Div);
  74. }
  75. else
  76. {
  77. outpw(REG_SC1_CTL, SC_CTL_SCEN_Msk | SC_CTL_NSB_Msk); // Enable smartcard interface and stop bit = 1
  78. outpw(REG_SC1_UARTCTL, SCUART_CHAR_LEN_8 | SCUART_PARITY_NONE | SC_UARTCTL_UARTEN_Msk); // Enable UART mode, disable parity and 8 bit per character
  79. outpw(REG_SC1_ETUCTL, u32Div);
  80. }
  81. return (u32Clk / (u32Div + 1));
  82. }
  83. /**
  84. * @brief Read data from smartcard UART interface.
  85. * @param[in] sc Smartcard module number
  86. * @param[in] pu8RxBuf The buffer to store receive the data.
  87. * @param[in] u32ReadBytes Target number of characters to receive.
  88. * @return Actual character number reads to buffer.
  89. * @details The function is used to read Rx data from RX FIFO.
  90. * @note This function does not block and return immediately if there's no data available.
  91. */
  92. UINT SCUART_Read(UINT sc, char *pu8RxBuf, UINT u32ReadBytes)
  93. {
  94. uint32_t u32Count;
  95. if (sc == 0)
  96. {
  97. for (u32Count = 0; u32Count < u32ReadBytes; u32Count++)
  98. {
  99. if (inpw(REG_SC0_STATUS) & SC_STATUS_RXEMPTY_Msk) // no data available
  100. {
  101. break;
  102. }
  103. pu8RxBuf[u32Count] = inpw(REG_SC0_DAT); // get data from FIFO
  104. }
  105. }
  106. else
  107. {
  108. for (u32Count = 0; u32Count < u32ReadBytes; u32Count++)
  109. {
  110. if (inpw(REG_SC1_STATUS) & SC_STATUS_RXEMPTY_Msk) // no data available
  111. {
  112. break;
  113. }
  114. pu8RxBuf[u32Count] = inpw(REG_SC1_DAT); // get data from FIFO
  115. }
  116. }
  117. return u32Count;
  118. }
  119. /**
  120. * @brief This function use to config smartcard UART mode line setting.
  121. * @param[in] sc Smartcard module number
  122. * @param[in] u32Baudrate Target baudrate of smartcard module. If this value is 0, UART baudrate will not change.
  123. * @param[in] u32DataWidth The data length, could be:
  124. * - \ref SCUART_CHAR_LEN_5
  125. * - \ref SCUART_CHAR_LEN_6
  126. * - \ref SCUART_CHAR_LEN_7
  127. * - \ref SCUART_CHAR_LEN_8
  128. * @param[in] u32Parity The parity setting, could be:
  129. * - \ref SCUART_PARITY_NONE
  130. * - \ref SCUART_PARITY_ODD
  131. * - \ref SCUART_PARITY_EVEN
  132. * @param[in] u32StopBits The stop bit length, could be:
  133. * - \ref SCUART_STOP_BIT_1
  134. * - \ref SCUART_STOP_BIT_2
  135. * @return Actual baudrate of smartcard.
  136. * @details Smartcard UART mode is operated in LIN data frame.
  137. */
  138. UINT SCUART_SetLineConfig(UINT sc, UINT u32Baudrate, UINT u32DataWidth, UINT u32Parity, UINT u32StopBits)
  139. {
  140. uint32_t u32Clk = SCUART_GetClock(sc), u32Div;
  141. if (u32Baudrate == 0) // keep original baudrate setting
  142. {
  143. u32Div = (sc == 0) ? inpw(REG_SC0_ETUCTL) & 0xFFF : inpw(REG_SC1_ETUCTL) & 0xFFF;
  144. }
  145. else
  146. {
  147. // Calculate divider for target baudrate
  148. u32Div = (u32Clk + (u32Baudrate >> 1) - 1) / u32Baudrate - 1;
  149. if (sc == 0)
  150. outpw(REG_SC0_ETUCTL, u32Div);
  151. else
  152. outpw(REG_SC1_ETUCTL, u32Div);
  153. }
  154. if (sc == 0)
  155. {
  156. outpw(REG_SC0_CTL, u32StopBits | SC_CTL_SCEN_Msk); // Set stop bit
  157. outpw(REG_SC0_UARTCTL, u32Parity | u32DataWidth | SC_UARTCTL_UARTEN_Msk); // Set character width and parity
  158. }
  159. else
  160. {
  161. outpw(REG_SC1_CTL, u32StopBits | SC_CTL_SCEN_Msk); // Set stop bit
  162. outpw(REG_SC1_UARTCTL, u32Parity | u32DataWidth | SC_UARTCTL_UARTEN_Msk); // Set character width and parity
  163. }
  164. return (u32Clk / (u32Div + 1));
  165. }
  166. /**
  167. * @brief This function use to set receive timeout count.
  168. * @param[in] sc Smartcard module number
  169. * @param[in] u32TOC Rx timeout counter, using baudrate as counter unit. Valid range are 0~0x1FF,
  170. * set this value to 0 will disable timeout counter.
  171. * @return None
  172. * @details The time-out counter resets and starts counting whenever the RX buffer received a
  173. * new data word. Once the counter decrease to 1 and no new data is received or CPU
  174. * does not read any data from FIFO, a receiver time-out interrupt will be generated.
  175. */
  176. void SCUART_SetTimeoutCnt(UINT sc, UINT u32TOC)
  177. {
  178. if (sc == 0)
  179. outpw(REG_SC0_RXTOUT, u32TOC);
  180. else
  181. outpw(REG_SC1_RXTOUT, u32TOC);
  182. }
  183. /**
  184. * @brief Write data to smartcard UART interface.
  185. * @param[in] sc Smartcard module number
  186. * @param[in] pu8TxBuf The buffer containing data to send to transmit FIFO.
  187. * @param[in] u32WriteBytes Number of data to send.
  188. * @return None
  189. * @details This function is to write data into transmit FIFO to send data out.
  190. * @note This function blocks until all data write into FIFO.
  191. */
  192. void SCUART_Write(UINT sc, char *pu8TxBuf, UINT u32WriteBytes)
  193. {
  194. uint32_t u32Count;
  195. if (sc == 0)
  196. {
  197. for (u32Count = 0; u32Count != u32WriteBytes; u32Count++)
  198. {
  199. while (inpw(REG_SC0_STATUS) & SC_STATUS_TXFULL_Msk); // Wait 'til FIFO not full
  200. outpw(REG_SC0_DAT, pu8TxBuf[u32Count]); // Write 1 byte to FIFO
  201. }
  202. }
  203. else
  204. {
  205. for (u32Count = 0; u32Count != u32WriteBytes; u32Count++)
  206. {
  207. while (inpw(REG_SC0_STATUS) & SC_STATUS_TXFULL_Msk); // Wait 'til FIFO not full
  208. outpw(REG_SC1_DAT, pu8TxBuf[u32Count]); // Write 1 byte to FIFO
  209. }
  210. }
  211. }
  212. /*@}*/ /* end of group SCUART_EXPORTED_FUNCTIONS */
  213. /*@}*/ /* end of group SCUART_Driver */
  214. /*@}*/ /* end of group Standard_Driver */