Controlling GPIO in CP210x C#

风流意气都作罢 提交于 2021-02-07 09:38:55

问题


I need to control the GPIO pins of a CP210x device using CP210xManufacturing.dll and CP210xRuntime.dll provided by Silicon Labs. I manage to open and close the device and to get the part number. (In my case a CP2105).

I think I successfully read the latch, but I'm not sure. I also call the write latch function, and no error is returned and neither do any pins toggle.

According to the provided utility (CP21xxCustomizationUtility.exe) it shows that both ports are in GPIO mode.

Here is my code: using System; using System.Runtime.InteropServices;

namespace CP210x
{
   public class CP210x
   {
      [DllImport("CP210xManufacturing.dll")]
      private static extern Int32 CP210x_GetNumDevices(ref Int32 numOfDevices);
      public static Int32 GetNumDevices(ref Int32 numOfDevices)
      {
         return CP210x_GetNumDevices(ref numOfDevices);
      } 

      [DllImport("CP210xManufacturing.dll")]
      private static extern Int32 CP210x_Open(Int32 deviceNum, ref IntPtr handle);
      public static Int32 Open(Int32 deviceNum, ref IntPtr handle)
      {
         return CP210x_Open(deviceNum, ref handle);
      }

      [DllImport("CP210xManufacturing.dll")]
      private static extern Int32 CP210x_Close(IntPtr handle);
      public static Int32 Close(IntPtr handle)
      {
         return CP210x_Close(handle);
      }

      [DllImport("CP210xManufacturing.dll")]
      private static extern Int32 CP210x_GetPartNumber(IntPtr handle, Byte[] lpbPartNum);
      public static Int32 GetPartNumber(IntPtr handle, Byte[] lpbPartNum)
      {
         return CP210x_GetPartNumber(handle, lpbPartNum);
      }

      [DllImport("CP210xRuntime.dll")]
      private static extern Int32 CP210xRT_WriteLatch(IntPtr handle, UInt16 mask, UInt16 latch);
      public static Int32 WriteLatch(IntPtr handle, UInt16 mask, UInt16 latch)
      {
         return CP210xRT_WriteLatch(handle, mask, latch);
      }

      [DllImport("CP210xRuntime.dll")]
      private static extern Int32 CP210xRT_ReadLatch(IntPtr handle, UInt16[] lpLatch);
      public static Int32 ReadLatch(IntPtr handle, UInt16[] lpLatch)
      {
         return CP210xRT_ReadLatch(handle, lpLatch);
      }
   }
}

and the function in my class calling the DLL methods:

private static void ResetTelit()
{
   Int32 numOfDevices = 0;
   Int32 retVal = CP210x.CP210x.GetNumDevices(ref numOfDevices);
   IntPtr handle = IntPtr.Zero;
   Byte[] prtNum = new Byte[1];
   UInt16[] latch = new UInt16[8];
   UInt16 mask = 0x01;
   if (numOfDevices > 0)
   {
      retVal = CP210x.CP210x.Open(0, ref handle);
      retVal = CP210x.CP210x.GetPartNumber(handle, prtNum);
      if (prtNum[0] == 5)
      {
         retVal = CP210x.CP210x.ReadLatch(handle, latch);  

         for (Int32 idx = 0; idx < 16; idx++)
         {
            retVal = CP210x.CP210x.WriteLatch(handle, (UInt16)(mask << idx), 0x01);
         }               
      }
      retVal = CP210x.CP210x.Close(handle);
   }
}

I do realise that the issue might be in the DLL wrapper, but I can't figure it out.

Refer to Windows Data Types. This assists in the DLL wrapper.

Refer to CP21xxCustomizationUtility. This contains the DLLs.

Google "silicon labs an169" to get the USBXpress® Programmer’s Guide.

So the question is what is wrong here? How do I toggle the GPIO pins?


回答1:


The answer to the question is that the code is fine. The code works. The issue is the IC. The code works fine with the CP2103, but not so good with the CP2105. It seems like the CP2105 is setup differently.



来源:https://stackoverflow.com/questions/43914716/controlling-gpio-in-cp210x-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!