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 Nobuaki Aoki

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?

UserRevisionLine numberNew contents of line
nobukuma 0:003889bc474f 1
nobukuma 0:003889bc474f 2 /*
nobukuma 0:003889bc474f 3 Copyright (c) 2010 Peter Barrett
nobukuma 0:003889bc474f 4
nobukuma 0:003889bc474f 5 Permission is hereby granted, free of charge, to any person obtaining a copy
nobukuma 0:003889bc474f 6 of this software and associated documentation files (the "Software"), to deal
nobukuma 0:003889bc474f 7 in the Software without restriction, including without limitation the rights
nobukuma 0:003889bc474f 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
nobukuma 0:003889bc474f 9 copies of the Software, and to permit persons to whom the Software is
nobukuma 0:003889bc474f 10 furnished to do so, subject to the following conditions:
nobukuma 0:003889bc474f 11
nobukuma 0:003889bc474f 12 The above copyright notice and this permission notice shall be included in
nobukuma 0:003889bc474f 13 all copies or substantial portions of the Software.
nobukuma 0:003889bc474f 14
nobukuma 0:003889bc474f 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
nobukuma 0:003889bc474f 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
nobukuma 0:003889bc474f 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
nobukuma 0:003889bc474f 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
nobukuma 0:003889bc474f 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
nobukuma 0:003889bc474f 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
nobukuma 0:003889bc474f 21 THE SOFTWARE.
nobukuma 0:003889bc474f 22 */
nobukuma 0:003889bc474f 23
nobukuma 0:003889bc474f 24 #include "stdlib.h"
nobukuma 0:003889bc474f 25 #include "stdio.h"
nobukuma 0:003889bc474f 26 #include "string.h"
nobukuma 0:003889bc474f 27
nobukuma 0:003889bc474f 28 #include "MassStorage.h"
nobukuma 0:003889bc474f 29
nobukuma 0:003889bc474f 30 #if 0
nobukuma 0:003889bc474f 31 int SCSIRequestSense(int device);
nobukuma 0:003889bc474f 32
nobukuma 0:003889bc474f 33 int DoSCSI(int device, const u8* cmd, int cmdLen, int flags, u8* data, u32 transferLen) {
nobukuma 0:003889bc474f 34 CBW cbw;
nobukuma 0:003889bc474f 35 cbw.Signature = CBW_SIGNATURE;
nobukuma 0:003889bc474f 36 cbw.Tag = 0;
nobukuma 0:003889bc474f 37 cbw.TransferLength = transferLen;
nobukuma 0:003889bc474f 38 cbw.Flags = flags;
nobukuma 0:003889bc474f 39 cbw.LUN = 0;
nobukuma 0:003889bc474f 40 cbw.CBLength = cmdLen;
nobukuma 0:003889bc474f 41 memset(cbw.CB,0,sizeof(cbw.CB));
nobukuma 0:003889bc474f 42 memcpy(cbw.CB,cmd,cmdLen);
nobukuma 0:003889bc474f 43
nobukuma 0:003889bc474f 44 int r;
nobukuma 0:003889bc474f 45 //r = USBBulkTransfer(device,0x01,(u8*)&cbw,31); // Send the command
nobukuma 0:003889bc474f 46 r = USBBulkTransfer(device,BULK_ENDPOINT_OUT,(u8*)&cbw,31); // Send the command
nobukuma 0:003889bc474f 47 if (r < 0) {
nobukuma 0:003889bc474f 48 printf("first transfer returns %d\n", r);
nobukuma 0:003889bc474f 49 return r;
nobukuma 0:003889bc474f 50 }
nobukuma 0:003889bc474f 51 if (data) {
nobukuma 0:003889bc474f 52 //r = USBBulkTransfer(device,flags | 1,data,transferLen);
nobukuma 0:003889bc474f 53 r = USBBulkTransfer(device,flags ? BULK_ENDPOINT_IN : BULK_ENDPOINT_OUT,data,transferLen);
nobukuma 0:003889bc474f 54 if (r < 0) {
nobukuma 0:003889bc474f 55 printf("second transfer returns %d (flags=%02xH)\n", r, flags);
nobukuma 0:003889bc474f 56 return r;
nobukuma 0:003889bc474f 57 }
nobukuma 0:003889bc474f 58 }
nobukuma 0:003889bc474f 59
nobukuma 0:003889bc474f 60 CSW csw;
nobukuma 0:003889bc474f 61 csw.Signature = 0;
nobukuma 0:003889bc474f 62 //r = USBBulkTransfer(device,0x81,(u8*)&csw,13);
nobukuma 0:003889bc474f 63 r = USBBulkTransfer(device,BULK_ENDPOINT_IN,(u8*)&csw,13);
nobukuma 0:003889bc474f 64 if (r < 0) {
nobukuma 0:003889bc474f 65 printf("third transfer returns %d\n", r);
nobukuma 0:003889bc474f 66 return r;
nobukuma 0:003889bc474f 67 }
nobukuma 0:003889bc474f 68 if (csw.Signature != CSW_SIGNATURE)
nobukuma 0:003889bc474f 69 return ERR_BAD_CSW_SIGNATURE;
nobukuma 0:003889bc474f 70
nobukuma 0:003889bc474f 71 // ModeSense?
nobukuma 0:003889bc474f 72 if (csw.Status == 1 && cmd[0] != 3)
nobukuma 0:003889bc474f 73 return SCSIRequestSense(device);
nobukuma 0:003889bc474f 74
nobukuma 0:003889bc474f 75 return csw.Status;
nobukuma 0:003889bc474f 76 }
nobukuma 0:003889bc474f 77
nobukuma 0:003889bc474f 78 int SCSITestUnitReady(int device) {
nobukuma 0:003889bc474f 79 u8 cmd[6];
nobukuma 0:003889bc474f 80 memset(cmd,0,6);
nobukuma 0:003889bc474f 81 return DoSCSI(device,cmd,6,DEVICE_TO_HOST,0,0);
nobukuma 0:003889bc474f 82 }
nobukuma 0:003889bc474f 83
nobukuma 0:003889bc474f 84 int SCSIRequestSense(int device) {
nobukuma 0:003889bc474f 85 u8 cmd[6] = {0x03,0,0,0,18,0};
nobukuma 0:003889bc474f 86 u8 result[18];
nobukuma 0:003889bc474f 87 int r = DoSCSI(device,cmd,6,DEVICE_TO_HOST,result,18);
nobukuma 0:003889bc474f 88 return r;
nobukuma 0:003889bc474f 89 }
nobukuma 0:003889bc474f 90
nobukuma 0:003889bc474f 91 int SCSIInquiry(int device) {
nobukuma 0:003889bc474f 92 u8 cmd[6] = {0x12,0,0,0,36,0};
nobukuma 0:003889bc474f 93 u8 result[36+2];
nobukuma 0:003889bc474f 94 result[36] = '\n';
nobukuma 0:003889bc474f 95 result[37] = 0;
nobukuma 0:003889bc474f 96 int r = DoSCSI(device,cmd,6,DEVICE_TO_HOST,result,36);
nobukuma 0:003889bc474f 97 if (r == 0)
nobukuma 0:003889bc474f 98 printf((const char*)result + 8);
nobukuma 0:003889bc474f 99 return r;
nobukuma 0:003889bc474f 100 }
nobukuma 0:003889bc474f 101
nobukuma 0:003889bc474f 102 int SCSIReadCapacity(int device, u32* blockCount, u32* blockSize) {
nobukuma 0:003889bc474f 103 u8 cmd[10] = {0x25,0,0,0,8,0,0,0,0,0};
nobukuma 0:003889bc474f 104 u8 result[8];
nobukuma 0:003889bc474f 105 *blockSize = 0;
nobukuma 0:003889bc474f 106 *blockCount = 0;
nobukuma 0:003889bc474f 107 int r = DoSCSI(device,cmd,10,DEVICE_TO_HOST,result,8);
nobukuma 0:003889bc474f 108 if (r == 0) {
nobukuma 0:003889bc474f 109 *blockCount = BE32(result);
nobukuma 0:003889bc474f 110 *blockSize = BE32(result+4);
nobukuma 0:003889bc474f 111 }
nobukuma 0:003889bc474f 112 return r;
nobukuma 0:003889bc474f 113 }
nobukuma 0:003889bc474f 114
nobukuma 0:003889bc474f 115 int SCSITransfer(int device, u32 blockAddr, u32 blockCount, u8* dst, u32 blockSize, int direction) {
nobukuma 0:003889bc474f 116 // USB hardware will only do 4k per transfer
nobukuma 0:003889bc474f 117 while (blockCount*blockSize > 4096) {
nobukuma 0:003889bc474f 118 int count = 4096/blockSize;
nobukuma 0:003889bc474f 119 int r = SCSITransfer(device,blockAddr,count,dst,blockSize,direction);
nobukuma 0:003889bc474f 120 dst += count*blockSize;
nobukuma 0:003889bc474f 121 blockAddr += count;
nobukuma 0:003889bc474f 122 blockCount -= count;
nobukuma 0:003889bc474f 123 }
nobukuma 0:003889bc474f 124
nobukuma 0:003889bc474f 125 u8 cmd[10];
nobukuma 0:003889bc474f 126 memset(cmd,0,10);
nobukuma 0:003889bc474f 127 cmd[0] = (direction == DEVICE_TO_HOST) ? 0x28 : 0x2A;
nobukuma 0:003889bc474f 128 BE32(blockAddr,cmd+2);
nobukuma 0:003889bc474f 129 BE16(blockCount,cmd+7);
nobukuma 0:003889bc474f 130 return DoSCSI(device,cmd,10,direction,dst,blockSize*blockCount);
nobukuma 0:003889bc474f 131 }
nobukuma 0:003889bc474f 132
nobukuma 0:003889bc474f 133 int MassStorage_ReadCapacity(int device, u32* blockCount, u32* blockSize) {
nobukuma 0:003889bc474f 134 int result = SCSIReadCapacity(device,blockCount,blockSize);
nobukuma 0:003889bc474f 135 printf("MSReadCapacity(%d) = %d, blocks=%u, blksiz=%u\n", device, result, *blockCount, *blockSize);
nobukuma 0:003889bc474f 136 return result;
nobukuma 0:003889bc474f 137 }
nobukuma 0:003889bc474f 138
nobukuma 0:003889bc474f 139 int MassStorage_Read(int device, u32 blockAddr, u32 blockCount, u8* dst, u32 blockSize = 512) {
nobukuma 0:003889bc474f 140 int result = SCSITransfer(device,blockAddr,blockCount,dst,blockSize,DEVICE_TO_HOST);
nobukuma 0:003889bc474f 141 printf("MSRead(%d, %u, %u, %p, %d) = %d\n", device, blockAddr, blockCount, dst, blockSize, result);
nobukuma 0:003889bc474f 142 return result;
nobukuma 0:003889bc474f 143 }
nobukuma 0:003889bc474f 144
nobukuma 0:003889bc474f 145 int MassStorage_Write(int device, u32 blockAddr, u32 blockCount, u8* dst, u32 blockSize = 512) {
nobukuma 0:003889bc474f 146 int result = SCSITransfer(device,blockAddr,blockCount,dst,blockSize,HOST_TO_DEVICE);
nobukuma 0:003889bc474f 147 printf("MSWrite(%d, %u, %u, %p, %d) = %d\n", device, blockAddr, blockCount, dst, blockSize, result);
nobukuma 0:003889bc474f 148 return result;
nobukuma 0:003889bc474f 149 }
nobukuma 0:003889bc474f 150 #endif