Moved implementation of user timer to the firmware layer, implemented it for iriver, and made it shareable based on priorities. On iriver, the user timer is shared between the backlight fading and other use, so if a plugin registers the timer, the backlight will resort to simple on/off switching until the plugin releases the timer again. Sorted and bumped the plugin api.

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@7242 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Jens Arnold 2005-07-26 20:01:11 +00:00
parent c20a00ef3e
commit e44372ef18
12 changed files with 337 additions and 202 deletions

View file

@ -45,6 +45,7 @@
#include "mp3data.h"
#include "powermgmt.h"
#include "system.h"
#include "timer.h"
#include "sound.h"
#include "database.h"
#if (CONFIG_HWCODEC == MASNONE)
@ -74,9 +75,6 @@ extern unsigned char pluginbuf[];
/* for actual plugins only, not for codecs */
static bool plugin_loaded = false;
static int plugin_size = 0;
#ifndef SIMULATOR
static void (*pfn_timer)(void) = NULL; /* user timer handler */
#endif
static void (*pfn_tsr_exit)(void) = NULL; /* TSR exit callback */
static int plugin_test(int api_version, int model, int memsize);
@ -116,6 +114,14 @@ static const struct plugin_api rockbox_api = {
lcd_fillrect,
lcd_mono_bitmap_part,
lcd_mono_bitmap,
#if LCD_DEPTH > 1
lcd_set_foreground,
lcd_get_foreground,
lcd_set_background,
lcd_get_background,
lcd_bitmap_part,
lcd_bitmap,
#endif
lcd_putsxy,
lcd_puts_style,
lcd_puts_scroll_style,
@ -126,6 +132,7 @@ static const struct plugin_api rockbox_api = {
scrollbar,
checkbox,
font_get,
font_getstringsize,
#endif
backlight_on,
backlight_off,
@ -211,6 +218,9 @@ static const struct plugin_api rockbox_api = {
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
cpu_boost,
#endif
timer_register,
timer_unregister,
timer_set_period,
#endif
/* strings and memory */
@ -220,6 +230,7 @@ static const struct plugin_api rockbox_api = {
strlen,
strrchr,
strcmp,
strncmp,
strcasecmp,
strncasecmp,
memset,
@ -289,6 +300,10 @@ static const struct plugin_api rockbox_api = {
&tagdb_fd,
&tagdb_initialized,
tagdb_init,
/* runtime database */
&rundbheader,
&rundb_fd,
&rundb_initialized,
/* misc */
srand,
@ -299,13 +314,12 @@ static const struct plugin_api rockbox_api = {
set_time,
plugin_get_buffer,
plugin_get_audio_buffer,
#ifndef SIMULATOR
plugin_register_timer,
plugin_unregister_timer,
#endif
plugin_tsr,
#if defined(DEBUG) || defined(SIMULATOR)
debugf,
#endif
#ifdef ROCKBOX_HAS_LOGF
logf,
#endif
&global_settings,
mp3info,
@ -326,24 +340,6 @@ static const struct plugin_api rockbox_api = {
/* new stuff at the end, sort into place next time
the API gets incompatible */
#ifdef ROCKBOX_HAS_LOGF
logf,
#endif
&rundbheader,
&rundb_fd,
&rundb_initialized,
strncmp,
#if LCD_DEPTH > 1
lcd_set_foreground,
lcd_get_foreground,
lcd_set_background,
lcd_get_background,
lcd_bitmap_part,
lcd_bitmap,
#endif
#ifdef HAVE_LCD_BITMAP
font_getstringsize,
#endif
};
int plugin_load(const char* plugin, void* parameter)
@ -486,68 +482,6 @@ void* plugin_get_audio_buffer(int* buffer_size)
return audiobuf;
}
#ifndef SIMULATOR
/* Register a periodic time callback, called every "cycles" CPU clocks.
Note that this function will be called in interrupt context! */
int plugin_register_timer(int cycles, int prio, void (*timer_callback)(void))
{
int phi = 0; /* bits for the prescaler */
int prescale = 1;
while (cycles > 0x10000)
{ /* work out the smallest prescaler that makes it fit */
phi++;
prescale *= 2;
cycles /= 2;
}
if (prescale > 8 || cycles == 0 || prio < 1 || prio > 15)
return 0; /* error, we can't do such period, bad argument */
backlight_allow_timer(false); /* stop backlight from messing with the timer */
#if CONFIG_CPU == SH7034
and_b(~0x10, &TSTR); /* Stop the timer 4 */
and_b(~0x10, &TSNC); /* No synchronization */
and_b(~0x10, &TMDR); /* Operate normally */
pfn_timer = timer_callback; /* install 2nd level ISR */
and_b(~0x01, &TSR4);
TIER4 = 0xF9; /* Enable GRA match interrupt */
GRA4 = (unsigned short)(cycles - 1);
TCR4 = 0x20 | phi; /* clear at GRA match, set prescaler */
IPRD = (IPRD & 0xFF0F) | prio << 4; /* interrupt priority */
or_b(0x10, &TSTR); /* start timer 4 */
#else
pfn_timer = timer_callback;
#endif
return cycles * prescale; /* return the actual period, in CPU clocks */
}
/* disable the user timer */
void plugin_unregister_timer(void)
{
#if CONFIG_CPU == SH7034
and_b(~0x10, &TSTR); /* stop the timer 4 */
IPRD = (IPRD & 0xFF0F); /* disable interrupt */
pfn_timer = NULL;
#endif
backlight_allow_timer(true);
}
#if CONFIG_CPU == SH7034
/* interrupt handler for user timer */
#pragma interrupt
void IMIA4(void)
{
if (pfn_timer != NULL)
pfn_timer(); /* call the user timer function */
and_b(~0x01, &TSR4); /* clear the interrupt */
}
#endif /* CONFIG_CPU == SH7034 */
#endif /* #ifndef SIMULATOR */
/* The plugin wants to stay resident after leaving its main function, e.g.
runs from timer or own thread. The callback is registered to later
instruct it to free its resources before a new plugin gets loaded. */

View file

@ -48,6 +48,7 @@
#include "pcm_playback.h"
#endif
#include "settings.h"
#include "timer.h"
#include "thread.h"
#include "playlist.h"
#ifdef HAVE_LCD_BITMAP
@ -87,12 +88,12 @@
#endif
/* increase this every time the api struct changes */
#define PLUGIN_API_VERSION 46
#define PLUGIN_API_VERSION 47
/* update this to latest version if a change to the api struct breaks
backwards compatibility (and please take the opportunity to sort in any
new function which are "waiting" at the end of the function table) */
#define PLUGIN_MIN_API_VERSION 42
#define PLUGIN_MIN_API_VERSION 47
/* plugin return codes */
enum plugin_status {
@ -168,6 +169,23 @@ struct plugin_api {
int stride, int x, int y, int width, int height);
void (*lcd_mono_bitmap)(const unsigned char *src, int x, int y,
int width, int height);
#if LCD_DEPTH > 1
#ifdef HAVE_LCD_COLOR
void (*lcd_set_foreground)(struct rgb color);
struct rgb (*lcd_get_foreground)(void);
void (*lcd_set_background)(struct rgb color);
struct rgb (*lcd_get_background)(void);
#else
void (*lcd_set_foreground)(int brightness);
int (*lcd_get_foreground)(void);
void (*lcd_set_background)(int brightness);
int (*lcd_get_background)(void);
#endif
void (*lcd_bitmap_part)(const unsigned char *src, int src_x, int src_y,
int stride, int x, int y, int width, int height);
void (*lcd_bitmap)(const unsigned char *src, int x, int y, int width,
int height);
#endif
void (*lcd_putsxy)(int x, int y, const unsigned char *string);
void (*lcd_puts_style)(int x, int y, const unsigned char *str, int style);
void (*lcd_puts_scroll_style)(int x, int y, const unsigned char* string,
@ -181,6 +199,8 @@ struct plugin_api {
int min_shown, int max_shown, int orientation);
void (*checkbox)(int x, int y, int width, int height, bool checked);
struct font* (*font_get)(int font);
int (*font_getstringsize)(const unsigned char *str, int *w, int *h,
int fontnumber);
#endif
void (*backlight_on)(void);
void (*backlight_off)(void);
@ -272,6 +292,11 @@ struct plugin_api {
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
void (*cpu_boost)(bool on_off);
#endif
bool (*timer_register)(int reg_prio, void (*unregister_callback)(void),
long cycles, int int_prio,
void (*timer_callback)(void));
void (*timer_unregister)(void);
bool (*timer_set_period)(long count);
#endif
/* strings and memory */
@ -281,6 +306,7 @@ struct plugin_api {
size_t (*strlen)(const char *str);
char * (*strrchr)(const char *s, int c);
int (*strcmp)(const char *, const char *);
int (*strncmp)(const char *, const char *, size_t);
int (*strcasecmp)(const char *, const char *);
int (*strncasecmp)(const char *s1, const char *s2, size_t n);
void* (*memset)(void *dst, int c, size_t length);
@ -350,6 +376,10 @@ struct plugin_api {
int *tagdb_fd;
int *tagdb_initialized;
int (*tagdb_init) (void);
/* runtime database */
struct rundb_header *rundbheader;
int *rundb_fd;
int *rundb_initialized;
/* misc */
void (*srand)(unsigned int seed);
@ -361,13 +391,12 @@ struct plugin_api {
int (*set_time)(const struct tm *tm);
void* (*plugin_get_buffer)(int* buffer_size);
void* (*plugin_get_audio_buffer)(int* buffer_size);
#ifndef SIMULATOR
int (*plugin_register_timer)(int cycles, int prio, void (*timer_callback)(void));
void (*plugin_unregister_timer)(void);
#endif
void (*plugin_tsr)(void (*exit_callback)(void));
#if defined(DEBUG) || defined(SIMULATOR)
void (*debugf)(const char *fmt, ...);
#endif
#ifdef ROCKBOX_HAS_LOGF
void (*logf)(const char *fmt, ...);
#endif
struct user_settings* global_settings;
bool (*mp3info)(struct mp3entry *entry, const char *filename, bool v1first);
@ -395,41 +424,11 @@ struct plugin_api {
/* new stuff at the end, sort into place next time
the API gets incompatible */
#ifdef ROCKBOX_HAS_LOGF
void (*logf)(const char *fmt, ...);
#endif
struct rundb_header *rundbheader;
int *rundb_fd;
int *rundb_initialized;
int (*strncmp)(const char *, const char *, size_t);
#if LCD_DEPTH > 1
#ifdef HAVE_LCD_COLOR
void (*lcd_set_foreground)(struct rgb color);
struct rgb (*lcd_get_foreground)(void);
void (*lcd_set_background)(struct rgb color);
struct rgb (*lcd_get_background)(void);
#else
void (*lcd_set_foreground)(int brightness);
int (*lcd_get_foreground)(void);
void (*lcd_set_background)(int brightness);
int (*lcd_get_background)(void);
#endif
void (*lcd_bitmap_part)(const unsigned char *src, int src_x, int src_y,
int stride, int x, int y, int width, int height);
void (*lcd_bitmap)(const unsigned char *src, int x, int y, int width,
int height);
#endif
#ifdef HAVE_LCD_BITMAP
int (*font_getstringsize)(const unsigned char *str, int *w, int *h,
int fontnumber);
#endif
};
int plugin_load(const char* plugin, void* parameter);
void* plugin_get_buffer(int *buffer_size);
void* plugin_get_audio_buffer(int *buffer_size);
int plugin_register_timer(int cycles, int prio, void (*timer_callback)(void));
void plugin_unregister_timer(void);
void plugin_tsr(void (*exit_callback)(void));
/* defined by the plugin */

View file

@ -230,15 +230,15 @@ void timer_set_mode(int mode)
if (mode == TM_RX_TIMEOUT)
{
rb->plugin_register_timer(gTimer.timeout, 11, timer4_isr);
rb->timer_register(1, NULL, gTimer.timeout, 11, timer4_isr);
}
else if (mode == TM_TRANSMIT)
{
rb->plugin_register_timer(gTimer.transmit, 14, timer4_isr);
rb->timer_register(1, NULL, gTimer.transmit, 14, timer4_isr);
}
else
{
rb->plugin_unregister_timer();
rb->timer_unregister();
}
}
@ -252,7 +252,7 @@ void timer4_isr(void) /* IMIA4 */
break;
case TM_RX_TIMEOUT:
receive_timeout_isr();
rb->plugin_unregister_timer(); /* single shot */
rb->timer_unregister(); /* single shot */
break;
default:
timer_set_mode(TM_OFF); /* spurious interrupt */

View file

@ -257,11 +257,11 @@ void gray_show(bool enable)
if (enable)
{
_gray_info.flags |= _GRAY_RUNNING;
_gray_rb->plugin_register_timer(FREQ / 67, 1, _timer_isr);
_gray_rb->timer_register(1, NULL, FREQ / 67, 1, _timer_isr);
}
else
{
_gray_rb->plugin_unregister_timer();
_gray_rb->timer_unregister();
_gray_info.flags &= ~_GRAY_RUNNING;
_gray_rb->lcd_update(); /* restore whatever there was before */
}

View file

@ -235,7 +235,7 @@ void cleanup(void *parameter)
{
(void)parameter;
rb->plugin_unregister_timer();
rb->timer_unregister();
rb->mp3_play_stop(); /* stop audio ISR */
led(0);
}
@ -278,7 +278,7 @@ enum plugin_status plugin_start(struct plugin_api* api, void* parameter){
rb->mp3_play_stop(); // stop audio ISR
calc_period();
rb->plugin_register_timer(((*rb->cpu_frequency)/1024), 1, timer_callback);
rb->timer_register(1, NULL, (*rb->cpu_frequency)/1024, 1, timer_callback);
draw_display();

View file

@ -192,7 +192,7 @@ void cleanup(void *parameter)
{
(void)parameter;
rb->plugin_unregister_timer();
rb->timer_unregister();
}
enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
@ -205,7 +205,7 @@ enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
(void)parameter;
rb = api;
rb->plugin_register_timer(FREQ / 67, 1, timer_isr);
rb->timer_register(1, NULL, FREQ / 67, 1, timer_isr);
while (!exit)
{

View file

@ -388,7 +388,7 @@ void timer4_isr(void)
else
{
gPlay.bVideoUnderrun = true;
rb->plugin_unregister_timer(); // disable ourselves
rb->timer_unregister(); // disable ourselves
return; // no data available
}
}
@ -490,7 +490,7 @@ int SeekTo(int fd, int nPos)
if (gPlay.bHasAudio)
rb->mp3_play_stop(); // stop audio ISR
if (gPlay.bHasVideo)
rb->plugin_unregister_timer(); // stop the timer
rb->timer_unregister(); // stop the timer
rb->lseek(fd, nPos, SEEK_SET);
@ -538,9 +538,9 @@ int SeekTo(int fd, int nPos)
gPlay.bVideoUnderrun = false;
// start display interrupt
#if FREQ == 12000000 /* Ondio speed kludge */
rb->plugin_register_timer(gPlay.nFrameTimeAdjusted, 1, timer4_isr);
rb->timer_register(1, NULL, gPlay.nFrameTimeAdjusted, 1, timer4_isr);
#else
rb->plugin_register_timer(gFileHdr.video_frametime, 1, timer4_isr);
rb->timer_register(1, NULL, gFileHdr.video_frametime, 1, timer4_isr);
#endif
}
@ -553,7 +553,7 @@ void Cleanup(void *fd)
rb->close(*(int*)fd); // close the file
if (gPlay.bHasVideo)
rb->plugin_unregister_timer(); // stop video ISR, now I can use the display again
rb->timer_unregister(); // stop video ISR, now I can use the display again
if (gPlay.bHasAudio)
rb->mp3_play_stop(); // stop audio ISR
@ -705,7 +705,7 @@ int PlayTick(int fd)
if (gPlay.bHasAudio)
rb->mp3_play_pause(false); // pause audio
if (gPlay.bHasVideo)
rb->plugin_unregister_timer(); // stop the timer
rb->timer_unregister(); // stop the timer
}
else if (gPlay.state == paused)
{
@ -719,10 +719,10 @@ int PlayTick(int fd)
if (gPlay.bHasVideo)
{ // start the video
#if FREQ == 12000000 /* Ondio speed kludge */
rb->plugin_register_timer(
rb->timer_register(1, NULL,
gPlay.nFrameTimeAdjusted, 1, timer4_isr);
#else
rb->plugin_register_timer(
rb->timer_register(1, NULL,
gFileHdr.video_frametime, 1, timer4_isr);
#endif
}

View file

@ -101,6 +101,7 @@ hwcompat.c
kernel.c
rolo.c
thread.c
timer.c
crt0.S
#endif
mp3_playback.c

View file

@ -27,6 +27,7 @@
#include "usb.h"
#include "power.h"
#include "system.h"
#include "timer.h"
#ifdef HAVE_REMOTE_LCD
#include "lcd-remote.h"
@ -66,7 +67,6 @@ static unsigned int remote_backlight_timeout = 5;
static const char backlight_fade_value[8] = { 0, 1, 2, 4, 6, 8, 10, 20 };
static int fade_in_count = 1;
static int fade_out_count = 4;
static bool timer_allowed = true;
static bool bl_timer_active = false;
static int bl_dim_current = BL_PWM_COUNT;
@ -75,44 +75,20 @@ static int bl_pwm_counter = 0;
static volatile int bl_cycle_counter = 0;
static enum {DIM_STATE_START, DIM_STATE_MAIN} bl_dim_state = DIM_STATE_START;
void backlight_start_timer(void)
{
unsigned int count;
if (bl_timer_active)
return ;
/* Prevent cpu frequency changes while dimming. */
cpu_boost(true);
count = 1;
bl_timer_active = true;
/* We are using timer 1 */
TRR1 = count; /* The reference count */
TCN1 = 0; /* reset the timer */
TMR1 = 0x011d; /* prescaler=2, restart, CLK/16, enabled */
TER1 = 0xff; /* Clear all events */
/* ICR2 (Timer2) */
ICR0 = (ICR0 & 0xffff00ff) | 0x00009000; /* Interrupt on level 4.0 */
IMR &= ~(1<<10);
}
void TIMER1(void) __attribute__ ((interrupt_handler));
void TIMER1(void)
static void backlight_isr(void)
{
int timer_period;
bool idle = false;
timer_period = FREQ / 2000 * BL_PWM_INTERVAL / 1000 / 32;
switch (bl_dim_state) {
timer_period = FREQ / 1000 * BL_PWM_INTERVAL / 1000;
switch (bl_dim_state)
{
/* New cycle */
case DIM_STATE_START:
bl_pwm_counter = 0;
bl_cycle_counter++;
if (bl_dim_current > 0 && bl_dim_current < BL_PWM_COUNT)
if (bl_dim_current > 0 && bl_dim_current < BL_PWM_COUNT)
{
and_l(~0x00020000, &GPIO1_OUT);
bl_pwm_counter = bl_dim_current;
@ -125,6 +101,7 @@ void TIMER1(void)
and_l(~0x00020000, &GPIO1_OUT);
else
or_l(0x00020000, &GPIO1_OUT);
if (bl_dim_current == bl_dim_target)
idle = true;
}
@ -137,9 +114,8 @@ void TIMER1(void)
bl_dim_state = DIM_STATE_START;
timer_period = timer_period * (BL_PWM_COUNT - bl_pwm_counter) / BL_PWM_COUNT;
break ;
}
if ((bl_dim_target > bl_dim_current) && (bl_cycle_counter >= fade_in_count))
{
bl_dim_current++;
@ -155,37 +131,50 @@ void TIMER1(void)
if (idle)
{
cpu_boost(false);
timer_unregister();
bl_timer_active = false;
TMR1 = 0;
}
TRR1 = timer_period;
TCN1 = 0;
TER1 = 0xff; /* Clear all events */
else
timer_set_period(timer_period);
}
static void __backlight_dim(int value)
static void backlight_switch(void)
{
if (bl_dim_target > (BL_PWM_COUNT/2))
{
and_l(~0x00020000, &GPIO1_OUT);
bl_dim_current = BL_PWM_COUNT;
}
else
{
or_l(0x00020000, &GPIO1_OUT);
bl_dim_current = 0;
}
}
static void backlight_release_timer(void)
{
cpu_boost(false);
timer_unregister();
bl_timer_active = false;
backlight_switch();
}
static void backlight_dim(int value)
{
bl_dim_target = value;
backlight_start_timer();
}
void backlight_allow_timer(bool on)
{
timer_allowed = on;
if (bl_timer_active)
return ;
if (!timer_allowed && bl_timer_active)
if (timer_register(0, backlight_release_timer, 1, 0, backlight_isr))
{
cpu_boost(false);
bl_dim_current = bl_dim_target;
bl_timer_active = false;
TMR1 = 0;
if (bl_dim_current)
and_l(~0x00020000, &GPIO1_OUT);
else
or_l(0x00020000, &GPIO1_OUT);
/* Prevent cpu frequency changes while dimming. */
cpu_boost(true);
bl_timer_active = true;
}
else
backlight_switch();
}
void backlight_set_fade_in(int index)
@ -202,8 +191,8 @@ void backlight_set_fade_out(int index)
static void __backlight_off(void)
{
#if CONFIG_BACKLIGHT == BL_IRIVER
if (timer_allowed && (fade_out_count > 0))
__backlight_dim(0);
if (fade_out_count > 0)
backlight_dim(0);
else
{
bl_dim_target = bl_dim_current = 0;
@ -224,8 +213,8 @@ static void __backlight_off(void)
static void __backlight_on(void)
{
#if CONFIG_BACKLIGHT == BL_IRIVER
if (timer_allowed && (fade_in_count > 0))
__backlight_dim(BL_PWM_COUNT);
if (fade_in_count > 0)
backlight_dim(BL_PWM_COUNT);
else
{
bl_dim_target = bl_dim_current = BL_PWM_COUNT;

View file

@ -30,9 +30,6 @@ void backlight_set_timeout(int index);
#if CONFIG_BACKLIGHT == BL_IRIVER
void backlight_set_fade_in(int index);
void backlight_set_fade_out(int index);
void backlight_allow_timer(bool on);
#else
#define backlight_allow_timer(on)
#endif
bool backlight_get_on_when_charging(void);
void backlight_set_on_when_charging(bool yesno);

34
firmware/export/timer.h Normal file
View file

@ -0,0 +1,34 @@
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
* Copyright (C) 2005 Jens Arnold
*
* 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.
*
****************************************************************************/
#ifndef __TIMER_H__
#define __TIMER_H__
#include <stdbool.h>
#include "config.h"
#ifndef SIMULATOR
bool timer_register(int reg_prio, void (*unregister_callback)(void),
long cycles, int int_prio, void (*timer_callback)(void));
bool timer_set_period(long cycles);
void timer_unregister(void);
#endif /* !SIMULATOR */
#endif /* __TIMER_H__ */

181
firmware/timer.c Normal file
View file

@ -0,0 +1,181 @@
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
* Copyright (C) 2005 Jens Arnold
*
* 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.
*
****************************************************************************/
#include <stdbool.h>
#include "config.h"
#include "cpu.h"
#include "system.h"
#include "timer.h"
#ifndef SIMULATOR
static int timer_prio = -1;
static void (*pfn_timer)(void) = NULL; /* timer callback */
static void (*pfn_unregister)(void) = NULL; /* unregister callback */
/* interrupt handler */
#if CONFIG_CPU == SH7034
void IMIA4(void) __attribute__((interrupt_handler));
void IMIA4(void)
{
if (pfn_timer != NULL)
pfn_timer();
and_b(~0x01, &TSR4); /* clear the interrupt */
}
#elif defined CPU_COLDFIRE
void TIMER1(void) __attribute__ ((interrupt_handler));
void TIMER1(void)
{
if (pfn_timer != NULL)
pfn_timer();
TER1 = 0xff; /* clear all events */
}
#endif /* CONFIG_CPU */
static bool timer_set(long cycles, bool start)
{
int phi = 0; /* bits for the prescaler */
int prescale = 1;
#ifdef CPU_COLDFIRE
cycles >>= 1; /* the coldfire timer works on busclk == cpuclk/2 */
#endif
while (cycles > 0x10000)
{ /* work out the smallest prescaler that makes it fit */
#if CONFIG_CPU == SH7034
phi++;
#endif
prescale *= 2;
cycles >>= 1;
}
#if CONFIG_CPU == SH7034
if (prescale > 8)
return false;
if (start)
{
if (pfn_unregister != NULL)
{
pfn_unregister();
pfn_unregister = NULL;
}
and_b(~0x10, &TSTR); /* Stop the timer 4 */
and_b(~0x10, &TSNC); /* No synchronization */
and_b(~0x10, &TMDR); /* Operate normally */
TIER4 = 0xF9; /* Enable GRA match interrupt */
}
TCR4 = 0x20 | phi; /* clear at GRA match, set prescaler */
GRA4 = (unsigned short)(cycles - 1);
if (start || (TCNT4 >= GRA4))
TCNT4 = 0;
and_b(~0x01, &TSR4); /* clear an eventual interrupt */
#elif defined CPU_COLDFIRE
if (prescale > 4096)
return false;
if (prescale > 256)
{
phi = 0x05; /* prescale sysclk/16, timer enabled */
prescale >>= 4;
}
else
phi = 0x03; /* prescale sysclk, timer enabled */
if (start)
{
if (pfn_unregister != NULL)
{
pfn_unregister();
pfn_unregister = NULL;
}
phi &= ~1; /* timer disabled at start */
}
/* We are using timer 1 */
TMR1 = 0x0018 | (unsigned short)phi | ((unsigned short)(prescale - 1) << 8);
TRR1 = (unsigned short)(cycles - 1);
if (start || (TCN1 >= TRR1))
TCN1 = 0; /* reset the timer */
TER1 = 0xff; /* clear all events */
#endif /* CONFIG_CPU */
return true;
}
/* Register a user timer, called every <count> CPU cycles */
bool timer_register(int reg_prio, void (*unregister_callback)(void),
long cycles, int int_prio, void (*timer_callback)(void))
{
if (reg_prio <= timer_prio || cycles == 0)
return false;
#if CONFIG_CPU == SH7034
if (int_prio < 1 || int_prio > 15)
return false;
#elif defined CPU_COLDFIRE
(void)int_prio;
#endif
if (!timer_set(cycles, true))
return false;
pfn_timer = timer_callback;
pfn_unregister = unregister_callback;
timer_prio = reg_prio;
#if CONFIG_CPU == SH7034
IPRD = (IPRD & 0xFF0F) | int_prio << 4; /* interrupt priority */
or_b(0x10, &TSTR); /* start timer 4 */
#elif defined CPU_COLDFIRE
/* ICR2 (Timer1) */
ICR0 = (ICR0 & 0xffff00ff) | 0x00009000; /* interrupt on level 4.0 */
and_l(~(1<<10), &IMR);
TMR1 |= 1; /* start timer */
#endif
return true;
}
bool timer_set_period(long cycles)
{
return timer_set(cycles, false);
}
void timer_unregister(void)
{
#if CONFIG_CPU == SH7034
and_b(~0x10, &TSTR); /* stop the timer 4 */
IPRD = (IPRD & 0xFF0F); /* disable interrupt */
#elif defined CPU_COLDFIRE
TMR1 = 0; /* disable timer 1 */
or_l((1<<10), &IMR); /* disable interrupt */
#endif
pfn_timer = NULL;
pfn_unregister = NULL;
timer_prio = -1;
}
#endif /* !SIMULATOR */