forked from Lotlab/nrf52-keyboard
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinterrupt.c
134 lines (119 loc) · 3.22 KB
/
interrupt.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
/*
Copyright (C) 2018,2019 Jim Jiang <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "interrupt.h"
#include "CH554_SDCC.h"
#include "endpoints.h"
// #include <stdio.h>
typedef void (*handler_t)(void);
const __CODE handler_t EndpointPacketOutHandler[5] = {
USB_OUT_EP0,
USB_OUT_EP1,
USB_OUT_EP2,
USB_OUT_EP3,
USB_OUT_EP4
};
const __CODE handler_t EndpointPacketInHandler[5] = {
USB_IN_EP0,
USB_IN_EP1,
USB_IN_EP2,
USB_IN_EP3,
USB_IN_EP4
};
const __CODE handler_t EndpointPacketSetupHandler[5] = {
USB_SETUP_EP0,
USB_SETUP_EP1,
USB_SETUP_EP2,
USB_SETUP_EP3,
USB_SETUP_EP4
};
const __CODE handler_t EndpointPacketSofHandler[5] = {
USB_SOF_EP0,
USB_SOF_EP1,
USB_SOF_EP2,
USB_SOF_EP3,
USB_SOF_EP4
};
/** \brief USB 传输完成中断处理
*/
static void UsbTransfurEventHandler()
{
uint8_t ep = USB_INT_ST & MASK_UIS_ENDP;
switch (USB_INT_ST & MASK_UIS_TOKEN) {
case UIS_TOKEN_IN:
(*EndpointPacketInHandler[ep])();
break;
case UIS_TOKEN_OUT:
(*EndpointPacketOutHandler[ep])();
break;
case UIS_TOKEN_SETUP:
(*EndpointPacketSetupHandler[ep])();
break;
case UIS_TOKEN_SOF:
(*EndpointPacketSofHandler[ep])();
break;
default:
break;
}
UIF_TRANSFER = 0; //写0清空中断
}
/** \brief USB 总线复位事件中断处理
*
*/
static void UsbBusResetEventHandler()
{
UEP0_CTRL = UEP_R_RES_ACK | UEP_T_RES_NAK;
UEP1_CTRL = bUEP_AUTO_TOG | UEP_R_RES_ACK | UEP_T_RES_NAK;
UEP2_CTRL = bUEP_AUTO_TOG | UEP_R_RES_ACK | UEP_T_RES_NAK;
UEP3_CTRL = bUEP_AUTO_TOG | UEP_R_RES_ACK | UEP_T_RES_NAK;
UEP4_CTRL = bUEP_AUTO_TOG | UEP_R_RES_ACK | UEP_T_RES_NAK;
USB_DEV_AD = 0x00;
UIF_SUSPEND = 0;
UIF_TRANSFER = 0;
UIF_BUS_RST = 0; //清中断标志
// 重置状态
usb_state.is_ready = false;
usb_state.protocol = true;
usb_state.setup_state = SETUP_IDLE;
}
/** \brief USB 总线挂起或唤醒事件处理
*
* \return void
*
*/
static void UsbBusSuspendEventHandler()
{
UIF_SUSPEND = 0;
UsbSuspendEvt(USB_MIS_ST & bUMS_SUSPEND); //挂起
}
/** \brief USB 中断服务函数
*
* \return void
*
*/
void UsbIsr()
{
if (UIF_TRANSFER) {
//USB传输完成标志
UsbTransfurEventHandler();
} else if (UIF_BUS_RST) {
//设备模式USB总线复位中断
UsbBusResetEventHandler();
} else if (UIF_SUSPEND) {
//USB总线挂起/唤醒完成
UsbBusSuspendEventHandler();
} else {
//意外的中断,产生中断必然会设置此标志位
USB_INT_FG = 0xFF; //清中断标志
}
}