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 support@ccsinfo.com

PIC24F bootloader + AN1157

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



Joined: 25 Nov 2012
Posts: 4
Location: South Africa

View user's profile Send private message Visit poster's website

PIC24F bootloader + AN1157
PostPosted: Mon Nov 26, 2012 1:04 pm     Reply with quote

Hi all,

I'm trying to implement a serial port bootloader for a PIC24FJ64GA006 as per Microchip's app note AN1157 and using the PCD example (EX_PCD_Bootloader.c) as a starting point.

Bootloader starts at 0x400 and the App starts at 0xC00.

I'm using TeraTerm with XON/XOFF.

Problem
When prompted to send the HEX file, it sends data at a very slow speed (no where near 9600 baud) and after 1.2% it just hangs. After this I cannot run the bootloader again - I have to reprogram the PIC with an ICD3.

I've commented out the erase_program_memory section in loader_pcd.c since the memory will be erased prior to loading the bootloader code using an ICD3.

It almost seems like a flow control problem however I'm not convinced as I have used the exact TeraTerm setup to successfully bootload 18F devices in the past - maybe my code relocation isn't entirely correct?

I realise that there are already posts in this forum regrading PIC24F bootloaders (which I have read through), however I'm struggling with this problem and would really appreciate some advise as to where I'm going wrong...

Thanks,
Spero Colyvas

Bootloader Code:
Code:

#define LOADER_ADDR 0x400
#define LOADER_ADDR_END 0x490
#define APP_ADDR 0xC00

#include <24FJ64GA006.h>
#define PUSH_BUTTON PIN_E0
#use delay(clock=24000000)
#use rs232(baud=9600, xmit=PIN_F3, rcv=PIN_F2, uart1)      // Console UART
#fuses NOWDT,ICSP1,NODEBUG,NOWRT,NOPROTECT,NOJTAG,HS,PR

//#include "pcd_bootloader.h"
#include "loader_pcd.c"

#org LOADER_ADDR, LOADER_ADDR_END
void main(void) {
   setup_adc_ports(NO_ANALOGS);
// On powerup, if the designated button/pin is held low, the program
// will jump to the bootloader, allowing for serial programming.
   if(!input(PUSH_BUTTON)) {
//      #asm
//      goto LOADER_ADDR
//      #endasm
      printf("\n\rBootloader v3.00 - waiting for HEX file...");
      real_load_program();
      }

   else {
      #asm
      goto APP_ADDR
      #endasm
   }
}


Slightly modified loader_pcd.c code:
Code:
//#org default

//#org LOADER_ADDR+10, LOADER_END auto=0 default

void real_load_program (void){
   unsigned int1  do_ACKLOD, done=FALSE;
   unsigned int8  checksum, line_type;
   unsigned int16 l_addr,h_addr=0;
   unsigned int32 addr;
   #if getenv("FLASH_ERASE_SIZE")>2
      unsigned int32 next_addr;
   #endif
   unsigned int8  dataidx, i;
   unsigned int8 count=0;
   unsigned int8  data[32];
   unsigned int16  buffidx;
   char buffer[BUFFER_LEN_LOD];
   unsigned int32 address_erase;
//   for(address_erase=0;address_erase<(LOADER_ADDR);address_erase+=(getenv("FLASH_ERASE_SIZE")/2))
//  {
//      erase_program_memory(address_erase);
//   }

//   printf("\n\rErase complete, press any key\r\n");
//   getc();
//   printf("Waiting for program...\r\n");
   while (!done)  // Loop until the entire program is downloaded
   {
      buffidx = 0;  // Read into the buffer until 0x0D ('\r') is received or the buffer is full
      do {
         buffer[buffidx] = getc();
      } while ( (buffer[buffidx++] != '\r') && (buffidx <= BUFFER_LEN_LOD) );

      putchar (XOFF);  // Suspend sender

// Rest of the code below as per loader_pcd.c


App code:
Code:

// All sorts of #include above here
#define APP_START 0x0C00
#define APP_END 0x14FF
// Functions over here
#org 0x400, 0xBFF {}   // Reserve space for the bootloader
#org APP_START, APP_END
void main() {
// Main code here
Bill24



Joined: 30 Jun 2012
Posts: 45

View user's profile Send private message

PostPosted: Tue Nov 27, 2012 4:01 am     Reply with quote

The erase_program_memory() section in loader_pcd.c is required. Memory will be erased prior to loading the bootloader code using an ICD3, but then the boot loader writes to the flash. Then the next attempt to boot load will fail because the memory is no longer erased.

As for the speed problem, my guess is you are writing the HEX file line by line. I.e 32 bytes at a time. However for a PIC24 the flash page size is much larger ( I think 512 PIC24 words or 1536 bytes ) so you could buffer the incoming data and do less flash page writes which will speed thing up significantly.

If you application uses interrupts, they will be located in the interrupt vector table which is located before the boot loader. Hence this page has to be read to RAM, modified as required and the flash erased prior to a write back.

Your functions are placed prior to the #org statements so may get loaded into a flash address not included in your application space.

I would not jump to the application based only on the state of an input pin. The end of HEX file should be detected and if no errors have occurred in the whole download then set a success flag.


In short reuse as much of the code on this forum as possible.

Good Luck
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Tue Nov 27, 2012 6:38 am     Reply with quote

AN1157 is intending a failsafe bootloader and thus doesn't reprogram the interrupt vector, bootloader range and fuses memory range. The most simple way to map application interrupts is to use fixed addresses of interrupt procedures.

Regarding pcd_loader code I must confess that I didn't trace the changes that have been applied during the last years. I started with V4.078 which definitely had errors in the bootloader code, e.g. assuming wrong flash block sizes which had to be corrected. Also the operation of the built-in flash functions has been modified in details.

My general suggestion is to check operation of your bootloader with a hardware debugger, preferably MPLab. it should show you what's going wrong.
Spero



Joined: 25 Nov 2012
Posts: 4
Location: South Africa

View user's profile Send private message Visit poster's website

PostPosted: Tue Nov 27, 2012 12:39 pm     Reply with quote

Thanks for the input...starting to make sense now!
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