rtc.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /**
  2. ******************************************************************************
  3. * File Name : RTC.c
  4. * Description : This file provides code for the configuration
  5. * of the RTC instances.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under Ultimate Liberty license
  13. * SLA0044, the "License"; You may not use this file except in compliance with
  14. * the License. You may obtain a copy of the License at:
  15. * www.st.com/SLA0044
  16. *
  17. ******************************************************************************
  18. */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "rtc.h"
  21. /* USER CODE BEGIN 0 */
  22. RTC_DateTypeDef currentDate;
  23. RTC_TimeTypeDef currentTime;
  24. RTC_DateTypeDef setDate;
  25. RTC_TimeTypeDef setTime;
  26. struct TIMER timer[TIMER_COUNT];
  27. struct BLINK_TIMER blinker[BLINKER_COUNT];
  28. struct BREATHE_TIMER breathe;
  29. struct FLICKER_TIMER flicker[FLICKER_COUNT];
  30. /* USER CODE END 0 */
  31. RTC_HandleTypeDef hrtc;
  32. /* RTC init function */
  33. void MX_RTC_Init(void)
  34. {
  35. RTC_TimeTypeDef sTime = {0};
  36. RTC_DateTypeDef sDate = {0};
  37. /** Initialize RTC Only
  38. */
  39. hrtc.Instance = RTC;
  40. hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
  41. hrtc.Init.AsynchPrediv = 127;
  42. hrtc.Init.SynchPrediv = 255;
  43. hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
  44. hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  45. hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  46. if (HAL_RTC_Init(&hrtc) != HAL_OK)
  47. {
  48. Error_Handler();
  49. }
  50. /* USER CODE BEGIN Check_RTC_BKUP */
  51. /* USER CODE END Check_RTC_BKUP */
  52. /** Initialize RTC and set the Time and Date
  53. */
  54. sTime.Hours = 0;
  55. sTime.Minutes = 0;
  56. sTime.Seconds = 0;
  57. sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  58. sTime.StoreOperation = RTC_STOREOPERATION_RESET;
  59. if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK)
  60. {
  61. Error_Handler();
  62. }
  63. sDate.WeekDay = RTC_WEEKDAY_MONDAY;
  64. sDate.Month = RTC_MONTH_JANUARY;
  65. sDate.Date = 1;
  66. sDate.Year = 0;
  67. if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK)
  68. {
  69. Error_Handler();
  70. }
  71. }
  72. void HAL_RTC_MspInit(RTC_HandleTypeDef* rtcHandle)
  73. {
  74. if(rtcHandle->Instance==RTC)
  75. {
  76. /* USER CODE BEGIN RTC_MspInit 0 */
  77. /* USER CODE END RTC_MspInit 0 */
  78. /* RTC clock enable */
  79. __HAL_RCC_RTC_ENABLE();
  80. /* USER CODE BEGIN RTC_MspInit 1 */
  81. /* USER CODE END RTC_MspInit 1 */
  82. }
  83. }
  84. void HAL_RTC_MspDeInit(RTC_HandleTypeDef* rtcHandle)
  85. {
  86. if(rtcHandle->Instance==RTC)
  87. {
  88. /* USER CODE BEGIN RTC_MspDeInit 0 */
  89. /* USER CODE END RTC_MspDeInit 0 */
  90. /* Peripheral clock disable */
  91. __HAL_RCC_RTC_DISABLE();
  92. /* USER CODE BEGIN RTC_MspDeInit 1 */
  93. /* USER CODE END RTC_MspDeInit 1 */
  94. }
  95. }
  96. /* USER CODE BEGIN 1 */
  97. void timerEnable(uint8_t tmrIdx, uint32_t timeoutSpec)
  98. {
  99. timer[tmrIdx].timeoutSpec = timeoutSpec;
  100. timer[tmrIdx].isEnable = ON;
  101. }
  102. void timerDisable(uint8_t tmrIdx)
  103. {
  104. timer[tmrIdx].isEnable = OFF;
  105. }
  106. void timerRefresh(uint8_t tmrIdx)
  107. {
  108. timer[tmrIdx].startTime = HAL_GetTick();
  109. timer[tmrIdx].isAlarm = OFF;
  110. }
  111. void blinkerTimeSet(uint8_t idx, uint16_t on, uint16_t off, uint16_t rest, uint8_t count)
  112. {
  113. blinker[idx].blinkOnSpec = on;
  114. blinker[idx].blinkOffSpec = off;
  115. blinker[idx].blinkRestSpec = rest;
  116. blinker[idx].blinkCountSpec = count;
  117. blinker[idx].blinkisFinish = OFF ;
  118. }
  119. void disblinkerTime (uint8_t idx)
  120. {
  121. blinker[idx].isOn = 0 ; // <- this function only set 1 times , No re-entry allowed in loop
  122. blinker[idx].blinkOnSpec = 0 ;
  123. blinker[idx].blinkOffSpec = 0 ;
  124. blinker[idx].blinkRestSpec = 0 ;
  125. blinker[idx].blinkCountSpec = 0 ;
  126. blinker[idx].blinkCount = 0 ;
  127. }
  128. void flickerTimeSet(uint8_t idx, uint16_t on, uint16_t off)
  129. {
  130. flicker[idx].OnSpec = on;
  131. flicker[idx].OffSpec = off;
  132. }
  133. void breatheTimeSet(uint16_t up, uint16_t down, uint16_t R_color, uint16_t G_color, uint16_t B_color, uint16_t W_color)
  134. {
  135. breathe.timeUp = up;
  136. breathe.timeDown = down;
  137. breathe.RGB[0] = R_color;
  138. breathe.RGB[1] = G_color;
  139. breathe.RGB[2] = B_color;
  140. breathe.RGB[3] = W_color;
  141. }
  142. void LedOnRGBSet(uint16_t R_color, uint16_t G_color, uint16_t B_color, uint16_t W_color)
  143. {
  144. Charger.led_pwm_duty[0] = R_color;
  145. Charger.led_pwm_duty[1] = G_color;
  146. Charger.led_pwm_duty[2] = B_color;
  147. Charger.led_pwm_duty[3] = W_color;
  148. }
  149. uint32_t timerGetTimePass(uint8_t tmrIdx)
  150. {
  151. return ((HAL_GetTick()-timer[tmrIdx].startTime)/1000);
  152. }
  153. /* USER CODE END 1 */
  154. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/