Skip to content

Commit 30ef311

Browse files
committed
Issue#4250: Fix USB driver for Kinetis devices
Move memory allocation to be done at a different location. Currently allocated in a function that is called from the interrupt handler. Signed-off-by: Mahadevan Mahesh <[email protected]>
1 parent 2886f0b commit 30ef311

File tree

2 files changed

+26
-101
lines changed

2 files changed

+26
-101
lines changed

features/unsupported/USBDevice/USBDevice/USBEndpoints_KL25Z.h

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1717
*/
1818

19-
#define NUMBER_OF_LOGICAL_ENDPOINTS (16)
19+
#define NUMBER_OF_LOGICAL_ENDPOINTS (4)
2020
#define NUMBER_OF_PHYSICAL_ENDPOINTS (NUMBER_OF_LOGICAL_ENDPOINTS * 2)
2121

2222
/* Define physical endpoint numbers */
@@ -31,49 +31,13 @@
3131
#define EP2IN (5)
3232
#define EP3OUT (6)
3333
#define EP3IN (7)
34-
#define EP4OUT (8)
35-
#define EP4IN (9)
36-
#define EP5OUT (10)
37-
#define EP5IN (11)
38-
#define EP6OUT (12)
39-
#define EP6IN (13)
40-
#define EP7OUT (14)
41-
#define EP7IN (15)
42-
#define EP8OUT (16)
43-
#define EP8IN (17)
44-
#define EP9OUT (18)
45-
#define EP9IN (19)
46-
#define EP10OUT (20)
47-
#define EP10IN (21)
48-
#define EP11OUT (22)
49-
#define EP11IN (23)
50-
#define EP12OUT (24)
51-
#define EP12IN (25)
52-
#define EP13OUT (26)
53-
#define EP13IN (27)
54-
#define EP14OUT (28)
55-
#define EP14IN (29)
56-
#define EP15OUT (30)
57-
#define EP15IN (31)
5834

5935
/* Maximum Packet sizes */
6036

6137
#define MAX_PACKET_SIZE_EP0 (64)
6238
#define MAX_PACKET_SIZE_EP1 (64)
6339
#define MAX_PACKET_SIZE_EP2 (64)
6440
#define MAX_PACKET_SIZE_EP3 (1023)
65-
#define MAX_PACKET_SIZE_EP4 (64)
66-
#define MAX_PACKET_SIZE_EP5 (64)
67-
#define MAX_PACKET_SIZE_EP6 (64)
68-
#define MAX_PACKET_SIZE_EP7 (64)
69-
#define MAX_PACKET_SIZE_EP8 (64)
70-
#define MAX_PACKET_SIZE_EP9 (64)
71-
#define MAX_PACKET_SIZE_EP10 (64)
72-
#define MAX_PACKET_SIZE_EP11 (64)
73-
#define MAX_PACKET_SIZE_EP12 (64)
74-
#define MAX_PACKET_SIZE_EP13 (64)
75-
#define MAX_PACKET_SIZE_EP14 (64)
76-
#define MAX_PACKET_SIZE_EP15 (64)
7741

7842
/* Generic endpoints - intended to be portable accross devices */
7943
/* and be suitable for simple USB devices. */

features/unsupported/USBDevice/USBDevice/USBHAL_KL25Z.cpp

Lines changed: 25 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,12 @@ typedef struct BDT {
6464
uint32_t address; // Addr
6565
} BDT;
6666

67-
6867
// there are:
69-
// * 16 bidirectionnal endpt -> 32 physical endpt
70-
// * as there are ODD and EVEN buffer -> 32*2 bdt
68+
// * 4 bidirectionnal endpt -> 8 physical endpt
69+
// * as there are ODD and EVEN buffer -> 8*2 bdt
7170
MBED_ALIGN(512) BDT bdt[NUMBER_OF_PHYSICAL_ENDPOINTS * 2]; // 512 bytes aligned!
7271

73-
uint8_t * endpoint_buffer[(NUMBER_OF_PHYSICAL_ENDPOINTS - 2) * 2];
74-
uint8_t * endpoint_buffer_iso[2*2];
72+
uint8_t * endpoint_buffer[NUMBER_OF_PHYSICAL_ENDPOINTS * 2];
7573

7674
static uint8_t set_addr = 0;
7775
static uint8_t addr = 0;
@@ -102,28 +100,6 @@ USBHAL::USBHAL(void) {
102100
epCallback[5] = &USBHAL::EP3_IN_callback;
103101
epCallback[6] = &USBHAL::EP4_OUT_callback;
104102
epCallback[7] = &USBHAL::EP4_IN_callback;
105-
epCallback[8] = &USBHAL::EP5_OUT_callback;
106-
epCallback[9] = &USBHAL::EP5_IN_callback;
107-
epCallback[10] = &USBHAL::EP6_OUT_callback;
108-
epCallback[11] = &USBHAL::EP6_IN_callback;
109-
epCallback[12] = &USBHAL::EP7_OUT_callback;
110-
epCallback[13] = &USBHAL::EP7_IN_callback;
111-
epCallback[14] = &USBHAL::EP8_OUT_callback;
112-
epCallback[15] = &USBHAL::EP8_IN_callback;
113-
epCallback[16] = &USBHAL::EP9_OUT_callback;
114-
epCallback[17] = &USBHAL::EP9_IN_callback;
115-
epCallback[18] = &USBHAL::EP10_OUT_callback;
116-
epCallback[19] = &USBHAL::EP10_IN_callback;
117-
epCallback[20] = &USBHAL::EP11_OUT_callback;
118-
epCallback[21] = &USBHAL::EP11_IN_callback;
119-
epCallback[22] = &USBHAL::EP12_OUT_callback;
120-
epCallback[23] = &USBHAL::EP12_IN_callback;
121-
epCallback[24] = &USBHAL::EP13_OUT_callback;
122-
epCallback[25] = &USBHAL::EP13_IN_callback;
123-
epCallback[26] = &USBHAL::EP14_OUT_callback;
124-
epCallback[27] = &USBHAL::EP14_IN_callback;
125-
epCallback[28] = &USBHAL::EP15_OUT_callback;
126-
epCallback[29] = &USBHAL::EP15_IN_callback;
127103

128104
#if defined(TARGET_KL43Z) || defined(TARGET_K22F) || defined(TARGET_K64F)
129105
// enable USBFS clock
@@ -161,6 +137,10 @@ USBHAL::USBHAL(void) {
161137
USB0->USBCTRL &= ~(USB_USBCTRL_PDE_MASK | USB_USBCTRL_SUSP_MASK);
162138

163139
USB0->USBTRC0 |= 0x40;
140+
141+
/* Allocate control endpoint buffers */
142+
endpoint_buffer[EP_BDT_IDX(0, TX, ODD)] = (uint8_t *)malloc(MAX_PACKET_SIZE_EP0);
143+
endpoint_buffer[EP_BDT_IDX(0, RX, ODD)] = (uint8_t *)malloc(MAX_PACKET_SIZE_EP0);
164144
}
165145

166146
USBHAL::~USBHAL(void) { }
@@ -170,6 +150,15 @@ void USBHAL::connect(void) {
170150
USB0->CTL |= USB_CTL_USBENSOFEN_MASK;
171151
// Pull up enable
172152
USB0->CONTROL |= USB_CONTROL_DPPULLUPNONOTG_MASK;
153+
154+
// Allocate endpoint buffers; do allocate control endpoint buffers
155+
for (int i = 4; i < (NUMBER_OF_PHYSICAL_ENDPOINTS * 2); i++) {
156+
if ((i == EPISO_OUT) || (i == EPISO_IN)) {
157+
endpoint_buffer[i] = (uint8_t *)malloc(MAX_PACKET_SIZE_EPISO);
158+
} else {
159+
endpoint_buffer[i] = (uint8_t *)malloc(MAX_PACKET_SIZE_EPBULK);
160+
}
161+
}
173162
}
174163

175164
void USBHAL::disconnect(void) {
@@ -178,15 +167,11 @@ void USBHAL::disconnect(void) {
178167
// Pull up disable
179168
USB0->CONTROL &= ~USB_CONTROL_DPPULLUPNONOTG_MASK;
180169

181-
//Free buffers if required:
182-
for (int i = 0; i<(NUMBER_OF_PHYSICAL_ENDPOINTS - 2) * 2; i++) {
170+
//Free buffers if required; do not free the control endpoint buffers
171+
for (int i = 4; i < (NUMBER_OF_PHYSICAL_ENDPOINTS * 2); i++) {
183172
free(endpoint_buffer[i]);
184173
endpoint_buffer[i] = NULL;
185174
}
186-
free(endpoint_buffer_iso[2]);
187-
endpoint_buffer_iso[2] = NULL;
188-
free(endpoint_buffer_iso[0]);
189-
endpoint_buffer_iso[0] = NULL;
190175
}
191176

192177
void USBHAL::configureDevice(void) {
@@ -217,25 +202,12 @@ bool USBHAL::realiseEndpoint(uint8_t endpoint, uint32_t maxPacket, uint32_t flag
217202

218203
if ((flags & ISOCHRONOUS) == 0) {
219204
handshake_flag = USB_ENDPT_EPHSHK_MASK;
220-
if (IN_EP(endpoint)) {
221-
if (endpoint_buffer[EP_BDT_IDX(log_endpoint, TX, ODD)] == NULL)
222-
endpoint_buffer[EP_BDT_IDX(log_endpoint, TX, ODD)] = (uint8_t *) malloc (64);
223-
buf = &endpoint_buffer[EP_BDT_IDX(log_endpoint, TX, ODD)][0];
224-
} else {
225-
if (endpoint_buffer[EP_BDT_IDX(log_endpoint, RX, ODD)] == NULL)
226-
endpoint_buffer[EP_BDT_IDX(log_endpoint, RX, ODD)] = (uint8_t *) malloc (64);
227-
buf = &endpoint_buffer[EP_BDT_IDX(log_endpoint, RX, ODD)][0];
228-
}
205+
}
206+
207+
if (IN_EP(endpoint)) {
208+
buf = &endpoint_buffer[EP_BDT_IDX(log_endpoint, TX, ODD)][0];
229209
} else {
230-
if (IN_EP(endpoint)) {
231-
if (endpoint_buffer_iso[2] == NULL)
232-
endpoint_buffer_iso[2] = (uint8_t *) malloc (1023);
233-
buf = &endpoint_buffer_iso[2][0];
234-
} else {
235-
if (endpoint_buffer_iso[0] == NULL)
236-
endpoint_buffer_iso[0] = (uint8_t *) malloc (1023);
237-
buf = &endpoint_buffer_iso[0][0];
238-
}
210+
buf = &endpoint_buffer[EP_BDT_IDX(log_endpoint, RX, ODD)][0];
239211
}
240212

241213
// IN endpt -> device to host (TX)
@@ -329,12 +301,7 @@ EP_STATUS USBHAL::endpointReadResult(uint8_t endpoint, uint8_t * buffer, uint32_
329301
setup = 1;
330302
}
331303

332-
// non iso endpoint
333-
if (not_iso) {
334-
ep_buf = endpoint_buffer[idx];
335-
} else {
336-
ep_buf = endpoint_buffer_iso[0];
337-
}
304+
ep_buf = endpoint_buffer[idx];
338305

339306
for (n = 0; n < sz; n++) {
340307
buffer[n] = ep_buf[n];
@@ -377,13 +344,7 @@ EP_STATUS USBHAL::endpointWrite(uint8_t endpoint, uint8_t *data, uint32_t size)
377344
idx = EP_BDT_IDX(PHY_TO_LOG(endpoint), TX, 0);
378345
bdt[idx].byte_count = size;
379346

380-
381-
// non iso endpoint
382-
if (USB0->ENDPOINT[PHY_TO_LOG(endpoint)].ENDPT & USB_ENDPT_EPHSHK_MASK) {
383-
ep_buf = endpoint_buffer[idx];
384-
} else {
385-
ep_buf = endpoint_buffer_iso[2];
386-
}
347+
ep_buf = endpoint_buffer[idx];
387348

388349
for (n = 0; n < size; n++) {
389350
ep_buf[n] = data[n];

0 commit comments

Comments
 (0)