View Full Version : Bootloader For 18F4520
isaac
- 10th February 2010, 21:11
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
ScaleRobotics
- 11th February 2010, 00:41
Isaac,
I have used the PIC18F252 (no zero at the end) bootloader from http://www.etc.ugal.ro/cchiculita/software/picbootloader.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
Ioannis
- 11th February 2010, 09:58
Remember that the bootloader cannot protect your code from a reading/copying.
Ioannis
Charles Linquis
- 11th February 2010, 13:36
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.
Ioannis
- 12th February 2010, 07:59
Hi Charles. Thanks for the post. Can you elaborate on this?
I was pretty sure that Bootloader and Secutity don't go together.
Ioannis
Charles Linquis
- 12th February 2010, 13:49
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.
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
Ioannis
- 12th February 2010, 14:31
Many thanks for the post.
I will do some testing.
Ioannis
isaac
- 12th February 2010, 21:35
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
ScaleRobotics
- 13th February 2010, 04:06
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.
isaac
- 13th February 2010, 10:54
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
Ioannis
- 13th February 2010, 11:34
Which version of PBP you have? Its the PBP that matters.
Ioannis
mackrackit
- 13th February 2010, 11:35
MCS reads from the PBP directory for chips.
What version of PBP do you have?
isaac
- 13th February 2010, 13:58
i have got pbp 2.50
mackrackit
- 13th February 2010, 16:09
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.
????
HenrikOlsson
- 13th February 2010, 16:28
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.
rmteo
- 13th February 2010, 17:03
I have these if you are interested.
http://home.earthlink.net/~rmteo/sitebuildercontent/sitebuilderpictures/a_69q.jpg
mackrackit
- 13th February 2010, 17:07
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.
:o Sorry, my mistake..
isaac
- 13th February 2010, 17:46
I have these if you are interested.
http://home.earthlink.net/~rmteo/sitebuildercontent/sitebuilderpictures/a_69q.jpg
Cheers mate
i am interested as Henrik said its the loader hex files that i can't update
Regards
Isaac
rmteo
- 13th February 2010, 18:50
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.
rmteo
- 14th February 2010, 00:29
Isaac, e-mail sent.
isaac
- 15th February 2010, 14:40
Isaac, e-mail sent.
you r great man
Isaac
Luckyborg
- 19th March 2010, 18:37
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
ScaleRobotics
- 19th March 2010, 20:11
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/software/picbootloader.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
Luckyborg
- 19th March 2010, 20:59
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
ScaleRobotics
- 20th March 2010, 17:15
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
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
ScaleRobotics
- 22nd March 2010, 08:27
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.
http://www.picbasic.co.uk/forum/attachment.php?attachmentid=4104&stc=1&d=1269246294
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
Luckyborg
- 22nd March 2010, 18:01
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 :rolleyes: 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
ScaleRobotics
- 22nd March 2010, 18:10
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
ScaleRobotics
- 24th March 2010, 08:00
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
Luckyborg
- 24th March 2010, 16:27
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
ScaleRobotics
- 24th March 2010, 16:56
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
mackrackit
- 24th March 2010, 17:06
Send an email to Jeff and Charles...??
ScaleRobotics
- 24th March 2010, 17:12
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
mackrackit
- 24th March 2010, 17:14
OOPPS...
Wrong company.
Jeff is with MeLabs also.
Luckyborg
- 24th March 2010, 17:39
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
ScaleRobotics
- 24th March 2010, 17:47
I went ahead and wrote Mechanique. We will see what they say.
Walter
ScaleRobotics
- 24th March 2010, 17:56
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/content.php?r=96-Mechanique-Bootloader-Hex
Walter
Powered by vBulletin® Version 4.1.7 Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.