JLink SDK API 二次开发 C#版本库

说明

参考:

https://blog.csdn.net/weixin_47124634/article/details/136183404
内容来源于网络,结合UM8002更新了部分函数的签名,增加了一些注释
对于函数的使用定义,建议参考UM8002

主要头文件

using System;
using System.Runtime.InteropServices;
using System.Text;

namespace JLinkARMAPI
{
        /// <summary>
    /// JLink交互类
    /// </summary>
    public static partial class JLink
    {
        /// <summary>
        /// JLink连接信息
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        public struct ConnectInfo
        {
            /// <summary>
            /// This is the serial number
            /// </summary>
            public int SerialNumber;
            
            /// <summary>
            /// Connection type of the J-Link
            /// </summary>
            public uint Connection;
            
            /// <summary>
            /// USB Addr. Default is 0, values of 0..3 are permitted. Only filled if for J-Links connected via USB. For J-Links which are connected via TCP/IP this field is zeroed.
            /// </summary>
            public uint  USBAddr;
            
            /// <summary>
            /// IP Addr. of the connected emulator in case the emulator is connected via IP
            /// </summary>
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
            public byte[] aIPAddr;

            /// <summary>
            /// J-Link via IP only: Time period [ms] after which the UDP discover answer from emulator was received (-1 if emulator is connected over USB)
            /// </summary>
            public int Time;

            /// <summary>
            /// J-Link via IP only: Time period [us] after which the UDP discover answer from emulator was received (-1 if emulator is connected over USB)
            /// </summary>
            public ulong Time_us;

            /// <summary>
            /// J-Link via IP only: Hardware version of JLink
            /// </summary>
            public uint HWVersion;

            /// <summary>
            /// J-Link via IP only: MAC Addr
            /// </summary>
            public byte abMACAddr;

            /// <summary>
            /// J-Link via IP only: Product name
            /// </summary>
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
            public char[] acProduct;

            /// <summary>
            /// J-Link via IP only: Nickname of J-Link
            /// </summary>
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
            public char[] acNickName;

            /// <summary>
            /// J-Link via IP only: Firmware string of JLink
            /// </summary>
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 112)]
            public char[] acFWString;

            /// <summary>
            /// J-Link via IP only: Is J-Link configured for IP address reception via DHCP?
            /// </summary>
            public char IsDHCPAssignedIP;

            /// <summary>
            /// J-Link via IP only
            /// </summary>
            public char IsDHCPAssignedIPIsValid;

            /// <summary>
            /// J-Link via IP only: Number of IP connections which are currently established to this J-Link
            /// </summary>
            public char NumIPConnections;

            /// <summary>
            /// J-Link via IP only
            /// </summary>
            public char NumIPConnectionsIsValid;

            /// <summary>
            /// Dummy bytes to pad the structure size to 264 bytes. Reserved for future use.
            /// </summary>
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 34)]
            public byte[] aPadding;
        }
        
        /// <summary>
        /// 打开JLink设备
        /// </summary>
        /// <remarks>IntPtr.Zero成功,否则返回失败字符串const char *</remarks>
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern IntPtr JLINKARM_Open();

        /// <summary>
        /// 关闭JLink设备
        /// </summary>
        /// <remarks></remarks>
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern void JLINKARM_Close();

        /// <summary>
        /// 连接设备
        /// </summary>
        /// <returns>大于等于0成功 小于0失败,错误代码参考UM8002</returns>
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern int JLINKARM_Connect();

        /// <summary>
        /// 输入RTT命令
        /// </summary>
        /// <param name="cmd">RTT相关命令 参考UM8002</param>
        /// <param name="p">指向命令所需参数的指针 参考UM8002</param>
        /// <returns>大于等于0成功 小于0失败,参考UM8002</returns>
        [DllImport("JLinkARM.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern int JLINK_RTTERMINAL_Control(uint cmd, IntPtr p);

        /// <summary>
        /// 从RTT回读数据
        /// </summary>
        /// <param name="terminalIndex">要读取的终端索引</param>
        /// <param name="receiveBuffer">接收buffer</param>
        /// <param name="receiveBufferSize">接收buffer的大小</param>
        /// <returns>大于等于0成功,表示读取到的字节数 小于0失败,参考UM8002</returns>
        [DllImport("JLinkARM.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
        public static extern int JLINK_RTTERMINAL_Read(int terminalIndex, [Out(), MarshalAs(UnmanagedType.LPArray)] byte[] receiveBuffer, uint receiveBufferSize);

        /// <summary>
        /// 写数据到RTT
        /// </summary>
        /// <param name="terminalIndex">要读取的终端索引</param>
        /// <param name="transmitBuffer">发送buffer</param>
        /// <param name="transmitBufferSize">发送bufeer的大小</param>
        /// <returns>大于等于0成功,表示发送的字节数 小于0失败</returns>
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern int JLINK_RTTERMINAL_Write(int terminalIndex, [In(), MarshalAs(UnmanagedType.LPArray)] byte[] transmitBuffer, uint transmitBufferSize);

        /// <summary>
        /// 系统复位并停止CPU
        /// </summary>
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern void JLINKARM_Reset();

        /// <summary>
        /// 系统复位但不停止CPU
        /// </summary>
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern void JLINKARM_ResetNoHalt();

        /// <summary>
        /// 参考UM8002
        /// </summary>
        /// <param name="numinsts"></param>
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern void JLINKARM_GoAllowSim(uint numinsts);

        /// <summary>
        /// 执行程序
        /// </summary>
        /// <remarks></remarks>
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern void JLINKARM_Go();

        /// <summary>
        /// 中断程序执行
        /// </summary>
        /// <returns>0成功 1失败</returns>
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern byte JLINKARM_Halt();

        /// <summary>
        /// 单步执行
        /// </summary>
        /// <returns>0成功 1失败</returns>
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern byte JLINKARM_Step();

        /// <summary>
        /// 清除错误信息
        /// </summary>
        /// <remarks></remarks>
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern void JLINKARM_ClrError();

        /// <summary>
        /// 设置JLink接口速度
        /// </summary>
        /// <param name="speed">速率 单位kHz</param>
        /// <remarks>0为自动调整</remarks>
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern void JLINKARM_SetSpeed(uint speed);

        /// <summary>
        /// 设置JTAG为最高速度
        /// </summary>
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern void JLINKARM_SetMaxSpeed();

        /// <summary>
        /// 获取JLink当前速率,单位kHz
        /// </summary>
        /// <returns></returns>
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern ushort JLINKARM_GetSpeed();

        /// <summary>
        /// 当前MCU是否处于停止状态
        /// </summary>
        /// <returns>1已停止 0未停止 小于0失败</returns>
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern sbyte JLINKARM_IsHalted();

        /// <summary>
        /// 是否已连接
        /// </summary>
        /// <returns>1已连接 0未连接</returns>
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern byte JLINKARM_IsConnected();

        /// <summary>
        /// JLink是否已经可以操作了
        /// </summary>
        /// <returns>1已启动 0未启动</returns>
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern byte JLINKARM_IsOpen();

        /// <summary>
        /// 取消程序断点
        /// </summary>
        /// <param name="BPIndex">断点序号</param>
        /// <remarks>配合JLINKARM_SetBP()使用</remarks>
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern void JLINKARM_ClrBP(uint BPIndex);

        /// <summary>
        /// 设置程序断点
        /// </summary>
        /// <param name="BPIndex">断点序号</param>
        /// <param name="addr">目标地址</param>
        /// <remarks>建议使用JLINKARM_SetBPEx()替代</remarks>
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern void JLINKARM_SetBP(uint BPIndex, uint addr);

        /// <summary>
        /// 设置程序断点
        /// </summary>
        /// <param name="addr">目标地址</param>
        /// <param name="mode">断点类型</param>
        /// <returns>Handle,提供给JLINKARM_ClrBPEx()使用</returns>
        /// <returns>大于0,表示断点句柄 0失败</returns>
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern int JLINKARM_SetBPEx(uint addr, BP_MODE mode);

        /// <summary>
        /// 取消程序断点
        /// </summary>
        /// <param name="BPHandle"></param>
        /// <remarks>配合JLINKARM_SetBPEx()使用</remarks>
        /// <returns>0成功 1失败</returns>
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern int JLINKARM_ClrBPEx(int BPHandle);

        /// <summary>
        /// UM8002说该函数已弃用
        /// </summary>
        /// <param name="addr"></param>
        /// <param name="addrmark"></param>
        /// <param name="dat"></param>
        /// <param name="datmark"></param>
        /// <param name="ctrl"></param>
        /// <param name="ctrlmark"></param>
        /// <returns></returns>
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        private static extern int JLINKARM_SetWP(uint addr, uint addrmark, uint dat, uint datmark, byte ctrl, byte ctrlmark);

        /// <summary>
        /// UM8002说该函数已弃用
        /// </summary>
        /// <param name="handle"></param>
        /// <remarks>配合JLINKARM_SetWP()使用</remarks>
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern void JLINKARM_ClrWP(int handle);

        /// <summary>
        /// 设置寄存器
        /// </summary>
        /// <param name="regIndex"></param>
        /// <param name="data"></param>
        /// <returns>0成功 1失败</returns>
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern byte JLINKARM_WriteReg(ARM_REG regIndex, uint data);

        /// <summary>
        /// 读取寄存器
        /// </summary>
        /// <param name="regIndex"></param>
        /// <returns>寄存器值</returns>
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern uint JLINKARM_ReadReg(ARM_REG regIndex);

        /// <summary>
        /// 写入一段数据
        /// </summary>
        /// <param name="addr">地址</param>
        /// <param name="size">写入的大小</param>
        /// <param name="data">指向要写入的数据</param>
        /// <remarks>小于0失败 否则返回成功写入的字节数</remarks>
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern int JLINKARM_WriteMem(uint addr, uint size, byte[] data);

        /// <summary>
        /// 读取一段数据
        /// </summary>
        /// <param name="addr">地址</param>
        /// <param name="size">读取的大小</param>
        /// <param name="buffer">缓冲区</param>
        /// <remarks>0成功 1失败</remarks>
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern int JLINKARM_ReadMem(uint addr, uint size, [Out(), MarshalAs(UnmanagedType.LPArray)] byte[] buffer);

        /// <summary>
        /// 从调试通道获取一串数据
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="size">需要获取的数据长度</param>
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern void JLINKARM_ReadDCCFast([Out(), MarshalAs(UnmanagedType.LPArray)] uint[] buffer, uint size);

        /// <summary>
        /// 从调试通道获取一串数据
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="size">希望获取的数据长度</param>
        /// <param name="timeout"></param>
        /// <returns>实际获取的数据长度</returns>
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern int JLINKARM_ReadDCC([Out(), MarshalAs(UnmanagedType.LPArray)] uint[] buffer, uint size, int timeout);

        /// <summary>
        /// 向调试通道写入一串数据
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="size">需要写入的数据长度</param>
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern void JLINKARM_WriteDCCFast(uint[] buffer, uint size);

        /// <summary>
        /// 向调试通道写入一串数据
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="size">希望写入的数据长度</param>
        /// <param name="timeout"></param>
        /// <returns>实际写入的数据长度</returns>
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern int JLINKARM_WriteDCC(uint[] buffer, uint size, int timeout);

        /// <summary>
        /// 获取JLINK的DLL版本号
        /// </summary>
        /// <returns></returns>
        /// <remarks>使用10进制数表示</remarks>
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern uint JLINKARM_GetDLLVersion();

        /// <summary>
        /// 执行命令
        /// </summary>
        /// <param name="commandBytes">命令转UTF8后字节</param>
        /// <param name="oBuffer">用来保存命令执行返回内容的缓冲区指针</param>
        /// <param name="bufferSize">缓冲区大小</param>
        /// <returns>命令的返回值</returns>
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern int JLINKARM_ExecCommand([In(), MarshalAs(UnmanagedType.LPArray)] byte[] commandBytes, [Out(), MarshalAs(UnmanagedType.LPArray)] byte[] oBuffer, int bufferSize);

        /// <summary>
        /// 选择接口,0是JTAG 1是SWD
        /// </summary>
        /// <param name="type">0是JTAG 1是SWD</param>
        /// <returns>返回上一个选择的TIF</returns>
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern int JLINKARM_TIF_Select(int type);

        /// <summary>
        /// 擦除芯片
        /// </summary>
        /// <returns>大于等于0成功 小于0失败</returns>
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern int JLINK_EraseChip();

        /// <summary>
        /// 获取JLINK的固件版本号
        /// </summary>
        /// <returns></returns>
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern uint JLINKARM_GetHardwareVersion();

        /// <summary>
        /// 获得支持的特性描述
        /// </summary>
        /// <param name="oBuffer"></param>
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern void JLINKARM_GetFeatureString([Out(), MarshalAs(UnmanagedType.LPArray)] byte[] oBuffer);

        /// <summary>
        /// 获得EMO字符串
        /// </summary>
        /// <param name="oBuffer"></param>
        [DllImport("JLinkARM.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern void JLINKARM_GetOEMString([Out(), MarshalAs(UnmanagedType.LPArray)] byte[] oBuffer);

        /// <summary>
        /// 设置日志文件
        /// </summary>
        /// <param name="oBuffer"></param>
        [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);

        /// <summary>
        /// 返回编译时间
        /// </summary>
        /// <returns></returns>
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern StringBuilder JLINKARM_GetCompileDateTime();

        /// <summary>
        /// 读取JLink序列号
        /// </summary>
        /// <returns>大于等于0成功,表示序列号 小于0失败,参考UM8002</returns>
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern int JLINKARM_GetSN();

        /// <summary>
        /// 获取当前MCU的ID号
        /// </summary>
        /// <returns></returns>
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern uint JLINKARM_GetId();

        /// <summary>
        /// 读取32位数据
        /// </summary>
        /// <param name="addr"></param>
        /// <param name="leng"></param>
        /// <param name="buffer"></param>
        /// <param name="status"></param>
        /// <returns>大于等于0成功,表示读取数量 小于0失败</returns>
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        private static extern int JLINKARM_ReadMemU32(uint addr, uint leng, ref uint buffer, ref byte status);

        /// <summary>
        /// 写入32位的数据
        /// </summary>
        /// <param name="addr"></param>
        /// <param name="data"></param>
        /// <remarks>等于0成功 不等于0失败</remarks>
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern int JLINKARM_WriteU32(uint addr, uint data);

        /// <summary>
        /// 读取16位数据
        /// </summary>
        /// <param name="addr"></param>
        /// <param name="leng"></param>
        /// <param name="buffer"></param>
        /// <param name="status"></param>
        /// <returns>大于等于0成功,表示读取数量 小于0失败</returns>
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern int JLINKARM_ReadMemU16(uint addr, uint leng, ref ushort buffer, ref byte status);

        /// <summary>
        /// 写入16位的数据
        /// </summary>
        /// <param name="addr"></param>
        /// <param name="data"></param>
        /// <remarks>等于0成功 不等于0失败</remarks>
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern int JLINKARM_WriteU16(uint addr, ushort data);

        /// <summary>
        /// 读取8位数据
        /// </summary>
        /// <param name="addr"></param>
        /// <param name="leng"></param>
        /// <param name="buffer"></param>
        /// <param name="status"></param>
        /// <returns>大于等于0成功,表示读取数量 小于0失败</returns>
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern int JLINKARM_ReadMemU8(uint addr, uint leng, ref byte buffer, ref byte status);

        /// <summary>
        /// 写入8位的数据
        /// </summary>
        /// <param name="addr"></param>
        /// <param name="data"></param>
        /// <remarks>等于0成功 不等于0失败</remarks>
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern int JLINKARM_WriteU8(uint addr, byte data);

        /// <summary>
        /// 
        /// </summary>
        /// <param name="filepath">文件路径</param>
        /// <param name="addr">下载地址</param>
        /// <returns>
        /// <returns>大于等于0成功 小于0失败,表示错误代码</returns></returns>
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern int JLINK_DownloadFile([In][MarshalAs(UnmanagedType.LPArray)] byte[] filepath, uint addr);
        
        /// <summary>
        /// 获取仿真器列表
        /// </summary>
        /// <param name="connectIF">1=USB 2=IP</param>
        /// <param name="paConnectInfo">指向缓存区的指针(一个ConnectInfo占用264byte)</param>
        /// <param name="maxInfos">返回值最大数量</param>
        /// <returns>找到的设备数量</returns>
        [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        private static extern int JLINKARM_EMU_GetList(int connectIF, IntPtr paConnectInfo, int maxInfos);


        

#pragma warning disable 1591
        /// <summary>
        /// ARM内部寄存器
        /// </summary>
        /// <remarks></remarks>
        public enum ARM_REG : uint
        {
            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
        }

        /// <summary>
        /// 程序断点模式
        /// </summary>
        /// <remarks></remarks>
        public enum BP_MODE : uint
        {
            ARM = 1,
            THUMB = 2,
            HARD_ARM = 0xffffff01u,
            HARD_THUMB = 0xffffff02u,
            SOFT_ARM = 0xf1u,
            SOFT_THUMB = 0xf2u
        }

        /// <summary>
        /// 数据断点模式
        /// </summary>
        /// <remarks></remarks>
        public enum WP_MODE : uint
        {
            READ_WRITE,
            READ,
            WRITE
        }
#pragma warning restore 1591
    }
}

其它包装

using System.Text;

namespace JLinkARMAPI
{
    // 在这里自定义一些包装方法
    public static partial class JLink
    {
        /// <summary>
        /// 获得SDK版本字符串
        /// </summary>
        /// <returns></returns>
        public static string GetSDKVersionString()
        {
            uint version = JLINKARM_GetDLLVersion();
            uint major = version / 10000;
            uint minor = version % 10000 / 100;
            uint revision = version % 100;
            return $"{major}.{minor}.{revision}";
        }

        /// <summary>
        /// 获取JLink硬件版本字符串
        /// </summary>
        /// <returns></returns>
        public static string GetHardwareVersionString()
        {
            uint version = JLINKARM_GetHardwareVersion();
            uint major = version / 10000;
            uint minor = version % 10000;
            return $"{major}.{minor:00}";
        }

        /// <summary>
        /// 执行命令并返回结果,命令可用命令参考UM8001
        /// </summary>
        /// <param name="command"></param>
        /// <param name="result"></param>
        /// <returns>命令返回的值</returns>
        public static int ExecCommand(string command, out string result)
        {
            const int bufferSize = 1024;
            byte[] buffer = new byte[bufferSize];
            int commandReturnCode = JLINKARM_ExecCommand(Encoding.UTF8.GetBytes(command), buffer, bufferSize);
            result = Encoding.UTF8.GetString(buffer);
            if(result.IndexOf('\0') != -1) result = result.Substring(0, result.IndexOf('\0'));
            return commandReturnCode;
        }

        /// <summary>
        /// 写入一段数据
        /// </summary>
        /// <param name="addr">地址</param>
        /// <param name="data">指向要写入的数据</param>
        /// <remarks>小于0失败 否则返回成功写入的字节数</remarks>
        public static int JLINKARM_WriteMem(uint addr, byte[] data)
        {
            return JLINKARM_WriteMem(addr, (uint)data.Length, data);
        }

        /// <summary>
        /// 读取一段数据
        /// </summary>
        /// <param name="addr">地址</param>
        /// <param name="size">读取的大小</param>
        /// <param name="data">返回内容</param>
        /// <remarks>0成功 1失败</remarks>
        public static int JLINKARM_ReadMem(uint addr, uint size, out byte[] data)
        {
            data = new byte[size];
            return JLINKARM_ReadMem(addr, size, data);
        }

        /// <summary>
        /// 获取USB连接的J-Link设备列表
        /// </summary>
        /// <param name="maxInfos">最大获取数量</param>
        /// <returns></returns>
        public static IEnumerable<ConnectInfo> GetUSBEmulatorList(int maxInfos)
        {
            var result = new List<ConnectInfo>();
            // 申请内存空间
            int size = Marshal.SizeOf(typeof(ConnectInfo));
            IntPtr paConnectInfo = Marshal.AllocHGlobal(size * maxInfos);
            int deviceNum = JLINKARM_EMU_GetList(1, paConnectInfo, maxInfos);
            // 扫描到设备
            if (deviceNum > 0)
            {
                for (int i = 0; i < deviceNum; i++) 
                    result.Add((ConnectInfo)Marshal.PtrToStructure((paConnectInfo + size * i), typeof(ConnectInfo)) );
            }
            // 记得要释放
            Marshal.FreeHGlobal(paConnectInfo);
            return result;
        }
    }
}

来源链接:https://www.cnblogs.com/209jkjkjk/p/18619835

© 版权声明
THE END
支持一下吧
点赞5 分享
评论 抢沙发
头像
请文明发言!
提交
头像

昵称

取消
昵称表情代码快捷回复

    暂无评论内容