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-04-21 22:06:12 +00:00
|
|
|
|
#include "kernel.h"
|
2002-04-25 00:15:04 +00:00
|
|
|
|
#include "thread.h"
|
2002-05-05 18:34:06 +00:00
|
|
|
|
#include "sh7034.h"
|
|
|
|
|
#include "system.h"
|
|
|
|
|
#include "panic.h"
|
2002-04-21 22:06:12 +00:00
|
|
|
|
|
|
|
|
|
long current_tick = 0;
|
|
|
|
|
|
2002-06-04 12:47:39 +00:00
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Standard kernel stuff
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
void kernel_init(void)
|
|
|
|
|
{
|
2002-06-04 12:25:53 +00:00
|
|
|
|
int i;
|
|
|
|
|
|
2002-06-07 14:56:10 +00:00
|
|
|
|
/* Init the threading API */
|
|
|
|
|
init_threads();
|
|
|
|
|
|
2002-06-04 12:25:53 +00:00
|
|
|
|
/* Clear the tick task array */
|
2002-06-04 12:47:39 +00:00
|
|
|
|
for(i = 0;i < MAX_NUM_TICK_TASKS;i++)
|
2002-06-04 12:25:53 +00:00
|
|
|
|
{
|
|
|
|
|
tick_funcs[i] = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
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 )) {
|
2002-04-21 22:06:12 +00:00
|
|
|
|
yield();
|
2002-05-08 08:41:19 +00:00
|
|
|
|
}
|
2002-04-21 22:06:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void yield(void)
|
|
|
|
|
{
|
2002-04-25 00:15:04 +00:00
|
|
|
|
switch_thread();
|
2002-04-21 22:06:12 +00:00
|
|
|
|
}
|
2002-04-29 14:25:44 +00:00
|
|
|
|
|
2002-05-05 18:34:06 +00:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Interrupt level setting
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
int set_irq_level(int level)
|
|
|
|
|
{
|
2002-05-11 21:44:04 +00:00
|
|
|
|
int i;
|
|
|
|
|
/* Read the old level and set the new one */
|
|
|
|
|
asm volatile ("stc sr, %0" : "=r" (i));
|
|
|
|
|
asm volatile ("ldc %0, sr" : : "r" (level << 4));
|
|
|
|
|
return (i >> 4) & 0x0f;
|
2002-05-05 18:34:06 +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-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)
|
|
|
|
|
{
|
|
|
|
|
switch_thread();
|
|
|
|
|
}
|
|
|
|
|
|
2002-05-16 20:57:32 +00:00
|
|
|
|
*ev = q->events[(q->read++) & QUEUE_LENGTH_MASK];
|
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;
|
|
|
|
|
|
|
|
|
|
oldlevel = set_irq_level(15);
|
|
|
|
|
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
|
|
|
|
|
2002-05-23 09:22:07 +00:00
|
|
|
|
bool queue_empty(struct event_queue* q)
|
|
|
|
|
{
|
|
|
|
|
return ( q->read == q->write );
|
|
|
|
|
}
|
|
|
|
|
|
2002-05-05 18:34:06 +00:00
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Timer tick
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
static void tick_start(unsigned int interval_in_ms)
|
|
|
|
|
{
|
|
|
|
|
unsigned int count;
|
|
|
|
|
|
|
|
|
|
count = FREQ / 1000 / 8 * interval_in_ms;
|
|
|
|
|
|
|
|
|
|
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++;
|
|
|
|
|
|
|
|
|
|
TSR0 &= ~0x01;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int tick_add_task(void (*f)(void))
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
int oldlevel = set_irq_level(15);
|
|
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int tick_remove_task(void (*f)(void))
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
int oldlevel = set_irq_level(15);
|
|
|
|
|
|
|
|
|
|
/* 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)
|
2002-05-16 20:57:32 +00:00
|
|
|
|
yield();
|
|
|
|
|
|
|
|
|
|
/* ...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
|
|
|
|
}
|