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

loader.c code clarification

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



Joined: 08 Aug 2012
Posts: 34

View user's profile Send private message

loader.c code clarification
PostPosted: Tue Apr 07, 2015 10:27 am     Reply with quote

My bootloader code started with the sample code provided by ccs.

While reviewing the code in loader.c , I am trying to understand the rationale for this CONDITION:
Code:
if (addr < LOADER_ADDR || addr > LOADER_END) && addr < 0x300000)

what is the rationale to write to program memory if the addr is less than the LOADER_ADDR???
Code:

#define LOADER_SIZE  0x5FF
#define LOADER_END   0x7FF

#define LOADER_ADDR LOADER_END-LOADER_SIZE

Shouldn't it always be:
Code:
if (addr > LOADER_END && addr < 0x300000)

I have a PIC18F2680... so 0x300000 is where the fuses are set.

--------------------------------------------------------------

I am referring to the following section of the code:

Code:
if (buffer[0] == ':') {
         count = atoi_b16 (&buffer[1]);  // Get the number of bytes from the buffer

         // Get the lower 16 bits of address
         l_addr = make16(atoi_b16(&buffer[3]),atoi_b16(&buffer[5]));

         line_type = atoi_b16 (&buffer[7]);

         addr = make32(h_addr,l_addr);
           
         #if defined(__PCM__)  // PIC16 uses word addresses
            addr /= 2;
         #endif

         // If the line type is 1, then data is done being sent
         if (line_type == 1) {
            done = TRUE;
            do_ACKLOD = TRUE;
         #if defined(__PCM__)
         } else if ((addr < LOADER_ADDR || addr > LOADER_END) && addr < 0x2000){
         #elif defined(__PCH__)
         } else if ((addr < LOADER_ADDR || addr > LOADER_END) && addr < 0x300000){
         #endif
            checksum = 0;  // Sum the bytes to find the check sum value
            for (i=1; i<(buffidx-3); i+=2)
               checksum += atoi_b16 (&buffer[i]);
            checksum = 0xFF - checksum + 1;
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Apr 07, 2015 12:40 pm     Reply with quote

I think you have an older version of the compiler. The latest version
uses getenv() to find the top of program memory:
Code:
 #define LOADER_END      getenv("PROGRAM_MEMORY")-1


The latest version has this:
Code:
  if ((addr < LOADER_ADDR || addr > LOADER_END) && addr < getenv("PROGRAM_MEMORY"))

That line just says to me: Don't overwrite the loader, and don't write
beyond the end of program memory.


Also, the latest version of loader.c puts the loader right up against
the end of Program Memory:
Code:
#define LOADER_END      getenv("PROGRAM_MEMORY")-1

// Code in here sets the loader size.   It's 0x3FF for PCH.

#define LOADER_ADDR  LOADER_END-LOADER_SIZE
wirelessage



Joined: 08 Aug 2012
Posts: 34

View user's profile Send private message

PostPosted: Tue Apr 07, 2015 3:18 pm     Reply with quote

PCM Programmer:

My loader is at the top of the program memory. Loader is from 0x200 to 0x7FF in my case. In your example, it looks to be at the bottom of the program memory.

So, question that comes to my mind:
Should it matter if it is at the top or bottom of program memory?

If the loader is at the top of program memory (like my code):

it should be:

Code:
if( addr > LOADER_END && addr < 0x300000 )
//PCH - PIC18F2680.
OR
Code:
if( addr > LOADER_END && addr < getenv("PROGRAM MEMORY") )



If the loader is at the bottom of program memory, it looks to me that the condition should be:
Code:
if( addr < LOADER_ADDR && addr < getenv("PROGRAM MEMORY") )



The "or" condition does not make sense to me.
Ttelmah



Joined: 11 Mar 2010
Posts: 19535

View user's profile Send private message

PostPosted: Wed Apr 08, 2015 1:43 am     Reply with quote

That is what the OR handles.

Think about it.

You need first to test that the address is <0x300000. Otherwise it is not writeable.

So if the address is less than this and is _either_ above the top of the loader (so handling a loader in the bottom of memory), _or_ below the bottom of the loader (for a loader at the top of memory), then the address is one that can be written. This is what the line as posted by PCM_programmer gives. Exactly right.

As written, it allows the loader to be _anywhere_ in memory. Top, bottom, middle etc., just blocking writes in the area where the loader sits.
wirelessage



Joined: 08 Aug 2012
Posts: 34

View user's profile Send private message

PostPosted: Mon Apr 13, 2015 1:16 pm     Reply with quote

Thanks a lot guys!

Good discussion.

Regards.
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