2010-08-02 20:34:47 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (c) 2010 Thomas Martitz
|
|
|
|
*
|
|
|
|
* This program 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 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
2010-12-10 15:14:18 +00:00
|
|
|
#include <time.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
2011-03-11 23:38:36 +00:00
|
|
|
#include <pthread.h>
|
2010-08-02 20:34:47 +00:00
|
|
|
#include "config.h"
|
|
|
|
#include "system.h"
|
2010-08-07 21:30:22 +00:00
|
|
|
#include "button.h"
|
|
|
|
#include "audio.h"
|
2010-12-10 15:14:18 +00:00
|
|
|
#include "panic.h"
|
2010-08-02 20:34:47 +00:00
|
|
|
|
|
|
|
|
2011-03-11 23:38:36 +00:00
|
|
|
static pthread_cond_t wfi_cond = PTHREAD_COND_INITIALIZER;
|
|
|
|
static pthread_mutex_t wfi_mtx = PTHREAD_MUTEX_INITIALIZER;
|
2010-12-10 15:14:18 +00:00
|
|
|
/*
|
|
|
|
* call tick tasks and wake the scheduler up */
|
2011-03-11 23:38:36 +00:00
|
|
|
void timer_signal(union sigval arg)
|
2010-12-10 15:14:18 +00:00
|
|
|
{
|
2011-03-11 23:38:36 +00:00
|
|
|
(void)arg;
|
2010-12-10 15:14:18 +00:00
|
|
|
call_tick_tasks();
|
|
|
|
interrupt();
|
|
|
|
}
|
2010-08-07 21:30:22 +00:00
|
|
|
|
2010-08-02 20:34:47 +00:00
|
|
|
/*
|
2010-12-10 15:14:18 +00:00
|
|
|
* wait on the sem which the signal handler posts to save cpu time (aka sleep)
|
2010-08-02 20:34:47 +00:00
|
|
|
*
|
2010-12-10 15:14:18 +00:00
|
|
|
* other mechanisms could use them as well */
|
|
|
|
void wait_for_interrupt(void)
|
2010-08-02 20:34:47 +00:00
|
|
|
{
|
2011-03-11 23:38:36 +00:00
|
|
|
pthread_cond_wait(&wfi_cond, &wfi_mtx);
|
2010-08-02 20:34:47 +00:00
|
|
|
}
|
|
|
|
|
2011-03-11 23:38:36 +00:00
|
|
|
/*
|
|
|
|
* Wakeup the kernel, if sleeping (shall not be called from a signal handler) */
|
2010-12-10 15:14:18 +00:00
|
|
|
void interrupt(void)
|
2010-08-02 20:34:47 +00:00
|
|
|
{
|
2011-03-11 23:38:36 +00:00
|
|
|
pthread_cond_signal(&wfi_cond);
|
2010-08-02 20:34:47 +00:00
|
|
|
}
|
|
|
|
|
2010-12-10 15:14:18 +00:00
|
|
|
/*
|
|
|
|
* setup a hrtimer to send a signal to our process every tick */
|
|
|
|
void tick_start(unsigned int interval_in_ms)
|
2010-08-02 20:34:47 +00:00
|
|
|
{
|
2010-12-10 15:14:18 +00:00
|
|
|
int ret = 0;
|
|
|
|
timer_t timerid;
|
|
|
|
struct itimerspec ts;
|
2011-03-11 23:38:36 +00:00
|
|
|
sigevent_t sigev;
|
|
|
|
|
|
|
|
/* initializing in the declaration causes some weird warnings */
|
|
|
|
memset(&sigev, 0, sizeof(sigevent_t));
|
|
|
|
sigev.sigev_notify = SIGEV_THREAD,
|
|
|
|
sigev.sigev_notify_function = timer_signal,
|
2010-12-10 15:14:18 +00:00
|
|
|
|
|
|
|
ts.it_value.tv_sec = ts.it_interval.tv_sec = 0;
|
|
|
|
ts.it_value.tv_nsec = ts.it_interval.tv_nsec = interval_in_ms*1000*1000;
|
|
|
|
|
|
|
|
/* add the timer */
|
|
|
|
ret |= timer_create(CLOCK_REALTIME, &sigev, &timerid);
|
|
|
|
ret |= timer_settime(timerid, 0, &ts, NULL);
|
|
|
|
|
2011-03-11 23:38:36 +00:00
|
|
|
/* Grab the mutex already now and leave it to this thread. We don't
|
|
|
|
* care about race conditions when signaling the condition (because
|
|
|
|
* they are not critical), but a mutex is necessary due to the API */
|
|
|
|
pthread_mutex_lock(&wfi_mtx);
|
2010-12-10 15:14:18 +00:00
|
|
|
|
|
|
|
if (ret != 0)
|
|
|
|
panicf("%s(): %s\n", __func__, strerror(errno));
|
2010-08-02 20:34:47 +00:00
|
|
|
}
|
2010-12-10 15:14:18 +00:00
|
|
|
|
|
|
|
|
2010-08-02 20:34:47 +00:00
|
|
|
bool timer_register(int reg_prio, void (*unregister_callback)(void),
|
|
|
|
long cycles, void (*timer_callback)(void))
|
|
|
|
{
|
|
|
|
(void)reg_prio;
|
|
|
|
(void)unregister_callback;
|
|
|
|
(void)cycles;
|
|
|
|
(void)timer_callback;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool timer_set_period(long cycles)
|
|
|
|
{
|
|
|
|
(void)cycles;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void timer_unregister(void)
|
|
|
|
{
|
|
|
|
}
|