Esempio n. 1
0
        public override bool ReadU32(UInt32 Addr, UInt32 Count, ref UInt32[] data)
        {
            GCHandle addr_hndl = GCHandle.Alloc(data, GCHandleType.Pinned);
            IntPtr   addr_data = addr_hndl.AddrOfPinnedObject();
            bool     status    = (JLinkARM.JLINKARM_ReadMemU32(Addr, Count, addr_data, IntPtr.Zero) >= 0) ? true : false;

            addr_hndl.Free();
            return(status);
        }
Esempio n. 2
0
        public override bool WriteU8(UInt32 Addr, UInt32 Count, byte[] data)
        {
            GCHandle addr_hndl = GCHandle.Alloc(data, GCHandleType.Pinned);
            IntPtr   addr_data = addr_hndl.AddrOfPinnedObject();
            bool     status    = (JLinkARM.JLINKARM_WriteMem(Addr, Count, addr_data) >= 0) ? true : false;

            addr_hndl.Free();
            return(status);
        }
Esempio n. 3
0
        public override bool ReadU8(UInt32 Addr, ref byte data)
        {
            GCHandle addr_hndl = GCHandle.Alloc(data, GCHandleType.Pinned);
            IntPtr   addr_data = addr_hndl.AddrOfPinnedObject();
            bool     status    = (JLinkARM.JLINKARM_ReadMem(Addr, 1, addr_data) >= 0) ? true : false;

            addr_hndl.Free();
            return(status);
        }
Esempio n. 4
0
 public override void Disconnect()
 {
     if (IsConnected)
     {
         JLinkARM.JLINKARM_Close();
         IsOpen      = false;
         IsConnected = false;
         IsInited    = false;
     }
 }
Esempio n. 5
0
        public override void Restart()
        {
            if (IsConnected)
            {
                cb_log?.Invoke("Restart SoC ...");

                JLinkARM.JLINKARM_Reset();
                JLinkARM.JLINKARM_Go();

                IsInited = false;
            }
        }
Esempio n. 6
0
        public override bool Connect()
        {
            bool status = false;

            if (IsOpen & JLinkARM.JLINKARM_IsConnected() > 0) // JLinkARM.JLINKARM_Connect() >= 0
            {
                JLinkARM.JLINKARM_SetSpeed(TIFSpeed);
                status = true;
            }

            IsConnected = status;
            return(status);
        }
Esempio n. 7
0
        public override bool InitTargert()
        {
            bool   status = false;
            UInt32 data   = 0;

            FlashID = 0x0;

            cb_log?.Invoke("Init SoC ...");

            //JLinkARM.JLINKARM_Reset();
            JLinkARM.JLINKARM_Halt();
            ReadFlashID();

            // RAM test

            /*
             * UInt32[] test = new UInt32[200];
             * for (UInt16 i = 0; i < test.Length; i++)
             *  test[i] = i;
             * WriteU32((UInt32)AmebaZ_Addresses.RAMAddr, (UInt32)test.Length, test);
             * WriteU32((UInt32)AmebaZ_Addresses.RAMAddr, 0x11002200);
             *
             * UInt32[] test_r = new UInt32[200];
             * ReadU32((UInt32)AmebaZ_Addresses.RAMAddr, 200, ref test_r);
             */

            if (IsConnected)
            {
                // System Init
                ReadU32(0x40000210, ref data);
                data &= 0xe7ff_ffff; // 27, 28
                data |= 0x0400_0000; // 26
                WriteU32(0x40000210, data);

                // Load Flasher
                WriteU32(AmebaZ_Addresses.RAMImage1Addr, (UInt32)AmebaZ_Code.Flasher.Length, AmebaZ_Code.Flasher);

                JLinkARM.JLINKARM_WriteReg((UInt32)JLink_ARM_CM4_Register.FAULTMASK, 0x00);
                JLinkARM.JLINKARM_WriteReg((UInt32)JLink_ARM_CM4_Register.R13, (UInt32)AmebaZ_Addresses.Stack);        // Stack pointer
                JLinkARM.JLINKARM_WriteReg((UInt32)JLink_ARM_CM4_Register.R15, (UInt32)AmebaZ_Addresses.FirmwareAddr); // Program counter
                JLinkARM.JLINKARM_Go();
                status = true;
            }

            IsInited = status;
            return(status);
        }
Esempio n. 8
0
        public override bool OpenDebugger()
        {
            bool status = false;

            if (cb_log == null & CallbackLog != null)
            {
                cb_log = new CallbackLogDelegate(CallbackLog);
            }
            if (cb_err == null & CallbackError != null)
            {
                cb_err = new CallbackLogDelegate(CallbackError);
            }

            cb_log?.Invoke("Open Debugger ...");
            try
            {
                string err = JLinkARM.JLINKARM_OpenEx(cb_log, cb_err);

                if (err != null)
                {
                    status = false;
                    cb_err?.Invoke("JLINKARM_OpenEx Error!");
                    return(status);
                }
                //JLinkARM.JLINKARM_SetSpeed(TIFSpeed);
                JLinkARM.JLINKARM_SetSpeed(1000);
                JLinkARM.JLINKARM_TIF_Select(TIFType);
                JLinkARM.JLINKARM_SelectDeviceFamily(JLink_Device_Family.Cortex_M3);
                //JLinkARM.JLINKARM_SetResetType(JLink_Reset_Type.Reset_Pin);
                //JLinkARM.JLINKARM_ExecCommand("endian little", IntPtr.Zero, 0);
                //JLinkARM.JLINKARM_SetResetDelay(2);
                JLinkARM.JLINKARM_Reset();

                status = true;
            }
            catch (Exception ex)
            {
                cb_err?.Invoke(ex.Message);
            }

            IsOpen = status;
            return(status);
        }
Esempio n. 9
0
        public override void Restart()
        {
            if (IsConnected)
            {
                cb_log?.Invoke("Restart SoC ...");

                JLinkARM.JLINKARM_Reset();
                if (FlashID == 0x141520C2)
                {
                    WriteU32(0x40000210, AmebaA_Code.Reg210B[(byte)RestartType]);
                }
                else
                {
                    WriteU32(0x40000210, AmebaA_Code.Reg210A[(byte)RestartType]);
                }
                JLinkARM.JLINKARM_Go();

                IsInited = false;
            }
        }
Esempio n. 10
0
        public override bool ReadU32(UInt32 Addr, ref UInt32 data)
        {
            /*
             * byte[] data_b = new byte[4];
             * GCHandle addr_hndl = GCHandle.Alloc(data_b, GCHandleType.Pinned);
             * IntPtr addr_data = addr_hndl.AddrOfPinnedObject();
             * bool status = (JLinkARM.JLINKARM_ReadMem(Addr, 4, addr_data) == 0) ? true : false;
             * data = (UInt32)(data_b[0] | (data_b[1] << 8) | (data_b[2] << 16) | (data_b[3] << 24));
             * addr_hndl.Free();
             */

            UInt32[] data_r    = new UInt32[2]; // TODO: Костыль, иначе по IntPtr всегда возвращается 0х0 ...
            GCHandle addr_hndl = GCHandle.Alloc(data_r, GCHandleType.Pinned);
            IntPtr   addr_data = addr_hndl.AddrOfPinnedObject();
            bool     status    = (JLinkARM.JLINKARM_ReadMemU32(Addr, 1, addr_data, IntPtr.Zero) >= 0);

            addr_hndl.Free();
            data = data_r[0];

            return(status);
        }
Esempio n. 11
0
        public override bool ReadU32(UInt32 Addr, ref UInt32 data)
        {
            UInt32[] data_r    = new UInt32[2]; // TODO: Костыль, иначе по IntPtr всегда возвращается 0х0 ...
            GCHandle addr_hndl = GCHandle.Alloc(data_r, GCHandleType.Pinned);
            IntPtr   addr_data = addr_hndl.AddrOfPinnedObject();
            bool     status    = (JLinkARM.JLINKARM_ReadMemU32(Addr, 1, addr_data, IntPtr.Zero) >= 0);

            addr_hndl.Free();
            data = data_r[0];

            /*
             * byte[] data_b = new byte[8];
             * GCHandle addr_hndl = GCHandle.Alloc(data_b, GCHandleType.Pinned);
             * IntPtr addr_data = addr_hndl.AddrOfPinnedObject();
             * bool status = (JLinkARM.JLINKARM_ReadMemHW(Addr, 8, addr_data) == 0);
             * //data = (UInt32)(data_b[7] | data_b[6] << 4 | data_b[5] << 8 | data_b[4] << 12 | data_b[3] << 16 | data_b[2] << 20 | data_b[1] << 24 | data_b[0] << 28);
             * data = (UInt32)(data_b[0] | data_b[1] << 4 | data_b[2] << 8 | data_b[3] << 12 | data_b[4] << 16 | data_b[5] << 20 | data_b[6] << 24 | data_b[7] << 28);
             * addr_hndl.Free();
             */
            return(status);
        }
Esempio n. 12
0
        public bool FlashWrite(UInt32 Addr, UInt32 Size, byte[] Data)
        {
            bool  status         = false;
            bool  fullflasharray = false;
            Int32 Size_Left      = (Int32)Size;

            if (IsInited & Data.Length >= Size & Size > 0)
            {
                Int32  len          = 0;
                Int32  len_actual   = 0;
                UInt32 offset       = 0;
                UInt32 offset_flash = 0;

                fullflasharray = Data.Length == FlashSize;

                CallbackLog?.Invoke(String.Format("Flash Write at 0x{0:X}, size {1}", Addr, Size));
                if (Size > FlashSize)
                {
                    CallbackError?.Invoke(String.Format("Error! Size {0} > {1}", Size, FlashSize));
                    return(status);
                }

                status = true;
                TimeSpan time = StopwatchUtil.Time(() =>
                {
                    while (Size_Left > 0 & status)
                    {
                        len = Size_Left;

                        if (len > AmebaA_Addresses.Size)
                        {
                            len = (Int32)AmebaA_Addresses.Size;
                        }

                        offset_flash = Addr + offset;
                        len_actual   = len;

                        if ((len_actual & 0xff) > 0)
                        {
                            len_actual = (Int32)((len_actual & 0xffffff00) + 0x100);
                        }

                        byte[] Data_RAW = new byte[len_actual];
                        if (fullflasharray)
                        {
                            Array.Copy(Data, offset_flash, Data_RAW, 0, len_actual);
                        }
                        else
                        {
                            Array.Copy(Data, offset, Data_RAW, 0, len_actual);
                        }

                        GCHandle Data_RAW_hndl = GCHandle.Alloc(Data_RAW, GCHandleType.Pinned);
                        IntPtr Data_RAW_Ptr    = Data_RAW_hndl.AddrOfPinnedObject();
                        JLinkARM.JLINKARM_WriteMem(AmebaA_Addresses.Addr + 0x20, (UInt32)len_actual, Data_RAW_Ptr);
                        Data_RAW_hndl.Free();

                        //WriteU8(RTL8195_Addresses.Addr + 0x20, (UInt32)len_actual, Data_RAW);

                        UInt32 len_check = offset_flash;
                        while (len_check < (offset_flash + len_actual /*len*/) & status)
                        {
                            UInt32 sector = (UInt32)len_check /*len_actual*/ / AmebaA_Addresses.FlashSectorSize;
                            //CallbackLog?.Invoke(String.Format("Erase Sector {0}, Offset {0:X}...", sector, len_check));
                            if (!EraseSector(sector))
                            {
                                CallbackError?.Invoke(String.Format("Erase Block Error! Sector {0}", sector));
                                status = false;
                                break;
                            }
                            len_check += AmebaA_Addresses.FlashSectorSize;
                        }

                        if (!StartWriteBlock(offset_flash, (UInt32)len_actual /*len*/) & status)
                        {
                            CallbackError?.Invoke(String.Format("Write Block Error! Offset {0:X} Len {1}", offset_flash, len_actual /*len*/));
                            status = false;
                            break;
                        }

                        if (VerifyFlashWrite & status)
                        {
                            if (!StartVerifyBlock(offset_flash, (UInt32)len_actual /*len*/))
                            {
                                CallbackError?.Invoke(String.Format("Verify Block Error! Offset {0:X} Len {1}", offset_flash, len_actual /*len*/));
                                status = false;
                                break;
                            }
                        }

                        offset    += (UInt32)len_actual; // len;
                        Size_Left -= len_actual;         // len;
                    }
                });

                CallbackLog?.Invoke(String.Format("Time {0:0.00} ms, Speed {1:0.00} KB/s", time.TotalMilliseconds, (double)((double)Size / time.TotalMilliseconds)));
            }

            if (status)
            {
                CallbackLog?.Invoke("Full Flash Write Done!");
            }

            return(status);
        }
Esempio n. 13
0
        public override bool WriteU32(UInt32 Addr, UInt32 data)
        {
            bool status = (JLinkARM.JLINKARM_WriteU32(Addr, data) == 0) ? true : false;

            return(status);
        }
Esempio n. 14
0
        public override bool InitTargert()
        {
            bool   status = false;
            UInt32 data   = 0;

            FlashID = 0x0;
            //JedecID = 0x0;

            cb_log?.Invoke("Init SoC ...");

            JLinkARM.JLINKARM_Reset();
            //JLinkARM.JLINKARM_ResetNoHalt();

            // TEST
            //UInt32[] test = new UInt32[200];
            //WriteU32((UInt32)AmebaA_Flasher.RAMAddr, 4, new UInt32[] { 0x11aa22bb , 0x33cc44dd , 0x66778899, 0x11223344 });
            //WriteU32((UInt32)AmebaA_Flasher.RAMAddr, 0x11002200);
            //ReadU32((UInt32)AmebaA_Flasher.RAMAddr, ref data);
            //ReadU32((UInt32)AmebaA_Flasher.RAMAddr, 200, ref test);

            if (IsConnected)
            {
                // System Init
                WriteU32(0x40000304, 0x1FC00002);
                WriteU32(0x40000250, 0x400);
                WriteU32(0x40000340, 0x0);
                WriteU32(0x40000230, 0xdcc4);
                WriteU32(0x40000210, 0x11113);
                WriteU32(0x400002c0, 0x110011);
                WriteU32(0x40000320, 0xffffffff);

                WriteU32(0x40000014, 0x011); // Set CLK 166.66 MHz

                status = ReadU32(0x40000230, ref data);

                if (status)
                {
                    // enable spi flash peripheral clock
                    data |= 0x300;
                    WriteU32(0x40000230, data);

                    // enable spi flash peripheral
                    ReadU32(0x40000210, ref data);
                    data &= 0x0FFFFFF;
                    data |= 0x10;
                    WriteU32(0x40000210, data);

                    // select spi flash pinout (0 - internal), enable spi flash pins
                    ReadU32(0x400002C0, ref data);
                    data &= 0xFFFFFFF8; // 0xFFFFFFF9
                    data |= 0x1;
                    WriteU32(0x400002C0, data);

                    WriteU32(0x40006008, 0x0); // disable SPI FLASH operation
                    WriteU32(0x4000602C, 0x0); // disable all interrupts
                    WriteU32(0x40006010, 0x1); // use first "slave select" pin
                    WriteU32(0x40006014, 0x2); // baud rate, default value
                    WriteU32(0x40006018, 0x0); // tx fifo threshold
                    WriteU32(0x4000601C, 0x0); // rx fifo threshold
                    WriteU32(0x4000604C, 0x0); // disable DMA

                    WriteU32(AmebaA_Addresses.Addr + 0x08, 0);
                    WriteU32(AmebaA_Addresses.Addr + 0x00, 1);
                    WriteU32(AmebaA_Addresses.FirmwareAddr, (UInt32)AmebaA_Code.Flasher.Length, AmebaA_Code.Flasher);
                    // Если нажали сброс в пустую RAM, то запустит RTLConcoleRAM
                    WriteU32(AmebaA_Addresses.RAMBootAddr, (UInt32)AmebaA_Code.Console.Length, AmebaA_Code.Console);

                    JLinkARM.JLINKARM_WriteReg((UInt32)JLink_ARM_CM3_Register.FAULTMASK, 0x00);                            //0x01
                    JLinkARM.JLINKARM_WriteReg((UInt32)JLink_ARM_CM3_Register.R13, (UInt32)AmebaA_Addresses.Stack);        // $sp
                    JLinkARM.JLINKARM_WriteReg((UInt32)JLink_ARM_CM3_Register.R15, (UInt32)AmebaA_Addresses.FirmwareAddr); // $pc
                    JLinkARM.JLINKARM_Go();
                    //cb_log?.Invoke(String.Format("Is halted ? {0}", JLinkARM.JLINKARM_IsHalted()));
                    status = false;

                    if (WaitTarget())
                    {
                        ReadFlashID();
                        //ReadU32(AmebaA_Flasher.Addr + 0x0C, ref data);
                        //FlashID = (UInt32)((data & 0xFFFFFF) | (FlashWriteReadCMD(0xAB, 3, 1) << 24));
                        CheckFlashID();
                        status = true;
                    }
                }
            }

            IsInited = status;
            return(status);
        }
Esempio n. 15
0
        public bool FlashWrite(UInt32 Addr, UInt32 Size, byte[] Data)
        {
            bool   status     = false;
            Int32  Size_Left  = (Int32)Size;
            Int32  len_actual = 0;
            UInt32 offset     = 0;

            if (IsInited & Data.Length >= Size & Size > 0)
            {
                CallbackLog?.Invoke(String.Format("Flash Write at 0x{0:X}, size {1}", Addr, Size));
                status = true;
                TimeSpan time = StopwatchUtil.Time(() =>
                {
                    //JLinkARM.JLINKARM_SetBP(0, AmebaZ_Addresses.FlasherBreakpoint);
                    JLinkARM.JLINKARM_SetBPEx(AmebaZ_Addresses.FlasherBreakpoint, (0x000000F0 | 0xFFFFFF00 | 0x1));
                    //Thread.Sleep(25);
                    //JLinkARM.JLINKARM_GoEx(0x8000_0000, 1);

                    while (status)
                    {
                        len_actual = Size_Left;

                        if (len_actual > AmebaZ_Addresses.FlasherDataLen)
                        {
                            len_actual = (Int32)AmebaZ_Addresses.FlasherDataLen;
                        }

                        int halted = JLinkARM.JLINKARM_WaitForHalt(2000);
                        if (halted == 0)
                        {
                            CallbackError?.Invoke(String.Format("Wait for flasher Timeout!"));
                            status = false;
                            break;
                        }
                        else if (halted < 0)
                        {
                            CallbackError?.Invoke(String.Format("SoC Halt Error!"));
                            status = false;
                            break;
                        }

                        byte[] Data_RAW = new byte[len_actual];
                        Array.Copy(Data, offset, Data_RAW, 0, len_actual);

                        GCHandle Data_RAW_hndl = GCHandle.Alloc(Data_RAW, GCHandleType.Pinned);
                        IntPtr Data_RAW_Ptr    = Data_RAW_hndl.AddrOfPinnedObject();
                        JLinkARM.JLINKARM_WriteMem(AmebaZ_Addresses.FlasherData, (UInt32)len_actual, Data_RAW_Ptr);
                        Data_RAW_hndl.Free();

                        WriteU32(AmebaZ_Addresses.FlasherWriteAddr, Addr + offset);
                        WriteU32(AmebaZ_Addresses.FlasherBlockWriteSize, (UInt32)len_actual); // старт записи

                        Size_Left -= len_actual;
                        offset    += (UInt32)len_actual;

                        if (Size_Left == 0)
                        {
                            break;
                        }

                        //JLinkARM.JLINKARM_Go();
                        JLinkARM.JLINKARM_GoEx(0x8000_0000, 1);
                    }
                });

                WriteU8(AmebaZ_Addresses.FlasherSetComplete, 0x01); // выходим из загрузчика
                //JLinkARM.JLINKARM_Go();
                JLinkARM.JLINKARM_GoEx(0x8000_0000, 1);
                CallbackLog?.Invoke(String.Format("Time {0:0.00} ms, Speed {1:0.00} KB/s", time.TotalMilliseconds, (double)((double)Size / time.TotalMilliseconds)));
            }

            return(status);
        }
Esempio n. 16
0
        public override bool WriteU8(UInt32 Addr, byte data)
        {
            bool status = (JLinkARM.JLINKARM_WriteU8(Addr, data) == 0 ? true : false);

            return(status);
        }