Example #1
0
        private CP2112_DLL.HID_SMBUS_STATUS Write2(byte slaveAddress, ushort numBytesToWrite,
                                                   byte OffsetAddr, byte[] WriteBytes)
        {
            CP2112_DLL.HID_SMBUS_STATUS ret = CP2112_DLL.HID_SMBUS_STATUS.HID_SMBUS_SUCCESS;

            if (!isOpened())
            {
                return(CP2112_DLL.HID_SMBUS_STATUS.HID_SMBUS_ERROR);
            }

            byte[] wrbuf          = new byte[numBytesToWrite + 1];
            byte   wradr          = OffsetAddr;
            ushort destlength     = numBytesToWrite;
            ushort destindex      = 0;
            byte   status         = 0;
            byte   detailedstatus = 0;
            ushort numretries     = 0;
            ushort bytesread      = 0;

            while (numBytesToWrite > 0)
            {
                destlength = numBytesToWrite;
                if (numBytesToWrite > writeBlock)
                {
                    destlength = (ushort)writeBlock;
                }
                Array.Copy(WriteBytes, destindex, wrbuf, 1, destlength);
                wrbuf[0] = wradr;
                do
                {
                    ret = CP2112_DLL.HidSmbus_TransferStatusRequest(m_hidSmbus);
                    if (ret != CP2112_DLL.HID_SMBUS_STATUS.HID_SMBUS_SUCCESS)
                    {
                        return(ret);
                    }
                    ret = CP2112_DLL.HidSmbus_GetTransferStatusResponse(m_hidSmbus, ref status, ref detailedstatus,
                                                                        ref numretries, ref bytesread);
                    if (ret != CP2112_DLL.HID_SMBUS_STATUS.HID_SMBUS_SUCCESS)
                    {
                        return(ret);
                    }
                    if (status == (byte)CP2112_DLL.HID_SMBUS_TRANSFER_S0.HID_SMBUS_S0_ERROR)
                    {
                        return(CP2112_DLL.HID_SMBUS_STATUS.HID_SMBUS_ERROR);
                    }
                }while (status == 0x01);
                ret = CP2112_DLL.HidSmbus_WriteRequest(m_hidSmbus, slaveAddress, wrbuf, (byte)(destlength + 1));
                if (ret != CP2112_DLL.HID_SMBUS_STATUS.HID_SMBUS_SUCCESS)
                {
                    return(ret);
                }

                numBytesToWrite -= destlength;
                destindex       += destlength;
                wradr           += (byte)destlength;
            }
            return(ret);
        }
Example #2
0
        private CP2112_DLL.HID_SMBUS_STATUS Write3(byte slaveAddress, ushort numBytesToWrite,
                                                   byte OffsetAddr, byte[] WriteBytes)
        {
            CP2112_DLL.HID_SMBUS_STATUS ret = CP2112_DLL.HID_SMBUS_STATUS.HID_SMBUS_SUCCESS;

            if (!isOpened())
            {
                return(CP2112_DLL.HID_SMBUS_STATUS.HID_SMBUS_ERROR);
            }


            byte   wradr          = OffsetAddr;
            ushort destlength     = numBytesToWrite;
            ushort numBytesToRead = numBytesToWrite;

            byte[] readBytes      = new byte[numBytesToRead];
            ushort destindex      = 0;
            byte   status         = 0;
            byte   detailedstatus = 0;
            ushort numretries     = 0;
            ushort bytesread      = 0;

            while (numBytesToWrite > 0)
            {
                status     = 0;
                destlength = numBytesToWrite;
                if (numBytesToWrite > writeBlock)
                {
                    destlength = (ushort)writeBlock;
                }
                byte[] wrbuf = new byte[destlength + 1];
                Array.Copy(WriteBytes, destindex, wrbuf, 1, destlength);
                wrbuf[0] = wradr;

                ret = CP2112_DLL.HidSmbus_WriteRequest(m_hidSmbus, slaveAddress, wrbuf, (byte)(destlength + 1));
                Util.Delay(delayTime);
                //Console.WriteLine("destlength:" + destlength);
                if (ret != CP2112_DLL.HID_SMBUS_STATUS.HID_SMBUS_SUCCESS)
                {
                    return(ret);
                }
                do
                {
                    ret = CP2112_DLL.HidSmbus_TransferStatusRequest(m_hidSmbus);
                    if (ret != CP2112_DLL.HID_SMBUS_STATUS.HID_SMBUS_SUCCESS)
                    {
                        return(ret);
                    }
                    ret = CP2112_DLL.HidSmbus_GetTransferStatusResponse(m_hidSmbus, ref status, ref detailedstatus,
                                                                        ref numretries, ref bytesread);
                    if (ret != CP2112_DLL.HID_SMBUS_STATUS.HID_SMBUS_SUCCESS)
                    {
                        return(ret);
                    }
                    if (status == (byte)CP2112_DLL.HID_SMBUS_TRANSFER_S0.HID_SMBUS_S0_ERROR)
                    {
                        return(CP2112_DLL.HID_SMBUS_STATUS.HID_SMBUS_ERROR);
                    }
                    //Console.WriteLine("detailedstatus :" + detailedstatus + "status :" + status + "numretries:" + numretries);

                    if (numretries > 999)
                    {
                        break;
                    }
                }while (status != 0x02);



                numBytesToWrite -= destlength;
                destindex       += destlength;
                wradr           += (byte)destlength;
            }

            ret = CP2112_DLL.HidSmbus_CancelTransfer(m_hidSmbus);

            //Util.Delay(50);

            //ret=Read3(slaveAddress, numBytesToRead, OffsetAddr, ref readBytes);

            //if (!Util.arrayCompare(readBytes, WriteBytes))
            //{
            //    ret = CP2112_DLL.HID_SMBUS_STATUS.HID_SMBUS_WRITE_COMAPARE_FAIL;
            //}


            return(ret);
        }