返回> 网站首页 

使用VC++调用jlink烧录STM32

yoours2021-07-20 19:39:05 STM32 阅读 3450

简介一边听听音乐,一边写写文章。

测试通过,烧录完成后重新上电即可运行程序。
//JLINK TIF
#define JLINK_TIF_JTAG          0
#define JLINK_TIF_SWD           1

//RESET TYPE
#define JLINK_RESET_TYPE_NORMAL 0
#define JLINK_RESET_TYPE_CORE   1
#define JLINK_RESET_TYPE_PIN    2

//REGISTER INDEX
/* 
0 - 15     R0 - R15(SP=R13, PC=R15)
16          XPSR
17          MSP
18          PSP
19          RAZ
20          CFBP
21          APSR
22          EPSR
23          IPSR
24          PRIMASK
25          BASEPRI
26          FAULTMASK
27          CONTROL 
28          BASEPRI_MAX
29          IAPSR
30          EAPSR
31          IEPSR
*/

typedef void  (WINAPI *JLINK_SetLogFile)(char *file);
typedef DWORD (WINAPI *JLINK_GetDLLVersion)(void);
typedef DWORD (WINAPI *JLINK_GetHardwareVersion)(void);
typedef DWORD (WINAPI *JLINK_GetFirmwareString)(char *buff, int count);
typedef DWORD (WINAPI *JLINK_GetSN)(void);

typedef BOOL  (WINAPI *JLINK_ExecCommand)(char* cmd, int a, int b);
typedef DWORD (WINAPI *JLINK_TIF_Select)(int type);
typedef void  (WINAPI *JLINK_SetSpeed)(int speed);
typedef DWORD (WINAPI *JLINK_GetSpeed)(void);
typedef DWORD (WINAPI *JLINK_GetId)(void);
typedef DWORD (WINAPI *JLINK_GetDeviceFamily)(void);

typedef BOOL  (WINAPI *JLINK_Open)(void);
typedef void  (WINAPI *JLINK_Close)(void);
typedef BOOL  (WINAPI *JLINK_IsOpen)(void);

typedef BOOL  (WINAPI *JLINK_Connect)(void);
typedef BOOL  (WINAPI *JLINK_IsConnected)(void);
typedef int   (WINAPI *JLINK_Halt)(void);
typedef BOOL  (WINAPI *JLINK_IsHalted)(void);
typedef void  (WINAPI *JLINK_SetResetType)(int type);
typedef void  (WINAPI *JLINK_Reset)(void);
typedef void  (WINAPI *JLINK_Go)(void);
typedef void  (WINAPI *JLINK_GoIntDis)(void);
typedef DWORD (WINAPI *JLINK_ReadReg)(int index);
typedef int   (WINAPI *JLINK_WriteReg)(int index, DWORD data);

typedef int   (WINAPI *JLINK_ReadMem)(DWORD addr, int len, void *buf);
typedef int   (WINAPI *JLINK_WriteMem)(DWORD addr, int len, void *buf);
typedef int   (WINAPI *JLINK_WriteU8)(DWORD addr, BYTE data);
typedef int   (WINAPI *JLINK_WriteU16)(DWORD addr, WORD data);
typedef int   (WINAPI *JLINK_WriteU32)(DWORD addr, DWORD data);

typedef int   (WINAPI *JLINK_EraseChip)(void);
typedef int   (WINAPI *JLINK_DownloadFile)(LPCSTR file, DWORD addr);
typedef void  (WINAPI *JLINK_BeginDownload)(int index);
typedef void  (WINAPI *JLINK_EndDownload)(void);

typedef BOOL  (WINAPI *JLINK_HasError)(void);
typedef void  (WINAPI *JLINKARM_Go)(void);


int TestJLink()
{
    HMODULE  hModule = LoadLibrary("JLinkARM.dll");

    JLINK_GetDLLVersion         jLINK_GetDLLVersion      = (JLINK_GetDLLVersion)GetProcAddress(hModule, "JLINK_GetDLLVersion");
    JLINK_GetHardwareVersion    jLINK_GetHardwareVersion = (JLINK_GetHardwareVersion)GetProcAddress(hModule, "JLINK_GetHardwareVersion");
    JLINK_GetFirmwareString     jLINK_GetFirmwareString  = (JLINK_GetFirmwareString)GetProcAddress(hModule, "JLINK_GetFirmwareString");
    JLINK_GetSN                 jLINK_GetSN              = (JLINK_GetSN)GetProcAddress(hModule, "JLINK_GetSN");
    JLINK_GetId                 jLINK_GetId              = (JLINK_GetId)GetProcAddress(hModule, "JLINK_GetId");
    JLINK_GetDeviceFamily       jLINK_GetDeviceFamily    = (JLINK_GetDeviceFamily)GetProcAddress(hModule, "JLINK_GetDeviceFamily");
                            
    JLINK_SetLogFile            jLINK_SetLogFile         = (JLINK_SetLogFile)GetProcAddress(hModule, "JLINK_SetLogFile");
    JLINK_ExecCommand           jLINK_ExecCommand        = (JLINK_ExecCommand)GetProcAddress(hModule, "JLINK_ExecCommand");
                            
    JLINK_GetSpeed              jLINK_GetSpeed           = (JLINK_GetSpeed)GetProcAddress(hModule, "JLINK_GetSpeed");
    JLINK_SetSpeed              jLINK_SetSpeed           = (JLINK_SetSpeed)GetProcAddress(hModule, "JLINK_SetSpeed");
    JLINK_TIF_Select            jLINK_TIF_Select         = (JLINK_TIF_Select)GetProcAddress(hModule, "JLINK_TIF_Select");
                            
    JLINK_Open                  jLINK_Open               = (JLINK_Open)GetProcAddress(hModule, "JLINK_Open");
    JLINK_Close                 jLINK_Close              = (JLINK_Close)GetProcAddress(hModule, "JLINK_Close");
    JLINK_IsOpen                jLINK_IsOpen             = (JLINK_IsOpen)GetProcAddress(hModule, "JLINK_IsOpen");
                            
    JLINK_Connect               jLINK_Connect            = (JLINK_Connect)GetProcAddress(hModule, "JLINK_Connect");
    JLINK_IsConnected           jLINK_IsConnected        = (JLINK_IsConnected)GetProcAddress(hModule, "JLINK_IsConnected");
    JLINK_SetResetType          jLINK_SetResetType       = (JLINK_SetResetType)GetProcAddress(hModule, "JLINK_SetResetType");
    JLINK_Reset                 jLINK_Reset              = (JLINK_Reset)GetProcAddress(hModule, "JLINK_Reset");
    JLINK_Halt                  jLINK_Halt               = (JLINK_Halt)GetProcAddress(hModule, "JLINK_Halt");
    JLINK_IsHalted              jLINK_IsHalted           = (JLINK_IsHalted)GetProcAddress(hModule, "JLINK_IsHalted");
    JLINK_Go                    jLINK_Go                 = (JLINK_Go)GetProcAddress(hModule, "JLINK_Go");
    JLINK_GoIntDis              jLINK_GoIntDis           = (JLINK_GoIntDis)GetProcAddress(hModule, "JLINK_GoIntDis");
                            
    JLINK_ReadReg               jLINK_ReadReg            = (JLINK_ReadReg)GetProcAddress(hModule, "JLINK_ReadReg");
    JLINK_WriteReg              jLINK_WriteReg           = (JLINK_WriteReg)GetProcAddress(hModule, "JLINK_WriteReg");
                            
    JLINK_ReadMem               jLINK_ReadMem            = (JLINK_ReadMem)GetProcAddress(hModule, "JLINK_ReadMem");
    JLINK_WriteMem              jLINK_WriteMem           = (JLINK_WriteMem)GetProcAddress(hModule, "JLINK_WriteMem");
    JLINK_WriteU8               jLINK_WriteU8            = (JLINK_WriteU8)GetProcAddress(hModule, "JLINK_WriteU8");
    JLINK_WriteU16              jLINK_WriteU16           = (JLINK_WriteU16)GetProcAddress(hModule, "JLINK_WriteU16");
    JLINK_WriteU32              jLINK_WriteU32           = (JLINK_WriteU32)GetProcAddress(hModule, "JLINK_WriteU32");
                            
    JLINK_EraseChip             jLINK_EraseChip          = (JLINK_EraseChip)GetProcAddress(hModule, "JLINK_EraseChip");
    JLINK_DownloadFile          jLINK_DownloadFile       = (JLINK_DownloadFile)GetProcAddress(hModule, "JLINK_DownloadFile");
    JLINK_BeginDownload         jLINK_BeginDownload      = (JLINK_BeginDownload)GetProcAddress(hModule, "JLINK_BeginDownload");
    JLINK_EndDownload           jLINK_EndDownload        = (JLINK_EndDownload)GetProcAddress(hModule, "JLINK_EndDownload");
 
    JLINK_HasError              jLINK_HasError           = (JLINK_HasError)GetProcAddress(hModule, "JLINK_HasError");
    JLINKARM_Go                 jLINKARM_Go              = (JLINKARM_Go)GetProcAddress(hModule, "JLINKARM_Go");

    jLINK_SetLogFile("jlink.log");
    if(jLINK_Open()!=0)
        return 0;

    TRACE("-----------------------------\n");
    TRACE("DLL Version:  %u\n", jLINK_GetDLLVersion());
    TRACE("HW Version:   %u\n", jLINK_GetHardwareVersion());
    TRACE("SerialNumber: %u\n", jLINK_GetSN());
    TRACE("-----------------------------\n");
    jLINK_ExecCommand("device=STM32F207ZG", 0, 0);
    jLINK_TIF_Select(JLINK_TIF_JTAG);

    jLINK_SetSpeed(100000);
    TRACE("CPU ID: 0x%08X\n", jLINK_GetId());
    jLINK_Connect();

    if(jLINK_IsHalted()!=0)
    {
        jLINK_Halt();
    }

    //jLINK_EraseChip();
    jLINK_BeginDownload(0);

    int cnt=0;
    FILE *fp = fopen("FR51JB.bin","rb");
    while (!feof(fp))
    {
        BYTE data[1024];
        int rec = fread(data, 1, 1024, fp);
        jLINK_WriteMem(1024*cnt, rec, data);
        ::Sleep(1);
        cnt++;

        if(jLINK_HasError())
        {
            TRACE("\nfail\n");
            break;
        }
    }

    fclose(fp);

    jLINK_EndDownload();

    jLINKARM_Go();
    jLINK_Halt();

    FreeLibrary(hModule);
    return 0;
}
微信小程序扫码登陆

文章评论

3450人参与,0条评论