2014-11-22 10:26:01 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013, 2014 Ludwig Ortmann <ludwig.ortmann@fu-berlin.de>
|
2014-10-31 15:57:17 +01:00
|
|
|
*
|
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser
|
|
|
|
* General Public License v2.1. See the file LICENSE in the top level
|
|
|
|
* directory for more details.
|
2014-11-22 10:26:01 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Native CPU periph/rtc.h implementation
|
|
|
|
*
|
|
|
|
* The implementation uses POSIX system calls to emulate a real-time
|
|
|
|
* clock based on the system clock.
|
2014-10-31 15:57:17 +01:00
|
|
|
*
|
2014-11-22 11:06:23 +01:00
|
|
|
* @author Ludwig Ortmann <ludwig.ortmann@fu-berlin.de>
|
2014-10-31 15:57:17 +01:00
|
|
|
*
|
2014-12-16 15:46:56 +01:00
|
|
|
* @ingroup _native_cpu
|
|
|
|
* @defgroup _native_rtc
|
2014-10-31 15:57:17 +01:00
|
|
|
* @file
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <time.h>
|
|
|
|
#include <stdlib.h>
|
2014-11-22 11:06:23 +01:00
|
|
|
#include <string.h>
|
2014-10-31 15:57:17 +01:00
|
|
|
#include <err.h>
|
|
|
|
|
|
|
|
#include "periph/rtc.h"
|
|
|
|
#include "cpu.h"
|
|
|
|
|
|
|
|
#include "native_internal.h"
|
|
|
|
|
2014-12-16 15:41:51 +01:00
|
|
|
#define ENABLE_DEBUG (0)
|
|
|
|
#include "debug.h"
|
|
|
|
|
2014-12-16 15:46:56 +01:00
|
|
|
static int _native_rtc_initialized = 0;
|
|
|
|
static int _native_rtc_powered = 0;
|
|
|
|
static struct tm _native_rtc_alarm;
|
|
|
|
static rtc_alarm_cb_t _native_rtc_alarm_callback;
|
|
|
|
static void *_native_rtc_alarm_argument;
|
2014-10-31 15:57:17 +01:00
|
|
|
|
|
|
|
void rtc_init(void)
|
|
|
|
{
|
2014-11-22 11:06:23 +01:00
|
|
|
DEBUG("rtc_init\n");
|
|
|
|
|
2014-12-16 15:46:56 +01:00
|
|
|
memset(&_native_rtc_alarm, 0, sizeof(_native_rtc_alarm));
|
|
|
|
_native_rtc_alarm_callback = NULL;
|
|
|
|
_native_rtc_alarm_argument = NULL;
|
2014-11-22 11:06:23 +01:00
|
|
|
|
2014-12-16 15:46:56 +01:00
|
|
|
_native_rtc_initialized = 1;
|
2014-10-31 15:57:17 +01:00
|
|
|
printf("Native RTC initialized.\n");
|
2014-11-22 11:06:23 +01:00
|
|
|
|
2014-10-31 15:57:17 +01:00
|
|
|
rtc_poweron();
|
|
|
|
}
|
|
|
|
|
|
|
|
void rtc_poweron(void)
|
|
|
|
{
|
|
|
|
DEBUG("rtc_poweron\n");
|
2014-11-22 11:06:23 +01:00
|
|
|
|
2014-12-16 15:46:56 +01:00
|
|
|
if (!_native_rtc_initialized) {
|
2014-11-22 11:06:23 +01:00
|
|
|
warnx("rtc_poweron: not initialized");
|
|
|
|
return;
|
2014-10-31 15:57:17 +01:00
|
|
|
}
|
2014-11-22 11:06:23 +01:00
|
|
|
|
2014-12-16 15:46:56 +01:00
|
|
|
_native_rtc_powered = 1;
|
2014-10-31 15:57:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void rtc_poweroff(void)
|
|
|
|
{
|
|
|
|
DEBUG("rtc_poweroff()\n");
|
2014-11-22 11:06:23 +01:00
|
|
|
|
2014-12-16 15:46:56 +01:00
|
|
|
if (!_native_rtc_initialized) {
|
2014-11-22 11:06:23 +01:00
|
|
|
warnx("rtc_poweroff: not initialized");
|
|
|
|
}
|
2014-12-16 15:46:56 +01:00
|
|
|
if (!_native_rtc_powered) {
|
2014-11-22 11:06:23 +01:00
|
|
|
warnx("rtc_poweroff: not powered on");
|
|
|
|
}
|
|
|
|
|
2014-12-16 15:46:56 +01:00
|
|
|
_native_rtc_powered = 0;
|
2014-10-31 15:57:17 +01:00
|
|
|
}
|
|
|
|
|
2014-11-22 11:06:23 +01:00
|
|
|
/* TODO: implement time setting using a delta */
|
2014-10-31 15:57:17 +01:00
|
|
|
int rtc_set_time(struct tm *ttime)
|
|
|
|
{
|
2014-11-22 11:06:23 +01:00
|
|
|
(void) ttime;
|
|
|
|
|
2014-10-31 15:57:17 +01:00
|
|
|
DEBUG("rtc_set_time()\n");
|
|
|
|
|
2014-12-16 15:46:56 +01:00
|
|
|
if (!_native_rtc_initialized) {
|
2014-11-22 11:06:23 +01:00
|
|
|
warnx("rtc_set_time: not initialized");
|
|
|
|
return -1;
|
|
|
|
}
|
2014-12-16 15:46:56 +01:00
|
|
|
if (!_native_rtc_powered) {
|
2014-11-22 11:06:23 +01:00
|
|
|
warnx("rtc_set_time: not powered on");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-11-22 10:26:01 +01:00
|
|
|
warnx("rtc_set_time: not implemented");
|
2014-10-31 15:57:17 +01:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rtc_get_time(struct tm *ttime)
|
|
|
|
{
|
|
|
|
time_t t;
|
|
|
|
|
2014-12-16 15:46:56 +01:00
|
|
|
if (!_native_rtc_initialized) {
|
2014-11-22 11:06:23 +01:00
|
|
|
warnx("rtc_get_time: not initialized");
|
|
|
|
return -1;
|
|
|
|
}
|
2014-12-16 15:46:56 +01:00
|
|
|
if (!_native_rtc_powered) {
|
2014-11-22 11:06:23 +01:00
|
|
|
warnx("rtc_get_time: not powered on");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
_native_syscall_enter();
|
|
|
|
t = time(NULL);
|
2014-10-31 15:57:17 +01:00
|
|
|
|
2014-11-22 11:06:23 +01:00
|
|
|
if (localtime_r(&t, ttime) == NULL) {
|
|
|
|
err(EXIT_FAILURE, "rtc_get_time: localtime_r");
|
2014-10-31 15:57:17 +01:00
|
|
|
}
|
2014-11-22 11:06:23 +01:00
|
|
|
_native_syscall_leave();
|
2014-10-31 15:57:17 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-22 11:06:23 +01:00
|
|
|
/* TODO: implement alarm scheduling */
|
2014-10-31 15:57:17 +01:00
|
|
|
int rtc_set_alarm(struct tm *time, rtc_alarm_cb_t cb, void *arg)
|
|
|
|
{
|
|
|
|
(void) time;
|
|
|
|
(void) cb;
|
|
|
|
(void) arg;
|
|
|
|
|
2014-12-16 15:46:56 +01:00
|
|
|
if (!_native_rtc_initialized) {
|
2014-11-22 11:06:23 +01:00
|
|
|
warnx("rtc_set_alarm: not initialized");
|
|
|
|
return -1;
|
|
|
|
}
|
2014-12-16 15:46:56 +01:00
|
|
|
if (!_native_rtc_powered) {
|
2014-11-22 11:06:23 +01:00
|
|
|
warnx("rtc_set_alarm: not powered on");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-12-16 15:46:56 +01:00
|
|
|
memcpy(&_native_rtc_alarm, time, sizeof(_native_rtc_alarm));
|
2014-11-22 11:06:23 +01:00
|
|
|
|
2014-11-22 10:26:01 +01:00
|
|
|
warnx("rtc_set_alarm: not implemented");
|
2014-10-31 15:57:17 +01:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rtc_get_alarm(struct tm *time)
|
|
|
|
{
|
|
|
|
(void) time;
|
|
|
|
|
2014-12-16 15:46:56 +01:00
|
|
|
if (!_native_rtc_initialized) {
|
2014-11-22 11:06:23 +01:00
|
|
|
warnx("rtc_get_alarm: not initialized");
|
|
|
|
return -1;
|
|
|
|
}
|
2014-12-16 15:46:56 +01:00
|
|
|
if (!_native_rtc_powered) {
|
2014-11-22 11:06:23 +01:00
|
|
|
warnx("rtc_get_alarm: not powered on");
|
|
|
|
return -1;
|
|
|
|
}
|
2014-10-31 15:57:17 +01:00
|
|
|
|
2014-12-16 15:46:56 +01:00
|
|
|
memcpy(time, &_native_rtc_alarm, sizeof(_native_rtc_alarm));
|
2014-11-22 11:06:23 +01:00
|
|
|
|
|
|
|
return 0;
|
2014-10-31 15:57:17 +01:00
|
|
|
}
|
|
|
|
|
2014-11-22 11:06:23 +01:00
|
|
|
/* TODO: implement alarm unscheduling once rtc_set_alarm is
|
|
|
|
* implemented */
|
2014-10-31 15:57:17 +01:00
|
|
|
void rtc_clear_alarm(void)
|
|
|
|
{
|
2014-11-22 11:06:23 +01:00
|
|
|
DEBUG("rtc_clear_alarm()\n");
|
|
|
|
|
2014-12-16 15:46:56 +01:00
|
|
|
if (!_native_rtc_initialized) {
|
2014-11-22 11:06:23 +01:00
|
|
|
warnx("rtc_clear_alarm: not initialized");
|
|
|
|
}
|
2014-12-16 15:46:56 +01:00
|
|
|
if (!_native_rtc_powered) {
|
2014-11-22 11:06:23 +01:00
|
|
|
warnx("rtc_clear_alarm: not powered on");
|
|
|
|
}
|
|
|
|
|
2014-12-16 15:46:56 +01:00
|
|
|
memset(&_native_rtc_alarm, 0, sizeof(_native_rtc_alarm));
|
2014-10-31 15:57:17 +01:00
|
|
|
}
|