|
![](static/image/common/ico_lz.png)
楼主 |
发表于 2019-9-20 16:02:28
|
显示全部楼层
h文件
#ifndef _JK_H__
#define _JK_H__
//读取指定内存的值
typedef void(*pFunc_ReadMem)(unsigned int addr, unsigned int size, unsigned char *buf);
//想指定内存地址写入固定值
typedef void(*pFunc_WriteMem)(unsigned int addr, unsigned int size, const void *buf);
//选择芯片类型函数
typedef unsigned int(*pFunc_ExecCommand)(char *device, unsigned char *pbuf, unsigned int size);
//设置通信速度
typedef void(*pFunc_SetSpeed)(unsigned short speed);
//读取通信速度
typedef unsigned short(*pFunc_GetSpeed)(void);
//获取ID号
typedef unsigned int(*pFunc_GetId)(void);
typedef void(*pFunc_TIF_Select)(unsigned int i);
typedef unsigned int(*pFunc_IsConnected)(void);
//write reg
typedef void(*pFunc_WriteReg)(unsigned int index, unsigned int data);
//read reg
typedef unsigned int(*pFunc_ReadReg)(unsigned int index);
//read mem u32
typedef void(*pFunc_ReadMemU32)(unsigned int addr, unsigned int size, unsigned int *data, unsigned char *status);
//read mem u16
typedef void(*pFunc_ReadMemU16)(unsigned int addr, unsigned int size, unsigned short *data, unsigned char *status);
//read mem u8
typedef void(*pFunc_ReadMemU8)(unsigned int addr, unsigned int size, unsigned char *data, unsigned char *status);
//write u32
typedef void(*pFunc_WriteU32)(unsigned int addr, unsigned int data);
//read u32
typedef unsigned int(*pFunc_ReadU32)(unsigned int addr);
//write u16
typedef void(*pFunc_WriteU16)(unsigned int addr, unsigned short data);
//read u16
typedef unsigned short(*pFunc_ReadU16)(unsigned int addr);
//write u8
typedef void(*pFunc_WriteU8)(unsigned int addr, unsigned char data);
//read u8
typedef unsigned char(*pFunc_ReadU8)(unsigned int addr);
typedef struct
{
pFunc_ReadMem JLINKARM_ReadMem;
pFunc_WriteMem JLINKARM_WriteMem;
pFunc_WriteReg JLINKARM_WriteReg;
pFunc_ReadReg JLINKARM_ReadReg;
pFunc_ReadMemU32 JLINKARM_ReadMemU32;
pFunc_ReadMemU16 JLINKARM_ReadMemU16;
pFunc_ReadMemU8 JLINKARM_ReadMemU8;
pFunc_WriteU32 JLINKARM_WriteU32;
pFunc_ReadU32 JLINKARM_ReadU32;
pFunc_WriteU16 JLINKARM_WriteU16;
pFunc_ReadU16 JLINKARM_ReadU16;
pFunc_WriteU8 JLINKARM_WriteU8;
pFunc_ReadU8 JLINKARM_ReadU8;
pFunc_GetId JLINKARM_GetId;
pFunc_ExecCommand JLINKARM_ExecCommand;
pFunc_GetSpeed JLINKARM_GetSpeed;
pFunc_SetSpeed JLINKARM_SetSpeed;
pFunc_TIF_Select JLINKARM_TIF_Select;
pFunc_IsConnected JLINKARM_IsConnected;
}dsJlink;
extern dsJlink jlink;
unsigned int InitJLink(void);
#endif
c文件
#include "stdafx.h"
#include "windows.h"
//#include <iostream>
#include "jlink.h"
#pragma comment(lib, "JLinkARM.dll")
dsJlink jlink;
unsigned char JLINKARM_ReadU8(unsigned int addr)
{
unsigned char ret;
unsigned char st;
jlink.JLINKARM_ReadMemU8(addr, 1, &ret, &st);
return ret;
}
unsigned short JLINKARM_ReadU16(unsigned int addr)
{
unsigned short ret;
unsigned char st;
jlink.JLINKARM_ReadMemU16(addr, 1, &ret, &st);
return ret;
}
unsigned int JLINKARM_ReadU32(unsigned int addr)
{
unsigned int ret;
unsigned char st;
jlink.JLINKARM_ReadMemU32(addr, 1, &ret, &st);
return ret;
}
unsigned int InitJLink(void)
{
#define BUF_MAX_SIZE 600
unsigned char buf[BUF_MAX_SIZE];
//加载动态库
HMODULE hMod = LoadLibrary("JLinkARM.dll");
///根据函数名获取函数指针
jlink.JLINKARM_TIF_Select = (pFunc_TIF_Select)GetProcAddress(hMod, TEXT("JLINKARM_TIF_Select"));
jlink.JLINKARM_ExecCommand = (pFunc_ExecCommand)GetProcAddress(hMod, TEXT("JLINKARM_ExecCommand"));
jlink.JLINKARM_GetId = (pFunc_GetId)GetProcAddress(hMod, TEXT("JLINKARM_GetId"));
jlink.JLINKARM_IsConnected = (pFunc_IsConnected)GetProcAddress(hMod, TEXT("JLINKARM_IsConnected"));
jlink.JLINKARM_ReadMem = (pFunc_ReadMem)GetProcAddress(hMod, TEXT("JLINKARM_ReadMem"));
jlink.JLINKARM_WriteMem = (pFunc_WriteMem)GetProcAddress(hMod, TEXT("JLINKARM_WriteMem"));
jlink.JLINKARM_SetSpeed = (pFunc_SetSpeed)GetProcAddress(hMod, TEXT("JLINKARM_SetSpeed"));
jlink.JLINKARM_GetSpeed = (pFunc_GetSpeed)GetProcAddress(hMod, TEXT("JLINKARM_GetSpeed"));
jlink.JLINKARM_ReadMemU32 = (pFunc_ReadMemU32)GetProcAddress(hMod, TEXT("JLINKARM_ReadMemU32"));
jlink.JLINKARM_ReadMemU16 = (pFunc_ReadMemU16)GetProcAddress(hMod, TEXT("JLINKARM_ReadMemU16"));
jlink.JLINKARM_ReadMemU8 = (pFunc_ReadMemU8)GetProcAddress(hMod, TEXT("JLINKARM_ReadMemU8"));
jlink.JLINKARM_WriteU32 = (pFunc_WriteU32)GetProcAddress(hMod, TEXT("JLINKARM_WriteU32"));
jlink.JLINKARM_ReadU32 = (pFunc_ReadU32)GetProcAddress(hMod, TEXT("JLINKARM_ReadU32"));
jlink.JLINKARM_WriteU16 = (pFunc_WriteU16)GetProcAddress(hMod, TEXT("JLINKARM_WriteU16"));
jlink.JLINKARM_ReadU16 = (pFunc_ReadU16)GetProcAddress(hMod, TEXT("JLINKARM_ReadU16"));
jlink.JLINKARM_WriteU8 = (pFunc_WriteU8)GetProcAddress(hMod, TEXT("JLINKARM_WriteU8"));
jlink.JLINKARM_ReadU8 = (pFunc_ReadU8)GetProcAddress(hMod, TEXT("JLINKARM_ReadU8"));
jlink.JLINKARM_WriteReg = (pFunc_WriteReg)GetProcAddress(hMod, TEXT("JLINK_WriteReg"));
jlink.JLINKARM_ReadReg = (pFunc_ReadReg)GetProcAddress(hMod, TEXT("JLINK_ReadReg"));
//以下三个函数上面没有一次封装,这里进行二次封装一下
jlink.JLINKARM_ReadU32 = JLINKARM_ReadU32;
jlink.JLINKARM_ReadU16 = JLINKARM_ReadU16;
jlink.JLINKARM_ReadU8 = JLINKARM_ReadU8;
//选择设备
if (jlink.JLINKARM_TIF_Select != NULL)
{
jlink.JLINKARM_TIF_Select(1);
}
//设置芯片型号
if (jlink.JLINKARM_ExecCommand != NULL)
{
unsigned int ret = 0;
//选择芯片类型
ret = jlink.JLINKARM_ExecCommand("Device = Cortex-M3", buf, 64);
}
//判断jlink是否连接上
if (jlink.JLINKARM_IsConnected != NULL)
{
unsigned int ret = 0;
ret = jlink.JLINKARM_IsConnected();
printf("isConnected is %d\r\n", ret);
}
//设置jlink速度
if (jlink.JLINKARM_SetSpeed != NULL)
{
jlink.JLINKARM_SetSpeed(12000);
printf("set jlink speed ok\r\n");
}
//获取jlink速度
if (jlink.JLINKARM_GetSpeed != NULL)
{
unsigned int speed = jlink.JLINKARM_GetSpeed();
printf("get speed is %d khz\r\n", speed);
}
if (jlink.JLINKARM_WriteMem != NULL)
{
printf("jlink write mem func not null!\r\n");
}
return 0;
}
这是以前写得玩的 , 希望对你有用 |
|