main.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. /*
  14. This structure is used to hold the values representing a calendar time.
  15. It contains the following members, with the meanings as shown.
  16. int tm_sec seconds after minute [0-61] (61 allows for 2 leap-seconds)
  17. int tm_min minutes after hour [0-59]
  18. int tm_hour hours after midnight [0-23]
  19. int tm_mday day of the month [1-31]
  20. int tm_mon month of year [0-11]
  21. int tm_year current year-1900
  22. int tm_wday days since Sunday [0-6]
  23. int tm_yday days since January 1st [0-365]
  24. int tm_isdst daylight savings indicator (1 = yes, 0 = no, -1 = unknown)
  25. */
  26. #define WAKEUP_TEST FALSE
  27. #include <string.h>
  28. #include <stdlib.h>
  29. #include "ch.h"
  30. #include "hal.h"
  31. #include "shell.h"
  32. #include "chprintf.h"
  33. #if WAKEUP_TEST
  34. static RTCWakeup wakeupspec;
  35. #endif
  36. static RTCAlarm alarmspec;
  37. static RTCDateTime timespec;
  38. static time_t unix_time;
  39. /*
  40. * Awake state indicator thread
  41. */
  42. static THD_WORKING_AREA(blinkWA, 128);
  43. static THD_FUNCTION(blink_thd, arg){
  44. (void)arg;
  45. while (true) {
  46. chThdSleepMilliseconds(100);
  47. palTogglePad(GPIOC, GPIOC_LED);
  48. }
  49. }
  50. /*
  51. * Helper functions putting MCU in sleep state
  52. */
  53. static void anabiosis(void) {
  54. chSysLock();
  55. SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
  56. PWR->CR |= (PWR_CR_PDDS | PWR_CR_LPDS | PWR_CR_CSBF | PWR_CR_CWUF);
  57. RTC->ISR &= ~(RTC_ISR_ALRBF | RTC_ISR_ALRAF | RTC_ISR_WUTF | RTC_ISR_TAMP1F |
  58. RTC_ISR_TSOVF | RTC_ISR_TSF);
  59. __WFI();
  60. }
  61. /*
  62. * Console applet for sleep testing
  63. */
  64. static void cmd_sleep(BaseSequentialStream *chp, int argc, char *argv[]) {
  65. (void)argv;
  66. if (argc > 0) {
  67. chprintf(chp, "Usage: sleep\r\n");
  68. return;
  69. }
  70. chprintf(chp, "Going sleep...\r\n");
  71. chThdSleepMilliseconds(200);
  72. anabiosis();
  73. }
  74. /*
  75. * Console applet for periodic alaram testing
  76. */
  77. static void cmd_alarm(BaseSequentialStream *chp, int argc, char *argv[]) {
  78. int i = 0;
  79. (void)argv;
  80. if (argc < 1) {
  81. goto ERROR;
  82. }
  83. if ((argc == 1) && (strcmp(argv[0], "get") == 0)) {
  84. rtcGetAlarm(&RTCD1, 0, &alarmspec);
  85. i = (alarmspec.alrmr & 0b1111) + ((alarmspec.alrmr >> 4) & 0b111) * 10;
  86. chprintf(chp, "%U%s", i," - alarm in seconds\r\n");
  87. return;
  88. }
  89. if ((argc == 2) && (strcmp(argv[0], "set") == 0)) {
  90. i = atol(argv[1]);
  91. if (i > 59)
  92. goto ERROR;
  93. /* first disable all alrams if any */
  94. rtcSetAlarm(&RTCD1, 0, NULL);
  95. rtcSetAlarm(&RTCD1, 1, NULL);
  96. /* now set alarm only A */
  97. alarmspec.alrmr = ((i / 10) << 4) | (i % 10) |
  98. RTC_ALRMAR_MSK4 | RTC_ALRMAR_MSK3 | RTC_ALRMAR_MSK2;
  99. rtcSetAlarm(&RTCD1, 0, &alarmspec);
  100. return;
  101. }
  102. else {
  103. goto ERROR;
  104. }
  105. ERROR:
  106. chprintf(chp, "Usage: alarm get\r\n");
  107. chprintf(chp, " alarm set N\r\n");
  108. chprintf(chp, "where N is alarm second on every minute\r\n");
  109. chprintf(chp, "To test alarm functionality perform following steps:\r\n");
  110. chprintf(chp, "1) set alarm second using this command\r\n");
  111. chprintf(chp, "2) execute 'sleep' command\r\n");
  112. chprintf(chp, "3) wait until the red led starts blinking\r\n");
  113. chprintf(chp, "4) immediately execute 'date get' command\r\n");
  114. chprintf(chp, "5) check seconds's field in returned date.\r\n");
  115. chprintf(chp, " It must be close to programmed alarm second\r\n");
  116. }
  117. /*
  118. * helper function
  119. */
  120. static time_t GetTimeUnixSec(void) {
  121. struct tm tim;
  122. rtcGetTime(&RTCD1, &timespec);
  123. rtcConvertDateTimeToStructTm(&timespec, &tim, NULL);
  124. return mktime(&tim);
  125. }
  126. /*
  127. * helper function
  128. */
  129. static void GetTimeTm(struct tm *timp) {
  130. rtcGetTime(&RTCD1, &timespec);
  131. rtcConvertDateTimeToStructTm(&timespec, timp, NULL);
  132. }
  133. /*
  134. * helper function
  135. */
  136. static void SetTimeUnixSec(time_t unix_time) {
  137. struct tm tim;
  138. struct tm *canary;
  139. /* If the conversion is successful the function returns a pointer
  140. to the object the result was written into.*/
  141. canary = localtime_r(&unix_time, &tim);
  142. osalDbgCheck(&tim == canary);
  143. rtcConvertStructTmToDateTime(&tim, 0, &timespec);
  144. rtcSetTime(&RTCD1, &timespec);
  145. }
  146. /*
  147. * Console applet for date set and get
  148. */
  149. static void cmd_date(BaseSequentialStream *chp, int argc, char *argv[]) {
  150. (void)argv;
  151. struct tm timp = {0};
  152. if (argc == 0) {
  153. goto ERROR;
  154. }
  155. if ((argc == 1) && (strcmp(argv[0], "get") == 0)){
  156. unix_time = GetTimeUnixSec();
  157. if (unix_time == -1){
  158. chprintf(chp, "incorrect time in RTC cell\r\n");
  159. }
  160. else{
  161. chprintf(chp, "%D%s", unix_time, "\r\n");
  162. GetTimeTm(&timp);
  163. chprintf(chp, "%s", asctime(&timp));
  164. }
  165. return;
  166. }
  167. if ((argc == 2) && (strcmp(argv[0], "set") == 0)){
  168. unix_time = atol(argv[1]);
  169. if (unix_time > 0){
  170. SetTimeUnixSec(unix_time);
  171. return;
  172. }
  173. else{
  174. goto ERROR;
  175. }
  176. }
  177. else{
  178. goto ERROR;
  179. }
  180. ERROR:
  181. chprintf(chp, "Usage: date get\r\n");
  182. chprintf(chp, " date set N\r\n");
  183. chprintf(chp, "where N is time in seconds sins Unix epoch\r\n");
  184. chprintf(chp, "you can get current N value from unix console by the command\r\n");
  185. chprintf(chp, "%s", "date +\%s\r\n");
  186. return;
  187. }
  188. /*
  189. *
  190. */
  191. static SerialConfig ser_cfg = {
  192. 115200,
  193. 0,
  194. 0,
  195. 0,
  196. };
  197. /*
  198. *
  199. */
  200. static const ShellCommand commands[] = {
  201. {"alarm", cmd_alarm},
  202. {"date", cmd_date},
  203. {"sleep", cmd_sleep},
  204. {NULL, NULL}
  205. };
  206. /*
  207. *
  208. */
  209. static const ShellConfig shell_cfg1 = {
  210. (BaseSequentialStream *)&SD6,
  211. commands
  212. };
  213. /*
  214. * working area for shell thread
  215. */
  216. static THD_WORKING_AREA(waShell, 1024);
  217. /**
  218. * Main function.
  219. */
  220. int main(void){
  221. halInit();
  222. chSysInit();
  223. chThdCreateStatic(blinkWA, sizeof(blinkWA), NORMALPRIO, blink_thd, NULL);
  224. #if WAKEUP_TEST
  225. /* set wakeup */
  226. wakeupspec.wutr = ((uint32_t)4) << 16; /* select 1 Hz clock source */
  227. wakeupspec.wutr |= 9; /* set counter value to 9. Period will be 9+1 seconds. */
  228. rtcSTM32SetPeriodicWakeup(&RTCD1, &wakeupspec);
  229. osalThreadSleepSeconds(3);
  230. anabiosis();
  231. #else
  232. /* switch off wakeup */
  233. rtcSTM32SetPeriodicWakeup(&RTCD1, NULL);
  234. /* Shell initialization.*/
  235. sdStart(&SD6, &ser_cfg);
  236. shellInit();
  237. chThdCreateStatic(waShell, sizeof(waShell), NORMALPRIO,
  238. shellThread, (void *)&shell_cfg1);
  239. /* wait until user do not want to test wakeup */
  240. while (true){
  241. osalThreadSleepMilliseconds(200);
  242. }
  243. #endif /* WAKEUP_TEST */
  244. return 0;
  245. }