当前位置:   article > 正文

自己开发jlink烧录软件,烧录上位机, C#调用JLinkARM.dll实现软件烧录_c# jlink ob

c# jlink ob

最近需要写一个烧录软件,让工厂可以实现一键烧录。

1.命令行烧录

这里实现了运行一个bat文件进行一键烧录,不想看这部分的可以直接跳到第二部分,不受影响。

由于jlink的自带烧录软件JFlash.exe操作十分繁杂,需要自己开发一个简洁的烧录方法,一番操作发现,可以使用J-Link Commander 的命令行进行烧录,操作流程如下:

既然可以用这个命令行,那就可以用bat文件来执行,此间需要生成一个.jlink文件供命令行执行,总共需要两个文件,我这里是burn_hex.bat 和 burn_script.jlink,具体内容如下:

burn_hex.bat:

  1. @echo off
  2. set JLINK_PATH="C:\Program Files (x86)\SEGGER\JLink\JLink.exe"
  3. set HEX_FILE_PATH="Programmer.hex"
  4. set CHIP_MODEL=NRF52832
  5. %JLINK_PATH% -device %CHIP_MODEL% -if SWD -speed 4000 -CommanderScript burn_script.jlink

burn_script.jlink:

  1. connect
  2. device NRF52832_XXAA
  3. speed 4000
  4. TIF SWD
  5. loadfile D:\Programmer.hex
  6. r
  7. g
  8. q

两个 文件放在同一目录下,将hex路径设置正确,点击bat文件即可进行烧录。

命令行衍生上位机

既然可以按如上的方式烧录,那用上位机生成*.jlink文件再执行bat即可实现上位机烧录,但是这种方式不够灵活。

2.C#调用JLinkARM.dll烧录

通过调查发现,jlink的烧录都有调用JLinkARM.dll里面的接口,那开发者也可以调用,但是SEGGER并未开源,需要花钱购买全部的接口。好在有大佬成功逆向了部分接口,原帖:c#的 JLINK API,自己用的 (amobbs.com 阿莫电子技术论坛)

稍全的接口如下(C++):

  1. //arm.h,裡面的API list 如下
  2. /*********************************************************************
  3. *
  4. * API functions
  5. */
  6. void JLINKARM_Close(void);
  7. void JLINKARM_ClrBP(unsigned BPIndex);
  8. void JLINKARM_ClrError(void);
  9. void JLINKARM_EnableLog2File(void);
  10. const char * JLINKARM_GetCompileDateTime(void);
  11. U16 JLINKARM_GetEmbeddedFWVersion(void);
  12. void JLINKARM_GetHWStatus(JTAG_HW_STATUS * pStat);
  13. U32 JLINKARM_GetId(void);
  14. void JLINKARM_GetIdData(JTAG_ID_DATA * pIdData);
  15. U16 JLINKARM_GetSelDevice(void);
  16. int JLINKARM_GetVoltage(void);
  17. U16 JLINKARM_GetSpeed(void);
  18. void JLINKARM_Go(void);
  19. void JLINKARM_GoIntDis(void);
  20. char JLINKARM_Halt(void);
  21. char JLINKARM_HaltNoSave(void);
  22. char JLINKARM_IsConnected(void);
  23. char JLINKARM_IsHalted(void);
  24. const char * JLINKARM_Open(void);
  25. int JLINKARM_ReadDCC(U32 * pData, U32 NumItems, int TimeOut);
  26. void JLINKARM_ReadDCCFast(U32 * pData, U32 NumItems);
  27. U32 JLINKARM_ReadICEReg(int RegIndex);
  28. int JLINKARM_ReadMem (U32 addr, U32 count, void * p);
  29. void JLINKARM_ReadMemU8 (U32 Addr, U32 NumItems, U8 * pData, U8* pStatus);
  30. void JLINKARM_ReadMemU16(U32 Addr, U32 NumItems, U16* pData, U8* pStatus);
  31. void JLINKARM_ReadMemU32(U32 Addr, U32 NumItems, U32* pData, U8* pStatus);
  32. U32 JLINKARM_ReadReg (ARM_REG RegIndex);
  33. void JLINKARM_Reset(void);
  34. void JLINKARM_ResetPullsTRST (U8 OnOff);
  35. void JLINKARM_ResetPullsRESET(U8 OnOff);
  36. void JLINKARM_SelDevice(U16 DeviceIndex);
  37. void JLINKARM_SetBP(unsigned BPIndex, U32 Addr);
  38. int JLINKARM_SetEndian(int v);
  39. int JLINKARM_SetInitRegsOnReset(int v);
  40. void JLINKARM_SetMaxSpeed(void);
  41. void JLINKARM_SetResetDelay(int ms);
  42. int JLINKARM_SetResetPara(int Value);
  43. void JLINKARM_SetSpeed(int Speed);
  44. char JLINKARM_Step(void);
  45. int JLINKARM_Test(void);
  46. U16 JLINKARM_UpdateFirmware(void);
  47. U32 JLINKARM_UpdateFirmwareIfNewer(void);
  48. int JLINKARM_WaitDCCRead(int TimeOut);
  49. int JLINKARM_WriteDCC(const U32 * pData, U32 NumItems, int TimeOut);
  50. void JLINKARM_WriteDCCFast(const U32 * pData, U32 NumItems);
  51. void JLINKARM_WriteICEReg(int RegIndex, U32 Value, int AllowDelay);
  52. char JLINKARM_WriteReg(ARM_REG RegIndex, U32 Data);
  53. void JLINKARM_WriteMem(U32 addr, U32 count, const void * p);
  54. void JLINKARM_WriteMemDelayed(U32 Addr, U32 Count, const void * p);
  55. void JLINKARM_WriteU8 (U32 addr, U8 Data);
  56. void JLINKARM_WriteU16(U32 addr, U16 Data);
  57. void JLINKARM_WriteU32(U32 addr, U32 Data);
  58. void JLINKARM_EnableLogCom(void (*DebugFunc)(const char *));

 C#接口如下:

  1. /// <summary>
  2. /// 打开JLINK设备
  3. /// </summary>
  4. /// <remarks></remarks>
  5. [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  6. public static extern void JLINKARM_Open();
  7. /// <summary>
  8. /// 关闭JLINK设备
  9. /// </summary>
  10. /// <remarks></remarks>
  11. [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  12. public static extern void JLINKARM_Close();
  13. /// <summary>
  14. /// 连接设备
  15. /// </summary>
  16. [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  17. public static extern void JLINKARM_Connect();
  18. /// <summary>
  19. /// 开启RTT
  20. /// </summary>
  21. /// <param name="terminal"></param>
  22. /// <param name="size"></param>
  23. [DllImport("JLinkARM.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  24. public static extern UInt32 JLINK_RTTERMINAL_Control(UInt32 CMD, UInt32 size);
  25. /// <summary>
  26. /// 从RTT回读数据
  27. /// </summary>
  28. /// <param name="terminal"></param>
  29. /// <param name="rxBuffer"></param>
  30. /// <param name="len"></param>
  31. [DllImport("JLinkARM.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
  32. public static extern void JLINK_RTTERMINAL_Read(int terminal, [Out(), MarshalAs(UnmanagedType.LPArray)] byte[] rxBuffer, UInt32 size);
  33. static string gbk_ansi(string str)
  34. {
  35. string keyword;
  36. byte[] buffer = Encoding.GetEncoding("GB2312").GetBytes(str);
  37. keyword = Encoding.UTF8.GetString(buffer);
  38. return keyword;
  39. }
  40. public static string JLINKARM_ReadRTT_String()
  41. {
  42. try
  43. {
  44. byte[] aa = new byte[1000];
  45. JLINK_RTTERMINAL_Read(0, aa,1000);
  46. ASCIIEncoding kk = new ASCIIEncoding();
  47. //使用UTF-8编码
  48. UTF8Encoding uu = new UTF8Encoding();
  49. string ss = uu.GetString(aa);
  50. if(ss.Length>1)
  51. {
  52. Console.Write(ss);
  53. }
  54. return ss;
  55. }
  56. catch(Exception ex)
  57. {
  58. }
  59. return String.Empty;
  60. }
  61. /// <summary>
  62. /// 写数据到RTT
  63. /// </summary>
  64. /// <param name="terminal"></param>
  65. /// <param name="txBuffer"></param>
  66. /// <param name="len"></param>
  67. [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  68. public static extern void JLINK_RTTERMINAL_Write(int terminal, [In(), MarshalAs(UnmanagedType.LPArray)] byte[] txBuffer, UInt32 size);
  69. /// <summary>
  70. /// 系统复位
  71. /// </summary>
  72. /// <remarks></remarks>
  73. [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  74. public static extern void JLINKARM_Reset();
  75. [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  76. public static extern void JLINKARM_GoAllowSim();
  77. /// <summary>
  78. /// 执行程序
  79. /// </summary>
  80. /// <remarks></remarks>
  81. [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  82. public static extern void JLINKARM_Go();
  83. /// <summary>
  84. /// 中断程序执行
  85. /// </summary>
  86. /// <returns></returns>
  87. /// <remarks></remarks>
  88. [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  89. public static extern bool JLINKARM_Halt();
  90. /// <summary>
  91. /// 单步执行
  92. /// </summary>
  93. /// <returns></returns>
  94. /// <remarks></remarks>
  95. [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  96. public static extern bool JLINKARM_Step();
  97. /// <summary>
  98. /// 清除错误信息
  99. /// </summary>
  100. /// <remarks></remarks>
  101. [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  102. public static extern void JLINKARM_ClrError();
  103. /// <summary>
  104. /// 设置JLINK接口速度
  105. /// </summary>
  106. /// <param name="speed"></param>
  107. /// <remarks>0为自动调整</remarks>
  108. [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  109. public static extern void JLINKARM_SetSpeed(int speed);
  110. /// <summary>
  111. /// 设置JTAG为最高速度
  112. /// </summary>
  113. /// <remarks></remarks>
  114. [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  115. public static extern void JLINKARM_SetMaxSpeed()
  116. ;
  117. [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  118. public static extern UInt16 JLINKARM_GetSpeed()
  119. ;
  120. [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  121. public static extern UInt32 JLINKARM_GetVoltage()
  122. ;
  123. /// <summary>
  124. /// 当前MCU是否处于停止状态
  125. /// </summary>
  126. /// <returns></returns>
  127. /// <remarks></remarks>
  128. [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  129. public static extern bool JLINKARM_IsHalted()
  130. ;
  131. [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  132. public static extern bool JLINKARM_IsConnected()
  133. ;
  134. /// <summary>
  135. /// JLINK是否已经可以操作了
  136. /// </summary>
  137. /// <returns></returns>
  138. /// <remarks></remarks>
  139. [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  140. public static extern bool JLINKARM_IsOpen()
  141. ;
  142. /// <summary>
  143. /// 取消程序断点
  144. /// </summary>
  145. /// <param name="index">断点序号</param>
  146. /// <remarks>配合JLINKARM_SetBP()使用</remarks>
  147. [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  148. public static extern void JLINKARM_ClrBP(UInt32 index)
  149. ;
  150. /// <summary>
  151. /// 设置程序断点
  152. /// </summary>
  153. /// <param name="index">断点序号</param>
  154. /// <param name="addr">目标地址</param>
  155. /// <remarks>建议使用JLINKARM_SetBPEx()替代</remarks>
  156. [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  157. public static extern void JLINKARM_SetBP(UInt32 index, UInt32 addr)
  158. ;
  159. /// <summary>
  160. /// 设置程序断点
  161. /// </summary>
  162. /// <param name="addr">目标地址</param>
  163. /// <param name="mode">断点类型</param>
  164. /// <returns>Handle,提供给JLINKARM_ClrBPEx()使用</returns>
  165. /// <remarks></remarks>
  166. [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  167. public static extern int JLINKARM_SetBPEx(UInt32 addr, BP_MODE mode)
  168. ;
  169. /// <summary>
  170. /// 取消程序断点
  171. /// </summary>
  172. /// <param name="handle"></param>
  173. /// <remarks>配合JLINKARM_SetBPEx()使用</remarks>
  174. [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  175. public static extern void JLINKARM_ClrBPEx(int handle)
  176. ;
  177. [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  178. private static extern int JLINKARM_SetWP(UInt32 addr, UInt32 addrmark, UInt32 dat, UInt32 datmark, byte ctrl, byte ctrlmark)
  179. ;
  180. /// <summary>
  181. /// 取消数据断点
  182. /// </summary>
  183. /// <param name="handle"></param>
  184. /// <remarks>配合JLINKARM_SetWP()使用</remarks>
  185. [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  186. public static extern void JLINKARM_ClrWP(int handle)
  187. ;
  188. /// <summary>
  189. /// 设置寄存器
  190. /// </summary>
  191. /// <param name="index"></param>
  192. /// <param name="dat"></param>
  193. /// <remarks></remarks>
  194. [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  195. public static extern void JLINKARM_WriteReg(ARM_REG index, UInt32 dat)
  196. ;
  197. /// <summary>
  198. /// 读取寄存器
  199. /// </summary>
  200. /// <param name="index"></param>
  201. /// <returns></returns>
  202. /// <remarks></remarks>
  203. [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  204. public static extern UInt32 JLINKARM_ReadReg(ARM_REG index)
  205. ;
  206. /// <summary>
  207. /// 写入一段数据
  208. /// </summary>
  209. /// <param name="addr"></param>
  210. /// <param name="size"></param>
  211. /// <param name="buf"></param>
  212. /// <remarks></remarks>
  213. [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  214. public static extern void JLINKARM_WriteMem(UInt32 addr, UInt32 size, byte[] buf)
  215. ;
  216. /// <summary>
  217. /// 读取一段数据
  218. /// </summary>
  219. /// <param name="addr"></param>
  220. /// <param name="size"></param>
  221. /// <param name="buf"></param>
  222. /// <remarks></remarks>
  223. [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  224. public static extern void JLINKARM_ReadMem(UInt32 addr, UInt32 size, [Out(), MarshalAs(UnmanagedType.LPArray)] byte[] buf)
  225. ;
  226. /// <summary>
  227. /// 从调试通道获取一串数据
  228. /// </summary>
  229. /// <param name="buf"></param>
  230. /// <param name="size">需要获取的数据长度</param>
  231. /// <remarks></remarks>
  232. [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  233. public static extern void JLINKARM_ReadDCCFast([Out(), MarshalAs(UnmanagedType.LPArray)] UInt32[] buf, UInt32 size)
  234. ;
  235. /// <summary>
  236. /// 从调试通道获取一串数据
  237. /// </summary>
  238. /// <param name="buf"></param>
  239. /// <param name="size">希望获取的数据长度</param>
  240. /// <param name="timeout"></param>
  241. /// <returns>实际获取的数据长度</returns>
  242. /// <remarks></remarks>
  243. [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  244. public static extern UInt32 JLINKARM_ReadDCC([Out(), MarshalAs(UnmanagedType.LPArray)] UInt32[] buf, UInt32 size, int timeout)
  245. ;
  246. /// <summary>
  247. /// 向调试通道写入一串数据
  248. /// </summary>
  249. /// <param name="buf"></param>
  250. /// <param name="size">需要写入的数据长度</param>
  251. /// <remarks></remarks>
  252. [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  253. public static extern void JLINKARM_WriteDCCFast(UInt32[] buf, UInt32 size)
  254. ;
  255. /// <summary>
  256. /// 向调试通道写入一串数据
  257. /// </summary>
  258. /// <param name="buf"></param>
  259. /// <param name="size">希望写入的数据长度</param>
  260. /// <param name="timeout"></param>
  261. /// <returns>实际写入的数据长度</returns>
  262. /// <remarks></remarks>
  263. [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  264. public static extern UInt32 JLINKARM_WriteDCC(UInt32[] buf, UInt32 size, int timeout)
  265. ;
  266. /// <summary>
  267. /// 获取JLINK的DLL版本号
  268. /// </summary>
  269. /// <returns></returns>
  270. /// <remarks>使用10进制数表示</remarks>
  271. [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  272. public static extern UInt32 JLINKARM_GetDLLVersion()
  273. ;
  274. /// <summary>
  275. /// 执行命令
  276. /// </summary>
  277. /// <param name="oBuffer"></param>
  278. /// <param name="a"></param>
  279. /// <param name="b"></param>
  280. [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  281. public static extern void JLINKARM_ExecCommand([In(), MarshalAs(UnmanagedType.LPArray)] byte[] oBuffer,int a,int b);
  282. /// <summary>
  283. /// 选择接口,0是JTAG 1是SWD
  284. /// </summary>
  285. /// <param name="type"></param>
  286. [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  287. public static extern void JLINKARM_TIF_Select(int type);
  288. /// <summary>
  289. /// 获取JLINK的固件版本号
  290. /// </summary>
  291. /// <returns></returns>
  292. /// <remarks></remarks>
  293. [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  294. public static extern UInt32 JLINKARM_GetHardwareVersion()
  295. ;
  296. [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  297. private static extern void JLINKARM_GetFeatureString([Out(), MarshalAs(UnmanagedType.LPArray)] byte[] oBuffer)
  298. ;
  299. [DllImport("JLinkARM.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  300. private static extern void JLINKARM_GetOEMString([Out(), MarshalAs(UnmanagedType.LPArray)] byte[] oBuffer)
  301. ;
  302. [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  303. public static extern void JLINKARM_SetLogFile([In(), MarshalAs(UnmanagedType.LPArray)] byte[] oBuffer)
  304. ;
  305. [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  306. public static extern StringBuilder JLINKARM_GetCompileDateTime()
  307. ;
  308. [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  309. public static extern UInt32 JLINKARM_GetSN()
  310. ;
  311. /// <summary>
  312. /// 获取当前MCU的ID号
  313. /// </summary>
  314. /// <returns></returns>
  315. /// <remarks></remarks>
  316. [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  317. public static extern UInt32 JLINKARM_GetId()
  318. ;
  319. [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  320. private static extern void JLINKARM_ReadMemU32(UInt32 addr, UInt32 leng, ref UInt32 buf, ref byte status)
  321. ;
  322. /// <summary>
  323. /// 写入32位的数据
  324. /// </summary>
  325. /// <param name="addr"></param>
  326. /// <param name="dat"></param>
  327. /// <remarks></remarks>
  328. [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  329. public static extern void JLINKARM_WriteU32(UInt32 addr, UInt32 dat)
  330. ;
  331. [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  332. private static extern void JLINKARM_ReadMemU16(UInt32 addr, UInt32 leng, ref UInt16 buf, ref byte status)
  333. ;
  334. /// <summary>
  335. /// 写入16位的数据
  336. /// </summary>
  337. /// <param name="addr"></param>
  338. /// <param name="dat"></param>
  339. /// <remarks></remarks>
  340. [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  341. public static extern void JLINKARM_WriteU16(UInt32 addr, UInt16 dat)
  342. ;
  343. [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  344. private static extern void JLINKARM_ReadMemU8(UInt32 addr, UInt32 leng, ref byte buf, ref byte status)
  345. ;
  346. /// <summary>
  347. /// 写入8位的数据
  348. /// </summary>
  349. /// <param name="addr"></param>
  350. /// <param name="dat"></param>
  351. /// <remarks></remarks>
  352. [DllImport("JLinkARM.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  353. public static extern void JLINKARM_WriteU8(UInt32 addr, byte dat)
  354. ;

有了这个接口便可以调用连接jlink了,例如:

  1. Console.WriteLine("Jlink操作演示");
  2. JLinkHandler.JLINKARM_SetLogFile(System.Text.Encoding.UTF8.GetBytes("this_log.txt"));
  3. JLinkHandler.JLINKARM_Open();
  4. var ver = JLinkHandler.JLINKARM_GetDLLVersion();
  5. Console.WriteLine("DLL 版本: "+ver);
  6. Console.WriteLine("SN:" + JLinkHandler.JLINKARM_GetSN());
  7. Console.WriteLine("硬件版本: " + JLinkHandler.JLINKARM_GetHardwareVersion());
  8. JLinkHandler.JLINKARM_ExecCommand(System.Text.Encoding.UTF8.GetBytes("device = STM32H743VI"), 0, 0);
  9. JLinkHandler.JLINKARM_TIF_Select(1);
  10. JLinkHandler.JLINKARM_SetSpeed(4000);
  11. Console.WriteLine("CPU ID: "+JLinkHandler.JLINKARM_GetId());

可惜的是,并没有直接下载hex文件的接口,这就需要先了解hex文件的结构,进行进一步解析,再调用接口写到对应的地址下。这里有参考文章:

STM32的烧录和Hex/bin烧录文件解析、烧录文件是被如何存储到MCU中的?_stm32 bin文件解析-CSDN博客

hex文件内含有地址和数据,软件解析地址并烧录数据,烧录完毕之后,调用:JLINKARM_Go();,程序成功执行,搞了好几天终于成功了,记录一下。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/442350
推荐阅读
相关标签
  

闽ICP备14008679号