stm32f10x_bkp.c 8.3 KB

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