zheng xuguang

problem about flash programing on 9S12XDT256! Thanks all!

Discussion created by zheng xuguang on Oct 20, 2011
Latest reply on Oct 20, 2011 by kef

hi:

       I wrote a flash driver like this:

In FlashDrv.c

     

#pragma CONST_SEG __GPAGE_SEG SYS_InfoFlash
    volatile const uint16  FLASHDrv_dataTestUB; //定义在这里,用于占位   
#pragma CONST_SEG DEFAULT


void  FLASHDrv_Init(void)
{
    if(FCLKDIV_FDIVLD == 0)
    {
        FCLKDIV = 39;
    }
}

uint8  FLASHDrv_ProgramOneWord(uint16 * __far addrFLASHPUW , uint16 dataProgramUW)
{
   if(FSTAT_ACCERR == 1)
   {
      FSTAT = 0x10;
   }
   if(FSTAT_PVIOL == 1)
   {
        FSTAT = 0x20;
   }

   while (!FSTAT_CBEIF)

   FSTAT = 0x30;
   *addrFLASHPUW = dataProgramUW;
   FCMD = 0x20;
   FSTAT = FSTAT | 0x80;
   while (!FSTAT_CCIF)
   {
      if ((FSTAT_ACCERR == 1) || (FSTAT_PVIOL == 1))
      {
          return 0;
      }
   }
      return  1;
}

uint8  FLASHDrv_SectorErase(uint16 * __far addrFLASHPUW)
{
    if (FSTAT_ACCERR == 1)
  {
      FSTAT = 0x10;
  }
  if (FSTAT_PVIOL == 1)
  {
      FSTAT = 0x20;
  }
  
  while (!FSTAT_CBEIF)
  
  FSTAT = 0x30;
  *addrFLASHPUW = 0xffff;
  FCMD = 0x40;
  FSTAT = FSTAT | 0x80;
  while (!FSTAT_CCIF)
  {
     if ((FSTAT_ACCERR == 1) || (FSTAT_PVIOL == 1))
     {
        return 0;
     }
  }
  return 1;
}


uint8  FLASHDrv_MassWrite(uint16 * __far addrFLASHPUW, uint16 numByteUW, uint16 * __far addrDataPUW)
{
    uint16  numCycleCntUW;
    uint16 *__far  addrFlashArrayPUW = addrFLASHPUW;
    uint16 *__far  addrDataArrayPUW = addrDataPUW;
    uint16 numDataUW = *addrDataArrayPUW;
    uint8  numReturn = 1;
   
    if((((uint32)addrFLASHPUW%1024)==0)&&(numByteUW>0)) 

    {
        if(FLASHDrv_SectorErase(addrFLASHPUW))

        {
            if(numByteUW <= 2)
            {
                if(FLASHDrv_ProgramOneWord(addrFlashArrayPUW , numDataUW))
                    numReturn = 1;
                else
                    numReturn = 0;   
            }
            else
            {
                for(numCycleCntUW=2;numCycleCntUW<=numByteUW:smileywink:
                {
                    numCycleCntUW += 2;
                    if(FLASHDrv_ProgramOneWord(addrFlashArrayPUW , numDataUW))
                    {
                        addrFlashArrayPUW++;
                        addrDataArrayPUW++;
                        numDataUW = *addrDataArrayPUW;
                    }
                    else
                    {
                        numReturn = 0;
                        break;
                    }    
                }
            }
        }
        else
        {
            numReturn = 0;
        }       
    }   
    return  numReturn;   
}

 

In prm file

 

SYS_InfoFlash     INTO  PAGE_FC;

 

In main.c

 

typedef struct
{
    uint16   a;
    uint8    b;
    sint16   c;
    uint16   d;
    uint16   e;
    uint8    f;
    uint8    g;
    uint32   h;
   
}flashteststruct;

flashteststruct     flashtest;
flashteststruct     flashtest2;

 
void main(void)

    FLASHDrv_Init();
   
    flashtest.a = 1;
    flashtest.b = 3;
    flashtest.c = 0xfa03;
    flashtest.d = 0xc005;
    flashtest.e = 0xe043;
    flashtest.f = 0xbb;
    flashtest.g = 0xdc;
    flashtest.h = 0x1fa45;
           
    FLASHDrv_MassWrite((uint16 *__far)&FLASHDrv_dataTestUB,sizeof(flashteststruct), (uint16 *__far)&flashtest);
    FLASHDrv_MassRead((uint8 *__far)&FLASHDrv_dataTestUB,sizeof(flashteststruct), (uint8 *__far)&flashtest2);

    for(;:smileywink:
    {

    }

 

my problem is when  i run this program , the program can not run into for(;:smileywink: loop , the program is error, but when i allocate

SYS_InfoFlash     INTO  PAGE_E2; in prm file, the program run ok. i debuged this program in step mode, i found function FLASHDrv_MassWrite((uint16 *__far)&FLASHDrv_dataTestUB,sizeof(flashteststruct), (uint16 *__far)&flashtest) can't run correctly when i allocate SYS_InfoFlash     INTO  PAGE_FC. the program error is in function if(FLASHDrv_SectorErase(addrFLASHPUW)) .

        another problem is when i changeed function uint8  FLASHDrv_SectorErase(uint16 * __far addrFLASHPUW) into

uint8  FLASHDrv_SectorErase(uint8 * __far addrFLASHPUW), no matter i allocate SYS_InfoFlash     INTO  PAGE_FC or

allocate SYS_InfoFlash     INTO  PAGE_E2,  in two cases , the program all run correctly.

       can anyone help me about this program,
                                                                  thanks!

Outcomes