e200v2: make buttons and scrollwheel work. Still ugly as we need to disable interrupts while updating the lcd.

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@20019 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Dominik Wenger 2009-02-16 19:50:52 +00:00
parent aecc224eca
commit 2871963bee
3 changed files with 237 additions and 70 deletions

View File

@ -32,7 +32,6 @@
#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;
@ -46,13 +45,12 @@ static long next_backlight_on = 0;
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;
extern void lcd_button_support(void);
void button_init_device(void)
{
}
bool button_hold(void)
@ -60,64 +58,153 @@ bool button_hold(void)
return _button_hold();
}
/* clickwheel */
#ifndef BOOTLOADER
void clickwheel_int(void)
void clickwheel(unsigned int wheel_value)
{
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
*/
/* 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)
{
int repeat = 1; /* assume repeat */
unsigned long usec = TIMER1_VALUE; /* WAG!!! and it works!!*/
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;
}
old_wheel_value = wheel_value;
}
#endif /* BOOTLOADER */
/* device buttons */
void button_int(void)
int read_dbop(void)
{
int dir_save_b = 0;
int afsel_save_b = 0;
int dir_save_c = 0;
int afsel_save_c = 0;
/*write a red pixel */
lcd_button_support();
int_btn = BUTTON_NONE;
/* Set up dbop for input */
while (!(DBOP_STAT & (1<<10))); /* Wait for fifo to empty */
DBOP_CTRL |= (1<<19);
DBOP_CTRL &= ~(1<<16); /* disable output */
DBOP_TIMPOL_01 = 0xe167e167; //11100001011001111110000101100111
DBOP_TIMPOL_23 = 0xe167006e; //11100001011001110000000001101110
DBOP_CTRL |= (1<<15); /* start read */
while (!(DBOP_STAT & (1<<16))); /* wait for valid data */
int delay = 50;
while(delay--); /* small delay to set up read */
/* 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;
int ret = DBOP_DIN; /* now read dbop & store info*/
GPIOB_DIR = 0;
GPIOB_AFSEL = 0;
GPIOC_DIR = 0;
GPIOC_AFSEL = 0;
DBOP_TIMPOL_01 = 0x6e167;
DBOP_TIMPOL_23 = 0xa167e06f;
DBOP_CTRL |= (1<<16);
DBOP_CTRL &= ~(1<<19);
/* 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;
/* direct GPIO connections */
if (GPIOB_PIN(4))
int_btn |= BUTTON_POWER;
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;
return ret;
}
/*
@ -125,10 +212,64 @@ void button_int(void)
*/
int button_read_device(void)
{
#ifdef BOOTLOADER
/* Read buttons directly in the bootloader */
button_int();
#else
int btn = BUTTON_NONE;
/* read buttons from dbop */
int dbop = read_dbop();
/* hold button */
if(dbop & (1<<12))
{
hold_button = true;
return btn;
}
else
{
hold_button = false;
}
if (dbop & (1<<8))
btn |= BUTTON_POWER;
if (!(dbop & (1<<15)))
btn |= BUTTON_REC;
/* handle wheel */
int wheel_value = dbop & (1<<13|1<<14);
wheel_value >>= 13;
clickwheel(wheel_value);
/* 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);
int delay = 50; /* small delay needed to read buttons correctly */
while(delay--);
/* direct GPIO connections */
if (!GPIOC_PIN(2))
btn |= BUTTON_UP;
if (!GPIOC_PIN(3))
btn |= BUTTON_LEFT;
if (!GPIOC_PIN(4))
btn |= BUTTON_SELECT;
if (!GPIOC_PIN(5))
btn |= BUTTON_RIGHT;
if (!GPIOC_PIN(6))
btn |= BUTTON_DOWN;
/* return to settings needed for lcd */
GPIOC_DIR |= (1<<2|1<<3|1<<4|1<<5|1<<6);
GPIOC_AFSEL |= (1<<2|1<<3|1<<4|1<<5|1<<6);
#ifndef BOOTLOADER
/* light handling */
if (hold_button != hold_button_old)
{
@ -136,7 +277,6 @@ int button_read_device(void)
backlight_hold_changed(hold_button);
}
#endif /* BOOTLOADER */
/* The int_btn variable is set in the button interrupt handler */
return int_btn;
return btn;
}

View File

@ -30,10 +30,6 @@
bool button_hold(void);
void button_init_device(void);
int button_read_device(void);
#ifndef BOOTLOADER
void clickwheel_int(void);
#endif
void button_int(void);
/* Sandisk Sansa E200 button codes */

View File

@ -99,13 +99,13 @@ static void ams3525_dbop_init(void)
DBOP_TIMPOL_01 = 0xe167e167;
DBOP_TIMPOL_23 = 0xe167006e;
DBOP_CTRL = 0x41008;
DBOP_CTRL = (1<<18)|(1<<12)|(8<<0); /* short count, 16bit write, read-timing =8 */
GPIOB_AFSEL = 0xfc;
GPIOC_AFSEL = 0xff;
DBOP_TIMPOL_23 = 0x6000e;
DBOP_CTRL = 0x51008;
DBOP_CTRL = (1<<18)|(1<<16)|(1<<12)|(8<<0);/* short count,write enable, 16bit write, read-timing =8 */
DBOP_TIMPOL_01 = 0x6e167;
DBOP_TIMPOL_23 = 0xa167e06f;
@ -365,7 +365,10 @@ void lcd_update(void)
{
if (!display_on)
return;
/* we must disable interrupts because buttondriver also writes to lcd */
disable_irq();
lcd_write_reg(R_ENTRY_MODE, R_ENTRY_MODE_HORZ);
/* Set start position and window */
@ -377,7 +380,8 @@ void lcd_update(void)
lcd_write_cmd(R_WRITE_DATA_2_GRAM);
lcd_write_data((unsigned short *)lcd_framebuffer, LCD_WIDTH*LCD_HEIGHT);
enable_irq();
} /* lcd_update */
@ -389,7 +393,7 @@ void lcd_update_rect(int x, int y, int width, int height)
if (!display_on)
return;
if (x + width > LCD_WIDTH)
width = LCD_WIDTH - x; /* Clip right */
if (x < 0)
@ -405,6 +409,9 @@ void lcd_update_rect(int x, int y, int width, int height)
if (y >= ymax)
return; /* nothing left to do */
/* we must disable interrupts because buttondriver also writes to lcd */
disable_irq();
lcd_write_reg(R_ENTRY_MODE, R_ENTRY_MODE_HORZ);
/* Set start position and window */
lcd_write_reg(R_HORIZ_RAM_ADDR_POS,
@ -423,4 +430,28 @@ void lcd_update_rect(int x, int y, int width, int height)
ptr += LCD_WIDTH;
}
while (++y < ymax);
enable_irq();
} /* lcd_update_rect */
/* writes one read pixel outside the visible area, needed for correct dbop reads */
void lcd_button_support(void)
{
int x=LCD_HEIGHT+1;
int y=LCD_WIDTH+1;
int width=1;
int height=1;
unsigned short data = (0xf<<12);
lcd_write_reg(R_ENTRY_MODE, R_ENTRY_MODE_HORZ);
/* Set start position and window */
lcd_write_reg(R_HORIZ_RAM_ADDR_POS,
((x + width-1) << 8) | x);
lcd_write_reg(R_VERT_RAM_ADDR_POS,
((y_offset + y + height - 1) << 8) | (y_offset + y));
lcd_write_reg(R_RAM_ADDR_SET, ((y + y_offset) << 8) | x);
lcd_write_cmd(R_WRITE_DATA_2_GRAM);
lcd_write_data(&data, width);
}