ch32v30x_bkp.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /********************************** (C) COPYRIGHT *******************************
  2. * File Name : ch32v30x_bkp.c
  3. * Author : WCH
  4. * Version : V1.0.0
  5. * Date : 2024/03/06
  6. * Description : This file provides all the BKP firmware functions.
  7. *********************************************************************************
  8. * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
  9. * Attention: This software (modified or not) and binary are used for
  10. * microcontroller manufactured by Nanjing Qinheng Microelectronics.
  11. *******************************************************************************/
  12. #include "ch32v30x_bkp.h"
  13. #include "ch32v30x_rcc.h"
  14. /* BKP registers bit mask */
  15. /* OCTLR register bit mask */
  16. #define OCTLR_CAL_MASK ((uint16_t)0xFF80)
  17. #define OCTLR_MASK ((uint16_t)0xFC7F)
  18. /*********************************************************************
  19. * @fn BKP_DeInit
  20. *
  21. * @brief Deinitializes the BKP peripheral registers to their default reset values.
  22. *
  23. * @return none
  24. */
  25. void BKP_DeInit(void)
  26. {
  27. RCC_BackupResetCmd(ENABLE);
  28. RCC_BackupResetCmd(DISABLE);
  29. }
  30. /*********************************************************************
  31. * @fn BKP_TamperPinLevelConfig
  32. *
  33. * @brief Configures the Tamper Pin active level.
  34. *
  35. * @param BKP_TamperPinLevel: specifies the Tamper Pin active level.
  36. * BKP_TamperPinLevel_High - Tamper pin active on high level.
  37. * BKP_TamperPinLevel_Low - Tamper pin active on low level.
  38. *
  39. * @return none
  40. */
  41. void BKP_TamperPinLevelConfig(uint16_t BKP_TamperPinLevel)
  42. {
  43. if(BKP_TamperPinLevel)
  44. {
  45. BKP->TPCTLR |= (1 << 1);
  46. }
  47. else
  48. {
  49. BKP->TPCTLR &= ~(1 << 1);
  50. }
  51. }
  52. /*********************************************************************
  53. * @fn BKP_TamperPinCmd
  54. *
  55. * @brief Enables or disables the Tamper Pin activation.
  56. *
  57. * @param NewState - ENABLE or DISABLE.
  58. *
  59. * @return none
  60. */
  61. void BKP_TamperPinCmd(FunctionalState NewState)
  62. {
  63. if(NewState)
  64. {
  65. BKP->TPCTLR |= (1 << 0);
  66. }
  67. else
  68. {
  69. BKP->TPCTLR &= ~(1 << 0);
  70. }
  71. }
  72. /*********************************************************************
  73. * @fn BKP_ITConfig
  74. *
  75. * @brief Enables or disables the Tamper Pin Interrupt.
  76. *
  77. * @param NewState - ENABLE or DISABLE.
  78. *
  79. * @return none
  80. */
  81. void BKP_ITConfig(FunctionalState NewState)
  82. {
  83. if(NewState)
  84. {
  85. BKP->TPCSR |= (1 << 2);
  86. }
  87. else
  88. {
  89. BKP->TPCSR &= ~(1 << 2);
  90. }
  91. }
  92. /*********************************************************************
  93. * @fn BKP_RTCOutputConfig
  94. *
  95. * @brief Select the RTC output source to output on the Tamper pin.
  96. *
  97. * @param BKP_RTCOutputSource - specifies the RTC output source.
  98. * BKP_RTCOutputSource_None - no RTC output on the Tamper pin.
  99. * BKP_RTCOutputSource_CalibClock - output the RTC clock with
  100. * frequency divided by 64 on the Tamper pin.
  101. * BKP_RTCOutputSource_Alarm - output the RTC Alarm pulse signal
  102. * on the Tamper pin.
  103. * BKP_RTCOutputSource_Second - output the RTC Second pulse
  104. * signal on the Tamper pin.
  105. *
  106. * @return none
  107. */
  108. void BKP_RTCOutputConfig(uint16_t BKP_RTCOutputSource)
  109. {
  110. uint16_t tmpreg = 0;
  111. tmpreg = BKP->OCTLR;
  112. tmpreg &= OCTLR_MASK;
  113. tmpreg |= BKP_RTCOutputSource;
  114. BKP->OCTLR = tmpreg;
  115. }
  116. /*********************************************************************
  117. * @fn BKP_SetRTCCalibrationValue
  118. *
  119. * @brief Sets RTC Clock Calibration value.
  120. *
  121. * @param CalibrationValue - specifies the RTC Clock Calibration value.
  122. * This parameter must be a number between 0 and 0x7F.
  123. *
  124. * @return none
  125. */
  126. void BKP_SetRTCCalibrationValue(uint8_t CalibrationValue)
  127. {
  128. uint16_t tmpreg = 0;
  129. tmpreg = BKP->OCTLR;
  130. tmpreg &= OCTLR_CAL_MASK;
  131. tmpreg |= CalibrationValue;
  132. BKP->OCTLR = tmpreg;
  133. }
  134. /*********************************************************************
  135. * @fn BKP_WriteBackupRegister
  136. *
  137. * @brief Writes user data to the specified Data Backup Register.
  138. *
  139. * @param BKP_DR - specifies the Data Backup Register.
  140. * Data - data to write.
  141. *
  142. * @return none
  143. */
  144. void BKP_WriteBackupRegister(uint16_t BKP_DR, uint16_t Data)
  145. {
  146. __IO uint32_t tmp = 0;
  147. tmp = (uint32_t)BKP_BASE;
  148. tmp += BKP_DR;
  149. *(__IO uint32_t *)tmp = Data;
  150. }
  151. /*********************************************************************
  152. * @fn BKP_ReadBackupRegister
  153. *
  154. * @brief Reads data from the specified Data Backup Register.
  155. *
  156. * @param BKP_DR - specifies the Data Backup Register.
  157. * This parameter can be BKP_DRx where x=[1, 42].
  158. *
  159. * @return none
  160. */
  161. uint16_t BKP_ReadBackupRegister(uint16_t BKP_DR)
  162. {
  163. __IO uint32_t tmp = 0;
  164. tmp = (uint32_t)BKP_BASE;
  165. tmp += BKP_DR;
  166. return (*(__IO uint16_t *)tmp);
  167. }
  168. /*********************************************************************
  169. * @fn BKP_GetFlagStatus
  170. *
  171. * @brief Checks whether the Tamper Pin Event flag is set or not.
  172. *
  173. * @return FlagStatus - SET or RESET.
  174. */
  175. FlagStatus BKP_GetFlagStatus(void)
  176. {
  177. if(BKP->TPCSR & (1 << 8))
  178. {
  179. return SET;
  180. }
  181. else
  182. {
  183. return RESET;
  184. }
  185. }
  186. /*********************************************************************
  187. * @fn BKP_ClearFlag
  188. *
  189. * @brief Clears Tamper Pin Event pending flag.
  190. *
  191. * @return none
  192. */
  193. void BKP_ClearFlag(void)
  194. {
  195. BKP->TPCSR |= BKP_CTE;
  196. }
  197. /*********************************************************************
  198. * @fn BKP_GetITStatus
  199. *
  200. * @brief Checks whether the Tamper Pin Interrupt has occurred or not.
  201. *
  202. * @return ITStatus - SET or RESET.
  203. */
  204. ITStatus BKP_GetITStatus(void)
  205. {
  206. if(BKP->TPCSR & (1 << 9))
  207. {
  208. return SET;
  209. }
  210. else
  211. {
  212. return RESET;
  213. }
  214. }
  215. /*********************************************************************
  216. * @fn BKP_ClearITPendingBit
  217. *
  218. * @brief Clears Tamper Pin Interrupt pending bit.
  219. *
  220. * @return none
  221. */
  222. void BKP_ClearITPendingBit(void)
  223. {
  224. BKP->TPCSR |= BKP_CTI;
  225. }