Hello.   I'm using a MC9S08AW60 device. My program read a...

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Hello.   I'm using a MC9S08AW60 device. My program read a...

5,653 Views
dar77
Contributor I
Hello.
 
I'm using a MC9S08AW60 device. My program read a code (4 bytes) from an external device and I need to store it in the MCU Flash.
Are there any routines to write in the HCS08 Flash?
Does anyone have a C sample code?
 
Thanks.
 
 
Labels (1)
0 Kudos
17 Replies

969 Views
dar77
Contributor I
Hello.
 
I tried to write a byte at the Flash address 0x0870 with this C function:
 
unsigned char Program(void) {
 
  unsigned int Address = 0x0870;
  if (FSTAT & 0x10){                                               /* Check to see if FACCERR is set */
    FSTAT = FSTAT | 0x10;                                     /* Write a 1 to FACCERR to clear  */
  }
  (*((volatile unsigned char *)(Address))) = 0x11;  /* Write to somewhere in flash */
  FCMD = 0x20;                                                      /* Set command type */
  FSTAT = FSTAT | 0x80;                                       /* Put FCBEF at 1 */
  _asm NOP;                                                           /* Wait 4 cycles  */
  _asm NOP;
  _asm NOP;
  _asm NOP;
  if (FSTAT & 0x30){                                                /* Check to see if FACCERR or FVIOL are set */
    return 0xFF;                                                        /* If so error */
  }
  while ((FSTAT & 0x40)==0){                                /* else wait for command to complete */
 
  }
  return 1;
}
 
but it doesn't work as, when I debug the code,I see "uu" at the 0x0870 address.
The CW command window also tell me " Warning: Invalid FLASH frequency selected.".
I'm using CW 5.1 with Device Initialization, the bus clock is 4.0 MHZ and, according to the MC9S08AW60 datasheet, I set the clock divider = 20 so the flash clock is 200 KHZ.
I'm quite confused on this issue and I'm spending a lot of time without any result.
Is this the way to follow or am I completely wrong ?
Someone who helps me.
 
Thanks.


Message Edited by dar77 on 2007-12-03 12:20 PM
0 Kudos

969 Views
bigmac
Specialist III
Hello,
 
It would seem the primary problem with your code is that it will be resident in flash, and the flash memory becomes inaccessible during erase or programming operations.  This is why the function code must be transferred to RAM, and run from there.  You may use either a dedicated block of RAM for this purpose, or alternatively run the code from the stack.  The previously referenced code attempts to do the latter.
 
Incidentally, even though this referenced code uses assembler, it is a relatively simple matter to interface with your C code.  In this case, the assembly code should remain in a separate .ASM file, and within your C code you would provide functions that call the assembly routines.
 
/* Assembly sub-routines */
extern void *FlashProg1( void);
extern void *FlashErase1( void);
 
/* Program flash byte */
byte FlashProg( byte *pntr, byte val)
{
   byte err;
   __asm {
      ldhx  pntr
      lda   val
      jsr   FlashProg1
      sta   err
   }
   return err;
}

/* Erase flash page */
byte FlashErase( byte *pntr)
{
   byte err;
   __asm {
      ldhx  pntr
      jsr   FlashErase1
      sta   err
   }
   return err;
}
 
The flash frequency divider may only be written once out of reset.  Are you attempting to set the value multiple times?  The following function should flag this situation.
 
byte Flash_init( byte value)
{
   if (FCDIV & 0x80)
      return 1;  /* Error return */
   FCDIV = value & 0x7F;
   return 0;  /* No error */
}
 
Regards,
Mac
 
0 Kudos

969 Views
dar77
Contributor I
Ok bigmac.
Thanks for your answer.
 
So I have a ASM file where I write the definitions of the following assembly functions: FlashProg1,FlashErase1,DoOnStack,SpSub. Right?
In this file how can I reference the Flash register with the same name of CW Device Initialization?
Where do I have to put the ASM file in CW ? In the sources folder ?
Do I have to add other files to my project to use the ASM functions ?
 
0 Kudos

969 Views
bigmac
Specialist III
Hello,
 
See if the attached untested CW project provides the answers to your queries.
 
Regards,
Mac
 
0 Kudos

969 Views
bigmac
Specialist III
Hello,
 
I just realised that the PRM file within the project that I posted was the default one.  A dedicated sector for the non-volatile data will be needed.  The attached PRM file assumes the first flash sector for an 'AW32 device will be used for this purpose.
 
Regards,
Mac
 
0 Kudos

969 Views
dar77
Contributor I
Ok.
Thanks for your .prm file.
But a part from this, I really don't understand the line in the main function:
 
temp = FlashProg((byte *)NV_dat, NV_buf[i]);
 
You try to write 4 bytes from NV_buf array to NV_dat address but NV_buf is empty.
So I tried to initialize the NV_buf array but when I debug the code I don't see the new data
in my Flash page (0x8000:0x81FF). I think that the data in the NV_dat array remain static
and don't change whitin the source.
Then I commented out these lines:
 
  for (i = 0; i < NVSIZE; i++) {
       temp = FlashProg((byte *)NV_dat, NV_buf[i]);
   }
   
leaving only the lines:
  
byte temp, i;
   // MCU_init();
   temp = Flash_init(DIVIDER_VAL);
   _FEED_COP();      /* feeds the dog */
   /* Load required NV data into NV_buf array here */
   temp = FlashErase((byte *)NV_dat);
    for ( ; ; ) {
       _FEED_COP();  /* feeds the dog */
   }                              /* loop forever */
 
but when I debug the code it doesn't erase the Flash page.
Can you give me some hints ?
 
Thanks.
0 Kudos

969 Views
bigmac
Specialist III
Hello,
 
Sorry, I forgot to increment the flash address pointer value - the line should read:
 
temp = FlashProg((byte *)NV_dat + i, NV_buf[i]);
 
The cast was needed to prevent a compiler warning (since NV_dat is defined as const byte).
 
I purposely initialized the NV_dat array to correspond to the unprogrammed state, but could be any sequence of values to be programmed at the time when the code is loaded to flash.  So, with the values that I used, you wouldn't see any change as a result of page erase operation.
 
It was intended that you would initialize the NV_buf array to what you required, thus my comment line.
 
The following recent thread may be of interest, especially message 10 by Peg -
 
Regards,
Mac
 
0 Kudos

969 Views
dar77
Contributor I
Sorry,
 
Perhaps I'm wrong but I'm not able to run well the code. I updated it with your new
line and I initialized the NV_buf array with four random values but I continue
to see the NV_dat values and not the NV_buf when I debug.
 
This is what I see in Flash:
 
8000 FF FF FF FF uu uu uu uu
8001 uu uu uu uu uu uu uu uu
8002 uu uu uu uu uu uu uu uu
.
.
.
.
.
81FF uu uu uu uu uu uu uu uu

Thanks.
0 Kudos

969 Views
bigmac
Specialist III
Hello,
 
Have you adjusted the BUSFREQ macro within flash.h to match your actual situation?  If you have the incorrect value here, and since the value of DIVIDER_VAL is being used by the code, this will result in an incorrect flash clock frequency.  If you have already calculated the value required for the FCDIV register, you might alternatively directly use this value as the parameter for the Flash_init() function.
 
Regards,
Mac
 


Message Edited by bigmac on 2007-12-06 06:10 AM
0 Kudos

969 Views
dar77
Contributor I
Hello.
 
I did, but all is the same. If I use Device Initialization I don't have to use the line:
 
temp = Flash_init(DIVIDER_VAL);
 
and I have to use the line:
 
MCU_init();
 
And I also have to comment out all the #define in the flash.h file.
Is it right?
 
Thanks.
0 Kudos

969 Views
bigmac
Specialist III
Hello,


dar77 wrote:
And I also have to comment out all the #define in the flash.h file.
Is it right?

No, not correct, but it should do no harm.
 
I suggest that you try the following sequence -
Close down the debugger, and run your program without the debugger.
Open the debugger and examine the contents of the flash block.
 
Regards,
Mac
 
0 Kudos

969 Views
dar77
Contributor I
Excuse me, but I didn't understand the sequence.
Could you give me more hints?
And, if I don't have a physical board and I only use Full Chip Simulation,
am I able to see the data changes in the CW Memory window ?
I really don't know how to go on.
 
Thanks.
0 Kudos

969 Views
bigmac
Specialist III
Hello,
 
I don't believe flash programming is simulated - perhaps other will correct me if I am wrong.  I would suggest that you need to have the real hardware in order to test the particular code.
 
Regards,
Mac
 
0 Kudos

969 Views
CrasyCat
Specialist III
Hello
 
You are right bigmac. Flash programming is not simulated.
You need to test that on real hardware.
 
CrasyCat
0 Kudos

969 Views
J2MEJediMaster
Specialist I
The App Note, Programming and Erasing FLASH and EEPROM Memories on the MC68HC908AS60A/AZ60A, explains how to program its flash and provides some source code. AN2156 can be downloaded from here.
0 Kudos

969 Views
bigmac
Specialist III
Hello,
 
The Application Note referenced in the previous post (for HC908 device) may not be appropriate for the later 9S08AW60 device.  The programming process is quite different for the latter.
 
The following recent thread contains assembly code that can be made to work -
 
The sub-routines mostly of interest are labelled -
FlashProg1
FlashErase1
DoOnStack
SpSub
 
Regards,
Mac
 
0 Kudos

969 Views
J2MEJediMaster
Specialist I
Whoops. OK, sorry for the faulty information. Good save, bigmac.

All those 8-bit parts look the same to me... :smileysad:

---Tom
0 Kudos