RTC.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /********************************** (C) COPYRIGHT *******************************
  2. * File Name : RTC.c
  3. * Author : WCH
  4. * Version : V1.2
  5. * Date : 2022/01/18
  6. * Description : RTC configuration and its initialization
  7. *********************************************************************************
  8. * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
  9. * Attention: This software (modified or not) and binary are used for
  10. * microcontroller manufactured by Nanjing Qinheng Microelectronics.
  11. *******************************************************************************/
  12. /******************************************************************************/
  13. /* Header file contains */
  14. #include "HAL.h"
  15. /*********************************************************************
  16. * CONSTANTS
  17. */
  18. #define RTC_INIT_TIME_HOUR 0
  19. #define RTC_INIT_TIME_MINUTE 0
  20. #define RTC_INIT_TIME_SECEND 0
  21. /***************************************************
  22. * Global variables
  23. */
  24. volatile uint32_t RTCTigFlag;
  25. /*******************************************************************************
  26. * @fn RTC_SetTignTime
  27. *
  28. * @brief Configure RTC trigger time
  29. *
  30. * @param time - Trigger time.
  31. *
  32. * @return None.
  33. */
  34. void RTC_SetTignTime(uint32_t time)
  35. {
  36. RTC_WaitForLastTask();
  37. RTC_SetAlarm(time);
  38. RTC_WaitForLastTask();
  39. RTCTigFlag = 0;
  40. }
  41. /*******************************************************************************
  42. * @fn HAL_Time0Init
  43. *
  44. * @brief System timer initialization
  45. *
  46. * @param None.
  47. *
  48. * @return None.
  49. */
  50. void HAL_TimeInit(void)
  51. {
  52. uint16_t temp=0;
  53. uint8_t state=0;
  54. bleClockConfig_t conf={0};
  55. RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR|RCC_APB1Periph_BKP, ENABLE);
  56. PWR_BackupAccessCmd(ENABLE);
  57. #if( CLK_OSC32K )
  58. RCC_LSICmd(ENABLE);
  59. RCC_LSEConfig(RCC_LSE_OFF);
  60. RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
  61. #else
  62. RCC_LSEConfig(RCC_LSE_ON);
  63. /* Check the specified RCC logo position settings or not,
  64. * wait for the low-speed crystal oscillator to be ready */
  65. while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
  66. {
  67. temp++;
  68. Delay_Ms(10);
  69. }
  70. if(temp>=250)
  71. {
  72. printf("time error..\n");
  73. }
  74. RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
  75. #endif
  76. RCC_RTCCLKCmd(ENABLE);
  77. RTC_WaitForLastTask();
  78. RTC_WaitForLastTask();
  79. RTC_SetPrescaler(1);
  80. RTC_WaitForLastTask();
  81. RTC_SetCounter(0);
  82. RTC_WaitForLastTask();
  83. #if( CLK_OSC32K )
  84. Lib_Calibration_LSI();
  85. #endif
  86. conf.ClockAccuracy = CLK_OSC32K?1000:100;
  87. conf.ClockFrequency = CAB_LSIFQ/2;
  88. conf.ClockMaxCount = 0xFFFFFFFF;
  89. conf.getClockValue = RTC_GetCounter;
  90. state = TMOS_TimerInit( &conf );
  91. if(state)
  92. {
  93. PRINT("TMOS_TimerInit err %x\n",state);
  94. }
  95. }
  96. __attribute__((interrupt("WCH-Interrupt-fast")))
  97. void RTCAlarm_IRQHandler(void)
  98. {
  99. RTCTigFlag = 1;
  100. EXTI_ClearITPendingBit(EXTI_Line17);
  101. RTC_ClearITPendingBit(RTC_IT_ALR);
  102. RTC_WaitForLastTask();
  103. }
  104. /******************************** endfile @ time ******************************/