Index: firmware/export/config-fuze.h =================================================================== --- firmware/export/config-fuze.h (revision 19555) +++ firmware/export/config-fuze.h (working copy) @@ -109,7 +109,7 @@ /* define this if the unit uses a scrollwheel for navigation */ #define HAVE_SCROLLWHEEL /* define from which rotation speed [degree/sec] on the acceleration starts */ -#define WHEEL_ACCEL_START 540 +#define WHEEL_ACCEL_START 3000 /* define type of acceleration (1 = ^2, 2 = ^3, 3 = ^4) */ #define WHEEL_ACCELERATION 1 Index: firmware/target/arm/as3525/clock-target.h =================================================================== --- firmware/target/arm/as3525/clock-target.h (revision 19555) +++ firmware/target/arm/as3525/clock-target.h (working copy) @@ -53,7 +53,7 @@ #elif defined(SANSA_M200V4) #define AS3525_DBOP_FREQ 8000000 #elif defined(SANSA_FUZE) -#define AS3525_DBOP_FREQ 24000000 +#define AS3525_DBOP_FREQ 8000000 #elif defined(SANSA_E200V2) #define AS3525_DBOP_FREQ 8000000 #endif Index: firmware/target/arm/as3525/sansa-fuze/button-fuze.c =================================================================== --- firmware/target/arm/as3525/sansa-fuze/button-fuze.c (revision 19555) +++ firmware/target/arm/as3525/sansa-fuze/button-fuze.c (working copy) @@ -7,7 +7,8 @@ * \/ \/ \/ \/ \/ * $Id$ * - * Copyright (C) 2006 by Barry Wardell + * Copyright (C) 2008 by Thomas Martitz + * Copyright (C) 2008 by Dominik Wenger * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -19,7 +20,6 @@ * ****************************************************************************/ -/* Taken from button-h10.c by Barry Wardell and reverse engineering by MrH. */ #include "system.h" #include "button.h" @@ -29,107 +29,290 @@ #define WHEEL_REPEAT_INTERVAL 300000 #define WHEEL_FAST_ON_INTERVAL 20000 #define WHEEL_FAST_OFF_INTERVAL 60000 -#define WHEELCLICKS_PER_ROTATION 48 /* wheelclicks per full rotation */ +#define WHEELCLICKS_PER_ROTATION 150 /* wheelclicks per full rotation */ +#ifndef BOOTLOADER +#if defined(HAVE_SCROLLWHEEL) /* Clickwheel */ -#ifndef BOOTLOADER -static unsigned int old_wheel_value = 0; -static unsigned int wheel_repeat = BUTTON_NONE; -static unsigned int wheel_click_count = 0; -static unsigned int wheel_delta = 0; -static int wheel_fast_mode = 0; -static unsigned long last_wheel_usec = 0; -static unsigned long wheel_velocity = 0; -static long last_wheel_post = 0; -static long next_backlight_on = 0; +static unsigned int old_wheel_value = 0; +static unsigned int wheel_value = 0; +static unsigned int wheel_repeat = BUTTON_NONE; +static unsigned int wheel_click_count = 0; +static unsigned int wheel_delta = 0; +static int wheel_fast_mode = 0; +static unsigned long last_wheel_usec = 0; +static unsigned long wheel_velocity = 0; +static long last_wheel_post = 0; +static long next_backlight_on = 0; +#endif /* HAVE_SCROLLWHEEL */ /* Buttons */ static bool hold_button = false; static bool hold_button_old = false; -#define _button_hold() hold_button -#else -#define _button_hold() false /* FIXME */ -#endif /* BOOTLOADER */ -static int int_btn = BUTTON_NONE; - +#endif /* !BOOTLOADER */ +static int int_btn = BUTTON_NONE; +static short dbop_din = BUTTON_NONE; void button_init_device(void) { + GPIOA_DIR |= (1<<1); + GPIOA_PIN(1) = (1<<1); } -bool button_hold(void) +/* clickwheel */ +#if !defined(BOOTLOADER) && defined(HAVE_SCROLLWHEEL) +static void get_wheel(void) { - return _button_hold(); + + /* Read wheel + * Bits 13 and 14 of DBOP_DIN change as follows: + * Clockwise rotation 00 -> 01 -> 11 -> 10 -> 00 + * Counter-clockwise 00 -> 10 -> 11 -> 01 -> 00 + * + * that is for the 3rd byte of DBOP_DIN + * Clockwise: 0x0 > 0x2 > 0x6 > 0x4 + * Counter-clockwise: 0x0 > 0x4 > 0x6 > 0x2 + */ + static const unsigned char wheel_tbl[2][4] = + { + { 2, 0, 3, 1 }, /* Clockwise rotation */ + { 1, 3, 0, 2 }, /* Counter-clockwise */ + }; + wheel_value = dbop_din & (1<<13|1<<14); + wheel_value >>= 13; + /* did the wheel value change? */ + if (!hold_button) + { + unsigned int btn = BUTTON_NONE; + + if (old_wheel_value == wheel_tbl[0][wheel_value]) + btn = BUTTON_SCROLL_FWD; + else if (old_wheel_value == wheel_tbl[1][wheel_value]) + btn = BUTTON_SCROLL_BACK; + + if (btn != BUTTON_NONE) + { + int repeat = 1; /* assume repeat */ + unsigned long usec = TIMER2_VALUE; + unsigned v = (usec - last_wheel_usec) & 0x7fffffff; + + v = (v>0) ? 1000000 / v : 0; /* clicks/sec = 1000000 * clicks/usec */ + v = (v>0xffffff) ? 0xffffff : v; /* limit to 24 bit */ + + /* some velocity filtering to smooth things out */ + wheel_velocity = (7*wheel_velocity + v) / 8; + + if (btn != wheel_repeat) + { + /* direction reversals nullify all fast mode states */ + wheel_repeat = btn; + repeat = + wheel_fast_mode = + wheel_velocity = + wheel_click_count = 0; + } + + if (wheel_fast_mode != 0) + { + /* fast OFF happens immediately when velocity drops below + threshold */ + if (TIME_AFTER(usec, + last_wheel_usec + WHEEL_FAST_OFF_INTERVAL)) + { + /* moving out of fast mode */ + wheel_fast_mode = 0; + /* reset velocity */ + wheel_velocity = 0; + /* wheel_delta is always 1 in slow mode */ + wheel_delta = 1; + } + } + else + { + /* fast ON gets filtered to avoid inadvertent jumps to fast mode */ + if (repeat && wheel_velocity > 1000000/WHEEL_FAST_ON_INTERVAL) + { + /* moving into fast mode */ + wheel_fast_mode = 1 << 31; + wheel_click_count = 0; + wheel_velocity = 1000000/WHEEL_FAST_OFF_INTERVAL; + } + else if (++wheel_click_count < 2) + { + btn = BUTTON_NONE; + } + + /* wheel_delta is always 1 in slow mode */ + wheel_delta = 1; + } + + if (TIME_AFTER(current_tick, next_backlight_on) || + v <= 4) + { + /* poke backlight to turn it on or maintain it no more often + than every 1/4 second*/ + next_backlight_on = current_tick + HZ/4; + backlight_on(); + buttonlight_on(); + reset_poweroff_timer(); + } + + if (btn != BUTTON_NONE) + { + wheel_click_count = 0; + + /* generate repeats if quick enough */ + if (repeat && TIME_BEFORE(usec, + last_wheel_post + WHEEL_REPEAT_INTERVAL)) + btn |= BUTTON_REPEAT; + + last_wheel_post = usec; + + if (queue_empty(&button_queue)) + { + queue_post(&button_queue, btn, wheel_fast_mode | + (wheel_delta << 24) | wheel_velocity*360/WHEELCLICKS_PER_ROTATION); + /* message posted - reset delta */ + wheel_delta = 1; + } + else + { + /* skipped post - increment delta */ + if (++wheel_delta > 0x7f) + wheel_delta = 0x7f; + } + } + + last_wheel_usec = usec; + int_btn |= BUTTON_SCROLL; + } + } + old_wheel_value = wheel_value; } +#endif /* !defined(BOOTLOADER) && defined(SCROLLWHEEL) */ -/* clickwheel */ -#ifndef BOOTLOADER -void clickwheel_int(void) +/* get hold button state */ +static void get_hold(void) { + hold_button = dbop_din & (1<<12); } -#endif /* BOOTLOADER */ -/* device buttons */ +bool button_hold(void) +{ +#ifndef BOOTLOADER + return hold_button; +#else + return false; +#endif +} -/* device buttons */ -void button_int(void) +static void get_power(void) { - int dir_save_b = 0; - int afsel_save_b = 0; - int dir_save_c = 0; - int afsel_save_c = 0; + if (dbop_din & (1<<8)) + int_btn |= BUTTON_POWER; +} - int_btn = BUTTON_NONE; +static void get_button_from_dbob(void) +{ + int_btn &= ~(BUTTON_HOLD| + BUTTON_POWER| + BUTTON_SCROLL_BACK| + BUTTON_SCROLL_FWD| + BUTTON_SCROLL); - /* Save the current direction and afsel */ - dir_save_b = GPIOB_DIR; - afsel_save_b = GPIOB_AFSEL; - dir_save_c = GPIOC_DIR; - afsel_save_c = GPIOC_AFSEL; + /* Wait for fifo to empty */ + while ((DBOP_STAT & (1<<10)) == 0); - GPIOB_DIR = 0; - GPIOB_AFSEL = 0; - GPIOC_DIR = 0; - GPIOC_AFSEL = 0; + DBOP_CTRL |= (1<<19); + DBOP_CTRL &= ~(1<<16); /* disable output */ - /* These should not be needed with button event interupts */ - /* they are necessary now to clear out lcd data */ - GPIOC_PIN(0) |= 1; - GPIOC_PIN(1) |= 1; - GPIOC_PIN(2) |= 1; - GPIOC_PIN(3) |= 1; - GPIOC_PIN(4) |= 1; - GPIOC_PIN(5) |= 1; - GPIOC_PIN(6) |= 1; - GPIOC_PIN(7) |= 1; + DBOP_TIMPOL_01 = 0xe167e167; + DBOP_TIMPOL_23 = 0xe167006e; + int loop = 0; + do + { + asm volatile ("nop\n"); + loop++; + } while(loop < 64); + DBOP_CTRL |= (1<<15); /* start read */ + int temp; + do + { + temp = DBOP_STAT; + } while ((temp & (1<<16)) == 0); /* wait for valid data */ + + dbop_din = DBOP_DIN; /* now read */ + + DBOP_TIMPOL_01 = 0x6e167; + DBOP_TIMPOL_23 = 0xa167e06f; + + DBOP_CTRL |= (1<<16); + DBOP_CTRL &= ~(1<<19); + + get_hold(); + + get_wheel(); + + get_power(); +} + +static void get_button_from_gpio(void) +{ + /* reset buttons we're going to read */ + int_btn &= ~(BUTTON_LEFT| + BUTTON_RIGHT| + BUTTON_UP| + BUTTON_DOWN| + BUTTON_SELECT); + if(hold_button) + return; + /* set afsel, so that we can read our buttons */ + GPIOC_AFSEL &= ~(1<<2|1<<3|1<<4|1<<5|1<<6); + /* set dir so we can read our buttons (but reset the C pins first) */ + GPIOB_DIR &= ~(1<<4); + GPIOC_DIR |= (1<<2|1<<3|1<<4|1<<5|1<<6); + GPIOC_PIN(2) |= (1<<2); + GPIOC_PIN(3) |= (1<<3); + GPIOC_PIN(4) |= (1<<4); + GPIOC_PIN(5) |= (1<<5); + GPIOC_PIN(6) |= (1<<6); + + GPIOC_DIR &= ~(1<<2|1<<3|1<<4|1<<5|1<<6); + + /* small delay needed to read buttons correctly */ + int delay = 50; + while(delay >0) delay--; + /* direct GPIO connections */ - if (GPIOB_PIN(4)) - int_btn |= BUTTON_POWER; + if (!GPIOC_PIN(3)) + int_btn |= BUTTON_LEFT; + if (!GPIOC_PIN(2)) + int_btn |= BUTTON_UP; if (!GPIOC_PIN(6)) int_btn |= BUTTON_DOWN; if (!GPIOC_PIN(5)) int_btn |= BUTTON_RIGHT; if (!GPIOC_PIN(4)) int_btn |= BUTTON_SELECT; - if (!GPIOC_PIN(3)) - int_btn |= BUTTON_LEFT; - if (!GPIOC_PIN(2)) - int_btn |= BUTTON_UP; - /* return to settings needed for lcd */ - GPIOB_DIR = dir_save_b; - GPIOB_AFSEL = afsel_save_b; - GPIOC_DIR = dir_save_c; - GPIOC_AFSEL = afsel_save_c; + GPIOC_DIR |= (1<<2|1<<3|1<<4|1<<5|1<<6); + GPIOC_AFSEL |= (1<<2|1<<3|1<<4|1<<5|1<<6); } +static inline void get_buttons_from_hw(void) +{ + get_button_from_dbob(); + get_button_from_gpio(); +} /* * Get button pressed from hardware */ int button_read_device(void) { + get_buttons_from_hw(); #ifdef BOOTLOADER - /* Read buttons directly in the bootloader */ +/* button_int(); +*/ #else /* light handling */ if (hold_button != hold_button_old) @@ -139,6 +322,5 @@ } #endif /* BOOTLOADER */ - /* The int_btn variable is set in the button interrupt handler */ - return int_btn; + return int_btn; /* set in button_int */ } Index: firmware/target/arm/as3525/sansa-fuze/button-target.h =================================================================== --- firmware/target/arm/as3525/sansa-fuze/button-target.h (revision 19555) +++ firmware/target/arm/as3525/sansa-fuze/button-target.h (working copy) @@ -50,12 +50,14 @@ #define BUTTON_SCROLL_BACK 0x00000080 #define BUTTON_SCROLL_FWD 0x00000100 +#define BUTTON_SCROLL 0x00000200 -#define BUTTON_HOLD 0x00000200 +#define BUTTON_HOLD 0x00000400 #define BUTTON_MAIN (BUTTON_HOME|BUTTON_DOWN|BUTTON_RIGHT|BUTTON_LEFT \ |BUTTON_SELECT|BUTTON_UP|BUTTON_POWER \ - |BUTTON_SCROLLBACK|BUTTON_SCROLL_FWD|BUTTON_HOLD) + |BUTTON_SCROLLBACK|BUTTON_SCROLL_FWD|BUTTON_SCROLL| \ + |BUTTON_HOLD) /* No Remote control */ #define BUTTON_REMOTE 0 Index: firmware/target/arm/as3525/sansa-fuze/lcd-fuze.c =================================================================== --- firmware/target/arm/as3525/sansa-fuze/lcd-fuze.c (revision 19555) +++ firmware/target/arm/as3525/sansa-fuze/lcd-fuze.c (working copy) @@ -53,7 +53,7 @@ GPIOB_AFSEL = 0xfc; GPIOC_AFSEL = 0xff; - DBOP_TIMPOL_23 = 0x6000e; + DBOP_TIMPOL_23 = 0x6006e; DBOP_CTRL = 0x51008; DBOP_TIMPOL_01 = 0x6e167; DBOP_TIMPOL_23 = 0xa167e06f; @@ -252,8 +252,13 @@ GPIOA_PIN(3) = (1<<3); GPIOA_PIN(4) = 0; GPIOA_PIN(7) = 0; + + GPIOD_DIR |= (1<<7); + GPIOD_PIN(7) = (1<<7); + GPIOA_PIN(5) = (1<<5); + _display_on(); }