Index: firmware/target/arm/as3525/sansa-e200v2/button-e200v2.c =================================================================== --- firmware/target/arm/as3525/sansa-e200v2/button-e200v2.c (revision 19907) +++ firmware/target/arm/as3525/sansa-e200v2/button-e200v2.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,116 +20,252 @@ * ****************************************************************************/ -/* Taken from button-h10.c by Barry Wardell and reverse engineering by MrH. */ +/* Basic button driver for the E200v2 + * + * TODO: - Get the wheel working with interrupts + */ + #include "system.h" #include "button.h" +/*#include "button-target.h"*/ #include "backlight.h" -#include "powermgmt.h" -#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 */ -/* 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; /* 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; +#define hold_button false +#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); } +#if !defined(BOOTLOADER) + /* clickwheel:Sends BUTTON_SCROLL_FWD or BUTTON_SCROLL_BACK or BUTTON_REPEAT + to button_queue*/ +static void queue_wheel(void) +{ + static unsigned int wheel_value = 0; + static unsigned int old_wheel_value = 0; + static unsigned int wheel_repeat = BUTTON_NONE; + static int repeat; + static int counter = 0; + + static const unsigned char wheel_tbl[2][4] = + { + { 2, 0, 3, 1 }, /* Clockwise rotation */ + { 1, 3, 0, 2 }, /* Counter-clockwise */ + }; + /* 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 + */ + wheel_value = _dbop_din & (1<<13|1<<14); + wheel_value >>= 13; + + if (!hold_button) + { + /* did the wheel value change? */ + 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) + { + if (btn != wheel_repeat) + { + /* direction reversals nullify repeats */ + wheel_repeat = btn; + repeat = 0; + } + if (btn != BUTTON_NONE) + { + /* generate repeats if quick enough */ + /* getting BUTTON_REPEAT works like this: We increment repeat by + * if the wheel was turned, and decrement it by 1 each tick, + * that means: if you change the wheel fast enough, repeat will + * be >1 and we send BUTTON_REPEAT + */ + if (repeat > 0) + { + btn |= BUTTON_REPEAT; + } + repeat += 2; + + /* the wheel is more reliable if we don't send every change, + * every 4th is basically one "physical click" and is 1 item in + * the rockbox menus. We omit 3 of 4 posts to the button_queue, + * that works better. + */ + + if (queue_empty(&button_queue) && ++counter >= 4) + { + buttonlight_on(); + backlight_on(); + /* 1<<24 is rather arbitary, seems to work */ + queue_post(&button_queue, btn, 1<<24); + /* message posted - reset counter */ + counter = 0; + } + } + } + } + if (repeat > 0) + repeat--; + else + repeat = 0; + old_wheel_value = wheel_value; +} +#endif /* !defined(BOOTLOADER) */ + +#if !defined(BOOTLOADER) +/* get hold button state */ +static void get_hold(void) +{ + hold_button = _dbop_din & (1<<12); +} +#endif + bool button_hold(void) { - return _button_hold(); + return hold_button; } -/* clickwheel */ -#ifndef BOOTLOADER -void clickwheel_int(void) +static void get_power(void) { + if (_dbop_din & (1<<8)) + int_btn |= BUTTON_POWER; } -#endif /* BOOTLOADER */ -/* device buttons */ -void button_int(void) +static void get_rec(void) { - int dir_save_b = 0; - int afsel_save_b = 0; - int dir_save_c = 0; - int afsel_save_c = 0; + /* Bypass until Rockbox boots up or else settings are cleared. 57 or + * greater works, 56 does not. + */ - int_btn = BUTTON_NONE; + static int wait = 57; + if (wait-- <= 0) + { + wait=0; + if (!(_dbop_din & (1<<15))) + int_btn |= BUTTON_REC; + } +} - /* 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; +static void get_button_from_dbob(void) +{ + /* 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); + + /* Now reset buttons we're going to read from DBOP */ + int_btn &= ~(BUTTON_REC| + BUTTON_POWER); + +#if !defined(BOOTLOADER) + get_rec(); + get_hold(); + queue_wheel(); + +#endif + get_power(); +} + +static void get_button_from_gpio(void) +{ + /* Now reset buttons we're going to read from GPIO*/ + 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(2)) + int_btn |= BUTTON_UP; + if (!GPIOC_PIN(3)) + int_btn |= BUTTON_LEFT; + if (!GPIOC_PIN(4)) + int_btn |= BUTTON_SELECT; + if (!GPIOC_PIN(5)) + int_btn |= BUTTON_RIGHT; 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_gpio(); + get_button_from_dbob(); +} /* * Get button pressed from hardware */ int button_read_device(void) { -#ifdef BOOTLOADER - /* Read buttons directly in the bootloader */ - button_int(); -#else + get_buttons_from_hw(); +#ifndef BOOTLOADER /* light handling */ if (hold_button != hold_button_old) { @@ -136,7 +273,6 @@ backlight_hold_changed(hold_button); } #endif /* BOOTLOADER */ - - /* The int_btn variable is set in the button interrupt handler */ + /* set in get_button_from_dbob,get_button_from_gpio, get_rec, & get_power*/ return int_btn; }