Programming with the USBDM API is pretty straightforward. The documentation for the public API is available in USBDM_API.h.
Below is a simple example of using the API to dump the entire flash from a MC9S08JM60 chip. You can click on the various functions and definitions used in the example to obtain more information.
A similar example is available from the USBDM examples if the Eclipse plugin is installed.
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef __unix__
#include <memory.h>
#else
#include <mem.h>
#endif
#define USBDM_API_VERSION_REQUIRED (0x40A00) // Need V4.10.0
#if (USBDM_API_VERSION < USBDM_API_VERSION_REQUIRED)
#error "This program requires USBDM version 4.10.0 or later"
#endif
const int maxSrecSize = (1<<4);
typedef struct {
uint32_t start;
uint32_t end;
} MemRange;
struct DeviceDescription {
const char *name;
uint16_t interfaceSpeed;
unsigned numRanges;
MemRange ranges[10];
};
DeviceDescription deviceDescriptions[] = {
{ "MC9S08JM60",
0,
2,
{{0x10B0, 0x17FF},
{0x1960, 0xFFFF},}
},
{
"MC9S12NE64",
0,
2,
{{0x4000, 0x7FFF},
{0xC000, 0xFFFF},}
},
{
"MCF51CN128",
0,
1,
{{0x000000, 0x01FFFF},}
},
{
"MCF51JF128",
0,
1,
{{0x000000, 0x01FFFF},}
},
{
"MK40X256",
12000,
1,
{{0x000000, 0x03FFFF},}
},
};
void check(
USBDM_ErrorCode rc,
const char *file = NULL,
unsigned lineNum = 0) {
fprintf(stderr, "OK, [%s:#%4d]\n", file, lineNum);
return;
}
exit(rc);
}
#define CHECK(x) check((x), __FILE__, __LINE__)
void dumpSrec(uint8_t *buffer, uint32_t address, uint8_t size) {
bool allFF = true;
for(int sub=0; sub<size; sub++) {
if (buffer[sub] != 0xFF ) {
allFF = false;
break;
}
}
if ((size == 0) || allFF) {
return;
}
uint8_t *ptr = buffer;
uint8_t checkSum;
if ((address) < 0x10000U) {
printf("S1%02X%04X", size+3, address);
checkSum = size+3;
checkSum += (address>>8)&0xFF;
checkSum += (address)&0xFF;
}
else if (address < 0x1000000U) {
printf("S2%02X%06X", size+4, address);
checkSum = size+4;
checkSum += (address>>16)&0xFF;
checkSum += (address>>8)&0xFF;
checkSum += (address)&0xFF;
}
else {
printf("S3%02X%08X", size+5, address);
checkSum = size+5;
checkSum += (address>>24)&0xFF;
checkSum += (address>>16)&0xFF;
checkSum += (address>>8)&0xFF;
checkSum += (address)&0xFF;
}
while (size-->0) {
checkSum += *ptr;
printf("%02X", *ptr++);
}
checkSum = checkSum^0xFF;
printf("%02X\n", checkSum);
}
void dump(uint8_t *buffer, uint32_t address, unsigned size) {
fprintf(stderr, "dump [0x%06X-0x%06X]\n", address, address+size-1);
while (size>0) {
uint8_t oddBytes = address & (maxSrecSize-1);
uint8_t srecSize = maxSrecSize - oddBytes;
if (srecSize > size) {
srecSize = (uint8_t) size;
}
dumpSrec(buffer, address, srecSize);
address += srecSize;
buffer += srecSize;
size -= srecSize;
}
}
fprintf(stderr, "checkAPI(), required Version=%d, DLL version=%d \n",
}
}
void dumpMemoryRange(MemRange &memoryRange) {
uint8_t buffer[0x10000];
memset(buffer, 0xFF, sizeof(buffer));
fprintf(stderr, "Range [0x%06X-0x%06X]\n", memoryRange.start, memoryRange.end);
uint32_t size = memoryRange.end-memoryRange.start+1;
uint32_t address = memoryRange.start;
while (size > 0) {
uint32_t blockSize = size;
if (blockSize > sizeof(buffer)) {
blockSize = sizeof(buffer);
}
dump(buffer, address, blockSize);
address += blockSize;
size -= blockSize;
}
}
void openAndConnect(DeviceDescription &deviceDescription) {
unsigned deviceCount;
fprintf(stderr, "BDM Firmware version = %d.%d.%d\n", (ver>>16)&0xFF,(ver>>8)&0xFF,ver&0xFF);
fprintf(stderr, "DLL version = %d.%d.%d\n", (ver>>16)&0xFF,(ver>>8)&0xFF,ver&0xFF);
deviceDescription.targetType};
bool retry = false;
do {
fprintf(stderr, "Failed to connect: Error - %s\n"
"Please cycle target power & press enter to retry, N to abort: ",
char buff[1000];
gets(buff);
int ch = buff[0];
retry = (ch != 'N') && (ch != 'n');
}
CHECK(rc);
}
void dumpChip(DeviceDescription &deviceDescription) {
fprintf(stderr, "Dumping %s\n", deviceDescription.name);
openAndConnect(deviceDescription);
for (unsigned sub=0;
sub<deviceDescription.numRanges;
sub++) {
dumpMemoryRange(deviceDescription.ranges[sub]);
}
}
void usage(void) {
fprintf(stderr,
"Usage: CopyFlash target\n"
"Targets:\n");
for (unsigned index=0;
index < (sizeof(deviceDescriptions)/sizeof(deviceDescriptions[0]));
index++) {
fprintf(stderr, "%3d: %s\n", index, deviceDescriptions[index].name);
}
}
int main(int argc, char *argv[]) {
CHECK(checkAPI());
if (argc != 2) {
usage();
}
unsigned option = atoi(argv[1]);
if (option>=(sizeof(deviceDescriptions)/sizeof(deviceDescriptions[0]))) {
usage();
}
dumpChip(deviceDescriptions[option]);
return 0;
}