main.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #include "ch.h"
  14. #include "hal.h"
  15. RTCDateTime timespec;
  16. RTCAlarm alarmspec;
  17. #define TEST_ALARM_WAKEUP FALSE
  18. #if TEST_ALARM_WAKEUP
  19. /*
  20. * Running indicator thread.
  21. */
  22. static THD_WORKING_AREA(blinkWA, 128);
  23. static THD_FUNCTION(blink_thd, arg) {
  24. (void)arg;
  25. while (true) {
  26. chThdSleepMilliseconds(100);
  27. palTogglePad(GPIOC, GPIOC_LED);
  28. }
  29. return 0;
  30. }
  31. /*
  32. *
  33. */
  34. int main(void) {
  35. uint32_t tv_sec;
  36. halInit();
  37. chSysInit();
  38. chThdCreateStatic(blinkWA, sizeof(blinkWA), NORMALPRIO, blink_thd, NULL);
  39. /* compile ability test */
  40. rtcGetTime(&RTCD1, &timespec);
  41. /* set alarm in near future */
  42. rtcSTM32GetSecMsec(&RTCD1, &tv_sec, NULL);
  43. alarmspec.tv_sec = tv_sec + 20;
  44. rtcSetAlarm(&RTCD1, 0, &alarmspec);
  45. while (true){
  46. chThdSleepSeconds(10);
  47. /* going to anabiosis*/
  48. chSysLock();
  49. PWR->CR |= PWR_CR_CWUF | PWR_CR_CSBF;
  50. PWR->CR |= PWR_CR_PDDS | PWR_CR_LPDS;
  51. SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
  52. __WFI();
  53. }
  54. return 0;
  55. }
  56. #else /* TEST_ALARM_WAKEUP */
  57. /*
  58. * Test alarm period.
  59. */
  60. #define RTC_ALARMPERIOD 10
  61. binary_semaphore_t alarm_sem;
  62. /*
  63. * Alarm callback.
  64. */
  65. static void my_cb(RTCDriver *rtcp, rtcevent_t event) {
  66. (void)rtcp;
  67. switch (event) {
  68. case RTC_EVENT_OVERFLOW:
  69. palTogglePad(GPIOC, GPIOC_LED);
  70. break;
  71. case RTC_EVENT_SECOND:
  72. //palTogglePad(GPIOC, GPIOC_LED);
  73. break;
  74. case RTC_EVENT_ALARM:
  75. palTogglePad(GPIOC, GPIOC_LED);
  76. osalSysLockFromISR();
  77. chBSemSignalI(&alarm_sem);
  78. osalSysUnlockFromISR();
  79. break;
  80. }
  81. }
  82. static time_measurement_t sett, gett;
  83. int main(void) {
  84. msg_t status = MSG_TIMEOUT;
  85. uint32_t tv_sec;
  86. halInit();
  87. chSysInit();
  88. chBSemObjectInit(&alarm_sem, TRUE);
  89. chTMObjectInit(&sett);
  90. chTMObjectInit(&gett);
  91. /* compile ability test */
  92. chTMStartMeasurementX(&gett);
  93. rtcGetTime(&RTCD1, &timespec);
  94. chTMStopMeasurementX(&gett);
  95. rtcSTM32SetSec(&RTCD1, 1414845464);
  96. osalThreadSleepMilliseconds(10);
  97. rtcGetTime(&RTCD1, &timespec);
  98. timespec.month -= 1;
  99. chTMStartMeasurementX(&sett);
  100. rtcSetTime(&RTCD1, &timespec);
  101. chTMStopMeasurementX(&sett);
  102. osalThreadSleepMilliseconds(10);
  103. rtcGetTime(&RTCD1, &timespec);
  104. rtcSTM32GetSecMsec(&RTCD1, &tv_sec, NULL);
  105. alarmspec.tv_sec = tv_sec + RTC_ALARMPERIOD;
  106. rtcSetAlarm(&RTCD1, 0, &alarmspec);
  107. rtcSetCallback(&RTCD1, my_cb);
  108. while (true){
  109. /* Wait until alarm callback signaled semaphore.*/
  110. status = chBSemWaitTimeout(&alarm_sem, TIME_S2I(RTC_ALARMPERIOD + 5));
  111. if (status == MSG_TIMEOUT){
  112. osalSysHalt("time is out");
  113. }
  114. else{
  115. rtcSTM32GetSecMsec(&RTCD1, &tv_sec, NULL);
  116. alarmspec.tv_sec = tv_sec + RTC_ALARMPERIOD;
  117. rtcSetAlarm(&RTCD1, 0, &alarmspec);
  118. }
  119. }
  120. return 0;
  121. }
  122. #endif /* TEST_ALARM_WAKEUP */