fred rouatbi

Writing to flash MC9S12DP256

Discussion created by fred rouatbi on Oct 27, 2006
Latest reply on Oct 27, 2006 by Paulo Proenca
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

Outcomes