スマートコンセント PS-5204PSをmbedで
Dependencies: FatFileSystemCpp mbed TextLCD
Fork of BlueUSB by
Fusjitsu スマートコンセント FX-5204PSを利用できます。
BlueUSBを利用していますが、Bluetoothは兼用できません。
複数台利用するには、USBハブを使用してください。ただし相性があります。
・良ければ10台まで利用できるかも。
・稼働中に追加はできますが、外すことはできません。
LED1-3, スマートコンセント接続状態(3台まで表示)
LED4、点滅で正常動作
Revision 1:3f2890d103fb, committed 2012-09-26
- Comitter:
- yueee_yt
- Date:
- Wed Sep 26 06:29:47 2012 +0000
- Parent:
- 0:606b230e5b4a
- Child:
- 2:32a2d06f4fe2
- Commit message:
- Initial Version
;
Changed in this revision
--- a/AutoEvents.cpp Sat Apr 10 00:30:24 2010 +0000 +++ b/AutoEvents.cpp Wed Sep 26 06:29:47 2012 +0000 @@ -133,6 +133,12 @@ // Implemented in TestShell.cpp int OnBluetoothInsert(int device); +// Added by Y.Tauchi ------------------------------------ +// Implemented in usps.cpp +int OnUspsInsert(int device); +// ----------------------------------------------------------- + + void OnLoadDevice(int device, DeviceDescriptor* deviceDesc, InterfaceDescriptor* interfaceDesc) { printf("LoadDevice %d %02X:%02X:%02X\n",device,interfaceDesc->bInterfaceClass,interfaceDesc->bInterfaceSubClass,interfaceDesc->bInterfaceProtocol); @@ -154,6 +160,12 @@ if (interfaceDesc->bInterfaceSubClass == 0x01 && interfaceDesc->bInterfaceProtocol == 0x01) OnBluetoothInsert(device); // it's bluetooth! break; + // Added by Y.Tauchi ------------------------------------ + case CLASS_VENDOR_SPECIFIC: + if (interfaceDesc->bInterfaceSubClass == 0x01 && interfaceDesc->bInterfaceProtocol == 0x02) + OnUspsInsert(device); // it's USPS! (FUJITSU FX-5204PS) + break; +// ---------------------------------------------------------- default: StartAutoEvent(device,1,0); break;
--- a/FATFileSystem.lib Sat Apr 10 00:30:24 2010 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/mbed_unsupported/code/fatfilesystem/ \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/FatFileSystem.lib Wed Sep 26 06:29:47 2012 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/igorsk/code/FatFileSystemCpp/#88f22c32a456
--- a/USBHost.h Sat Apr 10 00:30:24 2010 +0000 +++ b/USBHost.h Wed Sep 26 06:29:47 2012 +0000 @@ -39,11 +39,11 @@ #define ENDPOINT_BULK 2 #define ENDPOINT_INTERRUPT 3 -#define DESCRIPTOR_TYPE_DEVICE 1 -#define DESCRIPTOR_TYPE_CONFIGURATION 2 -#define DESCRIPTOR_TYPE_STRING 3 -#define DESCRIPTOR_TYPE_INTERFACE 4 -#define DESCRIPTOR_TYPE_ENDPOINT 5 +#define DESCRIPTOR_TYPE_DEVICE 1 +#define DESCRIPTOR_TYPE_CONFIGURATION 2 +#define DESCRIPTOR_TYPE_STRING 3 +#define DESCRIPTOR_TYPE_INTERFACE 4 +#define DESCRIPTOR_TYPE_ENDPOINT 5 #define DESCRIPTOR_TYPE_HID 0x21 #define DESCRIPTOR_TYPE_REPORT 0x22 @@ -52,24 +52,24 @@ enum USB_CLASS_CODE { - CLASS_DEVICE, - CLASS_AUDIO, - CLASS_COMM_AND_CDC_CONTROL, - CLASS_HID, - CLASS_PHYSICAL = 0x05, - CLASS_STILL_IMAGING, - CLASS_PRINTER, - CLASS_MASS_STORAGE, - CLASS_HUB, - CLASS_CDC_DATA, - CLASS_SMART_CARD, - CLASS_CONTENT_SECURITY = 0x0D, - CLASS_VIDEO = 0x0E, - CLASS_DIAGNOSTIC_DEVICE = 0xDC, - CLASS_WIRELESS_CONTROLLER = 0xE0, - CLASS_MISCELLANEOUS = 0xEF, - CLASS_APP_SPECIFIC = 0xFE, - CLASS_VENDOR_SPECIFIC = 0xFF + CLASS_DEVICE, + CLASS_AUDIO, + CLASS_COMM_AND_CDC_CONTROL, + CLASS_HID, + CLASS_PHYSICAL = 0x05, + CLASS_STILL_IMAGING, + CLASS_PRINTER, + CLASS_MASS_STORAGE, + CLASS_HUB, + CLASS_CDC_DATA, + CLASS_SMART_CARD, + CLASS_CONTENT_SECURITY = 0x0D, + CLASS_VIDEO = 0x0E, + CLASS_DIAGNOSTIC_DEVICE = 0xDC, + CLASS_WIRELESS_CONTROLLER = 0xE0, + CLASS_MISCELLANEOUS = 0xEF, + CLASS_APP_SPECIFIC = 0xFE, + CLASS_VENDOR_SPECIFIC = 0xFF }; #define DEVICE_TO_HOST 0x80 @@ -77,22 +77,22 @@ #define REQUEST_TYPE_CLASS 0x20 #define RECIPIENT_DEVICE 0x00 #define RECIPIENT_INTERFACE 0x01 -#define RECIPIENT_ENDPOINT 0x02 -#define RECIPIENT_OTHER 0x03 +#define RECIPIENT_ENDPOINT 0x02 +#define RECIPIENT_OTHER 0x03 -#define GET_STATUS 0 -#define CLEAR_FEATURE 1 -#define SET_FEATURE 3 -#define SET_ADDRESS 5 -#define GET_DESCRIPTOR 6 -#define SET_DESCRIPTOR 7 -#define GET_CONFIGURATION 8 -#define SET_CONFIGURATION 9 -#define GET_INTERFACE 10 -#define SET_INTERFACE 11 -#define SYNCH_FRAME 11 +#define GET_STATUS 0 +#define CLEAR_FEATURE 1 +#define SET_FEATURE 3 +#define SET_ADDRESS 5 +#define GET_DESCRIPTOR 6 +#define SET_DESCRIPTOR 7 +#define GET_CONFIGURATION 8 +#define SET_CONFIGURATION 9 +#define GET_INTERFACE 10 +#define SET_INTERFACE 11 +#define SYNCH_FRAME 11 -// -5 is nak +// -5 is nak /* 0010 ACK Handshake 1010 NAK Handshake @@ -110,55 +110,55 @@ typedef struct { - u8 bLength; - u8 bDescriptorType; - u16 bcdUSB; - u8 bDeviceClass; - u8 bDeviceSubClass; - u8 bDeviceProtocol; - u8 bMaxPacketSize; - u16 idVendor; - u16 idProduct; - u16 bcdDevice; // version - u8 iManufacturer; - u8 iProduct; - u8 iSerialNumber; - u8 bNumConfigurations; -} DeviceDescriptor; // 16 bytes + u8 bLength; + u8 bDescriptorType; + u16 bcdUSB; + u8 bDeviceClass; + u8 bDeviceSubClass; + u8 bDeviceProtocol; + u8 bMaxPacketSize; + u16 idVendor; + u16 idProduct; + u16 bcdDevice; // version + u8 iManufacturer; + u8 iProduct; + u8 iSerialNumber; + u8 bNumConfigurations; +} DeviceDescriptor; // 16 bytes typedef struct { - u8 bLength; - u8 bDescriptorType; - u16 wTotalLength; - u8 bNumInterfaces; - u8 bConfigurationValue; // Value to use as an argument to select this configuration - u8 iConfiguration; // Index of String Descriptor describing this configuration - u8 bmAttributes; // Bitmap D7 Reserved, set to 1. (USB 1.0 Bus Powered),D6 Self Powered,D5 Remote Wakeup,D4..0 = 0 - u8 bMaxPower; // Maximum Power Consumption in 2mA units + u8 bLength; + u8 bDescriptorType; + u16 wTotalLength; + u8 bNumInterfaces; + u8 bConfigurationValue; // Value to use as an argument to select this configuration + u8 iConfiguration; // Index of String Descriptor describing this configuration + u8 bmAttributes; // Bitmap D7 Reserved, set to 1. (USB 1.0 Bus Powered),D6 Self Powered,D5 Remote Wakeup,D4..0 = 0 + u8 bMaxPower; // Maximum Power Consumption in 2mA units } ConfigurationDescriptor; typedef struct { - u8 bLength; - u8 bDescriptorType; - u8 bInterfaceNumber; - u8 bAlternateSetting; - u8 bNumEndpoints; - u8 bInterfaceClass; - u8 bInterfaceSubClass; - u8 bInterfaceProtocol; - u8 iInterface; // Index of String Descriptor Describing this interface + u8 bLength; + u8 bDescriptorType; + u8 bInterfaceNumber; + u8 bAlternateSetting; + u8 bNumEndpoints; + u8 bInterfaceClass; + u8 bInterfaceSubClass; + u8 bInterfaceProtocol; + u8 iInterface; // Index of String Descriptor Describing this interface } InterfaceDescriptor; typedef struct { - u8 bLength; - u8 bDescriptorType; - u8 bEndpointAddress; // Bits 0:3 endpoint, Bits 7 Direction 0 = Out, 1 = In (Ignored for Control Endpoints) - u8 bmAttributes; // Bits 0:1 00 = Control, 01 = Isochronous, 10 = Bulk, 11 = Interrupt - u16 wMaxPacketSize; - u8 bInterval; // Interval for polling endpoint data transfers. + u8 bLength; + u8 bDescriptorType; + u8 bEndpointAddress; // Bits 0:3 endpoint, Bits 7 Direction 0 = Out, 1 = In (Ignored for Control Endpoints) + u8 bmAttributes; // Bits 0:1 00 = Control, 01 = Isochronous, 10 = Bulk, 11 = Interrupt + u16 wMaxPacketSize; + u8 bInterval; // Interval for polling endpoint data transfers. } EndpointDescriptor; typedef struct { @@ -179,22 +179,22 @@ void USBLoop(); u8* USBGetBuffer(u32* len); -// Optional callback for transfers, called at interrupt time +// Optional callback for transfers, called at interrupt time typedef void (*USBCallback)(int device, int endpoint, int status, u8* data, int len, void* userData); -// Transfers +// Transfers int USBControlTransfer(int device, int request_type, int request, int value, int index, u8* data, int length, USBCallback callback = 0, void* userData = 0); int USBInterruptTransfer(int device, int ep, u8* data, int length, USBCallback callback = 0, void* userData = 0); -int USBBulkTransfer(int device, int ep, u8* data, int length, USBCallback callback = 0, void* userData = 0); +int USBBulkTransfer (int device, int ep, u8* data, int length, USBCallback callback = 0, void* userData = 0); -// Standard Device methods +// Standard Device methods int GetDescriptor(int device, int descType, int descIndex, u8* data, int length); int GetString(int device, int index, char* dst, int length); int SetAddress(int device, int new_addr); int SetConfiguration(int device, int configNum); int SetInterface(int device, int ifNum, int altNum); -// Implemented to notify app of the arrival of a device +// Implemented to notify app of the arrival of a device void OnLoadDevice(int device, DeviceDescriptor* deviceDesc, InterfaceDescriptor* interfaceDesc); #endif \ No newline at end of file
--- a/main.cpp Sat Apr 10 00:30:24 2010 +0000 +++ b/main.cpp Wed Sep 26 06:29:47 2012 +0000 @@ -34,34 +34,28 @@ int _device; u32 _blockSize; u32 _blockCount; - + public: - USBFileSystem() : FATFileSystem("usb"),_device(0),_blockSize(0),_blockCount(0) - { + USBFileSystem() : FATFileSystem("usb"),_device(0),_blockSize(0),_blockCount(0) { } - - void SetDevice(int device) - { + + void SetDevice(int device) { _device = device; } - - virtual int disk_initialize() - { + + virtual int disk_initialize() { return MassStorage_ReadCapacity(_device,&_blockCount,&_blockSize); } - - virtual int disk_write(const char *buffer, int block_number) - { + + virtual int disk_write(const char *buffer, int block_number) { return MassStorage_Write(_device,block_number,1,(u8*)buffer,_blockSize); } - - virtual int disk_read(char *buffer, int block_number) - { + + virtual int disk_read(char *buffer, int block_number) { return MassStorage_Read(_device,block_number,1,(u8*)buffer,_blockSize); } - - virtual int disk_sectors() - { + + virtual int disk_sectors() { return _blockCount; } }; @@ -69,16 +63,14 @@ void DumpFS(int depth, int count) { DIR *d = opendir("/usb"); - if (!d) - { + if (!d) { printf("USB file system borked\n"); return; } printf("\nDumping root dir\n"); struct dirent *p; - for(;;) - { + for(;;) { p = readdir(d); if (!p) break; @@ -112,10 +104,24 @@ } void TestShell(); - +void InitUSPS(); +void USPS_Refresh(); +DigitalOut led1(LED1); +Timer t; int main() { pc.baud(460800); printf("BlueUSB\nNow get a bunch of usb or bluetooth things and plug them in\n"); - TestShell(); + InitUSPS(); + //TestShell(); + USBInit(); + t.start(); + while(1){ + USBLoop(); + if(t>1 ){ + t.start(); + led1=!led1; +USPS_Refresh(); + } + } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/usps.cpp Wed Sep 26 06:29:47 2012 +0000 @@ -0,0 +1,174 @@ +#include "mbed.h" +#include "USBHost.h" +#include "Utils.h" + +u8 ms_buf[0x1A]; // USPS Streaming Buffer +int nUSPS; +int dUSPS[10]; +int eUSPS[10]; +void usps_cmd(int device,int cmd,int val,int len,int evt); +void usps_measurement_mode(int device); +void USPS_Refresh(); + +bool usps_debug=true; + +/* protocol */ +#define USPS_CMD_START 0x01 +#define USPS_CMD_VALUE 0x20 +#define USPS_CMD_GET_FIRMWARE 0xc0 +#define USPS_CMD_GET_SERIAL 0xc1 +#define USPS_CMD_GET_VOLTAGE 0xb0 +#define USPS_CMD_GET_TEMP 0xb4 +#define USPS_CMD_GET_FREQ 0xa1 + +#define USPS_MODE_WATTAGE 0x10 +#define USPS_MODE_CURRENT 0x30 + +#define EVENT_GET_FIRMWARE 0x11 +#define EVENT_GET_SERIAL 0x12 +#define EVENT_GET_TEMP 0x21 +#define EVENT_GET_VOLT 0x22 +#define EVENT_GET_FREQ 0x23 +#define EVENT_GET_POWER 0x24 +#define EVENT_START 0x13 +#define EVENT_VALUE 0x14 + +float temp; +float volt; +float freq; +float power1,power2,power3,power4; +unsigned char cmdFlag; + +void InitUSPS() +{ + nUSPS=0; +} + +void UspsEventCallback(int device, int endpoint, int status, u8* data, int len, void* userData) +{ + int i; + int Flag=0; + int ptr = 0; + if(status!=0) { + int i1=0; + if(usps_debug) printf("USPS Callback Status %d \n\r",status); + for(i=0; i<nUSPS; i++) { + if(dUSPS[i]==device) { + i1=i; + Flag=1; + } + } + if(Flag==1) { + for(i=i1+1; i<nUSPS; i++) { + dUSPS[i-1]=dUSPS[i]; + eUSPS[i-1]=eUSPS[i]; + } + nUSPS--; + } + return; + } + switch((int)userData) { + case EVENT_GET_FIRMWARE: + if(usps_debug) printf("USPS Device#%X Firmware VOL%X REL%X\r\n",device,data[0],data[1]); + break; + case EVENT_GET_SERIAL: + i=data[0]<<16|data[1]<<8|data[2]; + if(usps_debug) printf("USPS Device#%x SerialNo %X \r\n",device,i); + break; + case EVENT_GET_TEMP: + i=data[1]<<8|data[0]; + temp=i/100.0; + if(usps_debug) printf("USPS Device#%x Temp %5.2f \r\n",device,temp); + break; + case EVENT_GET_VOLT: + volt=data[0]; + if(usps_debug) printf("USPS Device#%x Volt %6.2f \r\n",device,volt); + break; + case EVENT_GET_FREQ: + i=data[1]<<8|data[0]; + freq=2000000.0/i; + if(usps_debug) printf("USPS Device#%x Freq %5.2f \r\n",device,freq); + break; + case EVENT_GET_POWER: + power4=(unsigned short)(data[0xf]<<8|data[0xe]); + power3=(unsigned short)(data[0xd]<<8|data[0xc]); + power2=(unsigned short)(data[0xb]<<8|data[0xa]); + power1=(unsigned short)(data[0x9]<<8|data[0x8]); + if(usps_debug) printf("USPS Device#%x:Power1 %5.0f[W]:Power2 %5.0f[W]:Power3 %5.0f[W]:Power4 %5.0f[W]\r\n",device,power1,power2,power3,power4); + break; + default: + if(usps_debug) printf("usps response \r\n"); + if(usps_debug) printf("USPS %02X %02X %02X \r\n",device,status,len); + if(usps_debug) printfBytes("",data,len); + } +} + +// Add Usps Interface +void USPS_Refresh() +{ + int i; + u8* dst = ms_buf; + for (i=0; i<nUSPS; i++) { + usps_cmd(dUSPS[i],USPS_CMD_GET_TEMP ,0,2,EVENT_GET_TEMP); + usps_cmd(dUSPS[i],USPS_CMD_GET_VOLTAGE, 0, 1,EVENT_GET_VOLT); + usps_cmd(dUSPS[i],USPS_CMD_GET_FREQ, 0, 8,EVENT_GET_FREQ); + USBInterruptTransfer(dUSPS[i], eUSPS[i], dst,0x10, UspsEventCallback, (void*)EVENT_GET_POWER); + } +} + +void AddUspsInterface(int device, InterfaceDescriptor* id, EndpointDescriptor* ed,int len) +{ + if ((ed->bmAttributes & 3) != ENDPOINT_INTERRUPT || !(ed->bEndpointAddress & 0x80)) + return; + usps_cmd(device,USPS_CMD_GET_FIRMWARE ,0,2,EVENT_GET_FIRMWARE); + usps_cmd(device,USPS_CMD_GET_SERIAL ,0,3,EVENT_GET_SERIAL); + usps_measurement_mode(device); + dUSPS[nUSPS]=device; + eUSPS[nUSPS]=ed->bEndpointAddress; + nUSPS++; +} + +void usps_measurement_mode(int device) +{ + int i; + u8* dst = ms_buf; + i= USBControlTransfer(device, 0x40, USPS_CMD_START ,0, 0, dst, 0,UspsEventCallback,(void*)EVENT_START); //UT_Vendor_Write + i= USBControlTransfer(device, 0x40, USPS_CMD_VALUE ,USPS_MODE_WATTAGE , 0, dst, 0,UspsEventCallback,(void*)EVENT_VALUE); //UT_Vendor_Write +} + +void usps_cmd(int device,int cmd,int val,int len,int evt) +{ + int i; + u8* dst = ms_buf; + i= USBControlTransfer(device, 0xc0, cmd,val, 0, dst, len,UspsEventCallback,(void*)evt); + if(i!=-100) printf("status : %d \r\n",i); +} +// Detected USPS Device +int OnUspsInsert(int device) +{ + u8 buffer[255]; + int err = GetDescriptor(device,DESCRIPTOR_TYPE_CONFIGURATION,0,buffer,255); + if (err < 0) + return err; + int len = buffer[2] | (buffer[3] << 8); + u8* d = buffer; + u8* end = d + len; + + if(usps_debug) printf("OnUspsInsert %02X %02X\r\n",device,len); // + //printfBytes(" -->",d,len); + while (d < end) { + if (d[1] == DESCRIPTOR_TYPE_INTERFACE) { + InterfaceDescriptor* id = (InterfaceDescriptor*)d; + d += d[0]; + while (d < end) { + if (d[1] == DESCRIPTOR_TYPE_ENDPOINT) { + if(usps_debug) printfBytes("Usps Endpoint",d,d[0]); + AddUspsInterface(device,id,(EndpointDescriptor*)d,d[4]); + } + d += d[0]; + } + } + d += d[0]; + } + return 0; +}