hal_bkp.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. ////////////////////////////////////////////////////////////////////////////////
  2. /// @file hal_bkp.c
  3. /// @author AE TEAM
  4. /// @brief THIS FILE PROVIDES ALL THE BKP FIRMWARE FUNCTIONS.
  5. ////////////////////////////////////////////////////////////////////////////////
  6. /// @attention
  7. ///
  8. /// THE EXISTING FIRMWARE IS ONLY FOR REFERENCE, WHICH IS DESIGNED TO PROVIDE
  9. /// CUSTOMERS WITH CODING INFORMATION ABOUT THEIR PRODUCTS SO THEY CAN SAVE
  10. /// TIME. THEREFORE, MINDMOTION SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT OR
  11. /// CONSEQUENTIAL DAMAGES ABOUT ANY CLAIMS ARISING OUT OF THE CONTENT OF SUCH
  12. /// HARDWARE AND/OR THE USE OF THE CODING INFORMATION CONTAINED HEREIN IN
  13. /// CONNECTION WITH PRODUCTS MADE BY CUSTOMERS.
  14. ///
  15. /// <H2><CENTER>&COPY; COPYRIGHT MINDMOTION </CENTER></H2>
  16. ////////////////////////////////////////////////////////////////////////////////
  17. // Define to prevent recursive inclusion
  18. #define _HAL_BKP_C_
  19. // Files includes
  20. #include "types.h"
  21. #include "hal_pwr.h"
  22. #include "hal_rcc.h"
  23. #include "hal_bkp.h"
  24. ////////////////////////////////////////////////////////////////////////////////
  25. /// @addtogroup MM32_Hardware_Abstract_Layer
  26. /// @{
  27. ////////////////////////////////////////////////////////////////////////////////
  28. /// @addtogroup BKP_HAL
  29. /// @{
  30. ////////////////////////////////////////////////////////////////////////////////
  31. /// @addtogroup BKP_Exported_Functions
  32. /// @{
  33. ////////////////////////////////////////////////////////////////////////////////
  34. /// @brief Deinitializes the BKP peripheral registers to their default reset
  35. /// values.
  36. /// @param None.
  37. /// @retval None.
  38. ////////////////////////////////////////////////////////////////////////////////
  39. void BKP_DeInit(void)
  40. {
  41. RCC_BackupResetCmd(ENABLE);
  42. RCC_BackupResetCmd(DISABLE);
  43. }
  44. ////////////////////////////////////////////////////////////////////////////////
  45. /// @brief Configures the Tamper Pin active level.
  46. /// @param tamper_pin_level: specifies the Tamper Pin active level.
  47. /// This parameter can be one of the following values:
  48. /// @arg BKP_TamperPinLevel_High: Tamper pin active on high level
  49. /// @arg BKP_TamperPinLevel_Low: Tamper pin active on low level
  50. /// @retval None.
  51. ////////////////////////////////////////////////////////////////////////////////
  52. void BKP_TamperPinLevelConfig(BKPTPAL_Typedef tamper_pin_level)
  53. {
  54. BKP->CR = tamper_pin_level;
  55. }
  56. ////////////////////////////////////////////////////////////////////////////////
  57. /// @brief Enables or disables the Tamper Pin activation.
  58. /// @param state: new state of the Tamper Pin activation.
  59. /// This parameter can be: ENABLE or DISABLE.
  60. /// @retval None.
  61. ////////////////////////////////////////////////////////////////////////////////
  62. void BKP_TamperPinCmd(FunctionalState state)
  63. {
  64. (state) ? SET_BIT(BKP->CR, BKP_CR_TPE) : CLEAR_BIT(BKP->CR, BKP_CR_TPE);
  65. }
  66. ////////////////////////////////////////////////////////////////////////////////
  67. /// @brief Enables or disables the Tamper Pin Interrupt.
  68. /// @param state: new state of the Tamper Pin Interrupt.
  69. /// This parameter can be: ENABLE or DISABLE.
  70. /// @retval None.
  71. ////////////////////////////////////////////////////////////////////////////////
  72. void BKP_ITConfig(FunctionalState state)
  73. {
  74. (state) ? SET_BIT(BKP->CSR, BKP_CSR_TPIE) : CLEAR_BIT(BKP->CSR, BKP_CSR_TPIE);
  75. }
  76. ////////////////////////////////////////////////////////////////////////////////
  77. /// @brief Select the RTC output source to output on the Tamper pin.
  78. /// @param rtc_output_source: specifies the RTC output source.
  79. /// This parameter can be one of the following values:
  80. /// @arg BKP_RTCOutputSource_None: no RTC output on the Tamper pin.
  81. /// @arg BKP_RTCOutputSource_CalibClock: output the RTC clock with frequency
  82. /// divided by 64 on the Tamper pin.
  83. /// @arg BKP_RTCOutputSource_Alarm: output the RTC Alarm pulse signal on
  84. /// the Tamper pin.
  85. /// @arg BKP_RTCOutputSource_Second: output the RTC Second pulse signal on
  86. /// the Tamper pin.
  87. /// @retval None.
  88. ////////////////////////////////////////////////////////////////////////////////
  89. void BKP_RTCOutputConfig(BKPRTCOUTPUTSRC_Typedef rtc_output_source)
  90. {
  91. MODIFY_REG(BKP->RTCCR, BKP_RTCCR_CCO | BKP_RTCCR_ASOE | BKP_RTCCR_ASOS, rtc_output_source);
  92. }
  93. ////////////////////////////////////////////////////////////////////////////////
  94. /// @brief Sets RTC Clock Calibration value.
  95. /// @param calibration_value: specifies the RTC Clock Calibration value.
  96. /// This parameter must be a number between 0 and 0x7F.
  97. /// @retval None.
  98. ////////////////////////////////////////////////////////////////////////////////
  99. void BKP_SetRTCCalibrationValue(u8 calibration_value)
  100. {
  101. MODIFY_REG(BKP->RTCCR, BKP_RTCCR_CAL, calibration_value);
  102. }
  103. ////////////////////////////////////////////////////////////////////////////////
  104. /// @brief Checks whether the Tamper Pin Event flag is set or not.
  105. /// @param None.
  106. /// @retval State: The new state of the Tamper Pin Event flag (SET or RESET).
  107. ////////////////////////////////////////////////////////////////////////////////
  108. FlagStatus BKP_GetFlagStatus(void)
  109. {
  110. return ((BKP->CSR & BKP_CSR_TEF) ? SET : RESET);
  111. }
  112. ////////////////////////////////////////////////////////////////////////////////
  113. /// @brief Clears Tamper Pin Event pending flag.
  114. /// @param None.
  115. /// @retval None.
  116. ////////////////////////////////////////////////////////////////////////////////
  117. void BKP_ClearFlag(void)
  118. {
  119. SET_BIT(BKP->CSR, BKP_CSR_CTE);
  120. }
  121. ////////////////////////////////////////////////////////////////////////////////
  122. /// @brief Checks whether the Tamper Pin Interrupt has occurred or not.
  123. /// @param None.
  124. /// @retval State: The new state of the Tamper Pin Interrupt (SET or RESET).
  125. ////////////////////////////////////////////////////////////////////////////////
  126. ITStatus BKP_GetITStatus(void)
  127. {
  128. return ((BKP->CSR & BKP_CSR_TIF) ? SET : RESET);
  129. }
  130. ////////////////////////////////////////////////////////////////////////////////
  131. /// @brief Clears Tamper Pin Interrupt pending bit.
  132. /// @param None.
  133. /// @retval None.
  134. ////////////////////////////////////////////////////////////////////////////////
  135. void BKP_ClearITPendingBit(void)
  136. {
  137. SET_BIT(BKP->CSR, BKP_CSR_CTI);
  138. }
  139. ////////////////////////////////////////////////////////////////////////////////
  140. /// @brief Writes user data to the specified data Backup Register.
  141. /// @param bkp_dr: specifies the data Backup Register.
  142. /// This parameter can be BKP_DRx where x:[1, 10]
  143. /// @param data: data to write
  144. /// @retval None.
  145. ////////////////////////////////////////////////////////////////////////////////
  146. void BKP_WriteBackupRegister(BKPDR_Typedef bkp_dr, u16 data)
  147. {
  148. *(vu16*)(BKP_BASE + bkp_dr) = data;
  149. }
  150. ////////////////////////////////////////////////////////////////////////////////
  151. /// @brief Reads data from the specified data Backup Register.
  152. /// @param bkp_dr: specifies the data Backup Register.
  153. /// This parameter can be BKP_DRx where x:[1, 10]
  154. /// @retval data: The content of the specified data Backup Register
  155. ////////////////////////////////////////////////////////////////////////////////
  156. u16 BKP_ReadBackupRegister(BKPDR_Typedef bkp_dr)
  157. {
  158. return (*(vu16*)(BKP_BASE + bkp_dr));
  159. }
  160. ////////////////////////////////////////////////////////////////////////////////
  161. // Extended function interface
  162. ////////////////////////////////////////////////////////////////////////////////
  163. /// @brief Initializes the BKP peripheral, enable access to the backup
  164. /// registers.
  165. /// @param None.
  166. /// @retval None.
  167. ////////////////////////////////////////////////////////////////////////////////
  168. void exBKP_Init(void)
  169. {
  170. RCC_APB1PeriphClockCmd(RCC_APB1ENR_PWR, ENABLE);
  171. //COMMON_EnableIpClock(emCLOCK_PWR);
  172. RCC_APB1PeriphClockCmd(RCC_APB1ENR_BKP, ENABLE);
  173. //COMMON_EnableIpClock(emCLOCK_BKP);
  174. RCC->BDCR |= RCC_BDCR_DBP;
  175. }
  176. ////////////////////////////////////////////////////////////////////////////////
  177. /// @brief Writes user data to the specified data Backup Register immediately.
  178. /// @param bkp_dr: specifies the data Backup Register.
  179. /// This parameter can be BKP_DRx where x:[1, 10]
  180. /// @param data: data to write
  181. /// @retval None.
  182. ////////////////////////////////////////////////////////////////////////////////
  183. void exBKP_ImmWrite(BKPDR_Typedef bkp_dr, u16 dat)
  184. {
  185. RCC->BDCR |= RCC_BDCR_DBP;
  186. *(vu16*)(BKP_BASE + bkp_dr) = dat;
  187. RCC->BDCR &= ~RCC_BDCR_DBP;
  188. }
  189. ////////////////////////////////////////////////////////////////////////////////
  190. /// @brief Reads data from the specified data Backup Register immediately.
  191. /// @param bkp_dr: specifies the data Backup Register.
  192. /// This parameter can be BKP_DRx where x:[1, 10]
  193. /// @retval data: The content of the specified data Backup Register
  194. ////////////////////////////////////////////////////////////////////////////////
  195. u16 exBKP_ImmRead(BKPDR_Typedef bkp_dr)
  196. {
  197. u16 dat;
  198. RCC->BDCR |= RCC_BDCR_DBP;
  199. dat = (*(vu16*)(BKP_BASE + bkp_dr));
  200. RCC->BDCR &= ~RCC_BDCR_DBP;
  201. return dat;
  202. }
  203. /// @}
  204. /// @}
  205. /// @}