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>
|
2005-03-01 14:33:45 +00:00
|
|
|
|
#include "config.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
|
|
|
|
|
2006-02-05 17:34:49 +00:00
|
|
|
|
#if ((CONFIG_CPU != PP5020) && (CONFIG_CPU != PP5002)) || !defined(BOOTLOADER)
|
2002-04-21 22:06:12 +00:00
|
|
|
|
long current_tick = 0;
|
2006-01-05 17:02:48 +00:00
|
|
|
|
#endif
|
2002-04-21 22:06:12 +00:00
|
|
|
|
|
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-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;
|
|
|
|
|
|
2005-11-12 14:48:52 +00:00
|
|
|
|
void queue_wait(struct event_queue *q, struct event *ev) ICODE_ATTR;
|
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));
|
2006-01-05 17:02:48 +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)
|
|
|
|
|
{
|
2006-08-12 08:27:48 +00:00
|
|
|
|
#if CONFIG_CPU == S3C2440 && defined(BOOTLOADER)
|
|
|
|
|
int counter;
|
|
|
|
|
TCON &= ~(1 << 20); // stop timer 4
|
|
|
|
|
// TODO: this constant depends on dividers settings inherited from
|
|
|
|
|
// firmware. Set them explicitly somwhere.
|
|
|
|
|
TCNTB4 = 12193 * ticks / HZ;
|
|
|
|
|
TCON |= 1 << 21; // set manual bit
|
|
|
|
|
TCON &= ~(1 << 21); // reset manual bit
|
|
|
|
|
TCON &= ~(1 << 22); //autoreload Off
|
|
|
|
|
TCON |= (1 << 20); // start timer 4
|
|
|
|
|
do {
|
|
|
|
|
counter = TCNTO4;
|
|
|
|
|
} while(counter > 0);
|
|
|
|
|
|
|
|
|
|
#else
|
2006-09-16 16:18:11 +00:00
|
|
|
|
sleep_thread(ticks);
|
2006-08-12 08:27:48 +00:00
|
|
|
|
#endif
|
2002-04-21 22:06:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void yield(void)
|
|
|
|
|
{
|
2006-08-31 19:19:35 +00:00
|
|
|
|
#if (CONFIG_CPU == S3C2440 || defined(ELIO_TPJ1022) && defined(BOOTLOADER))
|
|
|
|
|
/* Some targets don't like yielding in the bootloader */
|
2006-08-12 08:27:48 +00:00
|
|
|
|
#else
|
2006-09-16 16:18:11 +00:00
|
|
|
|
switch_thread(true, NULL);
|
2006-08-12 08:27:48 +00:00
|
|
|
|
#endif
|
2002-04-21 22:06:12 +00:00
|
|
|
|
}
|
2002-04-29 14:25:44 +00:00
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Queue handling stuff
|
|
|
|
|
****************************************************************************/
|
2006-09-16 16:18:11 +00:00
|
|
|
|
void queue_init(struct event_queue *q, bool register_queue)
|
2002-04-29 14:25:44 +00:00
|
|
|
|
{
|
|
|
|
|
q->read = 0;
|
|
|
|
|
q->write = 0;
|
2006-09-16 16:18:11 +00:00
|
|
|
|
q->thread = NULL;
|
|
|
|
|
|
|
|
|
|
if (register_queue)
|
|
|
|
|
{
|
|
|
|
|
/* Add it to the all_queues array */
|
|
|
|
|
all_queues[num_queues++] = q;
|
|
|
|
|
}
|
2002-04-29 14:25:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-01-23 10:53:47 +00:00
|
|
|
|
void queue_delete(struct event_queue *q)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
2006-01-23 10:59:07 +00:00
|
|
|
|
bool found = false;
|
2006-01-23 10:53:47 +00:00
|
|
|
|
|
2006-09-16 16:18:11 +00:00
|
|
|
|
wakeup_thread(&q->thread);
|
|
|
|
|
|
2006-01-23 10:53:47 +00:00
|
|
|
|
/* Find the queue to be deleted */
|
|
|
|
|
for(i = 0;i < num_queues;i++)
|
|
|
|
|
{
|
|
|
|
|
if(all_queues[i] == q)
|
2006-01-23 10:59:07 +00:00
|
|
|
|
{
|
|
|
|
|
found = true;
|
2006-01-23 10:53:47 +00:00
|
|
|
|
break;
|
2006-01-23 10:59:07 +00:00
|
|
|
|
}
|
2006-01-23 10:53:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-01-23 10:59:07 +00:00
|
|
|
|
if(found)
|
2006-01-23 10:53:47 +00:00
|
|
|
|
{
|
2006-01-23 10:59:07 +00:00
|
|
|
|
/* Move the following queues up in the list */
|
|
|
|
|
for(;i < num_queues-1;i++)
|
|
|
|
|
{
|
|
|
|
|
all_queues[i] = all_queues[i+1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
num_queues--;
|
2006-01-23 10:53:47 +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
|
|
|
|
{
|
2006-09-16 16:18:11 +00:00
|
|
|
|
if (q->read == q->write)
|
2002-04-29 14:25:44 +00:00
|
|
|
|
{
|
2006-11-11 05:33:24 +00:00
|
|
|
|
block_thread(&q->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)
|
|
|
|
|
{
|
2006-09-16 16:18:11 +00:00
|
|
|
|
if (q->read == q->write && ticks > 0)
|
2002-10-11 08:56:23 +00:00
|
|
|
|
{
|
2006-11-11 05:33:24 +00:00
|
|
|
|
block_thread_w_tmo(&q->thread, ticks);
|
2002-10-11 08:56:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-09-16 16:18:11 +00:00
|
|
|
|
if (q->read != q->write)
|
2002-10-11 08:56:23 +00:00
|
|
|
|
{
|
|
|
|
|
*ev = q->events[(q->read++) & QUEUE_LENGTH_MASK];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ev->id = SYS_TIMEOUT;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2005-01-24 14:26:24 +00:00
|
|
|
|
void queue_post(struct event_queue *q, long id, void *data)
|
2002-04-29 14:25:44 +00:00
|
|
|
|
{
|
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;
|
2006-09-16 16:18:11 +00:00
|
|
|
|
|
|
|
|
|
wakeup_thread(&q->thread);
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2006-10-19 11:43:13 +00:00
|
|
|
|
void queue_remove_from_head(struct event_queue *q, long id)
|
|
|
|
|
{
|
|
|
|
|
int oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
|
|
|
|
|
|
|
|
|
|
while (q->read != q->write &&
|
|
|
|
|
q->events[(q->read) & QUEUE_LENGTH_MASK].id == id)
|
|
|
|
|
{
|
|
|
|
|
q->read++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set_irq_level(oldlevel);
|
|
|
|
|
}
|
|
|
|
|
|
2005-01-30 15:40:25 +00:00
|
|
|
|
int queue_broadcast(long id, void *data)
|
2002-06-29 21:19:55 +00:00
|
|
|
|
{
|
|
|
|
|
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
|
2005-03-01 14:33:45 +00:00
|
|
|
|
void tick_start(unsigned int interval_in_ms)
|
2002-05-05 18:34:06 +00:00
|
|
|
|
{
|
2005-10-03 09:24:36 +00:00
|
|
|
|
unsigned long count;
|
2002-05-05 18:34:06 +00:00
|
|
|
|
|
2005-10-03 09:24:36 +00:00
|
|
|
|
count = CPU_FREQ * interval_in_ms / 1000 / 8;
|
2002-05-05 18:34:06 +00:00
|
|
|
|
|
2005-10-03 09:24:36 +00:00
|
|
|
|
if(count > 0x10000)
|
2002-05-05 18:34:06 +00:00
|
|
|
|
{
|
|
|
|
|
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 */
|
2005-10-03 09:24:36 +00:00
|
|
|
|
GRA0 = (unsigned short)(count - 1);
|
2002-05-05 18:34:06 +00:00
|
|
|
|
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 */
|
|
|
|
|
}
|
|
|
|
|
|
2005-10-03 09:24:36 +00:00
|
|
|
|
void IMIA0(void) __attribute__ ((interrupt_handler));
|
2002-05-05 18:34:06 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
2005-07-18 12:40:29 +00:00
|
|
|
|
#elif defined(CPU_COLDFIRE)
|
2005-03-01 14:33:45 +00:00
|
|
|
|
void tick_start(unsigned int interval_in_ms)
|
2004-10-27 07:07:54 +00:00
|
|
|
|
{
|
2005-10-03 09:24:36 +00:00
|
|
|
|
unsigned long count;
|
|
|
|
|
int prescale;
|
2004-10-27 07:07:54 +00:00
|
|
|
|
|
2005-10-03 09:24:36 +00:00
|
|
|
|
count = CPU_FREQ/2 * interval_in_ms / 1000 / 16;
|
2004-10-27 07:07:54 +00:00
|
|
|
|
|
2005-10-03 09:24:36 +00:00
|
|
|
|
if(count > 0x10000)
|
2004-10-27 07:07:54 +00:00
|
|
|
|
{
|
|
|
|
|
panicf("Error! The tick interval is too long (%d ms)\n",
|
|
|
|
|
interval_in_ms);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2005-10-03 09:24:36 +00:00
|
|
|
|
|
|
|
|
|
prescale = cpu_frequency / CPU_FREQ;
|
|
|
|
|
/* Note: The prescaler is later adjusted on-the-fly on CPU frequency
|
|
|
|
|
changes within timer.c */
|
2004-10-27 07:07:54 +00:00
|
|
|
|
|
|
|
|
|
/* We are using timer 0 */
|
|
|
|
|
|
2005-10-03 09:24:36 +00:00
|
|
|
|
TRR0 = (unsigned short)(count - 1); /* The reference count */
|
2004-10-27 07:07:54 +00:00
|
|
|
|
TCN0 = 0; /* reset the timer */
|
2005-10-03 09:24:36 +00:00
|
|
|
|
TMR0 = 0x001d | ((unsigned short)(prescale - 1) << 8);
|
|
|
|
|
/* restart, CLK/16, enabled, prescaler */
|
2004-10-27 07:07:54 +00:00
|
|
|
|
|
|
|
|
|
TER0 = 0xff; /* Clear all events */
|
|
|
|
|
|
2005-11-05 03:28:20 +00:00
|
|
|
|
ICR1 = 0x8c; /* Interrupt on level 3.0 */
|
2004-10-27 07:07:54 +00:00
|
|
|
|
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++;
|
|
|
|
|
|
|
|
|
|
TER0 = 0xff; /* Clear all events */
|
|
|
|
|
}
|
2005-01-09 23:24:02 +00:00
|
|
|
|
|
2006-08-02 09:46:51 +00:00
|
|
|
|
#elif defined(CPU_PP)
|
2005-11-07 23:07:19 +00:00
|
|
|
|
|
2006-01-05 17:02:48 +00:00
|
|
|
|
#ifndef BOOTLOADER
|
2005-12-12 13:53:22 +00:00
|
|
|
|
void TIMER1(void)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
2006-01-24 22:31:57 +00:00
|
|
|
|
TIMER1_VAL; /* Read value to ack IRQ */
|
2005-12-12 13:53:22 +00:00
|
|
|
|
/* 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++;
|
|
|
|
|
}
|
2006-01-05 17:02:48 +00:00
|
|
|
|
#endif
|
2005-12-12 13:53:22 +00:00
|
|
|
|
|
|
|
|
|
void tick_start(unsigned int interval_in_ms)
|
|
|
|
|
{
|
2006-01-05 17:02:48 +00:00
|
|
|
|
#ifndef BOOTLOADER
|
2006-01-24 22:31:57 +00:00
|
|
|
|
TIMER1_CFG = 0x0;
|
|
|
|
|
TIMER1_VAL;
|
2006-01-24 22:16:27 +00:00
|
|
|
|
/* enable timer */
|
2006-09-01 06:13:33 +00:00
|
|
|
|
TIMER1_CFG = 0xc0000000 | (interval_in_ms*1000 - 1);
|
2005-12-12 13:53:22 +00:00
|
|
|
|
/* unmask interrupt source */
|
2006-01-24 22:31:57 +00:00
|
|
|
|
CPU_INT_EN = TIMER1_MASK;
|
2006-01-05 17:02:48 +00:00
|
|
|
|
#else
|
|
|
|
|
/* We don't enable interrupts in the bootloader */
|
|
|
|
|
(void)interval_in_ms;
|
|
|
|
|
#endif
|
2005-11-07 23:07:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-01-12 00:35:50 +00:00
|
|
|
|
#elif CONFIG_CPU == PNX0101
|
|
|
|
|
|
|
|
|
|
void timer_handler(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++;
|
|
|
|
|
|
|
|
|
|
TIMERR0C = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void tick_start(unsigned int interval_in_ms)
|
|
|
|
|
{
|
|
|
|
|
TIMERR08 &= ~0x80;
|
|
|
|
|
TIMERR0C = 1;
|
|
|
|
|
TIMERR08 &= ~0x80;
|
|
|
|
|
TIMERR08 |= 0x40;
|
|
|
|
|
TIMERR00 = 3000000 * interval_in_ms / 1000;
|
|
|
|
|
TIMERR08 &= ~0xc;
|
|
|
|
|
TIMERR0C = 1;
|
|
|
|
|
|
|
|
|
|
irq_set_int_handler(4, timer_handler);
|
|
|
|
|
irq_enable_int(4);
|
|
|
|
|
|
|
|
|
|
TIMERR08 |= 0x80;
|
|
|
|
|
}
|
2006-08-12 08:01:54 +00:00
|
|
|
|
#elif CONFIG_CPU == S3C2440
|
|
|
|
|
void tick_start(unsigned int interval_in_ms)
|
|
|
|
|
{
|
|
|
|
|
unsigned long count;
|
|
|
|
|
|
|
|
|
|
/* period = (n + 1) / 128 , n = tick time count (1~127)*/
|
|
|
|
|
count = interval_in_ms / 1000 * 128 - 1;
|
2006-01-12 00:35:50 +00:00
|
|
|
|
|
2006-08-12 08:01:54 +00:00
|
|
|
|
if(count > 127)
|
|
|
|
|
{
|
|
|
|
|
panicf("Error! The tick interval is too long (%d ms)\n",
|
|
|
|
|
interval_in_ms);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Disable the tick */
|
|
|
|
|
TICNT &= ~(1<<7);
|
|
|
|
|
/* Set the count value */
|
|
|
|
|
TICNT |= count;
|
|
|
|
|
/* Start up the ticker */
|
|
|
|
|
TICNT |= (1<<7);
|
|
|
|
|
|
|
|
|
|
/* need interrupt handler ??? */
|
|
|
|
|
|
|
|
|
|
}
|
2004-10-27 07:07:54 +00:00
|
|
|
|
#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
|
|
|
|
|
2005-02-22 12:19:12 +00:00
|
|
|
|
#ifndef SIMULATOR
|
|
|
|
|
/*
|
|
|
|
|
* Simulator versions in uisimulator/SIMVER/
|
|
|
|
|
*/
|
|
|
|
|
|
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;
|
2006-09-16 16:18:11 +00:00
|
|
|
|
m->thread = NULL;
|
2002-05-16 20:57:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void mutex_lock(struct mutex *m)
|
|
|
|
|
{
|
2006-09-16 16:18:11 +00:00
|
|
|
|
if (m->locked)
|
|
|
|
|
{
|
|
|
|
|
/* Wait until the lock is open... */
|
2006-11-11 05:33:24 +00:00
|
|
|
|
block_thread(&m->thread);
|
2006-09-16 16:18:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2006-09-16 16:18:11 +00:00
|
|
|
|
if (m->thread == NULL)
|
|
|
|
|
m->locked = false;
|
|
|
|
|
else
|
|
|
|
|
wakeup_thread(&m->thread);
|
2002-05-16 20:57:32 +00:00
|
|
|
|
}
|
2005-02-22 12:19:12 +00:00
|
|
|
|
|
|
|
|
|
#endif
|