123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- #define WAKEUP_TEST FALSE
- #include <string.h>
- #include <stdlib.h>
- #include "ch.h"
- #include "hal.h"
- #include "shell.h"
- #include "chprintf.h"
- #if WAKEUP_TEST
- static RTCWakeup wakeupspec;
- #endif
- static RTCAlarm alarmspec;
- static RTCDateTime timespec;
- static time_t unix_time;
- static THD_WORKING_AREA(blinkWA, 128);
- static THD_FUNCTION(blink_thd, arg){
- (void)arg;
- while (true) {
- chThdSleepMilliseconds(100);
- palTogglePad(GPIOC, GPIOC_LED);
- }
- }
- static void anabiosis(void) {
- chSysLock();
- SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
- PWR->CR |= (PWR_CR_PDDS | PWR_CR_LPDS | PWR_CR_CSBF | PWR_CR_CWUF);
- RTC->ISR &= ~(RTC_ISR_ALRBF | RTC_ISR_ALRAF | RTC_ISR_WUTF | RTC_ISR_TAMP1F |
- RTC_ISR_TSOVF | RTC_ISR_TSF);
- __WFI();
- }
- static void cmd_sleep(BaseSequentialStream *chp, int argc, char *argv[]) {
- (void)argv;
- if (argc > 0) {
- chprintf(chp, "Usage: sleep\r\n");
- return;
- }
- chprintf(chp, "Going sleep...\r\n");
- chThdSleepMilliseconds(200);
- anabiosis();
- }
- static void cmd_alarm(BaseSequentialStream *chp, int argc, char *argv[]) {
- int i = 0;
- (void)argv;
- if (argc < 1) {
- goto ERROR;
- }
- if ((argc == 1) && (strcmp(argv[0], "get") == 0)) {
- rtcGetAlarm(&RTCD1, 0, &alarmspec);
- i = (alarmspec.alrmr & 0b1111) + ((alarmspec.alrmr >> 4) & 0b111) * 10;
- chprintf(chp, "%U%s", i," - alarm in seconds\r\n");
- return;
- }
- if ((argc == 2) && (strcmp(argv[0], "set") == 0)) {
- i = atol(argv[1]);
- if (i > 59)
- goto ERROR;
-
- rtcSetAlarm(&RTCD1, 0, NULL);
- rtcSetAlarm(&RTCD1, 1, NULL);
-
- alarmspec.alrmr = ((i / 10) << 4) | (i % 10) |
- RTC_ALRMAR_MSK4 | RTC_ALRMAR_MSK3 | RTC_ALRMAR_MSK2;
- rtcSetAlarm(&RTCD1, 0, &alarmspec);
- return;
- }
- else {
- goto ERROR;
- }
- ERROR:
- chprintf(chp, "Usage: alarm get\r\n");
- chprintf(chp, " alarm set N\r\n");
- chprintf(chp, "where N is alarm second on every minute\r\n");
- chprintf(chp, "To test alarm functionality perform following steps:\r\n");
- chprintf(chp, "1) set alarm second using this command\r\n");
- chprintf(chp, "2) execute 'sleep' command\r\n");
- chprintf(chp, "3) wait until the red led starts blinking\r\n");
- chprintf(chp, "4) immediately execute 'date get' command\r\n");
- chprintf(chp, "5) check seconds's field in returned date.\r\n");
- chprintf(chp, " It must be close to programmed alarm second\r\n");
- }
- static time_t GetTimeUnixSec(void) {
- struct tm tim;
- rtcGetTime(&RTCD1, ×pec);
- rtcConvertDateTimeToStructTm(×pec, &tim, NULL);
- return mktime(&tim);
- }
- static void GetTimeTm(struct tm *timp) {
- rtcGetTime(&RTCD1, ×pec);
- rtcConvertDateTimeToStructTm(×pec, timp, NULL);
- }
- static void SetTimeUnixSec(time_t unix_time) {
- struct tm tim;
- struct tm *canary;
-
- canary = localtime_r(&unix_time, &tim);
- osalDbgCheck(&tim == canary);
- rtcConvertStructTmToDateTime(&tim, 0, ×pec);
- rtcSetTime(&RTCD1, ×pec);
- }
- static void cmd_date(BaseSequentialStream *chp, int argc, char *argv[]) {
- (void)argv;
- struct tm timp = {0};
- if (argc == 0) {
- goto ERROR;
- }
- if ((argc == 1) && (strcmp(argv[0], "get") == 0)){
- unix_time = GetTimeUnixSec();
- if (unix_time == -1){
- chprintf(chp, "incorrect time in RTC cell\r\n");
- }
- else{
- chprintf(chp, "%D%s", unix_time, "\r\n");
- GetTimeTm(&timp);
- chprintf(chp, "%s", asctime(&timp));
- }
- return;
- }
- if ((argc == 2) && (strcmp(argv[0], "set") == 0)){
- unix_time = atol(argv[1]);
- if (unix_time > 0){
- SetTimeUnixSec(unix_time);
- return;
- }
- else{
- goto ERROR;
- }
- }
- else{
- goto ERROR;
- }
- ERROR:
- chprintf(chp, "Usage: date get\r\n");
- chprintf(chp, " date set N\r\n");
- chprintf(chp, "where N is time in seconds sins Unix epoch\r\n");
- chprintf(chp, "you can get current N value from unix console by the command\r\n");
- chprintf(chp, "%s", "date +\%s\r\n");
- return;
- }
- static SerialConfig ser_cfg = {
- 115200,
- 0,
- 0,
- 0,
- };
- static const ShellCommand commands[] = {
- {"alarm", cmd_alarm},
- {"date", cmd_date},
- {"sleep", cmd_sleep},
- {NULL, NULL}
- };
- static const ShellConfig shell_cfg1 = {
- (BaseSequentialStream *)&SD6,
- commands
- };
- static THD_WORKING_AREA(waShell, 1024);
- int main(void){
- halInit();
- chSysInit();
- chThdCreateStatic(blinkWA, sizeof(blinkWA), NORMALPRIO, blink_thd, NULL);
- #if WAKEUP_TEST
-
- wakeupspec.wutr = ((uint32_t)4) << 16;
- wakeupspec.wutr |= 9;
- rtcSTM32SetPeriodicWakeup(&RTCD1, &wakeupspec);
- osalThreadSleepSeconds(3);
- anabiosis();
- #else
-
- rtcSTM32SetPeriodicWakeup(&RTCD1, NULL);
-
- sdStart(&SD6, &ser_cfg);
- shellInit();
- chThdCreateStatic(waShell, sizeof(waShell), NORMALPRIO,
- shellThread, (void *)&shell_cfg1);
-
- while (true){
- osalThreadSleepMilliseconds(200);
- }
- #endif
- return 0;
- }
|