Writing to flash MC9S12DP256

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

Writing to flash MC9S12DP256

1,159 Views
Hannibal
Contributor I
I'm suing the folowing code to write to Flash, to address 0x308000, but was unable
to write the bute because of access error.


#include "stddefs.h"
#include

#define TABLE_ADDRESS_LOW 0x308000
#define TABLE_ADDRESS_HIGH 0x30FFFF

#pragma DATA_SEG FlashCtrl_DATA
#pragma CODE_SEG FlashCtrl_CODE

const volatile unsigned char far caliz2 @(0x308000) = 'B';

static Flash_Calibration[4];

void FlashCtrl_Init(void);


/* FlashCtrl_WriteWord */
unsigned char FlashCtrl_WriteWord(unsigned int *__far WriteAddress, unsigned int Data){

/* Check range of WriteAddress */
if(WriteAddress (unsigned int *__far)TABLE_ADDRESS_LOW || WriteAddress > (unsigned int *__far)TABLE_ADDRESS_HIGH)
return FALSE;

/* Has the FLASH divider clock been set— */
if(!FCLKDIV_FDIVLD)
FlashCtrl_Init();
//wb 0x100 CLKDIV // set FCLKDIV clock divider
//wb 0x103 0 // FCFNG select block 0
//wb 0x102 0x10 // set the WRALL bit in FTSTMOD to affect all blocks
//wb 0x104 0xa4 // FPROT all protection disabled
//wb 0x105 0x30 // clear PVIOL and ACCERR in FSTAT register
//ww 0xD000 0xFFFF // (dummy) write to flash array to buffer address and data
//wb 0x106 0x41 // write MASS ERASE command in FCMD register
//wb 0x105 0xC0 // clear CBEIF in FSTAT register to execute the command
//wait 10

FCNFG = 0x0;
FPROT = 0xA4;
FSTAT = 0x30;


/* Check command flag */
// if(!FSTAT_CBEIF)
// return FALSE;

/* Clear flags */
FSTAT = 48;

/* Array address and program data */
WriteAddress[0] = Data;
/* Word program command */
FCMD = 32;

/* Clear flag command buffer empty */
FSTAT = 128;

/* Is protection violation or acces error detected– */
if ((FSTAT_PVIOL == 1)||(FSTAT_ACCERR == 1))
return FALSE;

/* Wait to buffer empty */
while(FSTAT_CBEIF == 0);
/* Wait to command complete */
while(FSTAT_CCIF == 0);

/* Was write successful˜ */
if(WriteAddress[0] != Data)
return FALSE;

/* Return success */
return TRUE;
}

/* FlashCtrl_EraseSector */
unsigned char FlashCtrl_EraseSector(unsigned int *__far EraseAddress){

/* Check range of EraseAddress */
if(EraseAddress (unsigned int *__far)TABLE_ADDRESS_LOW || EraseAddress > (unsigned int *__far)TABLE_ADDRESS_HIGH)
return FALSE;

/* Has the FLASH divider clock been set™ */
if(!FCLKDIV_FDIVLD)
FlashCtrl_Init();

/* Check command flag */
if(!FSTAT_CBEIF)
return FALSE;

/* Clear flags */
FSTAT = 48;

/* Write to erase address */
EraseAddress[0] = 0x10;

/* Initiate Sector Erase commamd */
FCMD = 64;

/* Clear flag command buffer empty */
FSTAT = 128;

/* Is protection violation or access error detected ? */
if ((FSTAT_PVIOL == 1)||(FSTAT_ACCERR == 1))
/* If yes then error */
return FALSE;

/* Wait to buffer empty */
while(FSTAT_CBEIF == 0);
/* Wait to command complete */
while(FSTAT_CCIF == 0);

/* Return success */
return TRUE;
}

/* FlashCtrl_Init */
void FlashCtrl_Init(void)
{
/* 4mhz clock */
FCLKDIV = 20;
}

#pragma DATA_SEG DEFAULT
#pragma CODE_SEG DEFAULT
Labels (1)
0 Kudos
1 Reply

375 Views
NetGhost
Contributor I
Hi...
 
Perhaps you can look in codewarrior help.
 
There's a part named "How to Use Variables in EEPROM" where I found what I needed to write and read from eeprom.
Go to HELP - Contents - HC(S)12(X) - HC(S)12(X) Compiler - Appendices - Porting Tips and FAQs
 
 
Best Regards
 
Paulo
0 Kudos