コード例 #1
0
        static void Main(string[] args)
        {
            int ret;

            Byte[] write_buffer = new Byte[10240];
            Byte[] Read_buffer  = new Byte[10240];
            ControlSPI.VSI_INIT_CONFIG pSPI_Config = new ControlSPI.VSI_INIT_CONFIG();
            //Scan connected device
            ret = ControlSPI.VSI_ScanDevice(1);
            if (ret <= 0)
            {
                Console.WriteLine("No device connect!");
                return;
            }
            // Open device
            ret = ControlSPI.VSI_OpenDevice(ControlSPI.VSI_USBSPI, 0, 0);
            if (ret != ControlSPI.ERROR.SUCCESS)
            {
                Console.WriteLine("Open device error!");
                return;
            }
            // Initialize device
            pSPI_Config.ControlMode = 1;
            pSPI_Config.MasterMode  = 1;
            pSPI_Config.ClockSpeed  = 36000000;
            pSPI_Config.CPHA        = 0;
            pSPI_Config.CPOL        = 0;
            pSPI_Config.LSBFirst    = 0;
            pSPI_Config.TranBits    = 8;
            ret = ControlSPI.VSI_InitSPI(ControlSPI.VSI_USBSPI, 0, ref pSPI_Config);
            if (ret != ControlSPI.ERROR.SUCCESS)
            {
                Console.WriteLine("Initialize device error!");
                return;
            }
            // Get JEDEC ID
            write_buffer[0] = 0x9F;//SPI Flash get JEDEC ID command
            ret             = ControlSPI.VSI_WriteReadBytes(ControlSPI.VSI_USBSPI, 0, 0, write_buffer, 1, Read_buffer, 3);
            if (ret != ControlSPI.ERROR.SUCCESS)
            {
                Console.WriteLine("Get JEDEC ID error!");
                return;
            }
            else
            {
                Int32 JEDEC_ID = (Read_buffer[0] << 16) | (Read_buffer[1] << 8) | Read_buffer[2];
                Console.WriteLine("JEDEC ID(Hex):{0}", JEDEC_ID.ToString("X6"));
            }
            //Wait Busy
            do
            {
                write_buffer[0] = 0xD7;//SPI Flash read status register
                ret             = ControlSPI.VSI_WriteReadBytes(ControlSPI.VSI_USBSPI, 0, 0, write_buffer, 1, Read_buffer, 1);
                if (ret != ControlSPI.ERROR.SUCCESS)
                {
                    Console.WriteLine("Get status error!");
                    return;
                }
            } while ((Read_buffer[0] & 0x80) == 0);
            //Page Erase
            write_buffer[0] = 0x81;
            write_buffer[1] = 0x00;
            write_buffer[2] = 0x00;
            write_buffer[3] = 0x00;
            ret             = ControlSPI.VSI_WriteBytes(ControlSPI.VSI_USBSPI, 0, 0, write_buffer, 4);
            if (ret != ControlSPI.ERROR.SUCCESS)
            {
                Console.WriteLine("Sector Erase start error!");
                return;
            }
            else
            {
                Console.WriteLine("Sector erase start success!");
            }
            //Wait Busy
            do
            {
                write_buffer[0] = 0xD7;//SPI Flash read status register
                ret             = ControlSPI.VSI_WriteReadBytes(ControlSPI.VSI_USBSPI, 0, 0, write_buffer, 1, Read_buffer, 1);
                if (ret != ControlSPI.ERROR.SUCCESS)
                {
                    Console.WriteLine("Get status error!");
                    return;
                }
            } while ((Read_buffer[0] & 0x80) == 0);
            //Write data to buffer
            write_buffer[0] = 0x87;
            write_buffer[1] = 0x00;
            write_buffer[2] = 0x00;
            write_buffer[3] = 0x00;
            for (int i = 0; i < 528; i++)
            {
                write_buffer[4 + i] = (Byte)i;
            }
            ret = ControlSPI.VSI_WriteBytes(ControlSPI.VSI_USBSPI, 0, 0, write_buffer, 4 + 528);
            if (ret != ControlSPI.ERROR.SUCCESS)
            {
                Console.WriteLine("Write data error!");
                return;
            }
            else
            {
                Console.WriteLine("Write data succeed!");
            }
            //Buffer to Main Memory Page Program without Built-in Erase
            write_buffer[0] = 0x89;
            write_buffer[1] = 0x00;
            write_buffer[2] = 0x00;
            write_buffer[3] = 0x00;
            ret             = ControlSPI.VSI_WriteBytes(ControlSPI.VSI_USBSPI, 0, 0, write_buffer, 4);
            if (ret != ControlSPI.ERROR.SUCCESS)
            {
                Console.WriteLine("Write data to main memory error!");
                return;
            }
            else
            {
                Console.WriteLine("Write data to main memory success!");
            }
            //Wait Busy
            do
            {
                write_buffer[0] = 0xD7;
                ret             = ControlSPI.VSI_WriteReadBytes(ControlSPI.VSI_USBSPI, 0, 0, write_buffer, 1, Read_buffer, 1);
                if (ret != ControlSPI.ERROR.SUCCESS)
                {
                    Console.WriteLine("Get status error!");
                    return;
                }
            } while ((Read_buffer[0] & 0x80) == 0);
            //Main Memory Page Read
            write_buffer[0] = 0xD2;
            write_buffer[1] = 0x00;
            write_buffer[2] = 0x00;
            write_buffer[3] = 0x00;
            ret             = ControlSPI.VSI_WriteReadBytes(ControlSPI.VSI_USBSPI, 0, 0, write_buffer, 8, Read_buffer, 528);
            if (ret != ControlSPI.ERROR.SUCCESS)
            {
                Console.WriteLine("Read data error!");
                return;
            }
            else
            {
                Int32  read_data;
                String read_data_str = "";
                Console.WriteLine("Get Data:");
                for (int i = 0; i < 528; i++)
                {
                    read_data      = Read_buffer[i];
                    read_data_str += read_data.ToString("X2") + " ";
                    if (((i + 1) % 16) == 0)
                    {
                        read_data_str += "\n";
                    }
                }
                Console.WriteLine(read_data_str);
            }
            //Close device
            ret = ControlSPI.VSI_CloseDevice(ControlSPI.VSI_USBSPI, 0);
            if (ret != ControlSPI.ERROR.SUCCESS)
            {
                Console.WriteLine("Colse device error!");
            }
            else
            {
                Console.WriteLine("Colse device success!");
            }
            Console.ReadLine();
        }
コード例 #2
0
        static void Main(string[] args)
        {
            int ret, i;

            ControlSPI.VSI_INIT_CONFIG pSPI_Config = new ControlSPI.VSI_INIT_CONFIG();
            //Scan connected device
            ret = ControlSPI.VSI_ScanDevice(1);
            if (ret <= 0)
            {
                Console.WriteLine("No device connect!");
                return;
            }
            // Open device
            ret = ControlSPI.VSI_OpenDevice(ControlSPI.VSI_USBSPI, 0, 0);
            if (ret != ControlSPI.ERROR.SUCCESS)
            {
                Console.WriteLine("Open device error!");
                return;
            }

            // Initialize device(Master Mode, Hardware SPI, Half-Duplex)
            // function VSI_WriteBytes,VSI_ReadBytes,VSI_WriteReadBytes can be support in software SPI mode            //该模式下可以使用VSI_WriteBytes,VSI_ReadBytes,VSI_WriteReadBytes,VSI_BlockWriteBytes,VSI_BlockReadBytes,VSI_BlockWriteReadBytes这些函数
            pSPI_Config.ControlMode = 1;
            pSPI_Config.MasterMode  = 1;
            pSPI_Config.ClockSpeed  = 36000000;
            pSPI_Config.CPHA        = 0;
            pSPI_Config.CPOL        = 0;
            pSPI_Config.LSBFirst    = 0;
            pSPI_Config.TranBits    = 8;
            pSPI_Config.SelPolarity = 0;
            ret = ControlSPI.VSI_InitSPI(ControlSPI.VSI_USBSPI, 0, ref pSPI_Config);
            if (ret != ControlSPI.ERROR.SUCCESS)
            {
                Console.WriteLine("Initialize device error!");
                return;
            }

            Byte[] WriteDataTemp = new Byte[512];
            Byte[] ReadDataTemp  = new Byte[10240];
            Int32  StartAddr     = 0x000000; // Read data start address
            Int32  EndAddr       = 0x400000; // Read data end address
            Int32  Addr          = 0;

            char[] charTmp = new char[1024];
            Byte[] byteTmp = new Byte[1024];
            String strTmp  = new String(' ', 1024);
            Int64  temp    = 0;

            // JEDEC ID
            WriteDataTemp[0] = 0x9F;
            ret = ControlSPI.VSI_WriteReadBytes(ControlSPI.VSI_USBSPI, 0, 0, WriteDataTemp, 1, ReadDataTemp, 3);
            if (ret != ControlSPI.ERROR.SUCCESS)
            {
                Console.WriteLine("Read flash ID error :%d\n", ret);
                return;
            }
            else
            {
                Console.WriteLine("Flash ID = {0, -6:x}", (ReadDataTemp[0] << 16) | (ReadDataTemp[1] << 8) | (ReadDataTemp[2]));
            }
            // Open file



            FileStream fs = new FileStream("Data.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);

            //FileStream aFile = File.OpenRead("Data.txt");
            Console.WriteLine("Start read data,please wait...\n");
            // Read data
            for (Addr = StartAddr; Addr < EndAddr; Addr += 4096)
            {
                WriteDataTemp[0] = 0x0B;
                WriteDataTemp[1] = (Byte)(Addr >> 16);
                WriteDataTemp[2] = (Byte)(Addr >> 8);
                WriteDataTemp[3] = (Byte)(Addr >> 0);
                WriteDataTemp[4] = 0x00;
                ret = ControlSPI.VSI_WriteReadBytes(ControlSPI.VSI_USBSPI, 0, 0, WriteDataTemp, 4, ReadDataTemp, 4096);
                if (ret != ControlSPI.ERROR.SUCCESS)
                {
                    Console.WriteLine("Read data error :%d\n", ret);
                    Console.ReadLine();
                    return;
                }
                else
                {
                    for (int j = 0; j < 4096; j += 64)
                    {
                        strTmp = null;
                        strTmp = new string(' ', 1024);
                        for (i = 0; i < 32; i++)
                        {
                            temp   = (ReadDataTemp[j + i * 2] << 8) | ReadDataTemp[j + i * 2 + 1];
                            strTmp = strTmp + temp.ToString();
                            strTmp = strTmp.Replace(" ", "");
                        }

                        charTmp = strTmp.ToCharArray();
                        byteTmp = Encoding.Default.GetBytes(charTmp);
                        fs.Write(byteTmp, 0, strTmp.Length);
                    }
                }
            }
            fs.Close();
            Console.WriteLine("Read data end!\n");
            Console.ReadLine();
            return;
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: gstarnet/Ginkgo
        static void Main(string[] args)
        {
            int ret;

            Byte[] write_buffer = new Byte[10240];
            Byte[] read_buffer  = new Byte[10240];
            ControlSPI.VSI_INIT_CONFIG pSPI_Config = new ControlSPI.VSI_INIT_CONFIG();
            //Scan connected device
            ret = ControlSPI.VSI_ScanDevice(1);
            if (ret <= 0)
            {
                Console.WriteLine("No device connect!");
                return;
            }
            // Open device
            ret = ControlSPI.VSI_OpenDevice(ControlSPI.VSI_USBSPI, 0, 0);
            if (ret != ControlSPI.ERROR.SUCCESS)
            {
                Console.WriteLine("Open device error!");
                return;
            }
            // Initialize device(Master Mode, Hardware SPI, Half-Duplex)
            pSPI_Config.ControlMode = 1;
            pSPI_Config.MasterMode  = 1;
            pSPI_Config.ClockSpeed  = 36000000;
            pSPI_Config.CPHA        = 0;
            pSPI_Config.CPOL        = 0;
            pSPI_Config.LSBFirst    = 0;
            pSPI_Config.TranBits    = 8;
            pSPI_Config.SelPolarity = 0;
            ret = ControlSPI.VSI_InitSPI(ControlSPI.VSI_USBSPI, 0, ref pSPI_Config);
            if (ret != ControlSPI.ERROR.SUCCESS)
            {
                Console.WriteLine("Initialize device error!");
                return;
            }

            // Write enable
            write_buffer[0] = 0x06; //write enable command
            ret             = ControlSPI.VSI_WriteReadBytes(ControlSPI.VSI_USBSPI, 0, 0, write_buffer, 1, read_buffer, 3);
            if (ret != ControlSPI.ERROR.SUCCESS)
            {
                Console.WriteLine("Flash write enable error {0}", ret);
                return;
            }
            else
            {
                Console.WriteLine("Write enable success!\n");
            }
            // Write data to address 0
            write_buffer[0] = 0x02;
            write_buffer[1] = 0x00; // low 8bits address, please see m95040 datasheet for detail
            for (Int32 i = 0; i < 16; i++)
            {
                write_buffer[i + 2] = (Byte)i;
            }
            ret = ControlSPI.VSI_WriteBytes(ControlSPI.VSI_USBSPI, 0, 0, write_buffer, 18);
            if (ret != ControlSPI.ERROR.SUCCESS)
            {
                Console.WriteLine("Write enable success!");
                return;
            }
            else
            {
                Console.WriteLine("Write 16 byte success!");
            }
            // Delay to ensure data write completely
            System.Threading.Thread.Sleep(100);

            // Read the written data
            write_buffer[0] = 0x03;
            write_buffer[1] = 0x00; // low 8bits address, please see m95040 datasheet for detail
            ret             = ControlSPI.VSI_WriteReadBytes(ControlSPI.VSI_USBSPI, 0, 0, write_buffer, 2, read_buffer, 16);
            if (ret != ControlSPI.ERROR.SUCCESS)
            {
                Console.WriteLine("Read Data error {0}", ret);
                return;
            }
            else
            {
                Console.WriteLine("Read Data success");
            }

            for (Int32 i = 0; i < 16; i++)
            {
                Console.Write("{0,  2}  ", read_buffer[i]);
            }
        }
コード例 #4
0
        static void Main(string[] args)
        {
            int ret;

            ControlSPI.VSI_INIT_CONFIG pSPI_Config = new ControlSPI.VSI_INIT_CONFIG();
            //Scan connected device
            ret = ControlSPI.VSI_ScanDevice(1);
            if (ret <= 0)
            {
                Console.WriteLine("No device connect!");
                return;
            }
            // Open device
            ret = ControlSPI.VSI_OpenDevice(ControlSPI.VSI_USBSPI, 0, 0);
            if (ret != ControlSPI.ERROR.SUCCESS)
            {
                Console.WriteLine("Open device error!");
                return;
            }
            // Initialize device(Master Mode, Hardware SPI, Half-Duplex)
            // Function VSI_WriteBytes,VSI_ReadBytes,VSI_WriteReadBytes,
            //    VSI_BlockWriteBytes,VSI_BlockReadBytes,VSI_BlockWriteReadBytes can be support in this mode

            pSPI_Config.ControlMode = 1;
            pSPI_Config.MasterMode  = 1;
            pSPI_Config.ClockSpeed  = 36000000;
            pSPI_Config.CPHA        = 0;
            pSPI_Config.CPOL        = 0;
            pSPI_Config.LSBFirst    = 0;
            pSPI_Config.TranBits    = 8;
            ret = ControlSPI.VSI_InitSPI(ControlSPI.VSI_USBSPI, 0, ref pSPI_Config);
            if (ret != ControlSPI.ERROR.SUCCESS)
            {
                Console.WriteLine("Initialize device error!");
                return;
            }
            Byte[] write_buffer = new Byte[10240];
            Byte[] read_buffer  = new Byte[10240];
            // Write data to SPI bus
            for (int i = 0; i < 512; i++)
            {
                write_buffer[i] = (Byte)i;
            }
            ret = ControlSPI.VSI_WriteBytes(ControlSPI.VSI_USBSPI, 0, 0, write_buffer, 512);
            if (ret != ControlSPI.ERROR.SUCCESS)
            {
                Console.WriteLine("Write data error!");
                return;
            }
            // Read data from SPI bus
            ret = ControlSPI.VSI_ReadBytes(ControlSPI.VSI_USBSPI, 0, 0, read_buffer, 512);
            if (ret != ControlSPI.ERROR.SUCCESS)
            {
                Console.WriteLine("Read data error!");
                return;
            }
            Console.WriteLine("Read Data:");
            for (int i = 0; i < 512; i++)
            {
                Console.Write(read_buffer[i].ToString("X2") + " ");
            }
            // Write data and then read data(clock separated ), CS is still enable
            ret = ControlSPI.VSI_WriteReadBytes(ControlSPI.VSI_USBSPI, 0, 0, write_buffer, 256, read_buffer, 512);
            if (ret != ControlSPI.ERROR.SUCCESS)
            {
                Console.WriteLine("Write&Read data error!");
                return;
            }
            Console.WriteLine("Write&Read Data:");
            for (int i = 0; i < 512; i++)
            {
                Console.Write(read_buffer[i].ToString("X2") + " ");
            }

            //SPI master, Half-Duplex, Block mode, Write data
            UInt16 BlockSize    = 4;
            UInt16 BlockNum     = 5;
            UInt32 IntervalTime = 10;//in us
            Byte   data         = 0;

            for (int blockNumIndex = 0; blockNumIndex < BlockNum; blockNumIndex++)
            {
                for (int blockSizeIndex = 0; blockSizeIndex < BlockSize; blockSizeIndex++)
                {
                    write_buffer[blockNumIndex * BlockSize + blockSizeIndex] = data;
                    data++;
                }
            }

            // CS will be enable BlockNum times and write BlockSize bits data after call VSI_BlockWriteBytes(),
            // CS signal interval is IntervalTime(in us)
            ret = ControlSPI.VSI_BlockWriteBytes(ControlSPI.VSI_USBSPI, 0, 0, write_buffer, BlockSize, BlockNum, IntervalTime);
            if (ret != ControlSPI.ERROR.SUCCESS)
            {
                Console.WriteLine("Block Write data error!");
                return;
            }
            //SPI master, Half-Duplex, Block mode, Read data
            for (int blockNumIndex = 0; blockNumIndex < BlockNum; blockNumIndex++)
            {
                for (int blockSizeIndex = 0; blockSizeIndex < BlockSize; blockSizeIndex++)
                {
                    write_buffer[blockNumIndex * BlockSize + blockSizeIndex] = data;
                    data++;
                }
            }
            // CS will be enable BlockNum times and write BlockSize bits data after call VSI_BlockWriteBytes(),
            // CS signal interval is IntervalTime(in us)
            ret = ControlSPI.VSI_BlockReadBytes(ControlSPI.VSI_USBSPI, 0, 0, read_buffer, BlockSize, BlockNum, IntervalTime);
            if (ret != ControlSPI.ERROR.SUCCESS)
            {
                Console.WriteLine("Block Read data error!");
                return;
            }
            //SPI master, Half-Duplex, Block mode, Write&Read data
            for (int blockNumIndex = 0; blockNumIndex < BlockNum; blockNumIndex++)
            {
                for (int blockSizeIndex = 0; blockSizeIndex < BlockSize; blockSizeIndex++)
                {
                    write_buffer[blockNumIndex * BlockSize + blockSizeIndex] = data;
                    data++;
                }
            }
            // CS will be enable BlockNum times and write BlockSize bits data after call VSI_BlockWriteBytes(),
            // CS signal interval is IntervalTime(in us)

            UInt16 WriteBlockSize = 2;
            UInt16 ReadBlockSize  = 4;

            ret = ControlSPI.VSI_BlockWriteReadBytes(ControlSPI.VSI_USBSPI, 0, 0, write_buffer, WriteBlockSize, read_buffer, ReadBlockSize, BlockNum, IntervalTime);
            if (ret != ControlSPI.ERROR.SUCCESS)
            {
                Console.WriteLine("Block Write&Read data error!");
                return;
            }

            // Initialize device(Master Mode, Software SPI, Half-Duplex)
            // function VSI_WriteBytes,VSI_ReadBytes,VSI_WriteReadBytes can be support in software SPI mode
            // Hardware SPI cannot support function VSI_WriteBits,VSI_ReadBits,VSI_WriteReadBits, but can be used in 1-wired mode
            pSPI_Config.ControlMode = 2;
            pSPI_Config.MasterMode  = 1;
            pSPI_Config.ClockSpeed  = 100000;
            pSPI_Config.CPHA        = 0;
            pSPI_Config.CPOL        = 0;
            pSPI_Config.LSBFirst    = 0;
            pSPI_Config.TranBits    = 8;
            ret = ControlSPI.VSI_InitSPI(ControlSPI.VSI_USBSPI, 0, ref pSPI_Config);
            if (ret != ControlSPI.ERROR.SUCCESS)
            {
                Console.WriteLine("Initialize device error!");
                return;
            }
            // Write data with binary, up to 10240 bits
            StringBuilder write_buffer_bin;
            StringBuilder read_buffer_bin;

            write_buffer_bin = new StringBuilder("10110100100101");
            ret = ControlSPI.VSI_WriteBits(ControlSPI.VSI_USBSPI, 0, 0, write_buffer_bin);//send 14bit data
            if (ret != ControlSPI.ERROR.SUCCESS)
            {
                Console.WriteLine("Write bit error!");
                return;
            }
            // Read data with binary, up to 10240 bits
            read_buffer_bin = new StringBuilder(10240);
            ret             = ControlSPI.VSI_ReadBits(ControlSPI.VSI_USBSPI, 0, 0, read_buffer_bin, 19);//read 19bit data
            if (ret != ControlSPI.ERROR.SUCCESS)
            {
                Console.WriteLine("Read bit error!");
                return;
            }
            else
            {
                Console.WriteLine("Read bits:");
                Console.WriteLine(read_buffer_bin);
            }
            // Read and write data with binary
            write_buffer_bin = new StringBuilder("000011110101001");
            ret = ControlSPI.VSI_WriteReadBits(ControlSPI.VSI_USBSPI, 0, 0, write_buffer_bin, read_buffer_bin, 25);//read 19bit data
            if (ret != ControlSPI.ERROR.SUCCESS)
            {
                Console.WriteLine("Write bit error!");
                return;
            }
            else
            {
                Console.WriteLine("Read bits:");
                Console.WriteLine(read_buffer_bin);
            }
            // Initialize device(Slave Mode, Hardware SPI, Full-Duplex)
            pSPI_Config.ControlMode = 0; // Hardware SPI, Full-Duplex
            pSPI_Config.MasterMode  = 0; // Slave Mode
            pSPI_Config.CPHA        = 0; // Clock Polarity and Phase must be same to master
            pSPI_Config.CPOL        = 0;
            pSPI_Config.LSBFirst    = 0;
            pSPI_Config.TranBits    = 8; // Support 8bit mode only
            ret = ControlSPI.VSI_InitSPI(ControlSPI.VSI_USBSPI, 0, ref pSPI_Config);
            if (ret != ControlSPI.ERROR.SUCCESS)
            {
                Console.WriteLine("Initialize device error!");
                return;
            }
            // Write data in slave mode(call function VSI_SlaveWriteBytes), data will not send out via MISO pin immediately until chip select by master,
            //  function VSI_SlaveWriteBytes return immediately after called, the data stored in adapter memory buffer
            for (int i = 0; i < 8; i++)
            {
                write_buffer[i] = (Byte)i;
            }
            ret = ControlSPI.VSI_SlaveWriteBytes(ControlSPI.VSI_USBSPI, 0, write_buffer, 8);
            if (ret != ControlSPI.ERROR.SUCCESS)
            {
                Console.WriteLine("Write data error!");
                return;
            }
            // Write data in slave mode: last parameter(100) is waiting time(in us),
            //  function will return immediately if time-out(no matter whether read the data or not)
            // Function judge received number of data via parameter read_data_num
            // ATTENTION: After call function VSI_SlaveWriteBytes,
            //  slave will receive the data when master read data(slave in full-duplex)
            //  master can call function VSI_SlaveReadBytes to discard received data after read the data complete from slave
            Int32 read_data_num = 0;

            ret = ControlSPI.VSI_SlaveReadBytes(ControlSPI.VSI_USBSPI, 0, read_buffer, ref read_data_num, 100);
            if (ret != ControlSPI.ERROR.SUCCESS)
            {
                Console.WriteLine("Read data error!");
                return;
            }
            else
            {
                if (read_data_num > 0)
                {
                    Console.WriteLine("Read data num:" + read_data_num);
                    Console.WriteLine("Read data(Hex):");
                    for (int i = 0; i < read_data_num; i++)
                    {
                        Console.WriteLine(read_buffer[i].ToString("X2"));
                    }
                }
                else
                {
                    Console.WriteLine("No data!");
                }
            }
        }
コード例 #5
0
ファイル: Program.cs プロジェクト: gstarnet/Ginkgo
        static void Main(string[] args)
        {
            int ret;

            ControlSPI.VSI_INIT_CONFIG pSPI_Config = new ControlSPI.VSI_INIT_CONFIG();
            //Scan connected device
            ret = ControlSPI.VSI_ScanDevice(1);
            if (ret <= 0)
            {
                Console.WriteLine("No device connect!");
                return;
            }
            // Open device
            ret = ControlSPI.VSI_OpenDevice(ControlSPI.VSI_USBSPI, 0, 0);
            if (ret != ControlSPI.ERROR.SUCCESS)
            {
                Console.WriteLine("Open device error!");
                return;
            }
            // Initialize device
            pSPI_Config.ControlMode = 1;
            pSPI_Config.MasterMode  = 1;
            pSPI_Config.ClockSpeed  = 36000000;
            pSPI_Config.CPHA        = 0;
            pSPI_Config.CPOL        = 0;
            pSPI_Config.LSBFirst    = 0;
            pSPI_Config.TranBits    = 8;
            ret = ControlSPI.VSI_InitSPI(ControlSPI.VSI_USBSPI, 0, ref pSPI_Config);
            if (ret != ControlSPI.ERROR.SUCCESS)
            {
                Console.WriteLine("Initialize device error!");
                return;
            }
            // Get JEDEC ID
            Byte[] write_buffer = new Byte[10240];
            Byte[] Read_buffer  = new Byte[10240];
            write_buffer[0] = 0x9F;//SPI Flash get JEDEC ID command
            ret             = ControlSPI.VSI_WriteReadBytes(ControlSPI.VSI_USBSPI, 0, 0, write_buffer, 1, Read_buffer, 3);
            if (ret != ControlSPI.ERROR.SUCCESS)
            {
                Console.WriteLine("Get JEDEC ID error!");
                return;
            }
            else
            {
                Int32 JEDEC_ID = (Read_buffer[0] << 16) | (Read_buffer[1] << 8) | Read_buffer[2];
                Console.WriteLine("JEDEC ID(Hex):{0}", JEDEC_ID.ToString("X6"));
            }
            // Send write enable
            write_buffer[0] = 0x06;//SPI Flash write enable command
            ret             = ControlSPI.VSI_WriteBytes(ControlSPI.VSI_USBSPI, 0, 0, write_buffer, 1);
            if (ret != ControlSPI.ERROR.SUCCESS)
            {
                Console.WriteLine("Write data error!");
                return;
            }
            // Erase first sector start address h
            write_buffer[0] = 0x20;//SPI Flash sector erase command
            write_buffer[1] = 0x00;
            write_buffer[2] = 0x00;
            write_buffer[3] = 0x00;
            ret             = ControlSPI.VSI_WriteBytes(ControlSPI.VSI_USBSPI, 0, 0, write_buffer, 4);
            if (ret != ControlSPI.ERROR.SUCCESS)
            {
                Console.WriteLine("Write data error!");
                return;
            }
            // Delay to ensure the operation to complete
            System.Threading.Thread.Sleep(100);
            // Write Enable
            write_buffer[0] = 0x06;//SPI Flash write enable command
            ret             = ControlSPI.VSI_WriteBytes(ControlSPI.VSI_USBSPI, 0, 0, write_buffer, 1);
            if (ret != ControlSPI.ERROR.SUCCESS)
            {
                Console.WriteLine("Write data error!");
                return;
            }
            // Page Program
            write_buffer[0] = 0x02; //SPI Flash write data command
            write_buffer[1] = 0x00; //address
            write_buffer[2] = 0x00; //
            write_buffer[3] = 0x00; //
            for (int i = 0; i < 256; i++)
            {
                write_buffer[4 + i] = (Byte)i;
            }
            ret = ControlSPI.VSI_WriteBytes(ControlSPI.VSI_USBSPI, 0, 0, write_buffer, 4 + 256);
            if (ret != ControlSPI.ERROR.SUCCESS)
            {
                Console.WriteLine("Write data error!");
                return;
            }
            else
            {
                Console.WriteLine("Write data succeed!");
            }
            // Delay to ensure the operation to complete
            System.Threading.Thread.Sleep(10);
            // Read Data
            write_buffer[0] = 0x03; //SPI Flash read data command
            write_buffer[1] = 0x00; //
            write_buffer[2] = 0x00; //
            write_buffer[3] = 0x00; //
            ret             = ControlSPI.VSI_WriteReadBytes(ControlSPI.VSI_USBSPI, 0, 0, write_buffer, 4, Read_buffer, 256);
            if (ret != ControlSPI.ERROR.SUCCESS)
            {
                Console.WriteLine("Read data error!");
                return;
            }
            else
            {
                Int32  read_data;
                String read_data_str = "";
                Console.WriteLine("Get Data:");
                for (int i = 0; i < 256; i++)
                {
                    read_data      = Read_buffer[i];
                    read_data_str += read_data.ToString("X2") + " ";
                    if (((i + 1) % 24) == 0)
                    {
                        read_data_str += "\n";
                    }
                }
                Console.WriteLine(read_data_str);
            }
            Console.ReadLine();
        }