USB Host Driver with Socket Modem support. Works with revision 323 of mbed-src but broken with any later version.

Dependencies:   FATFileSystem

Fork of F401RE-USBHost by Norimasa Okamoto

Revision:
18:61554f238584
Parent:
17:4a710e2ba162
Child:
21:b4d53cc6d6ac
--- a/USBHost/USBHALHost_KL46Z.cpp	Mon Jun 23 20:48:53 2014 +0900
+++ b/USBHost/USBHALHost_KL46Z.cpp	Tue Jul 01 18:33:31 2014 +0900
@@ -1,13 +1,8 @@
 // Simple USBHost for FRDM-KL46Z
 #if defined(TARGET_KL46Z)||defined(TARGET_KL25Z)||defined(TARGET_K64F)
-#include "USBHALHost_KL46Z.h"
+#include "USBHALHost.h"
 #include <algorithm>
 
-template <bool>struct CtAssert;
-template <>struct CtAssert<true> {};
-#define CTASSERT(A) CtAssert<A>();
-
-
 #ifdef _USB_DBG
 #define USB_DBG(...) do{fprintf(stderr,"[%s@%d] ",__PRETTY_FUNCTION__,__LINE__);fprintf(stderr,__VA_ARGS__);fprintf(stderr,"\n");} while(0);
 #define USB_DBG_HEX(A,B) debug_hex(A,B)
@@ -17,13 +12,11 @@
 #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
+#undef USB_TEST_ASSERT
+extern void usb_test_assert_internal(const char *expr, const char *file, int line);
+#define USB_TEST_ASSERT(EXPR) while(!(EXPR)){usb_test_assert_internal(#EXPR,__FILE__,__LINE__);}
+
+#define USB_TRACE1(A) while(0)
 
 #define USB_INFO(...) do{fprintf(stderr,__VA_ARGS__);fprintf(stderr,"\n");}while(0);
 
@@ -156,7 +149,7 @@
         }
     }
     wait_ms(100);
-    USB_TEST_ASSERT_FALSE(USB0->CTL & USB_CTL_SE0_MASK);
+    USB_TEST_ASSERT(!(USB0->CTL & USB_CTL_SE0_MASK));
     root_lowSpeed = (USB0->CTL & USB_CTL_JSTATE_MASK) ? false : true;
     return root_lowSpeed;
 }
@@ -181,6 +174,8 @@
 
 int USBHALHost::token_setup(USBEndpoint* ep, SETUP_PACKET* setup, uint16_t wLength) {
     USBDeviceConnected* dev = ep->getDevice();
+    setAddr(dev->getAddress(), dev->getSpeed());
+
     for(int retry = 0;; retry++) {
         token_ready();
         USB0->ENDPOINT[0].ENDPT = (root_lowSpeed ? USB_ENDPT_HOSTWOHUB_MASK : 0x00) |
@@ -220,6 +215,10 @@
 }
 
 int USBHALHost::token_in(USBEndpoint* ep, uint8_t* data, int size, int retryLimit) {
+    USBDeviceConnected* dev = ep->getDevice();
+    setAddr(dev->getAddress(), dev->getSpeed());
+    setEndpoint();
+
     for(int retry = 0;; retry++) {
         token_ready();
         int idx = EP0_BDT_IDX(RX, rx_ptr);
@@ -268,6 +267,11 @@
 }
 
 int USBHALHost::token_out(USBEndpoint* ep, const uint8_t* data, int size, int retryLimit) {
+    USBDeviceConnected* dev = ep->getDevice();
+    USB_TEST_ASSERT(dev);
+    setAddr(dev->getAddress(), dev->getSpeed());
+    setEndpoint();
+
     for(int retry = 0;; retry++) {
         token_ready();
         int idx = EP0_BDT_IDX(TX, tx_ptr);
@@ -305,7 +309,37 @@
     }
 }
 
+void USBHALHost::multi_token_inNB(USBEndpoint* ep, uint8_t* data, int size) {
+    USB_TRACE1(size);
+    USB_TEST_ASSERT(ep->getState() != USB_TYPE_PROCESSING);
+    ep->setBuffer(data, size);
+    ep->setState(USB_TYPE_PROCESSING);
+}
+
+USB_TYPE USBHALHost::multi_token_inNB_result(USBEndpoint* ep) {
+    USB_TEST_ASSERT(ep->getState() == USB_TYPE_PROCESSING);
+    uint8_t* buf = ep->getBufStart();
+    int size = ep->getBufSize();
+    int result = multi_token_in(ep, buf, size, false);
+    USB_TRACE1(result);
+    if (result < 0) {
+        return USB_TYPE_PROCESSING;
+    }
+    ep->setLengthTransferred(result);
+    ep->setState(USB_TYPE_IDLE);
+    return USB_TYPE_OK;
+}
+
+void USBHALHost::setToggle(USBEndpoint* ep, uint8_t toggle) {
+    USB_TEST_ASSERT(toggle == 1);
+    ep->setData01(toggle == 0 ? DATA0 : DATA1);
+}
+
 int USBHALHost::token_iso_in(USBEndpoint* ep, uint8_t* data, int size) {
+    USBDeviceConnected* dev = ep->getDevice();
+    USB_TEST_ASSERT(dev);
+    setAddr(dev->getAddress());
+
     while(USB0->CTL & USB_CTL_TXSUSPENDTOKENBUSY_MASK); // TOKEN_BUSY ?
     USB0->ISTAT |= USB_ISTAT_SOFTOK_MASK; // Clear SOF
     while (!(USB0->ISTAT & USB_ISTAT_SOFTOK_MASK));
@@ -371,7 +405,7 @@
     return read_len;
 }
 
-int USBHALHost::multi_token_out(USBEndpoint* ep, const uint8_t* data, size_t total, bool block) {
+int USBHALHost::multi_token_out(USBEndpoint* ep, const uint8_t* data, size_t total) {
     if (total == 0) {
         return token_out(ep);
     }