CyPD3125 已经修改I2C地址无效

公告

大中华汽车电子生态圈社区并入开发者社区- 更多资讯点击此

Tip / 登入 to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
brzh_4088906
Level 2
Level 2
10 replies posted 5 replies posted First question asked

大家好!

我有一个需求,需要使用65215 通过I2C 链接两颗Cypd3125,其中一颗(u1)地址为0x08(默认地址),另一颗(u2)使用硬件修改I2C地址为0x40,奇怪的是u1正常,u2无法识别,把u1 I2C断开,U2 I2C地址修改为0x08,可以正常使用HPIv2通信及更新FW,有没有人遇到过这样的问题,分享一下?

0 点赞
3 回复数
YiZ_31
Moderator
Moderator
Moderator
1000 replies posted 750 replies posted 500 replies posted

您看如果您单独只接一颗CCG3把address改为0x40是否可以识别。你用于下拉SWD_CLK的电阻阻值是多少?是否可以测量下上电时这个脚的电平?

0 点赞
brzh_4088906
Level 2
Level 2
10 replies posted 5 replies posted First question asked

Hi,

我有单独接一颗CCG3, 用于下拉SWD_CLK的电阻阻值为10k,上电后测量电压为0V,根据datasheet,此时为0x40。在未烧录任何fw时,使用example 例程I2CMaster读不到0x40设备,0x08倒是可以读到,请问是不是bootloader中并未对SWD_CLK做处理,或者只做了高阻抗(float)的处理,而没有对Low处理?请帮忙看看CYPD3125-40LQXI_notebook demo main.c  函数 get_hpi_slave_addr(void)对SWD_CLK的处理是否正确。

谢谢

Brack Zhang

0 点赞
brzh_4088906
Level 2
Level 2
10 replies posted 5 replies posted First question asked

Hi,

我自己写了一个使用HPIv2 更新CYPD3125的更新code,请帮忙看一下,硬件I2C地址是0x08时,感觉可以,但把硬件I2C地址修改为0x40,后不能更新,请帮忙看看,谢谢

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Runtime.InteropServices;
using System.IO;
using System.Threading;

namespace MTIPD3125FW
{
    public partial class MTIPD3125FW
    {
        /// <summary>
        /// Success
        /// </summary>       
        internal const int ERR_SUCCESS = 0;
        /// <summary>
        /// Error opening communication channel
        /// </summary>
        internal const int ERR_OPEN = 1;
        /// <summary>
        /// Error closing communication channel
        /// </summary>
        internal const int ERR_CLOSE = 2;
        /// <summary>
        /// Error reading data
        /// </summary>
        internal const int ERR_READ = 3;
        /// <summary>
        /// Error writing data
        /// </summary>
        internal const int ERR_WRITE = 4;

        //internal const uint SLAVE_ADDRESS = 0x08;

        char[] trimbytes = { '-', '0' };
        byte errorCount = 0;

        CY_RETURN_STATUS cyReturnStatus;
        CY_DEVICE_INFO cyDeviceInfo = new CY_DEVICE_INFO();
        IntPtr cyDeviceHandle = new IntPtr(0);

        CY_I2C_CONFIG cyI2CConfig = new CY_I2C_CONFIG();
        CY_DATA_BUFFER cyDataBuffer = new CY_DATA_BUFFER();
        CY_DATA_BUFFER cyReadDataBuffer = new CY_DATA_BUFFER();
        CY_I2C_DATA_CONFIG cyI2CDataConfig = new CY_I2C_DATA_CONFIG();

        byte I2C_Devicenum = 0;
        byte I2C_Interfacenum = 0;

        byte[] siliconID = new byte[4];
        byte siliconRev;
        byte chkSumType;

        byte[] blVersion = new byte[8];
        byte[] fwVersion = new byte[8];
        byte[] fw2Version = new byte[8];
        byte[] fw1Loc = new byte[2];
        byte[] fw2Loc = new byte[2];
        byte slaveDeviceAddr = 0x40;
        string swVersion = "00.01.00.00";
        string fw1FileName = "CYPD3125-40LQXI_notebook01_1.cyacd";
        string fw2FileName = "CYPD3125-40LQXI_notebook01_2.cyacd";
        //string fw1FileName = "DockF_1.cyacd";
        //string fw2FileName = "DockF_2.cyacd";
        private unsafe CY_RETURN_STATUS Init_USB_I2C(CY_DEVICE_INFO info)
        {
            byte numdevice = new byte();
            cyI2CDataConfig.isStopBit = 0x01;
            cyI2CDataConfig.slaveAddress = 0x00;
            cyI2CDataConfig.isNakBit = 0x01;
            cyReturnStatus = CyGetListofDevices(&numdevice);
            if (cyReturnStatus != CY_RETURN_STATUS.CY_SUCCESS)
            {
                Console.WriteLine("Failed to initialize USB-I2C Bridge.");
                Console.WriteLine("Error Code: " + cyReturnStatus.ToString());
            }

            if (numdevice != 0)
            {
                for (byte index = 0; index < numdevice; index++)
                {
                    cyReturnStatus = CyGetDeviceInfo(index, &info);
                    Console.WriteLine("Device: " + index.ToString());

                    byte* bytes = &info.DeviceFrindlyName[0];
                    string s = new string((sbyte*)bytes);
                    Console.WriteLine(s);
                    Console.WriteLine(info.eDeviceType1.ToString());
                    Console.WriteLine("VID: " + info.VidPid.vid.ToString());
                    Console.WriteLine("PID: " + info.VidPid.pid.ToString());

                    if (info.eDeviceType1 == CY_DEVICE_TYPE.CY_TYPE_I2C)
                    {
                        I2C_Devicenum = index;
                        I2C_Interfacenum = cyDeviceInfo.NumInterfaces;
                        break;
                    }
                }
            }
            return cyReturnStatus;
        }
        public uint MaxTransferSize
        {
            get { return 384; } // original 64, for test modify to 256  //768
        }

        public unsafe int OpenConnection()
        {
            CY_RETURN_STATUS cy_stat;
            fixed (IntPtr* hdl = &cyDeviceHandle)
            {
                try
                {
                    cy_stat = CyOpen(I2C_Devicenum, I2C_Interfacenum, hdl);
                }
                catch (InvalidOperationException e)
                {
                    Console.WriteLine(e.ToString());
                }

            }

            fixed (CY_I2C_CONFIG* cfg = &cyI2CConfig)
            {
                cy_stat = CyGetI2cConfig(cyDeviceHandle, cfg);

                cyI2CConfig.Frequency = 100000;
                cyI2CDataConfig.slaveAddress = slaveDeviceAddr;//((uint)0x10) >> 1;
                cyI2CConfig.isMaster = 1;

                cy_stat = CySetI2cConfig(cyDeviceHandle, cfg);
            }

            if (cy_stat == CY_RETURN_STATUS.CY_SUCCESS)
            {
                return ERR_SUCCESS;
            }
            else
            {
                return ERR_OPEN;
            }
        }

        public int CloseConnection()
        {
            int status = 0;
            CY_RETURN_STATUS cy_stat;
            if (cyDeviceHandle == null)
                return status;
            cy_stat = CyClose(cyDeviceHandle);
            if (cy_stat == CY_RETURN_STATUS.CY_SUCCESS)
            {
                status = ERR_SUCCESS;
            }
            else
            {
                status = ERR_CLOSE;
            }
            return status;
        }
        public void strToHex(string str, byte[] buf)
        {
            //Console.WriteLine(str);
            int i = 0;
            //处理基数位 注意,一个字节是2位
            for (i = 0; i < (str.Length - str.Length % 2) / 2; i++)
            {
                buf = Convert.ToByte(str.Substring(i * 2, 2), 16);
            }
            if (str.Length % 2 != 0)
            {
                buf[i + 1] = Convert.ToByte(str.Substring(str.Length - 1, 1), 16);
            }
        }

        public unsafe byte checkDeviceMode()
        {
            int stat;
            string strdata;
            byte[] transfer = new byte[MaxTransferSize];
            byte[] receivebuf = new byte[MaxTransferSize];
            byte[] tmpdata = new byte[2];

            CY_RETURN_STATUS cystat;
            stat = OpenConnection(); //open device

            transfer[0] = 0x00;
            transfer[1] = 0x00;
            transfer[2] = 0x03;
            transfer[3] = 0x04;
            transfer[4] = 0x05;

            cyI2CDataConfig.isStopBit = 0x00;
            cyI2CDataConfig.isNakBit = 0x00;
            cyI2CDataConfig.slaveAddress = slaveDeviceAddr;// SLAVE_ADDRESS;  //Don't include R/W, we want the right-aligned 7-bit address only
            cyDataBuffer.length = 2;

            fixed (byte* b = transfer)
            {
                cyDataBuffer.buffer = b;
                fixed (CY_I2C_DATA_CONFIG* d = &cyI2CDataConfig)
                {
                    fixed (CY_DATA_BUFFER* buf = &cyDataBuffer)
                    {
                        cystat = CyI2cWrite(cyDeviceHandle, d, buf, 5000);
                    }
                }
            }

            cyI2CDataConfig.isStopBit = 0x01;
            cyI2CDataConfig.isNakBit = 0x01;
            fixed (byte* b = receivebuf)
            {
                cyReadDataBuffer.buffer = b;
                cyReadDataBuffer.length = 1;
                fixed (CY_I2C_DATA_CONFIG* d_conf = &cyI2CDataConfig)
                {
                    fixed (CY_DATA_BUFFER* data_buf = &cyReadDataBuffer)
                    {
                        cystat = CyI2cRead(cyDeviceHandle, d_conf, data_buf, 5000);
                    }
                }
            }
            //Console.WriteLine(BitConverter.ToString(receivebuf));
            strdata = BitConverter.ToString(receivebuf).TrimEnd('-', '0');
            //Console.WriteLine( "R123: " + strdata + "\r\n");
            strToHex(strdata, tmpdata);

            CloseConnection();
            return tmpdata[0];
        }

        public unsafe void writeReg(byte[] inBuf, uint inLen, byte[] outBuf, uint outLen = 0)
        {
            int stat;
            string strdata;

            byte[] transfer = new byte[MaxTransferSize];
            byte[] receivebuf = new byte[MaxTransferSize];
            byte[] tmpdata = new byte[MaxTransferSize];

            CY_RETURN_STATUS cystat;
            stat = OpenConnection(); //open device

            if (outLen > 0)
            {
                cyI2CDataConfig.isStopBit = 0x00;
                cyI2CDataConfig.isNakBit = 0x00;
            }
            else
            {
                cyI2CDataConfig.isStopBit = 0x01;
                cyI2CDataConfig.isNakBit = 0x00;
            }

            cyI2CDataConfig.slaveAddress = slaveDeviceAddr;// SLAVE_ADDRESS;  //Don't include R/W, we want the right-aligned 7-bit address only
            cyDataBuffer.length = inLen;


            fixed (byte* b = inBuf)
            {
                cyDataBuffer.buffer = b;
                fixed (CY_I2C_DATA_CONFIG* d = &cyI2CDataConfig)
                {
                    fixed (CY_DATA_BUFFER* buf = &cyDataBuffer)
                    {
                        cystat = CyI2cWrite(cyDeviceHandle, d, buf, 5000);
                    }
                }
            }
            if (outLen > 0)
            {
                cyI2CDataConfig.isStopBit = 0x01;
                cyI2CDataConfig.isNakBit = 0x01;
                fixed (byte* b = outBuf)
                {
                    cyReadDataBuffer.buffer = b;
                    cyReadDataBuffer.length = outLen;
                    fixed (CY_I2C_DATA_CONFIG* d_conf = &cyI2CDataConfig)
                    {
                        fixed (CY_DATA_BUFFER* data_buf = &cyReadDataBuffer)
                        {
                            cystat = CyI2cRead(cyDeviceHandle, d_conf, data_buf, 5000);
                        }
                    }
                }
                strdata = BitConverter.ToString(outBuf).TrimEnd('-', '0');
                strdata = strdata.Replace("-", "");
                strToHex(strdata, tmpdata);
            }

            CloseConnection();
            return;
        }

        /******************************
         * condition: BOOT FW
         *
         * return:(wait for test)
         * 
         * 
         * ****************************/
        public byte getDeviceMode()
        {
            byte[] inbuf = new byte[MaxTransferSize];
            byte[] outbuf = new byte[MaxTransferSize];
            byte inLen = 2;
            byte outLen = 1;
            inbuf[0] = 0x00;
            inbuf[1] = 0x00;
            writeReg(inbuf, inLen, outbuf, outLen);

            return outbuf[0];
        }

        public byte getBootModeReason()
        {
            byte[] inbuf = new byte[MaxTransferSize];
            byte[] outbuf = new byte[MaxTransferSize];
            byte inLen = 2;
            byte outLen = 1;
            inbuf[0] = 0x01;
            inbuf[1] = 0x00;
            writeReg(inbuf, inLen, outbuf, outLen);

            return outbuf[0];
        }

        /********************************************
         * condition: BOOT & FW
         * return : 2byte
         *   0x0000  --device silicon ID
         * ******************************************/
        public byte[] getSiliconId()
        {
            byte[] inbuf = new byte[MaxTransferSize];
            byte[] outbuf = new byte[MaxTransferSize];
            byte inLen = 2;
            byte outLen = 2;
            inbuf[0] = 0x02;
            inbuf[1] = 0x00;
            writeReg(inbuf, inLen, outbuf, outLen);

            siliconID[0] = outbuf[1];
            siliconID[1] = outbuf[0];

            return siliconID;
        }

        /************************************************
         * condition: BOOT
         * return:
         * *********************************************/
        public void getBootLoaderLastRow()
        {

        }

        /***********************************************
         * condition: BOOT & FW
         * Command:
         *   DEV_INTR
         *   PORT0_INTER
         *   PORT1_INTER
         * return: 1 byte INTR GPIO status
         *
         * ********************************************/
        public byte getIntReg()
        {
            byte[] inbuf = new byte[MaxTransferSize];
            byte[] outbuf = new byte[MaxTransferSize];
            byte inLen = 2;
            byte outLen = 1;
            inbuf[0] = 0x06;
            inbuf[1] = 0x00;
            writeReg(inbuf, inLen, outbuf, outLen);

            return outbuf[0];
        }
        /**********************************************
         * condition: BOOT & FW
         * Command:
         *   DEV_INTR
         *   PORT0_INTER
         *   PORT1_INTER
         * return: 1 byte INTR GPIO status
         *    writing non-zero values to this register clears interrupts
         * *******************************************/
        public void clearIntReg()
        {
            byte[] inbuf = new byte[MaxTransferSize];
            byte[] outbuf = new byte[MaxTransferSize];
            byte inLen = 3;
            byte outLen = 0;
            inbuf[0] = 0x06;
            inbuf[1] = 0x00;
            inbuf[2] = 0x01;
            writeReg(inbuf, inLen, outbuf, outLen);
        }
        public void clearIntReg(byte v)
        {
            byte[] inbuf = new byte[MaxTransferSize];
            byte[] outbuf = new byte[MaxTransferSize];
            byte inLen = 3;
            byte outLen = 0;
            inbuf[0] = 0x06;
            inbuf[1] = 0x00;
            inbuf[2] = v;
            writeReg(inbuf, inLen, outbuf, outLen);
        }
        /***********************************************************
         * condition: FW
         * Command:
         *   1.JUMP to BOOT mode
         *   2.JUMP to Alternate Firmware
         * return:
         *  RESET_COMPLETE  -- if reset is successful
         *  INVALID COMMAND -- if signature is invalid
         * *********************************************************/
        public byte jumpToBootLoader()
        {
            byte[] inbuf = new byte[MaxTransferSize];
            byte[] outbuf = new byte[MaxTransferSize];
            byte inLen = 3;
            byte outLen = 1;
            inbuf[0] = 0x07;
            inbuf[1] = 0x00;
            inbuf[2] = 0x4A; //jump to bootloader  0x41 = 'A'
            writeReg(inbuf, inLen, outbuf, outLen);

            return outbuf[0];
        }

        /***********************************************************
         * condition: FW
         * Command:
         *   1.JUMP to BOOT mode
         *   2.JUMP to Alternate Firmware
         * return:
         *  RESET_COMPLETE  -- if reset is successful
         *  INVALID COMMAND -- if signature is invalid
         * *********************************************************/
        public void jumpToAltFw()
        {
            byte[] inbuf = new byte[3];
            byte[] outbuf = new byte[2];
            byte inLen = 3;
            byte outLen = 0;
            inbuf[0] = 0x07;
            inbuf[1] = 0x00;
            inbuf[2] = 0x41;  //jump to alt fw
            writeReg(inbuf, inLen, outbuf, outLen);
        }
        public byte altFirmWare()
        {
            byte ret;

            clearIntReg();
            ret = getIntReg();

            jumpToAltFw();
            Thread.Sleep(50);
            ret = getIntReg();
            if (ret != 0X01)
            {
                //Console.WriteLine("device no found!");
            }
            ret = getResponse();

            clearIntReg();

            return ret;
        }
        /***********************************************************
         * Condition: BOOT & FW
         * Command:
         *   1.I2C RESET
         *   2.DEVICE RESET
         * Return: 2 bytes
         *   1. INVALID COMMAND   -- if signature or command byte is
         *                          invalid, or when the PD port are
         *                          active.
         *   2. SUCCESS  -- if I2C rest is successful.
         *   3. RESET COMPLETE -- if full device reset is successful.
         * *********************************************************/
        public void reSet(byte val)
        {
            byte[] inbuf = new byte[MaxTransferSize];
            byte[] outbuf = new byte[MaxTransferSize];
            byte inLen = 4;
            byte outLen = 0;
            inbuf[0] = 0x08;
            inbuf[1] = 0x00;
            inbuf[2] = 0x52;
            inbuf[3] = val;
            writeReg(inbuf, inLen, outbuf, outLen);
        }

        public bool devReset(byte val)
        {
            byte ret = 0;

            clearIntReg();
            ret = getIntReg();
            reSet(val);
            ret = getIntReg();
            if (ret != 0x01)
            {
                clearIntReg();
                return false;
            }

            ret = getResponse();
            if (ret != 0x80)
            {
                clearIntReg();
                return false;
            }
            clearIntReg();
            return true;
        }
        /***********************************************************
         * condition: BOOT & FW
         * Command:
         *   Enter Flashing MODE
         * return: 1 byte
         *  SUCCESS  -- if flashing mode entry is accepted
         *  INVALID COMMAND -- if signature is invalid, or if flash
         *                     mode is not supported
         * *********************************************************/
        public void enterFlashMode()
        {
            byte[] inbuf = new byte[MaxTransferSize];
            byte[] outbuf = new byte[MaxTransferSize];
            byte inLen = 3;
            byte outLen = 0;
            inbuf[0] = 0x0A;
            inbuf[1] = 0x00;
            inbuf[2] = 0x50;
            writeReg(inbuf, inLen, outbuf, outLen);
        }

        /**********************************************************
         * condition: BOOT & FW
         * Command: Validate FW image
         * Parameter:
         *    b -- 0x01 -- FW1
         *         0x02 -- FW2
         * Return: 1 byte
         *   1. INVALID COMMAND  -- if validate command is not support
         *   2. INVALID ARGUMENT -- if FW mode field is invalid.
         *   3. SUCCESS  -- if firmware validation if sucessful  //0x02 Firmware ok
         *   4. FW_INVALID -- if firmware image is invalid
         *
         * ********************************************************/

        public byte validateFw(byte b)
        {
            byte[] inbuf = new byte[MaxTransferSize];
            byte[] outbuf = new byte[MaxTransferSize];
            byte inLen = 3;
            byte outLen = 1;
            inbuf[0] = 0x0B;
            inbuf[1] = 0x00;
            inbuf[2] = b;
            writeReg(inbuf, inLen, outbuf, outLen);

            return outbuf[0];
        }

        /**********************************************************
         * condition: BOOT & FW
         * Command:
         *   1. Flash Write -- 0x01
         *   2. Flash Read  -- 0x00
         * Parameter:
         *    rw -- 0x00 -- read
         *          0x01 -- write
         *    low -- rowNum low Bytes
         *    high -- rowNum high Bytes
         * Return: 4 byte, via INTR GPIO readback
         *   1. INVALID COMMAND  -- if  not in flashing mode or if
         *                          signature or command field is invalid.
         *   2. INVALID ARGUMENT -- if flash row specified is invaid.
         *   3. SUCCESS  -- if flash write peration is successful  //0x02 Firmware ok
         *   4. Flash data available  -- if flash read operation is successful.
         *   5. Flash data available  -- if flash read operation is successful
         * ********************************************************/


        public void flashRowReadWrite(byte rw, byte low, byte high)
        {
            byte[] inbuf = new byte[6];
            byte[] outbuf = new byte[6];
            byte inLen = 6;
            byte outLen = 0;
            inbuf[0] = 0x0C;
            inbuf[1] = 0x00;
            inbuf[2] = 0x46;
            inbuf[3] = rw;
            inbuf[4] = low;
            inbuf[5] = high;
            writeReg(inbuf, inLen, outbuf, outLen);
        }
        /************************************************************************************************
         * Condition: Boot & FW
         * Command:
         *    1. 8 bytes BL Version
         *    2. 8 bytes FW Version
         * Return:
         *    blVersion  -- 8 bytes BL Version
         *    fwVersion  -- 8 bytes FW Version
         * *********************************************************************************************/
        public void readAllVersion()
        {
            byte[] inbuf = new byte[2];
            byte[] outbuf = new byte[20];
            byte inLen = 2;
            byte outLen = 16;
            inbuf[0] = 0x10;
            inbuf[1] = 0x00;
            writeReg(inbuf, inLen, outbuf, outLen);
            for (int i = 0; i < 8; i++)
            {
                blVersion = outbuf;
            }

            for (int i = 0; i < 8; i++)
            {
                fwVersion = outbuf[i + 8];
            }
        }

        public void getFw2Version()
        {
            byte[] inbuf = new byte[2];
            byte[] outbuf = new byte[8];
            byte inLen = 2;
            byte outLen = 8;
            inbuf[0] = 0x20;
            inbuf[1] = 0x00;
            writeReg(inbuf, inLen, outbuf, outLen);
            for (int i = 0; i < 8; i++)
            {
                fw2Version = outbuf;
            }
        }

        /************************************************************************************************
         * Condition: Boot & FW
         * Command:
         *    1. 2 bytes location of FW1
         *    2. 2 bytes location of FW2
         * Return:
         *    fw1Loc  -- 2 bytes Firmware1 Binary location
         *    fw2Loc  -- 2 bytes Firmware2 Binary Location
         * *********************************************************************************************/
        public void getFwBinaryLocation(byte[] fw1loc, byte[] fw2loc)
        {
            byte[] inbuf = new byte[2];
            byte[] outbuf = new byte[4];
            byte inLen = 2;
            byte outLen = 4;
            inbuf[0] = 0x28;
            inbuf[1] = 0x00;
            writeReg(inbuf, inLen, outbuf, outLen);
            fw1loc[0] = outbuf[0];
            fw1loc[1] = outbuf[1];
            fw2loc[0] = outbuf[2];
            fw2loc[1] = outbuf[3];
        }
        /************************************************************************************************
         * Condition:  FW
         * Command:
         *   1. PD port enable bit mask
         * Parameter:
         *   1. enable -- 0x00   :enable
         *                0x01   :disable
         * Return: via INTR GPIO get feedback
         *   1. INVALID COMMAND -- if the port changed is not supported
         *   2. SUCCESS -- if the port enable/disable is successful
         * *********************************************************************************************/
        public void enablePDPort(byte enable)
        {
            byte[] inbuf = new byte[3];
            byte[] outbuf = new byte[4];
            byte inLen = 3;
            byte outLen = 0;
            inbuf[0] = 0x2C;
            inbuf[1] = 0x00;
            inbuf[2] = enable;
            writeReg(inbuf, inLen, outbuf, outLen);
        }

        public byte PDPortEnable(byte enable)
        {
            byte ret = 0x02;

            clearIntReg(0x01);
            ret = getIntReg();
            if (ret != 0x00)
            {
                //Console.WriteLine(" Interrupt initialize failed");
            }
            enablePDPort(enable);
            ret = getIntReg();
            if (ret != 0x01)
            {
                //Console.WriteLine(" device not found");
            }
            ret = getResponse();

            return ret;
        }
        /************************************************************************************************
         * Condition:  FW
         * Command:
         *   1. Set application boot priority
         * Parameter:
         *  
         * Return: via INTR GPIO get feedback
         *   1. INVALID COMMAND -- if setting application priority is not supported
         *   2. SUCCESS -- if command is successful
         * *********************************************************************************************/
        public void setAppPriority()
        {
            byte[] inbuf = new byte[2];
            byte[] outbuf = new byte[1];
            byte inLen = 2;
            byte outLen = 1;
            inbuf[0] = 0x2F;
            inbuf[1] = 0x00;
            inbuf[2] = 0x00;
            writeReg(inbuf, inLen, outbuf, outLen);
        }

        /************************************************************************************************
         * Condition:  FW
         * Command:
         *   1. Read customer specific information
         * Parameter:
         *  
         * Return:
         *   1. INVALID ARGUMENT -- if signature is invalid.
         *   2. SUCCESS -- if command is successful
         * *********************************************************************************************/
        public void readCustomerInfo()
        {
            byte[] inbuf = new byte[2];
            byte[] outbuf = new byte[1];
            byte inLen = 2;
            byte outLen = 1;
            inbuf[0] = 0x30;
            inbuf[1] = 0x00;
            inbuf[2] = 0x00;
            writeReg(inbuf, inLen, outbuf, outLen);
        }


        /************************************************************************************************
       * Condition:  BOOT & FW
       * Command:
       *   1. Response port ID and response code
       * Parameter:
       *  
       * Return:
       *   1. INVALID ARGUMENT -- if signature is invalid.
       *   2. SUCCESS -- if command is successful
       * *********************************************************************************************/
        public byte getResponse()
        {
            byte[] inbuf = new byte[2];
            byte[] outbuf = new byte[2];
            byte inLen = 2;
            byte outLen = 2;
            inbuf[0] = 0x7E;
            inbuf[1] = 0x00;
            writeReg(inbuf, inLen, outbuf, outLen);
            //Console.WriteLine("Respone 0x{0:X2} 0x{1:X2}", outbuf[0], outbuf[1]);
            return outbuf[0];
        }
        private void getAppPriority()
        {
            byte[] inbuf = new byte[2];
            byte[] outbuf = new byte[2];
            byte inLen = 2;
            byte outLen = 0;
            inbuf[0] = 0x2F;
            inbuf[1] = 0x00;
            writeReg(inbuf, inLen, outbuf, outLen);
        }

        public void setAppPriority(byte v)
        {
            byte[] inbuf = new byte[3];
            byte[] outbuf = new byte[2];
            byte inLen = 3;
            byte outLen = 2;
            inbuf[0] = 0x2E;
            inbuf[1] = 0x00;
            inbuf[2] = v;
            writeReg(inbuf, inLen, outbuf, outLen);
        }

        public void printTime()
        {
            DateTime dt = DateTime.Now;
            Console.WriteLine("\n\rNow time is : {0}\n\r", dt.ToLocalTime().ToString());
        }
        public bool FwIsExist(string fileName)
        {
            return File.Exists(fileName);
        }

        public bool isChkSumRight(byte[] buf, int len)
        {
            byte Sum = 0;
            if (len == 0)
            {
                return false;
            }
            for (int i = 0; i < (len - 1); i++)
            {
                Sum += buf;
            }
            Sum = (byte)~Sum;
            Sum += 1;
            //Console.WriteLine("isChkSumRight sum = 0x{0:X2} buf[133] = 0x{1:X2}", Sum, buf[len -1]);
            if (Sum != buf[len - 1])
            {
                return false;
            }

            return true;
        }
        public byte OpenFileWS(string filePath)
        {
            FileStream fileStream = null;
            byte[] buf = new byte[MaxTransferSize];
            byte[] rowData = new byte[130];//CCG3:128+2 CCG4-5:256+2
            byte status = 0;
            byte[] rowNum = new byte[2];
            byte[] flashdata = new byte[MaxTransferSize];
            byte[] lowRowNum = new byte[1];
            byte[] highRowNum = new byte[1];
            byte low = 0, high = 0;

            try
            {
                if (File.Exists(filePath))
                {
                    fileStream = new FileStream(filePath, FileMode.Open);
                }
                else
                {
                    Console.WriteLine("\nFile open failed\r\n");
                    return 0x01; //file open error
                }
                StreamReader sr = new StreamReader(fileStream);
                string line;
                //Console.WriteLine("\nStart to Record################################Start to Record\n");
                printTime();
                while ((line = sr.ReadLine()) != null)
                {
                    if (line[0] != ':')
                    {
                        strToHex(line, buf);
                        siliconID[0] = buf[0];
                        siliconID[1] = buf[1];
                        siliconID[2] = buf[2];
                        siliconID[3] = buf[3];
                        siliconRev = buf[4];
                        chkSumType = buf[5];
                        string tmpstr = string.Format("\nsiliconID = 0x{0:X2} 0x{1:X2} 0x{2:X2} 0x{3:X2} siliconRev = 0x{4:X2} chkSumType = 0x{5:X2}\n", siliconID[0], siliconID[1], siliconID[2], siliconID[3], siliconRev, chkSumType);
                        Console.WriteLine(tmpstr);
                        //Console.WriteLine("\nsiliconRev = 0x{0:X2} chkSumType = 0x{1:X2}\n", siliconRev, chkSumType);
                    }
                    else if (line[0] == ':')
                    {
                        line = line.Remove(0, 1); // 去除':'
                        strToHex(line, buf);

                        if (isChkSumRight(buf, 134)==false)  //GCC3: 128 + 6
                        {
                            Console.WriteLine("\ncheck sum Error\r\n");
                            errorCount++;
                            return 1; // 校验错误
                        }
              
                        low = buf[2];
                        high = buf[1];
                        //Console.WriteLine("0x{0:X2} 0x{1:X2}\n", high, low);

                        rowData[0] = 0x00;
                        rowData[1] = 0x02;

                        for (int i = 0; i < 128; i++)  //GCC2:128
                        {
                            rowData[2 + i] = buf[5 + i];
                        }

                        status = programRow(rowData, 130, low, high);
                        if (status != 0)
                        {
                            Console.WriteLine("\nProgram failed....\n");
                            errorCount++;
                        }
                    }
                }

                printTime();
                //Console.WriteLine("\nEnd to Record################################ End to Record\n");
            }
            catch (FileNotFoundException e)
            {
                Console.WriteLine(e.ToString());
            }
            //Console.WriteLine("\nflash error counter = 0x{0:X2}\r\n", errorCount);
            return (byte)(status + errorCount);
        }

        public byte programRow(byte[] fdata, byte len, byte low, byte high)
        {
            byte[] inbuf = new byte[MaxTransferSize];
            byte[] outbuf = new byte[MaxTransferSize];
            byte inLen;
            byte outLen;
            byte iStatus;
            byte status;

            clearIntReg();
            iStatus = getIntReg();
            if (iStatus != 0x00)
            {
                //Console.WriteLine("\n#######################00000000000000000  device no response  0x{0:X2}\n", iStatus);
                return 0x01;//
            }

            inLen = len;
            outLen = 0;
            writeReg(fdata, inLen, outbuf, outLen); //data write memory
            flashRowReadWrite(0x01, low, high);
            Thread.Sleep(100);
            iStatus = getIntReg();
            //Console.WriteLine("flash write iStatus1 = 0x{0:X2}", iStatus);
            if (iStatus == 0x00)
            {
                Console.WriteLine("\n device no response \n");
            }
            else if (iStatus != 0x01) //get Responce for devices
            {
                //Console.WriteLine("\n#######################11111111111111111111111111111  0x{0:X2}\n", iStatus);
                return 0x01;// erro for test

            }
            else if (iStatus == 0x02) //get Response for Port 1
            {
                Console.WriteLine("\n Port 1 response \n");
            }
            else if (iStatus == 0x04) //get Response for Port 2
            {
                Console.WriteLine("\n Port 2 response \n");
            }
            status = getResponse();
            //Console.WriteLine("flash Write iStatus = 0x{0:X2}  Result status = 0x{1:X2}",iStatus, status);
            Console.Write(".");
            if (status != 0x02)
            {
                //Console.WriteLine("\n#######################2222222222222 Command Invalidate  0x{0:X2}\n", status);
                clearIntReg();
                return 0x01;// return error
            }

            clearIntReg();

            return 0x00;
        }
        public void bootloadUpdate()
        {
            //fw1ToFw2Update();
            byte status; ;
            byte ret;


            //clear INTR
            clearIntReg();
            //get Interrupt Register
            status = getIntReg();
            if (status != 0x00)
            {
                Console.WriteLine("\nInterrupt initialize failed s tatus = 0x{0:X2}\n", status);
            }

            //enter flash mode
            enterFlashMode();
            //Console.WriteLine(" enterfalsh mode Status = 0x{0:X2}\n", status);
            status = getIntReg();
            if (status == 0x01) //need to get respo0:Xnse
            {
                // get response
                ret = getResponse();
                if (ret == 0x02)
                {
                    Console.WriteLine("Enter flash mode successful\n");
                    //clear INTR
                    clearIntReg();

                    status = getIntReg();
                    ret = OpenFileWS(fw1FileName);
                    if (ret != 0x00)
                    {
                        Console.WriteLine("\nFw2 updated failed!\n");
                        return;
                    }
                }
                else
                {
                    Console.WriteLine("\nEnter flash mode Failed status = 0x{0:X2}\r\n", status);
                }
                clearIntReg();
            }
            else
            {
                Console.WriteLine("\nBootLoader update No device Response !\n");
            }

        }
        public void fw1ToFw2Update()
        {
            byte status; ;
            byte ret;


            //clear INTR
            clearIntReg();
            //get Interrupt Register
            status = getIntReg();
            if(status != 0x00)
            {
                Console.WriteLine("\nInterrupt initialize failed s tatus = 0x{0:X2}\n", status);
            }
           
            //enter flash mode
            enterFlashMode();
            //Console.WriteLine(" enterfalsh mode Status = 0x{0:X2}\n", status);
            status = getIntReg();
            if (status == 0x01) //need to get response
            {
                // get response
                ret = getResponse();
                if (ret == 0x02)
                {
                   //Console.WriteLine("Enter flash mode successful\n");
                    //clear INTR
                    clearIntReg();

                    status = getIntReg();
                    ret = OpenFileWS(fw2FileName);
                    if (ret != 0x00)
                    {
                        //Console.WriteLine("\nFw2 updated failed!\n");
                        return;
                    }
                }
                else
                {
                    Console.WriteLine("\nEnter flash mode Failed status = 0x{0:X2}\r\n", status);
                }
                clearIntReg();
            }
            else
            {
                Console.WriteLine("\nfw1tofw2 No device Response !\n");
            }
            clearIntReg();
        }
        public void fw2ToFw1Update()
        {
            byte status; ;
            byte ret;


            //clear INTR
            clearIntReg();
            //get Interrupt Register
            status = getIntReg();
            if (status != 0x00)
            {
                Console.WriteLine("\nInterrupt initialize failed s tatus = 0x{0:X2}\n", status);
            }

            //enter flash mode
            enterFlashMode();
            //Console.WriteLine(" enterfalsh mode Status = 0x{0:X2}\n", status);
            status = getIntReg();
            if (status == 0x01) //need to get response
            {
                // get response
                ret = getResponse();
                if (ret == 0x02)
                {
                    //Console.WriteLine("Enter flash mode successful\n");
                    //clear INTR
                    clearIntReg();

                    status = getIntReg();
                    ret = OpenFileWS(fw1FileName);
                    if (ret != 0x00)
                    {
                        //Console.WriteLine("\nFw1 Update failed !\n");
                        return;
                    }
                }
                else
                {
                    Console.WriteLine("\nEnter flash mode Failed status = 0x{0:X2}\r\n", status);
                }
                clearIntReg();
            }
            else
            {
                Console.WriteLine("\nfw2tofw1 No device Response !\n");
            }
            clearIntReg();
        }
        public bool fwModeToggle(byte mode)
        {
            byte mode1 = 0x00;
            byte timeout = 0;
            byte ret = 0;

            while ((mode != mode1) && (timeout <= 8))
            {
                ret = altFirmWare();
                Thread.Sleep(50);
                mode1 = getDeviceMode();
                timeout++;
                //Console.WriteLine("\ndevice timeout = 0x{0:X2}  mode1 = 0x{1:X2}\n", timeout, mode1);
            }
            //Console.WriteLine("\ndevice mode = 0x{0:X2}  mode1 = 0x{1:X2}\n", mode,mode1);
            return ((mode == mode1) ? true : false);
        }

        static int Main(string[] args)
        {
            MTIPD3125FW mti = new MTIPD3125FW();
            CY_DEVICE_INFO cyDeviceInfo = new CY_DEVICE_INFO();
            CY_RETURN_STATUS status;
            byte deviceMode;
            int ret = 0;
            bool success = true;
            bool isFw1updated = true;

            if (args.Length == 1 && args[0] == "version")
            {
                Console.WriteLine("{0:S}", mti.swVersion);
                return 0;
            }
            //获得I2C Address
            if (args.Length == 1 && (args[0] == "0x08"))
            {
                mti.slaveDeviceAddr = 0x08;
                //mti.fw1FileName = "brack/" + mti.fw1FileName;
                //mti.fw2FileName = "brack/" + mti.fw2FileName;
            }
            else if (args.Length == 1 && (args[0] == "0x40"))
            {
                mti.slaveDeviceAddr = 0x40;
                mti.fw1FileName = "brack/" + mti.fw1FileName;
                mti.fw2FileName = "brack/" + mti.fw2FileName;
            }
            else
            {
                Console.WriteLine("\r\nSlave Device Address Error, Please Check!\r\n");
                return -1; //slave adress error
            }
            Console.WriteLine("\r\nSlave Device Address is 0x{0:X2}\r\n", mti.slaveDeviceAddr);

            status = mti.Init_USB_I2C(cyDeviceInfo);
            if (status != 0)
            {
                Console.WriteLine("\nUSB I2C inital failed\n");
                success = false;
                return -1;
            }
           
            if (mti.FwIsExist(mti.fw1FileName) && mti.FwIsExist(mti.fw2FileName))
            {
                // Console.WriteLine("\nFind Fw1 & Fw2 files! \n");
            }
            else
            {
                Console.WriteLine("\nFw1 or Fw2 file  no be found! \n");
                success = false;
                return -2; //更新文件不存在
            }
           
            mti.PDPortEnable(0x00);
            mti.clearIntReg();

            if (mti.devReset(0x01))
            {
                Console.WriteLine("\nDevice Reset!\n");
            }

            deviceMode = mti.getDeviceMode();
            Console.WriteLine("\nMain deviceMode = 0x{0:X2}", deviceMode);


            if (deviceMode == 0x80)
            {
                Console.WriteLine("\r\nNow is Bootloader status\r\n");

                Console.WriteLine("\n####################### Start to Program FW1 ##################################\n");
                mti.bootloadUpdate();
                Console.WriteLine("\n####################### End to Program FW1 ####################################\n");
                isFw1updated = false;
                mti.validateFw(0x01);
                mti.PDPortEnable(0x03);
            }

            mti.PDPortEnable(0x03);
            Thread.Sleep(2000);

            if (mti.devReset(0x01))
            {
                Console.WriteLine("\nDevice Reset!\n");
            }
            //deviceMode = mti.getDeviceMode();
            //Console.WriteLine("\ndeviceMode = 0x{0:X2}\n", deviceMode);

            //return -1;
            mti.PDPortEnable(0x00);
            deviceMode = mti.getDeviceMode();
            Console.WriteLine("\nafter PDP disable  deviceMode = 0x{0:X2}", deviceMode);
            if (mti.fwModeToggle(0x81))
            {
                Console.WriteLine("\r\nNow is FW1 status\r\n");
                Console.WriteLine("\n####################### Start to Program FW2 ##################################\n");
                mti.fw1ToFw2Update();
                Console.WriteLine("\n####################### End to Program FW2 ####################################\n");
            }
            else
            {
                Console.WriteLine("\r\nFw2 update failed !\r\n");
                success = false;
                return -1;
            }
            mti.validateFw(0x01);
            mti.PDPortEnable(0x03);

            Thread.Sleep(2000);
            mti.PDPortEnable(0x00);
            if (mti.devReset(0x00))
            {
                Console.WriteLine("\r\nDevice Reset!\r\n");
            }
            if (isFw1updated)
            {
                deviceMode = mti.getDeviceMode();
                if (mti.fwModeToggle(0x82))
                {
                    Console.WriteLine("\r\nNow is FW2 status\r\n");
                    Console.WriteLine("\n####################### Start to Program FW1 ##################################\n");
                    mti.fw2ToFw1Update();
                    Console.WriteLine("\n####################### End to Program FW1 ####################################\n");
                }
                else
                {
                    Console.WriteLine("\r\nFw1 update failed !\r\n");
                    success = false;
                    return -1;
                }
            }

            Thread.Sleep(2000);
            mti.PDPortEnable(0x03);
            if (mti.devReset(0x01))
            {
                Console.WriteLine("\nDevice Reset!\n");
            }
            Thread.Sleep(500);
            if (success)
            {
                Console.WriteLine("\nFW update Successful!\n");
            }
            else
            {
                Console.WriteLine("\nFW update Failed!\n");
                return -1;
            }
            //mti.CloseConnection();
            return 0;
        }

    }
}

0 点赞