This is the latest working repository used in our demo video for the Maxim to display temperature readings on Bluetooth

Dependencies:   USBDevice

Committer:
darienf
Date:
Sat Apr 10 03:05:42 2021 +0000
Revision:
3:36de8b9e4b1a
ayoooo

Who changed what in which revision?

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