low_power.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #include"common.h"
  2. #include "drv_io.h"
  3. #include "drv_usart.h"
  4. #include "drv_usart_2.h"
  5. #include "app.h"
  6. #include "app_rs485_1.h"
  7. #include "app_rs485_2.h"
  8. #include "drv_can.h"
  9. #include "app_adas.h"
  10. #include "measure_vol.h"
  11. #include "app_can.h"
  12. #include "low_power.h"
  13. #include "measure_temprature.h"
  14. DELAY_COMMON enter_sleep_delay;
  15. void LP_Measure_Vol_Initial(void)
  16. {
  17. /* enable GPIOA clock */
  18. rcu_periph_clock_enable(RCU_GPIOA);
  19. rcu_periph_clock_enable(RCU_AF);
  20. /* enable ADC0 clock */
  21. rcu_periph_clock_enable(RCU_ADC0);
  22. /* config ADC clock */
  23. rcu_adc_clock_config(RCU_CKADC_CKAPB2_DIV8);
  24. gpio_init(GPIOA, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_5);
  25. }
  26. void Set_Enter_Sleep_Delay(void)
  27. {
  28. enter_sleep_delay.set = 1;
  29. enter_sleep_delay.count = 0;
  30. }
  31. void Reset_Enter_Sleep_Delay(void)
  32. {
  33. enter_sleep_delay.set = 0;
  34. enter_sleep_delay.count = 0;
  35. }
  36. void Enter_Sleep_Delay_Timeout(void)
  37. {
  38. if(enter_sleep_delay.set)
  39. {
  40. if(++enter_sleep_delay.count >= 15000)
  41. {
  42. memset(&enter_sleep_delay,0x00,sizeof(enter_sleep_delay));
  43. g_event |= ENTER_SLEEP_EVENT;
  44. }
  45. }
  46. }
  47. void RTC_Open(void)
  48. {
  49. nvic_irq_enable(RTC_IRQn,1,0);
  50. /* enable PMU and BKPI clocks */
  51. rcu_periph_clock_enable(RCU_BKPI);
  52. rcu_periph_clock_enable(RCU_PMU);
  53. /* allow access to BKP domain */
  54. pmu_backup_write_enable();
  55. /* reset backup domain */
  56. bkp_deinit();
  57. exti_init(EXTI_17, EXTI_INTERRUPT, EXTI_TRIG_RISING);
  58. exti_interrupt_flag_clear(EXTI_17);
  59. exti_interrupt_enable(EXTI_17);
  60. /* enable LXTAL */
  61. rcu_osci_on(RCU_IRC40K);
  62. /* wait till LXTAL is ready */
  63. rcu_osci_stab_wait(RCU_IRC40K);
  64. /* select RCU_LXTAL as RTC clock source */
  65. rcu_rtc_clock_config(RCU_RTCSRC_IRC40K);
  66. /* enable RTC Clock */
  67. rcu_periph_clock_enable(RCU_RTC);
  68. /* wait for RTC registers synchronization */
  69. rtc_register_sync_wait();
  70. /* wait until last write operation on RTC registers has finished */
  71. rtc_lwoff_wait();
  72. /* enable the RTC second interrupt*/
  73. rtc_interrupt_enable(RTC_INT_ALARM);
  74. /* wait until last write operation on RTC registers has finished */
  75. rtc_lwoff_wait();
  76. /* set RTC prescaler: set RTC period to 1s */
  77. rtc_prescaler_set(40);
  78. /* wait until last write operation on RTC registers has finished */
  79. rtc_lwoff_wait();
  80. rtc_lwoff_wait();
  81. rtc_alarm_config(1000);
  82. rtc_lwoff_wait();
  83. }
  84. void Enter_Sleep(void)
  85. {
  86. uint32_t temp;
  87. #if DEBUG_MODE == 0
  88. Feed_Watch_Dog();
  89. #endif
  90. systick_close();
  91. usart_deinit(USART0);
  92. usart_deinit(USART1);
  93. can_deinit(CAN0);
  94. RTC_Open();
  95. //while(1);
  96. SLEEP:
  97. rcu_periph_clock_enable(RCU_PMU);
  98. pmu_to_deepsleepmode(PMU_LDO_LOWPOWER, WFI_CMD);
  99. #if 1
  100. SystemInit();
  101. systick_config();
  102. LP_Measure_Vol_Initial();
  103. temp = Measure_Vol();
  104. systick_close();
  105. if(temp > 20000 && temp < 120000)
  106. {
  107. __set_FAULTMASK(1);
  108. NVIC_SystemReset();
  109. }
  110. else
  111. {
  112. #if DEBUG_MODE == 0
  113. Feed_Watch_Dog();
  114. #endif
  115. goto SLEEP;
  116. }
  117. #else
  118. SystemInit();
  119. __set_FAULTMASK(1);
  120. NVIC_SystemReset();
  121. #endif
  122. }
  123. void Wake_Up(void)
  124. {
  125. }
  126. void Low_Power_Initial(void)
  127. {
  128. Set_Enter_Sleep_Delay();
  129. }
  130. void RTC_IRQHandler(void)
  131. {
  132. if (rtc_flag_get(RTC_FLAG_SECOND) != RESET)
  133. {
  134. /* clear the RTC second interrupt flag*/
  135. rtc_flag_clear(RTC_FLAG_SECOND);
  136. }
  137. if (rtc_flag_get(RTC_FLAG_ALARM) != RESET)
  138. {
  139. /* clear the RTC second interrupt flag*/
  140. rtc_flag_clear(RTC_FLAG_ALARM);
  141. exti_interrupt_flag_clear(EXTI_17);
  142. //rtc_alarm_config(5000);
  143. rtc_lwoff_wait();
  144. rtc_counter_set(0x00);
  145. rtc_lwoff_wait();
  146. }
  147. }