コード例 #1
0
        // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // //

        public void Connect()
        {
            // Otwarcie portu
            OpenPort(portName, portBaud, portParity, portStopBits);

            // Reset
            if (resetLine != ResetLine.None)
            {
                DeviceReset();
                ClearBuffer();
            }

            // Połączenie z bootloaderem
            Console.Write("Waiting for a bootloader ...");
            TimeOut.DoWork(delegate { WaitFor('?'); }, defaultDeviceConnectionTimeout);
            Send('s');
            Console.WriteLine("[OK]");
            //Sprawdzanie nagłówka
            TimeOut.DoWork(delegate { WaitFor(0xA0); }, 501); //Identyfikator bootloadera
            // Na razie programuje tylko atm8
            if ((ReadByte() != 0x32))
            {
                throw new Exception("Chip identification failed!"); // Odczyt typu uC
            }
            lfuse = ReadByte();                                     // Dolne fuse
            hfuse = ReadByte();                                     // Górne fuse
            lockb = ReadByte();                                     // Lockbity
            Console.WriteLine("Recieved header. Chip: ATMega32 32kB flash Hfuse:0x{0:X2} Lfuse:0x{1:X2} Lockbis:0x{2:X2}.", hfuse, lfuse, lockb);
        }
コード例 #2
0
        public void WriteEEPROM(UInt16 address, byte data)
        {
            TimeOut.DoWork(delegate { WaitFor('>'); }, 101);
            //Enter EEPROM writing mode
            Send('E');
            ClearBuffer();

            //Send address in two bytes
            byte[] ba = BitConverter.GetBytes(address);
            Send(ba[0]);
            Send(ba[1]);

            //Send data byte
            Send(data);
            Thread.Sleep(5); // Some time for eeprom to finish writing
        }
コード例 #3
0
        public void WriteFlashPage(PageOfCode page)
        {
            byte crc = 0xFF;

            //Enter flash programming mode
            Send('F');
            ClearBuffer();


            //Send page address
            byte[] address = BitConverter.GetBytes(page.number * device.FlashPageSize);
            Send(address[0]);
            Send(address[1]);
            crc ^= address[0];
            crc ^= address[1];

            //Czeka na znak gotowosci
            TimeOut.DoWork(delegate { WaitFor('>'); }, 1003);

            for (int j = 0; j < device.FlashPageSize; j++)
            {
                Send(page.code[j]);
                crc ^= page.code[j];
            }

            byte crcResp = 0xFF;

            TimeOut.DoWork(delegate { crcResp = ReadByte(); }, 1003);

            // Sprawdzenie sumy kontrolnej
            if (crc != crcResp)
            {
                //Console.WriteLine("CRC ERROR {0:X2} {1:X2}", crc, crcResp);
                if (OnFlashPageWriteError != null)
                {
                    OnFlashPageWriteError();
                }
                Send('X');
                WriteFlashPage(page);
                return;
            }

            // Wszystko ok strona zostaje zapisana
            Send('k');
        }
コード例 #4
0
        public byte ReadEEPROM(UInt16 address)
        {
            TimeOut.DoWork(delegate { WaitFor('>'); }, 101);

            //Enter EEPROM reading mode
            Send('e');
            ClearBuffer();

            //Send address in two bytes
            byte[] ba = BitConverter.GetBytes(address);
            Send(ba[0]);
            Send(ba[1]);

            byte response = 0x00;

            TimeOut.DoWork(delegate { response = ReadByte(); }, 501);
            return(response);
        }
コード例 #5
0
        //2DO
        public String ReadFlashPage(UInt16 pageNum)
        {
            string str = "";

            Send('f');
            ClearBuffer();
            //Adress
            byte[] ba = BitConverter.GetBytes((pageNum + 1) * 0xFF);
            Send(ba[0]);
            Send(ba[1]);

            for (int j = 0; j < device.FlashPageSize; j++)
            {
                byte b = 0x00;
                TimeOut.DoWork(delegate { b = ReadByte(); }, 501);
                str += String.Format("{0:X2}", b);
            }
            return(str);
        }
コード例 #6
0
        public String ReadFlash(string filename)
        {
            FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write);

            string str = "";

            Console.WriteLine("  0%                          100%");
            Console.WriteLine("  V                            V");
            Console.Write("  ");
            int lastnum = 0;

            for (int i = 0; i < device.FlashPages; i++)
            {
                Send('f');
                ClearBuffer();
                //Adress
                byte[] ba = BitConverter.GetBytes((i + 1) * 0xFF);
                Send(ba[0]);
                Send(ba[1]);

                for (int j = 0; j < device.FlashPageSize; j++)
                {
                    byte b = 0x00;
                    TimeOut.DoWork(delegate { b = ReadByte(); }, 501);
                    fs.WriteByte(b);
                    str += String.Format("{0:X2}", b);
                }
                //Console.WriteLine(str);
                int newnum = (int)((float)30 * i / (device.FlashPages - 1));
                for (int k = 0; k < (newnum - lastnum); k++)
                {
                    Console.Write("|");
                }
                lastnum = newnum;
                str     = "";
            }
            Console.WriteLine();
            fs.Flush();
            fs.Close();
            return(str);
        }