Author Topic: PIC chip bootloader problem  (Read 2183 times)

0 Members and 1 Guest are viewing this topic.

Offline VK3DRBTopic starter

  • Super Contributor
  • ***
  • Posts: 2252
  • Country: au
PIC chip bootloader problem
« on: October 13, 2016, 02:39:58 am »
Hi.

I am writing bootloader for the PIC 16F1947 written in C. I have never written a bootloader for the PIC chip. The  challenge here is getting the bootloader to changing the instruction pointer to point to a new section of flash. I plan to put the bootloader in upper section of flash, and the application is compiled as per normal with no code offset. Or I can have the bootloader near the start and compile the application to run form a higher section of flash. Does anyone have experience how to do a far jump in C for the PIC mid range, so the bootloader can switch to the application? I am having troubles with the XC8 compiler using in-line assembly code. And the debugger is pretty crappy, using a PICkit/3.

Maybe I should go back to the old MPLAB 8 with the Hi-tech C compiler.

Any suggestions so I can get started? are there any boot loaders off-the-shelf for the 16F1947?

Thanks!

 

Offline VK3DRBTopic starter

  • Super Contributor
  • ***
  • Posts: 2252
  • Country: au
Re: PIC chip bootloader problem
« Reply #1 on: October 13, 2016, 07:16:45 am »
MCC's code generator for the bootloader is seriously stuffed. I cannot get it working and I don't know if anyone else can. I think the easiest is to write my own.

Maybe all I need is how to do a longjump in C to an absolute address (the PIC16f1947 Hi-tech C compiler does not support this), or in inline assembly code - either using the crappy XC8 under MPLAB X bloatware, or better still under the old MPLAB 8.x with Hi-Tech C.

Its a bloody dogs breakfast. I wish I had used Atmel.   
 

Offline mikeselectricstuff

  • Super Contributor
  • ***
  • Posts: 13748
  • Country: gb
    • Mike's Electric Stuff
Re: PIC chip bootloader problem
« Reply #2 on: October 13, 2016, 08:29:45 am »
On XC8 it's pretty straightforward but you do need to work round some quirks.
Compile your main code with the codeoffset linker setting as required.
Here's an example of the vectors in my bootloader for an 16F1823 - this only enters the bootloader on a power-on reset. (One option to enter your main code is to just force a watchdog reset)

 void bootvecs(void) @0x00
{
#asm
btfss 3,4 // status, /TO
goto 0x200 // watchdog timeout - go to app code
goto _main
nop
MOVLP 2
goto 0x204 // int code fixup
#endasm      
   }

The only issue is forcing the compiler to put the code where you want it - you need to check the assembler listing, as it sometimes ignores the @xxx directive.
You need to put the snippet above as the first procedure in the source file, otherwise it will put other code there and ignore the @ directive
You also need to put a call to the code to prevent the compiler from optimising it out - just set up a call that the compiler can't figure out will never get called.
e.g.
void main(void) @0x08
{
..your bootloader
} while(1);

bootvecs(); // force inclusion of bootvecs - optimised out otherwise
   
   }

One thing I've never managed to achieve for various obscure reasons is have the main code and bootloader in the same source project. One issue is  the @ operator overrides codeoffset but it can get itself confused.

Youtube channel:Taking wierd stuff apart. Very apart.
Mike's Electric Stuff: High voltage, vintage electronics etc.
Day Job: Mostly LEDs
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf