-
Notifications
You must be signed in to change notification settings - Fork 150
/
usb.c
257 lines (198 loc) · 5.78 KB
/
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
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
/*
* STM32 HID Bootloader - USB HID bootloader for STM32F10X
* Copyright (c) 2018 Bruno Freitas - bruno@brunofreitas.com
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <stm32f10x.h>
#include <stdlib.h>
#include "usb.h"
#include "bitwise.h"
USB_RxTxBuf_t RxTxBuffer[MAX_EP_NUM];
volatile uint8_t DeviceAddress = 0;
volatile uint16_t DeviceConfigured = 0, DeviceStatus = 0;
void (*_EPHandler)(uint16_t) = NULL;
void (*_USBResetHandler)(void) = NULL;
/* USB String Descriptors */
const uint8_t sdVendor[] = {
0x22, // Size,
0x03, // Descriptor type
'w', 0, 'w', 0, 'w', 0, '.', 0, 's', 0, 'e', 0, 'r', 0, 'a', 0, 's', 0,
'i', 0, 'd', 0, 'i', 0, 's', 0, '.', 0, 'g', 0, 'r', 0
};
const uint8_t sdProduct[] = {
0x2C, // Size,
0x03, // Descriptor type
'S', 0, 'T', 0, 'M', 0, '3', 0, '2', 0, 'F', 0, ' ', 0, 'H', 0, 'I', 0,
'D', 0, ' ', 0, 'B', 0, 'o', 0, 'o', 0, 't', 0, 'l', 0, 'o', 0, 'a', 0,
'd', 0, 'e', 0, 'r', 0
};
const uint8_t sdSerial[] = {
0x16, // Size,
0x03, // Descriptor type
'1',0,'2',0,'3',0,'4',0,'5',0,'6',0,'7',0,'8',0,'9',0,'0',0
};
const uint8_t sdLangID[] = {
0x04, // Size,
0x03, // Descriptor type
0x09, 0x04
};
void USB_PMA2Buffer(uint8_t EPn) {
uint8_t Count = RxTxBuffer[EPn].RXL = (_GetEPRxCount(EPn) & 0x3FF);
uint32_t *Address = (uint32_t *) (PMAAddr + _GetEPRxAddr(EPn) * 2);
uint16_t *Destination = (uint16_t *) RxTxBuffer[EPn].RXB;
for (uint8_t i = 0; i < Count; i++) {
*(uint16_t *) Destination = *(uint16_t *) Address;
Destination++;
Address++;
}
}
void USB_Buffer2PMA(uint8_t EPn) {
uint32_t *Destination;
uint8_t Count;
Count = RxTxBuffer[EPn].TXL <= RxTxBuffer[EPn].MaxPacketSize ? RxTxBuffer[EPn].TXL : RxTxBuffer[EPn].MaxPacketSize;
_SetEPTxCount(EPn, Count);
Destination = (uint32_t *) (PMAAddr + _GetEPTxAddr(EPn) * 2);
for (uint8_t i = 0; i < (Count + 1) / 2; i++) {
*(uint32_t *) Destination = *(uint16_t *) RxTxBuffer[EPn].TXB;
Destination++;
RxTxBuffer[EPn].TXB++;
}
RxTxBuffer[EPn].TXL -= Count;
}
void USB_SendData(uint8_t EPn, uint16_t *Data, uint16_t Length) {
if (EPn > 0 && !DeviceConfigured) {
return;
}
RxTxBuffer[EPn].TXL = Length;
RxTxBuffer[EPn].TXB = Data;
if (Length > 0) {
USB_Buffer2PMA(EPn);
} else {
_SetEPTxCount(EPn, 0);
}
_SetEPTxValid(EPn);
}
void USB_Shutdown() {
bit_set(RCC->APB2ENR, RCC_APB2ENR_IOPAEN);
// Disable USB IRQ
NVIC_DisableIRQ(USB_LP_CAN1_RX0_IRQn);
_SetISTR(0);
DeviceConfigured = DeviceStatus = 0;
_EPHandler = NULL;
_USBResetHandler = NULL;
// Turn USB Macrocell off (FRES + PWDN)
_SetCNTR(0x03);
// PA_12 output mode: General purpose output open drain (b01)
bit_set(GPIOA->CRH, GPIO_CRH_CNF12_0);
bit_clear(GPIOA->CRH, GPIO_CRH_CNF12_1);
// Set PA_12 to output
bit_set(GPIOA->CRH, GPIO_CRH_MODE12);// PA_12 set as: Output mode, max speed 50 MHz.
// Sinks A12 to GND
GPIOA->BRR = GPIO_BRR_BR12;
// Disable USB Clock on APB1
bit_clear(RCC->APB1ENR, RCC_APB1ENR_USBEN);
}
static void USB_TurnOn() {
bit_set(RCC->APB2ENR, RCC_APB2ENR_IOPAEN);
// PA_12 output mode: General purpose Input Float (b01)
bit_set(GPIOA->CRH, GPIO_CRH_CNF12_0);
bit_clear(GPIOA->CRH, GPIO_CRH_CNF12_1);
// Set PA_12 to Input mode
bit_clear(GPIOA->CRH, GPIO_CRH_MODE12);
}
void USB_Init(void (*EPHandlerPtr)(uint16_t), void (*ResetHandlerPtr)(void)) {
// Reset RX and TX lengths inside RxTxBuffer struct for all endpoints
for(int i = 0; i < MAX_EP_NUM; i++) {
RxTxBuffer[i].RXL = RxTxBuffer[i].TXL = 0;
}
_EPHandler = EPHandlerPtr;
_USBResetHandler = ResetHandlerPtr;
USB_TurnOn();
DeviceConfigured = DeviceStatus = 0;
bit_set(RCC->APB1ENR, RCC_APB1ENR_USBEN);
NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn);
/*** CNTR_PWDN = 0 ***/
_SetCNTR(CNTR_FRES);
/* The following sequence is recommended:
1- FRES = 0
2- Wait until RESET flag = 1 (polling)
3- clear ISTR register */
/*** CNTR_FRES = 0 ***/
_SetCNTR(0);
/* Wait until RESET flag = 1 (polling) */
while (!(_GetISTR() & ISTR_RESET));
/*** Clear pending interrupts ***/
_SetISTR(0);
/*** Set interrupt mask ***/
_SetCNTR(CNTR_RESETM | CNTR_SUSPM | CNTR_WKUPM);
}
uint16_t USB_IsDeviceConfigured() {
return DeviceConfigured;
}
void USB_LP_CAN1_RX0_IRQHandler() {
// Handle Reset
if (_GetISTR() & ISTR_RESET) {
_SetISTR(_GetISTR() & CLR_RESET);
if(_USBResetHandler) {
_USBResetHandler();
}
return;
}
// Handle EP data
if (_GetISTR() & ISTR_CTR) { // Handle data on EP
if(_EPHandler) {
_EPHandler(_GetISTR());
}
_SetISTR(_GetISTR() & CLR_CTR);
return;
}
// Handle DOVR
if (_GetISTR() & ISTR_DOVR) {
_SetISTR(_GetISTR() & CLR_DOVR);
return;
}
// Handle Suspend
if (_GetISTR() & ISTR_SUSP) {
_SetISTR(_GetISTR() & CLR_SUSP);
// If device address is assigned, then reset it
if (_GetDADDR() & 0x007f) {
_SetDADDR(0);
_SetCNTR(_GetCNTR() & ~CNTR_SUSPM);
}
return;
}
// Handle Error
if (_GetISTR() & ISTR_ERR) {
_SetISTR(_GetISTR() & CLR_ERR);
return;
}
// Handle Wakeup
if (_GetISTR() & ISTR_WKUP) {
_SetISTR(_GetISTR() & CLR_WKUP);
return;
}
// Handle SOF
if (_GetISTR() & ISTR_SOF) {
_SetISTR(_GetISTR() & CLR_SOF);
return;
}
// Handle ESOF
if (_GetISTR() & ISTR_ESOF) {
_SetISTR(_GetISTR() & CLR_ESOF);
return;
}
// Default to clear all interrupt flags
_SetISTR(0);
}