usbd_lld_int.c
9.19 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*!
\file usbd_lld_int.c
\brief USB device low level interrupt routines
\version 2020-08-01, V3.0.0, firmware for GD32F30x
*/
/*
Copyright (c) 2020, GigaDevice Semiconductor Inc.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/
#include "usbd_lld_int.h"
#include "usbd_lld_core.h"
/* local function prototypes ('static') */
static void usbd_int_suspend (usb_dev *udev);
/*!
\brief handle USB high priority successful transfer event
\param[in] udev: pointer to USB device instance
\param[out] none
\retval none
*/
void usbd_int_hpst (usb_dev *udev)
{
__IO uint16_t int_status = 0U;
/* wait till interrupts are not pending */
while ((int_status = (uint16_t)USBD_INTF) & (uint16_t)INTF_STIF) {
/* get endpoint number */
uint8_t ep_num = (uint8_t)(int_status & INTF_EPNUM);
uint8_t transc_num = (uint8_t)TRANSC_UNKNOWN;
if (int_status & INTF_DIR) {
if (USBD_EPxCS(ep_num) & EPxCS_RX_ST) {
uint16_t count = 0U;
usb_transc *transc = &udev->transc_out[ep_num];
/* clear successful receive interrupt flag */
USBD_EP_RX_ST_CLEAR(ep_num);
count = udev->drv_handler->ep_read (transc->xfer_buf, ep_num, (uint8_t)EP_BUF_DBL);
user_buffer_free(ep_num, (uint8_t)DBUF_EP_OUT);
transc->xfer_buf += count;
transc->xfer_count += count;
transc->xfer_len -= count;
if ((0U == transc->xfer_len) || (count < transc->max_len)) {
USBD_EP_RX_STAT_SET(ep_num, EPRX_NAK);
transc_num = (uint8_t)TRANSC_OUT;
}
}
} else {
/* handle the in direction transaction */
if (USBD_EPxCS(ep_num) & EPxCS_TX_ST) {
/* clear successful transmit interrupt flag */
USBD_EP_TX_ST_CLEAR(ep_num);
transc_num = (uint8_t)TRANSC_IN;
}
}
if ((uint8_t)TRANSC_UNKNOWN != transc_num) {
udev->ep_transc[ep_num][transc_num](udev, ep_num);
}
}
}
/*!
\brief USB interrupt events service routine
\param[in] none
\param[out] none
\retval none
*/
void usbd_isr (void)
{
__IO uint16_t int_status = (uint16_t)USBD_INTF;
__IO uint16_t int_flag = (uint16_t)(USBD_INTF & (USBD_CTL & USBD_INTEN));
usb_dev *udev = usbd_core.dev;
if (INTF_STIF & int_flag) {
/* wait till interrupts are not pending */
while ((int_status = (uint16_t)USBD_INTF) & (uint16_t)INTF_STIF) {
/* get endpoint number */
uint8_t ep_num = (uint8_t)(int_status & INTF_EPNUM);
if (int_status & INTF_DIR) {
/* handle the USB OUT direction transaction */
if (USBD_EPxCS(ep_num) & EPxCS_RX_ST) {
/* clear successful receive interrupt flag */
USBD_EP_RX_ST_CLEAR(ep_num);
if (USBD_EPxCS(ep_num) & EPxCS_SETUP) {
if (ep_num == 0U) {
udev->ep_transc[ep_num][TRANSC_SETUP](udev, ep_num);
} else {
return;
}
} else {
usb_transc *transc = &udev->transc_out[ep_num];
uint16_t count = udev->drv_handler->ep_read (transc->xfer_buf, ep_num, (uint8_t)EP_BUF_SNG);
transc->xfer_buf += count;
transc->xfer_count += count;
if ((transc->xfer_count >= transc->xfer_len) || (count < transc->max_len)) {
if (udev->ep_transc[ep_num][TRANSC_OUT]) {
udev->ep_transc[ep_num][TRANSC_OUT](udev, ep_num);
}
} else {
udev->drv_handler->ep_rx_enable(udev, ep_num);
}
}
}
} else {
/* handle the USB IN direction transaction */
if (USBD_EPxCS(ep_num) & EPxCS_TX_ST) {
/* clear successful transmit interrupt flag */
USBD_EP_TX_ST_CLEAR(ep_num);
usb_transc *transc = &udev->transc_in[ep_num];
if (transc->xfer_len == 0U) {
if (udev->ep_transc[ep_num][TRANSC_IN]) {
udev->ep_transc[ep_num][TRANSC_IN](udev, ep_num);
}
} else {
usbd_ep_send(udev, ep_num, transc->xfer_buf, transc->xfer_len);
}
}
}
}
}
if (INTF_WKUPIF & int_flag) {
/* clear wakeup interrupt flag in INTF */
CLR(WKUPIF);
/* restore the old cur_status */
udev->cur_status = udev->backup_status;
#ifdef LPM_ENABLED
if ((0U == udev->pm.remote_wakeup_on) && (0U == udev->lpm.L1_resume)) {
resume_mcu(udev);
} else if (1U == udev->pm.remote_wakeup_on) {
/* no operation */
} else {
udev->lpm.L1_resume = 0U;
}
/* clear L1 remote wakeup flag */
udev->lpm.L1_remote_wakeup = 0U;
#else
if (0U == udev->pm.remote_wakeup_on) {
resume_mcu(udev);
}
#endif /* LPM_ENABLED */
}
if (INTF_SPSIF & int_flag) {
if(!(USBD_CTL & CTL_RSREQ)) {
usbd_int_suspend (udev);
/* clear of suspend interrupt flag bit must be done after setting of CTLR_SETSPS */
CLR(SPSIF);
}
}
if (INTF_SOFIF & int_flag) {
/* clear SOF interrupt flag in INTF */
CLR(SOFIF);
/* if necessary, user can add code here */
if (NULL != usbd_int_fops) {
(void)usbd_int_fops->SOF(udev);
}
}
if (INTF_ESOFIF & int_flag) {
/* clear ESOF interrupt flag in INTF */
CLR(ESOFIF);
/* control resume time by ESOFs */
if (udev->pm.esof_count > 0U) {
if (0U == --udev->pm.esof_count) {
if (udev->pm.remote_wakeup_on) {
USBD_CTL &= ~CTL_RSREQ;
udev->pm.remote_wakeup_on = 0U;
} else {
USBD_CTL |= CTL_RSREQ;
udev->pm.esof_count = 3U;
udev->pm.remote_wakeup_on = 1U;
}
}
}
}
if (INTF_RSTIF & int_flag) {
/* clear reset interrupt flag in INTF */
CLR(RSTIF);
udev->drv_handler->ep_reset(udev);
}
#ifdef LPM_ENABLED
if (INTF_L1REQ & int_flag) {
/* clear L1 ST bit in LPM INTF */
USBD_INTF = CLR(L1REQ);
/* read BESL field from subendpoint0 register which corresponds to HIRD parameter in LPM spec */
udev->lpm.besl = (USBD_LPMCS & LPMCS_BLSTAT) >> 4;
/* read BREMOTEWAKE bit from subendpoint0 register which corresponding to bRemoteWake bit in LPM request */
udev->lpm.L1_remote_wakeup = (USBD_LPMCS & LPMCS_REMWK) >> 3;
/* process USB device core layer suspend routine */
usbd_int_suspend(udev);
}
#endif /* LPM_ENABLED */
}
/*!
\brief handle USB suspend event
\param[in] udev: pointer to USB device instance
\param[out] none
\retval none
*/
static void usbd_int_suspend (usb_dev *udev)
{
/* store the device current status */
udev->backup_status = udev->cur_status;
/* set device in suspended state */
udev->cur_status = (uint8_t)USBD_SUSPENDED;
/* usb enter in suspend mode and mcu system in low power mode */
if (udev->pm.suspend_enabled) {
usbd_to_suspend(udev);
} else {
/* if not possible then resume after xx ms */
udev->pm.esof_count = 3U;
}
}