Пример #1
0
        private static bool Force_PKS_Into_Prog_Mode_Cmd()
        {
            bool   result = false;
            ushort num    = 0;

            byte[] array = new byte[65];
            Array.Clear(array, 0, array.Length);
            array[1] = 66;
            array[2] = 0;
            if (Bootloader.Retrieve_BL_FW_Version_Cmd(ref num))
            {
                return(true);
            }
            if (USBWrite.Send_Data_Packet_To_PICkitS(ref array))
            {
                Firmware.restart_the_device();
                if (Bootloader.Retrieve_BL_FW_Version_Cmd(ref num))
                {
                    result = true;
                }
            }
            return(result);
        }
Пример #2
0
        public static bool Load_Firmware(string p_file_name, int p_device_type, ref string p_error_str, ref int p_error_code)
        {
            bool result = false;

            if (p_device_type == 2)
            {
                Firmware.m_devicetype    = Firmware.DEVICETYPE.LIN;
                Firmware.m_upper_fw_addr = 16384;
            }
            else
            {
                Firmware.m_devicetype    = Firmware.DEVICETYPE.PKSA;
                Firmware.m_upper_fw_addr = 32768;
            }
            Firmware.m_array_size = Firmware.m_upper_fw_addr - 8192;
            if (Firmware.parse_hex_file_put_in_array(p_file_name))
            {
                if (Firmware.Force_PKS_Into_Prog_Mode_Cmd())
                {
                    bool   flag  = false;
                    byte[] array = new byte[14];
                    if (Firmware.Clear_BL_Flash())
                    {
                        if (!Firmware.Write_BL_Flash(ref Firmware.m_flash_data_array))
                        {
                            p_error_str  = "Error - writing flash";
                            flag         = true;
                            p_error_code = 4;
                        }
                        else if (!Bootloader.Write_BL_Config_Bytes(ref Firmware.m_config_bytes_array, ref Firmware.m_config_bool_array))
                        {
                            flag         = true;
                            p_error_str  = "Error - writing config bytes";
                            p_error_code = 5;
                        }
                        else
                        {
                            Firmware.copy_current_fw_array_into_prior_array();
                            if (Firmware.Read_BL_Flash(8192, Firmware.m_upper_fw_addr - 8192, ref Firmware.m_flash_data_array))
                            {
                                for (int i = 0; i < Firmware.m_array_size; i++)
                                {
                                    if (Firmware.m_flash_data_array[i] != Firmware.m_prior_flash_data_array[i])
                                    {
                                        p_error_str  = string.Format("Error - Verification error:  flash byte {0:X2} reads as {1:X2}\nbut should be {2:X2}\n", i, Firmware.m_flash_data_array[i], Firmware.m_prior_flash_data_array[i]);
                                        flag         = true;
                                        p_error_code = 8;
                                        break;
                                    }
                                }
                                if (Bootloader.Read_BL_Config_Data(ref array))
                                {
                                    for (int j = 0; j < array.Length; j++)
                                    {
                                        if (Firmware.m_config_bool_array[j] && array[j] != Firmware.m_config_bytes_array[j])
                                        {
                                            p_error_str  = string.Format("Error - Verification error:  Config byte {0:X2} reads as {1:X2}\nbut should be {2:X2}\n", j, array[j], Firmware.m_config_bytes_array[j]);
                                            flag         = true;
                                            p_error_code = 9;
                                            break;
                                        }
                                    }
                                }
                                else
                                {
                                    flag         = true;
                                    p_error_str  = "Error - reading config bytes";
                                    p_error_code = 7;
                                }
                            }
                            else
                            {
                                flag         = true;
                                p_error_str  = "Error - reading flash.";
                                p_error_code = 6;
                            }
                        }
                    }
                    else
                    {
                        flag         = true;
                        p_error_str  = "Error - clearing flash prior to writing firmware";
                        p_error_code = 3;
                    }
                    if (!flag)
                    {
                        if (Firmware.m_devicetype == Firmware.DEVICETYPE.LIN)
                        {
                            p_error_str = string.Format("LIN firmware written and verified using hex file {0}.\n", p_file_name);
                        }
                        else
                        {
                            p_error_str = string.Format("PICkit Serial firmware written and verified using hex file {0}.\n", p_file_name);
                        }
                        p_error_code = 0;
                        result       = true;
                    }
                    if (Bootloader.Issue_BL_Reset())
                    {
                        Firmware.restart_the_device();
                    }
                    else
                    {
                        p_error_code = 10;
                        p_error_str  = "Error - sending BL Reset Command.  Firmware may not have been updated correctly.";
                        result       = false;
                    }
                }
                else
                {
                    p_error_code = 2;
                    p_error_str  = "Error - Could not enter programming mode prior to updating firmware - download aborted";
                }
            }
            else
            {
                p_error_code = 1;
                p_error_str  = "Error - Could not read hex file - download aborted";
            }
            return(result);
        }