-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAno_USB.c
138 lines (132 loc) · 3.7 KB
/
Ano_USB.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/******************** (C) COPYRIGHT 2017 ANO Tech ********************************
* 作者 :匿名科创
* 官网 :www.anotc.com
* 淘宝 :anotc.taobao.com
* 技术Q群 :190169595
* 描述 :USB通信相关函数
**********************************************************************************/
#include "RTL.h"
#include <rl_usb.h>
#include <stm32f4xx.h> /* STM32F4xx Definitions */
#define __NO_USB_LIB_C
#include "usb_config.c"
#include "Ano_DT.h"
#include "Ano_USB.h"
u8 HID_SEND_TIMEOUT = 5; //hid发送不足一帧时,等待HID_SEND_TIMEOUT周期进行发送
u8 hid_datatemp[256]; //hid环形缓冲区
u8 hid_datatemp_begin = 0; //环形缓冲区数据指针,指向应当发送的数据
u8 hid_datatemp_end = 0; //环形缓冲区数据结尾
u8 hid_data2send[64]; //hid发送缓存,一个hid数据帧64字节,第一字节表示有效数据字节数,0-63,后面是数据,最多63字节
void Usb_Hid_Init (void)
{
usbd_init();
usbd_connect(__TRUE);
}
void Usb_Hid_Adddata(u8 *dataToSend , u8 length)
{
for(u8 i=0; i<length; i++)
{
hid_datatemp[hid_datatemp_end++] = dataToSend[i];
}
}
void Usb_Hid_Send (void)
{
static u8 notfull_timeout=0;
if(hid_datatemp_end > hid_datatemp_begin)
{
if((hid_datatemp_end - hid_datatemp_begin) >= 63)
{
notfull_timeout = 0;
hid_data2send[0] = 63;
for(u8 i=0; i<63; i++)
{
hid_data2send[i+1] = hid_datatemp[hid_datatemp_begin++];
}
usbd_hid_get_report_trigger(0, hid_data2send, 64); //send
}
else
{
notfull_timeout++;
if(notfull_timeout == HID_SEND_TIMEOUT)
{
notfull_timeout = 0;
hid_data2send[0] = hid_datatemp_end - hid_datatemp_begin;
for(u8 i=0; i<63; i++)
{
if(i<hid_datatemp_end - hid_datatemp_begin)
hid_data2send[i+1] = hid_datatemp[hid_datatemp_begin+i];
else
hid_data2send[i+1] = 0;
}
hid_datatemp_begin = hid_datatemp_end;
usbd_hid_get_report_trigger(0, hid_data2send, 64); //send
}
}
}
else if(hid_datatemp_end < hid_datatemp_begin)
{
if((256 - hid_datatemp_begin + hid_datatemp_end) >= 63)
{
notfull_timeout = 0;
hid_data2send[0] = 63;
for(u8 i=0; i<63; i++)
{
hid_data2send[i+1] = hid_datatemp[hid_datatemp_begin++];
}
usbd_hid_get_report_trigger(0, hid_data2send, 64); //send
}
else
{
notfull_timeout++;
if(notfull_timeout == HID_SEND_TIMEOUT)
{
notfull_timeout = 0;
hid_data2send[0] = 256 - hid_datatemp_begin + hid_datatemp_end;
for(u8 i=0; i<63; i++)
{
if(i<256 - hid_datatemp_begin + hid_datatemp_end)
hid_data2send[i+1] = hid_datatemp[(u8)(hid_datatemp_begin+i)];
else
hid_data2send[i+1] = 0;
}
hid_datatemp_begin = hid_datatemp_end;
usbd_hid_get_report_trigger(0, hid_data2send, 64); //dend
}
}
}
}
int usbd_hid_get_report (U8 rtype, U8 rid, U8 *buf, U8 req)
{
switch (rtype)
{
case HID_REPORT_INPUT:
switch (rid)
{
case 0:
switch (req)
{
case USBD_HID_REQ_EP_CTRL:
case USBD_HID_REQ_PERIOD_UPDATE:
return 64;
case USBD_HID_REQ_EP_INT:
break;
}
break;
}
break;
case HID_REPORT_FEATURE:
return (1);
}
return (0);
}
void usbd_hid_set_report (U8 rtype, U8 rid, U8 *buf, int len, U8 req) {
switch (rtype) {
case HID_REPORT_OUTPUT:
for(u8 i = 1; i<=(*(buf)); i++)
ANO_DT_Data_Receive_Prepare(*(buf+i)); //hid接收到数据会调用此函数
break;
case HID_REPORT_FEATURE:
//feat = buf[0];
break;
}
}