2011-12-14 20:00:23 +00:00
|
|
|
/*
|
|
|
|
ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
|
|
|
|
2011 Giovanni Di Sirio.
|
|
|
|
|
|
|
|
This file is part of ChibiOS/RT.
|
|
|
|
|
|
|
|
ChibiOS/RT is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
ChibiOS/RT is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include <time.h>
|
2011-12-15 20:49:24 +00:00
|
|
|
#include <stdlib.h>
|
2011-12-14 20:00:23 +00:00
|
|
|
|
|
|
|
#include "ch.h"
|
|
|
|
#include "hal.h"
|
|
|
|
|
2011-12-17 19:08:16 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Notes. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
/*
|
|
|
|
This structure is used to hold the values representing a calendar time.
|
|
|
|
It contains the following members, with the meanings as shown.
|
|
|
|
|
|
|
|
int tm_sec seconds after minute [0-61] (61 allows for 2 leap-seconds)
|
|
|
|
int tm_min minutes after hour [0-59]
|
|
|
|
int tm_hour hours after midnight [0-23]
|
|
|
|
int tm_mday day of the month [1-31]
|
|
|
|
int tm_mon month of year [0-11]
|
|
|
|
int tm_year current year-1900
|
|
|
|
int tm_wday days since Sunday [0-6]
|
|
|
|
int tm_yday days since January 1st [0-365]
|
|
|
|
int tm_isdst daylight savings indicator (1 = yes, 0 = no, -1 = unknown)
|
|
|
|
*/
|
|
|
|
|
2011-12-14 20:00:23 +00:00
|
|
|
RTCTime timespec;
|
|
|
|
RTCAlarm alarmspec;
|
2011-12-15 20:49:24 +00:00
|
|
|
RTCWakeup wakeupspec;
|
2011-12-14 20:00:23 +00:00
|
|
|
RTCCallbackConfig cb_cfg;
|
|
|
|
time_t unix_time;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Alarms callback
|
|
|
|
*/
|
|
|
|
static inline void exti_rtcalarm_cb(EXTDriver *extp, expchannel_t channel){
|
|
|
|
(void)extp;
|
|
|
|
(void)channel;
|
|
|
|
if (RTCD1.id_rtc->ISR | RTC_ISR_ALRBF){
|
|
|
|
RTCD1.id_rtc->ISR &= ~RTC_ISR_ALRBF;
|
|
|
|
}
|
|
|
|
if (RTCD1.id_rtc->ISR | RTC_ISR_ALRAF){
|
|
|
|
RTCD1.id_rtc->ISR &= ~RTC_ISR_ALRAF;
|
|
|
|
}
|
2011-12-15 20:49:24 +00:00
|
|
|
palTogglePad(GPIOB, GPIOB_LED_R);
|
2011-12-14 20:00:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-12-15 20:49:24 +00:00
|
|
|
* Periodic wakeup callback
|
2011-12-14 20:00:23 +00:00
|
|
|
*/
|
|
|
|
static inline void exti_rtcwakeup_cb(EXTDriver *extp, expchannel_t channel){
|
|
|
|
(void)extp;
|
|
|
|
(void)channel;
|
2011-12-15 20:49:24 +00:00
|
|
|
/* manually clear flags because exti driver does not do that */
|
2011-12-14 20:00:23 +00:00
|
|
|
if (RTCD1.id_rtc->ISR | RTC_ISR_WUTF){
|
|
|
|
RTCD1.id_rtc->ISR &= ~RTC_ISR_WUTF;
|
|
|
|
}
|
2011-12-16 13:18:55 +00:00
|
|
|
palTogglePad(GPIOB, GPIOB_LED_B);
|
2011-12-15 20:49:24 +00:00
|
|
|
palTogglePad(GPIOB, GPIOB_LED_R);
|
2011-12-14 20:00:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const EXTConfig extcfg = {
|
|
|
|
{
|
|
|
|
{EXT_CH_MODE_DISABLED, NULL},
|
|
|
|
{EXT_CH_MODE_DISABLED, NULL},
|
|
|
|
{EXT_CH_MODE_DISABLED, NULL},
|
|
|
|
{EXT_CH_MODE_DISABLED, NULL},
|
|
|
|
{EXT_CH_MODE_DISABLED, NULL},
|
|
|
|
{EXT_CH_MODE_DISABLED, NULL},
|
|
|
|
{EXT_CH_MODE_DISABLED, NULL},
|
|
|
|
{EXT_CH_MODE_DISABLED, NULL},
|
|
|
|
{EXT_CH_MODE_DISABLED, NULL},
|
|
|
|
{EXT_CH_MODE_DISABLED, NULL},
|
|
|
|
{EXT_CH_MODE_DISABLED, NULL},
|
|
|
|
{EXT_CH_MODE_DISABLED, NULL},
|
|
|
|
{EXT_CH_MODE_DISABLED, NULL},
|
|
|
|
{EXT_CH_MODE_DISABLED, NULL},
|
|
|
|
{EXT_CH_MODE_DISABLED, NULL},
|
|
|
|
{EXT_CH_MODE_DISABLED, NULL},
|
|
|
|
{EXT_CH_MODE_DISABLED, NULL},
|
2011-12-17 19:08:16 +00:00
|
|
|
{EXT_CH_MODE_RISING_EDGE | EXT_CH_MODE_AUTOSTART, exti_rtcalarm_cb},/* RTC alarms */
|
2011-12-14 20:00:23 +00:00
|
|
|
{EXT_CH_MODE_DISABLED, NULL},
|
|
|
|
{EXT_CH_MODE_DISABLED, NULL},
|
|
|
|
{EXT_CH_MODE_DISABLED, NULL},
|
2011-12-17 19:08:16 +00:00
|
|
|
{EXT_CH_MODE_DISABLED, NULL},/* timestamp */
|
|
|
|
{EXT_CH_MODE_RISING_EDGE| EXT_CH_MODE_AUTOSTART, exti_rtcwakeup_cb},/* wakeup */
|
2011-12-14 20:00:23 +00:00
|
|
|
},
|
|
|
|
EXT_MODE_EXTI(
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
2011-12-17 19:08:16 +00:00
|
|
|
0)/* 15 */
|
2011-12-14 20:00:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert from STM32 BCD to classical format.
|
|
|
|
*/
|
|
|
|
void bcd2tm(struct tm *timp, uint32_t tv_time, uint32_t tv_date){
|
|
|
|
timp->tm_isdst = -1;
|
|
|
|
|
|
|
|
timp->tm_wday = ((tv_date >> 13) & 0x7);
|
|
|
|
if(timp->tm_wday == 7)
|
|
|
|
timp->tm_wday = 0;
|
|
|
|
timp->tm_mday = (tv_date & 0xF) + ((tv_date >> 4) & 0x3) * 10;
|
|
|
|
timp->tm_mon = (((tv_date >> 8) & 0xF) + ((tv_date >> 12) & 0x1) * 10) - 1;
|
|
|
|
timp->tm_year = (((tv_date >> 16)& 0xF) + ((tv_date >> 20) & 0xF) * 10) + 2000 - 1900;
|
|
|
|
|
|
|
|
timp->tm_sec = (tv_time & 0xF) + ((tv_time >> 4) & 0x7) * 10;
|
|
|
|
timp->tm_min = ((tv_time >> 8)& 0xF) + ((tv_time >> 12) & 0x7) * 10;
|
|
|
|
timp->tm_hour = ((tv_time >> 16)& 0xF) + ((tv_time >> 20) & 0x3) * 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert from classical format to STM32 BCD
|
|
|
|
*/
|
|
|
|
void tm2bcd(struct tm *timp, RTCTime *timespec){
|
2011-12-17 19:08:16 +00:00
|
|
|
uint32_t v = 0;
|
2011-12-14 20:00:23 +00:00
|
|
|
|
|
|
|
timespec->tv_date = 0;
|
|
|
|
timespec->tv_time = 0;
|
|
|
|
|
2011-12-15 15:13:37 +00:00
|
|
|
v = timp->tm_year - 100;
|
|
|
|
timespec->tv_date |= (((v / 10) & 0xF) << 20) | ((v % 10) << 16);
|
2011-12-14 20:00:23 +00:00
|
|
|
if (timp->tm_wday == 0)
|
2011-12-15 15:13:37 +00:00
|
|
|
v = 7;
|
2011-12-14 20:00:23 +00:00
|
|
|
else
|
2011-12-15 15:13:37 +00:00
|
|
|
v = timp->tm_wday;
|
|
|
|
timespec->tv_date |= (v & 7) << 13;
|
|
|
|
v = timp->tm_mon + 1;
|
|
|
|
timespec->tv_date |= (((v / 10) & 1) << 12) | ((v % 10) << 8);
|
|
|
|
v = timp->tm_mday;
|
|
|
|
timespec->tv_date |= (((v / 10) & 3) << 4) | (v % 10);
|
|
|
|
v = timp->tm_hour;
|
|
|
|
timespec->tv_time |= (((v / 10) & 3) << 20) | ((v % 10) << 16);
|
|
|
|
v = timp->tm_min;
|
|
|
|
timespec->tv_time |= (((v / 10) & 7) << 12) | ((v % 10) << 8);
|
|
|
|
v = timp->tm_sec;
|
|
|
|
timespec->tv_time |= (((v / 10) & 7) << 4) | (v % 10);
|
2011-12-14 20:00:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Main function.
|
|
|
|
*/
|
|
|
|
int main(void){
|
|
|
|
struct tm timp;
|
|
|
|
|
|
|
|
halInit();
|
|
|
|
chSysInit();
|
|
|
|
|
|
|
|
extStart(&EXTD1, &extcfg);
|
|
|
|
|
2011-12-15 20:49:24 +00:00
|
|
|
/* tune wakeup callback */
|
2011-12-17 19:08:16 +00:00
|
|
|
wakeupspec.wakeup = ((uint32_t)4) << 16; /* select 1 Hz clock source */
|
|
|
|
wakeupspec.wakeup |= 3; /* set counter value to 3. Period will be 3+1 seconds. */
|
2011-12-15 20:49:24 +00:00
|
|
|
rtcSetWakeup(&RTCD1, &wakeupspec);
|
|
|
|
|
|
|
|
/* enable wakeup callback */
|
|
|
|
cb_cfg.cb_cfg = WAKEUP_CB_FLAG;
|
|
|
|
rtcSetCallback(&RTCD1, &cb_cfg);
|
|
|
|
|
2011-12-14 20:00:23 +00:00
|
|
|
/* get current time in unix format */
|
|
|
|
rtcGetTime(&RTCD1, ×pec);
|
|
|
|
bcd2tm(&timp, timespec.tv_time, timespec.tv_date);
|
|
|
|
unix_time = mktime(&timp);
|
|
|
|
|
2011-12-17 19:08:16 +00:00
|
|
|
if (unix_time == -1){/* incorrect time in RTC cell */
|
2011-12-15 15:13:37 +00:00
|
|
|
unix_time = 1000000000;
|
|
|
|
}
|
2011-12-15 20:49:24 +00:00
|
|
|
/* set correct time */
|
2011-12-15 15:13:37 +00:00
|
|
|
tm2bcd((localtime(&unix_time)), ×pec);
|
|
|
|
rtcSetTime(&RTCD1, ×pec);
|
2011-12-14 20:00:23 +00:00
|
|
|
|
|
|
|
while (TRUE){
|
2011-12-15 15:13:37 +00:00
|
|
|
rtcGetTime(&RTCD1, ×pec);
|
|
|
|
bcd2tm(&timp, timespec.tv_time, timespec.tv_date);
|
|
|
|
unix_time = mktime(&timp);
|
|
|
|
chThdSleepMilliseconds(1500);
|
2011-12-14 20:00:23 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-15 15:13:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|