stm32f10x_bkp.c 11 KB

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