about the IAP function

Started by yyf10601, August 30, 2007, 06:06:32 AM

Previous topic - Next topic

yyf10601

When  I program the P89V51RD2 with its ISP via scom ,It worked well.
But When I program the program via the IAP function routine through SCOM  ,it doesn't work . it notify that "unable to connect at the specifed rate,try reducint the baut rate and reset the device into ISP mode ". I can't enter ISP mode  and can't program via scom any more
I locate function Function_Test at 0x4000.
I don't know what happened .


Attach hex file is the file of problem

So any one help me .Thanks in advance.
/* Here is my program*/

#include " reg52.h"
#define uint     unsigned int
#define uchar    unsigned char
typedef uint   WORD
typedef uchar  BYTE

#define SCON8B 0xd0
#define SCON9B 0xf0


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 = 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 main()
{
   Init_Serial();         //   initialize the serial com
   Function_Test();       // call the function to test the basic IAP operation
   while( 1 )
   {
      SendChar( 0xcc );
      Delayms( 1000);
   }
}

Jan Waclawek

#1
If you run IAP functions from Block0, they work on Block1.
In other words, you have overwritten parts of your bootloader.
This is why you should use the bootloader's IAP API to get portions of Block0 reprogrammed (the bootloader runs from Block1 so it can modify Block0).

You need to reprogram the bootloader in a parallel programmer now.

Jan Waclawek

PS. You should better come to 8052.com forum to discuss this, as this is not really FlashMagic related.
PS2. What, finally somebody believes me that for documentation on Philips/NXP chip one has to go to SST...

sanketbarot

If I am not wrong Mr. jan then does it mean that the code in Block0 and when Try to perfor the IAP at that time it is overlayed by the IAP rotuines...

so rather performing parallel proag.. can we load the code directly from address 2000H in the flash???

Jan Waclawek

Quote from: sanketbarot on September 17, 2007, 12:06:29 AM
If I am not wrong Mr. jan then does it mean that the code in Block0 and when Try to perfor the IAP at that time it is overlayed by the IAP rotuines...

so rather performing parallel proag.. can we load the code directly from address 2000H in the flash???

I don't understand your question.

Can you please rephrase it, perhaps giving more details?

JW

sanketbarot

Sorry Brother I understand it wrongly.....