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

Revision 1031, 14.0 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        long hackkey;
9        long canonkey;
10} KeyMap;
11
12volatile long *mmio1 = (void*)0xc0220204;
13
14static long kbd_new_state[3];
15static long kbd_prev_state[3];
16static long kbd_mod_state;
17static KeyMap keymap[];
18static long last_kbd_key = 0;
19static int usb_power=0;
20static int remote_key, remote_count;
21static int shoot_counter=0;
22#define DELAY_TIMEOUT 10000
23
24#define NEW_SS (0x2000)
25#define SD_READONLY_FLAG (0x20000)
26
27#define USB_MASK 0x40
28#define USB_REG 1
29
30#ifndef MALLOCD_STACK
31static char kbd_stack[NEW_SS];
32#endif
33
34long __attribute__((naked)) wrap_kbd_p1_f() ;
35
36long get_mmio(void)
37{
38long x = (long)*mmio1; 
39return x;       
40}
41
42void wait_until_remote_button_is_released(void)
43{
44long x;
45int count1;
46int count2;
47int tick,tick2,tick3;
48int nSW;
49int prev_usb_power,cur_usb_power;
50
51static int nMode;
52
53asm volatile ("STMFD SP!, {R0-R11,LR}\n"); // store R0-R11 and LR in stack
54
55
56debug_led(1);
57tick=get_tick_count();
58tick2 = tick;
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)))
61
62  {
63if(conf.ricoh_ca1_mode && conf.remote_enable)
64{
65   nMode=0;
66        x=get_mmio();
67
68        if(x&USB_MASK) nMode=1;
69
70} // ricoh_ca1_mode
71
72
73if(conf.ricoh_ca1_mode && conf.remote_enable)
74    {
75        if(shooting_get_drive_mode()==1 && state_shooting_progress == SHOOTING_PROGRESS_PROCESSING){                    //continuous-shooting mode
76                if(conf.bracket_type>2){                          // alternating
77                        if(shoot_counter<2) shutter_int=3;
78                        shoot_counter--;
79                        }
80                else{                                              // lighter/darker
81                        prev_usb_power=0;
82                        nSW = 0;
83                        do
84                                {     
85                                x=get_mmio();
86                                cur_usb_power = x&USB_MASK;
87                                if(cur_usb_power)
88                            {
89                                        if(!prev_usb_power)
90                                   {
91                                                 tick2 = get_tick_count();
92                                                 prev_usb_power=cur_usb_power;
93                                                }
94                                           else
95                                    {
96                                                 if((int)get_tick_count()-tick2>1000) {debug_led(0);}
97                                                }
98                                        }
99                                else{
100                                        if(prev_usb_power)
101                                {
102                                                tick3 = (int)get_tick_count()-tick2;
103                                                if(nSW==10)
104                                         {
105                                                         if(tick3>50) shutter_int=1;
106                                                         nSW=20;
107                                                        }
108                                                if(nSW==0 && tick3>0)
109                                          {
110                                                         if(tick3<50)
111                                             {
112                                                            nSW=10;
113                                                           }
114                                                       else
115                                             {
116                                                              if(tick3>1000) shutter_int=1;
117                                                                nSW=20;
118                                                            }
119                                                         }
120                                                      prev_usb_power=cur_usb_power;
121                                                }
122                                          } // else
123
124                                           if((int)get_tick_count()-tick >= DELAY_TIMEOUT) {nSW=20;shutter_int=2;}
125                                      }
126
127                                while(nSW<20);
128                         }    // lighter/darker
129
130                  }             //continuous-shooting mode
131 
132                 else{          // normal mode
133                        shoot_counter=0;
134                        if(conf.bracket_type>2)
135                       {
136                                shoot_counter=(conf.bracket_type-2)*2;
137                                }
138           do
139             x=get_mmio();
140
141        while(((!(x&USB_MASK) && (nMode==0)) || ((x&USB_MASK) && (nMode==1))) &&  ((int)get_tick_count()-tick < DELAY_TIMEOUT));
142
143}
144} // ricoh ca1 mode
145
146else
147{
148   do
149   x=get_mmio();
150   while((x&USB_MASK) &&  ((int)get_tick_count()-tick < DELAY_TIMEOUT));
151}
152
153
154if (conf.synch_delay_enable && conf.synch_delay_value>0)                                // if delay is switched on and greater than 0
155  {
156    for (count1=0;count1<conf.synch_delay_value+(conf.synch_delay_coarse_value*1000);count1++) // wait delay_value * 0.1ms
157    {
158      for (count2=0;count2<1400;count2++)            // delay approx. 0.1ms
159        {
160        }
161     }
162  }
163}
164debug_led(0);
165asm volatile ("LDMFD SP!, {R0-R11,LR}\n"); // restore R0-R11 and LR from stack
166}
167
168
169/*
170void wait_until_remote_button_is_released(void)
171{
172int count1;
173int count2;
174int tick,tick2,tick3;
175int nSW;
176int prev_usb_power,cur_usb_power;
177 // ------ add by Masuji SUTO (start) --------------
178    static int nMode;
179 // ------ add by Masuji SUTO (end)   --------------
180
181asm volatile ("STMFD SP!, {R0-R11,LR}\n"); // store R0-R11 and LR in stack
182
183
184debug_led(1);
185tick = get_tick_count();
186tick2 = tick;
187static long usb_physw[3];
188   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)))                                     
189// if (conf.synch_enable && conf.ricoh_ca1_mode && conf.remote_enable && (!shooting_get_drive_mode()|| ((shooting_get_drive_mode()==2) && state_shooting_progress != SHOOTING_PROGRESS_PROCESSING)))   // synch mode enable so wait for USB to disconnect
190  {
191
192// ------ add by Masuji SUTO (start) --------------
193        nMode=0;
194        usb_physw[1] = 0;                                             // makes sure USB bit is cleared.
195        _kbd_read_keys_r2(usb_physw);
196        if((usb_physw[1] & USB_MASK)==USB_MASK) nMode=1;
197// ------ add by Masuji SUTO (end)   --------------
198
199if(conf.ricoh_ca1_mode && conf.remote_enable)
200{
201        if(shooting_get_drive_mode()==1 && state_shooting_progress == SHOOTING_PROGRESS_PROCESSING){                    //continuous-shooting mode
202                if(conf.bracket_type>2){
203                        if(shoot_counter<2) shutter_int=3;
204                        shoot_counter--;
205                        }
206                else{
207                        prev_usb_power=0;
208                        nSW = 0;
209                        do
210                                {     
211                                usb_physw[1] = 0;                                             // makes sure USB bit is cleared.
212                                _kbd_read_keys_r2(usb_physw);
213                                cur_usb_power = (usb_physw[1] & USB_MASK)==USB_MASK;
214                                if(cur_usb_power){
215                                        if(!prev_usb_power){
216                                                tick2 = get_tick_count();
217                                                prev_usb_power=cur_usb_power;
218                                                }
219                                        else{
220                                                if((int)get_tick_count()-tick2>1000) {debug_led(0);}
221                                                }
222                                        }
223                                else{
224                                        if(prev_usb_power){
225                                                tick3 = (int)get_tick_count()-tick2;
226                                                if(nSW==10) {
227                                                        if(tick3>50) shutter_int=1;
228                                                        nSW=20;
229                                                        }
230                                                if(nSW==0 && tick3>0) {
231                                                        if(tick3<50) {
232                                                        nSW=10;
233                                                        }
234                                                else{
235                                                        if(tick3>1000) shutter_int=1;
236                                                                nSW=20;
237                                                        }
238                                                }
239                                                prev_usb_power=cur_usb_power;
240                                                }
241                                        }
242                                if((int)get_tick_count()-tick >= DELAY_TIMEOUT) {nSW=20;shutter_int=2;}
243                                }
244                         while(nSW<20);
245                         }
246                }               //continuous-shooting mode
247                else{           // normal mode
248                        shoot_counter=0;
249                        if(conf.bracket_type>2){
250                                shoot_counter=(conf.bracket_type-2)*2;
251                                }
252   do
253         {     
254            usb_physw[1] = 0;                                             // makes sure USB bit is cleared.
255           _kbd_read_keys_r2(usb_physw);
256           }
257 //  while(((usb_physw[1] & USB_MASK)==USB_MASK) && ((int)get_tick_count()-tick < DELAY_TIMEOUT));
258
259// ------ modif by Masuji SUTO (start) --------------
260        while(((((usb_physw[1] & USB_MASK)!=USB_MASK) && (nMode==0)) || (((usb_physw[1] & USB_MASK)==USB_MASK) && (nMode==1))) && ((int)get_tick_count()-tick < DELAY_TIMEOUT));
261// ------ modif by Masuji SUTO (end)   --------------
262        }
263 }
264
265else
266   {
267
268      do
269          {
270            usb_physw[1] = 0;                                             // makes sure USB bit is cleared.
271           _kbd_read_keys_r2(usb_physw);
272             
273           }
274        while((usb_physw[1]&USB_MASK) &&  ((int)get_tick_count()-tick < DELAY_TIMEOUT));
275    }
276
277  }
278
279if (conf.synch_delay_enable && conf.synch_delay_value>0)                                // if delay is switched on and greater than 0
280  {
281    for (count1=0;count1<conf.synch_delay_value+(conf.synch_delay_coarse_value*1000);count1++) // wait delay_value * 0.1ms
282    {
283      for (count2=0;count2<1400;count2++)            // delay approx. 0.1ms
284        {
285        }
286     }
287  }
288
289debug_led(0);
290
291
292asm volatile ("LDMFD SP!, {R0-R11,LR}\n"); // restore R0-R11 and LR from stack
293}
294*/
295
296static void __attribute__((noinline)) mykbd_task_proceed()
297{
298    while (physw_run){
299        _SleepTask(10);
300
301        if (wrap_kbd_p1_f() == 1){ // autorepeat ?
302            _kbd_p2_f();
303        }
304    }
305}
306
307void __attribute__((naked,noinline)) mykbd_task()
308{
309    /* WARNING
310     * Stack pointer manipulation performed here!
311     * This means (but not limited to):
312     *  function arguments destroyed;
313     *  function CAN NOT return properly;
314     *  MUST NOT call or use stack variables before stack
315     *  is setup properly;
316     *
317     */
318
319    register int i;
320    register long *newstack;
321
322#ifndef MALLOCD_STACK
323    newstack = (void*)kbd_stack;
324#else
325    newstack = malloc(NEW_SS);
326#endif
327
328    for (i=0;i<NEW_SS/4;i++)
329        newstack[i]=0xdededede;
330
331    asm volatile (
332        "MOV    SP, %0"
333        :: "r"(((char*)newstack)+NEW_SS)
334        : "memory"
335    );
336
337    mykbd_task_proceed();
338
339    /* function can be modified to restore SP here...
340     */
341
342    _ExitTask();
343}
344
345
346long __attribute__((naked,noinline)) wrap_kbd_p1_f()
347{
348
349    asm volatile(
350                "STMFD   SP!, {R4-R7,LR}\n"
351                "SUB     SP, SP, #0xC\n"
352                "BL      my_kbd_read_keys\n"
353                "B       _kbd_p1_f_cont\n"
354    );
355    return 0; // shut up the compiler
356}
357
358
359void my_kbd_read_keys()
360{
361    kbd_prev_state[0] = kbd_new_state[0];
362    kbd_prev_state[1] = kbd_new_state[1];
363    kbd_prev_state[2] = kbd_new_state[2];
364
365    _kbd_pwr_on();
366
367    kbd_fetch_data(kbd_new_state);
368
369#if 0
370    if ((new_state[2] & 0x00001000 /* print button */) == 0 )
371        started();
372    else
373        finished();
374#endif
375
376
377    if (kbd_process() == 0){
378        // leave it alone...
379        physw_status[0] = kbd_new_state[0];
380        physw_status[1] = kbd_new_state[1];
381        physw_status[2] = kbd_new_state[2];
382#if 0
383        kbd_mod_state = kbd_new_state[2];
384#endif
385    } else {
386        // override keys
387#if 0
388        physw_status[2] = kbd_mod_state;
389#else
390        physw_status[0] = kbd_new_state[0];
391        physw_status[1] = kbd_new_state[1];
392        physw_status[2] = (kbd_new_state[2] & (~0xaffe)) |
393                          (kbd_mod_state & 0xaffe);
394#endif
395#if defined(USB_MASK) && defined(USB_REG)
396remote_key = (kbd_new_state[USB_REG] & USB_MASK)==USB_MASK;
397
398                        if (remote_key)
399                                remote_count += 1;
400                        else if (remote_count) {
401                                usb_power = remote_count;
402                                remote_count = 0;
403                        }
404                if (conf.remote_enable) {
405        physw_status[USB_REG] = kbd_new_state[USB_REG] & ~USB_MASK;
406                }
407#endif
408    }
409
410    _kbd_read_keys_r2(physw_status);
411    physw_status[2] = physw_status[2] & ~SD_READONLY_FLAG;
412
413    _kbd_pwr_off();
414
415}
416int get_usb_power(int edge)
417{
418        int x;
419
420        if (edge) return remote_key;
421        x = usb_power;
422        usb_power = 0;
423        return x;
424}
425/****************/
426
427
428void kbd_key_press(long key)
429{
430    int i;
431    for (i=0;keymap[i].hackkey;i++){
432        if (keymap[i].hackkey == key){
433            kbd_mod_state &= ~keymap[i].canonkey;
434            return;
435        }
436    }
437}
438
439void kbd_key_release(long key)
440{
441    int i;
442    for (i=0;keymap[i].hackkey;i++){
443        if (keymap[i].hackkey == key){
444            kbd_mod_state |= keymap[i].canonkey;
445            return;
446        }
447    }
448}
449
450void kbd_key_release_all()
451{
452    kbd_mod_state |= 0xaffe;
453}
454
455long kbd_is_key_pressed(long key)
456{
457    int i;
458    for (i=0;keymap[i].hackkey;i++){
459        if (keymap[i].hackkey == key){
460            return ((kbd_new_state[2] & keymap[i].canonkey) == 0) ? 1:0;
461        }
462    }
463    return 0;
464}
465
466long kbd_is_key_clicked(long key)
467{
468    int i;
469    for (i=0;keymap[i].hackkey;i++){
470        if (keymap[i].hackkey == key){
471            return ((kbd_prev_state[2] & keymap[i].canonkey) != 0) &&
472                    ((kbd_new_state[2] & keymap[i].canonkey) == 0);
473        }
474    }
475    return 0;
476}
477
478long kbd_get_pressed_key()
479{
480    int i;
481    for (i=0;keymap[i].hackkey;i++){
482        if ((kbd_new_state[2] & keymap[i].canonkey) == 0){
483            return keymap[i].hackkey;
484        }
485    }
486    return 0;
487}
488
489long kbd_get_clicked_key()
490{
491    int i;
492    for (i=0;keymap[i].hackkey;i++){
493        if (((kbd_prev_state[2] & keymap[i].canonkey) != 0) &&
494            ((kbd_new_state[2] & keymap[i].canonkey) == 0)){
495            return keymap[i].hackkey;
496        }
497    }
498    return 0;
499}
500
501void kbd_reset_autoclicked_key() {
502    last_kbd_key = 0;
503}
504
505long kbd_get_autoclicked_key() {
506    static long last_kbd_time = 0, press_count = 0;
507    register long key, t;
508
509    key=kbd_get_clicked_key();
510    if (key) {
511        last_kbd_key = key;
512        press_count = 0;
513        last_kbd_time = get_tick_count();
514        return key;
515    } else {
516        if (last_kbd_key && kbd_is_key_pressed(last_kbd_key)) {
517            t = get_tick_count();
518            if (t-last_kbd_time>((press_count)?175:500)) {
519                ++press_count;
520                last_kbd_time = t;
521                return last_kbd_key;
522            } else {
523                return 0;
524            }
525        } else {
526            last_kbd_key = 0;
527            return 0;
528        }
529    }
530}
531
532long kbd_use_zoom_as_mf() {
533    static long v;
534    static long zoom_key_pressed = 0;
535
536    if (kbd_is_key_pressed(KEY_ZOOM_IN) && (mode_get()&MODE_MASK) == MODE_REC) {
537        get_property_case(PROPCASE_FOCUS_MODE, &v, 4);
538        if (v) {
539            kbd_key_release_all();
540            kbd_key_press(KEY_RIGHT);
541            zoom_key_pressed = KEY_ZOOM_IN;
542            return 1;
543        }
544    } else {
545        if (zoom_key_pressed==KEY_ZOOM_IN) {
546            kbd_key_release(KEY_RIGHT);
547            zoom_key_pressed = 0;
548            return 1;
549        }
550    }
551    if (kbd_is_key_pressed(KEY_ZOOM_OUT) && (mode_get()&MODE_MASK) == MODE_REC) {
552        get_property_case(PROPCASE_FOCUS_MODE, &v, 4);
553        if (v) {
554            kbd_key_release_all();
555            kbd_key_press(KEY_LEFT);
556            zoom_key_pressed = KEY_ZOOM_OUT;
557            return 1;
558        }
559    } else {
560        if (zoom_key_pressed==KEY_ZOOM_OUT) {
561            kbd_key_release(KEY_LEFT);
562            zoom_key_pressed = 0;
563            return 1;
564        }
565    }
566    return 0;
567}
568
569
570
571
572
573static KeyMap keymap[] = {
574    /* tiny bug: key order matters. see kbd_get_pressed_key()
575     * for example
576     */
577        {KEY_UP      , 0x00000020 },
578        {KEY_DOWN    , 0x00000040 },
579        {KEY_LEFT    , 0x00000080 },
580        {KEY_RIGHT   , 0x00000100 },
581        {KEY_SET     , 0x00000200 },
582        {KEY_SHOOT_HALF, 0x00000002 },
583        {KEY_SHOOT_FULL, 0x00000004 },
584        {KEY_ZOOM_IN , 0x00000008 },
585        {KEY_ZOOM_OUT, 0x00000010 },
586        {KEY_MENU    , 0x00000400 },
587        {KEY_DISPLAY , 0x00000800 },
588        {KEY_PRINT   , 0x00002000 },
589        { 0, 0 }
590};
591
592void kbd_fetch_data(long *dst)
593{
594    volatile long *mmio1 = (void*)0xc0220204;
595    volatile long *mmio2 = (void*)0xc0220208;
596
597    dst[0] = 0;
598    dst[1] = *mmio1;
599    dst[2] = *mmio2 & 0xffff;
600}
601
Note: See TracBrowser for help on using the repository browser.