Usb read

Dependencies:   FATFileSystem

Fork of F401RE-USBHost by Norimasa Okamoto

Revision:
16:981c3104f6c0
Parent:
11:61843badd06e
Child:
17:4a710e2ba162
--- a/USBHost/USBHALHost_KL46Z.cpp	Fri Jun 13 01:52:44 2014 +0000
+++ b/USBHost/USBHALHost_KL46Z.cpp	Mon Jun 23 20:30:04 2014 +0900
@@ -1,6 +1,7 @@
 // Simple USBHost for FRDM-KL46Z
 #if defined(TARGET_KL46Z)||defined(TARGET_KL25Z)
 #include "USBHALHost_KL46Z.h"
+#include <algorithm>
 
 template <bool>struct CtAssert;
 template <>struct CtAssert<true> {};
@@ -70,6 +71,10 @@
     // Disable IRQ
     NVIC_DisableIRQ(USB0_IRQn);
 
+#if defined(TARGET_K64F)
+    MPU->CESR=0;
+#endif
+
     // choose usb src as PLL
     SIM->SOPT2 |= (SIM_SOPT2_USBSRC_MASK | SIM_SOPT2_PLLFLLSEL_MASK);
 
@@ -337,6 +342,61 @@
     USB0->ISTAT |= USB_ISTAT_SOFTOK_MASK; // clear SOF
 }
 
+int USBHALHost::multi_token_in(USBEndpoint* ep, uint8_t* data, size_t total, bool block) {
+    if (total == 0) {
+        return token_in(ep);
+    }
+    int retryLimit = block ? 10 : 0;
+    int read_len = 0;
+    for(int n = 0; read_len < total; n++) {
+        int size = std::min((int)total-read_len, ep->getSize());
+        int result = token_in(ep, data+read_len, size, retryLimit);
+        if (result < 0) {
+            if (block) {
+                return -1;
+            }
+            if (LastStatus == NAK) {
+                if (n == 0) {
+                    return -1;
+                }
+                break;
+            }
+            return result;
+        }
+        read_len += result;
+        if (result < ep->getSize()) {
+            break;
+        }
+    }
+    return read_len;
+}
+
+int USBHALHost::multi_token_out(USBEndpoint* ep, const uint8_t* data, size_t total, bool block) {
+    if (total == 0) {
+        return token_out(ep);
+    }
+    int write_len = 0;
+    for(int n = 0; write_len < total; n++) {
+        int size = std::min((int)total-write_len, ep->getSize());
+        int result = token_out(ep, data+write_len, size);
+        if (result < 0) {
+            if (LastStatus == NAK) {
+                if (n == 0) {
+                    return -1;
+                }
+                break;
+            }
+            USB_DBG("token_out result=%d %02x", result, LastStatus);
+            return result;
+        }
+        write_len += result;
+        if (result < ep->getSize()) {
+            break;
+        }
+    }
+    return write_len;
+}
+
 void USBHALHost::_usbisr(void) {
     if (instHost) {
         instHost->UsbIrqhandler();