1 Star 2 Fork 1

Eddie/ch559-usb-host

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Ps2Mouse.c 7.76 KB
一键复制 编辑 原始数据 按行查看 历史
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
#include "Type.h"
#include "CH559.h"
#include "System.h"
#include "PinDefine.h"
#include "Protocol.h"
#include "Ps2Mouse.h"
#ifdef COUNT_FACTOR
#undef COUNT_FACTOR
#endif
#ifdef USE_PLL
#define COUNT_FACTOR 4
#else
#define COUNT_FACTOR 1
#endif
#define STANDARD_MOUSE_LEN 3
#define INTELLI_MOUSE_LEN 4
//鼠标数据长度
static UINT8 s_ps2MouseDataLen = STANDARD_MOUSE_LEN;
void DisablePs2MousePort()
{
CLR_GPIO_BIT(PIN_MS_CLK);
SET_GPIO_OUTPUT(PIN_MS_CLK);
}
static BOOL SendPs2MouseByte(UINT8 c)
{
UINT8 parity = 0x00;
UINT8 i;
UINT16 j;
CLR_GPIO_BIT(PIN_MS_CLK);
SET_GPIO_OUTPUT(PIN_MS_CLK);
mDelayuS(105);
CLR_GPIO_BIT(PIN_MS_DAT);
SET_GPIO_OUTPUT(PIN_MS_DAT);
mDelayuS(20);
SET_GPIO_BIT(PIN_MS_CLK);
SET_GPIO_INPUT(PIN_MS_CLK);
j = 3000 * COUNT_FACTOR;
do
{
j--;
if (j == 0)
{
return FALSE;
}
} while (GET_GPIO_BIT(PIN_MS_CLK)); //等待时钟变低
for (i = 0; i < 8; i++)
{
if (c & 0x01)
{
SET_GPIO_BIT(PIN_MS_DAT);
}
else
{
CLR_GPIO_BIT(PIN_MS_DAT);
}
j = 500 * COUNT_FACTOR;
do
{
j--;
if (j == 0)
{
return FALSE;
}
} while (!GET_GPIO_BIT(PIN_MS_CLK));
j = 500 * COUNT_FACTOR;
do
{
j--;
if (j == 0)
{
return FALSE;
}
} while (GET_GPIO_BIT(PIN_MS_CLK));
parity ^= (c & 0x01);
c >>= 1;
}
parity ^= 0x01;
//奇校验位
if (parity & 0x01)
{
SET_GPIO_BIT(PIN_MS_DAT);
}
else
{
CLR_GPIO_BIT(PIN_MS_DAT);
}
j = 500 * COUNT_FACTOR;
do
{
j--;
if (j == 0)
{
return FALSE;
}
} while (!GET_GPIO_BIT(PIN_MS_CLK));
j = 500 * COUNT_FACTOR;
do
{
j--;
if (j == 0)
{
return FALSE;
}
} while (GET_GPIO_BIT(PIN_MS_CLK));
//停止位
SET_GPIO_BIT(PIN_MS_DAT);
j = 500 * COUNT_FACTOR;
do
{
j--;
if (j == 0)
{
return FALSE;
}
} while (!(PIN_MS_CLK));
SET_GPIO_INPUT(PIN_MS_DAT);
j = 500 * COUNT_FACTOR;
do
{
j--;
if (j == 0)
{
return FALSE;
}
} while (GET_GPIO_BIT(PIN_MS_CLK));
//等待ack
j = 500 * COUNT_FACTOR;
do
{
j--;
if (j == 0)
{
return FALSE;
}
} while (!GET_GPIO_BIT(PIN_MS_CLK));
return TRUE;
}
static BOOL ReceivePs2MouseByte(UINT8 *pData)
{
UINT8 dat = 0x00;
UINT8 parity = 0x00;
UINT8 parityTemp;
UINT16 j;
UINT8 i;
SET_GPIO_BIT(PIN_MS_CLK);
SET_GPIO_INPUT(PIN_MS_CLK);
SET_GPIO_BIT(PIN_MS_DAT);
SET_GPIO_INPUT(PIN_MS_DAT);
if (GET_GPIO_BIT(PIN_MS_DAT))
{
return FALSE;
}
if (!GET_GPIO_BIT(PIN_MS_CLK) && !GET_GPIO_BIT(PIN_MS_DAT))
{
j = 500 * COUNT_FACTOR;
do
{
j--;
if (j == 0)
{
return FALSE;
}
} while (!GET_GPIO_BIT(PIN_MS_CLK));
j = 500 * COUNT_FACTOR;
do
{
j--;
if (j == 0)
{
return FALSE;
}
} while (GET_GPIO_BIT(PIN_MS_CLK));
//获取数据
for (i = 0; i < 8; i++)
{
if (!GET_GPIO_BIT(PIN_MS_CLK))
{
dat >>= 1;
if (GET_GPIO_BIT(PIN_MS_DAT))
{
dat |= 0x80;
}
else
{
dat &= 0x7f;
}
parity ^= GET_GPIO_BIT(PIN_MS_DAT);
j = 500 * COUNT_FACTOR;
do
{
j--;
if (j == 0)
{
return FALSE;
}
} while (!GET_GPIO_BIT(PIN_MS_CLK));
j = 500 * COUNT_FACTOR;
do
{
j--;
if (j == 0)
{
return FALSE;
}
} while (GET_GPIO_BIT(PIN_MS_CLK));
}
}
if (!GET_GPIO_BIT(PIN_MS_CLK))
{
parityTemp = GET_GPIO_BIT(PIN_MS_DAT);
j = 500 * COUNT_FACTOR;
do
{
j--;
if (j == 0)
{
return FALSE;
}
} while (!GET_GPIO_BIT(PIN_MS_CLK));
j = 500 * COUNT_FACTOR;
do
{
j--;
if (j == 0)
{
return FALSE;
}
} while (GET_GPIO_BIT(PIN_MS_CLK));
parity ^= 0x01;
}
else
{
return FALSE;
}
if (parityTemp != parity)
{
return FALSE;
}
if (!GET_GPIO_BIT(PIN_MS_CLK))
{
j = 500 * COUNT_FACTOR;
do
{
j--;
if (j == 0)
{
return FALSE;
}
} while (!GET_GPIO_BIT(PIN_MS_CLK));
*pData = dat;
return TRUE;
}
else
{
return FALSE;
}
}
return FALSE;
}
UINT8 TransceivePs2MousePort(UINT8 *pPs2)
{
UINT16 i;
UINT8 dat;
UINT8 ptr = 0;
UINT8 sum = 0;
UINT8 j;
UINT8 header;
UINT8 res = PS2_MS_NONE;
pPs2[ptr] = ID_PS2_MOUSE;
sum ^= pPs2[ptr];
ptr++;
i = 800 /** COUNT_FACTOR*/;
while (!ReceivePs2MouseByte(&dat))
{
i--;
if (i == 0)
{
return res;
}
}
pPs2[ptr] = dat;
ptr++;
sum ^= dat;
if (dat == 0xaa)
{
res = PS2_MS_AA;
return res;
}
header = dat;
for (j = 0; j < s_ps2MouseDataLen - 1; j++)
{
i = 1000 * COUNT_FACTOR;
while (!ReceivePs2MouseByte(&dat))
{
i--;
if (i == 0)
{
res = PS2_MS_INVALID_DATA;
return res;
}
}
pPs2[ptr] = dat;
ptr++;
sum ^= dat;
}
if (s_ps2MouseDataLen == STANDARD_MOUSE_LEN)
{
pPs2[ptr] = 0;
ptr++;
}
pPs2[ptr] = sum;
if (header & 0xc0)
{
//有溢出的数据
res = PS2_MS_INVALID_DATA;
}
else
{
res = PS2_MS_VALID_DATA;
}
return res;
}
void InitPs2Mouse()
{
UINT16 i;
UINT8 dat;
UINT8 k;
//复位
if (!SendPs2MouseByte(0xff))
{
return;
}
i = 1000 * COUNT_FACTOR;
while (!ReceivePs2MouseByte(&dat))
{
i--;
if (i == 0)
{
return;
}
}
k = 3 * COUNT_FACTOR;
i = 1000 * COUNT_FACTOR;
while (!ReceivePs2MouseByte(&dat))
{
i--;
if (i == 0)
{
k--;
if (k == 0)
{
return;
}
}
}
i = 1000 * COUNT_FACTOR;
while (!ReceivePs2MouseByte(&dat))
{
i--;
if (i == 0)
{
return;
}
}
//0xf3
if (!SendPs2MouseByte(0xf3))
{
return;
}
i = 1000 * COUNT_FACTOR;
while (!ReceivePs2MouseByte(&dat))
{
i--;
if (i == 0)
{
return;
}
}
//0xc8
if (!SendPs2MouseByte(0xc8))
{
return;
}
i = 1000 * COUNT_FACTOR;
while (!ReceivePs2MouseByte(&dat))
{
i--;
if (i == 0)
{
return;
}
}
//0xf3
if (!SendPs2MouseByte(0xf3))
{
return;
}
i = 1000 * COUNT_FACTOR;
while (!ReceivePs2MouseByte(&dat))
{
i--;
if (i == 0)
{
return;
}
}
//0x64
if (!SendPs2MouseByte(0x64))
{
return;
}
i = 1000 * COUNT_FACTOR;
while (!ReceivePs2MouseByte(&dat))
{
i--;
if (i == 0)
{
return;
}
}
//0xf3
if (!SendPs2MouseByte(0xf3))
{
return;
}
i = 1000 * COUNT_FACTOR;
while (!ReceivePs2MouseByte(&dat))
{
i--;
if (i == 0)
{
return;
}
}
//0x50
if (!SendPs2MouseByte(0x50))
{
return;
}
i = 1000 * COUNT_FACTOR;
while (!ReceivePs2MouseByte(&dat))
{
i--;
if (i == 0)
{
return;
}
}
//0xf2
if (!SendPs2MouseByte(0xf2))
{
return;
}
i = 1000 * COUNT_FACTOR;
while (!ReceivePs2MouseByte(&dat))
{
i--;
if (i == 0)
{
return;
}
}
i = 1000 * COUNT_FACTOR;
while (!ReceivePs2MouseByte(&dat))
{
i--;
if (i == 0)
{
return;
}
}
if (dat == 0x00)
{
//标准3字节PS2
s_ps2MouseDataLen = STANDARD_MOUSE_LEN;
}
else
{
//4字节PS2
s_ps2MouseDataLen = INTELLI_MOUSE_LEN;
}
//0xe8
if (!SendPs2MouseByte(0xe8))
{
return;
}
i = 1000 * COUNT_FACTOR;
while (!ReceivePs2MouseByte(&dat))
{
i--;
if (i == 0)
{
return;
}
}
//0x03
if (!SendPs2MouseByte(0x03))
{
return;
}
i = 1000 * COUNT_FACTOR;
while (!ReceivePs2MouseByte(&dat))
{
i--;
if (i == 0)
{
return;
}
}
//0xe6
if (!SendPs2MouseByte(0xe6))
{
return;
}
i = 1000 * COUNT_FACTOR;
while (!ReceivePs2MouseByte(&dat))
{
i--;
if (i == 0)
{
return;
}
}
//0xf3
if (!SendPs2MouseByte(0xf3))
{
return;
}
i = 1000 * COUNT_FACTOR;
while (!ReceivePs2MouseByte(&dat))
{
i--;
if (i == 0)
{
return;
}
}
//0x28
if (!SendPs2MouseByte(0x28))
{
return;
}
i = 1000 * COUNT_FACTOR;
while (!ReceivePs2MouseByte(&dat))
{
i--;
if (i == 0)
{
return;
}
}
//0xf4
if (!SendPs2MouseByte(0xf4))
{
return;
}
i = 1000 * COUNT_FACTOR;
while (!ReceivePs2MouseByte(&dat))
{
i--;
if (i == 0)
{
return;
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/zhu_huijia0001/ch559-usb-host.git
git@gitee.com:zhu_huijia0001/ch559-usb-host.git
zhu_huijia0001
ch559-usb-host
ch559-usb-host
master

搜索帮助