|

楼主 |
发表于 2021-3-30 11:17:49
|
显示全部楼层
北京市北京市
#include "main.h"
#include<stdio.h>
#include<windows.h>
#include <stdlib.h>
#include <time.h>
#define JLINK_TIF_JTAG 0
#define JLINK_TIF_SWD 1
void (WINAPI *JLINK_SetLogFile)(char *file);
DWORD (WINAPI *JLINK_GetDLLVersion)(void);
DWORD (WINAPI *JLINK_GetHardwareVersion)(void);
DWORD (WINAPI *JLINK_GetFirmwareString)(char *buff, int count);
DWORD (WINAPI *JLINK_GetSN)(void);
BOOL (WINAPI *JLINK_ExecCommand)(char* cmd, int a, int b);
DWORD (WINAPI *JLINK_TIF_Select)(int type);
void (WINAPI *JLINK_SetSpeed)(int speed);
DWORD (WINAPI *JLINK_GetSpeed)(void);
DWORD (WINAPI *JLINK_GetId)(void);
DWORD (WINAPI *JLINK_GetDeviceFamily)(void);
BOOL (WINAPI *JLINK_Open)(void);
void (WINAPI *JLINK_Close)(void);
BOOL (WINAPI *JLINK_IsOpen)(void);
BOOL (WINAPI *JLINK_Connect)(void);
BOOL (WINAPI *JLINK_IsConnected)(void);
int (WINAPI *JLINK_Halt)(void);
BOOL (WINAPI *JLINK_IsHalted)(void);
void (WINAPI *JLINK_SetResetType)(int type);
void (WINAPI *JLINK_Reset)(void);
void (WINAPI *JLINK_Go)(void);
void (WINAPI *JLINK_GoIntDis)(void);
DWORD (WINAPI *JLINK_ReadReg)(int index);
int (WINAPI *JLINK_WriteReg)(int index, DWORD data);
int (WINAPI *JLINK_ReadMem)(DWORD addr, int len, void *buf);
int (WINAPI *JLINK_WriteMem)(DWORD addr, int len, void *buf);
int (WINAPI *JLINK_WriteU8)(DWORD addr, BYTE data);
int (WINAPI *JLINK_WriteU16)(DWORD addr, WORD data);
int (WINAPI *JLINK_WriteU32)(DWORD addr, DWORD data);
int (WINAPI *JLINK_EraseChip)(void);
int (WINAPI *JLINK_DownloadFile)(LPCSTR file, DWORD addr);
void (WINAPI *JLINK_BeginDownload)(int index);
void (WINAPI *JLINK_EndDownload)(void);
void JlinkInit(void)
{
HMODULE hModule = LoadLibrary("JlinkARM.dll");
if (hModule != NULL)
{
printf("dll open success\n");
}else
{
printf("dll open fail\n");
}
JLINK_GetDLLVersion = (void*)GetProcAddress(hModule, "JLINK_GetDLLVersion");
JLINK_GetHardwareVersion = (void*)GetProcAddress(hModule, "JLINK_GetHardwareVersion");
JLINK_GetFirmwareString = (void*)GetProcAddress(hModule, "JLINK_GetFirmwareString");
JLINK_GetSN = (void*)GetProcAddress(hModule, "JLINK_GetSN");
JLINK_GetId = (void*)GetProcAddress(hModule, "JLINK_GetId");
JLINK_GetDeviceFamily = (void*)GetProcAddress(hModule, "JLINK_GetDeviceFamily");
JLINK_SetLogFile = (void*)GetProcAddress(hModule, "JLINK_SetLogFile");
JLINK_ExecCommand = (void*)GetProcAddress(hModule, "JLINK_ExecCommand");
JLINK_GetSpeed = (void*)GetProcAddress(hModule, "JLINK_GetSpeed");
JLINK_SetSpeed = (void*)GetProcAddress(hModule, "JLINK_SetSpeed");
JLINK_TIF_Select = (void*)GetProcAddress(hModule, "JLINK_TIF_Select");
JLINK_Open = (void*)GetProcAddress(hModule, "JLINK_Open");
JLINK_Close = (void*)GetProcAddress(hModule, "JLINK_Close");
JLINK_IsOpen = (void*)GetProcAddress(hModule, "JLINK_IsOpen");
JLINK_Connect = (void*)GetProcAddress(hModule, "JLINK_Connect");
JLINK_IsConnected = (void*)GetProcAddress(hModule, "JLINK_IsConnected");
JLINK_SetResetType = (void*)GetProcAddress(hModule, "JLINK_SetResetType");
JLINK_Reset = (void*)GetProcAddress(hModule, "JLINK_Reset");
JLINK_Halt = (void*)GetProcAddress(hModule, "JLINK_Halt");
JLINK_IsHalted = (void*)GetProcAddress(hModule, "JLINK_IsHalted");
JLINK_Go = (void*)GetProcAddress(hModule, "JLINK_Go");
JLINK_GoIntDis = (void*)GetProcAddress(hModule, "JLINK_GoIntDis");
JLINK_ReadReg = (void*)GetProcAddress(hModule, "JLINK_ReadReg");
JLINK_WriteReg = (void*)GetProcAddress(hModule, "JLINK_WriteReg");
JLINK_ReadMem = (void*)GetProcAddress(hModule, "JLINK_ReadMem");
JLINK_WriteMem = (void*)GetProcAddress(hModule, "JLINK_WriteMem");
JLINK_WriteU8 = (void*)GetProcAddress(hModule, "JLINK_WriteU8");
JLINK_WriteU16 = (void*)GetProcAddress(hModule, "JLINK_WriteU16");
JLINK_WriteU32 = (void*)GetProcAddress(hModule, "JLINK_WriteU32");
JLINK_EraseChip = (void*)GetProcAddress(hModule, "JLINK_EraseChip");
JLINK_DownloadFile = (void*)GetProcAddress(hModule, "JLINK_DownloadFile");
JLINK_BeginDownload = (void*)GetProcAddress(hModule, "JLINK_BeginDownload");
JLINK_EndDownload = (void*)GetProcAddress(hModule, "JLINK_EndDownload");
}
typedef int(*_Key);
unsigned char tbuf[200];
unsigned int xbuf;
int main()
{
JlinkInit();//JLink初始化
//JLINK_SetLogFile("jlink.log");//设置日志文件
if(!JLINK_Open())//打开JLink
{
printf("JLINK OPEN SUCCESS \n"); //输出显示-打开成功!
}else printf("JLINK OPEN fail \n");//输出显示-打开失败!
printf("DLL Version: %u\n", JLINK_GetDLLVersion()); //获取J-Flash版本
JLINK_ExecCommand("device = MKL16Z128xxx4", 0, 0);//设置芯片型号
JLINK_TIF_Select(JLINK_TIF_SWD)//选择连接方式
JLINK_SetSpeed(10000);//设置烧录速度
if(JLINK_Connect()) //JLink连接
{
printf("JLINK_连接失败!\n");
return 0;//返回0
}else printf("JLINK_连接成功!\n");
//printf("HW Version: %u\n", JLINK_GetHardwareVersion());//获取硬件版本
//printf("SerialNumber: %u\n", JLINK_GetSN());//获取序列号
//JLINK_EraseChip();//擦除芯片。
if(JLINK_DownloadFile("927-01.bin",0x8000000))//烧录:(参数:文件路径,地址)
{
printf("JLINK 下载失败 \n");
return 0; //返回0
}else printf("JLINK 下载成功 \n");
printf("DEVICE : %d \n",JLINK_GetDeviceFamily());//获取设备唯一标识
printf("DEVICEID : %d \n",JLINK_GetId());//获取ID
JLINK_ReadMem(0x4004804c,4,&xbuf);//读内存
printf("DEVICE 型号 ; %x \n",xbuf);
switch((xbuf>>24)&0xff)
{
case 0:
printf("芯片容量为 8KB FLASH;\n");
break;
case 1:
printf("芯片容量为 16KB FLASH;\n");
break;
case 3:
printf("芯片容量为 32KB FLASH;\n");
break;
case 5:
printf("芯片容量为 64KB FLASH;\n");
break;
case 7:
printf("芯片容量为 128KB FLASH;\n");
break;
case 9:
printf("芯片容量为 256KB FLASH;\n");
break;
}
//JLINK_BeginDownload(0);
JLINK_Reset();
//JLINK_Halt();
//Sleep(5000);
/*
JLINK_ReadMem(0x00,100,tbuf);
int i;
for(i=0;i<100;)
{
printf("%x ",tbuf[i]);
i++;
if(i%10 == 0)printf("\n");
}
printf("\n");
*/
//JLINK_EraseChip();//擦除芯片
JLINK_Halt();
JLINK_Close();
//Sleep(5000);
//JLINK_Open();
//JLINK_Close();
}
//////////////////////////////////////////////////////////////
//示例https://blog.csdn.net/qq446252221/article/details/89878996
JLINK_SetLogFile("jlink.log");//设置日志文件
JLINK_Open();//打开
printf("Load driver OK.\n");
printf("-----------------------------\n");
printf("DLL Version: %u\n", JLINK_GetDLLVersion());//获取版本
printf("HW Version: %u\n", JLINK_GetHardwareVersion());//获取硬件版本
printf("SerialNumber: %u\n", JLINK_GetSN());//获取序列号
printf("-----------------------------\n");
JLINK_ExecCommand("device = NRF51822_XXAC", 0, 0);//设置芯片
JLINK_TIF_Select(JLINK_TIF_SWD);//设置连接方式,JATG为0;SWD为1。
JLINK_SetSpeed(10000);//设置烧录速度
JLINK_Connect();//链接JLink
JLINK_EraseChip();//擦除芯片。
JLINK_DownloadFile("boot.bin",0x00000000))//烧录:(参数:文件路径,地址)
JLINK_GetId();//获取ID
JLINK_GetDeviceFamily());//获取设备唯一标识
JLINK_ReadMem(0x4004804c,4,&xbuf);//读内存
JLINK_Reset()//复位
JLINK_Halt();//停止
JLINK_Close();//关闭
|
|