⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 opensourcebdm_dll.c

📁 Freescale的S08BDM开发工具制作资料
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
    Open Source BDM - interface DLL
    Copyright (C) 2005

	 Change History:
	 ------------------
	 *****Prominent Notice-This software was modified from TBDML software - 12/05

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <windows.h>
#include <time.h>
#include <stdio.h>
#include "log.h"
#include "version.h"
#include "opensourcebdm.h"
#include "opensourcebdm_usb.h"
#include "commands.h"

/* define symbol "LOG" during compilation to produce a log file opensourcebdm_dll.log */



static unsigned char usb_data[MAX_DATA_SIZE+2];

/* returns version of the DLL in BCD format */
unsigned char WINAPI __declspec(dllexport) opensourcebdm_dll_version(void) {
	return(opensourcebdm_DLL_VERSION);
}

/* initialises USB and returns number of devices found */
unsigned char WINAPI __declspec(dllexport) opensourcebdm_init(void) {
	unsigned char i;
	opensourcebdm_usb_find_devices();	/* look for devices on all USB busses */
	i=opensourcebdm_usb_cnt();
	print("opensourcebdm_INIT: Usb initialised, found %d device(s)\r\n",i);
	return(i);					/* count the devices found and return the number */
}

/* opens a device with given number (0...) */
/* returns 0 on success and 1 on error */
unsigned char WINAPI __declspec(dllexport) opensourcebdm_open(unsigned char device_no) {
	print("opensourcebdm_OPEN: Trying to open device #%d\r\n",device_no);
	return(opensourcebdm_usb_open(device_no));
}

/* closes currently open device */
void WINAPI __declspec(dllexport) opensourcebdm_close(void) {
	print("opensourcebdm_CLOSE: Trying to close the device\r\n");
	opensourcebdm_usb_close();
}

/* gets version of the interface (HW and SW) in BCD format */
unsigned int WINAPI __declspec(dllexport) opensourcebdm_get_version(void) {
	usb_data[0]=3;	/* get 3 bytes */
	usb_data[1]=CMD_GET_VER;
	opensourcebdm_usb_recv_ep0(usb_data);
	return((*((unsigned int *)(usb_data+1)))&0xffff);
}

/* returns status of the last command (either the command number or CMD_FAILED or CMD_UNKNOWN */
unsigned char WINAPI __declspec(dllexport) opensourcebdm_get_last_sts(void) {
	unsigned char i;
	usb_data[0]=1;	 /* get 1 byte */
	usb_data[1]=CMD_GET_LAST_STATUS;
	opensourcebdm_usb_recv_ep0(usb_data);
	if ((usb_data[0]==CMD_FAILED)||(usb_data[0]==CMD_UNKNOWN)) i=1; else i=0;
	return(i);
}

/* sets target MCU type */
/* returns 0 on success and non-zero on failure */
unsigned char WINAPI __declspec(dllexport) opensourcebdm_set_target_type(target_type_e target_type) {
	usb_data[0]=1;	 /* get 1 byte */
	usb_data[1]=CMD_SET_TARGET;
	usb_data[2]=target_type;
	opensourcebdm_usb_recv_ep0(usb_data);
	return(!(usb_data[0]==CMD_SET_TARGET));
}

/* measures BDM frequency of the target using the SYNC feature and connects to it */
/* returns 0 on success and non-zero on failure */
unsigned char WINAPI __declspec(dllexport) opensourcebdm_target_sync(void) {
	usb_data[0]=1;	 /* get 1 byte */
	usb_data[1]=CMD_CONNECT;
	opensourcebdm_usb_recv_ep0(usb_data);
	return(!(usb_data[0]==CMD_CONNECT));
}

/* resets the target to normal or special mode */
/* returns 0 on success and non-zero on failure */
unsigned char WINAPI __declspec(dllexport) opensourcebdm_target_reset(target_mode_e target_mode) {
	usb_data[0]=1;	 /* get 1 byte */
	usb_data[1]=CMD_RESET;
	usb_data[2]=target_mode;
	opensourcebdm_usb_recv_ep0(usb_data);
	return(!(usb_data[0]==CMD_RESET));
}

/* fills user supplied structure with actual state of BDM communication channel */
/* returns 0 on success and non-zero on failure */
unsigned char WINAPI __declspec(dllexport) opensourcebdm_bdm_sts(bdm_status_t *bdm_status) {
	usb_data[0]=3;	 /* get 3 bytes */
	usb_data[1]=CMD_GET_STATUS;
	opensourcebdm_usb_recv_ep0(usb_data);
	if (usb_data[0]!=CMD_GET_STATUS) return(1);
	bdm_status->ackn_state = usb_data[1]&0x01;
	bdm_status->reset_state = (usb_data[1]&0x02)>>1;
	bdm_status->connection_state = (usb_data[1]&0x18)>>3;
	return(0);
}


#if SELAHCS08
//
#else

/* reads one byte from the BDM area of memory */
/* returns byte read from memory */
unsigned char WINAPI __declspec(dllexport) opensourcebdm_read_bd(unsigned int address) {
	//ST======================================= ***********************
	{
	usb_data[0]=2;	 /* get 2 bytes */
	usb_data[1]=CMD_READ_BD;
	*((unsigned int*)(usb_data+2)) = address;
	opensourcebdm_usb_recv_ep0(usb_data);
	return(usb_data[1]);
	}


//#endif
}

/* writes one byte to the BDM area of memory */
/* returns 0 on success and non-zero on failure */
unsigned char WINAPI __declspec(dllexport) opensourcebdm_write_bd(unsigned int address, unsigned char data) {
	usb_data[0]=1;	 /* get 1 byte */
	usb_data[1]=CMD_WRITE_BD;
	*((unsigned int*)(usb_data+2)) = address;
	usb_data[4] = data;
	opensourcebdm_usb_recv_ep0(usb_data);
	return(!(usb_data[0]==CMD_WRITE_BD));
}

#endif


/* starts target execution from current PC address */
/* returns 0 on success and non-zero on failure */
unsigned char WINAPI __declspec(dllexport) opensourcebdm_target_go(void) {
	usb_data[0]=1;	 /* get 1 byte */
	usb_data[1]=CMD_GO1;
	opensourcebdm_usb_recv_ep0(usb_data);
	return(!(usb_data[0]==CMD_GO1));
}

/* steps over a single target instruction */
/* returns 0 on success and non-zero on failure */
unsigned char WINAPI __declspec(dllexport) opensourcebdm_target_step(void) {
	usb_data[0]=1;	 /* get 1 byte */
	usb_data[1]=CMD_STEP1;
	opensourcebdm_usb_recv_ep0(usb_data);
	return(!(usb_data[0]==CMD_STEP1));
}

/* brings the target into active background mode */
/* returns 0 on success and non-zero on failure */
unsigned char WINAPI __declspec(dllexport) opensourcebdm_target_halt(void) {
	usb_data[0]=1;	 /* get 1 byte */
	usb_data[1]=CMD_HALT;
	opensourcebdm_usb_recv_ep0(usb_data);
	return(!(usb_data[0]==CMD_HALT));
}

/* returns crystal frequency of the target in MHz */
float WINAPI __declspec(dllexport) opensourcebdm_get_speed(void) {
	usb_data[0]=3;	 /* get 3 bytes */
	usb_data[1]=CMD_READ_SPEED1;
	opensourcebdm_usb_recv_ep0(usb_data);
	return(60.0 * 2 * 128 / (*((unsigned int *)(usb_data+1))&0xffff));
}

/* sets the communication speed, parameter is crystal frequency in MHz */
/* returns 0 on success and non-zero on failure */
unsigned char WINAPI __declspec(dllexport) opensourcebdm_set_speed(float crystal_frequency) {
	/* the command parameter in this case is expected length of SYNC pulse expressed in 60MHz ticks */
	usb_data[0]=1;	 /* get 1 byte */
	usb_data[1]=CMD_SET_SPEED1;
	*((unsigned int*)(usb_data+2)) = 60.0 * 2 * 128 / crystal_frequency;
	opensourcebdm_usb_recv_ep0(usb_data);
	return(!(usb_data[0]==CMD_SET_SPEED1));
}

/* reads one byte from memory */
unsigned char WINAPI __declspec(dllexport) opensourcebdm_read_byte(unsigned int address) {
	usb_data[0]=2;	 /* get 2 bytes */
	usb_data[1]=CMD_READ_8;
	*((unsigned int*)(usb_data+2)) = address;
	opensourcebdm_usb_recv_ep0(usb_data);
	return(usb_data[1]);
}

/* writes one byte to memory */
void WINAPI __declspec(dllexport) opensourcebdm_write_byte(unsigned int address, unsigned char data) {
	usb_data[0]=4;	 /* send 4 bytes */
	usb_data[1]=CMD_WRITE_8;
	*((unsigned int*)(usb_data+2)) = address;
	usb_data[4] = data;
	opensourcebdm_usb_send_ep0(usb_data);
}

#if SELAHCS08
//not WORD operations for HCS08
#else
/* reads one word from memory (address must be aligned) */
unsigned int WINAPI __declspec(dllexport) opensourcebdm_read_word(unsigned int address) {
	usb_data[0]=3;	 /* get 3 bytes */
	usb_data[1]=CMD_READ_16;
	*((unsigned int*)(usb_data+2)) = address;
	opensourcebdm_usb_recv_ep0(usb_data);
	return((*((unsigned int *)(usb_data+1))&0xffff));
}

/* writes one word to memory (address must be aligned) */
void WINAPI __declspec(dllexport) opensourcebdm_write_word(unsigned int address, unsigned int data) {
	usb_data[0]=5;	 /* send 5 bytes */
	usb_data[1]=CMD_WRITE_16;
	*((unsigned int*)(usb_data+2)) = address;
	*((unsigned int*)(usb_data+4)) = data;
	opensourcebdm_usb_send_ep0(usb_data);
}
#endif

/* reads block from memory */
/* data is returned to the user supplied buffer */
void WINAPI __declspec(dllexport) opensourcebdm_read_block(unsigned int address, unsigned int count, unsigned char *data) {
	unsigned int i;
	if ((count>MAX_DATA_SIZE)&&(address&0x0001)) {
		/* if address is odd and we are going to read more than one block make the first packet 1 byte short to align the address */
		usb_data[1]=CMD_READ_BLOCK1;
		*((unsigned int*)(usb_data+2)) = address;
		usb_data[4]=(MAX_DATA_SIZE&0xfe)-1;
		#ifdef FAST_BLOCK
			usb_data[0]=4;
			opensourcebdm_usb_send_ep2(usb_data);
			usb_data[0]=(MAX_DATA_SIZE&0xfe)-1+1;
			opensourcebdm_usb_recv_ep2(usb_data);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -