fm33lc0xx_fl_u7816.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /**
  2. *******************************************************************************************************
  3. * @file fm33lc0xx_fl_u7816.c
  4. * @author FMSH Application Team
  5. * @brief Src file of U7816 FL Module
  6. *******************************************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) [2019] [Fudan Microelectronics]
  10. * THIS SOFTWARE is licensed under the Mulan PSL v1.
  11. * can use this software according to the terms and conditions of the Mulan PSL v1.
  12. * You may obtain a copy of Mulan PSL v1 at:
  13. * http://license.coscl.org.cn/MulanPSL
  14. * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
  16. * PURPOSE.
  17. * See the Mulan PSL v1 for more details.
  18. *
  19. *******************************************************************************************************
  20. */
  21. #include "fm33lc0xx_fl_rcc.h"
  22. #include "fm33lc0xx_fl_rmu.h"
  23. #include "fm33lc0xx_fl_u7816.h"
  24. #include "fm33_assert.h"
  25. /** @addtogroup FM33LC0XX_FL_Driver
  26. * @{
  27. */
  28. /** @addtogroup U7816
  29. * @{
  30. */
  31. /* Private macros ------------------------------------------------------------*/
  32. /** @addtogroup U7816_FL_Private_Macros
  33. * @{
  34. */
  35. #define IS_FL_U7816_INSTANCE(INTANCE) ((INTANCE) == U7816)
  36. #define IS_FL_U7816_CLOCK_FRQUENCE(__VALUE__) (((__VALUE__) >=1000000)||\
  37. ((__VALUE__) <= 5000000))
  38. #define IS_FL_U7816_TX_PARITHERROR_AUTO_RETRY(__VALUE__) (((__VALUE__) == FL_DISABLE)||\
  39. ((__VALUE__) == FL_ENABLE))
  40. #define IS_FL_U7816_RETRY_CNT(__VALUE__) (((__VALUE__) == FL_U7816_RETRY_COUNT_1)||\
  41. ((__VALUE__) == FL_U7816_RETRY_COUNT_3))
  42. #define IS_FL_U7816_BLOCKGUARD(__VALUE__) (((__VALUE__) == FL_ENABLE)||\
  43. ((__VALUE__) == FL_DISABLE))
  44. #define IS_FL_U7816_AUTO_PULL(__VALUE__) (((__VALUE__) == FL_ENABLE)||\
  45. ((__VALUE__) == FL_DISABLE))
  46. #define IS_FL_U7816_PARITH(__VALUE__) (((__VALUE__) == FL_U7816_PARITY_EVEN)||\
  47. ((__VALUE__) == FL_U7816_PARITY_ODD )||\
  48. ((__VALUE__) == FL_U7816_PARITY_ALWAYS_1)||\
  49. ((__VALUE__) == FL_U7816_PARITY_NONE))
  50. #define IS_FL_U7816_TX_GUARD(__VALUE__) (((__VALUE__) == FL_U7816_TX_GUARD_TIME_2ETU)||\
  51. ((__VALUE__) == FL_U7816_TX_GUARD_TIME_3ETU))
  52. #define IS_FL_U7816_RX_GUARD(__VALUE__) (((__VALUE__) == FL_U7816_RX_GUARD_TIME_2ETU)||\
  53. ((__VALUE__) == FL_U7816_RX_GUARD_TIME_1ETU))
  54. #define IS_FL_U7816_ERROR_GUARD(__VALUE__) (((__VALUE__) == FL_U7816_ERROR_GUARD_TIME_2ETU)||\
  55. ((__VALUE__) == FL_U7816_ERROR_GUARD_TIME_1ETU))
  56. #define IS_FL_U7816_ERROR_SIGNALWIDTH(__VALUE__) (((__VALUE__) == FL_U7816_ERROR_SIGNAL_WIDTH_2ETU)||\
  57. ((__VALUE__) == FL_U7816_ERROR_SIGNAL_WIDTH_1P5ETU)||\
  58. ((__VALUE__) == FL_U7816_ERROR_SIGNAL_WIDTH_1ETU))
  59. #define IS_FL_U7816_RX_AUTO_ERROR_SIG(__VALUE__) (((__VALUE__) == FL_DISABLE)||\
  60. ((__VALUE__) == FL_ENABLE))
  61. #define IS_FL_U7816_BIT_DIRECTION(__VALUE__) (((__VALUE__) == FL_U7816_BIT_ORDER_LSB_FIRST)||\
  62. ((__VALUE__) == FL_U7816_BIT_ORDER_MSB_FIRST))
  63. /**
  64. * @}
  65. */
  66. /** @addtogroup UART_FL_EF_Init
  67. * @{
  68. */
  69. /**
  70. * @brief 复位U7816外设
  71. * @param U7816x 外设入口地址
  72. * @retval 错误状态,可能值:
  73. * -FL_PASS 外设寄存器值恢复复位值
  74. * -FL_FAIL 未成功执行
  75. */
  76. FL_ErrorStatus FL_U7816_DeInit(U7816_Type *U7816x)
  77. {
  78. assert_param(IS_FL_U7816_INSTANCE(U7816x));
  79. /* 使能外设复位 */
  80. FL_RCC_EnablePeripheralReset();
  81. /* 复位U7816外设寄存器 */
  82. FL_RCC_EnableResetAPB1Peripheral(FL_RCC_RSTAPB_U7816);
  83. FL_RCC_DisableResetAPB1Peripheral(FL_RCC_RSTAPB_U7816);
  84. /* 关闭外设总线始时钟和工作时钟 */
  85. FL_RCC_DisableGroup3BusClock(FL_RCC_GROUP3_BUSCLK_U7816);
  86. /* 锁定外设复位 */
  87. FL_RCC_DisablePeripheralReset();
  88. return FL_PASS;
  89. }
  90. /**
  91. * @brief 配置U7816
  92. * @param U7816x 外设入口地址
  93. * @param U7816_InitStruct 指向 @ref FL_U7816_InitTypeDef 结构体的指针
  94. * @retval 错误状态,可能值:
  95. * -FL_PASS 外设寄存器值恢复复位值
  96. * -FL_FAIL 未成功执行
  97. */
  98. FL_ErrorStatus FL_U7816_Init(U7816_Type *U7816x, FL_U7816_InitTypeDef *U7816_InitStruct)
  99. {
  100. uint32_t Fclk;
  101. uint32_t tempClkdiv;
  102. /* 参数检查 */
  103. assert_param(IS_FL_U7816_INSTANCE(U7816x));
  104. assert_param(IS_FL_U7816_CLOCK_FRQUENCE(U7816_InitStruct->outputClockFreqence));
  105. assert_param(IS_FL_U7816_TX_PARITHERROR_AUTO_RETRY(U7816_InitStruct->txAutoRetry));
  106. assert_param(IS_FL_U7816_RETRY_CNT(U7816_InitStruct->retryCnt));
  107. assert_param(IS_FL_U7816_BLOCKGUARD(U7816_InitStruct->blockGuard));
  108. assert_param(IS_FL_U7816_PARITH(U7816_InitStruct->parity));
  109. assert_param(IS_FL_U7816_RX_GUARD(U7816_InitStruct->rxGuardTime));
  110. assert_param(IS_FL_U7816_ERROR_GUARD(U7816_InitStruct->errorGuardTime));
  111. assert_param(IS_FL_U7816_ERROR_SIGNALWIDTH(U7816_InitStruct->errorSignalWidth));
  112. assert_param(IS_FL_U7816_RX_AUTO_ERROR_SIG(U7816_InitStruct->rxAutoErrorSignal));
  113. assert_param(IS_FL_U7816_BIT_DIRECTION(U7816_InitStruct->transferOrder));
  114. assert_param(IS_FL_U7816_AUTO_PULL(U7816_InitStruct->strongPullUp));
  115. assert_param(IS_FL_U7816_TX_GUARD(U7816_InitStruct->txGuardTime));
  116. /* 时钟使能 */
  117. FL_RCC_EnableGroup3BusClock(FL_RCC_GROUP3_BUSCLK_U7816);
  118. /* 卡时钟 */
  119. Fclk = FL_RCC_GetAPB1ClockFreq();
  120. tempClkdiv = Fclk / U7816_InitStruct->outputClockFreqence - 1;
  121. FL_U7816_WriteClockDivision(U7816x, tempClkdiv);
  122. /* 发送收到error signal后自动重发 */
  123. if(U7816_InitStruct->txAutoRetry == FL_ENABLE)
  124. {
  125. FL_U7816_EnableTXParityErrorAutoRetry(U7816x);
  126. }
  127. else
  128. {
  129. FL_U7816_DisableTXParityErrorAutoRetry(U7816x);
  130. }
  131. /* 发送失败重试次数 */
  132. FL_U7816_SetRetryCount(U7816x, U7816_InitStruct->retryCnt);
  133. /* 发送一次之间的保护时间单位etu */
  134. FL_U7816_SetTXGuardTime(U7816x, U7816_InitStruct->txGuardTime);
  135. /* 强上拉*/
  136. if(U7816_InitStruct->strongPullUp == FL_ENABLE)
  137. {
  138. FL_U7816_EnablePullup(U7816x);
  139. }
  140. else
  141. {
  142. FL_U7816_DisablePullup(U7816x);
  143. }
  144. /* 块保护,插入block guard time */
  145. if(U7816_InitStruct->blockGuard == FL_ENABLE)
  146. {
  147. FL_U7816_EnableBlockGuardTime(U7816x);
  148. }
  149. else
  150. {
  151. FL_U7816_DisableBlockGuardTime(U7816x);
  152. }
  153. /* 校验位 */
  154. FL_U7816_SetParity(U7816x, U7816_InitStruct->parity);
  155. /* 接收一次之间的保护时间单位etu */
  156. FL_U7816_SetRXGuardTime(U7816x, U7816_InitStruct->rxGuardTime);
  157. /* 错误之后的保护时间单位etu */
  158. FL_U7816_SetErrorGuardTime(U7816x, U7816_InitStruct->errorGuardTime);
  159. /* 错误信号时间 单位etu */
  160. FL_U7816_SetErrorSignalWidth(U7816x, U7816_InitStruct->errorSignalWidth);
  161. /* 接收校验错是否自动重发error signal */
  162. if(U7816_InitStruct->rxAutoErrorSignal == FL_ENABLE)
  163. {
  164. FL_U7816_EnableRXParityErrorAutoRetry(U7816x);
  165. }
  166. else
  167. {
  168. FL_U7816_DisableRXParityErrorAutoRetry(U7816x);
  169. }
  170. /* 传输bit方向 */
  171. FL_U7816_SetBitOrder(U7816x, U7816_InitStruct->transferOrder);
  172. /* baud */
  173. FL_U7816_WriteBaudRate(U7816x, U7816_InitStruct->baud);
  174. /* 额外保护时间单位etu */
  175. FL_U7816_WriteExtraGuardTime(U7816x, U7816_InitStruct->extraGuardTime);
  176. return FL_PASS;
  177. }
  178. /**
  179. * @brief 将 @ref FL_U7816_InitTypeDef 结构体初始化为默认配置
  180. * @param U7816_InitStruct 指向 @ref FL_U7816_InitTypeDef 结构体的指针
  181. *
  182. * @retval None
  183. */
  184. void FL_U7816_StructInit(FL_U7816_InitTypeDef *U7816_InitStruct)
  185. {
  186. U7816_InitStruct->outputClockFreqence = 4000000;
  187. U7816_InitStruct->txAutoRetry = FL_ENABLE;
  188. U7816_InitStruct->retryCnt = FL_U7816_RETRY_COUNT_1;
  189. U7816_InitStruct->strongPullUp = FL_ENABLE;
  190. U7816_InitStruct->blockGuard = FL_DISABLE;
  191. U7816_InitStruct->parity = FL_U7816_PARITY_EVEN;
  192. U7816_InitStruct->rxGuardTime = FL_U7816_RX_GUARD_TIME_2ETU;
  193. U7816_InitStruct->txGuardTime = FL_U7816_TX_GUARD_TIME_2ETU;
  194. U7816_InitStruct->errorGuardTime = FL_U7816_ERROR_GUARD_TIME_1ETU;
  195. U7816_InitStruct->errorSignalWidth = FL_U7816_ERROR_SIGNAL_WIDTH_2ETU;
  196. U7816_InitStruct->rxAutoErrorSignal = FL_ENABLE;
  197. U7816_InitStruct->transferOrder = FL_U7816_BIT_ORDER_LSB_FIRST;
  198. U7816_InitStruct->baud = 372 - 1;
  199. U7816_InitStruct->extraGuardTime = 0;
  200. }
  201. /**
  202. * @}
  203. */
  204. /**
  205. * @}
  206. */
  207. /**
  208. * @}
  209. */
  210. /******************************************* END OF FILE *******************************************/