HAL_bkp.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /**
  2. ******************************************************************************
  3. * @file HAL_bkp.c
  4. * @author AE Team
  5. * @version V1.0.0
  6. * @date 28/7/2017
  7. * @brief This file provides all the BKP firmware functions.
  8. ******************************************************************************
  9. * @copy
  10. *
  11. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  12. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  13. * TIME. AS A RESULT, MindMotion SHALL NOT BE HELD LIABLE FOR ANY
  14. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  15. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  16. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  17. *
  18. * <h2><center>&copy; COPYRIGHT 2017 MindMotion</center></h2>
  19. */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "HAL_bkp.h"
  22. #include "HAL_rcc.h"
  23. /** @addtogroup StdPeriph_Driver
  24. * @{
  25. */
  26. /** @defgroup BKP
  27. * @brief BKP driver modules
  28. * @{
  29. */
  30. /** @defgroup BKP_Private_TypesDefinitions
  31. * @{
  32. */
  33. /**
  34. * @}
  35. */
  36. /** @defgroup BKP_Private_Defines
  37. * @{
  38. */
  39. /* ------------ BKP registers bit address in the alias region --------------- */
  40. #define BKP_OFFSET (BKP_BASE - PERIPH_BASE)
  41. /* --- CR Register ----*/
  42. /* Alias word address of TPAL bit */
  43. #define CR_OFFSET (BKP_OFFSET + 0x30)
  44. #define TPAL_BitNumber 0x01
  45. #define CR_TPAL_BB (PERIPH_BB_BASE + (CR_OFFSET * 32) + (TPAL_BitNumber * 4))
  46. /* Alias word address of TPE bit */
  47. #define TPE_BitNumber 0x00
  48. #define CR_TPE_BB (PERIPH_BB_BASE + (CR_OFFSET * 32) + (TPE_BitNumber * 4))
  49. /* --- CSR Register ---*/
  50. /* Alias word address of TPIE bit */
  51. #define CSR_OFFSET (BKP_OFFSET + 0x34)
  52. #define TPIE_BitNumber 0x02
  53. #define CSR_TPIE_BB (PERIPH_BB_BASE + (CSR_OFFSET * 32) + (TPIE_BitNumber * 4))
  54. /* Alias word address of TIF bit */
  55. #define TIF_BitNumber 0x09
  56. #define CSR_TIF_BB (PERIPH_BB_BASE + (CSR_OFFSET * 32) + (TIF_BitNumber * 4))
  57. /* Alias word address of TEF bit */
  58. #define TEF_BitNumber 0x08
  59. #define CSR_TEF_BB (PERIPH_BB_BASE + (CSR_OFFSET * 32) + (TEF_BitNumber * 4))
  60. /* ---------------------- BKP registers bit mask ------------------------ */
  61. /* RTCCR register bit mask */
  62. #define RTCCR_CAL_Mask ((uint16_t)0xFF80)
  63. #define RTCCR_Mask ((uint16_t)0xFC7F)
  64. /* CSR register bit mask */
  65. #define CSR_CTE_Set ((uint16_t)0x0001)
  66. #define CSR_CTI_Set ((uint16_t)0x0002)
  67. /**
  68. * @}
  69. */
  70. /** @defgroup BKP_Private_Macros
  71. * @{
  72. */
  73. /**
  74. * @}
  75. */
  76. /** @defgroup BKP_Private_Variables
  77. * @{
  78. */
  79. /**
  80. * @}
  81. */
  82. /** @defgroup BKP_Private_FunctionPrototypes
  83. * @{
  84. */
  85. /**
  86. * @}
  87. */
  88. /** @defgroup BKP_Private_Functions
  89. * @{
  90. */
  91. /**
  92. * @brief Deinitializes the BKP peripheral registers to their default
  93. * reset values.
  94. * @param None
  95. * @retval : None
  96. */
  97. void BKP_DeInit(void)
  98. {
  99. RCC_BackupResetCmd(ENABLE);
  100. RCC_BackupResetCmd(DISABLE);
  101. }
  102. /**
  103. * @brief Configures the Tamper Pin active level.
  104. * @param BKP_TamperPinLevel: specifies the Tamper Pin active level.
  105. * This parameter can be one of the following values:
  106. * @arg BKP_TamperPinLevel_High: Tamper pin active on high level
  107. * @arg BKP_TamperPinLevel_Low: Tamper pin active on low level
  108. * @retval : None
  109. */
  110. void BKP_TamperPinLevelConfig(uint16_t BKP_TamperPinLevel)
  111. {
  112. /* Check the parameters */
  113. assert_param(IS_BKP_TAMPER_PIN_LEVEL(BKP_TamperPinLevel));
  114. *(__IO uint32_t *) CR_TPAL_BB = BKP_TamperPinLevel;
  115. }
  116. /**
  117. * @brief Enables or disables the Tamper Pin activation.
  118. * @param NewState: new state of the Tamper Pin activation.
  119. * This parameter can be: ENABLE or DISABLE.
  120. * @retval : None
  121. */
  122. void BKP_TamperPinCmd(FunctionalState NewState)
  123. {
  124. /* Check the parameters */
  125. assert_param(IS_FUNCTIONAL_STATE(NewState));
  126. *(__IO uint32_t *) CR_TPE_BB = (uint32_t)NewState;
  127. }
  128. /**
  129. * @brief Enables or disables the Tamper Pin Interrupt.
  130. * @param NewState: new state of the Tamper Pin Interrupt.
  131. * This parameter can be: ENABLE or DISABLE.
  132. * @retval : None
  133. */
  134. void BKP_ITConfig(FunctionalState NewState)
  135. {
  136. /* Check the parameters */
  137. assert_param(IS_FUNCTIONAL_STATE(NewState));
  138. *(__IO uint32_t *) CSR_TPIE_BB = (uint32_t)NewState;
  139. }
  140. /**
  141. * @brief Select the RTC output source to output on the Tamper pin.
  142. * @param BKP_RTCOutputSource: specifies the RTC output source.
  143. * This parameter can be one of the following values:
  144. * @arg BKP_RTCOutputSource_None: no RTC output on the Tamper pin.
  145. * @arg BKP_RTCOutputSource_CalibClock: output the RTC clock
  146. * with frequency divided by 64 on the Tamper pin.
  147. * @arg BKP_RTCOutputSource_Alarm: output the RTC Alarm pulse
  148. * signal on the Tamper pin.
  149. * @arg BKP_RTCOutputSource_Second: output the RTC Second pulse
  150. * signal on the Tamper pin.
  151. * @retval : None
  152. */
  153. void BKP_RTCOutputConfig(uint16_t BKP_RTCOutputSource)
  154. {
  155. uint16_t tmpreg = 0;
  156. /* Check the parameters */
  157. assert_param(IS_BKP_RTC_OUTPUT_SOURCE(BKP_RTCOutputSource));
  158. tmpreg = BKP->RTCCR;
  159. /* Clear CCO, ASOE and ASOS bits */
  160. tmpreg &= RTCCR_Mask;
  161. /* Set CCO, ASOE and ASOS bits according to BKP_RTCOutputSource value */
  162. tmpreg |= BKP_RTCOutputSource;
  163. /* Store the new value */
  164. BKP->RTCCR = tmpreg;
  165. }
  166. /**
  167. * @brief Sets RTC Clock Calibration value.
  168. * @param CalibrationValue: specifies the RTC Clock Calibration value.
  169. * This parameter must be a number between 0 and 0x7F.
  170. * @retval : None
  171. */
  172. void BKP_SetRTCCalibrationValue(uint8_t CalibrationValue)
  173. {
  174. uint16_t tmpreg = 0;
  175. /* Check the parameters */
  176. assert_param(IS_BKP_CALIBRATION_VALUE(CalibrationValue));
  177. tmpreg = BKP->RTCCR;
  178. /* Clear CAL[6:0] bits */
  179. tmpreg &= RTCCR_CAL_Mask;
  180. /* Set CAL[6:0] bits according to CalibrationValue value */
  181. tmpreg |= CalibrationValue;
  182. /* Store the new value */
  183. BKP->RTCCR = tmpreg;
  184. }
  185. /**
  186. * @brief Writes user data to the specified Data Backup Register.
  187. * @param BKP_DR: specifies the Data Backup Register.
  188. * This parameter can be BKP_DRx where x:[1, 42]
  189. * @param Data: data to write
  190. * @retval : None
  191. */
  192. void BKP_WriteBackupRegister(uint16_t BKP_DR, uint16_t Data)
  193. {
  194. /* Check the parameters */
  195. assert_param(IS_BKP_DR(BKP_DR));
  196. *(__IO uint16_t *) (BKP_BASE + BKP_DR) = Data;
  197. }
  198. /**
  199. * @brief Reads data from the specified Data Backup Register.
  200. * @param BKP_DR: specifies the Data Backup Register.
  201. * This parameter can be BKP_DRx where x:[1, 42]
  202. * @retval : The content of the specified Data Backup Register
  203. */
  204. uint16_t BKP_ReadBackupRegister(uint16_t BKP_DR)
  205. {
  206. /* Check the parameters */
  207. assert_param(IS_BKP_DR(BKP_DR));
  208. return (*(__IO uint16_t *) (BKP_BASE + BKP_DR));
  209. }
  210. /**
  211. * @brief Checks whether the Tamper Pin Event flag is set or not.
  212. * @param None
  213. * @retval : The new state of the Tamper Pin Event flag (SET or RESET).
  214. */
  215. FlagStatus BKP_GetFlagStatus(void)
  216. {
  217. return (FlagStatus)(*(__IO uint32_t *) CSR_TEF_BB);
  218. }
  219. /**
  220. * @brief Clears Tamper Pin Event pending flag.
  221. * @param None
  222. * @retval : None
  223. */
  224. void BKP_ClearFlag(void)
  225. {
  226. /* Set CTE bit to clear Tamper Pin Event flag */
  227. BKP->CSR |= CSR_CTE_Set;
  228. }
  229. /**
  230. * @brief Checks whether the Tamper Pin Interrupt has occurred or not.
  231. * @param None
  232. * @retval : The new state of the Tamper Pin Interrupt (SET or RESET).
  233. */
  234. ITStatus BKP_GetITStatus(void)
  235. {
  236. return (ITStatus)(*(__IO uint32_t *) CSR_TIF_BB);
  237. }
  238. /**
  239. * @brief Clears Tamper Pin Interrupt pending bit.
  240. * @param None
  241. * @retval : None
  242. */
  243. void BKP_ClearITPendingBit(void)
  244. {
  245. /* Set CTI bit to clear Tamper Pin Interrupt pending bit */
  246. BKP->CSR |= CSR_CTI_Set;
  247. }
  248. /**
  249. * @}
  250. */
  251. /**
  252. * @}
  253. */
  254. /**
  255. * @}
  256. */
  257. /*-------------------------(C) COPYRIGHT 2017 MindMotion ----------------------*/