polling_PS2.c
上传用户:caisangzi8
上传日期:2013-10-25
资源大小:15756k
文件大小:9k
源码类别:

DVD

开发平台:

C/C++

  1. // polling PS/2 interface
  2. //created by JSLin  //20041022
  3. //well, basically, this file will be included when you defined SUPPORT_PS2 in cfg_xxxxxx.h
  4. //#ifdef  POLLING_DBG
  5. #define MONE_PS2_MOUSE
  6. //#endif
  7. #define X_Pos_Max  340
  8. #define Y_Pos_Max  230
  9. #ifndef SUPPORT_GAMEPAD_MOUSE
  10. #define SendInitCMD_M_0 0
  11. #define SendInitCMD_M_1 1
  12. #define InitCMD_M_End 2
  13. #define SendInitCMD_M_Fail 3
  14. #define Response_M_OK 1
  15. #define Response_M_Faile 0
  16. #define Enable_M_Read 1
  17. #define Disable_M_Read 0
  18. // PS/2 mouse command
  19. #define RESET_M 0xff
  20. #define WorkMod_M 0xEA //StreamMode 0xEA; RemoteMode 0xF0; WrapMode 0xEC
  21. #define Read_Dev_type 0xF2 //read mouse type, receiver 0xFA; standard mouse 0x00; other mouse ....
  22. #define SetSampl_M 0xF3 //set sample rate
  23. #define SamplRat_M 0x28 //10/sec 0x0A; 20/sec 0x14;40/sec 0x28;60/sec 0x3c;80/sec 0x50;100/sec 0x64;200/sec 0xC8
  24. #define Enable_M 0xF4 //enable mouse working
  25. #define MouseInitEnd 0x00
  26. #endif
  27. #ifdef SUPPORT_GAMEPAD_MOUSE
  28. extern int ReadGamePad(void);
  29. #else
  30. void checkMouseEvent(void);
  31. void resetMouse(void);
  32. #endif
  33. void polling_mouse(void);
  34. void updateMouseEvent(void);
  35. typedef struct
  36. {
  37.     BYTE    status;
  38.     BYTE    x;
  39.     BYTE    y;
  40. } MOUSE_EVENT;
  41. MOUSE_EVENT m;
  42. #ifdef SUPPORT_GAMEPAD_MOUSE
  43. BYTE GAMEPAD_DATA0;
  44. BYTE GAMEPAD0_dir;
  45. BYTE GAMEPAD0_btn;
  46. #else
  47. int iMouseState = SendInitCMD_M_0;
  48. BYTE *pMouseResponsebuf, *pMouseRecvPoint;
  49. BYTE  bRtsPoint = 0;
  50. BYTE bHostSendCmdCount = 0;
  51. BYTE bPS2_read_flag = Enable_M_Read;
  52. const BYTE bMouseInitCmd[] = {/*RESET_M,*/ WorkMod_M, SetSampl_M, SamplRat_M, Enable_M, MouseInitEnd};
  53. #endif
  54. #ifdef SUPPORT_GAMEPAD_MOUSE
  55. void polling_mouse(void)
  56. {
  57.     if(ReadGamePad()>0)
  58.     {
  59.         GAMEPAD_DATA0 = regs0->iop_data[4]&0xff;
  60.         GAMEPAD0_dir = GAMEPAD_DATA0&0xf;
  61.         GAMEPAD0_btn = GAMEPAD_DATA0>>4;
  62.         #ifdef MONE_PS2_MOUSE
  63.             //printf("iop_data[4]_L : dir[%x]   btn[%x]n", GAMEPAD0_dir, GAMEPAD0_btn);
  64.         #endif
  65.         updateMouseEvent();
  66.     }
  67. return;
  68. }
  69. void updateMouseEvent(void)
  70. {
  71. //mouse position
  72.     if((~GAMEPAD0_dir)&0x8)
  73.     {
  74.         mouse_pos_y -=1;
  75.     }
  76.     if((~GAMEPAD0_dir)&0x4)
  77.     {
  78.         mouse_pos_y +=1;
  79.     }
  80.     if((~GAMEPAD0_dir)&0x2)
  81.     {
  82.         mouse_pos_x -=1;
  83.     }
  84.     if((~GAMEPAD0_dir)&0x1)
  85.     {
  86.         mouse_pos_x +=1;
  87.     }
  88.     if(mouse_pos_x > X_Pos_Max) 
  89.         {mouse_pos_x = X_Pos_Max;}
  90.     if(mouse_pos_x < 0) 
  91.         {mouse_pos_x = 0;}
  92.     if(mouse_pos_y > Y_Pos_Max) 
  93.         {mouse_pos_y = Y_Pos_Max;}
  94.     if(mouse_pos_y < 0) 
  95.         {mouse_pos_y = 0;}
  96. //mouse button
  97.     if((~GAMEPAD0_btn)&0x8)
  98.     {
  99.         mouse_btn_status |=0x1;
  100.     }
  101.     if((~GAMEPAD0_btn)&0x4)
  102.     {
  103.         mouse_btn_status |=0x2;
  104.     }
  105.     if(GAMEPAD0_btn==0xf)
  106.     {
  107.         mouse_btn_status = 0x0;
  108.     }
  109. }
  110. #else
  111. void polling_mouse(void)
  112. {
  113.     BYTE bNextSend;
  114.     static BYTE bMouse_send_fail;
  115.     
  116.     pMouseResponsebuf = (BYTE *) (SDRAM_BASE_UNCACHED + (regs0->iopya*1024) + 0x302);
  117.     pMouseRecvPoint = (BYTE *) (SDRAM_BASE_UNCACHED + (regs0->iopya*1024) + 0x300);
  118.     switch(iMouseState)
  119.     {
  120.         case SendInitCMD_M_0:   //send command to mouse controller
  121.             if(bMouseInitCmd[bHostSendCmdCount] == MouseInitEnd)
  122.             {
  123.                 iMouseState = InitCMD_M_End;
  124.                 PS2_INIT_DONE = 0x1;
  125.             }
  126.             else
  127.             {
  128.                 #ifdef MONE_PS2_MOUSE
  129.                     printf("n  PS2: XXX  send MouseCmd=[%x] iop_data5=[%x]n",
  130.                             bMouseInitCmd[bHostSendCmdCount], regs0->iop_data[5]);
  131.                 #endif
  132.                 regs0->iop_data[5]=0x8000|bMouseInitCmd[bHostSendCmdCount]; //iop_data2    
  133.                 iMouseState = SendInitCMD_M_1;
  134.                 #ifdef MONE_PS2_MOUSE
  135.                     printf("n  PS2: send MouseCmd=[%x] iop_data5=[%x]n",
  136.                             bMouseInitCmd[bHostSendCmdCount], regs0->iop_data[5]);
  137.                 #endif
  138.             }
  139.             break;
  140.         case SendInitCMD_M_1:
  141.             delay(100);
  142.             #ifdef MONE_PS2_MOUSE
  143.                 printf("n  PS2: SendInitCMD_M_1 [%x] [%x] [%x] [%x]n", 
  144.                 bRtsPoint, *pMouseRecvPoint, pMouseResponsebuf[(*pMouseRecvPoint)-1], regs0->iop_data[5]);
  145.             #endif
  146.             
  147.             if((bRtsPoint == *pMouseRecvPoint) || (pMouseResponsebuf[0]!=0xFA))
  148.             {
  149.                 bMouse_send_fail++;
  150.                 if(bMouse_send_fail ==30)
  151.                 {
  152.                     iMouseState = SendInitCMD_M_0;
  153.                     bMouse_send_fail=0;
  154.                     iMouseState = SendInitCMD_M_Fail;
  155.                     regs0->iop_data[5] = 0x00;
  156.                     #ifdef MONE_PS2_MOUSE
  157.                         //printf("Mouse didn't connectn");
  158.                     #endif
  159.                 }
  160.                 break;
  161.             }
  162.             bMouse_send_fail = 0;
  163.             //check response from mouse controller
  164.             bNextSend = Response_M_Faile;
  165.             switch(pMouseResponsebuf[0])
  166.             {
  167.                 case 0xFA:  //ACK
  168.                     bHostSendCmdCount++;
  169.                     bNextSend = Response_M_OK;
  170.                     break;
  171.                     
  172.                 case 0xFE:  //resend
  173.                     bNextSend = Response_M_OK;
  174.                     break;
  175.                 default:
  176.                       break;
  177.             }
  178.             //send next command to mouse controller
  179.             if(bNextSend == Response_M_OK) 
  180.             {
  181.                 *pMouseRecvPoint = 0;
  182.                 pMouseResponsebuf[0] = 0;
  183.                 iMouseState = SendInitCMD_M_0;
  184.             }
  185.             break;
  186.         case InitCMD_M_End:
  187.                 #ifdef MONE_PS2_MOUSE
  188.                    // printf("n  InitCMD_M_End   n");
  189.                 #endif
  190.                 if(*pMouseRecvPoint)
  191.                 {
  192.                     #ifdef MONE_PS2_MOUSE   
  193.                         //printf("pMouseRecvPoint[%x]  address[%x]n", *pMouseRecvPoint, &(*pMouseRecvPoint));
  194.                     #endif                
  195.                     checkMouseEvent();//(*pMouseRecvPoint);
  196.                  }
  197.             break;
  198.         case SendInitCMD_M_Fail:
  199.         default:
  200.             break;
  201.     }
  202. }
  203. void checkMouseEvent(void)//(BYTE RX)
  204. {
  205.     BYTE bRtsTmp = bRtsPoint;
  206.     int ReceiverCount;
  207.     #ifdef MONE_PS2_MOUSE
  208.         //printf("a [%x] [%x]n", bRtsTmp, *pMouseRecvPoint);
  209.     #endif
  210.     while(1)
  211.     {
  212.         BYTE *m1 = (BYTE *)&m;
  213.         for (ReceiverCount=0; ReceiverCount<3; ReceiverCount++)
  214.         {
  215.             if((bRtsTmp == *pMouseRecvPoint) || (bRtsTmp >12)) 
  216.             {
  217.                 *pMouseRecvPoint = 0;
  218.                 break;
  219.             }
  220.             #ifdef MONE_PS2_MOUSE                    
  221.                 //printf("[%x] [%x]t",&(pMouseResponsebuf[bRtsTmp]), pMouseResponsebuf[bRtsTmp]);
  222.             #endif
  223.             *m1 = pMouseResponsebuf[bRtsTmp];
  224.             m1++;
  225.             bRtsTmp++;
  226.             bPS2_read_flag = Enable_M_Read;
  227.             
  228.         }
  229.         if(ReceiverCount == 3)
  230.         {
  231.             if((m.status&0x08) == 0x08) //valid mouse event
  232.             {
  233.                 updateMouseEvent();
  234.             }
  235.         }
  236.         else 
  237.         {        
  238.             break;
  239.         }
  240.     }
  241. }
  242. void updateMouseEvent(void)
  243. {
  244.     //update mouse position and button status
  245.     //X axle
  246.     if((m.status & 0x10) == 0x10)
  247.     {
  248.         if((((m.x)^0xff)|0x01) > 8) 
  249.             { mouse_pos_x -=8;}
  250.         else
  251.         { mouse_pos_x -= (((m.x)^0xff)|0x01);}
  252.     }
  253.     else
  254.     {
  255.         if(m.x > 8) 
  256.             {mouse_pos_x+=8;}
  257.         else
  258.             {mouse_pos_x += m.x;}
  259.     }
  260.     //Y axle
  261.     if((m.status & 0x20) == 0x20)
  262.     {
  263.         if((((m.y)^0xff)|0x01)>8)
  264.             {mouse_pos_y +=8;}
  265.         else
  266.             {mouse_pos_y += (((m.y)^0xff)|0x01);}
  267.     }
  268.     else
  269.     {
  270.         if(m.y>8) 
  271.             {mouse_pos_y -= 8;}
  272.         else
  273.             {mouse_pos_y -= m.y;}
  274.     }
  275.     if(mouse_pos_x > X_Pos_Max) 
  276.         {mouse_pos_x = X_Pos_Max;}
  277.     if(mouse_pos_x < 0) 
  278.         {mouse_pos_x = 0;}
  279.     if(mouse_pos_y > Y_Pos_Max) 
  280.         {mouse_pos_y = Y_Pos_Max;}
  281.     if(mouse_pos_y < 0) 
  282.         {mouse_pos_y = 0;}
  283.     //mouse button
  284.     mouse_btn_status = (m.status & 0x07);
  285.     
  286.     
  287.     #ifdef MONE_PS2_MOUSE
  288.         printf("==>mous  X[%d]   Y[%d]  B[%d] n", mouse_pos_x, mouse_pos_y, mouse_btn_status);
  289.     #endif    
  290. }
  291. void resetMouse(void)
  292. {
  293.     regs0->iop_data[5]=0x8000|RESET_M;
  294. }
  295. #endif