gd32f10x_bkp.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /**
  2. ******************************************************************************
  3. * @brief BKP functions of the firmware library.
  4. ******************************************************************************
  5. */
  6. /* Includes ------------------------------------------------------------------*/
  7. #include "gd32f10x_bkp.h"
  8. #include "gd32f10x_rcc.h"
  9. /** @addtogroup GD32F10x_Firmware
  10. * @{
  11. */
  12. /** @defgroup BKP
  13. * @brief BKP driver modules
  14. * @{
  15. */
  16. /** @defgroup BKP_Private_Defines
  17. * @{
  18. */
  19. /* RTCOUTPUT bit is masked in BKP_RCCR register */
  20. #define RTCOUTPUT_MASK ((uint16_t)0xFC7F)
  21. /**
  22. * @}
  23. */
  24. /** @defgroup BKP_Private_Functions
  25. * @{
  26. */
  27. /**
  28. * @brief Reset the BKP peripheral registers.
  29. * @param None
  30. * @retval None
  31. */
  32. void BKP_DeInit(void)
  33. {
  34. RCC_BackupReset_Enable(ENABLE);
  35. RCC_BackupReset_Enable(DISABLE);
  36. }
  37. /**
  38. * @brief Write user data to the BKP_DRx register.
  39. * @param BKP_DR: the Backup Data Register.
  40. * This parameter can be BKP_DRx where x can be (1..42)
  41. * @param Data: data to write
  42. * @retval None
  43. */
  44. void BKP_WriteBackupRegister(uint16_t BKP_DR, uint16_t Data)
  45. {
  46. __IO uint32_t temp = 0;
  47. temp = (uint32_t)BKP_BASE;
  48. temp += BKP_DR;
  49. /* Store the write data */
  50. *(__IO uint16_t *) temp = Data;
  51. }
  52. /**
  53. * @brief Read data from the BKP_DRx register.
  54. * @param BKP_DR: The Backup Data Register.
  55. * This parameter can be BKP_DRx where x can be (1..42)
  56. * @retval The content of the BKP_DRx register.
  57. */
  58. uint16_t BKP_ReadBackupRegister(uint16_t BKP_DR)
  59. {
  60. __IO uint32_t temp = 0;
  61. temp = (uint32_t)BKP_BASE;
  62. temp += BKP_DR;
  63. /* Return the BKP_DRx register value */
  64. return (*(__IO uint16_t *) temp);
  65. }
  66. /**
  67. * @brief Configure the RTC output on the Tamper pin.
  68. * @param BKP_RTCOUTPUT: the RTC output.
  69. * This parameter can be one of the following values:
  70. * @arg BKP_RTCOUTPUT_NULL: no RTC output on the TAMPER pin.
  71. * @arg BKP_RTCOUTPUT_CLKCAL: output the RTC clock with frequency
  72. * divided by 64 on the TAMPER pin.
  73. * @arg BKP_RTCOUTPUT_ALARM: output the RTC Alarm pulse signal on
  74. * the TAMPER pin.
  75. * @arg BKP_RTCOUTPUT_SECOND: output the RTC Second pulse signal on
  76. * the TAMPER pin.
  77. * @retval None
  78. */
  79. void BKP_RTCOutputConfig(uint16_t BKP_RTCOUTPUT)
  80. {
  81. uint16_t temp = 0;
  82. temp = BKP->RCCR;
  83. /* Clear RCCOE, ROE and ROS bits */
  84. temp &= RTCOUTPUT_MASK;
  85. /* Set RCCOE, ROE and ROS bits according to BKP_RTCOUTPUT value */
  86. temp |= BKP_RTCOUTPUT;
  87. /* Store the new value */
  88. BKP->RCCR = temp;
  89. }
  90. /**
  91. * @brief Set RTC Clock Calibration value.
  92. * @param CalibrationValue: the RTC Clock Calibration value.
  93. * This parameter must be a number between 0 and 0x7F.
  94. * @retval None
  95. */
  96. void BKP_SetRTCCalibrationValue(uint8_t CalibrationValue)
  97. {
  98. uint16_t temp = 0;
  99. temp = BKP->RCCR;
  100. /* Clear RCCV[6:0] bits */
  101. temp &= ~BKP_RCCR_RCCV;
  102. /* Set CAL[6:0] bits according to CalibrationValue */
  103. temp |= CalibrationValue;
  104. /* Store the new value */
  105. BKP->RCCR = temp;
  106. }
  107. /**
  108. * @brief Configure the TAMPER pin active level.
  109. * @param BKP_TPAL: the TAMPER pin active level.
  110. * This parameter can be one of the following values:
  111. * @arg BKP_TPAL_HIGH: TAMPER pin active on high level
  112. * @arg BKP_TPAL_LOW: TAMPER pin active on low level
  113. * @param NewValue: New value of the TAMPER pin state.
  114. * This parameter can be: ENABLE or DISABLE.
  115. * @retval None
  116. */
  117. void BKP_TamperPinConfig(uint16_t BKP_TPAL, TypeState NewValue)
  118. {
  119. uint16_t temp = 0;
  120. temp = BKP->TPCR;
  121. /* Clear TPE bit */
  122. temp &= ~((uint16_t)BKP_TPCR_TPE);
  123. /* Configure TPAL bit according to BKP_TPAL value */
  124. temp |= BKP_TPAL;
  125. /* Store the new value */
  126. BKP->TPCR = temp;
  127. /* Enable the TAMPER pin */
  128. if (NewValue != DISABLE) {
  129. /* The TAMPER pin is dedicated for the Backup Reset function */
  130. BKP->TPCR |= BKP_TPCR_TPE;
  131. } else {
  132. /* The TAMPER pin is free for GPIO functions */
  133. BKP->TPCR &= ~((uint16_t)BKP_TPCR_TPE);
  134. }
  135. }
  136. /**
  137. * @brief Enable or disable the Tamper Interrupt.
  138. * @param NewValue: New value of the Tamper Interrupt.
  139. * This parameter can be: ENABLE or DISABLE.
  140. * @retval None
  141. */
  142. void BKP_TamperINT_Enable(TypeState NewValue)
  143. {
  144. if (NewValue != DISABLE) {
  145. /* Enable the tamper interrupt */
  146. BKP->TIER |= BKP_TIER_TIE;
  147. } else {
  148. /* Disable the tamper interrupt */
  149. BKP->TIER &= ~BKP_TIER_TIE;
  150. }
  151. }
  152. /**
  153. * @brief Get the bit flag of Tamper Event.
  154. * @param None
  155. * @retval The new value of Tamper Event flag (SET or RESET).
  156. */
  157. TypeState BKP_GetBitState(void)
  158. {
  159. /* Check and get the Tamper Event flag */
  160. if ((BKP->TIER & BKP_TIER_TEF) != (uint16_t)RESET) {
  161. /* Tamper Event occured */
  162. return SET;
  163. } else {
  164. /* No Tamper Event occured */
  165. return RESET;
  166. }
  167. }
  168. /**
  169. * @brief Clear the bit flag of Tamper Event.
  170. * @param None
  171. * @retval None
  172. */
  173. void BKP_ClearBitState(void)
  174. {
  175. /* Set the TER bit to clear Tamper Event flag */
  176. BKP->TIER |= BKP_TIER_TER;
  177. }
  178. /**
  179. * @brief Get the interrupt bit flag of Tamper Interrupt.
  180. * @param None
  181. * @retval The new value of the Tamper Interrupt flag (SET or RESET).
  182. */
  183. TypeState BKP_GetIntBitState(void)
  184. {
  185. /* Check and get the Tamper Interrupt flag */
  186. if ((BKP->TIER & BKP_TIER_TIF) != (uint16_t)RESET) {
  187. /* Tamper Interrupt occured */
  188. return SET;
  189. } else {
  190. /* No Tamper Interrupt occured */
  191. return RESET;
  192. }
  193. }
  194. /**
  195. * @brief Clear the interrupt bit flag of Tamper Interrupt.
  196. * @param None
  197. * @retval None
  198. */
  199. void BKP_ClearIntBitState(void)
  200. {
  201. /* Set the TIR bit to clear Tamper Interrupt flag */
  202. BKP->TIER |= BKP_TIER_TIR;
  203. }
  204. /**
  205. * @}
  206. */
  207. /**
  208. * @}
  209. */
  210. /**
  211. * @}
  212. */