-
Notifications
You must be signed in to change notification settings - Fork 28
/
usb_endp.c
91 lines (78 loc) · 2.43 KB
/
usb_endp.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
#include "usb_endp.h"
#include "ch554_platform.h"
#include "usb_hid_keyboard.h"
#define MAX_PACKET_SIZE 64
// The buffer (Tx and Rx) must have an even address, size: 66 (0x42)
xdatabuf(EP1_ADDR, Ep1Buffer, 64 > (MAX_PACKET_SIZE + 2) ? 64 : (MAX_PACKET_SIZE + 2));
// The buffer (Tx and Rx) must have an even address, size: 132 (0x84)
xdatabuf(EP2_ADDR, Ep2Buffer, 128 > (2 * MAX_PACKET_SIZE + 4) ? 128 : (2 * MAX_PACKET_SIZE + 4));
// The buffer (Tx and Rx) must have an even address, size: 132 (0x84)
xdatabuf(EP3_ADDR, Ep3Buffer, 128 > (2 * MAX_PACKET_SIZE + 4) ? 128 : (2 * MAX_PACKET_SIZE + 4));
uint8_t USB_EP_HALT_SET(uint8_t ep) {
switch (ep) {
case 0x82:
UEP2_CTRL = UEP2_CTRL & (~bUEP_T_TOG) | UEP_T_RES_STALL;
return 0;
case 0x02:
UEP2_CTRL = UEP2_CTRL & (~bUEP_R_TOG) | UEP_R_RES_STALL;
return 0;
case 0x83:
UEP3_CTRL = UEP3_CTRL & (~bUEP_T_TOG) | UEP_T_RES_STALL;
return 0;
case 0x03:
UEP3_CTRL = UEP3_CTRL & (~bUEP_R_TOG) | UEP_R_RES_STALL;
return 0;
case 0x81:
UEP1_CTRL = UEP1_CTRL & (~bUEP_T_TOG) | UEP_T_RES_STALL;
return 0;
default:
return 0xFF;
}
}
uint8_t USB_EP_HALT_CLEAR(uint8_t ep) {
switch (ep)
{
case 0x82:
UEP2_CTRL = UEP2_CTRL & ~(bUEP_T_TOG | MASK_UEP_T_RES) | UEP_T_RES_NAK;
return 0;
case 0x02:
UEP2_CTRL = UEP2_CTRL & ~(bUEP_R_TOG | MASK_UEP_R_RES) | UEP_R_RES_ACK;
return 0;
case 0x83:
UEP3_CTRL = UEP3_CTRL & ~(bUEP_T_TOG | MASK_UEP_T_RES) | UEP_T_RES_NAK;
return 0;
case 0x03:
UEP3_CTRL = UEP3_CTRL & ~(bUEP_R_TOG | MASK_UEP_R_RES) | UEP_R_RES_ACK;
return 0;
case 0x81:
UEP1_CTRL = UEP1_CTRL & ~(bUEP_T_TOG | MASK_UEP_T_RES) | UEP_T_RES_NAK;
return 0;
default:
return 0xFF;
}
}
extern uint8_t keyState, kbdModifier, kbdKey;
void USB_EP1_IN(void) {
UEP1_T_LEN = 0;
UEP1_CTRL = UEP1_CTRL & ~MASK_UEP_T_RES | UEP_T_RES_NAK; // No data to send
if (keyState == KBD_STATE_KEYDOWN)
keyState = KBD_STATE_IDLE;
}
void USB_EP2_IN(void) {
UEP2_T_LEN = 0;
UEP2_CTRL = UEP2_CTRL & ~MASK_UEP_T_RES | UEP_T_RES_NAK; // No data to send
}
void USB_EP2_OUT(void) {
uint8_t i;
if (U_TOG_OK) { // Discard unsynchronized packets
for (i = 0; i < USB_RX_LEN; i++)
EP2_TX_BUF[i] = EP2_RX_BUF[i] ^ 0xFF; // Invert bits and Tx to host (for validation)
if (EP2_RX_BUF[0]==0xEE) {
keyState = KBD_STATE_KEYDOWN;
kbdModifier = EP2_RX_BUF[1];
kbdKey = EP2_RX_BUF[2];
}
UEP2_T_LEN = USB_RX_LEN;
UEP2_CTRL = UEP2_CTRL & ~MASK_UEP_T_RES | UEP_T_RES_ACK; // Enable Tx
}
}