GambitDev / F401RE-USBHost

Dependencies:   FATFileSystem

Fork of F401RE-USBHost by Norimasa Okamoto

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBHostSocketModem.cpp Source File

USBHostSocketModem.cpp

00001 /* mbed USBHost Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "USBHostSocketModem.h"
00018 #include "dbg.h"
00019 
00020 #define DEVICE_TO_HOST  0x80
00021 #define HOST_TO_DEVICE  0x00
00022 //Communications and CDC Control Class
00023 #define CDCCONTROL 0x2
00024 //CDC Data Class
00025 #define CDCDATA 0xA
00026 //CDC Set Control Line State Request
00027 #define SET_CONTROL_LINE_STATE 0x22
00028 //CDC Request Type
00029 #define DTR_REQUEST_TYPE 0x21
00030 //Set Bits of SetControlLineState register to Enable DTR
00031 #define DTR 0x0003
00032 
00033 
00034 USBHostSocketModem::USBHostSocketModem()
00035 {
00036     host = USBHost::getHostInst();
00037     init();
00038 }
00039 
00040 void USBHostSocketModem::init() 
00041 {
00042     dev_connected = false;
00043     dev = NULL;
00044     bulk_in = NULL;
00045     bulk_out = NULL;
00046     int_in = NULL;
00047     int_out = NULL;
00048     dev_connected = false;
00049     sm_intf = -1;
00050     sm_device_found = false;
00051     dev_connected = false;
00052     nb_ep = 0;
00053 }
00054 
00055 
00056 bool USBHostSocketModem::connected()
00057 {
00058     return dev_connected;
00059 }
00060 
00061 bool USBHostSocketModem::connect()
00062 {
00063 
00064     if (dev_connected) {
00065         return true;
00066     }
00067 
00068     for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
00069         if ((dev = host->getDevice(i)) != NULL) {
00070             
00071             USB_DBG("Trying to connect SocketModem device\r\n");
00072             if(host->enumerate(dev, this)){
00073                 printf("didn't fully enumerate\n");
00074                 break;
00075             }
00076             
00077             if (sm_device_found) {
00078                 //Set DTR
00079                 USBHostSocketModem::setDTR();
00080                 int_out = dev->getEndpoint(int_intf, INTERRUPT_ENDPOINT, OUT);
00081                 bulk_out = dev->getEndpoint(bulk_intf, BULK_ENDPOINT, OUT);
00082                 if (bulk_in == NULL){
00083                     printf("bulk_in == NULL\n");
00084                 }
00085                 bulk_in = dev->getEndpoint(bulk_intf, BULK_ENDPOINT, IN);
00086                 printf("!SocketModem Found!\n");
00087                 
00088                 if (!bulk_in || !bulk_out){
00089                     continue;
00090                 }
00091 
00092                 USB_INFO("New SocketModem device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, sm_intf);
00093                 dev->setName("SocketModem", bulk_intf);
00094                 host->registerDriver(dev, bulk_intf, this, &USBHostSocketModem::init);
00095 
00096                 dev_connected = true;
00097                 return true;
00098             }
00099         } //if()
00100     } //for()
00101     init();
00102     return false;
00103 }
00104 
00105 /*virtual*/ void USBHostSocketModem::setVidPid(uint16_t vid, uint16_t pid)
00106 {
00107     // we don't check VID/PID for MSD driver
00108 }
00109 
00110 /*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
00111 {
00112     //Debug print statements
00113     //printf("Parsing Interface\n");
00114     //printf("class = %i\n",intf_class);
00115     //printf("subclass = %i\n",intf_subclass);
00116     //printf("msd_intf = %i\n",msd_intf);
00117     //printf("intf_class = %i\n",intf_class);
00118     if ((sm_intf == -1) && (intf_class == CDCCONTROL))
00119     {
00120         sm_intf = intf_nb;
00121         int_intf = intf_nb;
00122         //printf("parse interface: sm_intf = %i\n", sm_intf);
00123         return true;
00124     }
00125     else if ((sm_intf == 0) && (intf_class == CDCDATA))
00126     {
00127         sm_intf = intf_nb;
00128         bulk_intf = intf_nb;
00129         //printf("parse interface: sm_intf = %i\n", sm_intf);
00130         return true;
00131     }
00132     return false;
00133 }
00134 
00135 /*virtual*/ bool USBHostSocketModem::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used
00136 {
00137     //printf("Setting up Endpoint\n");
00138     if ((intf_nb == int_intf) || (intf_nb == bulk_intf)){
00139         /*if (type == INTERRUPT_ENDPOINT)*/
00140         if ((type == BULK_ENDPOINT) || (type == INTERRUPT_ENDPOINT)){
00141             //printf("type == %s\n", type);
00142             nb_ep++;
00143             if (nb_ep == 2)
00144                 //printf("nb_ep == 2\n");
00145                 sm_device_found = true;
00146             return true;
00147         }
00148     }
00149     return false;
00150 }
00151 
00152 void USBHostSocketModem::setDTR()
00153 {
00154     //Set USB DTR to enable communication
00155     int res = host->controlWrite(dev, DTR_REQUEST_TYPE, SET_CONTROL_LINE_STATE, DTR, 0, NULL, 0);
00156 }
00157 
00158 void USBHostSocketModem::testAT()
00159 {
00160     uint8_t buf[] = "AT\r";
00161     uint8_t result[64] = {0};
00162     int rc,rt,i;
00163     rc = host->bulkWrite(dev, bulk_out, buf, sizeof(buf));
00164     printf("rc = %i\n", rc);
00165     rt = host->bulkRead(dev, bulk_in, result, sizeof(result));
00166     printf("rt = %i\n", rt);
00167     for (i = 0;i<sizeof(result);i++){
00168         printf("result %i = %i\n", i, result[i]);
00169     }
00170 }
00171 
00172 
00173 
00174