2002-04-21 22:06:12 +00:00
|
|
|
|
/***************************************************************************
|
|
|
|
|
* __________ __ ___.
|
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
|
* $Id$
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2002 by Bj<EFBFBD>rn Stenberg
|
|
|
|
|
*
|
|
|
|
|
* All files in this archive are subject to the GNU General Public License.
|
|
|
|
|
* See the file COPYING in the source tree root for full license agreement.
|
|
|
|
|
*
|
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
|
* KIND, either express or implied.
|
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
2002-05-05 18:34:06 +00:00
|
|
|
|
#include <stdlib.h>
|
2002-06-29 21:30:42 +00:00
|
|
|
|
#include <string.h>
|
2002-04-21 22:06:12 +00:00
|
|
|
|
#include "kernel.h"
|
2002-04-25 00:15:04 +00:00
|
|
|
|
#include "thread.h"
|
2004-10-27 07:07:54 +00:00
|
|
|
|
#include "cpu.h"
|
2002-05-05 18:34:06 +00:00
|
|
|
|
#include "system.h"
|
|
|
|
|
#include "panic.h"
|
2002-04-21 22:06:12 +00:00
|
|
|
|
|
|
|
|
|
long current_tick = 0;
|
|
|
|
|
|
2002-12-18 14:57:45 +00:00
|
|
|
|
static void (*tick_funcs[MAX_NUM_TICK_TASKS])(void);
|
2002-06-04 12:25:53 +00:00
|
|
|
|
|
2002-05-05 18:34:06 +00:00
|
|
|
|
static void tick_start(unsigned int interval_in_ms);
|
|
|
|
|
|
2002-06-29 21:19:55 +00:00
|
|
|
|
/* This array holds all queues that are initiated. It is used for broadcast. */
|
|
|
|
|
static struct event_queue *all_queues[32];
|
|
|
|
|
static int num_queues;
|
|
|
|
|
|
2002-08-01 08:14:01 +00:00
|
|
|
|
void sleep(int ticks) __attribute__ ((section(".icode")));
|
2004-06-16 11:30:28 +00:00
|
|
|
|
void queue_wait(struct event_queue *q, struct event *ev) __attribute__ ((section(".icode")));
|
2002-08-01 08:14:01 +00:00
|
|
|
|
|
2002-05-05 18:34:06 +00:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Standard kernel stuff
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
void kernel_init(void)
|
|
|
|
|
{
|
2002-06-07 14:56:10 +00:00
|
|
|
|
/* Init the threading API */
|
|
|
|
|
init_threads();
|
|
|
|
|
|
2002-06-29 21:19:55 +00:00
|
|
|
|
memset(tick_funcs, 0, sizeof(tick_funcs));
|
|
|
|
|
|
|
|
|
|
num_queues = 0;
|
|
|
|
|
memset(all_queues, 0, sizeof(all_queues));
|
2002-06-04 12:25:53 +00:00
|
|
|
|
|
2002-05-07 23:01:42 +00:00
|
|
|
|
tick_start(1000/HZ);
|
2002-05-05 18:34:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-04-21 22:06:12 +00:00
|
|
|
|
void sleep(int ticks)
|
|
|
|
|
{
|
2002-05-08 08:41:19 +00:00
|
|
|
|
/* Always sleep at least 1 tick */
|
|
|
|
|
int timeout = current_tick + ticks + 1;
|
2002-04-21 22:06:12 +00:00
|
|
|
|
|
2002-05-08 08:41:19 +00:00
|
|
|
|
while (TIME_BEFORE( current_tick, timeout )) {
|
2003-02-14 09:44:34 +00:00
|
|
|
|
sleep_thread();
|
2002-05-08 08:41:19 +00:00
|
|
|
|
}
|
2003-02-14 09:44:34 +00:00
|
|
|
|
wake_up_thread();
|
2002-04-21 22:06:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void yield(void)
|
|
|
|
|
{
|
2002-04-25 00:15:04 +00:00
|
|
|
|
switch_thread();
|
2003-02-14 09:44:34 +00:00
|
|
|
|
wake_up_thread();
|
2002-04-21 22:06:12 +00:00
|
|
|
|
}
|
2002-04-29 14:25:44 +00:00
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Queue handling stuff
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
void queue_init(struct event_queue *q)
|
|
|
|
|
{
|
|
|
|
|
q->read = 0;
|
|
|
|
|
q->write = 0;
|
2002-06-29 21:19:55 +00:00
|
|
|
|
|
|
|
|
|
/* Add it to the all_queues array */
|
|
|
|
|
all_queues[num_queues++] = q;
|
2002-04-29 14:25:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-05-16 20:57:32 +00:00
|
|
|
|
void queue_wait(struct event_queue *q, struct event *ev)
|
2002-04-29 14:25:44 +00:00
|
|
|
|
{
|
|
|
|
|
while(q->read == q->write)
|
|
|
|
|
{
|
2003-02-14 09:44:34 +00:00
|
|
|
|
sleep_thread();
|
2002-04-29 14:25:44 +00:00
|
|
|
|
}
|
2003-02-14 09:44:34 +00:00
|
|
|
|
wake_up_thread();
|
2002-04-29 14:25:44 +00:00
|
|
|
|
|
2002-05-16 20:57:32 +00:00
|
|
|
|
*ev = q->events[(q->read++) & QUEUE_LENGTH_MASK];
|
2002-04-29 14:25:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-10-11 08:56:23 +00:00
|
|
|
|
void queue_wait_w_tmo(struct event_queue *q, struct event *ev, int ticks)
|
|
|
|
|
{
|
|
|
|
|
unsigned int timeout = current_tick + ticks;
|
|
|
|
|
|
|
|
|
|
while(q->read == q->write && TIME_BEFORE( current_tick, timeout ))
|
|
|
|
|
{
|
2003-02-14 09:44:34 +00:00
|
|
|
|
sleep_thread();
|
2002-10-11 08:56:23 +00:00
|
|
|
|
}
|
2003-02-14 09:44:34 +00:00
|
|
|
|
wake_up_thread();
|
2002-10-11 08:56:23 +00:00
|
|
|
|
|
|
|
|
|
if(q->read != q->write)
|
|
|
|
|
{
|
|
|
|
|
*ev = q->events[(q->read++) & QUEUE_LENGTH_MASK];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ev->id = SYS_TIMEOUT;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2002-04-29 14:25:44 +00:00
|
|
|
|
void queue_post(struct event_queue *q, int id, void *data)
|
|
|
|
|
{
|
2002-05-08 22:07:41 +00:00
|
|
|
|
int wr;
|
|
|
|
|
int oldlevel;
|
|
|
|
|
|
2004-03-02 11:32:59 +00:00
|
|
|
|
oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
|
2002-05-08 22:07:41 +00:00
|
|
|
|
wr = (q->write++) & QUEUE_LENGTH_MASK;
|
2002-04-29 14:25:44 +00:00
|
|
|
|
|
|
|
|
|
q->events[wr].id = id;
|
|
|
|
|
q->events[wr].data = data;
|
2002-05-08 22:07:41 +00:00
|
|
|
|
set_irq_level(oldlevel);
|
2002-04-29 14:25:44 +00:00
|
|
|
|
}
|
2002-05-05 18:34:06 +00:00
|
|
|
|
|
2004-08-16 23:37:23 +00:00
|
|
|
|
bool queue_empty(const struct event_queue* q)
|
2002-05-23 09:22:07 +00:00
|
|
|
|
{
|
|
|
|
|
return ( q->read == q->write );
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-01 06:24:05 +00:00
|
|
|
|
void queue_clear(struct event_queue* q)
|
2004-09-01 06:20:21 +00:00
|
|
|
|
{
|
2004-09-01 06:24:05 +00:00
|
|
|
|
int oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
|
2004-09-01 06:20:21 +00:00
|
|
|
|
q->read = 0;
|
|
|
|
|
q->write = 0;
|
2004-09-01 06:24:05 +00:00
|
|
|
|
set_irq_level(oldlevel);
|
2004-09-01 06:20:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-06-29 21:19:55 +00:00
|
|
|
|
int queue_broadcast(int id, void *data)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
for(i = 0;i < num_queues;i++)
|
|
|
|
|
{
|
|
|
|
|
queue_post(all_queues[i], id, data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return num_queues;
|
|
|
|
|
}
|
2002-05-05 18:34:06 +00:00
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Timer tick
|
|
|
|
|
****************************************************************************/
|
2004-10-27 07:07:54 +00:00
|
|
|
|
#if CONFIG_CPU == SH7034
|
2002-05-05 18:34:06 +00:00
|
|
|
|
static void tick_start(unsigned int interval_in_ms)
|
|
|
|
|
{
|
|
|
|
|
unsigned int count;
|
|
|
|
|
|
2002-10-09 06:35:29 +00:00
|
|
|
|
count = FREQ * interval_in_ms / 1000 / 8;
|
2002-05-05 18:34:06 +00:00
|
|
|
|
|
|
|
|
|
if(count > 0xffff)
|
|
|
|
|
{
|
|
|
|
|
panicf("Error! The tick interval is too long (%d ms)\n",
|
|
|
|
|
interval_in_ms);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* We are using timer 0 */
|
|
|
|
|
|
|
|
|
|
TSTR &= ~0x01; /* Stop the timer */
|
|
|
|
|
TSNC &= ~0x01; /* No synchronization */
|
|
|
|
|
TMDR &= ~0x01; /* Operate normally */
|
|
|
|
|
|
|
|
|
|
TCNT0 = 0; /* Start counting at 0 */
|
|
|
|
|
GRA0 = count;
|
|
|
|
|
TCR0 = 0x23; /* Clear at GRA match, sysclock/8 */
|
|
|
|
|
|
|
|
|
|
/* Enable interrupt on level 1 */
|
|
|
|
|
IPRC = (IPRC & ~0x00f0) | 0x0010;
|
|
|
|
|
|
|
|
|
|
TSR0 &= ~0x01;
|
|
|
|
|
TIER0 = 0xf9; /* Enable GRA match interrupt */
|
|
|
|
|
|
|
|
|
|
TSTR |= 0x01; /* Start timer 1 */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#pragma interrupt
|
|
|
|
|
void IMIA0(void)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
/* Run through the list of tick tasks */
|
2002-06-04 12:47:39 +00:00
|
|
|
|
for(i = 0;i < MAX_NUM_TICK_TASKS;i++)
|
2002-05-05 18:34:06 +00:00
|
|
|
|
{
|
|
|
|
|
if(tick_funcs[i])
|
|
|
|
|
{
|
|
|
|
|
tick_funcs[i]();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
current_tick++;
|
2003-02-14 09:44:34 +00:00
|
|
|
|
wake_up_thread();
|
2002-05-05 18:34:06 +00:00
|
|
|
|
|
|
|
|
|
TSR0 &= ~0x01;
|
|
|
|
|
}
|
2004-10-27 07:07:54 +00:00
|
|
|
|
#elif CONFIG_CPU == MCF5249
|
|
|
|
|
static void tick_start(unsigned int interval_in_ms)
|
|
|
|
|
{
|
|
|
|
|
unsigned int count;
|
|
|
|
|
|
|
|
|
|
count = FREQ/2 * interval_in_ms / 1000 / 16;
|
|
|
|
|
|
|
|
|
|
if(count > 0xffff)
|
|
|
|
|
{
|
|
|
|
|
panicf("Error! The tick interval is too long (%d ms)\n",
|
|
|
|
|
interval_in_ms);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* We are using timer 0 */
|
|
|
|
|
|
|
|
|
|
TRR0 = count; /* The reference count */
|
|
|
|
|
TCN0 = 0; /* reset the timer */
|
|
|
|
|
TMR0 = 0x001d; /* no prescaler, restart, CLK/16, enabled */
|
|
|
|
|
|
|
|
|
|
TER0 = 0xff; /* Clear all events */
|
|
|
|
|
|
|
|
|
|
ICR0 = (ICR0 & 0xff00ffff) | 0x008c0000; /* Interrupt on level 3.0 */
|
|
|
|
|
IMR &= ~0x200;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TIMER0(void) __attribute__ ((interrupt_handler));
|
|
|
|
|
void TIMER0(void)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
/* Run through the list of tick tasks */
|
|
|
|
|
for(i = 0;i < MAX_NUM_TICK_TASKS;i++)
|
|
|
|
|
{
|
|
|
|
|
if(tick_funcs[i])
|
|
|
|
|
{
|
|
|
|
|
tick_funcs[i]();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
current_tick++;
|
|
|
|
|
wake_up_thread();
|
|
|
|
|
|
|
|
|
|
TER0 = 0xff; /* Clear all events */
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2002-05-05 18:34:06 +00:00
|
|
|
|
|
|
|
|
|
int tick_add_task(void (*f)(void))
|
|
|
|
|
{
|
|
|
|
|
int i;
|
2004-03-02 11:32:59 +00:00
|
|
|
|
int oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
|
2002-05-05 18:34:06 +00:00
|
|
|
|
|
|
|
|
|
/* Add a task if there is room */
|
2002-06-04 12:47:39 +00:00
|
|
|
|
for(i = 0;i < MAX_NUM_TICK_TASKS;i++)
|
2002-05-05 18:34:06 +00:00
|
|
|
|
{
|
|
|
|
|
if(tick_funcs[i] == NULL)
|
|
|
|
|
{
|
|
|
|
|
tick_funcs[i] = f;
|
|
|
|
|
set_irq_level(oldlevel);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
set_irq_level(oldlevel);
|
2002-06-29 21:30:42 +00:00
|
|
|
|
panicf("Error! tick_add_task(): out of tasks");
|
2002-05-05 18:34:06 +00:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int tick_remove_task(void (*f)(void))
|
|
|
|
|
{
|
|
|
|
|
int i;
|
2004-03-02 11:32:59 +00:00
|
|
|
|
int oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
|
2002-05-05 18:34:06 +00:00
|
|
|
|
|
|
|
|
|
/* Remove a task if it is there */
|
2002-06-04 12:47:39 +00:00
|
|
|
|
for(i = 0;i < MAX_NUM_TICK_TASKS;i++)
|
2002-05-05 18:34:06 +00:00
|
|
|
|
{
|
|
|
|
|
if(tick_funcs[i] == f)
|
|
|
|
|
{
|
|
|
|
|
tick_funcs[i] = NULL;
|
|
|
|
|
set_irq_level(oldlevel);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set_irq_level(oldlevel);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2002-05-16 20:57:32 +00:00
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Simple mutex functions
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
void mutex_init(struct mutex *m)
|
|
|
|
|
{
|
2002-06-04 12:47:39 +00:00
|
|
|
|
m->locked = false;
|
2002-05-16 20:57:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void mutex_lock(struct mutex *m)
|
|
|
|
|
{
|
|
|
|
|
/* Wait until the lock is open... */
|
2002-06-04 12:47:39 +00:00
|
|
|
|
while(m->locked)
|
2003-02-14 09:44:34 +00:00
|
|
|
|
sleep_thread();
|
|
|
|
|
wake_up_thread();
|
2002-05-16 20:57:32 +00:00
|
|
|
|
|
|
|
|
|
/* ...and lock it */
|
2002-06-04 12:47:39 +00:00
|
|
|
|
m->locked = true;
|
2002-05-16 20:57:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void mutex_unlock(struct mutex *m)
|
|
|
|
|
{
|
2002-06-04 12:47:39 +00:00
|
|
|
|
m->locked = false;
|
2002-05-16 20:57:32 +00:00
|
|
|
|
}
|