IAP

Started by satkumar, August 14, 2010, 06:20:59 AM

Previous topic - Next topic

satkumar

 Hi,
We have problem about IAP in P89V51RD2. I have flashing the below code HEX file to P89V51RD2 chip. After reset the chip, the chip again not going to programming mode. I am not able to re-flash again. Please give me a solution. I am attaching the code here.
flash magic prompted a red message "reset the device into isp mode now"!!
#include "reg52.h"
#define uint     unsigned int
#define uchar    unsigned char
#define UINT8 unsigned char

typedef uint   WORD;
typedef uchar  BYTE;

UINT8 TestFlag;

#define SCON8B 0xd0
//#define SCON9B 0x50


sfr FCF    = 0xB1;
sfr FFS    = 0xB2;
sfr FAL    = 0xB3;
sfr FAH    = 0xB4;
sfr FDAT   = 0xB5;
sfr FST    = 0xB6;

/***************************************************
  name: IAP_Wait
  function: wait untill IAP operation finishing
  Input: none
  Output:none
***************************************************/
void IAP_Wait( void )
{
   while ( (FST & 0x04) != 0 );                     //check bit FST.2 1:busy 0:idle
}

/**********************************************************
name: IAP_Read(WORD addr)
  function: read the data at the flash address
  Input: flash address
  Output:data readed from flash
***********************************************************/
BYTE IAP_Read(WORD addr)
{
    BYTE c;
    FCF |= 0x40;                                  //SETB FCF.6
    FAH=addr>>8;                                  // get the higher 8 bit of the  address
    FAL=addr&0x00ff;                              //get the lower 8 bit of the  address
    FFS = 0x0C;                                   //select funtion number 3:read user code or data
    c = FDAT;                                     
    return c;                                     //rerun user code or data
}


/**********************************************************
  name: IAP_Program()
  function: program the data or code into specified flash address
  Input: flash address
  Output:status of IAP program operation 0: sucess,1:fail
***********************************************************/
bit IAP_Program(BYTE addr,BYTE dat)
{
   BYTE d;                                 

   FCF |= 0x40;                             //SETB FCF.6

    FAH=addr>>8;                                // get the higher 8 bit of the  address

    FAL=addr&0x00ff;                             //get the lower 8 bit of the  address
   FDAT = dat;
   FFS = 0x0E;                               //select IAP function number02 :program user code by byte
   IAP_Wait();                               //wait for finishing of IAP operation
   d = IAP_Read(addr);                       //verity the data or code with the byte which was programed just now
   if ( d == dat )
      return 0;
   else
      return 1;
}



/**********************************************************
  name: IAP_SectorErase( WORD sector )
  function: Erase the flash by sector
  Input: sector number  0< sector number <511
  Output:none
***********************************************************/
void IAP_SectorErase(WORD sector)
{
   WORD addr;                               
   if ( sector >= 512 ) return;
//   addr.i = sector * 128;
    addr=sector*128;
   FCF |= 0x40;                             //SETB FCF.6     
//   FAH = addr.CharPart.H;
    FAH = addr>>8;                           // get the higher 8 bit of the  address
//   FAL = addr.CharPart.L;
    FAL= addr&0x00ff;                        //get the lower 8 bit of the  address
   FFS = 0x0B;                         //select IAP function number 08:Erase user code in sector
   IAP_Wait();                        //wait for finishing of IAP operation
}



//function : send byte one time
void SendChar(unsigned char  c)
{
   SBUF = c;
   while (!TI);
   TI = 0;

}

//function: serial port intialize set buadroat at 57600
void Init_Serial(void)
{
   SCON = 0x50;//scon9b;   /*Timer 1 is being used to generate baud rates.*/
   TMOD = 0x21;   //T/C0 mode 1 timer; T/C1 mode2 timer
   TH1 = 0xFF;     //57600
   PCON = 0x80;   /* buad rate *2   */
   TR1 = 1;        /*TCON.6*/
   ES = 1;         /*IE.4      */
   IP = 0x0;
   EA=0;           // close interrupt

}



/* test the basic IAP operation of  P89V51RD2BN
   basic operation: program read erase
*/
void Function_Test( void )
{
   IAP_SectorErase(128);                       //Erase sector 128
   IAP_SectorErase(129);                     //Erase sector 129
   TestFlag=IAP_Program( 0x6000,0xaa );        // program  0xaa at 0x6000
   if( !TestFlag )
      {
         SendChar( 0xaa );                    // to notify that operation of programing ,vetifying ,reading are all correct
      }
   else
     {
         SendChar( 0xbb );                    //to notify that operation of IAP is failing
     }
}

 
void Delayms (unsigned int itime)   //TimeDelay
   {
   unsigned int j, i;
   for (i=0;i<itime;i++)
   for (j=0;j<127;j++);

   }



void main()
{
   Init_Serial();         //   initialize the serial com
   Function_Test();       // call the function to test the basic IAP operation
   while( 1 )
   {
      //SendChar( 0xcc );
      Delayms(1000);
   }
}


Please help me anyone