Hi pgo!
Thanks to your help, I finally managed to use the API and read all memory.
As you pointed out, the problem was that I unintendedly programmed the chip in secure mode.
I recently fixed linker script to define VECTOR/BDM region to be 0xFF00-0xFFFF (it was
defined as 0xFF80-0xFFFF with the size of _256_ before, and I thought that was a bug).
But that caused FSEC to be programmed to 0xFF, enabling secure mode.
Once I fixed it again to program 0xFE for FSEC, the code worked perfectly.
I'm pasting final working code here, so other people who might be interested can read it:
// USBDM API test to dump program memory.#include <stdio.h>#include <stdlib.h>#include <stdint.h>#include "USBDM_API.h"#define call(is_fatal, ...) do { \ int err = __VA_ARGS__; \ printf("#%d: %s\n", __LINE__, #__VA_ARGS__); \ if (err != BDM_RC_OK) { \ printf("[E] err=%d, %s\n", err, USBDM_GetErrorString(err)); \ if (is_fatal) { \ USBDM_Close(); USBDM_Exit(); exit(0); \ } \ } \ } while (0)#define check(...) call(1, __VA_ARGS__)#define trace(...) call(0, __VA_ARGS__)#define dumpmem_(size, name, name_str) do { \ int val = 0; \ check(USBDM_ReadMemory(1, size, name, (void *)&val)); \ printf("%s: 0x%.*X\n", name_str, 2 * size, val); \ } while (0)#define dumpmem(size, name) dumpmem_(size, name, #name)void dumphex(uint8_t *buf, int addr, int len) { int i, j; for (i = 0; i < len;) { printf("%.8X:", addr + i); for (j = i + 16; i < j && i < len;) { printf(" %.2X", buf[i++]); } printf("\n"); }}int main(int argc, char **argv) { unsigned int deviceCount; check(USBDM_Init()); check(USBDM_FindDevices(&deviceCount)); check(USBDM_Open(0)); check(USBDM_SetTargetVdd(BDM_TARGET_VDD_3V3)); check(USBDM_SetTargetType(T_HCS12)); check(USBDM_TargetReset(RESET_HARDWARE|RESET_SPECIAL)); // error can be ignored, if only PARTID is needed. trace(USBDM_Connect());#define PARTID 0x001A#define FSEC 0x0101 dumpmem(2, PARTID); dumpmem(1, FSEC);//#define ROMADDR 0x4000//#define ROMSIZE (0xFFFF - 0x4000 + 1)#define ROMADDR 0xFF00#define ROMSIZE 256 uint8_t buffer[65536] = {0}; check(USBDM_ReadMemory(1, ROMSIZE, ROMADDR, buffer)); dumphex(buffer, ROMADDR, ROMSIZE); check(USBDM_Close()); check(USBDM_Exit()); exit(0);}
Best Regards,