Esempio n. 1
0
 /*
  * Write 1 byte data to 5510
  */
 public static void LCD_WriteByte(Byte Data, Byte Command)
 {
     if (Command != 0)
     {
         ControlGPIO.VGI_SetPins(ControlGPIO.VGI_USBGPIO, 0, ControlPin.LCD_DC_PIN);
     }
     else
     {
         ControlGPIO.VGI_ResetPins(ControlGPIO.VGI_USBGPIO, 0, ControlPin.LCD_DC_PIN);
     }
     Byte[] temp = new Byte[1];
     temp[0] = Data;
     ControlSPI.VSI_WriteBytes(ControlSPI.VSI_USBSPI, 0, 0, temp, 1);
 }
Esempio n. 2
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();
        }
Esempio n. 3
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!");
                }
            }
        }
Esempio n. 4
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
            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;
            }

            // Start write data speed test
            Console.WriteLine("Start write data speed test.....");
            // Write Data Speed Test
            Byte[] write_buffer = new Byte[10240];
            Byte[] read_buffer = new Byte[10240];
            long   litmp = 0;
            double StartTime, EndTime;
            double dfFreq;

            timer.QueryPerformanceFrequency(ref litmp); // Get the performance counter frequency, in n/s
            dfFreq = litmp;
            timer.QueryPerformanceCounter(ref litmp);   // Get the current value of the performance counter
            StartTime = litmp;                          // Start time
            for (i = 0; i < 400; i++)
            {
                ret = ControlSPI.VSI_WriteBytes(ControlSPI.VSI_USBSPI, 0, 0, write_buffer, 10000);
                if (ret != ControlSPI.ERROR.SUCCESS)
                {
                    Console.WriteLine("write data error!");
                    break;
                }
            }
            timer.QueryPerformanceCounter(ref litmp);   // Get the current value of the performance counter
            EndTime = litmp;

            // Print the write data speed information
            Console.WriteLine("Write Data Numbers: {0} Bytes\n", i * 10000);
            Console.WriteLine("Write Data Elapsed Time: {0:###.000}\n", (EndTime - StartTime) / dfFreq);
            Console.WriteLine("Write Data Speed: {0:###.000} KByte/s\n", ((i * 10000) / 1024) / ((EndTime - StartTime) / dfFreq));
            // Start Write Data Speed Test
            Console.WriteLine("\nStart Read Data Speed Test...\n");
            timer.QueryPerformanceCounter(ref litmp); // Get the current value of the performance counter
            StartTime = litmp;                        // Start time
            for (i = 0; i < 400; i++)
            {
                ret = ControlSPI.VSI_ReadBytes(ControlSPI.VSI_USBSPI, 0, 0, read_buffer, 10000);
                if (ret != ControlSPI.ERROR.SUCCESS)
                {
                    Console.WriteLine("Read data error!\n");
                    break;
                }
            }
            timer.QueryPerformanceCounter(ref litmp); // Get the current value of the performance counter
            EndTime = litmp;                          // Stop time
            // Print the write data speed information
            Console.WriteLine("Read Data Numbers: {0:d} Bytes\n", i * 10000);
            Console.WriteLine("Read Data Elapsed Time: {0:f}\n", (EndTime - StartTime) / dfFreq);
            Console.WriteLine("Read Data Speed: {0:f} KByte/s\n", ((i * 10000) / 1024) / ((EndTime - StartTime) / dfFreq));

            return;
        }
Esempio n. 5
0
        static void Main(string[] args)
        {
            int ret;

            //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
            ControlSPI.VSI_INIT_CONFIG SPI_Config;

            SPI_Config.ControlMode = 1;
            SPI_Config.MasterMode  = 1;
            SPI_Config.ClockSpeed  = 36000000;
            SPI_Config.CPHA        = 0;
            SPI_Config.CPOL        = 0;
            SPI_Config.LSBFirst    = 0;
            SPI_Config.TranBits    = 8;
            SPI_Config.SelPolarity = 0;
            ret = ControlSPI.VSI_InitSPI(ControlSPI.VSI_USBSPI, 0, ref SPI_Config);
            if (ret != ControlSPI.ERROR.SUCCESS)
            {
                Console.WriteLine("Initialize device error!");
                return;
            }


            Byte[] WriteDataTemp = new Byte[512];
            char[] data_buff     = new char[4000];
            String strBuff       = new string(' ', 512);

            String[] strTmp = new string[1000];
            UInt16   j;

            // Open file
            FileStream   fs   = new FileStream("data.txt", FileMode.Open, FileAccess.Read);
            StreamReader ftmp = new StreamReader(fs);

            // Read file and write to SPI and console
            do
            {
                // Read data one row from file
                strBuff = ftmp.ReadLine();


                // The string every row use space to separate
                strTmp = strBuff.Split(new char[] { ' ' });
                // The string convert to numerical
                if (strBuff.Length > 1)//if (i > 1)
                {
                    Console.Write("Write Data(Hex): ");
                }
                Console.WriteLine();
                for (j = 0; j < (strTmp.Length); j++)
                {
                    WriteDataTemp[j] = (Byte)(int.Parse(strTmp[j]));// attention data overflow
                    Console.Write("{0,3:d}  ", int.Parse(strTmp[j]));
                }
                Console.WriteLine();
                // Send out the data via USB_SPI
                if (j > 0)
                {
                    ret = ControlSPI.VSI_WriteBytes(ControlSPI.VSI_USBSPI, 0, 0, WriteDataTemp, j);
                    if (ret != ControlSPI.ERROR.SUCCESS)
                    {
                        Console.WriteLine("Write enable error");
                        return;
                    }
                }
            } while (!ftmp.EndOfStream);
            fs.Close();
            return;
        }
Esempio n. 6
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(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]);
            }
        }
Esempio n. 7
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
            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();
        }