• AVR Freaks

Hot!Update PIC FW from SD-card? Like a bootloader.

Author
AndersG
Super Member
  • Total Posts : 200
  • Reward points : 0
  • Joined: 2008/08/05 04:51:24
  • Location: 0
  • Status: offline
2020/05/20 10:47:14 (permalink)
5 (1)

Update PIC FW from SD-card? Like a bootloader.

Hi!
 
I have this thing that is based on an 18F46K40 that has an SD card for storage. I use FATFS to access the SD card. Now, would it not be nice if I could update the FW from a file on that card? Is that possible? Ie can a 18F46K40 update the code?
 
I guess I would have to segment the code so that the startup code and bootstrap is first, together with the FATFS code. That part rarely changes and then have the rest, starting at a known location so I could update it from a file?
 
Clearly must someone have done this before, but I cannot find any examples when I search, so pointers are appreciated!
#1

18 Replies Related Threads

    barbiani
    Super Member
    • Total Posts : 246
    • Reward points : 0
    • Status: offline
    Re: Update PIC FW from SD-card? Like a bootloader. 2020/05/20 11:53:41 (permalink)
    +2 (2)
    Sure you can. Take a serial bootloader example and change where it gets the data packet through UART to fatfs read file from the SD card.
     
    The fatfs library can be trimmed down to read-only, making it easier to fit in the boot block.
     
    Your application and the bootloader will be 2 different projects.
    #2
    NKurzman
    A Guy on the Net
    • Total Posts : 18667
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: offline
    Re: Update PIC FW from SD-card? Like a bootloader. 2020/05/20 12:17:14 (permalink)
    +5 (5)
    Not Like a Bootloader, It is a Bootloder.
    #3
    AndersG
    Super Member
    • Total Posts : 200
    • Reward points : 0
    • Joined: 2008/08/05 04:51:24
    • Location: 0
    • Status: offline
    Re: Update PIC FW from SD-card? Like a bootloader. 2020/05/20 12:59:59 (permalink)
    +1 (1)
    "The fatfs library can be trimmed down to read-only, making it easier to fit in the boot block."
    Yes, but that means duplicating the code, right? I am  a bit tight on program space as it is.
    #4
    ric
    Super Member
    • Total Posts : 27022
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: Update PIC FW from SD-card? Like a bootloader. 2020/05/20 13:22:36 (permalink)
    +4 (4)
    AndersG
    Yes, but that means duplicating the code, right?

    Exactly.

    I am  a bit tight on program space as it is.

    Then it's probable you can't do this unless to a larger part.
    It would be a LOT of work to somehow re-use the bootloader code in the main app, particularly regarding the compiled stack.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #5
    upand_at_them
    Super Member
    • Total Posts : 437
    • Reward points : 0
    • Joined: 2005/05/16 07:02:38
    • Location: Pennsylvania
    • Status: online
    Re: Update PIC FW from SD-card? Like a bootloader. 2020/05/20 13:59:26 (permalink)
    0
    Why can't the bootloader use the FAT routines and just not overwrite them?  Isn't that what ORG is for? :)
    #6
    ric
    Super Member
    • Total Posts : 27022
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: Update PIC FW from SD-card? Like a bootloader. 2020/05/20 15:04:13 (permalink)
    +1 (1)
    I assumed both were in C.

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #7
    NKurzman
    A Guy on the Net
    • Total Posts : 18667
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: offline
    Re: Update PIC FW from SD-card? Like a bootloader. 2020/05/20 17:42:55 (permalink)
    +4 (4)
    If you have to ask you can't afford it.
    While it is theoretically possible to make a combo Bootloader / Program, It would be very challenging.  easier in ASM than C.  With the Limitation that you could not fix any bugs in the "Bootloader common area"  But if you have to ask how to do it, then you probably lack the skills to do it.  My advice is also get a bigger PIC.
     
    You are not the First to ask this question.  None have posted back that they succeeded.
    #8
    pcbbc
    Super Member
    • Total Posts : 1691
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: Update PIC FW from SD-card? Like a bootloader. 2020/05/21 07:02:14 (permalink)
    +1 (3)
    Don't put a file system on the SDCard.  Just store the firmware (with a header) starting from sector 0.
    Get the bootloader to recognise the header, then load the firmware.  If it's at a fixed sector offset, for a fixed length, you don't need a file system at all.
     
    Of course:
    a) This requires you to distribute specially prepared SDCards
    b) Or, if uses are self upgrading, provide a utility that erases and specially formats a card for them
     
    Also, at least on a legacy MBR formatted card, there's a lot of space (reserved for extended bootstap code I think?) between the MBR and the first sector of the first partition.  4MB on one of my cards here.  You could probably squirrel away a firmware update in there.  Although some AV software might object.  Advantage would be you don't have to reformat the users card.
     
    Or add some external flash to your design, have the main app upload from the SDCard to the external flash, and have the bootloader upgrade from there.
     
    Lots of options.
    #9
    AndersG
    Super Member
    • Total Posts : 200
    • Reward points : 0
    • Joined: 2008/08/05 04:51:24
    • Location: 0
    • Status: offline
    Re: Update PIC FW from SD-card? Like a bootloader. 2020/05/22 01:33:40 (permalink)
    0
    Thanks all. I really appreciate the comments. Yes the program is in C, no I cannot easily use a bigger MCU, unless someone points me to a pin-compatible, but larger MCU than the 46K40 :) But, I do have about 20k free right now, so I might squeeze in a bootloader.
     
    I have about 100 of these units out there and it would be nice to have an easier upgrade path so what I will do at this point is to do a test with the serial bootloader to learn the ropes, so to speak.
     
    What pcppc suggested is in-fact very cool and in that case could I distribute an EXE that wrote the required info an jut about any card. I will give it some serious consideration. Reading the update from a fixed location on the SD card should not be much more code than reading it over serial.
     
    I also agree with NKurzman that what I originally thought of is overly complex.
    #10
    AndersG
    Super Member
    • Total Posts : 200
    • Reward points : 0
    • Joined: 2008/08/05 04:51:24
    • Location: 0
    • Status: offline
    Re: Update PIC FW from SD-card? Like a bootloader. 2020/05/22 20:54:35 (permalink)
    #11
    AndersG
    Super Member
    • Total Posts : 200
    • Reward points : 0
    • Joined: 2008/08/05 04:51:24
    • Location: 0
    • Status: offline
    Re: Update PIC FW from SD-card? Like a bootloader. 2020/05/22 20:59:36 (permalink)
    +1 (1)
    Edit: Found it, but the editor will not let me include it. :(
    #12
    NKurzman
    A Guy on the Net
    • Total Posts : 18667
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: offline
    Re: Update PIC FW from SD-card? Like a bootloader. 2020/05/22 23:44:01 (permalink)
    0
    You can use a PICKit4 as a stand-alone programmer. If that helps.
    #13
    AndersG
    Super Member
    • Total Posts : 200
    • Reward points : 0
    • Joined: 2008/08/05 04:51:24
    • Location: 0
    • Status: offline
    Re: Update PIC FW from SD-card? Like a bootloader. 2020/05/23 08:12:10 (permalink)
    0
    Yes. I know, but then that assumes everyone has one :) Target audience is hobbyists so many do though.
    #14
    NKurzman
    A Guy on the Net
    • Total Posts : 18667
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: offline
    Re: Update PIC FW from SD-card? Like a bootloader. 2020/05/23 08:33:19 (permalink)
    +1 (1)
    You may want to make sure the board is compatible with the SNAP programmer.
    That is pretty cheap.
    #15
    crosland
    Super Member
    • Total Posts : 1939
    • Reward points : 0
    • Joined: 2005/05/10 10:55:05
    • Location: Warks, UK
    • Status: offline
    Re: Update PIC FW from SD-card? Like a bootloader. 2020/05/23 08:41:28 (permalink)
    0
    ric
    AndersG
    Yes, but that means duplicating the code, right?

    Exactly.

    I am  a bit tight on program space as it is.

    It would be a LOT of work to somehow re-use the bootloader code in the main app, particularly regarding the compiled stack.




    It was easy with C18. I did it in a low pin count USB PIC where I could not fit two copies of the USB stack (for bootloader and app). Rather than a "bootloader" it was a "BIOS" that provided services to the app, mainly USB.
     
    The downside was the code for those services could not be directly updated by the bootloader.
     
    The upside was the PIC never had to be reset so the USB never had to re-enumerate when bootloading.
    #16
    crosland
    Super Member
    • Total Posts : 1939
    • Reward points : 0
    • Joined: 2005/05/10 10:55:05
    • Location: Warks, UK
    • Status: offline
    Re: Update PIC FW from SD-card? Like a bootloader. 2020/05/23 08:46:07 (permalink)
    0
    In fact, I don't see why you couldn't do it in XC8 with a compiled stack. It's just a single "app" context.
     
    Data memory use will be less efficient as you can't reuse data space that is only needed for the "bootloader" in the "app".
    #17
    ric
    Super Member
    • Total Posts : 27022
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: Update PIC FW from SD-card? Like a bootloader. 2020/05/23 14:40:06 (permalink)
    +1 (1)
    crosland
    In fact, I don't see why you couldn't do it in XC8 with a compiled stack. It's just a single "app" context.
     
    Data memory use will be less efficient as you can't reuse data space that is only needed for the "bootloader" in the "app".

    You would have to lock down every single variable used by the bootloader, including auto variables, so they were always in the same place to make sure they didn't overlap application variables.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #18
    AndersG
    Super Member
    • Total Posts : 200
    • Reward points : 0
    • Joined: 2008/08/05 04:51:24
    • Location: 0
    • Status: offline
    Re: Update PIC FW from SD-card? Like a bootloader. 2020/05/24 05:06:45 (permalink)
    0
    You may want to make sure the board is compatible with the SNAP programmer.
    That is pretty cheap.
    I have an ICSP header there. I assume that will do? I also know of some that has used even cheaper alternatives out there, such ac PICPgm and JDM.
    post edited by AndersG - 2020/05/24 12:30:45
    #19
    Jump to:
    © 2020 APG vNext Commercial Version 4.5