repo time

Dependencies:   mbed MAX14720 MAX30205 USBDevice

Committer:
darienf
Date:
Tue Apr 06 06:41:40 2021 +0000
Revision:
20:6d2af70c92ab
another repo

Who changed what in which revision?

UserRevisionLine numberNew contents of line
darienf 20:6d2af70c92ab 1 using Microsoft.VisualBasic;
darienf 20:6d2af70c92ab 2 using System;
darienf 20:6d2af70c92ab 3 using System.Collections;
darienf 20:6d2af70c92ab 4 using System.Data;
darienf 20:6d2af70c92ab 5 using System.Diagnostics;
darienf 20:6d2af70c92ab 6 using System.Drawing;
darienf 20:6d2af70c92ab 7 using System.Windows.Forms;
darienf 20:6d2af70c92ab 8 using Microsoft.Win32.SafeHandles;
darienf 20:6d2af70c92ab 9 using System.Runtime.InteropServices;
darienf 20:6d2af70c92ab 10
darienf 20:6d2af70c92ab 11
darienf 20:6d2af70c92ab 12 namespace SimpleHID
darienf 20:6d2af70c92ab 13 {
darienf 20:6d2af70c92ab 14 class NimitzHID
darienf 20:6d2af70c92ab 15 {
darienf 20:6d2af70c92ab 16 internal const byte COMMAND_SUCCEED = 0xEE;
darienf 20:6d2af70c92ab 17 internal const byte COMMAND_FAILED = 0xFA;
darienf 20:6d2af70c92ab 18
darienf 20:6d2af70c92ab 19 // private IntPtr deviceNotificationHandle;
darienf 20:6d2af70c92ab 20 // private Boolean exclusiveAccess;
darienf 20:6d2af70c92ab 21 private SafeFileHandle hidHandle;
darienf 20:6d2af70c92ab 22 private String hidUsage;
darienf 20:6d2af70c92ab 23 private Boolean myDeviceDetected;
darienf 20:6d2af70c92ab 24 private String myDevicePathName;
darienf 20:6d2af70c92ab 25 private SafeFileHandle readHandle;
darienf 20:6d2af70c92ab 26 private SafeFileHandle writeHandle;
darienf 20:6d2af70c92ab 27
darienf 20:6d2af70c92ab 28 // private Debugging MyDebugging = new Debugging(); // For viewing results of API calls via Debug.Write.
darienf 20:6d2af70c92ab 29 private Guid hidGuid;
darienf 20:6d2af70c92ab 30 private IntPtr deviceNotificationHandle = System.IntPtr.Zero;
darienf 20:6d2af70c92ab 31 private HID myHID;
darienf 20:6d2af70c92ab 32
darienf 20:6d2af70c92ab 33 //internal FrmMain FrmMy;
darienf 20:6d2af70c92ab 34
darienf 20:6d2af70c92ab 35 /// <summary>
darienf 20:6d2af70c92ab 36 /// Define a class of delegates that point to the Hid.ReportIn.Read function.
darienf 20:6d2af70c92ab 37 /// The delegate has the same parameters as Hid.ReportIn.Read.
darienf 20:6d2af70c92ab 38 /// Used for asynchronous reads from the device.
darienf 20:6d2af70c92ab 39 /// </summary>
darienf 20:6d2af70c92ab 40
darienf 20:6d2af70c92ab 41 private delegate void ReadInputReportDelegate(SafeFileHandle hidHandle, SafeFileHandle readHandle, SafeFileHandle writeHandle, ref Boolean myDeviceDetected, ref Byte[] readBuffer, ref Boolean success);
darienf 20:6d2af70c92ab 42
darienf 20:6d2af70c92ab 43 // This delegate has the same parameters as AccessForm.
darienf 20:6d2af70c92ab 44 // Used in accessing the application's form from a different thread.
darienf 20:6d2af70c92ab 45
darienf 20:6d2af70c92ab 46 private delegate void MarshalToForm(String action, String textToAdd);
darienf 20:6d2af70c92ab 47 /// <summary>
darienf 20:6d2af70c92ab 48 /// Uses a series of API calls to locate a HID-class device
darienf 20:6d2af70c92ab 49 /// by its Vendor ID and Product ID.
darienf 20:6d2af70c92ab 50 /// </summary>
darienf 20:6d2af70c92ab 51 ///
darienf 20:6d2af70c92ab 52 /// <returns>
darienf 20:6d2af70c92ab 53 /// True if the device is detected, False if not detected.
darienf 20:6d2af70c92ab 54 /// </returns>
darienf 20:6d2af70c92ab 55
darienf 20:6d2af70c92ab 56 internal Boolean OpenNimitzH()
darienf 20:6d2af70c92ab 57 {
darienf 20:6d2af70c92ab 58 Boolean deviceFound = false;
darienf 20:6d2af70c92ab 59 String[] devicePathName = new String[128];
darienf 20:6d2af70c92ab 60 Guid hidGuid = Guid.Empty;
darienf 20:6d2af70c92ab 61 Int32 memberIndex = 0;
darienf 20:6d2af70c92ab 62 Int32 myProductID = 0x1365;
darienf 20:6d2af70c92ab 63 Int32 myVendorID = 0x0B6A;
darienf 20:6d2af70c92ab 64 Boolean success = false;
darienf 20:6d2af70c92ab 65
darienf 20:6d2af70c92ab 66 myDeviceDetected = false;
darienf 20:6d2af70c92ab 67
darienf 20:6d2af70c92ab 68 myHID.findHIDs();
darienf 20:6d2af70c92ab 69
darienf 20:6d2af70c92ab 70 // ***
darienf 20:6d2af70c92ab 71 // API function: 'HidD_GetHidGuid
darienf 20:6d2af70c92ab 72 // Purpose: Retrieves the interface class GUID for the HID class.
darienf 20:6d2af70c92ab 73 // Accepts: 'A System.Guid object for storing the GUID.
darienf 20:6d2af70c92ab 74 // ***
darienf 20:6d2af70c92ab 75
darienf 20:6d2af70c92ab 76 Hid.HidD_GetHidGuid(ref hidGuid);
darienf 20:6d2af70c92ab 77
darienf 20:6d2af70c92ab 78 // Fill an array with the device path names of all attached HIDs.
darienf 20:6d2af70c92ab 79
darienf 20:6d2af70c92ab 80 deviceFound = MyDeviceManagement.FindDeviceFromGuid(hidGuid, ref devicePathName);
darienf 20:6d2af70c92ab 81
darienf 20:6d2af70c92ab 82 // If there is at least one HID, attempt to read the Vendor ID and Product ID
darienf 20:6d2af70c92ab 83 // of each device until there is a match or all devices have been examined.
darienf 20:6d2af70c92ab 84
darienf 20:6d2af70c92ab 85 if (deviceFound)
darienf 20:6d2af70c92ab 86 {
darienf 20:6d2af70c92ab 87 memberIndex = 0;
darienf 20:6d2af70c92ab 88
darienf 20:6d2af70c92ab 89 do
darienf 20:6d2af70c92ab 90 {
darienf 20:6d2af70c92ab 91 // ***
darienf 20:6d2af70c92ab 92 // API function:
darienf 20:6d2af70c92ab 93 // CreateFile
darienf 20:6d2af70c92ab 94 // Purpose:
darienf 20:6d2af70c92ab 95 // Retrieves a handle to a device.
darienf 20:6d2af70c92ab 96 // Accepts:
darienf 20:6d2af70c92ab 97 // A device path name returned by SetupDiGetDeviceInterfaceDetail
darienf 20:6d2af70c92ab 98 // The type of access requested (read/write).
darienf 20:6d2af70c92ab 99 // FILE_SHARE attributes to allow other processes to access the device while this handle is open.
darienf 20:6d2af70c92ab 100 // A Security structure or IntPtr.Zero.
darienf 20:6d2af70c92ab 101 // A creation disposition value. Use OPEN_EXISTING for devices.
darienf 20:6d2af70c92ab 102 // Flags and attributes for files. Not used for devices.
darienf 20:6d2af70c92ab 103 // Handle to a template file. Not used.
darienf 20:6d2af70c92ab 104 // Returns: a handle without read or write access.
darienf 20:6d2af70c92ab 105 // This enables obtaining information about all HIDs, even system
darienf 20:6d2af70c92ab 106 // keyboards and mice.
darienf 20:6d2af70c92ab 107 // Separate handles are used for reading and writing.
darienf 20:6d2af70c92ab 108 // ***
darienf 20:6d2af70c92ab 109 hidHandle = FileIO.CreateFile(devicePathName[memberIndex], 0, FileIO.FILE_SHARE_READ | FileIO.FILE_SHARE_WRITE, IntPtr.Zero, FileIO.OPEN_EXISTING, 0, 0);
darienf 20:6d2af70c92ab 110
darienf 20:6d2af70c92ab 111 if (!hidHandle.IsInvalid)
darienf 20:6d2af70c92ab 112 {
darienf 20:6d2af70c92ab 113 // The returned handle is valid,
darienf 20:6d2af70c92ab 114 // so find out if this is the device we're looking for.
darienf 20:6d2af70c92ab 115
darienf 20:6d2af70c92ab 116 // Set the Size property of DeviceAttributes to the number of bytes in the structure.
darienf 20:6d2af70c92ab 117 MyHid.DeviceAttributes.Size = Marshal.SizeOf(MyHid.DeviceAttributes);
darienf 20:6d2af70c92ab 118
darienf 20:6d2af70c92ab 119 // ***
darienf 20:6d2af70c92ab 120 // API function:
darienf 20:6d2af70c92ab 121 // HidD_GetAttributes
darienf 20:6d2af70c92ab 122
darienf 20:6d2af70c92ab 123 // Purpose:
darienf 20:6d2af70c92ab 124 // Retrieves a HIDD_ATTRIBUTES structure containing the Vendor ID,
darienf 20:6d2af70c92ab 125 // Product ID, and Product Version Number for a device.
darienf 20:6d2af70c92ab 126
darienf 20:6d2af70c92ab 127 // Accepts:
darienf 20:6d2af70c92ab 128 // A handle returned by CreateFile.
darienf 20:6d2af70c92ab 129 // A pointer to receive a HIDD_ATTRIBUTES structure.
darienf 20:6d2af70c92ab 130
darienf 20:6d2af70c92ab 131 // Returns:
darienf 20:6d2af70c92ab 132 // True on success, False on failure.
darienf 20:6d2af70c92ab 133 // ***
darienf 20:6d2af70c92ab 134
darienf 20:6d2af70c92ab 135 success = Hid.HidD_GetAttributes(hidHandle, ref MyHid.DeviceAttributes);
darienf 20:6d2af70c92ab 136
darienf 20:6d2af70c92ab 137 if (success)
darienf 20:6d2af70c92ab 138 {
darienf 20:6d2af70c92ab 139 // Find out if the device matches the one we're looking for.
darienf 20:6d2af70c92ab 140 if ((MyHid.DeviceAttributes.VendorID == myVendorID) && (MyHid.DeviceAttributes.ProductID == myProductID))
darienf 20:6d2af70c92ab 141 {
darienf 20:6d2af70c92ab 142 myDeviceDetected = true;
darienf 20:6d2af70c92ab 143 // Save the DevicePathName for OnDeviceChange().
darienf 20:6d2af70c92ab 144 myDevicePathName = devicePathName[memberIndex];
darienf 20:6d2af70c92ab 145 }
darienf 20:6d2af70c92ab 146 else
darienf 20:6d2af70c92ab 147 {
darienf 20:6d2af70c92ab 148 // It's not a match, so close the handle.
darienf 20:6d2af70c92ab 149 myDeviceDetected = false;
darienf 20:6d2af70c92ab 150 hidHandle.Close();
darienf 20:6d2af70c92ab 151 }
darienf 20:6d2af70c92ab 152 }
darienf 20:6d2af70c92ab 153 else
darienf 20:6d2af70c92ab 154 {
darienf 20:6d2af70c92ab 155 // There was a problem in retrieving the information.
darienf 20:6d2af70c92ab 156 myDeviceDetected = false;
darienf 20:6d2af70c92ab 157 hidHandle.Close();
darienf 20:6d2af70c92ab 158 }
darienf 20:6d2af70c92ab 159 }
darienf 20:6d2af70c92ab 160
darienf 20:6d2af70c92ab 161 // Keep looking until we find the device or there are no devices left to examine.
darienf 20:6d2af70c92ab 162 memberIndex = memberIndex + 1;
darienf 20:6d2af70c92ab 163 }
darienf 20:6d2af70c92ab 164 while (!((myDeviceDetected || (memberIndex == devicePathName.Length))));
darienf 20:6d2af70c92ab 165 }
darienf 20:6d2af70c92ab 166
darienf 20:6d2af70c92ab 167 if (myDeviceDetected)
darienf 20:6d2af70c92ab 168 {
darienf 20:6d2af70c92ab 169 // The device was detected.
darienf 20:6d2af70c92ab 170 // Learn the capabilities of the device.
darienf 20:6d2af70c92ab 171 MyHid.Capabilities = MyHid.GetDeviceCapabilities(hidHandle);
darienf 20:6d2af70c92ab 172
darienf 20:6d2af70c92ab 173 // Find out if the device is a system mouse or keyboard.
darienf 20:6d2af70c92ab 174 hidUsage = MyHid.GetHidUsage(MyHid.Capabilities);
darienf 20:6d2af70c92ab 175
darienf 20:6d2af70c92ab 176 // Get handles to use in requesting Input and Output reports.
darienf 20:6d2af70c92ab 177 readHandle = FileIO.CreateFile(myDevicePathName, FileIO.GENERIC_READ, FileIO.FILE_SHARE_READ | FileIO.FILE_SHARE_WRITE, IntPtr.Zero, FileIO.OPEN_EXISTING, FileIO.FILE_FLAG_OVERLAPPED, 0);
darienf 20:6d2af70c92ab 178 writeHandle = FileIO.CreateFile(myDevicePathName, FileIO.GENERIC_WRITE, FileIO.FILE_SHARE_READ | FileIO.FILE_SHARE_WRITE, IntPtr.Zero, FileIO.OPEN_EXISTING, 0, 0);
darienf 20:6d2af70c92ab 179
darienf 20:6d2af70c92ab 180 // Flush any waiting reports in the input buffer. (optional)
darienf 20:6d2af70c92ab 181 MyHid.FlushQueue(readHandle);
darienf 20:6d2af70c92ab 182 }
darienf 20:6d2af70c92ab 183 return myDeviceDetected;
darienf 20:6d2af70c92ab 184 }
darienf 20:6d2af70c92ab 185
darienf 20:6d2af70c92ab 186 internal void CloseNimitzH()
darienf 20:6d2af70c92ab 187 {
darienf 20:6d2af70c92ab 188 if (!(hidHandle == null))
darienf 20:6d2af70c92ab 189 {
darienf 20:6d2af70c92ab 190 if (!(hidHandle.IsInvalid))
darienf 20:6d2af70c92ab 191 {
darienf 20:6d2af70c92ab 192 hidHandle.Close();
darienf 20:6d2af70c92ab 193 }
darienf 20:6d2af70c92ab 194 }
darienf 20:6d2af70c92ab 195
darienf 20:6d2af70c92ab 196 if (!(readHandle == null))
darienf 20:6d2af70c92ab 197 {
darienf 20:6d2af70c92ab 198 if (!(readHandle.IsInvalid))
darienf 20:6d2af70c92ab 199 {
darienf 20:6d2af70c92ab 200 readHandle.Close();
darienf 20:6d2af70c92ab 201 }
darienf 20:6d2af70c92ab 202 }
darienf 20:6d2af70c92ab 203
darienf 20:6d2af70c92ab 204 if (!(writeHandle == null))
darienf 20:6d2af70c92ab 205 {
darienf 20:6d2af70c92ab 206 if (!(writeHandle.IsInvalid))
darienf 20:6d2af70c92ab 207 {
darienf 20:6d2af70c92ab 208 writeHandle.Close();
darienf 20:6d2af70c92ab 209 }
darienf 20:6d2af70c92ab 210 }
darienf 20:6d2af70c92ab 211 }
darienf 20:6d2af70c92ab 212
darienf 20:6d2af70c92ab 213 // This function only checks the status variable and does not actively check
darienf 20:6d2af70c92ab 214 // if the NimitzH is still present. This function
darienf 20:6d2af70c92ab 215 // only checks the variable to avoid any timeout delays.
darienf 20:6d2af70c92ab 216 internal Boolean NimitzHPresent()
darienf 20:6d2af70c92ab 217 {
darienf 20:6d2af70c92ab 218 if (myDeviceDetected)
darienf 20:6d2af70c92ab 219 return true;
darienf 20:6d2af70c92ab 220 else
darienf 20:6d2af70c92ab 221 return false;
darienf 20:6d2af70c92ab 222 }
darienf 20:6d2af70c92ab 223
darienf 20:6d2af70c92ab 224 //////////////////////////////////////////////////////////////////////
darienf 20:6d2af70c92ab 225 // Functions that can be called once NimitzH is opened
darienf 20:6d2af70c92ab 226 //////////////////////////////////////////////////////////////////////
darienf 20:6d2af70c92ab 227
darienf 20:6d2af70c92ab 228 // This function calls the NimitzH Read Revision command.
darienf 20:6d2af70c92ab 229 // This is done by sending 0xC2, and then receiving 1 byte major rev, then 1byte minor Rev, and then 0xD2
darienf 20:6d2af70c92ab 230 // This function returns firmware MAJORVERSION in upper word and MINORVERSION in lower word
darienf 20:6d2af70c92ab 231 // This function returns -1 if an error occurs
darienf 20:6d2af70c92ab 232
darienf 20:6d2af70c92ab 233 internal int GetFirmwareVersion()
darienf 20:6d2af70c92ab 234 {
darienf 20:6d2af70c92ab 235 Boolean success = false;
darienf 20:6d2af70c92ab 236 Byte[] inputBuffer = null;
darienf 20:6d2af70c92ab 237 Byte[] dataOut = new Byte[2];
darienf 20:6d2af70c92ab 238
darienf 20:6d2af70c92ab 239 dataOut[0] = 0x01;//number of bytes to send
darienf 20:6d2af70c92ab 240 dataOut[1] = 0x02;//firmware command
darienf 20:6d2af70c92ab 241
darienf 20:6d2af70c92ab 242 success = InputOutputReports(dataOut, ref inputBuffer);
darienf 20:6d2af70c92ab 243
darienf 20:6d2af70c92ab 244 if (!success)
darienf 20:6d2af70c92ab 245 return -1;
darienf 20:6d2af70c92ab 246
darienf 20:6d2af70c92ab 247 if (inputBuffer[2] == COMMAND_SUCCEED)
darienf 20:6d2af70c92ab 248 return (inputBuffer[3] << 16) + inputBuffer[4];
darienf 20:6d2af70c92ab 249 else
darienf 20:6d2af70c92ab 250 return -1;
darienf 20:6d2af70c92ab 251
darienf 20:6d2af70c92ab 252 }
darienf 20:6d2af70c92ab 253 internal Boolean TestSendPacket(byte[] sendData, ref byte[] readData)
darienf 20:6d2af70c92ab 254 {
darienf 20:6d2af70c92ab 255 Boolean success = false;
darienf 20:6d2af70c92ab 256 Byte[] inputBuffer = null;
darienf 20:6d2af70c92ab 257 byte numData = (byte)sendData.Length;
darienf 20:6d2af70c92ab 258 Byte[] dataOut = new Byte[3 + numData];
darienf 20:6d2af70c92ab 259
darienf 20:6d2af70c92ab 260 if ((numData > 62) || (numData < 0))
darienf 20:6d2af70c92ab 261 return false; // Max packet size is 62 bytes
darienf 20:6d2af70c92ab 262
darienf 20:6d2af70c92ab 263 dataOut[0] = (byte)(0x02 + numData);//number of bytes to send (include command and data bytes)
darienf 20:6d2af70c92ab 264 dataOut[1] = 0x01;//command byte
darienf 20:6d2af70c92ab 265 dataOut[2] = (byte)(numData);//number of data bytes
darienf 20:6d2af70c92ab 266
darienf 20:6d2af70c92ab 267 for (int i = 0; i < numData; i++)
darienf 20:6d2af70c92ab 268 dataOut[i + 3] = sendData[i];
darienf 20:6d2af70c92ab 269
darienf 20:6d2af70c92ab 270 success = InputOutputReports(dataOut, ref inputBuffer);
darienf 20:6d2af70c92ab 271
darienf 20:6d2af70c92ab 272 if (!success)
darienf 20:6d2af70c92ab 273 return false;
darienf 20:6d2af70c92ab 274
darienf 20:6d2af70c92ab 275 for (int i = 0; i < numData; i++)
darienf 20:6d2af70c92ab 276 readData[i] = inputBuffer[i + 3];
darienf 20:6d2af70c92ab 277
darienf 20:6d2af70c92ab 278 if (inputBuffer[2] == COMMAND_SUCCEED)
darienf 20:6d2af70c92ab 279 return true;
darienf 20:6d2af70c92ab 280 else
darienf 20:6d2af70c92ab 281 return false;
darienf 20:6d2af70c92ab 282 }
darienf 20:6d2af70c92ab 283 /****************I2CPacketWrite****************/
darienf 20:6d2af70c92ab 284 /* Description: Generates the correct sequence of events on the I2C bus for a packet write (up to 60 data bytes)
darienf 20:6d2af70c92ab 285 /* Information: New Implementation for packet writes ( SHOULD BE USED in most ALLL Writes/Read of and I2C Device )!!
darienf 20:6d2af70c92ab 286 /* Paramters: slaveAddress = slave address of DUT (R/W# is set in firmware), numBytes = Number of data bytes to write (1 to 60),
darienf 20:6d2af70c92ab 287 /* memoryaddress = Desired memory location, databuffer = pointer to the data to write on the buss*/
darienf 20:6d2af70c92ab 288 internal Boolean I2CWritePacket(Byte slaveAddress, Byte memoryaddress, Byte numBytes, Byte[] databuffer)
darienf 20:6d2af70c92ab 289 {
darienf 20:6d2af70c92ab 290 Boolean success = false;
darienf 20:6d2af70c92ab 291 Byte[] inputBuffer = null;
darienf 20:6d2af70c92ab 292 Byte[] dataOut = new Byte[64];
darienf 20:6d2af70c92ab 293
darienf 20:6d2af70c92ab 294 if ((numBytes > 59) || (numBytes < 0))
darienf 20:6d2af70c92ab 295 return false; // Max packet size is 60 bytes
darienf 20:6d2af70c92ab 296
darienf 20:6d2af70c92ab 297 dataOut[0] = (byte)(numBytes + 4);
darienf 20:6d2af70c92ab 298 dataOut[1] = 0x20;
darienf 20:6d2af70c92ab 299 dataOut[2] = slaveAddress;
darienf 20:6d2af70c92ab 300 dataOut[3] = memoryaddress;
darienf 20:6d2af70c92ab 301 dataOut[4] = numBytes;
darienf 20:6d2af70c92ab 302
darienf 20:6d2af70c92ab 303 for (int i = 0; i < numBytes; i++)
darienf 20:6d2af70c92ab 304 dataOut[i + 5] = databuffer[i];
darienf 20:6d2af70c92ab 305
darienf 20:6d2af70c92ab 306 success = InputOutputReports(dataOut, ref inputBuffer);
darienf 20:6d2af70c92ab 307
darienf 20:6d2af70c92ab 308 if (!success)
darienf 20:6d2af70c92ab 309 return false;
darienf 20:6d2af70c92ab 310
darienf 20:6d2af70c92ab 311 if (inputBuffer[2] == COMMAND_SUCCEED)
darienf 20:6d2af70c92ab 312 return true;
darienf 20:6d2af70c92ab 313 else
darienf 20:6d2af70c92ab 314 return false;
darienf 20:6d2af70c92ab 315 }
darienf 20:6d2af70c92ab 316
darienf 20:6d2af70c92ab 317 /****************I2CPacketRead****************/
darienf 20:6d2af70c92ab 318 /* Description: Generates the correct sequence of events on the I2C bus for a packet read (up to 60 data bytes)
darienf 20:6d2af70c92ab 319 /* Information: New Implementation for packet reads ( SHOULD BE USED in most ALLL Writes/Read of and I2C Device )!!
darienf 20:6d2af70c92ab 320 /* Paramters: slaveAddress = slave address of DUT (R/W# is set in firmware), numBytes = Number of data bytes to write (1 to 60),
darienf 20:6d2af70c92ab 321 /* memoryaddress = Desired memory location, databuffer = pointer to the data read should be saved*/
darienf 20:6d2af70c92ab 322 internal Boolean I2CReadPacket(Byte slaveAddress, Byte memoryaddress, Byte numBytes, ref Byte[] databuffer)
darienf 20:6d2af70c92ab 323 {
darienf 20:6d2af70c92ab 324 Boolean success = false;
darienf 20:6d2af70c92ab 325 Byte[] inputBuffer = null;
darienf 20:6d2af70c92ab 326 Byte[] dataOut = new Byte[5];
darienf 20:6d2af70c92ab 327
darienf 20:6d2af70c92ab 328 if ((numBytes > 62) || (numBytes < 1))
darienf 20:6d2af70c92ab 329 return false; // Max packet size is 62 bytes
darienf 20:6d2af70c92ab 330
darienf 20:6d2af70c92ab 331 dataOut[0] = 0x04;
darienf 20:6d2af70c92ab 332 dataOut[1] = 0x21;
darienf 20:6d2af70c92ab 333 dataOut[2] = (byte)(slaveAddress | 0x01);
darienf 20:6d2af70c92ab 334 dataOut[3] = memoryaddress;
darienf 20:6d2af70c92ab 335 dataOut[4] = numBytes;
darienf 20:6d2af70c92ab 336
darienf 20:6d2af70c92ab 337 success = InputOutputReports(dataOut, ref inputBuffer);
darienf 20:6d2af70c92ab 338
darienf 20:6d2af70c92ab 339 if (!success)
darienf 20:6d2af70c92ab 340 return false;
darienf 20:6d2af70c92ab 341
darienf 20:6d2af70c92ab 342 for (int i = 0; i < numBytes; i++)
darienf 20:6d2af70c92ab 343 databuffer[i] = inputBuffer[i + 3];
darienf 20:6d2af70c92ab 344
darienf 20:6d2af70c92ab 345 if (inputBuffer[2] == COMMAND_SUCCEED)
darienf 20:6d2af70c92ab 346 return true;
darienf 20:6d2af70c92ab 347 else
darienf 20:6d2af70c92ab 348 return false;
darienf 20:6d2af70c92ab 349 }
darienf 20:6d2af70c92ab 350
darienf 20:6d2af70c92ab 351 internal Boolean SPIReadPacket(Byte address, Byte numBytes, ref Byte[] data)
darienf 20:6d2af70c92ab 352 {
darienf 20:6d2af70c92ab 353 Boolean success = false;
darienf 20:6d2af70c92ab 354 Byte[] inputBuffer = null;
darienf 20:6d2af70c92ab 355 Byte[] dataOut = new Byte[4];
darienf 20:6d2af70c92ab 356
darienf 20:6d2af70c92ab 357 if ((numBytes > 62) || (numBytes < 1))
darienf 20:6d2af70c92ab 358 return false; // Max packet size is 62 bytes
darienf 20:6d2af70c92ab 359
darienf 20:6d2af70c92ab 360 dataOut[0] = 3;
darienf 20:6d2af70c92ab 361 dataOut[1] = 0x41;
darienf 20:6d2af70c92ab 362 dataOut[2] = address;
darienf 20:6d2af70c92ab 363 dataOut[3] = numBytes;
darienf 20:6d2af70c92ab 364
darienf 20:6d2af70c92ab 365 success = InputOutputReports(dataOut, ref inputBuffer);
darienf 20:6d2af70c92ab 366
darienf 20:6d2af70c92ab 367 if (!success)
darienf 20:6d2af70c92ab 368 return false;
darienf 20:6d2af70c92ab 369
darienf 20:6d2af70c92ab 370 for (int i = 0; i < numBytes; i++)
darienf 20:6d2af70c92ab 371 data[i] = inputBuffer[i + 3];
darienf 20:6d2af70c92ab 372
darienf 20:6d2af70c92ab 373 if (inputBuffer[2] == COMMAND_SUCCEED)
darienf 20:6d2af70c92ab 374 return true;
darienf 20:6d2af70c92ab 375 else
darienf 20:6d2af70c92ab 376 return false;
darienf 20:6d2af70c92ab 377 }
darienf 20:6d2af70c92ab 378
darienf 20:6d2af70c92ab 379 internal Boolean SPIStartSampling(Byte address, Byte numBytes, uint numSamples, ref Byte[] data)
darienf 20:6d2af70c92ab 380 {
darienf 20:6d2af70c92ab 381 Boolean success = false;
darienf 20:6d2af70c92ab 382 Byte[] inputBuffer = null;
darienf 20:6d2af70c92ab 383 Byte[] dataOut = new Byte[6];
darienf 20:6d2af70c92ab 384
darienf 20:6d2af70c92ab 385 if ((numBytes > 62) || (numBytes < 1))
darienf 20:6d2af70c92ab 386 return false; // Max packet size is 62 bytes
darienf 20:6d2af70c92ab 387
darienf 20:6d2af70c92ab 388 dataOut[0] = 5;
darienf 20:6d2af70c92ab 389 dataOut[1] = 0x45;
darienf 20:6d2af70c92ab 390 dataOut[2] = address;
darienf 20:6d2af70c92ab 391 dataOut[3] = numBytes;
darienf 20:6d2af70c92ab 392 dataOut[4] = (byte)(numSamples >> 8);
darienf 20:6d2af70c92ab 393 dataOut[5] = (byte)numSamples;
darienf 20:6d2af70c92ab 394
darienf 20:6d2af70c92ab 395 success = InputOutputReports(dataOut, ref inputBuffer);
darienf 20:6d2af70c92ab 396
darienf 20:6d2af70c92ab 397 if (!success)
darienf 20:6d2af70c92ab 398 return false;
darienf 20:6d2af70c92ab 399
darienf 20:6d2af70c92ab 400 for (int i = 0; i < numBytes; i++)
darienf 20:6d2af70c92ab 401 data[i] = inputBuffer[i + 3];
darienf 20:6d2af70c92ab 402
darienf 20:6d2af70c92ab 403 if (inputBuffer[2] == COMMAND_SUCCEED)
darienf 20:6d2af70c92ab 404 return true;
darienf 20:6d2af70c92ab 405 else
darienf 20:6d2af70c92ab 406 return false;
darienf 20:6d2af70c92ab 407 }
darienf 20:6d2af70c92ab 408
darienf 20:6d2af70c92ab 409 internal bool SPIWritePacket(Byte address, Byte numBytes, Byte[] data)
darienf 20:6d2af70c92ab 410 {
darienf 20:6d2af70c92ab 411 Boolean success = false;
darienf 20:6d2af70c92ab 412 Byte[] inputBuffer = null;
darienf 20:6d2af70c92ab 413 Byte[] dataOut = new Byte[64];
darienf 20:6d2af70c92ab 414
darienf 20:6d2af70c92ab 415 if ((numBytes > 60) || (numBytes < 1))
darienf 20:6d2af70c92ab 416 return false; // Max packet size is 60 bytes
darienf 20:6d2af70c92ab 417
darienf 20:6d2af70c92ab 418 dataOut[0] = (byte)(numBytes + 3);
darienf 20:6d2af70c92ab 419 dataOut[1] = 0x40;
darienf 20:6d2af70c92ab 420 dataOut[2] = address;
darienf 20:6d2af70c92ab 421 dataOut[3] = numBytes;
darienf 20:6d2af70c92ab 422
darienf 20:6d2af70c92ab 423 for (int i = 0; i < numBytes; i++)
darienf 20:6d2af70c92ab 424 dataOut[4 + i] = data[i];
darienf 20:6d2af70c92ab 425
darienf 20:6d2af70c92ab 426 success = InputOutputReports(dataOut, ref inputBuffer);
darienf 20:6d2af70c92ab 427
darienf 20:6d2af70c92ab 428 if (!success)
darienf 20:6d2af70c92ab 429 return false;
darienf 20:6d2af70c92ab 430
darienf 20:6d2af70c92ab 431 if (inputBuffer[2] == COMMAND_SUCCEED)
darienf 20:6d2af70c92ab 432 return true;
darienf 20:6d2af70c92ab 433 else
darienf 20:6d2af70c92ab 434 return false;
darienf 20:6d2af70c92ab 435 }
darienf 20:6d2af70c92ab 436 /// <summary>
darienf 20:6d2af70c92ab 437 /// Sends an Output report, then retrieves an Input report.
darienf 20:6d2af70c92ab 438 /// Assumes report ID = 0 for both reports.
darienf 20:6d2af70c92ab 439 /// </summary>
darienf 20:6d2af70c92ab 440
darienf 20:6d2af70c92ab 441 private Boolean InputOutputReports(Byte[] data, ref Byte[] inputReportBuffer)
darienf 20:6d2af70c92ab 442 {
darienf 20:6d2af70c92ab 443 // Byte[] inputReportBuffer = null;
darienf 20:6d2af70c92ab 444 Byte[] outputReportBuffer = null;
darienf 20:6d2af70c92ab 445 Boolean success = false;
darienf 20:6d2af70c92ab 446
darienf 20:6d2af70c92ab 447 success = false;
darienf 20:6d2af70c92ab 448
darienf 20:6d2af70c92ab 449 // Don't attempt to exchange reports if valid handles aren't available
darienf 20:6d2af70c92ab 450 // (as for a mouse or keyboard under Windows 2000/XP.)
darienf 20:6d2af70c92ab 451
darienf 20:6d2af70c92ab 452 if (!readHandle.IsInvalid && !writeHandle.IsInvalid)
darienf 20:6d2af70c92ab 453 {
darienf 20:6d2af70c92ab 454 // Don't attempt to send an Output report if the HID has no Output report.
darienf 20:6d2af70c92ab 455 if (MyHid.Capabilities.OutputReportByteLength > 0)
darienf 20:6d2af70c92ab 456 {
darienf 20:6d2af70c92ab 457 // Set the size of the Output report buffer.
darienf 20:6d2af70c92ab 458
darienf 20:6d2af70c92ab 459 outputReportBuffer = new Byte[MyHid.Capabilities.OutputReportByteLength];
darienf 20:6d2af70c92ab 460
darienf 20:6d2af70c92ab 461 outputReportBuffer[0] = 0; // Ensure Report ID = 0
darienf 20:6d2af70c92ab 462
darienf 20:6d2af70c92ab 463
darienf 20:6d2af70c92ab 464 for (int i = 0; i < data.Length; i++)
darienf 20:6d2af70c92ab 465 outputReportBuffer[i + 1] = data[i];
darienf 20:6d2af70c92ab 466
darienf 20:6d2af70c92ab 467 // Use WriteFile to send the report.
darienf 20:6d2af70c92ab 468 // If the HID has an interrupt OUT endpoint, WriteFile uses an
darienf 20:6d2af70c92ab 469 // interrupt transfer to send the report.
darienf 20:6d2af70c92ab 470 // If not, WriteFile uses a control transfer.
darienf 20:6d2af70c92ab 471
darienf 20:6d2af70c92ab 472 Hid.OutputReportViaInterruptTransfer myOutputReport = new Hid.OutputReportViaInterruptTransfer();
darienf 20:6d2af70c92ab 473 success = myOutputReport.Write(outputReportBuffer, writeHandle);
darienf 20:6d2af70c92ab 474 }
darienf 20:6d2af70c92ab 475
darienf 20:6d2af70c92ab 476 if (!success)
darienf 20:6d2af70c92ab 477 return false;
darienf 20:6d2af70c92ab 478
darienf 20:6d2af70c92ab 479 // Read an Input report
darienf 20:6d2af70c92ab 480 success = false;
darienf 20:6d2af70c92ab 481
darienf 20:6d2af70c92ab 482 // Don't attempt to send an Input report if the HID has no Input report.
darienf 20:6d2af70c92ab 483 // (The HID spec requires all HIDs to have an interrupt IN endpoint,
darienf 20:6d2af70c92ab 484 // which suggests that all HIDs must support Input reports.)
darienf 20:6d2af70c92ab 485
darienf 20:6d2af70c92ab 486 if (MyHid.Capabilities.InputReportByteLength > 0)
darienf 20:6d2af70c92ab 487 {
darienf 20:6d2af70c92ab 488 // Set the size of the Input report buffer.
darienf 20:6d2af70c92ab 489 inputReportBuffer = new Byte[MyHid.Capabilities.InputReportByteLength];
darienf 20:6d2af70c92ab 490
darienf 20:6d2af70c92ab 491 Hid.InputReportViaInterruptTransfer myInputReport = new Hid.InputReportViaInterruptTransfer();
darienf 20:6d2af70c92ab 492 myInputReport.Read(hidHandle, readHandle, writeHandle, ref myDeviceDetected, ref inputReportBuffer, ref success);
darienf 20:6d2af70c92ab 493
darienf 20:6d2af70c92ab 494 }
darienf 20:6d2af70c92ab 495
darienf 20:6d2af70c92ab 496 }
darienf 20:6d2af70c92ab 497 return success;
darienf 20:6d2af70c92ab 498 }
darienf 20:6d2af70c92ab 499 }
darienf 20:6d2af70c92ab 500 }