00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <p24FJ64GA002.h>
00026
00027 #include "types.h"
00028
00029 #include "librtc.h"
00030 #include "libps2.h"
00031
00032
00033
00034
00035
00036 #define GPIO_PS2_CLK PORTBbits.RB8
00037 #define GPIO_PS2_DAT PORTBbits.RB9
00038
00039 #define PS2_KEY_BUFFER_SIZE 5
00040 #define PS2_CODE_BUFFER_SIZE 16
00041
00042
00043
00044
00045
00046 PS2_T ps2;
00047
00048
00049 static unsigned char key_buf[ PS2_KEY_BUFFER_SIZE ];
00050 static unsigned char key_stptr;
00051 static unsigned char key_enptr;
00052
00053
00054 static unsigned char code_buf[ PS2_CODE_BUFFER_SIZE ];
00055 static unsigned char code_stptr;
00056 static unsigned char code_enptr;
00057
00058
00059
00060 static const unsigned char key[] = {
00061 KEY_NOP,
00062 KEY_F9,
00063 KEY_NOP,
00064 KEY_F5,
00065 KEY_F3,
00066 KEY_F1,
00067 KEY_F2,
00068 KEY_F12,
00069 KEY_NOP,
00070 KEY_F10,
00071 KEY_F8,
00072 KEY_F6,
00073 KEY_F4,
00074 KEY_TAB,
00075 KEY_HANKAKU,
00076 KEY_NOP,
00077 KEY_NOP,
00078 KEY_ALT,
00079 KEY_RSHIFT,
00080 KEY_HIRAGANA,
00081 KEY_CTRL,
00082 'q',
00083 '1',
00084 KEY_NOP,
00085 KEY_NOP,
00086 KEY_NOP,
00087 'z',
00088 's',
00089 'a',
00090 'w',
00091 '2',
00092 KEY_LWIN,
00093 KEY_NOP,
00094 'c',
00095 'x',
00096 'd',
00097 'e',
00098 '4',
00099 '3',
00100 KEY_RWIN,
00101 KEY_NOP,
00102 ' ',
00103 'v',
00104 'f',
00105 't',
00106 'r',
00107 '5',
00108 KEY_APP,
00109 KEY_NOP,
00110 'n',
00111 'b',
00112 'h',
00113 'g',
00114 'y',
00115 '6',
00116 KEY_NOP,
00117 KEY_NOP,
00118 KEY_NOP,
00119 'm',
00120 'j',
00121 'u',
00122 '7',
00123 '8',
00124 KEY_NOP,
00125 KEY_NOP,
00126 ',',
00127 'k',
00128 'i',
00129 'o',
00130 '0',
00131 '9',
00132 KEY_NOP,
00133 KEY_NOP,
00134 '.',
00135 '/',
00136 'l',
00137 ';',
00138 'p',
00139 '-',
00140 KEY_NOP,
00141 KEY_NOP,
00142 '¥¥',
00143 ':',
00144 KEY_NOP,
00145 '@',
00146 '^',
00147 KEY_NOP,
00148 KEY_NOP,
00149 KEY_CAPS,
00150 KEY_RSHIFT,
00151 KEY_ENTER,
00152 '[',
00153 KEY_NOP,
00154 ']',
00155 KEY_NOP,
00156 KEY_NOP,
00157 KEY_NOP,
00158 KEY_NOP,
00159 KEY_NOP,
00160 KEY_NOP,
00161 KEY_HENKAN,
00162 KEY_NOP,
00163 KEY_BACKSPACE,
00164 KEY_MUHENKAN,
00165 KEY_NOP,
00166 KEY_END,
00167 '¥¥',
00168 KEY_LEFT,
00169 KEY_HOME,
00170 KEY_NOP,
00171 KEY_NOP,
00172 KEY_NOP,
00173 KEY_INS,
00174 KEY_DEL,
00175 KEY_DOWN,
00176 KEY_NOP,
00177 KEY_RIGHT,
00178 KEY_UP,
00179 KEY_ESC,
00180 KEY_NUM,
00181 KEY_F11,
00182 KEY_NOP,
00183 KEY_PAGEDOWN,
00184 KEY_NOP,
00185 KEY_PRINT,
00186 KEY_PAGEUP,
00187 KEY_SCROLL,
00188 KEY_NOP,
00189 KEY_NOP,
00190 KEY_NOP,
00191 KEY_NOP,
00192 KEY_F7,
00193 };
00194
00195
00196
00197
00198
00199 static BOOL PS2_key_put(unsigned char buf);
00200 static void PS2_code_buf_clear(void);
00201 static BOOL PS2_code_put(unsigned char buf);
00202 static unsigned char PS2_code_get(void);
00203 static unsigned char PS2_code_get_buf(void);
00204 static BOOL PS2_code_get_key(void);
00205
00206
00207
00208
00209
00210
00211
00212 static unsigned char reverse (unsigned char buf)
00213 {
00214 return(
00215 ((buf & 0b00000001) << 7)
00216 + ((buf & 0b00000010) << 5)
00217 + ((buf & 0b00000100) << 3)
00218 + ((buf & 0b00001000) << 1)
00219 + ((buf & 0b00010000) >> 1)
00220 + ((buf & 0b00100000) >> 3)
00221 + ((buf & 0b01000000) >> 5)
00222 + ((buf & 0b10000000) >> 7)
00223 );
00224
00225 }
00226
00227 void __attribute__((interrupt, auto_psv)) _CNInterrupt(void)
00228 {
00229 static unsigned int last = 0x0000;
00230 static unsigned int buf = 0x0000;
00231 static unsigned char count = 0x00;
00232
00233 IFS1bits.CNIF = 0;
00234
00235
00236 if(GPIO_PS2_CLK == 0){
00237
00238
00239 if(RTC_get_ticks(last, rtc.tick) > 5){
00240 buf = 0x0000;
00241 count = 0x00;
00242 }
00243 last = rtc.tick;
00244
00245
00246 buf = (buf << 1) + GPIO_PS2_DAT;
00247 count++;
00248
00249
00250 if(count > 10){
00251 PS2_code_put(reverse(buf >> 2));
00252 buf = 0x0000;
00253 count = 0x00;
00254 }
00255
00256 } else {
00257
00258 }
00259
00260
00261 return;
00262 }
00263
00264
00265
00266
00267
00268 void PS2_init(void)
00269 {
00270 PS2_key_buf_clear();
00271 PS2_code_buf_clear();
00272
00273 ps2.wait = 3000;
00274
00275 CNEN2bits.CN22IE = 1;
00276 IPC4bits.CNIP = 5;
00277 IEC1bits.CNIE = 1;
00278 IFS1bits.CNIF = 0;
00279
00280 return;
00281 }
00282
00283
00284
00285
00286
00287 BOOL PS2_main(void)
00288 {
00289
00290 PS2_code_get_key();
00291 return(TRUE);
00292 }
00293
00294
00295
00296
00297 void PS2_key_buf_clear(void)
00298 {
00299 key_stptr = 0x00;
00300 key_enptr = 0x00;
00301
00302 PS2_code_buf_clear();
00303
00304 return;
00305 }
00306
00307
00308
00309
00310
00311 static void PS2_code_buf_clear(void)
00312 {
00313 code_stptr = 0x00;
00314 code_enptr = 0x00;
00315 return;
00316 }
00317
00318
00319
00320
00321
00322 static BOOL PS2_key_put(unsigned char buf)
00323 {
00324 unsigned char nxptr;
00325
00326 nxptr = key_enptr + 1;
00327 if(nxptr >= PS2_KEY_BUFFER_SIZE) nxptr = 0;
00328
00329
00330 if(key_stptr == nxptr){
00331 return(FALSE);
00332 }
00333
00334 key_buf[ key_enptr ] = buf;
00335 key_enptr = nxptr;
00336
00337 return(TRUE);
00338 }
00339
00340
00341
00342
00343
00344 static BOOL PS2_code_put(unsigned char buf)
00345 {
00346 unsigned char nxptr;
00347
00348 nxptr = code_enptr + 1;
00349 if(nxptr >= PS2_CODE_BUFFER_SIZE) nxptr = 0;
00350
00351
00352 if(code_stptr == nxptr){
00353 return(FALSE);
00354 }
00355
00356 code_buf[ code_enptr ] = buf;
00357 code_enptr = nxptr;
00358
00359 return(TRUE);
00360 }
00361
00362
00363
00364
00365
00366 unsigned char PS2_key_get(void)
00367 {
00368 unsigned char buf, nxptr;
00369
00370
00371 if(key_stptr == key_enptr){
00372 return(0x00);
00373 }
00374
00375 buf = key_buf[ key_stptr ];
00376
00377 nxptr = key_stptr + 1;
00378 if(nxptr >= PS2_KEY_BUFFER_SIZE) nxptr = 0;
00379 key_stptr = nxptr;
00380
00381 return(buf);
00382 }
00383
00384
00385 unsigned char PS2_key_check(void)
00386 {
00387
00388 if(key_stptr == key_enptr){
00389 return(0x00);
00390 }
00391
00392 return(key_buf[ key_stptr ]);
00393 }
00394
00395
00396
00397
00398
00399 static unsigned char PS2_code_get(void)
00400 {
00401 unsigned char buf, nxptr;
00402
00403
00404 if(code_stptr == code_enptr){
00405 return(0x00);
00406 }
00407
00408 buf = code_buf[ code_stptr ];
00409
00410 nxptr = code_stptr + 1;
00411 if(nxptr >= PS2_CODE_BUFFER_SIZE) nxptr = 0;
00412 code_stptr = nxptr;
00413
00414 return(buf);
00415 }
00416
00417
00418
00419
00420
00421 unsigned char PS2_code_get_buf(void)
00422 {
00423 if(code_stptr >= code_enptr) return(code_stptr - code_enptr);
00424 else return(code_stptr + code_enptr);
00425 }
00426
00427
00428
00429
00430
00431
00432
00433
00434 static BOOL PS2_code_get_key(void)
00435 {
00436 unsigned char buf;
00437 static unsigned char last = 0x00;
00438 static unsigned int tick = 0x0000;
00439
00440 while((buf = PS2_code_get()) != 0x00){
00441 if(RTC_get_ticks(tick, rtc.tick) > ps2.wait){
00442 last = 0x00;
00443 }
00444
00445
00446 if(buf != last && buf < sizeof(key)){
00447 if(key[buf] != KEY_NOP){
00448 if(!PS2_key_put(key[buf])){
00449 PS2_key_buf_clear();
00450 PS2_key_put(key[buf]);
00451 }
00452 last = buf;
00453 tick = rtc.tick;
00454 }
00455 }
00456 }
00457
00458 return(TRUE);
00459 }