Pulse Oximeter (NONIN) communicates with mbed via Bluetooth dongle and sends Heart Rate and Oxygen Saturation via GPRS module
Dependencies: C12832 GPS GSM mbed
Fork of myBlueUSB_localfix by
myUSBHost/MassStorage.h@0:003889bc474f, 2013-12-07 (annotated)
- Committer:
- nobukuma
- Date:
- Sat Dec 07 14:19:00 2013 +0000
- Revision:
- 0:003889bc474f
http://mbed.org/users/networker/code/myBlueUSB/ rev13??rev12??????????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nobukuma | 0:003889bc474f | 1 | #ifndef MASSSTORAGE_H |
nobukuma | 0:003889bc474f | 2 | #define MASSSTORAGE_H |
nobukuma | 0:003889bc474f | 3 | |
nobukuma | 0:003889bc474f | 4 | #include "Utils.h" |
nobukuma | 0:003889bc474f | 5 | #include "USBHost.h" |
nobukuma | 0:003889bc474f | 6 | |
nobukuma | 0:003889bc474f | 7 | |
nobukuma | 0:003889bc474f | 8 | #define ERR_BAD_CSW_SIGNATURE -200 |
nobukuma | 0:003889bc474f | 9 | |
nobukuma | 0:003889bc474f | 10 | #define CBW_SIGNATURE 0x43425355 |
nobukuma | 0:003889bc474f | 11 | #define CSW_SIGNATURE 0x53425355 |
nobukuma | 0:003889bc474f | 12 | |
nobukuma | 0:003889bc474f | 13 | #define BULK_ENDPOINT_IN 0x81 |
nobukuma | 0:003889bc474f | 14 | #define BULK_ENDPOINT_OUT 0x02 |
nobukuma | 0:003889bc474f | 15 | |
nobukuma | 0:003889bc474f | 16 | // Command Block |
nobukuma | 0:003889bc474f | 17 | typedef struct { |
nobukuma | 0:003889bc474f | 18 | u32 Signature; |
nobukuma | 0:003889bc474f | 19 | u32 Tag; |
nobukuma | 0:003889bc474f | 20 | u32 TransferLength; |
nobukuma | 0:003889bc474f | 21 | u8 Flags; |
nobukuma | 0:003889bc474f | 22 | u8 LUN; |
nobukuma | 0:003889bc474f | 23 | u8 CBLength; |
nobukuma | 0:003889bc474f | 24 | u8 CB[16]; // only 6 really |
nobukuma | 0:003889bc474f | 25 | } CBW; |
nobukuma | 0:003889bc474f | 26 | |
nobukuma | 0:003889bc474f | 27 | // Status block |
nobukuma | 0:003889bc474f | 28 | typedef struct { |
nobukuma | 0:003889bc474f | 29 | u32 Signature; |
nobukuma | 0:003889bc474f | 30 | u32 Tag; |
nobukuma | 0:003889bc474f | 31 | u32 DataResidue; |
nobukuma | 0:003889bc474f | 32 | u8 Status; |
nobukuma | 0:003889bc474f | 33 | } CSW; |
nobukuma | 0:003889bc474f | 34 | |
nobukuma | 0:003889bc474f | 35 | class USBSCSI { |
nobukuma | 0:003889bc474f | 36 | protected: |
nobukuma | 0:003889bc474f | 37 | int _device; |
nobukuma | 0:003889bc474f | 38 | unsigned char epin, epout; |
nobukuma | 0:003889bc474f | 39 | public: |
nobukuma | 0:003889bc474f | 40 | USBSCSI() : _device(0) {} |
nobukuma | 0:003889bc474f | 41 | |
nobukuma | 0:003889bc474f | 42 | void SetDevice(int device, unsigned char in, unsigned char out) |
nobukuma | 0:003889bc474f | 43 | { |
nobukuma | 0:003889bc474f | 44 | _device = device; |
nobukuma | 0:003889bc474f | 45 | epin = in; |
nobukuma | 0:003889bc474f | 46 | epout = out; |
nobukuma | 0:003889bc474f | 47 | } |
nobukuma | 0:003889bc474f | 48 | protected: |
nobukuma | 0:003889bc474f | 49 | int DoSCSI(const u8* cmd, int cmdLen, int flags, u8* data, u32 transferLen) { |
nobukuma | 0:003889bc474f | 50 | CBW cbw; |
nobukuma | 0:003889bc474f | 51 | cbw.Signature = CBW_SIGNATURE; |
nobukuma | 0:003889bc474f | 52 | cbw.Tag = 0; |
nobukuma | 0:003889bc474f | 53 | cbw.TransferLength = transferLen; |
nobukuma | 0:003889bc474f | 54 | cbw.Flags = flags; |
nobukuma | 0:003889bc474f | 55 | cbw.LUN = 0; |
nobukuma | 0:003889bc474f | 56 | cbw.CBLength = cmdLen; |
nobukuma | 0:003889bc474f | 57 | memset(cbw.CB,0,sizeof(cbw.CB)); |
nobukuma | 0:003889bc474f | 58 | memcpy(cbw.CB,cmd,cmdLen); |
nobukuma | 0:003889bc474f | 59 | |
nobukuma | 0:003889bc474f | 60 | int r; |
nobukuma | 0:003889bc474f | 61 | //r = USBBulkTransfer(device,0x01,(u8*)&cbw,31); // Send the command |
nobukuma | 0:003889bc474f | 62 | r = USBBulkTransfer(_device,epout,(u8*)&cbw,31); // Send the command |
nobukuma | 0:003889bc474f | 63 | if (r < 0) { |
nobukuma | 0:003889bc474f | 64 | printf("first transfer returns %d\n", r); |
nobukuma | 0:003889bc474f | 65 | return r; |
nobukuma | 0:003889bc474f | 66 | } |
nobukuma | 0:003889bc474f | 67 | if (data) { |
nobukuma | 0:003889bc474f | 68 | //r = USBBulkTransfer(device,flags | 1,data,transferLen); |
nobukuma | 0:003889bc474f | 69 | r = USBBulkTransfer(_device,flags ? epin : epout,data,transferLen); |
nobukuma | 0:003889bc474f | 70 | if (r < 0) { |
nobukuma | 0:003889bc474f | 71 | printf("second transfer returns %d (flags=%02xH)\n", r, flags); |
nobukuma | 0:003889bc474f | 72 | return r; |
nobukuma | 0:003889bc474f | 73 | } |
nobukuma | 0:003889bc474f | 74 | } |
nobukuma | 0:003889bc474f | 75 | |
nobukuma | 0:003889bc474f | 76 | CSW csw; |
nobukuma | 0:003889bc474f | 77 | csw.Signature = 0; |
nobukuma | 0:003889bc474f | 78 | //r = USBBulkTransfer(device,0x81,(u8*)&csw,13); |
nobukuma | 0:003889bc474f | 79 | r = USBBulkTransfer(_device,epin,(u8*)&csw,13); |
nobukuma | 0:003889bc474f | 80 | if (r < 0) { |
nobukuma | 0:003889bc474f | 81 | printf("third transfer returns %d\n", r); |
nobukuma | 0:003889bc474f | 82 | return r; |
nobukuma | 0:003889bc474f | 83 | } |
nobukuma | 0:003889bc474f | 84 | if (csw.Signature != CSW_SIGNATURE) |
nobukuma | 0:003889bc474f | 85 | return ERR_BAD_CSW_SIGNATURE; |
nobukuma | 0:003889bc474f | 86 | |
nobukuma | 0:003889bc474f | 87 | // ModeSense? |
nobukuma | 0:003889bc474f | 88 | if (csw.Status == 1 && cmd[0] != 3) |
nobukuma | 0:003889bc474f | 89 | return SCSIRequestSense(); |
nobukuma | 0:003889bc474f | 90 | |
nobukuma | 0:003889bc474f | 91 | return csw.Status; |
nobukuma | 0:003889bc474f | 92 | } |
nobukuma | 0:003889bc474f | 93 | |
nobukuma | 0:003889bc474f | 94 | int SCSITestUnitReady() { |
nobukuma | 0:003889bc474f | 95 | u8 cmd[6]; |
nobukuma | 0:003889bc474f | 96 | memset(cmd,0,6); |
nobukuma | 0:003889bc474f | 97 | return DoSCSI(cmd,6,DEVICE_TO_HOST,0,0); |
nobukuma | 0:003889bc474f | 98 | } |
nobukuma | 0:003889bc474f | 99 | |
nobukuma | 0:003889bc474f | 100 | int SCSIRequestSense() { |
nobukuma | 0:003889bc474f | 101 | u8 cmd[6] = {0x03,0,0,0,18,0}; |
nobukuma | 0:003889bc474f | 102 | u8 result[18]; |
nobukuma | 0:003889bc474f | 103 | int r = DoSCSI(cmd,6,DEVICE_TO_HOST,result,18); |
nobukuma | 0:003889bc474f | 104 | return r; |
nobukuma | 0:003889bc474f | 105 | } |
nobukuma | 0:003889bc474f | 106 | |
nobukuma | 0:003889bc474f | 107 | int SCSIInquiry() { |
nobukuma | 0:003889bc474f | 108 | u8 cmd[6] = {0x12,0,0,0,36,0}; |
nobukuma | 0:003889bc474f | 109 | u8 result[36+2]; |
nobukuma | 0:003889bc474f | 110 | result[36] = '\n'; |
nobukuma | 0:003889bc474f | 111 | result[37] = 0; |
nobukuma | 0:003889bc474f | 112 | int r = DoSCSI(cmd,6,DEVICE_TO_HOST,result,36); |
nobukuma | 0:003889bc474f | 113 | if (r == 0) |
nobukuma | 0:003889bc474f | 114 | printf((const char*)result + 8); |
nobukuma | 0:003889bc474f | 115 | return r; |
nobukuma | 0:003889bc474f | 116 | } |
nobukuma | 0:003889bc474f | 117 | |
nobukuma | 0:003889bc474f | 118 | int SCSIReadCapacity(u32* blockCount, u32* blockSize) { |
nobukuma | 0:003889bc474f | 119 | u8 cmd[10] = {0x25,0,0,0,8,0,0,0,0,0}; |
nobukuma | 0:003889bc474f | 120 | u8 result[8]; |
nobukuma | 0:003889bc474f | 121 | *blockSize = 0; |
nobukuma | 0:003889bc474f | 122 | *blockCount = 0; |
nobukuma | 0:003889bc474f | 123 | int r = DoSCSI(cmd,10,DEVICE_TO_HOST,result,8); |
nobukuma | 0:003889bc474f | 124 | if (r == 0) { |
nobukuma | 0:003889bc474f | 125 | *blockCount = BE32(result); |
nobukuma | 0:003889bc474f | 126 | *blockSize = BE32(result+4); |
nobukuma | 0:003889bc474f | 127 | } |
nobukuma | 0:003889bc474f | 128 | return r; |
nobukuma | 0:003889bc474f | 129 | } |
nobukuma | 0:003889bc474f | 130 | |
nobukuma | 0:003889bc474f | 131 | int SCSITransfer(u32 blockAddr, u32 blockCount, u8* dst, u32 blockSize, int direction) { |
nobukuma | 0:003889bc474f | 132 | // USB hardware will only do 4k per transfer |
nobukuma | 0:003889bc474f | 133 | while (blockCount*blockSize > 4096) { |
nobukuma | 0:003889bc474f | 134 | int count = 4096/blockSize; |
nobukuma | 0:003889bc474f | 135 | int r = SCSITransfer(blockAddr,count,dst,blockSize,direction); |
nobukuma | 0:003889bc474f | 136 | dst += count*blockSize; |
nobukuma | 0:003889bc474f | 137 | blockAddr += count; |
nobukuma | 0:003889bc474f | 138 | blockCount -= count; |
nobukuma | 0:003889bc474f | 139 | } |
nobukuma | 0:003889bc474f | 140 | |
nobukuma | 0:003889bc474f | 141 | u8 cmd[10]; |
nobukuma | 0:003889bc474f | 142 | memset(cmd,0,10); |
nobukuma | 0:003889bc474f | 143 | cmd[0] = (direction == DEVICE_TO_HOST) ? 0x28 : 0x2A; |
nobukuma | 0:003889bc474f | 144 | BE32(blockAddr,cmd+2); |
nobukuma | 0:003889bc474f | 145 | BE16(blockCount,cmd+7); |
nobukuma | 0:003889bc474f | 146 | return DoSCSI(cmd,10,direction,dst,blockSize*blockCount); |
nobukuma | 0:003889bc474f | 147 | } |
nobukuma | 0:003889bc474f | 148 | }; |
nobukuma | 0:003889bc474f | 149 | #endif |