12
#include <kernel/lthread/lthread.h>
14
#include <drivers/rtc.h>
16
#include <embox/unit.h>
18
EMBOX_UNIT_INIT(rtc_unit_init);
20
static struct lthread rtc_lthread;
22
extern struct rtc_device *global_rtc_device;
24
struct rtc_device *rtc_get_device(char *name) {
25
return global_rtc_device;
28
int rtc_get_time(struct rtc_device *rtc, struct tm *tm) {
31
if (!rtc || !rtc->rtc_ops || !rtc->rtc_ops->get_time) {
35
res = rtc->rtc_ops->get_time(rtc, tm);
40
int rtc_set_time(struct rtc_device *rtc, struct tm *tm) {
43
if (!rtc || !rtc->rtc_ops || !rtc->rtc_ops->set_time) {
47
res = rtc->rtc_ops->set_time(rtc, tm);
52
int rtc_get_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm) {
55
if (!rtc || !rtc->rtc_ops || !rtc->rtc_ops->get_alarm) {
59
res = rtc->rtc_ops->get_alarm(rtc, &(alarm->tm));
64
int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm) {
67
if (!rtc || !rtc->rtc_ops || !rtc->rtc_ops->set_alarm) {
71
res = rtc_alarm_irq_enable(rtc, 0);
75
res = rtc->rtc_ops->set_alarm(rtc, &(alarm->tm));
79
res = rtc_alarm_irq_enable(rtc, 1);
85
int rtc_irq_register(struct rtc_device *rtc, struct rtc_task *t) {
96
res = rtc_alarm_irq_enable(rtc, 0);
101
res = rtc_alarm_irq_enable(rtc, 1);
106
void rtc_irq_unregister(struct rtc_device *rtc, struct rtc_task *t) {
109
if (!rtc || (rtc->irq_task != t)) {
117
res = rtc_alarm_irq_enable(rtc, 0);
121
rtc->irq_task = NULL;
127
int rtc_alarm_irq_enable(struct rtc_device *rtc, int enabled) {
130
if (!rtc || !rtc->rtc_ops || !rtc->rtc_ops->alarm_irq_enable) {
134
res = rtc->rtc_ops->alarm_irq_enable(rtc,enabled);
139
void rtc_update_irq(struct rtc_device *rtc, int num, uint32_t events) {
140
rtc_alarm_irq_enable(rtc, 0);
141
if (rtc->irq_task && rtc->irq_task->func) {
142
lthread_launch(&rtc_lthread);
146
static int rtc_lthread_handler(struct lthread *self) {
148
if (!global_rtc_device) {
152
if (global_rtc_device->irq_task && global_rtc_device->irq_task->func) {
153
global_rtc_device->irq_task->func(global_rtc_device->irq_task->private_data);
160
static int rtc_unit_init(void) {
161
lthread_init(&rtc_lthread, rtc_lthread_handler);