Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: F401RE-USBHostMIDI_RecieveExample
Fork of F401RE-USBHost by
Diff: USBHost/USBHALHost.cpp
- Revision:
- 8:6463cd1964c0
- Parent:
- 7:9a20482c9a7a
- Child:
- 9:7f9f64cf5ded
--- a/USBHost/USBHALHost.cpp Tue Jan 28 06:50:12 2014 +0000
+++ b/USBHost/USBHALHost.cpp Fri Jan 31 13:45:07 2014 +0000
@@ -1,6 +1,5 @@
// Simple USBHost for FRDM-KL46Z
#include "USBHALHost.h"
-#include <algorithm>
template <bool>struct CtAssert;
template <>struct CtAssert<true> {};
@@ -16,8 +15,13 @@
#define USB_DBG_HEX(A,B) while(0)
#endif
+#ifdef _USB_TEST
#define USB_TEST_ASSERT(A) while(!(A)){fprintf(stderr,"\n\n%s@%d %s ASSERT!\n\n",__PRETTY_FUNCTION__,__LINE__,#A);exit(1);};
#define USB_TEST_ASSERT_FALSE(A) USB_TEST_ASSERT(!(A))
+#else
+#define USB_TEST_ASSERT(A) while(0)
+#define USB_TEST_ASSERT_FALSE(A) while(0)
+#endif
#define BD_OWN_MASK (1<<7)
#define BD_DATA01_MASK (1<<6)
@@ -98,7 +102,7 @@
NVIC_SetVector(USB0_IRQn, (uint32_t)_usbisr);
NVIC_EnableIRQ(USB0_IRQn);
- wait_attach();
+ bool lowSpeed = wait_attach();
for(int retry = 2; retry > 0; retry--) {
// Enable RESET
@@ -123,7 +127,7 @@
USB_ERREN_DMAERREN_MASK|
USB_ERREN_BTSERREN_MASK;
- if (enumeration()) {
+ if (addDevice(0, 0, lowSpeed)) {
break;
}
USB_DBG("retry=%d", retry);
@@ -131,27 +135,24 @@
}
}
-void USBHALHost::wait_attach() {
+bool USBHALHost::wait_attach() {
attach_done = false;
USB0->INTEN = USB_INTEN_ATTACHEN_MASK;
while(!attach_done);
wait_ms(100);
USB_TEST_ASSERT_FALSE(USB0->CTL & USB_CTL_SE0_MASK);
- lowSpeed = (USB0->CTL & USB_CTL_JSTATE_MASK) ? false : true;
- if (lowSpeed) { // low speed
- USB0->ENDPOINT[0].ENDPT |= USB_ENDPT_HOSTWOHUB_MASK;
- }
- USB_DBG("lowSpeed=%d", lowSpeed);
+ root_lowSpeed = (USB0->CTL & USB_CTL_JSTATE_MASK) ? false : true;
+ return root_lowSpeed;
}
-void USBHALHost::setAddr(int _addr) {
- USB0->ADDR = (lowSpeed ? USB_ADDR_LSEN_MASK : 0x00) | USB_ADDR_ADDR(_addr);
+void USBHALHost::setAddr(int _addr, bool _lowSpeed) {
+ USB0->ADDR = (_lowSpeed ? USB_ADDR_LSEN_MASK : 0x00) | USB_ADDR_ADDR(_addr);
}
-void USBHALHost::setEndpoint(bool use_retry) {
- USB0->ENDPOINT[0].ENDPT = (lowSpeed ? USB_ENDPT_HOSTWOHUB_MASK : 0x00)|
+void USBHALHost::setEndpoint() {
+ USB0->ENDPOINT[0].ENDPT = (root_lowSpeed ? USB_ENDPT_HOSTWOHUB_MASK : 0x00)|
+ USB_ENDPT_RETRYDIS_MASK|
USB_ENDPT_EPCTLDIS_MASK|
- (use_retry ? 0x00 : USB_ENDPT_RETRYDIS_MASK)|
USB_ENDPT_EPRXEN_MASK|
USB_ENDPT_EPTXEN_MASK|
USB_ENDPT_EPHSHK_MASK;
@@ -163,9 +164,10 @@
}
int USBHALHost::token_setup(USBEndpoint* ep, SETUP_PACKET* setup, uint16_t wLength) {
+ USBDeviceConnected* dev = ep->getDevice();
for(int retry = 0;; retry++) {
token_ready();
- USB0->ENDPOINT[0].ENDPT = (lowSpeed ? USB_ENDPT_HOSTWOHUB_MASK : 0x00) |
+ USB0->ENDPOINT[0].ENDPT = (root_lowSpeed ? USB_ENDPT_HOSTWOHUB_MASK : 0x00) |
USB_ENDPT_RETRYDIS_MASK|
USB_ENDPT_EPRXEN_MASK|
USB_ENDPT_EPTXEN_MASK|
@@ -219,7 +221,7 @@
USB_TEST_ASSERT(ep->getData01() == LastStatus);
ep->setData01(LastStatus == DATA0 ? DATA1 : DATA0);
if (retry > 0) {
- USB_DBG("len=%d retry=%d %02x", len, retry, prev_LastStatus);
+ //USB_DBG("len=%d retry=%d %02x", len, retry, prev_LastStatus);
}
return len;
} else if (LastStatus == STALL) {
@@ -395,12 +397,16 @@
}
void debug_hex(uint8_t* buf, int size) {
+ int n = 0;
for(int i = 0; i < size; i++) {
fprintf(stderr, "%02x ", buf[i]);
- if (i%16 == 15) {
- fprintf(stderr, "\r\n");
+ if (++n >= 16) {
+ fprintf(stderr, "\n");
+ n = 0;
}
}
- fprintf(stderr, "\r\n");
+ if (n > 0) {
+ fprintf(stderr, "\n");
+ }
}
