stm32f10x_bkp.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /**
  2. ******************************************************************************
  3. * @file stm32f10x_bkp.c
  4. * @author MCD Application Team
  5. * @version V3.3.0
  6. * @date 04/16/2010
  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, 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 2010 STMicroelectronics</center></h2>
  19. */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "stm32f10x_bkp.h"
  22. #include "stm32f10x_rcc.h"
  23. /** @addtogroup STM32F10x_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 reset values.
  93. * @param None
  94. * @retval None
  95. */
  96. void BKP_DeInit(void)
  97. {
  98. RCC_BackupResetCmd(ENABLE);
  99. RCC_BackupResetCmd(DISABLE);
  100. }
  101. /**
  102. * @brief Configures the Tamper Pin active level.
  103. * @param BKP_TamperPinLevel: specifies the Tamper Pin active level.
  104. * This parameter can be one of the following values:
  105. * @arg BKP_TamperPinLevel_High: Tamper pin active on high level
  106. * @arg BKP_TamperPinLevel_Low: Tamper pin active on low level
  107. * @retval None
  108. */
  109. void BKP_TamperPinLevelConfig(uint16_t BKP_TamperPinLevel)
  110. {
  111. /* Check the parameters */
  112. assert_param(IS_BKP_TAMPER_PIN_LEVEL(BKP_TamperPinLevel));
  113. *(__IO uint32_t *) CR_TPAL_BB = BKP_TamperPinLevel;
  114. }
  115. /**
  116. * @brief Enables or disables the Tamper Pin activation.
  117. * @param NewState: new state of the Tamper Pin activation.
  118. * This parameter can be: ENABLE or DISABLE.
  119. * @retval None
  120. */
  121. void BKP_TamperPinCmd(FunctionalState NewState)
  122. {
  123. /* Check the parameters */
  124. assert_param(IS_FUNCTIONAL_STATE(NewState));
  125. *(__IO uint32_t *) CR_TPE_BB = (uint32_t)NewState;
  126. }
  127. /**
  128. * @brief Enables or disables the Tamper Pin Interrupt.
  129. * @param NewState: new state of the Tamper Pin Interrupt.
  130. * This parameter can be: ENABLE or DISABLE.
  131. * @retval None
  132. */
  133. void BKP_ITConfig(FunctionalState NewState)
  134. {
  135. /* Check the parameters */
  136. assert_param(IS_FUNCTIONAL_STATE(NewState));
  137. *(__IO uint32_t *) CSR_TPIE_BB = (uint32_t)NewState;
  138. }
  139. /**
  140. * @brief Select the RTC output source to output on the Tamper pin.
  141. * @param BKP_RTCOutputSource: specifies the RTC output source.
  142. * This parameter can be one of the following values:
  143. * @arg BKP_RTCOutputSource_None: no RTC output on the Tamper pin.
  144. * @arg BKP_RTCOutputSource_CalibClock: output the RTC clock with frequency
  145. * divided by 64 on the Tamper pin.
  146. * @arg BKP_RTCOutputSource_Alarm: output the RTC Alarm pulse signal on
  147. * the Tamper pin.
  148. * @arg BKP_RTCOutputSource_Second: output the RTC Second pulse signal on
  149. * the Tamper pin.
  150. * @retval None
  151. */
  152. void BKP_RTCOutputConfig(uint16_t BKP_RTCOutputSource)
  153. {
  154. uint16_t tmpreg = 0;
  155. /* Check the parameters */
  156. assert_param(IS_BKP_RTC_OUTPUT_SOURCE(BKP_RTCOutputSource));
  157. tmpreg = BKP->RTCCR;
  158. /* Clear CCO, ASOE and ASOS bits */
  159. tmpreg &= RTCCR_Mask;
  160. /* Set CCO, ASOE and ASOS bits according to BKP_RTCOutputSource value */
  161. tmpreg |= BKP_RTCOutputSource;
  162. /* Store the new value */
  163. BKP->RTCCR = tmpreg;
  164. }
  165. /**
  166. * @brief Sets RTC Clock Calibration value.
  167. * @param CalibrationValue: specifies the RTC Clock Calibration value.
  168. * This parameter must be a number between 0 and 0x7F.
  169. * @retval None
  170. */
  171. void BKP_SetRTCCalibrationValue(uint8_t CalibrationValue)
  172. {
  173. uint16_t tmpreg = 0;
  174. /* Check the parameters */
  175. assert_param(IS_BKP_CALIBRATION_VALUE(CalibrationValue));
  176. tmpreg = BKP->RTCCR;
  177. /* Clear CAL[6:0] bits */
  178. tmpreg &= RTCCR_CAL_Mask;
  179. /* Set CAL[6:0] bits according to CalibrationValue value */
  180. tmpreg |= CalibrationValue;
  181. /* Store the new value */
  182. BKP->RTCCR = tmpreg;
  183. }
  184. /**
  185. * @brief Writes user data to the specified Data Backup Register.
  186. * @param BKP_DR: specifies the Data Backup Register.
  187. * This parameter can be BKP_DRx where x:[1, 42]
  188. * @param Data: data to write
  189. * @retval None
  190. */
  191. void BKP_WriteBackupRegister(uint16_t BKP_DR, uint16_t Data)
  192. {
  193. __IO uint32_t tmp = 0;
  194. /* Check the parameters */
  195. assert_param(IS_BKP_DR(BKP_DR));
  196. tmp = (uint32_t)BKP_BASE;
  197. tmp += BKP_DR;
  198. *(__IO uint32_t *) tmp = Data;
  199. }
  200. /**
  201. * @brief Reads data from the specified Data Backup Register.
  202. * @param BKP_DR: specifies the Data Backup Register.
  203. * This parameter can be BKP_DRx where x:[1, 42]
  204. * @retval The content of the specified Data Backup Register
  205. */
  206. uint16_t BKP_ReadBackupRegister(uint16_t BKP_DR)
  207. {
  208. __IO uint32_t tmp = 0;
  209. /* Check the parameters */
  210. assert_param(IS_BKP_DR(BKP_DR));
  211. tmp = (uint32_t)BKP_BASE;
  212. tmp += BKP_DR;
  213. return (*(__IO uint16_t *) tmp);
  214. }
  215. /**
  216. * @brief Checks whether the Tamper Pin Event flag is set or not.
  217. * @param None
  218. * @retval The new state of the Tamper Pin Event flag (SET or RESET).
  219. */
  220. FlagStatus BKP_GetFlagStatus(void)
  221. {
  222. return (FlagStatus)(*(__IO uint32_t *) CSR_TEF_BB);
  223. }
  224. /**
  225. * @brief Clears Tamper Pin Event pending flag.
  226. * @param None
  227. * @retval None
  228. */
  229. void BKP_ClearFlag(void)
  230. {
  231. /* Set CTE bit to clear Tamper Pin Event flag */
  232. BKP->CSR |= CSR_CTE_Set;
  233. }
  234. /**
  235. * @brief Checks whether the Tamper Pin Interrupt has occurred or not.
  236. * @param None
  237. * @retval The new state of the Tamper Pin Interrupt (SET or RESET).
  238. */
  239. ITStatus BKP_GetITStatus(void)
  240. {
  241. return (ITStatus)(*(__IO uint32_t *) CSR_TIF_BB);
  242. }
  243. /**
  244. * @brief Clears Tamper Pin Interrupt pending bit.
  245. * @param None
  246. * @retval None
  247. */
  248. void BKP_ClearITPendingBit(void)
  249. {
  250. /* Set CTI bit to clear Tamper Pin Interrupt pending bit */
  251. BKP->CSR |= CSR_CTI_Set;
  252. }
  253. /**
  254. * @}
  255. */
  256. /**
  257. * @}
  258. */
  259. /**
  260. * @}
  261. */
  262. /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/