summaryrefslogtreecommitdiff
path: root/firmware/target/hosted/ypr0/kernel-ypr0.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/target/hosted/ypr0/kernel-ypr0.c')
-rw-r--r--firmware/target/hosted/ypr0/kernel-ypr0.c163
1 files changed, 163 insertions, 0 deletions
diff --git a/firmware/target/hosted/ypr0/kernel-ypr0.c b/firmware/target/hosted/ypr0/kernel-ypr0.c
new file mode 100644
index 0000000000..bcf2cee583
--- /dev/null
+++ b/firmware/target/hosted/ypr0/kernel-ypr0.c
@@ -0,0 +1,163 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (c) 2010 Thomas Martitz
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22
23#include <time.h>
24#include <signal.h>
25#include <errno.h>
26#include <unistd.h>
27#include <pthread.h>
28#include "config.h"
29#include "system.h"
30#include "button.h"
31#include "audio.h"
32#include "panic.h"
33#include "timer.h"
34
35
36static pthread_cond_t wfi_cond = PTHREAD_COND_INITIALIZER;
37static pthread_mutex_t wfi_mtx = PTHREAD_MUTEX_INITIALIZER;
38/*
39 * call tick tasks and wake the scheduler up */
40void timer_signal(union sigval arg)
41{
42 (void)arg;
43 call_tick_tasks();
44 interrupt();
45}
46
47/*
48 * wait on the sem which the signal handler posts to save cpu time (aka sleep)
49 *
50 * other mechanisms could use them as well */
51void wait_for_interrupt(void)
52{
53 pthread_cond_wait(&wfi_cond, &wfi_mtx);
54}
55
56/*
57 * Wakeup the kernel, if sleeping (shall not be called from a signal handler) */
58void interrupt(void)
59{
60 pthread_cond_signal(&wfi_cond);
61}
62
63
64/*
65 * setup a hrtimer to send a signal to our process every tick
66 */
67union sigval tick_arg = {
68 .sival_int = 0,
69};
70
71void tick_start(unsigned int interval_in_ms)
72{
73 int ret = 0;
74 timer_t timerid;
75 struct itimerspec ts;
76 sigevent_t sigev;
77
78 /* initializing in the declaration causes some weird warnings */
79 memset(&sigev, 0, sizeof(sigevent_t));
80 sigev.sigev_notify = SIGEV_THREAD,
81 sigev.sigev_notify_function = timer_signal;
82
83 ts.it_value.tv_sec = ts.it_interval.tv_sec = 0;
84 ts.it_value.tv_nsec = ts.it_interval.tv_nsec = interval_in_ms*1000*1000;
85
86 /* add the timer */
87 ret |= timer_create(CLOCK_REALTIME, &sigev, &timerid);
88 ret |= timer_settime(timerid, 0, &ts, NULL);
89
90 /* Grab the mutex already now and leave it to this thread. We don't
91 * care about race conditions when signaling the condition (because
92 * they are not critical), but a mutex is necessary due to the API */
93 pthread_mutex_lock(&wfi_mtx);
94
95 if (ret != 0)
96 panicf("%s(): %s\n", __func__, strerror(errno));
97}
98
99#define cycles_to_microseconds(cycles) \
100 ((int)((1000000*cycles)/TIMER_FREQ))
101
102
103static timer_t timer_tid;
104static int timer_prio = -1;
105void (*global_unreg_callback)(void);
106void (*global_timer_callback)(void);
107
108static void timer_cb(union sigval arg)
109{
110 (void)arg;
111 if (global_timer_callback)
112 global_timer_callback();
113}
114
115bool timer_register(int reg_prio, void (*unregister_callback)(void),
116 long cycles, void (*timer_callback)(void))
117{
118 int ret = 0;
119 struct itimerspec ts;
120 sigevent_t sigev;
121 long in_us = cycles_to_microseconds(cycles);
122
123 if (reg_prio <= timer_prio || in_us <= 0)
124 return false;
125
126 if (timer_prio >= 0 && global_unreg_callback)
127 global_unreg_callback();
128
129 /* initializing in the declaration causes some weird warnings */
130 memset(&sigev, 0, sizeof(sigevent_t));
131 sigev.sigev_notify = SIGEV_THREAD,
132 sigev.sigev_notify_function = timer_cb;
133
134 ts.it_value.tv_sec = ts.it_interval.tv_sec = in_us / 1000000;
135 ts.it_value.tv_nsec = ts.it_interval.tv_nsec = (in_us%1000000)*1000;
136
137 /* add the timer */
138 ret |= timer_create(CLOCK_REALTIME, &sigev, &timer_tid);
139 ret |= timer_settime(timer_tid, 0, &ts, NULL);
140
141 global_timer_callback = timer_callback;
142 global_unreg_callback = unregister_callback;
143 timer_prio = reg_prio;
144
145 return ret == 0;
146}
147
148bool timer_set_period(long cycles)
149{
150 struct itimerspec ts;
151 long in_us = cycles_to_microseconds(cycles);
152 ts.it_value.tv_sec = ts.it_interval.tv_sec = in_us / 1000000;
153 ts.it_value.tv_nsec = ts.it_interval.tv_nsec = (in_us%1000000)*1000;
154
155 return timer_settime(timer_tid, 0, &ts, NULL) == 0;
156}
157
158void timer_unregister(void)
159{
160 timer_delete(timer_tid);
161 timer_prio = -1;
162}
163