rtc.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #include "rtc.h"
  2. #include "stm32f4xx_hal.h"
  3. void RTC_Init()
  4. {
  5. /* Peripheral clock enable */
  6. __HAL_RCC_RTC_ENABLE();
  7. /* Peripheral interrupt init*/
  8. HAL_NVIC_SetPriority(RTC_WKUP_IRQn, 7, 0);
  9. HAL_NVIC_EnableIRQ(RTC_WKUP_IRQn);
  10. /* Disable the write protection for RTC registers */
  11. RTC->WPR = 0xCA;
  12. RTC->WPR = 0x53;
  13. /* Check if the Initialization mode is set */
  14. if((RTC->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
  15. {
  16. /* Set the Initialization mode */
  17. RTC->ISR = (uint32_t)RTC_INIT_MASK;
  18. /* Wait till RTC is in INIT state and if Time out is reached exit */
  19. while((RTC->ISR & RTC_ISR_INITF) == (uint32_t)RESET);
  20. }
  21. /* Clear RTC_CR FMT, OSEL and POL Bits */
  22. RTC->CR &= ((uint32_t)~(RTC_CR_FMT | RTC_CR_OSEL | RTC_CR_POL));
  23. /* Set RTC_CR register */
  24. RTC->CR |= (uint32_t)(RTC_HOURFORMAT_24 | RTC_OUTPUT_DISABLE | RTC_OUTPUT_POLARITY_HIGH);
  25. /* Configure the RTC PRER */
  26. RTC->PRER = (uint32_t)(255);
  27. RTC->PRER |= (uint32_t)(127 << 16);
  28. /* Exit Initialization mode */
  29. RTC->ISR &= (uint32_t)~RTC_ISR_INIT;
  30. RTC->TAFCR &= (uint32_t)~RTC_TAFCR_ALARMOUTTYPE;
  31. RTC->TAFCR |= (uint32_t)(RTC_OUTPUT_TYPE_OPENDRAIN);
  32. /* Enable the write protection for RTC registers */
  33. RTC->WPR = 0xFF;
  34. /**Enable the WakeUp
  35. */
  36. //HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, 59, RTC_WAKEUPCLOCK_CK_SPRE_16BITS);
  37. }