Skip to content

Commit fe4d51f

Browse files
jcbudacmaglie
authored andcommittedSep 9, 2015
Update for host and Device USB
1 parent 6608091 commit fe4d51f

File tree

4 files changed

+60
-50
lines changed

4 files changed

+60
-50
lines changed
 

‎cores/arduino/USB/USBCore.cpp

+19-41
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ USBDevice_SAMD21G18x usbd;
2929
static char isRemoteWakeUpEnabled = 0;
3030
static char isEndpointHalt = 0;
3131

32+
extern void (*gpf_isr)(void);
33+
34+
// USB_Handler ISR
35+
extern "C" void UDD_Handler(void) {
36+
USBDevice.ISRHandler();
37+
}
38+
39+
40+
3241
const uint16_t STRING_LANGUAGE[2] = {
3342
(3<<8) | (2+2),
3443
0x0409 // English
@@ -285,6 +294,8 @@ void USBDeviceClass::init()
285294
while (GCLK->STATUS.bit.SYNCBUSY)
286295
;
287296

297+
UHD_SetStack(&UDD_Handler);
298+
288299
// Reset USB Device
289300
usbd.reset();
290301

@@ -388,9 +399,6 @@ void USBDeviceClass::initEP(uint32_t ep, uint32_t config)
388399
// Release OUT EP
389400
usbd.epBank0SetMultiPacketSize(ep, 64);
390401
usbd.epBank0SetByteCount(ep, 0);
391-
392-
// The RAM Buffer is empty: we can receive data
393-
//usbd.epBank0ResetReady(ep);
394402
}
395403
else if (config == (USB_ENDPOINT_TYPE_BULK | USB_ENDPOINT_IN(0)))
396404
{
@@ -405,12 +413,12 @@ void USBDeviceClass::initEP(uint32_t ep, uint32_t config)
405413
else if (config == USB_ENDPOINT_TYPE_CONTROL)
406414
{
407415
// XXX: Needed?
408-
usbd.epBank0DisableAutoZLP(ep);
409-
usbd.epBank1DisableAutoZLP(ep);
416+
// usbd.epBank0DisableAutoZLP(ep);
417+
// usbd.epBank1DisableAutoZLP(ep);
410418

411419
// Setup Control OUT
412420
usbd.epBank0SetSize(ep, 64);
413-
usbd.epBank0SetAddress(ep, &udd_ep_out_cache_buffer[0]);
421+
usbd.epBank0SetAddress(ep, &udd_ep_out_cache_buffer[ep]);
414422
usbd.epBank0SetType(ep, 1); // CONTROL OUT / SETUP
415423

416424
// Setup Control IN
@@ -424,8 +432,6 @@ void USBDeviceClass::initEP(uint32_t ep, uint32_t config)
424432

425433
// NAK on endpoint OUT, the bank is full.
426434
usbd.epBank0SetReady(ep);
427-
// NAK on endpoint IN, the bank is not yet filled in.
428-
//usbd.epBank1ResetReady(ep);
429435
}
430436
}
431437

@@ -461,7 +467,6 @@ bool USBDeviceClass::connected()
461467
uint32_t USBDeviceClass::recvControl(void *_data, uint32_t len)
462468
{
463469
uint8_t *data = reinterpret_cast<uint8_t *>(_data);
464-
// NO RXOUT ???????
465470

466471
// The RAM Buffer is empty: we can receive data
467472
usbd.epBank0ResetReady(0);
@@ -499,17 +504,8 @@ uint32_t USBDeviceClass::recv(uint32_t ep, void *_data, uint32_t len)
499504

500505
usbd.epBank0DisableTransferComplete(ep);
501506

502-
// NAK on endpoint OUT, the bank is full.
503-
//usbd.epBank0SetReady(CDC_ENDPOINT_OUT);
504-
505507
memcpy(_data, udd_ep_out_cache_buffer[ep], len);
506508

507-
// uint8_t *buffer = udd_ep_out_cache_buffer[ep];
508-
// uint8_t *data = reinterpret_cast<uint8_t *>(_data);
509-
// for (uint32_t i=0; i<len; i++) {
510-
// data[i] = buffer[i];
511-
// }
512-
513509
// release empty buffer
514510
if (len && !available(ep)) {
515511
// The RAM Buffer is empty: we can receive data
@@ -539,40 +535,23 @@ uint8_t USBDeviceClass::armRecvCtrlOUT(uint32_t ep, uint32_t len)
539535
usbd.epBank0SetAddress(ep, &udd_ep_out_cache_buffer[ep]);
540536
usbd.epBank0SetMultiPacketSize(ep, 8);
541537
usbd.epBank0SetByteCount(ep, 0);
542-
//usbd.epBank0ResetReady(0);
543-
//while (!usbd.epBank0IsTransferComplete(ep)) {}
544-
//while (usbd.epBank0IsReady(ep)) {}
545538

546-
//usbd.epBank0SetByteCount(0, 0);
547-
//usbd.epBank0SetMultiPacketSize(0, 8);
548539
usbd.epBank0ResetReady(ep);
549540

550541
// Wait OUT
551542
while (!usbd.epBank0IsReady(ep)) {}
552-
while (!usbd.epBank0IsTransferComplete(ep)) {} // XXX: while(USB->DEVICE.DeviceEndpoint[ep].EPINTFLAG.bit.TRCPT == 0);
543+
while (!usbd.epBank0IsTransferComplete(ep)) {}
553544
return usbd.epBank0ByteCount(ep);
554545
}
555546

556547
uint8_t USBDeviceClass::armRecv(uint32_t ep, uint32_t len)
557548
{
558-
//usbd.epBank0SetSize(ep, 64);
559-
//usbd.epBank0SetAddress(ep, &udd_ep_out_cache_buffer[ep]);
560-
//usbd.epBank0SetMultiPacketSize(ep, 64); // XXX: Should be "len"?
561549
uint16_t count = usbd.epBank0ByteCount(ep);
562550
if (count >= 64) {
563551
usbd.epBank0SetByteCount(ep, count - 64);
564552
} else {
565553
usbd.epBank0SetByteCount(ep, 0);
566554
}
567-
// The RAM Buffer is empty: we can receive data
568-
//usbd.epBank0ResetReady(ep);
569-
570-
// Wait for transfer to complete
571-
//while (!usbd.epBank0IsTransferComplete(ep)) {}
572-
//while (usbd.epBank0IsReady(ep)) {}
573-
// NAK on endpoint OUT, the bank is full.
574-
//usbd.epBank0ResetReady(ep);
575-
576555
return usbd.epBank0ByteCount(ep);
577556
}
578557

@@ -837,8 +816,7 @@ void USBDeviceClass::ISRHandler()
837816
stall(0);
838817
}
839818

840-
// XXX: Should be really cleared?
841-
if (usbd.epBank1IsStalled(0)) // XXX:(USB->DEVICE.DeviceEndpoint[0].EPINTFLAG.bit.STALL)
819+
if (usbd.epBank1IsStalled(0))
842820
{
843821
usbd.epBank1AckStalled(0);
844822

@@ -878,6 +856,6 @@ void USBDeviceClass::ISRHandler()
878856
USBDeviceClass USBDevice;
879857

880858
// USB_Handler ISR
881-
extern "C" void USB_Handler(void) {
882-
USBDevice.ISRHandler();
883-
}
859+
// extern "C" void USB_Handler(void) {
860+
// USBDevice.ISRHandler();
861+
// }

‎cores/arduino/USB/USB_interrupt.c

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
Copyright (c) 2014 Arduino LLC. All right reserved.
3+
4+
This library is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU Lesser General Public
6+
License as published by the Free Software Foundation; either
7+
version 2.1 of the License, or (at your option) any later version.
8+
9+
This library is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
See the GNU Lesser General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public
15+
License along with this library; if not, write to the Free Software
16+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
void (*gpf_isr)(void) = (0UL);
20+
21+
void USB_Handler( void )
22+
{
23+
if (gpf_isr)
24+
gpf_isr();
25+
}
26+
27+
void UHD_SetStack(void (*pf_isr)(void))
28+
{
29+
gpf_isr = pf_isr;
30+
}

‎cores/arduino/USB/samd21_host.c

+9-7
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,13 @@
3434
//#define TRACE_UOTGHS_HOST(x) x
3535
#define TRACE_UOTGHS_HOST(x)
3636

37-
//extern void (*gpf_isr)(void);
38-
3937
// Handle UOTGHS Host driver state
4038
static uhd_vbus_state_t uhd_state = UHD_STATE_NO_VBUS;
4139

4240
__attribute__((__aligned__(4))) volatile UsbHostDescriptor usb_pipe_table[USB_EPT_NUM];
4341

4442
extern void (*gpf_isr)(void);
4543

46-
void UHD_SetStack(void (*pf_isr)(void))
47-
{
48-
gpf_isr = pf_isr;
49-
}
5044

5145
// NVM Software Calibration Area Mapping
5246
// USB TRANSN calibration value. Should be written to the USB PADCAL register.
@@ -180,7 +174,6 @@ void UHD_Init(void)
180174
/**
181175
* \brief Interrupt sub routine for USB Host state machine management.
182176
*/
183-
//static void UHD_ISR(void)
184177
void UHD_Handler(void)
185178
{
186179
uint16_t flags;
@@ -518,4 +511,13 @@ uint32_t UHD_Pipe_Is_Transfer_Complete(uint32_t ul_pipe, uint32_t ul_token_type)
518511

519512
return 0;
520513
}
514+
515+
516+
517+
518+
// USB_Handler ISR
519+
// void USB_Handler(void) {
520+
// UHD_Handler();
521+
// }
522+
521523
#endif // HOST_DEFINED

‎libraries/USBHost/src/Usb.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ e-mail : support@circuitsathome.com
2121
#include "Usb.h"
2222

2323

24-
#ifdef ARDUINO_SAMD_ZERO
24+
//#ifdef ARDUINO_SAMD_ZERO
2525

2626
static uint32_t usb_error = 0;
2727
static uint32_t usb_task_state = USB_DETACHED_SUBSTATE_INITIALIZE;
@@ -854,4 +854,4 @@ uint32_t USBHost::setConf(uint32_t addr, uint32_t ep, uint32_t conf_value) {
854854
return ( ctrlReq(addr, ep, bmREQ_SET, USB_REQUEST_SET_CONFIGURATION, conf_value, 0x00, 0x0000, 0x0000, 0x0000, NULL, NULL));
855855
}
856856

857-
#endif //ARDUINO_SAMD_ZERO
857+
//#endif //ARDUINO_SAMD_ZERO

0 commit comments

Comments
 (0)
Please sign in to comment.