Dennis Shutt

HC908GT8(16) writing non-volatile data to FLASH

Discussion created by Dennis Shutt on May 24, 2006
Latest reply on May 25, 2006 by Dennis Shutt
I am working on small project using the HC908GT8 mcu and need to record a small amount of data to FLASH and also store the trim value. I have written the following code after reading a few threads on here and the user manual for the derivative I am using.
Unfortunatly I do not have a board here to test with. I will be able to do a hardware test in the morning though. Just thought I would present my idea and see if anyone had any further input and or could use it for other HC(S)08 family members.
 
First here is the prm file in which I have defined two small segments. One section is in FLASH and the other in RAM. The FLASH segment is used to compile and store the ProgramFlash function. The RAM segment will hold a copy of the ProgramFlash function.
 
Code:
/* This is a linker parameter file for the GT8 */NAMES END /* CodeWarrior will pass all the needed files to the linker by command line. But here you may add your own files too. */SEGMENTS /* Here all RAM/ROM areas of the device are listed. Used in PLACEMENT below. */    ROM                      =  READ_ONLY    0xE051 TO 0xFDFF;    ROM2                     =  READ_ONLY    0xE000 TO 0xE050 FILL 0x00;    Z_RAM                    =  READ_WRITE   0x0040 TO 0x00FF;    RAM                      =  READ_WRITE   0x0151 TO 0x023F;    RAM2                     =  READ_WRITE   0x0100 TO 0x0150;ENDPLACEMENT /* Here all predefined and user segments are placed into the SEGMENTS defined above. */    DEFAULT_RAM                         INTO  RAM;    DEFAULT_ROM, ROM_VAR, STRINGS       INTO  ROM; /*  In case you want to use  as well, be sure the option -OnB=b is passed to the compiler. */    _DATA_ZEROPAGE, MY_ZEROPAGE         INTO  Z_RAM;        /* Code section for Flash programing function */    FLASH_CODE                          INTO  ROM2;    /* Copy section for Flash programming function */    /* Code must be ran from RAM whill programming Flash */    FLASH_RAM                           INTO  RAM2;    END/* Define the system STACK size */STACKSIZE 0x50/* Reset vector: this is the default entry point for an application. */VECTOR 0 _Startup/* Items in this section will be included during linking */ENTRIESProgramFlashEND

 
Now for the code.
Code:
#include <hidef.h> /* for EnableInterrupts macro */#include "derivative.h" /* include peripheral declarations *//* Define block protect register location */volatile const unsigned char BLOCK_PROTECT_REGISTER @ 0xFF7E;/* ProgramFlashByte function type define */typedef void (*PFB)(char*, char); /* Declare and initialize ProgramFlashByte */const PFB ProgramFlashByte = (PFB)0x0100;/* This segment is declared in the prm linker placement file *//* Segment address is 0x0100 to 0x0150 */#pragma DATA_SEG FLASH_RAM/* Declare array to copy ProgramFlash function into */unsigned char FlashRoutine[0x50];#pragma DATA_SEG DEFAULT/* This segment is declared in the prm linker placement file *//* Segment address is 0xE000 to 0xE050 */#pragma CODE_SEG FLASH_CODE/* This function will be copied to RAM at startup *//* Bus frequency MUST be at minimum 1 Mhz! for HC08GT8(16) *//* Delay timing is for bus speed of 2.457600Mhz maximum */void ProgramFlash(char* Address, char c){  /* Variables on the STACK */  char Buffer;    /* We must disable interrupts so no FLASH is accessed */  /* Remember to turn back on in main code */  DisableInterrupts;    /* Programming step 1 */  FLCR = 1;  /* Programming step 2 */  Buffer = BLOCK_PROTECT_REGISTER;           /* Programming step 3 */  Address[0] = c;  /* Wait for minimum 10us */  asm{    ldx #10;  Loop1:    dbnzx Loop1;  }  /* Programming step 5 */  FLCR = 9;  /* Wait for minimum 5us */  asm{    ldx #5;  Loop2:    dbnzx Loop2;  }  /* Programming step 7 */  Address[0] = c;  /* Wait for minimum 30us */  asm{    ldx #30;  Loop3:    dbnzx Loop3;  }  /* Programming step 10 */  FLCR = 8;  /* Wait for minimum 5us */  asm{    ldx #5;  Loop4:    dbnzx Loop4;  }  /* Programming step 12 */  FLCR = 0;  /* Programming step 13 */  asm{    nop;    nop;  }}#pragma CODE_SEG DEFAULT/* Function prototype(s) */void Cpu_Initialize(void);/* main() function */void main(void) {  /* Pointer to ProgramFlash function in FLASH */  volatile const char *Ptr = (volatile const char *)0xE000;  /* Used as index during code move from FLASH to RAM */  int Index;    /* Initialize cpu */  Cpu_Initialize();    /* Copy ProgramFlash function to RAM */  for(Index=0; Index<0x50; Index++){    FlashRoutine[Index] = Ptr[Index];  }  /* Program byte in FLASH using function in RAM */  ProgramFlashByte((char*)0xFF80, 20);    /* Loop forever */  for(;;) {  }}/* Initialize cpu registers and internal clock */void Cpu_Initialize(void) {  /* CONFIG1: COPRS=0,LVISTOP=0,LVIRSTD=1,LVIPWRD=0,LVI5OR3=1,SSREC=0,STOP=0,COPD=1 */  CONFIG1 = 41;  /* CONFIG2: Unused=0,Unused=0,EXTXTALEN=0,EXTSLOW=0,EXTCLKEN=0,Unused=0,OSCENINSTOP=0,Unused=0 */  CONFIG2 = 0;   /* ICG multiplier factor, 9.830400Mhz internal clock, 2.457600Mhz bus */  /* 307.2Khz * 32 */  ICGMR = 32;  /* Has the osc trim been set— */  if(*(byte*)0xFF80 != 0xFF)    ICGTR = *(byte*)0xFF80;  /* Wait for internal oscillator stabilization */  while(!ICGCR_ICGS);                  }

 
I believe this should work as inteneded(let me stress the should part). If it works I was thinking of trying to save the interrupt enable state and restore interrupts after programming the byte if they were enabled on entry to the ProgramFlashByte function. I read in the sticky post of the problems encountered when an interrupt occured while programming to the Flash array.
 

Outcomes