Bootloader For 18F4520


Closed Thread
Results 1 to 37 of 37
  1. #1
    Join Date
    May 2004
    Location
    brighton
    Posts
    149

    Default Bootloader For 18F4520

    Hey All
    I have being off pic programming for quite a while now due to other commitments
    i have only just come back and wanted to continue with my beloved 18F452
    and my LAB-X1 Experimenter Board only to find that microchip are recommending the 18f4520 for new designs.
    my problem is that i use pbp 2.50 with microcode studio plus but the loader does not contain the bootloader code for the 18f4520 .
    i have emailed Mecanique about this since monday but they haven't so far replied to my email.
    i have done the online update but its still not there.
    I am presently going through the data sheet of the 18f4520 to see what are the things that i need to change in my code the one thing i never used in my program was the caparator all other things are just normal stuff like LCD, A/D
    i am i opening up a can of worms with the 18f4520 or is it as easy to use as the 18f452.

    Regards
    Isaac

  2. #2
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default

    Isaac,

    I have used the PIC18F252 (no zero at the end) bootloader from http://www.etc.ugal.ro/cchiculita/so...bootloader.htm
    on a PIC18F2520 chip, and it did work correctly. The site says you can use a PIC18F4520 with the same bootloader code. If you look on the page I linked, you will notice what different chips operate with their Tiny bootloader. This might mean that the same would be true with Micro Code Studio's bootloaders. You might try a couple close chips hex code from MCS, and see if it works.

    Or, you could use the Tiny bootloader instead. But I agree, it is nicer to be able to do it right from inside micro code studio.

    On a side note, with Tiny Bootloader they give you the assembly code for the bootloader, so you can add in two lines to change the hardware serial port to inverted. That way, you can do without a max232 chip.

    As far as the worms, I am not sure if it is a single serving, or family sized can. I have only used the PIC18F2520, and not the predecessors. But if you ever made the move from PIC16 to PIC18 devices, it's got to be a piece of cake in comparison.

    Walter
    Attached Images Attached Images  
    Last edited by ScaleRobotics; - 11th February 2010 at 00:53.

  3. #3
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,807


    Did you find this post helpful? Yes | No

    Default

    Remember that the bootloader cannot protect your code from a reading/copying.

    Ioannis

  4. #4
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default

    Ioannis, it is possible for your PBP application code to disable the bootloader by modifying it or erasing it. You can even change configuration registers at run time.


    You can get a pretty good level of code protection that way.

    I have military customers who want to do a code update, and when they are happy that the load went well, select a menu option. They then get code protection to the limit of what Microchip offers.
    Charles Linquist

  5. #5
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,807


    Did you find this post helpful? Yes | No

    Default

    Hi Charles. Thanks for the post. Can you elaborate on this?

    I was pretty sure that Bootloader and Secutity don't go together.

    Ioannis

  6. #6
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default

    I don't know about all the others, but the 18F series lets you modify FLASH at run-time. You have to first erase a block, (8-64 bytes, depending on your chip type - and you have to start at a block boundary) with the ERASECODE command. After erasure, you can write whatever you want.
    I did this in several versions or "levels". This is the lowest level.

    The following to allow the bootloader to run (or not) depending on a program variable. If you set PROTECT and called PatchLoader, the bootloader will not run. If in the program, you set PROTECT = 0, and run it, the bootloader will again work.

    It should be noted that this was used with PBP 250a and an 18F8722 and MCLOADER.

    PBP2.50a could not access the upper half of the big chips (>64K) memory using READCODE,WRITECODE or ERASECODE, so I had to modify PBPPPIC18.LIB to include a new SYSTEM variable called UPMEM. The library modification set TBLPTRU to UPMEM at the beginning of these 3 routines, and cleared UPMEM at the end (so as not to screw up PBP). By setting UPMEM to 1, I could read the upper half of memory (and by setting it to 0x30, you can set/read the configuration registers). You will see that I need to set upmem before each operation to get to that upper half of FLASH. You wouldn't have to deal with that in a smaller chip.

    I have done quite a bit of this, let me know if you need any more help.


    Code:
    PatchLoader:
    
              UPMEM = 0
          
             ERASECODE $0
           
             WRITECODE $0,$EFE0          ; Change the initial jump vector
             WRITECODE $2,$F0FF          ; 0X1FFC0
     
             WRITECODE $4,$FFFF
             WRITECODE $6,$FFFF
           
             for xx = $8 TO $3E STEP 2    ; Do the rest of the block
             WRITECODE XX,$FFFF
             NEXT Xx
             
             PAUSE 100
             
             PROTECT = 1
             GOSUB PROTECT_UNPROTECT 
    
             RETURN
    
    
    PROTECT_UNPROTECT:
    
             UPMEM = 1     
             ERASECODE $FFC0
            
             UPMEM = 1 
             WRITECODE $FFC0,$6B08
             UPMEM = 1 
             WRITECODE $FFC2,$0000
             UPMEM = 1 
             WRITECODE $FFC4,$0000
             UPMEM = 1 
             WRITECODE $FFC6,$0000
             UPMEM = 1 
             WRITECODE $FFC8,$3F08
             UPMEM = 1 
             WRITECODE $FFCA,$D7FB
             
           if PROTECT = 0 THEN        
                 UPMEM = 1 
                 WRITECODE $FFCC,$EF82
                 UPMEM = 1 
                 WRITECODE $FFCE,$F0FE    ; JUMP TO 1FD04
           ELSE  
                 UPMEM = 1 
                 WRITECODE $FFCC,$EF78
                 UPMEM = 1 
                 WRITECODE $FFCE,$F0FE    ; JUMP TO 1fcF0
           ENDIF
            
           FOR XX = $FFD0 TO $FFFE STEP 2
                 UPMEM = 1 
                 WRITECODE XX,$FFFF
           NEXT XX
             
             RETURN
    
    REPROTECT:
             UPMEM = 1
             READCODE $FFCC,X
               IF X = $82 THEN
                HSEROUT [13,10,10,"Reprotecting Loader",13,10]
                PROTECT = 1
                GOSUB PROTECT_UNPROTECT
               ENDIF 
              RETURN  
              
    GetTypeLoader:
             READCODE 0,TypeLoader
             RETURN
    Charles Linquist

  7. #7
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,807


    Did you find this post helpful? Yes | No

    Default

    Many thanks for the post.

    I will do some testing.

    Ioannis

  8. #8
    Join Date
    May 2004
    Location
    brighton
    Posts
    149


    Did you find this post helpful? Yes | No

    Default

    it looks like Mecanique wont support the 18f4520 as they never got back to
    me.
    Did anyone get the boot loaded from Mecanique ?
    Just really want to know


    Regards
    Isaac

  9. #9
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by isaac View Post
    it looks like Mecanique wont support the 18f4520 as they never got back to
    me.
    Did anyone get the boot loaded from Mecanique ?
    Just really want to know


    Regards
    Isaac
    OK, I originally posted away from my computer. My MCS has the 18F4520_20.hex file in it. I have MCS version 3.0.0.5. Which version do you have?

    I have just tried the 18F2520_20.hex file, and got it to work. (I don't have a 18F4520 to try).

    You should be able to update MCS over the internet. Not sure if that would update your .hex files, but I don't see why not.

  10. #10
    Join Date
    May 2004
    Location
    brighton
    Posts
    149


    Did you find this post helpful? Yes | No

    Default Bootloader For 18F4520

    Thanks for the info Walter

    My MCS dont have the 18F4520_20.hex file in it. I have MCS version 3.0.0.5

    nor the 18F2520_20.hex file

    i did the update MCS over the internet but it dont update the loader .hex files, but I don't see why
    Attached Images Attached Images  

  11. #11
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,807


    Did you find this post helpful? Yes | No

    Default

    Which version of PBP you have? Its the PBP that matters.

    Ioannis

  12. #12
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    MCS reads from the PBP directory for chips.
    What version of PBP do you have?
    Dave
    Always wear safety glasses while programming.

  13. #13
    Join Date
    May 2004
    Location
    brighton
    Posts
    149


    Did you find this post helpful? Yes | No

    Default Bootloader For 18F4520

    i have got pbp 2.50

  14. #14
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by isaac View Post
    i have got pbp 2.50
    Should be there.....
    Sounds stupid but maybe you just do not see it???
    Sometimes the chips are not in the order one would think...

    In the drop down box scroll all the way to the bottom and work your way up.
    ????
    Dave
    Always wear safety glasses while programming.

  15. #15
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default

    Dave,
    He's looking for the bootloader .hex file to be programmed into the chip - not the PBP "header files". Unfortunately the MCSP online updating doesn't update the .hex files for the bootloader application so you're stuck with the ones you got on the CD when you bought it. I was promised a new CD when I bought my last PBP update from Mecanique but it never came...

    /Henrik.

  16. #16
    Join Date
    May 2007
    Posts
    604


    Did you find this post helpful? Yes | No

    Default

    I have these if you are interested.

  17. #17
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by HenrikOlsson View Post
    Dave,
    He's looking for the bootloader .hex file to be programmed into the chip - not the PBP "header files". Unfortunately the MCSP online updating doesn't update the .hex files for the bootloader application so you're stuck with the ones you got on the CD when you bought it. I was promised a new CD when I bought my last PBP update from Mecanique but it never came...

    /Henrik.
    Sorry, my mistake..
    Dave
    Always wear safety glasses while programming.

  18. #18
    Join Date
    May 2004
    Location
    brighton
    Posts
    149


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by rmteo View Post
    I have these if you are interested.
    Cheers mate

    i am interested as Henrik said its the loader hex files that i can't update

    Regards
    Isaac

  19. #19
    Join Date
    May 2007
    Posts
    604


    Did you find this post helpful? Yes | No

    Default

    Isaac, I zipped the entire folder (all of the HEX files) and it is 113Kb. We just need to figure out how to get it to you.

  20. #20
    Join Date
    May 2007
    Posts
    604


    Did you find this post helpful? Yes | No

    Default

    Isaac, e-mail sent.

  21. #21
    Join Date
    May 2004
    Location
    brighton
    Posts
    149


    Did you find this post helpful? Yes | No

    Default Bootloader For 18F4520

    Quote Originally Posted by rmteo View Post
    Isaac, e-mail sent.
    you r great man

    Isaac

  22. #22
    Join Date
    Feb 2009
    Location
    Southern California
    Posts
    86


    Did you find this post helpful? Yes | No

    Default

    Anyone have any idea what needs to be done to modify either of the included files to run at 40 MHz instead of 4 or 20? I looked over both hex files and there are only a few lines difference between them, but I have no idea what I'm actually looking at.

    David

  23. #23
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Luckyborg View Post
    Anyone have any idea what needs to be done to modify either of the included files to run at 40 MHz instead of 4 or 20? I looked over both hex files and there are only a few lines difference between them, but I have no idea what I'm actually looking at.

    David
    MCS has not provided any source code (that I am aware of) for their bootloader, so I think it would be pretty hard to modify it to run at double the speed. If you don't mind it not working directly through the MCS interface, then you could modify the assembly source code given by the Tiny Pic Bootloader, located here: http://www.etc.ugal.ro/cchiculita/so...bootloader.htm

    And run it beside your MCS.

    .... But Maybe,
    To still use MCS's bootloader hex, you could think about switching PLL settings on the fly in your code. See: http://www.picbasic.co.uk/forum/showthread.php?t=4093
    Maybe you could set the low power interrupt to change it back to 20 mhz when it powers down? Kind of backwards, but might work.

    Walter

  24. #24
    Join Date
    Feb 2009
    Location
    Southern California
    Posts
    86


    Did you find this post helpful? Yes | No

    Default

    I've looked into this a little, but as I have never used mplab and try to avoid assembly my first attempts have failed. If no one has any better ideas, I'll try to look into it more next week. Thanks for the help

    David

  25. #25
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default 20mhz bootloader and 48mhz code

    Ok, this works for me. I'm using microcode stuidios 18f4550 20mhz hex bootloader file, and change the speed to 48mhz on the fly using Darrel's RTconfig.inc file.

    Will post code tonight for a 18f4520

    Code:
    DEFINE OSC 48	' Lets work at 48 MHz with our 20mhz bootloader!
    define LOADER_USED 1
    @ #include "RTconfig.inc"  ;Darrel's write to configs while running inc
    tempbyte var byte
    redled		var PORTD.1
    
    ;----Change back to low speed for bootloader at reset
    @  ReadConfig?CB  _CONFIG1L, _tempbyte    ; Read  CONFIG1L
    tempbyte.0=1
    tempbyte.1=1
    @  WriteConfig?CB  _CONFIG1L, _tempbyte   ; Write CONFIG1L
    @  ReadConfig?CB  _CONFIG1H, _tempbyte    ; Read  CONFIG1H
    tempbyte.1=0
    @  WriteConfig?CB  _CONFIG1H, _tempbyte   ; Write CONFIG1H
    
    pause 1000   ; Give bootloader time to do it's thing 
    ;----Accelerate up to 48mhz for your code
    @  ReadConfig?CB  _CONFIG1L, _tempbyte    ; Read  CONFIG1L
    tempbyte.0=0
    tempbyte.1=0
    @  WriteConfig?CB  _CONFIG1L, _tempbyte   ; Write  CONFIG1L
    @  ReadConfig?CB  _CONFIG1H, _tempbyte    ; Read  CONFIG1H
    tempbyte.1=1
    @  WriteConfig?CB  _CONFIG1H, _tempbyte
    
    main:
    	pause 500
    	high redled
    	pause 500
    	low  redled
    	goto main
    end
    Last edited by ScaleRobotics; - 20th March 2010 at 18:11.

  26. #26
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default Using other than 4 and 20mhz for MCS bootloader hex

    Well, the 18F4520 is a little harder, because it has a 4x pll with no pre/post scalers. Since your only external OSC options using the MCS bootloader hex files (as is) is either 4mhz, or 20mhz, that does not help.

    So I wanted to try a 10mhz crystal. The closest I had was 8mhz. But I had some luck with that.
    Checking the difference between the two hex files (4mhz and 20mhz) using the PicKit2, there were two addresses that differed. The first one (7D06) is where the baud speed is set. For a 4mhz crystal, this should be set to 0E0C hex, with 0E as an assembly command, and the last 0C as the SPBRG setting. To change this for a 10mhz chip (without 4xpll for now), should be 0E20.



    Setting this to 0E19 was correct for my 8mhz crystal, and allowed me to use the MCS bootloader hex at 8mhz. That's promising.

    Now here is where it gets a little fuzzier. The only other address that changes from 4mhz to 20mhz is 7E70. For a 4mhz, it is set to 0F, for a 20mhz it is set to 1F. It looks like it sends this to the serial port, from what I can make out of the disassembler I used. Not sure what it does with this info.....

    I tried setting everything for 8mhz with 4xpll directly using the PicKit2, but the bootloader would not work. I have not tried doing it with run time configs. And I may well be missing a setting.

    I have a little more playing to do. Maybe you can test it running at 10mhz as a first step. If that worked, we could try to do something like I did above with the 18f4550, and switch speeds using Darrel's run time config include file.

    Walter
    Attached Images Attached Images  
    Last edited by ScaleRobotics; - 22nd March 2010 at 08:32.

  27. #27
    Join Date
    Feb 2009
    Location
    Southern California
    Posts
    86


    Did you find this post helpful? Yes | No

    Default

    I ran into a couple little snags when attempting your solution. the first being the melabs programmer doesn't allow directly modifying the hex file, so I had to use notepad and discovered it is all stored in Little-Endian but I actually got to remember something from college after 10 years So I think I did it correctly other than my check sum was no longer good, but hopefully it is OK.

    My second problem is my chip doesn't use config1L so I got a bunch of errors and will have to compare data sheets later to figure out what you were doing in your example. Unfortunately I need to be productive for the next few hours and will have to get into this later today. I'll let you know if I have any luck

    Thanks for your help

    David

  28. #28
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default

    Hey David,

    Well I was thinking of just trying the 10mhz part first, and trying to make a 10mhz bootloader hex. Let me modify it for you, and send it your way. That will be the first step. If we can get that going, we can try to do something similar to what was done on the 18F4550, only, yes, we would have to modify the config settings to match the 18f4520.

    Walter

  29. #29
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default

    It looks to be pretty easy to modify the baud rate of the mcLoader hex files, at least with the PICkit2. I changed the CONFIG1H setting for HS 4x PLL, and also modified the 7D06 address with the SPBRG (BRGH=1) setting for 19,200 baud rate for my 8mhz x 4 crystal. I left address 7E70 at 0F, and it works with the loader program, while running at 32mhz. Should be able to do the same with a 10mhz crystal at 4x PLL.

    David, I sent you a 40 mhz version to try.

    Walter

  30. #30
    Join Date
    Feb 2009
    Location
    Southern California
    Posts
    86


    Did you find this post helpful? Yes | No

    Default

    Thanks for the solution it seems to be working great.

    I know there have been a few announcements recently about copyright protections, does anyone know if it is legal to post the solution for others to use? Walter and I both seem that we should be able to, but I want to make sure I'm following the rules.

    David

  31. #31
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Luckyborg View Post
    Walter and I both seem that we should be able to, but I want to make sure I'm following the rules.
    I think that Mechanique SHOULD let us, since it is just the hex files for the mcLoader. And since their update does not seem to update the hex files for most people, it seems like they should be made available. No one can do a thing with them without the mcloader executable, so as long as that was not posted, it seems like it would be good customer service to make the hex files available.

    However, since I do not see any postings of any of their mcloader hex files, I am assuming this is a no-no.

    Walter

  32. #32
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Send an email to Jeff and Charles...??
    Dave
    Always wear safety glasses while programming.

  33. #33
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mackrackit View Post
    Send an email to Jeff and Charles...??
    I thought Charles was just on the MeLabs side of things and that Mechanique was a different company all together? Pardon my ignorance, but who is Jeff? (No offence Jeff).

    Walter

  34. #34
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    OOPPS...

    Wrong company.

    Jeff is with MeLabs also.
    Dave
    Always wear safety glasses while programming.

  35. #35
    Join Date
    Feb 2009
    Location
    Southern California
    Posts
    86


    Did you find this post helpful? Yes | No

    Default

    Unfortunately Mecanique doesn't seem to respond to any emails based on my experience and other threads I've read here in the forum. On their website they claim they will provide custom loader files free of charge. However if they had done that, then Walter wouldn't have needed to help me out in the first place. Oh Well

    David

  36. #36
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default

    I went ahead and wrote Mechanique. We will see what they say.

    Walter

  37. #37
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default

    Ok, I guess you are right! Ask and you shall receive!

    I just received an email from David Barker at Mechanique, giving me permission to post the hex files. So here is just two of them that i have done for now. I will post more later.

    http://www.scalerobotics.com/18f4520_40.hex.txt

    http://www.scalerobotics.com/18f4520_10a.hex.txt

    Let me know if there are any issues.

    See the wiki here: http://www.picbasic.co.uk/forum/cont...Bootloader-Hex

    Walter
    Last edited by ScaleRobotics; - 20th July 2010 at 17:03.

Similar Threads

  1. PIC18F4680 bootloader
    By vinyl_theif in forum General
    Replies: 1
    Last Post: - 29th January 2009, 17:45
  2. 18F4550 Bootloader enter via eeprom setting
    By bradb in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 22nd November 2008, 23:51
  3. USBDemo with Bootloader
    By vacpress in forum USB
    Replies: 4
    Last Post: - 25th January 2007, 22:29
  4. Bootloader Problems
    By rossfree in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 11th February 2005, 17:51
  5. Replies: 3
    Last Post: - 26th January 2005, 13:41

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts