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

Files at this revision

API Documentation at this revision

Comitter:
fritz291
Date:
Mon Jun 22 18:49:28 2015 +0000
Parent:
20:9827f135ccb6
Child:
22:eaefd60717c4
Commit message:
USB Host Driver with Socket Modem support

Changed in this revision

USBHost/USBHALHost_KL46Z.cpp Show annotated file Show diff for this revision Revisions of this file
USBHost/USBHALHost_KL46Z.h Show annotated file Show diff for this revision Revisions of this file
USBHostSocketModem/USBHostSocketModem.cpp Show annotated file Show diff for this revision Revisions of this file
USBHostSocketModem/USBHostSocketModem.h Show annotated file Show diff for this revision Revisions of this file
--- a/USBHost/USBHALHost_KL46Z.cpp	Sun May 10 09:55:16 2015 +0000
+++ b/USBHost/USBHALHost_KL46Z.cpp	Mon Jun 22 18:49:28 2015 +0000
@@ -1,5 +1,5 @@
 // Simple USBHost for FRDM-KL46Z
-#if defined(TARGET_KL46Z)||defined(TARGET_KL25Z)||defined(TARGET_K64F)
+#if defined(TARGET_KL46Z)||defined(TARGET_KL25Z)||defined(TARGET_K64F)||defined(TARGET_GAMBIT)
 #include "USBHALHost.h"
 #include <algorithm>
 
@@ -63,8 +63,10 @@
 void USBHALHost::init() {
     // Disable IRQ
     NVIC_DisableIRQ(USB0_IRQn);
-
-#if defined(TARGET_K64F)
+#if defined(TARGET_GAMBIT)||defined(TARGET_K64F)
+    initPins();
+#endif
+#if defined(TARGET_K64F)||defined(TARGET_GAMBIT)
     MPU->CESR=0;
 #endif
 
@@ -515,6 +517,23 @@
     }
 }
 
+void USBHALHost::initPins()
+{
+    //Start with usb hub disabled in reset mode
+    DigitalOut usb_hub_nreset(PTA8,0);
+    //cell_nreset resets the cell radio
+    DigitalOut cell_nreset(PTA2,0);
+    printf("Resetting the radio to prevent processor freeze....\n");
+    printf("Start with USB hub disabled to prevent any USB transaction with Telit Module\n");
+    wait(1);
+    cell_nreset = 1;
+    wait(4);
+    //turn hub on
+    usb_hub_nreset = 1;
+    printf("turning USB hub back on\n");
+    wait(1);
+}
+
 #endif
 
 
--- a/USBHost/USBHALHost_KL46Z.h	Sun May 10 09:55:16 2015 +0000
+++ b/USBHost/USBHALHost_KL46Z.h	Mon Jun 22 18:49:28 2015 +0000
@@ -48,6 +48,8 @@
     USB_TYPE multi_token_inNB_result(USBEndpoint* ep);
     void setToggle(USBEndpoint* ep, uint8_t toggle);
     int token_iso_in(USBEndpoint* ep, uint8_t* data, int size);
+    //Initialize Gambit USB pins
+    virtual void initPins();
 
 private:
     void setAddr(int addr, bool lowSpeed = false);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBHostSocketModem/USBHostSocketModem.cpp	Mon Jun 22 18:49:28 2015 +0000
@@ -0,0 +1,158 @@
+/* mbed USBHost Library
+ * Copyright (c) 2006-2013 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "USBHostSocketModem.h"
+#include "dbg.h"
+
+#define DEVICE_TO_HOST  0x80
+#define HOST_TO_DEVICE  0x00
+
+USBHostSocketModem::USBHostSocketModem()
+{
+    host = USBHost::getHostInst();
+    init();
+}
+
+void USBHostSocketModem::init() 
+{
+    dev_connected = false;
+    dev = NULL;
+    bulk_in = NULL;
+    bulk_out = NULL;
+    int_in = NULL;
+    int_out = NULL;
+    dev_connected = false;
+    sm_intf = -1;
+    sm_device_found = false;
+    dev_connected = false;
+    nb_ep = 0;
+}
+
+
+bool USBHostSocketModem::connected()
+{
+    return dev_connected;
+}
+
+bool USBHostSocketModem::connect()
+{
+
+    if (dev_connected) {
+        return true;
+    }
+
+    for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
+        if ((dev = host->getDevice(i)) != NULL) {
+            
+            USB_DBG("Trying to connect SocketModem device\r\n");
+            if(host->enumerate(dev, this)){
+                printf("didn't fully enumerate\n");
+                break;
+            }
+            
+            if (sm_device_found) {
+                //Set DTR
+                USBHostSocketModem::setDTR();
+                int_out = dev->getEndpoint(int_intf, INTERRUPT_ENDPOINT, OUT);
+                bulk_out = dev->getEndpoint(bulk_intf, BULK_ENDPOINT, OUT);
+                if (bulk_in == NULL){
+                    printf("bulk_in == NULL\n");
+                }
+                bulk_in = dev->getEndpoint(bulk_intf, BULK_ENDPOINT, IN);
+                //int address1 = dev->getEndpoint(msd_intf, BULK_ENDPOINT, OUT).getAddress();
+                //int address2 = dev->getEndpoint(msd_intf, BULK_ENDPOINT, OUT).getAddress();
+                //printf("add1 = %i\n",address1);
+                //printf("add2 = %i\n",address2);
+                /*
+                int_in = dev->getEndpoint(msd_intf, INTERRUPT_ENDPOINT, IN);
+                int_out = dev->getEndpoint(msd_intf, INTERRUPT_ENDPOINT, OUT);
+                */
+                printf("!SocketModem Found!\n");
+                
+                if (!bulk_in || !bulk_out){
+                    continue;
+                }
+
+                USB_INFO("New SocketModem device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, sm_intf);
+                dev->setName("SocketModem", bulk_intf);
+                host->registerDriver(dev, bulk_intf, this, &USBHostSocketModem::init);
+
+                dev_connected = true;
+                return true;
+            }
+        } //if()
+    } //for()
+    init();
+    return false;
+}
+
+/*virtual*/ void USBHostSocketModem::setVidPid(uint16_t vid, uint16_t pid)
+{
+    // we don't check VID/PID for MSD driver
+}
+
+/*virtual*/ bool USBHostSocketModem::parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol) //Must return true if the interface should be parsed
+{
+    printf("Parsing Interface\n");
+    //printf("class = %i\n",intf_class);
+    //printf("subclass = %i\n",intf_subclass);
+    //printf("msd_intf = %i\n",msd_intf);
+    printf("intf_class = %i\n",intf_class);
+    if ((sm_intf == -1) && (intf_class == 0x2))
+    {
+        sm_intf = intf_nb;
+        int_intf = intf_nb;
+        //printf("parse interface: sm_intf = %i\n", sm_intf);
+        return true;
+    }
+    else if ((sm_intf == 0) && (intf_class == 0xa))
+    {
+        sm_intf = intf_nb;
+        bulk_intf = intf_nb;
+        //printf("parse interface: sm_intf = %i\n", sm_intf);
+        return true;
+    }
+    return false;
+}
+
+/*virtual*/ bool USBHostSocketModem::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used
+{
+    printf("Setting up Endpoint\n");
+    if ((intf_nb == int_intf) || (intf_nb == bulk_intf)){
+        /*if (type == INTERRUPT_ENDPOINT)*/
+        if ((type == BULK_ENDPOINT) || (type == INTERRUPT_ENDPOINT)){
+            printf("type == %s\n", type);
+            nb_ep++;
+            if (nb_ep == 2)
+                printf("nb_ep == 2\n");
+                sm_device_found = true;
+            return true;
+        }
+    }
+    return false;
+}
+
+void USBHostSocketModem::setDTR()
+{
+    //Set USB DTR to enable communication
+    uint16_t dtr = 0x0003;
+    int res = host->controlWrite(dev, 0x21, 0x22, dtr, 0, NULL, 0);
+}
+
+
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBHostSocketModem/USBHostSocketModem.h	Mon Jun 22 18:49:28 2015 +0000
@@ -0,0 +1,78 @@
+/* mbed USBHost Library
+ * Copyright (c) 2006-2013 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef USBHOSTSOCKETMODEM_H
+#define USBHOSTSOCKETMODEM_H
+
+#include "USBHostConf.h"
+#include "USBHost.h"
+
+
+/** 
+ * A class to communicate a USB flash disk
+ */
+class USBHostSocketModem : public IUSBEnumerator {
+public:
+    /**
+    * Constructor
+    *
+    * @param rootdir mount name
+    */
+    USBHostSocketModem();
+
+    /**
+    * Check if a MSD device is connected
+    *
+    * @return true if a MSD device is connected
+    */
+    bool connected();
+
+    /**
+     * Try to connect to a MSD device
+     *
+     * @return true if connection was successful
+     */
+    bool connect();
+
+protected:
+    //From IUSBEnumerator
+    virtual void setVidPid(uint16_t vid, uint16_t pid);
+    virtual bool parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol); //Must return true if the interface should be parsed
+    virtual bool useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir); //Must return true if the endpoint will be used
+    
+    //USB CDC initialization 
+    virtual void setDTR();
+        
+private:
+    USBHost * host;
+    USBDeviceConnected * dev;
+    bool dev_connected;
+    USBEndpoint * bulk_in;
+    USBEndpoint * bulk_out;
+    USBEndpoint * int_in;
+    USBEndpoint * int_out;
+    uint8_t nb_ep;
+
+    int sm_intf;
+    int int_intf;
+    int bulk_intf;
+    bool sm_device_found;
+    
+    void init();
+
+};
+
+#endif