aboutsummaryrefslogtreecommitdiffstats
path: root/testhal/STM32/STM32F1xx/RTC/main.c
blob: fada5aee407310326fb96c629ea8586a96568f58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
    ChibiOS - Copyright (C) 2006..2016 Giovanni Di Sirio

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

#include "ch.h"
#include "hal.h"

RTCDateTime timespec;
RTCAlarm alarmspec;

#define TEST_ALARM_WAKEUP     FALSE

#if TEST_ALARM_WAKEUP

/*
 * Running indicator thread.
 */
static THD_WORKING_AREA(blinkWA, 128);
static THD_FUNCTION(blink_thd, arg) {
  (void)arg;
  while (true) {
    chThdSleepMilliseconds(100);
    palTogglePad(GPIOC, GPIOC_LED);
  }
  return 0;
}

/*
 *
 */
int main(void) {

  uint32_t tv_sec;

  halInit();
  chSysInit();

  chThdCreateStatic(blinkWA, sizeof(blinkWA), NORMALPRIO, blink_thd, NULL);

  /* compile ability test */
  rtcGetTime(&RTCD1, &timespec);

  /* set alarm in near future */
  rtcSTM32GetSecMsec(&RTCD1, &tv_sec, NULL);
  alarmspec.tv_sec = tv_sec + 20;
  rtcSetAlarm(&RTCD1, 0, &alarmspec);

  while (true){
    chThdSleepSeconds(10);

    /* going to anabiosis*/
    chSysLock();
    PWR->CR |= PWR_CR_CWUF | PWR_CR_CSBF;
    PWR->CR |= PWR_CR_PDDS | PWR_CR_LPDS;
    SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
    __WFI();
  }
  return 0;
}

#else /* TEST_ALARM_WAKEUP */

/*
 * Test alarm period.
 */
#define RTC_ALARMPERIOD   10

binary_semaphore_t alarm_sem;

/*
 * Alarm callback.
 */
static void my_cb(RTCDriver *rtcp, rtcevent_t event) {

  (void)rtcp;

  switch (event) {
  case RTC_EVENT_OVERFLOW:
    palTogglePad(GPIOC, GPIOC_LED);
    break;
  case RTC_EVENT_SECOND:
    //palTogglePad(GPIOC, GPIOC_LED);
    break;
  case RTC_EVENT_ALARM:
    palTogglePad(GPIOC, GPIOC_LED);
    osalSysLockFromISR();
    chBSemSignalI(&alarm_sem);
    osalSysUnlockFromISR();
    break;
  }
}

static time_measurement_t sett, gett;

int main(void) {

  msg_t status = MSG_TIMEOUT;
  uint32_t tv_sec;

  halInit();
  chSysInit();
  chBSemObjectInit(&alarm_sem, TRUE);
  chTMObjectInit(&sett);
  chTMObjectInit(&gett);

  /* compile ability test */
  chTMStartMeasurementX(&gett);
  rtcGetTime(&RTCD1, &timespec);
  chTMStopMeasurementX(&gett);

  rtcSTM32SetSec(&RTCD1, 1414845464);
  osalThreadSleepMilliseconds(10);
  rtcGetTime(&RTCD1, &timespec);
  timespec.month -= 1;

  chTMStartMeasurementX(&sett);
  rtcSetTime(&RTCD1, &timespec);
  chTMStopMeasurementX(&sett);
  osalThreadSleepMilliseconds(10);

  rtcGetTime(&RTCD1, &timespec);

  rtcSTM32GetSecMsec(&RTCD1, &tv_sec, NULL);
  alarmspec.tv_sec = tv_sec + RTC_ALARMPERIOD;
  rtcSetAlarm(&RTCD1, 0, &alarmspec);

  rtcSetCallback(&RTCD1, my_cb);

  while (true){

    /* Wait until alarm callback signaled semaphore.*/
    status = chBSemWaitTimeout(&alarm_sem, TIME_S2I(RTC_ALARMPERIOD + 5));

    if (status == MSG_TIMEOUT){
      osalSysHalt("time is out");
    }
    else{
      rtcSTM32GetSecMsec(&RTCD1, &tv_sec, NULL);
      alarmspec.tv_sec = tv_sec + RTC_ALARMPERIOD;
      rtcSetAlarm(&RTCD1, 0, &alarmspec);
    }
  }
  return 0;
}

#endif /* TEST_ALARM_WAKEUP */