XOOMの動作状況を聞き処理を変えてみました。 USBケーブルを抜いた際に処理を終了するようにしました。

Dependencies:   mbed

Committer:
abe00makoto
Date:
Wed May 25 15:21:39 2011 +0000
Revision:
1:371cbed7a29b
Parent:
0:9fb6c423e32c
Child:
2:a05c7cbe396f

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
abe00makoto 1:371cbed7a29b 1 #include <stdio.h>
abe00makoto 1:371cbed7a29b 2 #include <stdlib.h>
abe00makoto 1:371cbed7a29b 3 #include <stdio.h>
abe00makoto 1:371cbed7a29b 4 #include <string.h>
abe00makoto 1:371cbed7a29b 5
abe00makoto 1:371cbed7a29b 6 #include "USBHost.h"
abe00makoto 1:371cbed7a29b 7 #include "Utils.h"
abe00makoto 1:371cbed7a29b 8 #include "ps3.h"
abe00makoto 1:371cbed7a29b 9 #include "ADK.h"
abe00makoto 1:371cbed7a29b 10 #include "mbed.h"
abe00makoto 1:371cbed7a29b 11
abe00makoto 1:371cbed7a29b 12 #define ADKLOG 1
abe00makoto 1:371cbed7a29b 13 #if ADKLOG
abe00makoto 1:371cbed7a29b 14 #define LOG(...) printf(__VA_ARGS__)
abe00makoto 1:371cbed7a29b 15 #define Log(...) printf(__VA_ARGS__)
abe00makoto 1:371cbed7a29b 16 #define log(...) printf(__VA_ARGS__)
abe00makoto 1:371cbed7a29b 17
abe00makoto 1:371cbed7a29b 18 #else
abe00makoto 1:371cbed7a29b 19 #define LOG(...) do {} while(0)
abe00makoto 1:371cbed7a29b 20 #define Log(...) do {} while(0)
abe00makoto 1:371cbed7a29b 21 #define log(...) do {} while(0)
abe00makoto 1:371cbed7a29b 22
abe00makoto 1:371cbed7a29b 23 #endif
abe00makoto 1:371cbed7a29b 24
abe00makoto 1:371cbed7a29b 25 //const manufacturer=
abe00makoto 1:371cbed7a29b 26
abe00makoto 1:371cbed7a29b 27
abe00makoto 1:371cbed7a29b 28 PwmOut led1(LED1);
abe00makoto 1:371cbed7a29b 29 PwmOut led2(LED2);
abe00makoto 1:371cbed7a29b 30 PwmOut led3(LED3);
abe00makoto 1:371cbed7a29b 31 PwmOut led4(LED4);
abe00makoto 1:371cbed7a29b 32
abe00makoto 1:371cbed7a29b 33 void AdkUSB::loop() {
abe00makoto 1:371cbed7a29b 34 log("enter loop\r\n");
abe00makoto 1:371cbed7a29b 35 u8 buf[3];
abe00makoto 1:371cbed7a29b 36
abe00makoto 1:371cbed7a29b 37 while (1) {
abe00makoto 1:371cbed7a29b 38 //wait_ms(10);
abe00makoto 1:371cbed7a29b 39 //printf("Adk.read run!-------------------------xxxxxxxxxxxxxxxxxxxxxxxx\r\n");
abe00makoto 1:371cbed7a29b 40 int len=this->read(buf,sizeof(buf),1);
abe00makoto 1:371cbed7a29b 41 if (len >0) {
abe00makoto 1:371cbed7a29b 42 log("buf[0]=%d,buf[1]=%d,buf[2]=%d\r\n",buf[0],buf[1],buf[2]);
abe00makoto 1:371cbed7a29b 43 if (buf[0] == 0x2) {
abe00makoto 1:371cbed7a29b 44 if (buf[1] == 0x0) {
abe00makoto 1:371cbed7a29b 45 led1=((float)buf[2])/255.0;
abe00makoto 1:371cbed7a29b 46 } else if (buf[1]==0x3) {
abe00makoto 1:371cbed7a29b 47 led2=((float)buf[2])/255.0;
abe00makoto 1:371cbed7a29b 48 } else if (buf[1]=0x6) {
abe00makoto 1:371cbed7a29b 49 led3=((float)buf[2])/255.0;
abe00makoto 1:371cbed7a29b 50 }
abe00makoto 1:371cbed7a29b 51 } else if (buf[0] ==0x3) {
abe00makoto 1:371cbed7a29b 52 if (buf[1] == 0x0) {
abe00makoto 1:371cbed7a29b 53 led4=buf[2]? 1.0:0.0;
abe00makoto 1:371cbed7a29b 54 }
abe00makoto 1:371cbed7a29b 55 }
abe00makoto 1:371cbed7a29b 56
abe00makoto 1:371cbed7a29b 57 //buf[0]=0x01;
abe00makoto 1:371cbed7a29b 58
abe00makoto 1:371cbed7a29b 59 } else {
abe00makoto 1:371cbed7a29b 60 led1=led2=led3=led4=0.0;
abe00makoto 1:371cbed7a29b 61 log("---------------------------------------------------------------loop end\r\n");
abe00makoto 1:371cbed7a29b 62 return;
abe00makoto 1:371cbed7a29b 63 }
abe00makoto 1:371cbed7a29b 64 }
abe00makoto 1:371cbed7a29b 65 }
abe00makoto 1:371cbed7a29b 66
abe00makoto 1:371cbed7a29b 67
abe00makoto 1:371cbed7a29b 68
abe00makoto 1:371cbed7a29b 69 int getProtocol(int device);
abe00makoto 1:371cbed7a29b 70 void sendString(int device, int index, const char *str);
abe00makoto 1:371cbed7a29b 71
abe00makoto 1:371cbed7a29b 72 bool switchDevice(int device) {
abe00makoto 1:371cbed7a29b 73
abe00makoto 1:371cbed7a29b 74 if (1==getProtocol(device)) {
abe00makoto 1:371cbed7a29b 75 log("device supports protocol 1\r\n");
abe00makoto 1:371cbed7a29b 76
abe00makoto 1:371cbed7a29b 77 } else {
abe00makoto 1:371cbed7a29b 78 log("could not read device protocol version\r\n");
abe00makoto 1:371cbed7a29b 79 return false;
abe00makoto 1:371cbed7a29b 80 }
abe00makoto 1:371cbed7a29b 81
abe00makoto 1:371cbed7a29b 82
abe00makoto 1:371cbed7a29b 83 sendString(device,ACCESSORY_STRING_MANUFACTURER,"Google, Inc.");
abe00makoto 1:371cbed7a29b 84 sendString(device,ACCESSORY_STRING_MODEL,"DemoKit");
abe00makoto 1:371cbed7a29b 85 sendString(device,ACCESSORY_STRING_DESCRIPTION,"DemoKit Arduino Board");
abe00makoto 1:371cbed7a29b 86 sendString(device,ACCESSORY_STRING_VERSION,"1.0");
abe00makoto 1:371cbed7a29b 87 sendString(device,ACCESSORY_STRING_URI,"http://www.android.com");
abe00makoto 1:371cbed7a29b 88 sendString(device,ACCESSORY_STRING_SERIAL,"0000000012345678");
abe00makoto 1:371cbed7a29b 89
abe00makoto 1:371cbed7a29b 90 USBControlTransfer(device,
abe00makoto 1:371cbed7a29b 91 HOST_TO_DEVICE|REQUEST_TYPE_VENDOR|RECIPIENT_DEVICE,
abe00makoto 1:371cbed7a29b 92 ACCESSORY_START,
abe00makoto 1:371cbed7a29b 93 0,//value
abe00makoto 1:371cbed7a29b 94 0, //index
abe00makoto 1:371cbed7a29b 95 0,
abe00makoto 1:371cbed7a29b 96 0,
abe00makoto 1:371cbed7a29b 97 0,
abe00makoto 1:371cbed7a29b 98 0 );
abe00makoto 1:371cbed7a29b 99
abe00makoto 1:371cbed7a29b 100 wait(1);
abe00makoto 1:371cbed7a29b 101
abe00makoto 1:371cbed7a29b 102 return true;
abe00makoto 1:371cbed7a29b 103
abe00makoto 1:371cbed7a29b 104 }
abe00makoto 1:371cbed7a29b 105
abe00makoto 1:371cbed7a29b 106
abe00makoto 1:371cbed7a29b 107 int getProtocol(int device) {
abe00makoto 1:371cbed7a29b 108 u16 data=-1;
abe00makoto 1:371cbed7a29b 109 USBControlTransfer(device,
abe00makoto 1:371cbed7a29b 110 DEVICE_TO_HOST|REQUEST_TYPE_VENDOR|RECIPIENT_DEVICE,
abe00makoto 1:371cbed7a29b 111 ACCESSORY_GET_PROTOCOL,
abe00makoto 1:371cbed7a29b 112 0,//value
abe00makoto 1:371cbed7a29b 113 0, //index
abe00makoto 1:371cbed7a29b 114 (u8*)&data,
abe00makoto 1:371cbed7a29b 115 2,
abe00makoto 1:371cbed7a29b 116 0,
abe00makoto 1:371cbed7a29b 117 0 );
abe00makoto 1:371cbed7a29b 118 //printf("return %d\r\n",data);
abe00makoto 1:371cbed7a29b 119 return data;
abe00makoto 1:371cbed7a29b 120
abe00makoto 1:371cbed7a29b 121 }
abe00makoto 1:371cbed7a29b 122
abe00makoto 1:371cbed7a29b 123 void sendString(int device, int index, const char *str) {
abe00makoto 1:371cbed7a29b 124
abe00makoto 1:371cbed7a29b 125 LOG("send_string start(%d,%d,%s) %d \r\n",device,index,str,strlen(str)+1);
abe00makoto 1:371cbed7a29b 126
abe00makoto 1:371cbed7a29b 127 //this is diffrent google's sample but error ocard same sample
abe00makoto 1:371cbed7a29b 128 USBControlTransfer(device,
abe00makoto 1:371cbed7a29b 129 DEVICE_TO_HOST /*HOST_TO_DEVIC use error why? please teach me*/|REQUEST_TYPE_VENDOR|RECIPIENT_DEVICE,
abe00makoto 1:371cbed7a29b 130 ACCESSORY_SEND_STRING,
abe00makoto 1:371cbed7a29b 131 0,//value
abe00makoto 1:371cbed7a29b 132 index,
abe00makoto 1:371cbed7a29b 133 (u8*)str,
abe00makoto 1:371cbed7a29b 134 strlen(str)+1
abe00makoto 1:371cbed7a29b 135 );
abe00makoto 1:371cbed7a29b 136
abe00makoto 1:371cbed7a29b 137 LOG("send_string end(%d,%d,%s)\r\n",device,index,str);
abe00makoto 1:371cbed7a29b 138
abe00makoto 1:371cbed7a29b 139 }
abe00makoto 1:371cbed7a29b 140
abe00makoto 1:371cbed7a29b 141 //int USBBulkTransfer(int device, int ep, u8* data, int length, USBCallback callback, void* userData)
abe00makoto 1:371cbed7a29b 142 int AdkUSB::read(u8 *buff, int len, int nakLimit) {
abe00makoto 1:371cbed7a29b 143 int ret=USBBulkTransfer(_device,input_ep|0x80,buff,len,0,0);
abe00makoto 1:371cbed7a29b 144 log("adkUSB read ------- xx %d \r\n",ret);
abe00makoto 1:371cbed7a29b 145 return ret;
abe00makoto 1:371cbed7a29b 146 }
abe00makoto 1:371cbed7a29b 147 int AdkUSB::write(u8 *buff, int len) {
abe00makoto 1:371cbed7a29b 148 log("adkUSB write ------- xxx \r\n");
abe00makoto 1:371cbed7a29b 149 return USBBulkTransfer(_device,output_ep,buff,len,0,0);
abe00makoto 1:371cbed7a29b 150 }
abe00makoto 1:371cbed7a29b 151
abe00makoto 1:371cbed7a29b 152
abe00makoto 1:371cbed7a29b 153 AdkUSB::AdkUSB(int device, int configuration, int interfaceNumber) {
abe00makoto 1:371cbed7a29b 154 log("connecting Android \r\n");
abe00makoto 1:371cbed7a29b 155 _device = device;
abe00makoto 1:371cbed7a29b 156 _configuration = configuration;
abe00makoto 1:371cbed7a29b 157 _interfaceNumber = interfaceNumber;
abe00makoto 1:371cbed7a29b 158 printf("device = %d configuration = %d interfaceNumber = %d\r\n", device, configuration, interfaceNumber);
abe00makoto 1:371cbed7a29b 159 int err;
abe00makoto 1:371cbed7a29b 160
abe00makoto 1:371cbed7a29b 161 u8 buffer[255];
abe00makoto 1:371cbed7a29b 162 err = GetDescriptor(_device,DESCRIPTOR_TYPE_CONFIGURATION,0,buffer,4);
abe00makoto 1:371cbed7a29b 163
abe00makoto 1:371cbed7a29b 164 if (err < 0) {
abe00makoto 1:371cbed7a29b 165 log("Failed to get descriptor\r\n");
abe00makoto 1:371cbed7a29b 166 return;
abe00makoto 1:371cbed7a29b 167 }
abe00makoto 1:371cbed7a29b 168
abe00makoto 1:371cbed7a29b 169
abe00makoto 1:371cbed7a29b 170 int len = buffer[2] | (buffer[3] << 8);
abe00makoto 1:371cbed7a29b 171 if (len > sizeof(buffer)) {
abe00makoto 1:371cbed7a29b 172 log("config descriptor too large\n");
abe00makoto 1:371cbed7a29b 173 /* might want to truncate here */
abe00makoto 1:371cbed7a29b 174 return;
abe00makoto 1:371cbed7a29b 175 }
abe00makoto 1:371cbed7a29b 176 err = GetDescriptor(_device,DESCRIPTOR_TYPE_CONFIGURATION,0,buffer,len);
abe00makoto 1:371cbed7a29b 177 u8* p = buffer;
abe00makoto 1:371cbed7a29b 178 input_ep=0;
abe00makoto 1:371cbed7a29b 179 output_ep=0;
abe00makoto 1:371cbed7a29b 180 EndpointDescriptor *epDesc;
abe00makoto 1:371cbed7a29b 181 while (p<(buffer+len)) {
abe00makoto 1:371cbed7a29b 182 u8 descLen = p[0];
abe00makoto 1:371cbed7a29b 183 u8 descType = p[1];
abe00makoto 1:371cbed7a29b 184 log("descLen=%d,descType=%d\r\n",descLen,descType);
abe00makoto 1:371cbed7a29b 185 switch (descType) {
abe00makoto 1:371cbed7a29b 186 case DESCRIPTOR_TYPE_CONFIGURATION:
abe00makoto 1:371cbed7a29b 187 log("config desc\r\n");
abe00makoto 1:371cbed7a29b 188 break;
abe00makoto 1:371cbed7a29b 189 case DESCRIPTOR_TYPE_INTERFACE:
abe00makoto 1:371cbed7a29b 190 log("interface desc\r\n");
abe00makoto 1:371cbed7a29b 191 break;
abe00makoto 1:371cbed7a29b 192 case DESCRIPTOR_TYPE_ENDPOINT:
abe00makoto 1:371cbed7a29b 193 epDesc=(EndpointDescriptor*)p;
abe00makoto 1:371cbed7a29b 194 if (!input_ep && (epDesc->bEndpointAddress& 0x80)) {
abe00makoto 1:371cbed7a29b 195 input_ep=epDesc->bEndpointAddress& 0x7f;
abe00makoto 1:371cbed7a29b 196 //PacketSize drop
abe00makoto 1:371cbed7a29b 197 log("input Endpoint address=%d,wMaxPacketSize=%d,bmAttributes=%d\r\n",input_ep,epDesc->wMaxPacketSize,epDesc->bmAttributes);
abe00makoto 1:371cbed7a29b 198
abe00makoto 1:371cbed7a29b 199 } else if (!output_ep) {
abe00makoto 1:371cbed7a29b 200 output_ep=epDesc->bEndpointAddress& 0x7f;
abe00makoto 1:371cbed7a29b 201 //PacketSize drop
abe00makoto 1:371cbed7a29b 202 log("output Endpoint address=%d,wMaxPacketSize=%d,bmAttributes=%d\r\n",input_ep,epDesc->wMaxPacketSize,epDesc->bmAttributes);
abe00makoto 1:371cbed7a29b 203 } else {
abe00makoto 1:371cbed7a29b 204 //other
abe00makoto 1:371cbed7a29b 205 log("non input,output Endpoint address=%d,wMaxPacketSize=%d,bmAttributes=%d\r\n",input_ep,epDesc->wMaxPacketSize,epDesc->bmAttributes);
abe00makoto 1:371cbed7a29b 206 }
abe00makoto 1:371cbed7a29b 207 break;
abe00makoto 1:371cbed7a29b 208 default:
abe00makoto 1:371cbed7a29b 209 log("unkown desc type(%d) \r\n",descType);
abe00makoto 1:371cbed7a29b 210 }
abe00makoto 1:371cbed7a29b 211 p+=descLen;
abe00makoto 1:371cbed7a29b 212 }
abe00makoto 1:371cbed7a29b 213
abe00makoto 1:371cbed7a29b 214 if (!(input_ep && output_ep)) {
abe00makoto 1:371cbed7a29b 215 log("can't find accessory endpoints\r\n");
abe00makoto 1:371cbed7a29b 216 return;
abe00makoto 1:371cbed7a29b 217 }
abe00makoto 1:371cbed7a29b 218
abe00makoto 1:371cbed7a29b 219 log("SetConfiguration\r\n");
abe00makoto 1:371cbed7a29b 220 err = SetConfiguration(device,configuration);
abe00makoto 1:371cbed7a29b 221 if (err < 0) {
abe00makoto 1:371cbed7a29b 222 log("SetConfiguration error\r\n");
abe00makoto 1:371cbed7a29b 223 wait(10);
abe00makoto 1:371cbed7a29b 224 }
abe00makoto 1:371cbed7a29b 225
abe00makoto 1:371cbed7a29b 226 log("ADK Stand by\r\n");
abe00makoto 1:371cbed7a29b 227 }