CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

What does this code mean?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Christophe



Joined: 10 May 2005
Posts: 323
Location: Belgium

View user's profile Send private message

What does this code mean?
PostPosted: Tue Apr 07, 2009 9:14 am     Reply with quote

Hi,

this piece of code is at the end of my bootloader. It should reset the PIC but it doesn't. Can you help me understand what these commands mean? I ported the code from PIC16LF877A to PIC16F886:

Code:

UserBootVector:
#asm                      // This is a reboot command written in Assembly.
      MOVLW   0x00        // these four commands will be overwritten with the boot vector we obtain from the HEX file.
      MOVWF   0x0A
      GOTO   0x00
      NOP
      NOP
#endasm
asmallri



Joined: 12 Aug 2004
Posts: 1634
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

Re: What does this code mean?
PostPosted: Tue Apr 07, 2009 9:37 am     Reply with quote

Christophe wrote:
Hi,

this piece of code is at the end of my bootloader. It should reset the PIC but it doesn't. Can you help me understand what these commands mean? I ported the code from PIC16LF877A to PIC16F886:

Code:

UserBootVector:
#asm                      // This is a reboot command written in Assembly.
      MOVLW   0x00        // these four commands will be overwritten with the boot vector we obtain from the HEX file.
      MOVWF   0x0A
      GOTO   0x00
      NOP
      NOP
#endasm


Well it will not help you because as the comment says, this code is overwritten by the user reset vector information extracted from the target applications hex file.

Never the less... The PIC16F does not have a reset instruction so to emulate a reset you need to setup the high program counter byte latch (PCLATH) at 0x0A to point to low memory. After you set PCLATH the next call or goto instruction executed will load this value into the high order of the program counter so goto 0x00 is really goto PCLATH | 0x00
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
Christophe



Joined: 10 May 2005
Posts: 323
Location: Belgium

View user's profile Send private message

PostPosted: Tue Apr 07, 2009 9:42 am     Reply with quote

I don't understand why my application does not reset after writing to the program memory. Only after pressing the MCLR (reset), the bootvector from the program HEX file is loaded or the program is launched.

Here's the code:

Code:

#include <16F886.H>
#device *=16
//#case
#fuses XT, PUT, NOWDT, NOPROTECT, NOLVP, NOCPD, NOWRT, BROWNOUT
#define READY_FOR_NEXT  0x11                                   // define possible reply bytes
#define FINISH_FLAG     0x55
#define BOOTLOADER_OVERWRITE  0x80
#define SerBufferSize 45                                       // serial input buffer size

#include "GOGOLOADER.H"
#ORG LOADER_BEGIN, LOADER_END auto=0 default                   // #org tells the compiler where to put the procedures in memory
#use DELAY (clock=4000000)
#use rs232 (baud = 19200, parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)

unsigned int a2i (unsigned char asciiByte)                     // a slim version of atoi().converts ascii text to integer
{                                                              // i.e. '1' = 1, 'A' = 10
      if (asciiByte >= 'A' && asciiByte <= 'F')
         return((asciiByte) - 'A' + 10);
      else if (asciiByte >= '0' && asciiByte <= '9')
         return ( asciiByte - '0');
}

unsigned int read8 ()
{                                                              // convert two ascii text to a 8 bit integer
     return( (a2i(getc()) << 4) + (a2i(getc())) );
}

void main ()
{
   unsigned int16 Buffer[SerBufferSize];                       // serial input buffer
   int1 notDone = 1, CODE_TOO_LARGE = FALSE;
   unsigned int recLen;                                        // HEX file record length
   unsigned int16 writeAddr, timeout = 0;                          // HEX file write address
   unsigned char recType;                                      // HEX file record type
   unsigned char i=0,j, triggers_found = 0;                    // general counters
   unsigned int16 UserBootVectorAddr;                          // holds the address of our new boot vector
   unsigned int16 User_BootVector [4] = { 0,0,0,0 };

   for ( i = 2; i > 0 ; i-- )
   {
      while ( !kbhit () && (++timeout < 0xFFFF) )               // 2 Seconden tijd om downloadcommando te sturen: 0xAA , 0xAA
         delay_us (16);

      if ( kbhit () )
      {
         if ( getc () == 170 )
            triggers_found++;
         else
            break;
      }
   }

   if ( triggers_found == 2 )
   {
      putc ('L');
      UserBootVectorAddr=label_address(UserBootVector);           // Get the adress in the bootloader where the reset vector will be
      while (notDone)
      {
         while (getc() != ':') ;                                  // Wait for ':'
         recLen = read8();                                        // Hex file Record length
         recLen >>= 1;                                            // we divided the Length by 2.
                                                                  // Each memory location on the PIC (16 bit) is
                                                                  // twice the unit size of the HEX file (8 bit).
         writeAddr  = ((int16)read8() << 8) + read8();            // Write Address
         writeAddr >>= 1;                                         // divide by 2
                                                                  // The physical address on the PIC is half the address in the HEX file.
         getc ();                                                 // ignore the first digit, it is always '0'
         recType = getc();                                        // Get the Rec Type

         if (recType == '1')                                      // End of file record
            notDone = 0;
         else if (recType == '0')                                 // data record: get the data
         {
            for (i=0; i < recLen ; i++)
                Buffer[i] = read8() + ((int16)read8() << 8);

            if ((writeAddr >= 0x2100) && (writeAddr <= 0x21FF))      // if data is in the EEPROM area
               write_eeprom((int) writeAddr, (int) Buffer[i]);

            else if ((writeAddr >= 0x2000) && (writeAddr <= 0x20FF)) // else if data is in the Configuration register area
            {}                                                        // Can't write configuration registers -> just skip.

            else if ( (writeAddr >= LOADER_BEGIN) && (writeAddr <= LOADER_END))   // else if data overlaps the bootloader code -> halt
            {
               CODE_TOO_LARGE = TRUE;
               break;
            }
            else
            {                                                      // else -> data is in program area
               for (i=0; i < recLen ;i++)
               {
                  if (!CODE_TOO_LARGE)
                  {
                     if ((writeAddr < 0x004) && (i<4) )
                     {
                        User_BootVector[i] = Buffer[i];
                     }
                     else
                        write_program_eeprom (writeAddr + i, Buffer[i]);
                  }
               }
            }
            putc (READY_FOR_NEXT);                                // Tells the PC to send the next line
         }
      }
      if ( CODE_TOO_LARGE )
         putc ( BOOTLOADER_OVERWRITE );
      else
      {
         for ( i=0; i < 4 ;i++)
              write_program_eeprom (UserBootVectorAddr + i, User_BootVector[i] );
         putc ( FINISH_FLAG );                                    // Tells the PC that we are finished
      }
      for (j=0;j<255;j++)                    // this loop gives the chip time to finish sending the serial byte before resetting it self.
      {
         for (i=0;i<255;i++) {};
      }
      //delay_ms ( 2 );
   } // While notdone

UserBootVector:
#asm                      // This is a reboot command written in Assembly.
      MOVLW   0x00        // these four commands will be overwritten with the boot vector we obtain from the HEX file.
      MOVWF   0x0A
      GOTO   0x00
      NOP
      NOP
#endasm
}// main
#ORG default
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group