source: trunk/platform/a570/kbd.c @ 1031

Revision 1031, 11.3 KB checked in by pixeldoc2000, 2 years ago (diff)

typo fixed in kbd.c (nomal -> normal)

  • Property svn:eol-style set to native
Line 
1#include "lolevel.h"
2#include "platform.h"
3#include "conf.h"
4#include "core.h"
5#include "keyboard.h"
6
7typedef struct {
8        short grp;
9        long hackkey;
10        long canonkey;
11} KeyMap;
12
13
14static long kbd_new_state[3];
15static long kbd_prev_state[3];
16static long kbd_mod_state[3];
17static KeyMap keymap[];
18static long last_kbd_key = 0;
19static long alt_mode_key_mask = 0x00000800;
20static int usb_power=0;
21static int remote_key, remote_count;
22static int shoot_counter=0;
23#define DELAY_TIMEOUT 10000
24#define USB_MASK (0x40000)
25#define USB_REG 2
26
27
28#define KEYS_MASK0 (0x00000000)
29#define KEYS_MASK1 (0xc0800000)
30#define KEYS_MASK2 (0x0ffc)
31
32#define NEW_SS (0x2000)
33#define SD_READONLY_FLAG (0x20000)
34
35#ifndef MALLOCD_STACK
36static char kbd_stack[NEW_SS];
37#endif
38
39long __attribute__((naked)) wrap_kbd_p1_f() ;
40
41void wait_until_remote_button_is_released(void)
42{
43int count1;
44int count2;
45int tick,tick2,tick3;
46int nSW;
47int prev_usb_power,cur_usb_power;
48
49 // ------ add by Masuji SUTO (start) --------------
50    static int nMode;
51 // ------ add by Masuji SUTO (end)   --------------
52
53asm volatile ("STMFD SP!, {R0-R11,LR}\n"); // store R0-R11 and LR in stack
54
55debug_led(1);
56tick = get_tick_count();
57tick2 = tick;
58static long usb_physw[3];
59                                       
60  if (conf.synch_enable && conf.ricoh_ca1_mode && conf.remote_enable && (!shooting_get_drive_mode()|| (shooting_get_drive_mode()==1) || ((shooting_get_drive_mode()==2) && state_shooting_progress != SHOOTING_PROGRESS_PROCESSING)))                                       // synch mode enable so wait for USB to disconnect
61  {
62// ------ add by Masuji SUTO (start) --------------
63        nMode=0;
64        usb_physw[2] = 0;                                             // makes sure USB bit is cleared.
65        _kbd_read_keys_r2(usb_physw);
66        if((usb_physw[2] & USB_MASK)==USB_MASK) nMode=1;
67// ------ add by Masuji SUTO (end)   --------------
68if(conf.ricoh_ca1_mode && conf.remote_enable)                   //ricoh_ca1_mode
69{
70        if(shooting_get_drive_mode()==1 && state_shooting_progress == SHOOTING_PROGRESS_PROCESSING){                    //continuous-shooting mode
71                if(conf.bracket_type>2){
72                        if(shoot_counter<2) shutter_int=3;
73                        shoot_counter--;
74                        }
75                else{
76                        prev_usb_power=0;
77                        nSW = 0;
78                        do
79                                {     
80                                usb_physw[2] = 0;                                             // makes sure USB bit is cleared.
81                                _kbd_read_keys_r2(usb_physw);
82                                cur_usb_power = (usb_physw[2] & USB_MASK)==USB_MASK;
83                                if(cur_usb_power){
84                                        if(!prev_usb_power){
85                                                tick2 = get_tick_count();
86                                                prev_usb_power=cur_usb_power;
87                                                }
88                                        else{
89                                                if((int)get_tick_count()-tick2>1000) {debug_led(0);}
90                                                }
91                                        }
92                                else{
93                                        if(prev_usb_power){
94                                                tick3 = (int)get_tick_count()-tick2;
95                                                if(nSW==10) {
96                                                        if(tick3>50) shutter_int=1;
97                                                        nSW=20;
98                                                        }
99                                                if(nSW==0 && tick3>0) {
100                                                        if(tick3<50) {
101                                                        nSW=10;
102                                                        }
103                                                else{
104                                                        if(tick3>1000) shutter_int=1;
105                                                                nSW=20;
106                                                        }
107                                                }
108                                                prev_usb_power=cur_usb_power;
109                                                }
110                                        }
111                                if((int)get_tick_count()-tick >= DELAY_TIMEOUT) {nSW=20;shutter_int=2;}
112                                }
113                         while(nSW<20);
114                         }
115                }               //continuous-shooting mode
116                else{           // normal mode
117                        shoot_counter=0;
118                        if(conf.bracket_type>2){
119                                shoot_counter=(conf.bracket_type-2)*2;
120                                }
121                   do
122                         {     
123                            usb_physw[2] = 0;                                             // makes sure USB bit is cleared.
124                           _kbd_read_keys_r2(usb_physw);
125                           }
126                //   while(((usb_physw[2] & USB_MASK)==USB_MASK) && ((int)get_tick_count()-tick < DELAY_TIMEOUT));
127                // ------ modif by Masuji SUTO (start) --------------
128                        while(((((usb_physw[2] & USB_MASK)!=USB_MASK) && (nMode==0)) || (((usb_physw[2] & USB_MASK)==USB_MASK) && (nMode==1))) && ((int)get_tick_count()-tick < DELAY_TIMEOUT));
129                // ------ modif by Masuji SUTO (end)   --------------
130                }               // normal mode
131        }               //ricoh_ca1_mode
132else            //ricoh_ca1_mode disebale
133   {
134
135      do
136          {
137            usb_physw[2] = 0;                                             // makes sure USB bit is cleared.
138           _kbd_read_keys_r2(usb_physw);
139           
140           }
141        while((usb_physw[2]&USB_MASK) &&  ((int)get_tick_count()-tick < DELAY_TIMEOUT));
142    }
143
144
145  }
146
147if (conf.synch_delay_enable && conf.synch_delay_value>0)                                // if delay is switched on and greater than 0
148  {
149    for (count1=0;count1<conf.synch_delay_value+(conf.synch_delay_coarse_value*1000);count1++) // wait delay_value * 0.1ms
150    {
151      for (count2=0;count2<1400;count2++)            // delay approx. 0.1ms
152        {
153        }
154     }
155  }
156
157debug_led(0);
158asm volatile ("LDMFD SP!, {R0-R11,LR}\n"); // restore R0-R11 and LR from stack
159}
160
161static void __attribute__((noinline)) mykbd_task_proceed()
162{
163    while (physw_run){
164        _SleepTask(10);
165
166        if (wrap_kbd_p1_f() == 1){ // autorepeat ?
167            _kbd_p2_f();
168        }
169    }
170}
171
172void __attribute__((naked,noinline)) mykbd_task(long ua, long ub, long uc, long ud, long ue, long uf)
173{
174    /* WARNING
175     * Stack pointer manipulation performed here!
176     * This means (but not limited to):
177     *  function arguments destroyed;
178     *  function CAN NOT return properly;
179     *  MUST NOT call or use stack variables before stack
180     *  is setup properly;
181     *
182     */
183
184    register int i;
185    register long *newstack;
186
187#ifndef MALLOCD_STACK
188    newstack = (void*)kbd_stack;
189#else
190    newstack = malloc(NEW_SS);
191#endif
192
193    for (i=0;i<NEW_SS/4;i++)
194        newstack[i]=0xdededede;
195
196    asm volatile (
197        "MOV    SP, %0"
198        :: "r"(((char*)newstack)+NEW_SS)
199        : "memory"
200    );
201
202    mykbd_task_proceed();
203
204    /* function can be modified to restore SP here...
205     */
206
207    _ExitTask();
208}
209
210
211long __attribute__((naked,noinline)) wrap_kbd_p1_f()
212{
213
214    asm volatile(
215                "STMFD   SP!, {R4-R7,LR}\n"
216                "SUB     SP, SP, #0xC\n"
217                "BL      my_kbd_read_keys\n"
218                "B       _kbd_p1_f_cont\n"
219    );
220    return 0; // shut up the compiler
221}
222
223
224void my_kbd_read_keys()
225{
226    kbd_prev_state[0] = kbd_new_state[0];
227    kbd_prev_state[1] = kbd_new_state[1];
228    kbd_prev_state[2] = kbd_new_state[2];
229    _kbd_pwr_on();
230    kbd_fetch_data(kbd_new_state);
231
232    if (kbd_process() == 0){
233        // leave it alone...
234        physw_status[0] = kbd_new_state[0];
235        physw_status[1] = kbd_new_state[1];
236        physw_status[2] = kbd_new_state[2];
237        physw_status[1] |= alt_mode_key_mask;
238
239    } else {
240      // override keys
241        physw_status[0] = (kbd_new_state[0] & (~KEYS_MASK0)) |
242              (kbd_mod_state[0] & KEYS_MASK0);
243
244        physw_status[1] = (kbd_new_state[1] & (~KEYS_MASK1)) |
245              (kbd_mod_state[1] & KEYS_MASK1);
246
247        physw_status[2] = (kbd_new_state[2] & (~KEYS_MASK2)) |
248              (kbd_mod_state[2] & KEYS_MASK2);
249        }
250
251    _kbd_read_keys_r2(physw_status);
252
253                remote_key = (physw_status[2] & USB_MASK)==USB_MASK;
254                if (remote_key)
255                        remote_count += 1;
256                else if (remote_count) {
257                        usb_power = remote_count;
258                        remote_count = 0;
259                }
260       
261        if (conf.remote_enable) {
262                physw_status[2] = physw_status[2] & ~(SD_READONLY_FLAG | USB_MASK);
263        }
264        else
265                physw_status[2] = physw_status[2] & ~SD_READONLY_FLAG;
266
267
268    _kbd_pwr_off();
269
270}
271
272int get_usb_power(int edge)
273{
274        int x;
275
276        if (edge) return remote_key;
277        x = usb_power;
278        usb_power = 0;
279        return x;
280}
281
282void kbd_set_alt_mode_key_mask(long key)
283{
284    int i;
285    for (i=0; keymap[i].hackkey; ++i) {
286        if (keymap[i].hackkey == key) {
287            alt_mode_key_mask = keymap[i].canonkey;
288            return;
289        }
290    }
291}
292
293void kbd_key_press(long key)
294{
295    int i;
296    for (i=0;keymap[i].hackkey;i++){
297        if (keymap[i].hackkey == key){
298            kbd_mod_state[keymap[i].grp] &= ~keymap[i].canonkey;
299            return;
300        }
301    }
302}
303
304void kbd_key_release(long key)
305{
306    int i;
307    for (i=0;keymap[i].hackkey;i++){
308        if (keymap[i].hackkey == key){
309            kbd_mod_state[keymap[i].grp] |= keymap[i].canonkey;
310            return;
311        }
312    }
313}
314
315void kbd_key_release_all()
316{
317  kbd_mod_state[0] |= KEYS_MASK0;
318  kbd_mod_state[1] |= KEYS_MASK1;
319  kbd_mod_state[2] |= KEYS_MASK2;
320}
321
322long kbd_is_key_pressed(long key)
323{
324    int i;
325    for (i=0;keymap[i].hackkey;i++){
326        if (keymap[i].hackkey == key){
327            return ((kbd_new_state[keymap[i].grp] & keymap[i].canonkey) == 0) ? 1:0;
328        }
329    }
330    return 0;
331}
332
333long kbd_is_key_clicked(long key)
334{
335    int i;
336    for (i=0;keymap[i].hackkey;i++){
337        if (keymap[i].hackkey == key){
338            return ((kbd_prev_state[keymap[i].grp] & keymap[i].canonkey) != 0) &&
339                    ((kbd_new_state[keymap[i].grp] & keymap[i].canonkey) == 0);
340        }
341    }
342    return 0;
343}
344
345long kbd_get_pressed_key()
346{
347    int i;
348    for (i=0;keymap[i].hackkey;i++){
349        if ((kbd_new_state[keymap[i].grp] & keymap[i].canonkey) == 0){
350            return keymap[i].hackkey;
351        }
352    }
353    return 0;
354}
355
356long kbd_get_clicked_key()
357{
358    int i;
359    for (i=0;keymap[i].hackkey;i++){
360        if (((kbd_prev_state[keymap[i].grp] & keymap[i].canonkey) != 0) &&
361            ((kbd_new_state[keymap[i].grp] & keymap[i].canonkey) == 0)){
362            return keymap[i].hackkey;
363        }
364    }
365    return 0;
366}
367
368void kbd_reset_autoclicked_key() {
369    last_kbd_key = 0;
370}
371
372long kbd_get_autoclicked_key() {
373    static long last_kbd_time = 0, press_count = 0;
374    register long key, t;
375
376    key=kbd_get_clicked_key();
377    if (key) {
378        last_kbd_key = key;
379        press_count = 0;
380        last_kbd_time = get_tick_count();
381        return key;
382    } else {
383        if (last_kbd_key && kbd_is_key_pressed(last_kbd_key)) {
384            t = get_tick_count();
385            if (t-last_kbd_time>((press_count)?175:500)) {
386                ++press_count;
387                last_kbd_time = t;
388                return last_kbd_key;
389            } else {
390                return 0;
391            }
392        } else {
393            last_kbd_key = 0;
394            return 0;
395        }
396    }
397       
398       
399}
400
401long kbd_use_zoom_as_mf() {
402    static long v;
403    static long zoom_key_pressed = 0;
404
405    if (kbd_is_key_pressed(KEY_ZOOM_IN) && (mode_get()&MODE_MASK) == MODE_REC) {
406        get_property_case(PROPCASE_FOCUS_MODE, &v, 4);
407        if (v) {
408            kbd_key_release_all();
409            kbd_key_press(KEY_RIGHT);
410            zoom_key_pressed = KEY_ZOOM_IN;
411            return 1;
412        }
413    } else {
414        if (zoom_key_pressed==KEY_ZOOM_IN) {
415            kbd_key_release(KEY_RIGHT);
416            zoom_key_pressed = 0;
417            return 1;
418        }
419    }
420    if (kbd_is_key_pressed(KEY_ZOOM_OUT) && (mode_get()&MODE_MASK) == MODE_REC) {
421        get_property_case(PROPCASE_FOCUS_MODE, &v, 4);
422        if (v) {
423            kbd_key_release_all();
424            kbd_key_press(KEY_LEFT);
425            zoom_key_pressed = KEY_ZOOM_OUT;
426            return 1;
427        }
428    } else {
429        if (zoom_key_pressed==KEY_ZOOM_OUT) {
430            kbd_key_release(KEY_LEFT);
431            zoom_key_pressed = 0;
432            return 1;
433        }
434    }
435    return 0;
436}
437
438
439
440static KeyMap keymap[] = {
441    /* tiny bug: key order matters. see kbd_get_pressed_key()
442     * for example
443     */
444        {2, KEY_UP      , 0x00000010 },
445        {2, KEY_DOWN    , 0x00000020 },
446        {2, KEY_LEFT    , 0x00000080 },
447        {2, KEY_RIGHT   , 0x00000040 },
448        {2, KEY_SET     , 0x00000100 },
449        {1, KEY_SHOOT_FULL, 0xC0000000 }, // note 3 here!
450        {1, KEY_SHOOT_HALF, 0x40000000 },
451        {2, KEY_ZOOM_IN , 0x00000004 },
452        {2, KEY_ZOOM_OUT        , 0x00000008 },
453        {2, KEY_MENU    , 0x00000200 },
454        {2, KEY_DISPLAY , 0x000000400 },
455        {2, KEY_PRINT   , 0x00000800},
456        {1, KEY_ERASE   , 0x00800000 },
457      //  {2, KEY_DUMMY , 0x00000800 },
458        { 0, 0 }
459};
460
461
462
463 void kbd_fetch_data(long *dst)
464{
465    volatile long *mmio0 = (void*)0xc0220200;
466    volatile long *mmio1 = (void*)0xc0220204;
467    volatile long *mmio2 = (void*)0xc0220208;
468
469    dst[0] = *mmio0;
470    dst[1] = *mmio1;
471    dst[2] = *mmio2 & 0xffff;
472}
473
Note: See TracBrowser for help on using the repository browser.