📄 ambamse.c
字号:
#include "copyright_wrs.h"/* includes */#include "vxWorks.h"#include "iv.h"#include "ioLib.h"#include "iosLib.h"#include "memLib.h"#include "tyLib.h"#include "errnoLib.h"#include "wdLib.h"#include "sysLib.h"#include "intLib.h"#include "taskLib.h"#include "ambakmi.h"/*#include "ambaKbd.c"*//* forward declarations */LOCAL void mseHwInit();LOCAL int mseOpen();LOCAL int mseClose();LOCAL void mseTxStart();LOCAL void at91cMseIntr (MSE_DEVICE *pDev);LOCAL void clkIntr();LOCAL void waitForCLK(int value);LOCAL void writeBit(UCHAR b) ;LOCAL unsigned char in_char;LOCAL UCHAR bitCnt = 0;LOCAL UCHAR in_char;LOCAL UCHAR parity;LOCAL UCHAR byte2;LOCAL UCHAR parity2 = 1;LOCAL UCHAR bitCnt2 =0;/*lab used for ack receivelab=0,use int to send data lab =1,wait for an acklab =2 ,rec data */LOCAL UCHAR lab =2; #include <ugl/sysWindML.h>/********************************************************************************* ambaPs2DevCreate - PS/2 mouse driver initialization routine** This routine creates a device for a PS/2 mouse. The AMBA hardware is* initialized when the keyboard interface is initialized.** RETURNS: device number, if sucessful; otherwise ERROR.** SEE ALSO: */int at91cPs2DevCreate ( char *name /* name to be associated with device */ ) { int mseDrvNum; /* device number for this driver */ DEV_HDR * pHdr; MSE_DEVICE * pMseDevice; /* device descriptors */ char * pName; WINDML_DEVICE *pDev; /* if device is already present, do not create again */ pHdr = iosDevFind (name, &pName); if ((pHdr != NULL) && (strcmp (name, pHdr->name) == 0)) return (OK); pMseDevice = (MSE_DEVICE *)malloc (sizeof (MSE_DEVICE)); /* enable the mouse interrupt */ pDev = sysWindMLDevGet (WINDML_POINTER_DEVICE, 0, 0, 0);intDisable(KBD_MSE_INT_LVL); intConnect(KBD_MSE_INT_VEC,clkIntr,(int)pMseDevice); /* Install the driver and return installation status */ mseDrvNum = iosDrvInstall(mseOpen, (FUNCPTR) NULL, mseOpen, (FUNCPTR) mseClose, tyRead, tyWrite, tyIoctl); /* Create the channels for this device */ if (tyDevInit(&pMseDevice->ty_dev, 512, 512, (FUNCPTR)mseTxStart) != OK) return (ERROR); /* initialize the interface */ mseHwInit (); KBD_INT_ENABLE; MSE_INT_ENABLE; /*taskDelay(120);*//*debug添加。。。。。*/ /* enable the pointer interrupt */ intEnable(KBD_MSE_INT_LVL); /* add the device to the I/O system */ if (iosDevAdd(&pMseDevice->ty_dev.devHdr,name,mseDrvNum) == ERROR) return (ERROR); return (mseDrvNum); }LOCAL void waitForCLK(int value)/*0,1 wait about 40us*/{ int i=0; while ((MSE_CLK_READ != value) && (i++<2000));/*Wait for clk */}LOCAL void writeBit(UCHAR b) /* rise edge */{ waitForCLK(0); MSE_DATA_WRITE(b); waitForCLK(1);}void at91cMsWrite(unsigned char value){ int i=0; UCHAR p=1; MSE_INT_DISABLE;/*此处最好添加相应是否有数据收程序*/ MSE_CLK_OUTPUT; /*clk,data as output*/ MSE_DATA_OUTPUT; MSE_CLK_WRITE(0);/*clk=0*/ MSE_DATA_WRITE(0);/*start bit=0*/ taskDelay(1);/*wait > 100us*/ MSE_CLK_INPUT; for(i=0;i<8;i++) { writeBit(value & 1); /*bit write*/ p = p + value; value = value >> 1; } writeBit(p & 1);/*check*/ writeBit(1); /*stop bit =1*/ MSE_DATA_INPUT;/*data release ,data as input*/ lab = 2; /*表示直接接收数据*/ bitCnt = 0;}/********************************************************************************* mseHwInit - initialize AMBA hardware for mouse support** This routine initializes the AMBA hardware for a PS/2 mouse.** RETURNS N/A** SEE ALSO: */LOCAL void mseHwInit (void) {/*pio enable*/ *AT91C_PIOB_PER = MSE_CLK | MSE_DATA;/* 100K Pullup reg disable*/ *AT91C_PIOB_PPUDR = MSE_CLK | MSE_DATA; intDisable(AT91C_INT_LVL_PIOB); *AT91C_PIOB_IDR = 0xffffffff; *AT91C_AIC_SMR3 = AT91C_AIC_SRCTYPE_INT_EDGE_TRIGGERED + 2;/* 实际验证0.5s比较好 */ at91cMsWrite(0xff); /* reset cmd ,get into stream mode */ taskDelay(30); /* ack is 0xfa ,aa ,00 */ at91cMsWrite(0xf2); /*id */ taskDelay(30); at91cMsWrite(0xf4); /*enable*/ taskDelay(60); return; }/********************************************************************************* mseOpen - open file** This routine opens the PS2 device.* * RETURNS device structure** SEE ALSO: */LOCAL int mseOpen ( MSE_DEVICE *dev, char *name, int mode ) { return ((int)dev); }/******************************************************************************* mseTxStart - send data to mouse** This routine transmits data to the PS2 device** RETURNS N/A** SEE ALSO: */LOCAL void mseTxStart ( MSE_DEVICE *dev ) { char out_char; while (tyITx(&dev->ty_dev,&out_char) == OK); }/******************************************************************************* mseClose - close file** This routine closes the PS2 device** RETURNS N/A** SEE ALSO: */LOCAL int mseClose ( MSE_DEVICE *dev ) { return((int)dev); }/******************************************************************************* ambaMseIntr - handle interrupts** This routine is the PS2 interrupt handler** RETURNS NA** SEE ALSO: */LOCAL void at91cMseIntr ( MSE_DEVICE *pDev /* device control structure */ ) { UINT16 data; int oldMask; oldMask=intLock(); if((MSE_CLK_READ == 0)&&(lab != 0)) /*fall-readbyte */ { data = MSE_DATA_READ; switch(lab) { case 1: /*ack*/ if(data==0) { lab = 2; } break; case 2: /*after ack*/ switch(bitCnt) { case 0: parity = 0; /*for odd parity*/ in_char = 0; if(data == 0) /*start bit*/ bitCnt++; else { intUnlock(oldMask); return; } break; case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8:/*in_char*/ bitCnt++; in_char |= data<< (bitCnt -2); parity += data; break; case 9: bitCnt++; parity += data; if((parity & 1) == 0) /*parity bit wrong*/ { bitCnt=0; } break; case 10: /*stop bit*/ bitCnt =0; if(data!= 0) { logMsg("m-%02x\n",in_char,2,3,4,5,6); tyIRd (&pDev->ty_dev,in_char); } break; default: bitCnt=0; break; } break; default: break; } } else if(lab==0) { bitCnt2++; if(bitCnt2<=8) /*in_char 1-8*/ { MSE_DATA_WRITE(byte2&1); parity2 += byte2; byte2 = byte2 >>1; } else if(bitCnt2 == 9)/*parity */ { MSE_DATA_WRITE(parity2 & 1); } else if(bitCnt2 == 10)/*stop */ { MSE_DATA_WRITE(1); lab = 1; bitCnt2 = 0;/* parity2 =1;*/ MSE_DATA_INPUT;/*data release ,data as input*/ } } intUnlock(oldMask); }/********************************************************************************* clkInt-* * This routine is called as interrupt comes** RETURNS: N/A** NOMANUAL*/LOCAL void clkIntr ( MSE_DEVICE *pDev /* device control structure */ ){ UINT16 mask,status; mask = *AT91C_PIOB_IMR; status = *AT91C_PIOB_ISR; status &= mask;#if 0 logMsg("status %x \n",status,2,3,4,5,6);#endif if((status & MSE_CLK) == MSE_CLK) { at91cMseIntr(pDev); } if((status & KBD_CLK) == KBD_CLK) { at91ckbdIntr(); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -