Daniel Lundin

Crash during flash programming

Discussion created by Daniel Lundin on Jun 20, 2006
Latest reply on Jun 28, 2006 by Guillaume MENANT
I'm using HCS12 DG128, CW3.1 and P&E Cyclone.

I'm trying to write my own flash programming routines.
They work fine if I single-step through the code in the CW debugger, but when I let the program run I get a crash after setting the CBEIF flag in FSTAT.

I get the message "Other BGND, illegal BP" in the debugger, and the program counter gets set to a value that doesn't make sense. When I try without debugger and BDM, the program hangs at the same spot, and sometimes it starts to behave strange (probably because the PC is corrupted).

I have no flash security, no interrupts, no wdog. The flash prescaler clock seems to be correct, 192kHz for 20MHz oscillator (value 0xCC in the clock register). I get the same behaviour on more than one MCU.

static FlashResult _write_word(uint8 CMD, uint16* far dest, uint16* source)
uint8 page = farToPage(dest); /* Get page address from far pointer */
uint16* near_ptr = farToNear(dest); /* Get 16-bit address from far pointer */
uint8 bank = flashGetBank(dest); /* Get bank 1 or 0 for DG128 */
volatile uint8 fstat;

FCNFG = 0x01; /* select bank 1 */
FSTAT = 0x30; /* clear error flags BANK 1 */
FCNFG = 0x00; /* select bank 0 */
FSTAT = 0x30; /* clear error flags BANK 0 */

FCNFG = bank; /* set correct bank for paged memory */

if((FSTAT & CBEIF)==0)

uint8 tmp;
tmp = PPAGE;
PPAGE = page;
*near_ptr = *source;
PPAGE = tmp;
*near_ptr = *source;

FSTAT = CBEIF; /* program/MCU crashes here */

fstat = FSTAT;

if(fstat & PVIOL)

if(fstat & ACCERR)

while(!(FSTAT & CCIF))

return FLASH_OK;

Any ideas?