-
Notifications
You must be signed in to change notification settings - Fork 11
/
usbhw.c
292 lines (256 loc) · 7.23 KB
/
usbhw.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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*****************************************************************************
* *
* DFU/SD/SDHC Bootloader for LPC17xx *
* *
* by Triffid Hunter *
* *
* *
* This firmware is Copyright (C) 2009-2010 Michael Moon aka Triffid_Hunter *
* *
* 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 2 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, write to the Free Software *
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
* *
*****************************************************************************/
#include "usbhw.h"
#include <LPC17xx.h>
#include <lpc17xx_clkpwr.h>
#include "lpc17xx_usb.h"
#include <stdio.h>
#if !(defined DEBUG)
#define printf(...) do {} while (0)
#endif
/// pointers for callbacks to EP1-15 both IN and OUT
usb_callback_pointer EPcallbacks[30];
void usb_init(void)
{
// enable USB hardware
LPC_SC->PCONP |= CLKPWR_PCONP_PCUSB;
// enable clocks
LPC_USB->USBClkCtrl |= DEV_CLK_EN | AHB_CLK_EN;
// wait for clocks to stabilise
while (LPC_USB->USBClkSt != (DEV_CLK_ON | AHB_CLK_ON));
// configure USBD+ and USBD-
LPC_PINCON->PINSEL1 &= 0xc3ffffff;
LPC_PINCON->PINSEL1 |= 0x14000000;
SIE_Disconnect();
// configure USB Connect
LPC_PINCON->PINSEL4 &= 0xfffcffff;
LPC_PINCON->PINSEL4 |= 0x00040000;
}
void usb_connect(void)
{
LPC_USB->USBDevIntEn = DEV_STAT | EP_SLOW;
usb_realise_endpoint(EP0IN, 64);
usb_realise_endpoint(EP0OUT, 64);
SIE_Connect();
}
void usb_disconnect(void)
{
SIE_Disconnect();
}
void usb_set_callback(uint8_t bEP, usb_callback_pointer callback)
{
EPcallbacks[EP(bEP)] = callback;
}
void usb_realise_endpoint(uint8_t bEP, uint16_t packet_size)
{
__disable_irq();
LPC_USB->USBDevIntClr = EP_RLZD;
LPC_USB->USBReEp |= EP(bEP);
LPC_USB->USBEpInd = EP2IDX(bEP);
LPC_USB->USBMaxPSize = packet_size;
__enable_irq();
while (!(LPC_USB->USBDevIntSt & EP_RLZD));
LPC_USB->USBDevIntClr = EP_RLZD;
LPC_USB->USBEpIntEn |= EP(bEP);
}
int usb_read_packet(uint8_t bEP, void *buffer, int buffersize)
{
int i = 0;
int l = 0;
uint32_t j = 0;
__disable_irq();
LPC_USB->USBCtrl = RD_EN | ((bEP & 0xF) << 2);
while ((LPC_USB->USBRxPLen & 0x800) != 0x800);
l = LPC_USB->USBRxPLen & 0x3FF;
if (l > buffersize)
{
// printf("Not enough room in buffer (got %d need %d), failing to read\n", buffersize, l);
__enable_irq();
return l;
}
if (l == 0)
j = LPC_USB->USBRxData;
else
{
for (i = 0; i < buffersize; i++)
{
if ((i & 3) == 0)
{
j = LPC_USB->USBRxData;
}
((uint8_t *) buffer)[i] = j & 0xFF;
j >>= 8;
if ((i & 3) == 3)
{
if (!(LPC_USB->USBCtrl & RD_EN))
{
SIE_SelectEndpoint(bEP);
SIE_ClearBuffer();
__enable_irq();
return l;
}
}
}
}
SIE_SelectEndpoint(bEP);
SIE_ClearBuffer();
__enable_irq();
return l;
}
int usb_write_packet(uint8_t bEP, void *data, int packetlen)
{
int i = 0;
uint8_t *d = (uint8_t *) data;
__disable_irq();
LPC_USB->USBCtrl = WR_EN | ((bEP & 0xF) << 2);
LPC_USB->USBTxPLen = packetlen;
if (packetlen)
{
for (i = 0;(LPC_USB->USBCtrl & WR_EN) && (i < packetlen);)
{
// printf("[%x]",d);
LPC_USB->USBTxData = ((d[0]) << 0) | ((d[1]) << 8) | ((d[2]) << 16) | ((d[3]) << 24);
d += 4;
i += 4;
}
}
else
{
LPC_USB->USBTxData = 0;
}
SIE_SelectEndpoint(bEP);
SIE_ValidateBuffer();
__ISB();
__enable_irq();
if (i > packetlen)
return packetlen;
return i;
}
void usb_ep_stall(uint8_t bEP)
{
SIE_SetEndpointStatus(bEP, SIE_EPST_ST);
}
void usb_ep_unstall(uint8_t bEP)
{
SIE_SetEndpointStatus(bEP, 0);
}
void usb_ep0_stall(void)
{
SIE_SetEndpointStatus(EP0OUT, SIE_EPST_CND_ST);
}
void usb_task(void)
{
if (LPC_USB->USBDevIntSt & FRAME)
{
// USBEvent_Frame(SIEgetFrameNumber());
LPC_USB->USBDevIntClr = FRAME;
}
if (LPC_USB->USBDevIntSt & DEV_STAT)
{
LPC_USB->USBDevIntClr = DEV_STAT;
uint8_t devStat = SIE_GetDeviceStatus();
if (devStat & SIE_DEVSTAT_SUS_CH)
{
// USBEvent_suspendStateChanged(devStat & SIE_DEVSTAT_SUS);
// printf("USB:Suspend\n");
}
if (devStat & SIE_DEVSTAT_RST)
{
printf("USB:Bus Reset\n");
USBEvent_busReset();
usb_realise_endpoint(EP0IN , 64);
usb_realise_endpoint(EP0OUT, 64);
SIE_SetMode(SIE_MODE_INAK_CI);
}
if (devStat & SIE_DEVSTAT_CON_CH)
{
// printf("USB:Connect\n");
// USBEvent_connectStateChanged(devStat & SIE_DEVSTAT_CON);
}
}
if (LPC_USB->USBDevIntSt & EP_SLOW)
{
int st = LPC_USB->USBEpIntSt;
// printf("INTST: %x\n", st);
if (st & EP(EP0OUT))
{
int st;
if ((st = SIE_SelectEndpointClearInterrupt(EP0OUT)) & SIE_EP_STP) // SETUP packet
{
// printf("EP0SETUP_ST: 0x%x\n", st);
// this is a setup packet
EP0setup();
}
else if (st & SIE_EP_FE) // OUT endpoint and FE = 1 (buffer has data)
{
// printf("EP0OUT_ST: 0x%x\n", st);
EP0out();
}
else // EP0OUT, FE = 0 (no data) - why are we interrupting?
{
uint8_t b[8];
// int l = usb_read_packet(EP0OUT, b, 8);
usb_read_packet(EP0OUT, b, 8);
// printf("EP0OUT: spurious interrupt (0x%x, %d)\n", st, l);
}
}
if (st & EP(EP0IN))
{
int st = SIE_SelectEndpointClearInterrupt(EP0IN);
if ((st & SIE_EP_FE) == 0)
EP0in();
// else
// printf("Interrupt on EP0IN: FE:%d ST:%d STP:%d PO:%d EPN:%d B1:%d B2:%d\n", st & 1, (st >> 1) & 1, (st >> 2) & 1, (st >> 3) & 1, (st >> 4) & 1, (st >> 5) & 1, (st >> 6) & 1);
}
if (st & ~(3UL))
{
int i = 3;
uint32_t bitmask = 1<<3;
for (;
i < 32;
i++, bitmask <<= 1
)
{
if (LPC_USB->USBEpIntSt & bitmask)
{
int st = SIE_SelectEndpointClearInterrupt(IDX2EP(i));
if (
((i & 1) && ((st & SIE_EP_FE) == 0)) || // IN endpoint and FE = 0 (space in buffer)
(((i & 1) == 0) && (st & SIE_EP_FE)) // OUT endpoint and FE = 1 (buffer has data)
)
{
// TODO: check for event receivers
}
}
}
}
LPC_USB->USBDevIntClr = EP_SLOW;
}
}
#ifndef __CC_ARM
__attribute__ ((interrupt)) void USB_IRQHandler() {
// usb_task();
}
#endif