• 【Jlink & C#】通过C#实现Jlink RTT上位机的功能


    目的

    某天看到安富莱家的自己开发的调试器可以实现输出中文调试信息到自己编写的Jlink RTT上位机上,觉得无比神奇,略好奇到底是如何做的,找了很久,发现其上位机的对应的实现好像也没有完全开源出来。

    后来经过很多搜索确认,知道了几个事实:

    1. Jlink自己自带的上位机J-Link RTT Viewer 不支持中文调试信息显示
    2. Jlink自己自带的命令行终端J-Link RTT Client是可以显示中文的,它的原理应该类似一个客户端连接到服务器,socket那种搞法的
    3. Jlink本身其实有提供一个叫SDK的东西,也就是直接控制Jlink的的库,但是很遗憾,这个是收费的,贼贵!!!!
    4. 某个国内的大佬使用pylink(python实现的控制Jlink的一个库)实现了一个类似与J-Link RTT Viewer 那样的东西,虽然比较简陋,但是我感觉已经很不错了,链接-》RTT-T

    那么有没有可能用C#来控制Jlink呢?

    C#控制Jlink的可能性

    答案是,有!!!
    解决思路在于,C#调用JLinkARM.dll里面的函数
    这里转载下我认为很有意义的发现

    1. JLinkARM.dll部分导出函数定义
    2. JlinkARM.DLL API与VB声明
    3. c#的 JLINK API,自己用的

    我和一个老哥讨论的一些成果

    感谢这个老哥xtq0009和我探讨了下Jlink DLL的问题,他也在做类似的东西~
    我总结下我和他探讨的成果~

    1. 他认为这个RTT的API可能不是那样用的,RTT的功能是通过芯片上的一篇内存区域来做的的,这个可以参考SEGGER JLINK的WIKI
      老哥说他感觉可能只要找到这个图中Up和Down描述描述符的位置,然后通过现在已知可以用的接口(读写寄存器的接口)可能就可以实现RTT了,之前我没有朝这个方向去找,去IDA反编译找API了~ 但是好像直接去读对应的RTT结构体的内存地址,会有问题,它提示是保护的,读不了!!
    2. 目前找这个描述符的位置似乎只能是通过编译固件出来之后的map文件来找,我原来其实也很好奇Jlink的RTT终端下面有一个自动获取地址的输入窗,我一直纳闷怎么用,是不是也和这个地址有关系,或者说这个地址是通过内核,芯片来决定的
      在这里插入图片描述
      连接上芯片的LOG,感觉对照数据手册,是有关联的,跟调试组件有关系,具体什么关系,具体Jlink RTT它用了啥组件,暂时不知道
      在这里插入图片描述

    我的研究

    结合以上的几个发现,加上我自己的测试,我把我对接JLinkARM.dll的C#接口分享出来,不保证都能用,我只是测试了几个常用的功能,RTT功能还有些问题,不稳定,有时候可以连上,有时候不能,但是连接Jlink,读取CPU ID,读取Jlink固件版本啥的似乎可以。

    稳定性问题

    不稳定的原因有可能找到了,原来RTT Read的接口,我没有指定传入缓存区的大小,指定了之后,基本上打开运行就是稳定的了
    但是数据有时候还是会重复读取一次,暂时没有找到原因,以下是运行截图,TEST那句话是我从板子输出的
    在这里插入图片描述

    中文RTT输出

    这个真实牛逼到我了
    我找到让Jlink RTT输出中文的方法了

    需要这么干:
    首先芯片的代码编码格式选择UTF-8编码
    注意是你输出RTT的代码的那个编码,因为RTT组件,它本身就是UTF-8编码的~~
    然后RTT_Read接口
    charset写auto

    这么写

            /// 
            /// 从RTT回读数据
            /// 
            /// 
            /// 
            /// 
            [DllImport("JLinkARM.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
            public static extern void JLINK_RTTERMINAL_Read(int terminal, [Out(), MarshalAs(UnmanagedType.LPArray)] byte[] rxBuffer, UInt32 size);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    然后C#调用这里使用UTF8编码读取RTT发送来的字符

            public static string JLINKARM_ReadRTT_String()
            {
                try
                {
                    byte[] aa = new byte[1000];
                    JLINK_RTTERMINAL_Read(0, aa,1000);
    
                   ASCIIEncoding kk = new ASCIIEncoding();
                    //使用UTF-8编码
                    UTF8Encoding uu = new UTF8Encoding();
                    string ss = uu.GetString(aa);
                    if(ss.Length>1)
                    {
                        Console.Write(ss);
                    }
                    return ss;
                }
                catch(Exception ex)
                {
    
                }
                return String.Empty;
    
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    然后见证奇迹的一刻~
    其实我非常想写埃斯B Jlink,真是够~~
    在这里插入图片描述

    RTT输入输出缓冲区的默认值

    注意SEGGER_RTT_Conf.h里面上下行数据缓冲区有默认值,
    默认值 上行1K,下行64字节,printf的空间是64字节
    在这里插入图片描述
    如果你需要收发比较长的数据请修改三个宏.

    使用的.net 4.8框架
    JLinkHandler.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    /*引入C/C++生成的dll时需要添加*/
    using System.Runtime.InteropServices;
    using System.Threading;
    
    namespace Jlink_demo
    {
        internal class JLinkHandler
        {
            public static  void JLINKARM_Sleep(int ms)
            {
                Thread.Sleep(ms);
            }
    
            /// 
            /// 打开JLINK设备
            /// 
            /// 
            [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            public static extern void JLINKARM_Open();
    
    
            /// 
            /// 关闭JLINK设备
            /// 
            /// 
            [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            public static extern void JLINKARM_Close();
    
            /// 
            /// 连接设备
            /// 
            [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            public static extern void JLINKARM_Connect();
    
            /// 
            /// 开启RTT
            /// 
            /// 
            /// 
            [DllImport("JLinkARM.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            public static extern UInt32 JLINK_RTTERMINAL_Control(UInt32 CMD, UInt32 size);
    
            /// 
            /// 从RTT回读数据
            /// 
            /// 
            /// 
            /// 
            [DllImport("JLinkARM.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
            public static extern void JLINK_RTTERMINAL_Read(int terminal, [Out(), MarshalAs(UnmanagedType.LPArray)] byte[] rxBuffer, UInt32 size);
    
    
            static string gbk_ansi(string str)
            {
                string keyword;
                byte[] buffer = Encoding.GetEncoding("GB2312").GetBytes(str);
                keyword = Encoding.UTF8.GetString(buffer);
                return keyword;
            }
    
            public static string JLINKARM_ReadRTT_String()
            {
                try
                {
                    byte[] aa = new byte[1000];
                    JLINK_RTTERMINAL_Read(0, aa,1000);
    
                   ASCIIEncoding kk = new ASCIIEncoding();
                    //使用UTF-8编码
                    UTF8Encoding uu = new UTF8Encoding();
                    string ss = uu.GetString(aa);
                    if(ss.Length>1)
                    {
                        Console.Write(ss);
                    }
                    return ss;
                }
                catch(Exception ex)
                {
    
                }
                return String.Empty;
    
            }
    
    
            /// 
            /// 写数据到RTT
            /// 
            /// 
            /// 
            /// 
            [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            public static extern void JLINK_RTTERMINAL_Write(int terminal, [In(), MarshalAs(UnmanagedType.LPArray)] byte[] txBuffer, UInt32 size);
    
    
    
    
            /// 
            /// 系统复位
            /// 
            /// 
            [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            public static extern void JLINKARM_Reset();
    
    
            [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            public static extern void JLINKARM_GoAllowSim();
    
    
            /// 
            /// 执行程序
            /// 
            /// 
            [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            public static extern void JLINKARM_Go();
    
    
            /// 
            /// 中断程序执行
            /// 
            /// 
            /// 
            [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            public static extern bool JLINKARM_Halt();
    
            /// 
            /// 单步执行
            /// 
            /// 
            /// 
            [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            public static extern bool JLINKARM_Step();
    
    
            /// 
            /// 清除错误信息
            /// 
            /// 
            [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            public static extern void JLINKARM_ClrError();
    
    
            /// 
            /// 设置JLINK接口速度
            /// 
            /// 
            /// 0为自动调整
            [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            public static extern void JLINKARM_SetSpeed(int speed);
    
    
            /// 
            /// 设置JTAG为最高速度
            /// 
            /// 
            [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            public static extern void JLINKARM_SetMaxSpeed()
            ;
    
            [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            public static extern UInt16 JLINKARM_GetSpeed()
           ;
    
            [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            public static extern UInt32 JLINKARM_GetVoltage()
        ;
    
            /// 
            /// 当前MCU是否处于停止状态
            /// 
            /// 
            /// 
            [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            public static extern bool JLINKARM_IsHalted()
            ;
    
            [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            public static extern bool JLINKARM_IsConnected()
           ;
    
            /// 
            /// JLINK是否已经可以操作了
            /// 
            /// 
            /// 
            [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            public static extern bool JLINKARM_IsOpen()
            ;
    
            /// 
            /// 取消程序断点
            /// 
            /// 断点序号
            /// 配合JLINKARM_SetBP()使用
            [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            public static extern void JLINKARM_ClrBP(UInt32 index)
           ;
    
            /// 
            /// 设置程序断点
            /// 
            /// 断点序号
            /// 目标地址
            /// 建议使用JLINKARM_SetBPEx()替代
            [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            public static extern void JLINKARM_SetBP(UInt32 index, UInt32 addr)
            ;
    
            /// 
            /// 设置程序断点
            /// 
            /// 目标地址
            /// 断点类型
            /// Handle,提供给JLINKARM_ClrBPEx()使用
            /// 
            [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            public static extern int JLINKARM_SetBPEx(UInt32 addr, BP_MODE mode)
          ;
            /// 
            /// 取消程序断点
            /// 
            /// 
            /// 配合JLINKARM_SetBPEx()使用
            [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            public static extern void JLINKARM_ClrBPEx(int handle)
            ;
    
            [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            private static extern int JLINKARM_SetWP(UInt32 addr, UInt32 addrmark, UInt32 dat, UInt32 datmark, byte ctrl, byte ctrlmark)
            ;
    
            /// 
            /// 取消数据断点
            /// 
            /// 
            /// 配合JLINKARM_SetWP()使用
            [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            public static extern void JLINKARM_ClrWP(int handle)
            ;
    
            /// 
            /// 设置寄存器
            /// 
            /// 
            /// 
            /// 
            [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            public static extern void JLINKARM_WriteReg(ARM_REG index, UInt32 dat)
           ;
    
            /// 
            /// 读取寄存器
            /// 
            /// 
            /// 
            /// 
            [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            public static extern UInt32 JLINKARM_ReadReg(ARM_REG index)
            ;
    
            /// 
            /// 写入一段数据
            /// 
            /// 
            /// 
            /// 
            /// 
            [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            public static extern void JLINKARM_WriteMem(UInt32 addr, UInt32 size, byte[] buf)
            ;
    
            /// 
            /// 读取一段数据
            /// 
            /// 
            /// 
            /// 
            /// 
            [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            public static extern void JLINKARM_ReadMem(UInt32 addr, UInt32 size, [Out(), MarshalAs(UnmanagedType.LPArray)] byte[] buf)
            ;
    
            /// 
            /// 从调试通道获取一串数据
            /// 
            /// 
            /// 需要获取的数据长度
            /// 
            [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            public static extern void JLINKARM_ReadDCCFast([Out(), MarshalAs(UnmanagedType.LPArray)] UInt32[] buf, UInt32 size)
           ;
    
            /// 
            /// 从调试通道获取一串数据
            /// 
            /// 
            /// 希望获取的数据长度
            /// 
            /// 实际获取的数据长度
            /// 
            [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            public static extern UInt32 JLINKARM_ReadDCC([Out(), MarshalAs(UnmanagedType.LPArray)] UInt32[] buf, UInt32 size, int timeout)
            ;
    
            /// 
            /// 向调试通道写入一串数据
            /// 
            /// 
            /// 需要写入的数据长度
            /// 
            [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            public static extern void JLINKARM_WriteDCCFast(UInt32[] buf, UInt32 size)
            ;
    
            /// 
            /// 向调试通道写入一串数据
            /// 
            /// 
            /// 希望写入的数据长度
            /// 
            /// 实际写入的数据长度
            /// 
            [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            public static extern UInt32 JLINKARM_WriteDCC(UInt32[] buf, UInt32 size, int timeout)
            ;
    
            /// 
            /// 获取JLINK的DLL版本号
            /// 
            /// 
            /// 使用10进制数表示
            [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            public static extern UInt32 JLINKARM_GetDLLVersion()
           ;
    
            /// 
            /// 执行命令
            /// 
            /// 
            /// 
            /// 
            [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            public static extern void JLINKARM_ExecCommand([In(), MarshalAs(UnmanagedType.LPArray)] byte[] oBuffer,int a,int b);
    
            /// 
            /// 选择接口,0是JTAG 1是SWD
            /// 
            /// 
            [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            public static extern void JLINKARM_TIF_Select(int type);
    
    
    
    
    
            /// 
            /// 获取JLINK的固件版本号
            /// 
            /// 
            /// 
            [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            public static extern UInt32 JLINKARM_GetHardwareVersion()
            ;
            [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            private static extern void JLINKARM_GetFeatureString([Out(), MarshalAs(UnmanagedType.LPArray)] byte[] oBuffer)
           ;
    
            [DllImport("JLinkARM.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            private static extern void JLINKARM_GetOEMString([Out(), MarshalAs(UnmanagedType.LPArray)] byte[] oBuffer)
            ;
    
            [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            public static extern void JLINKARM_SetLogFile([In(), MarshalAs(UnmanagedType.LPArray)] byte[] oBuffer)
    ;
    
    
            [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            public static extern StringBuilder JLINKARM_GetCompileDateTime()
            ;
    
            [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            public static extern UInt32 JLINKARM_GetSN()
            ;
    
            /// 
            /// 获取当前MCU的ID号
            /// 
            /// 
            /// 
            [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            public static extern UInt32 JLINKARM_GetId()
            ;
    
            [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            private static extern void JLINKARM_ReadMemU32(UInt32 addr, UInt32 leng, ref UInt32 buf, ref byte status)
           ;
    
            /// 
            /// 写入32位的数据
            /// 
            /// 
            /// 
            /// 
            [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            public static extern void JLINKARM_WriteU32(UInt32 addr, UInt32 dat)
          ;
    
            [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            private static extern void JLINKARM_ReadMemU16(UInt32 addr, UInt32 leng, ref UInt16 buf, ref byte status)
           ;
            /// 
            /// 写入16位的数据
            /// 
            /// 
            /// 
            /// 
            [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            public static extern void JLINKARM_WriteU16(UInt32 addr, UInt16 dat)
      ;
    
            [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            private static extern void JLINKARM_ReadMemU8(UInt32 addr, UInt32 leng, ref byte buf, ref byte status)
    ;
    
            /// 
            /// 写入8位的数据
            /// 
            /// 
            /// 
            /// 
            [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            public static extern void JLINKARM_WriteU8(UInt32 addr, byte dat)
           ;
    
            /// 
            /// 读取32位的数据
            /// 
            /// 
            /// 
            /// 
            public UInt32 JLINKARM_ReadU32(UInt32 addr)
            {
                UInt32 dat = 0;
                byte stu = 0;
                JLINKARM_ReadMemU32(addr, 1, ref dat, ref stu);
                return dat;
            }
    
            /// 
            /// 读取16位的数据
            /// 
            /// 
            /// 
            /// 
            public UInt16 JLINKARM_ReadU16(UInt32 addr)
            {
                UInt16 dat = 0;
                byte stu = 0;
                JLINKARM_ReadMemU16(addr, 1, ref dat, ref stu);
                return dat;
            }
    
            /// 
            /// 读取8位的数据
            /// 
            /// 
            /// 
            /// 
            public byte JLINKARM_ReadU8(UInt32 addr)
            {
                byte dat = 0;
                byte stu = 0;
                JLINKARM_ReadMemU8(addr, 1, ref dat, ref stu);
                return dat;
            }
    
            /// 
            /// 设置数据断点
            /// 
            /// 目标地址
            /// 地址屏蔽位
            /// 目标数据
            /// 数据屏蔽位
            /// 触发模式
            /// Handle,提供给JLINKARM_ClrWP()函数使用
            /// 当前数值除了屏蔽位以外的数据位,与目标数据除了屏蔽位以外的数据位,一致即可产生触发
            public int JLINKARM_SetWP(UInt32 addr, UInt32 addrmark, UInt32 dat, UInt32 datmark, WP_MODE mode)
            {
                switch (mode)
                {
                    case WP_MODE.READ_WRITE:
                        return JLINKARM_SetWP(addr, addrmark, dat, datmark, 0x8, 0xf7);
                    case WP_MODE.READ:
                        return JLINKARM_SetWP(addr, addrmark, dat, datmark, 0x8, 0xf6);
                    case WP_MODE.WRITE:
                        return JLINKARM_SetWP(addr, addrmark, dat, datmark, 0x9, 0xf6);
                    default:
                        {
                            return 0xff;
                        }
                }
            }
    
            public string JLINKARM_StringFeature()
            {
                byte[] aa = new byte[1000];
                JLINKARM_GetFeatureString(aa);
                ASCIIEncoding kk = new ASCIIEncoding();
                string ss = kk.GetString(aa);
                return ss;
            }
    
            public string JLINKARM_StringOEM()
            {
                byte[] aa = new byte[1000];
                JLINKARM_GetOEMString(aa);
                ASCIIEncoding kk = new ASCIIEncoding();
                string ss = kk.GetString(aa);
                return ss;
            }
    
            public void JLINKARM_Setup(string cmdstr)
            {
                //RmAnnotate rm = new RmAnnotate();
                //rm.MarkDosMode = false;
                //rm.MarkStartSpaceToTab = 0;
                //cmdstr = rm.Convert(cmdstr);
                cmdstr = cmdstr.Replace("\n", "").Replace(" ", "").Replace("\t", "").Replace("\r", "");
                string[] cmd = cmdstr.Split(';');
                for (int i = 0; i <= cmd.Length - 1; i++)
                {
                    _setupDoCmd(cmd[i]);
                }
            }
            private void _setupDoCmd(string cmdstr)
            {
                string cmd = cmdstr.ToLower();
                cmd = cmd.Replace("(", ",");
                cmd = cmd.Replace(")", "");
                cmd = cmd.TrimEnd(',');
                string[] arg = cmd.Split(',');
    
                UInt32 val1;
                UInt32 val2;
                if (arg.Length == 3)
                {
                    cmd = arg[0];
                    val1 = _setupGetVal(arg[1]);
                    val2 = _setupGetVal(arg[2]);
                }
                else if (arg.Length == 2)
                {
                    cmd = arg[0];
                    val1 = _setupGetVal(arg[1]);
                    val2 = 0;
                }
                else if (arg.Length == 1)
                {
                    cmd = arg[0];
                    val1 = 0;
                    val2 = 0;
                }
                else
                {
                    cmd = "";
                    val1 = 0;
                    val2 = 0;
                }
    
                if (cmd != "")
                {
                    Console.WriteLine("Do CMD: " + cmdstr);
                    switch (cmd)
                    {
                        case "setjtagspeed":
                            JLINKARM_SetSpeed((int)val1);
                            break;
                        case "delay":
                            JLINKARM_Sleep((int)val1);
                            break;
                        case "disablemmu":
                            Console.WriteLine("...........................CMD not Supported");
                            break;
                        case "go":
                            JLINKARM_Go(); break;
                        case "halt":
                            JLINKARM_Halt(); break;
                        case "reset":
                            JLINKARM_Reset();
                            if (val1 > 0)
                            {
                                JLINKARM_Sleep((int)val1);
                            }
                            break;
                        case "resetbp0":
                            Console.WriteLine("...........................CMD not Supported"); break;
                        case "resetadi":
                            Console.WriteLine("...........................CMD not Supported"); break;
                        case "read8":
                            JLINKARM_ReadU8(val1); break;
                        case "read16":
                            JLINKARM_ReadU16(val1); break;
                        case "read32":
                            JLINKARM_ReadU32(val1); break;
                        case "verify8":
                            do
                            {
                                byte aa = JLINKARM_ReadU8(val1);
                                if (aa == ((byte)val2 & 0xff))
                                {
                                    break; // TODO: might not be correct. Was : Exit Do
                                }
                                JLINKARM_Sleep(1);
                            } while (true);
                            break;
                        case "verify16":
                            do
                            {
                                ushort aa = JLINKARM_ReadU16(val1);
                                if (aa == ((ushort)val2 & 0xffff))
                                {
                                    break; // TODO: might not be correct. Was : Exit Do
                                }
                                JLINKARM_Sleep(1);
                            } while (true);
                            break;
                        case "verify32":
                            do
                            {
                                uint aa = JLINKARM_ReadU32(val1);
                                if (aa == val2)
                                {
                                    break; // TODO: might not be correct. Was : Exit Do
                                }
                                JLINKARM_Sleep(1);
                            } while (true);
                            break;
                        case "write8":
                            JLINKARM_WriteU8(val1, (byte)val2);
                            break;
                        case "write16":
                            JLINKARM_WriteU16(val1, (ushort)val2);
                            break;
                        case "write32":
                            JLINKARM_WriteU32(val1, val2);
                            break;
                        case "writeverify8":
                            do
                            {
                                JLINKARM_WriteU8(val1, (byte)val2);
                                byte aa = JLINKARM_ReadU8(val1);
                                if (aa == ((byte)val2 & 0xff))
                                {
                                    break; // TODO: might not be correct. Was : Exit Do
                                }
                                JLINKARM_Sleep(1);
                            } while (true);
                            break;
                        case "writeverify16":
                            do
                            {
                                JLINKARM_WriteU16(val1, (ushort)val2);
                                ushort aa = JLINKARM_ReadU16(val1);
                                if (aa == ((ushort)val2 & 0xffff))
                                {
                                    break; // TODO: might not be correct. Was : Exit Do
                                }
                                JLINKARM_Sleep(1);
                            } while (true);
                            break;
                        case "writeverify32":
                            do
                            {
                                JLINKARM_WriteU32(val1, val2);
                                uint aa = JLINKARM_ReadU32(val1);
                                if (aa == val2)
                                {
                                    break; // TODO: might not be correct. Was : Exit Do
                                }
                                JLINKARM_Sleep(1);
                            } while (true);
                            break;
    
                        case "writeregister":
                            JLINKARM_WriteReg((ARM_REG)val1, val2);
                            break;
                        case "writejtag_ir":
                            Console.WriteLine("...........................CMD not Supported");
                            break;
                        case "writejtag_dr":
                            Console.WriteLine("...........................CMD not Supported");
                            break;
                        default:
                            Console.WriteLine("...........................Unkonwned CMD");
                            break;
                    }
                }
            }
            private UInt32 _setupGetVal(string str)
            {
                UInt32 dd;
                if (str.StartsWith("0x") && str.Length >= 3)
                {
                    dd = Convert.ToUInt32(str.Substring(2), 16);
                }
                else
                {
                    dd = Convert.ToUInt32(str);
                }
                return dd;
            }
    
            /// 
            /// ARM内部寄存器
            /// 
            /// 
            public enum ARM_REG : UInt32
            {
                R0,
                R1,
                R2,
                R3,
                R4,
                R5,
                R6,
                R7,
                CPSR,
                R15,
                R8_USR,
                R9_USR,
                R10_USR,
                R11_USR,
                R12_USR,
                R13_USR,
                R14_USR,
                SPSR_FIQ,
                R8_FIQ,
                R9_FIQ,
                R10_FIQ,
                R11_FIQ,
                R12_FIQ,
                R13_FIQ,
                R14_FIQ,
                SPSR_SVC,
                R13_SVC,
                R14_SVC,
                SPSR_ABT,
                R13_ABT,
                R14_ABT,
                SPSR_IRQ,
                R13_IRQ,
                R14_IRQ,
                SPSR_UND,
                R13_UND,
                R14_UND,
                SPSR_SYS,
                R13_SYS,
                R14_SYS,
                PC = 9
            }
    
            /// 
            /// 程序断点模式
            /// 
            /// 
            public enum BP_MODE : UInt32
            {
                ARM = 1,
                THUMB = 2,
                HARD_ARM = 0xffffff01u,
                HARD_THUMB = 0xffffff02u,
                SOFT_ARM = 0xf1u,
                SOFT_THUMB = 0xf2u
            }
    
            /// 
            /// 数据断点模式
            /// 
            /// 
            public enum WP_MODE : UInt32
            {
                READ_WRITE,
                READ,
                WRITE
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358
    • 359
    • 360
    • 361
    • 362
    • 363
    • 364
    • 365
    • 366
    • 367
    • 368
    • 369
    • 370
    • 371
    • 372
    • 373
    • 374
    • 375
    • 376
    • 377
    • 378
    • 379
    • 380
    • 381
    • 382
    • 383
    • 384
    • 385
    • 386
    • 387
    • 388
    • 389
    • 390
    • 391
    • 392
    • 393
    • 394
    • 395
    • 396
    • 397
    • 398
    • 399
    • 400
    • 401
    • 402
    • 403
    • 404
    • 405
    • 406
    • 407
    • 408
    • 409
    • 410
    • 411
    • 412
    • 413
    • 414
    • 415
    • 416
    • 417
    • 418
    • 419
    • 420
    • 421
    • 422
    • 423
    • 424
    • 425
    • 426
    • 427
    • 428
    • 429
    • 430
    • 431
    • 432
    • 433
    • 434
    • 435
    • 436
    • 437
    • 438
    • 439
    • 440
    • 441
    • 442
    • 443
    • 444
    • 445
    • 446
    • 447
    • 448
    • 449
    • 450
    • 451
    • 452
    • 453
    • 454
    • 455
    • 456
    • 457
    • 458
    • 459
    • 460
    • 461
    • 462
    • 463
    • 464
    • 465
    • 466
    • 467
    • 468
    • 469
    • 470
    • 471
    • 472
    • 473
    • 474
    • 475
    • 476
    • 477
    • 478
    • 479
    • 480
    • 481
    • 482
    • 483
    • 484
    • 485
    • 486
    • 487
    • 488
    • 489
    • 490
    • 491
    • 492
    • 493
    • 494
    • 495
    • 496
    • 497
    • 498
    • 499
    • 500
    • 501
    • 502
    • 503
    • 504
    • 505
    • 506
    • 507
    • 508
    • 509
    • 510
    • 511
    • 512
    • 513
    • 514
    • 515
    • 516
    • 517
    • 518
    • 519
    • 520
    • 521
    • 522
    • 523
    • 524
    • 525
    • 526
    • 527
    • 528
    • 529
    • 530
    • 531
    • 532
    • 533
    • 534
    • 535
    • 536
    • 537
    • 538
    • 539
    • 540
    • 541
    • 542
    • 543
    • 544
    • 545
    • 546
    • 547
    • 548
    • 549
    • 550
    • 551
    • 552
    • 553
    • 554
    • 555
    • 556
    • 557
    • 558
    • 559
    • 560
    • 561
    • 562
    • 563
    • 564
    • 565
    • 566
    • 567
    • 568
    • 569
    • 570
    • 571
    • 572
    • 573
    • 574
    • 575
    • 576
    • 577
    • 578
    • 579
    • 580
    • 581
    • 582
    • 583
    • 584
    • 585
    • 586
    • 587
    • 588
    • 589
    • 590
    • 591
    • 592
    • 593
    • 594
    • 595
    • 596
    • 597
    • 598
    • 599
    • 600
    • 601
    • 602
    • 603
    • 604
    • 605
    • 606
    • 607
    • 608
    • 609
    • 610
    • 611
    • 612
    • 613
    • 614
    • 615
    • 616
    • 617
    • 618
    • 619
    • 620
    • 621
    • 622
    • 623
    • 624
    • 625
    • 626
    • 627
    • 628
    • 629
    • 630
    • 631
    • 632
    • 633
    • 634
    • 635
    • 636
    • 637
    • 638
    • 639
    • 640
    • 641
    • 642
    • 643
    • 644
    • 645
    • 646
    • 647
    • 648
    • 649
    • 650
    • 651
    • 652
    • 653
    • 654
    • 655
    • 656
    • 657
    • 658
    • 659
    • 660
    • 661
    • 662
    • 663
    • 664
    • 665
    • 666
    • 667
    • 668
    • 669
    • 670
    • 671
    • 672
    • 673
    • 674
    • 675
    • 676
    • 677
    • 678
    • 679
    • 680
    • 681
    • 682
    • 683
    • 684
    • 685
    • 686
    • 687
    • 688
    • 689
    • 690
    • 691
    • 692
    • 693
    • 694
    • 695
    • 696
    • 697
    • 698
    • 699
    • 700
    • 701
    • 702
    • 703
    • 704
    • 705
    • 706
    • 707
    • 708
    • 709
    • 710
    • 711
    • 712
    • 713
    • 714
    • 715
    • 716
    • 717
    • 718
    • 719
    • 720
    • 721
    • 722
    • 723
    • 724
    • 725
    • 726
    • 727
    • 728
    • 729
    • 730
    • 731
    • 732
    • 733
    • 734
    • 735
    • 736
    • 737
    • 738
    • 739
    • 740
    • 741
    • 742
    • 743
    • 744
    • 745
    • 746
    • 747
    • 748
    • 749
    • 750
    • 751
    • 752
    • 753
    • 754
    • 755
    • 756
    • 757
    • 758
    • 759
    • 760
    • 761
    • 762
    • 763
    • 764
    • 765
    • 766
    • 767
    • 768
    • 769
    • 770
    • 771
    • 772
    • 773
    • 774
    • 775
    • 776
    • 777
    • 778
    • 779
    • 780
    • 781
    • 782
    • 783
    • 784
    • 785
    • 786
    • 787
    • 788
    • 789
    • 790
    • 791
    • 792
    • 793
    • 794
    • 795

    Program.cs

    using Jlink_demo;
    using System;
    using System.Text;
    
    namespace MyApp // Note: actual namespace depends on the project name.
    {
        internal class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Jlink操作演示");
                JLinkHandler.JLINKARM_SetLogFile(System.Text.Encoding.UTF8.GetBytes("this_log.txt"));
                JLinkHandler.JLINKARM_Open();
                var ver = JLinkHandler.JLINKARM_GetDLLVersion();
                Console.WriteLine("DLL 版本: "+ver);
                Console.WriteLine("SN:" + JLinkHandler.JLINKARM_GetSN());
                Console.WriteLine("硬件版本: " + JLinkHandler.JLINKARM_GetHardwareVersion());
                JLinkHandler.JLINKARM_ExecCommand(System.Text.Encoding.UTF8.GetBytes("device = STM32H743VI"), 0, 0);
                JLinkHandler.JLINKARM_TIF_Select(1);
                JLinkHandler.JLINKARM_SetSpeed(4000);
                Console.WriteLine("CPU ID: "+JLinkHandler.JLINKARM_GetId());
                //JLinkHandler.JLINKARM_Connect();
                //JLinkHandler.JLINKARM_Halt();
    
                //RTT control函数传入的控制参数
                const UInt32 JLINKARM_RTTERMINAL_CMD_START = 0;
                const UInt32 JLINKARM_RTTERMINAL_CMD_STOP = 1;
                const UInt32 JLINKARM_RTTERMINAL_CMD_GETDESC = 2;
                const UInt32 JLINKARM_RTTERMINAL_CMD_GETNUMBUF = 3;
                const UInt32 JLINKARM_RTTERMINAL_CMD_GETSTAT = 4;
                //JLinkHandler.JLINK_RTTERMINAL_Control(JLINKARM_RTTERMINAL_CMD_STOP, 0);
                //JLinkHandler.JLINKARM_Sleep(10);
                //JLINK_RTTERMINAL_Control这个函数的传入参数不太确定
                JLinkHandler.JLINK_RTTERMINAL_Control(JLINKARM_RTTERMINAL_CMD_START, 0);
                JLinkHandler.JLINKARM_Sleep(10);
    
                //不确认能否正常使用的接口
    
                //var des = JLinkHandler.JLINK_RTTERMINAL_Control(JLINKARM_RTTERMINAL_CMD_GETDESC, 0);
                //Console.WriteLine($"查找到的描述符:{des:X8}");
                //var num_buf = JLinkHandler.JLINK_RTTERMINAL_Control(JLINKARM_RTTERMINAL_CMD_GETNUMBUF, 0);
                //Console.WriteLine($"缓冲区数量:{num_buf:D4}");
                //var stat = JLinkHandler.JLINK_RTTERMINAL_Control(JLINKARM_RTTERMINAL_CMD_GETSTAT, 0);
                //Console.WriteLine($"状态:{stat:X8}");
                //尝试读取RTT,但是不是每一次都可以成功读取到硬件的RTT调试信息
    
                int num = 500;
                while (num-- >0)
                {
                    Console.Write(JLinkHandler.JLINKARM_ReadRTT_String());//读取RTT接口也是,参数还不确定,有时候可以正常工作,有时候不能
                    JLinkHandler.JLINKARM_Sleep(100);
                }
                Console.WriteLine("结束");
                JLinkHandler.JLINK_RTTERMINAL_Control(JLINKARM_RTTERMINAL_CMD_STOP, 0);
                JLinkHandler.JLINKARM_Close();
    
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59

    最终成果

    我基于上面这些发现写了一个属于我自己的RTT工具,我叫它RTT_ViewerPlusProMax
    链接
    自用的,可能有些许不完善的,但是简单用应该是可以了…

    基本功能如下:
    在这里插入图片描述
    支持的功能大概就这些
    使用时的一些截图~
    在这里插入图片描述
    定时发送:
    在这里插入图片描述

  • 相关阅读:
    Python:>、>>、&、&& 的区别与用法
    【图像分割】距离正则化水平集演化及其在图像分割中的应用(Matlab代码实现)
    尚硅谷设计模式学习(四)工厂模式
    Java Field类简介说明
    荧光探针/近红外荧光/荧光纳米/水凝胶/纳米水凝胶pH荧光探针的研究
    Linux中部署Spring Cloud项目
    五、 计算机网络(考点篇)
    方法的使用
    9.构造器与垃圾收集器 对象的前世今生
    入职字节外包一个月,我离职了
  • 原文地址:https://blog.csdn.net/tpoem/article/details/126473953