bootloader that xfers code from I2C into program memory


Closed Thread
Results 1 to 31 of 31

Hybrid View

  1. #1

    Default bootloader that xfers code from I2C into program memory

    This question will undoubtedly seem rudimentary and simple to the bootload gurus here...

    I've seen a few posts here by people looking to transfer firmware upgrades from I2C to program memory.

    This would be very handy, since the code to get the update from a PC and store it temporarily in I2C EEPROM prior to the actual "firmware update" can be done with PBP without using the hardware UART, and furthermore, encryption can be done (to the hex file before the end user downloads it).

    As I understand it, the problem is that the instructions to do the actual writecode would have to be in high memory so it's not overwritten by the update process, and all the libraries are sitting in low memory so there's really no way to "stick" a subroutine written with PBP up there in high memory.

    Is there any way to combine two .hex files so that one can sit in upper memory (with the necessary libraries included at that location) and somehow just be called by the "normal" program residing in LOW memory (at which point it's overwritten)? I know it's not very efficient from a codespace standpoint, but if the extra memory is just sitting there....

    Thanks,

    Picster

  2. #2
    Join Date
    Sep 2009
    Posts
    737


    Did you find this post helpful? Yes | No

    Default Re: bootloader that xfers code from I2C into program memory

    I think that I posted somewhere code for that.
    But here is my code that copy from external I2C mem to FLASH
    Code:
        DEFINE RESET_ORG 1EC00h 'Move all library sub and code to start from location 125952
        EraseStr        VAR BYTE[128]
        Adr             VAR LONG
        MAdr            VAR WORD
        MCtrl           VAR BYTE
        FlashByte1      VAR BYTE
        FlashByte2      VAR BYTE
        FlashByte3      VAR BYTE
        Err             VAR BYTE
        BlockSize       VAR BYTE BANKA SYSTEM
    
        @ MovLW BLOCK_SIZE
        @ MovWF BlockSize
    
        Mem_Vcc=1
        PAUSE 6
        FOR Adr=0 TO 79999 STEP BlockSize
            ERASECODE Adr
        NEXT Adr
        FOR Adr=0 TO 79999
            MAdr=Adr.WORD0
            MCtrl=$A0 + ((Adr.BYTE2 & %00000011)<<1)
            Err=0
        BootI2CR:
            Err=Err+1
            IF Err>1 THEN PAUSE 1
            I2CREAD Mem_Sda,Mem_Scl,MCtrl,MAdr,[FlashByte1],BootI2CR
            IF Err>1 THEN PAUSE 1
            I2CREAD Mem_Sda,Mem_Scl,MCtrl,MAdr,[FlashByte2],BootI2CR
            IF Err>1 THEN PAUSE 1
            I2CREAD Mem_Sda,Mem_Scl,MCtrl,MAdr,[FlashByte3],BootI2CR
            IF FlashByte1<>FlashByte2 THEN GOTO BootI2CR
            IF FlashByte1<>FlashByte3 THEN GOTO BootI2CR
            WRITECODE Adr, FlashByte1
        NEXT Adr
        'Optional Erase I2C EEPROM
        FOR Adr=0 TO 79999 STEP 128
            MAdr=Adr.WORD0
            MCtrl=$A0 + ((Adr.BYTE2 & %00000011)<<1)
         BootI2CW:
            I2CWRITE Mem_Sda,Mem_Scl,MCtrl,MAdr,[STR EraseStr\128],BootI2CW
            PAUSE 5
        NEXT Adr
        INPUT Mem_Sda
        INPUT Mem_Scl
        Mem_Vcc=0
        @ RESET
    For first programming I joint two hex files manually.
    First compile bootloader, then open hex in MPLAB X, and copy code from program memory window from 1EC00h to end of bootloader and then format it and put at end of main app.
    Code:
    @ ORG 1EC00h
    StartBootloader:
    ASM
        dw 06A01h, 0EFEBh, 0F0F6h, 08A01h, 0AA01h, 09A01h, 0B601h, 0D003h, 08601h, 0D851h, 0E234h, 00E08h, 06E18h, 0D861h, 03617h, 02E18h
        dw 0D7FCh, 0AA01h, 0D83Bh, 0D869h, 0D862h, 0BA01h, 0D828h, 0D82Eh, 05017h, 090D8h, 0EFE8h, 0F0F6h, 08C01h, 0AC01h, 09C01h, 06ED9h
        dw 00603h, 0E203h, 0BC01h, 0D01Bh, 0D018h, 050DEh, 0D804h, 0E215h, 0D7F7h, 08A01h, 0AA01h, 09A01h, 0B401h, 0D005h, 00BFEh, 06E0Ch
        dw 08401h, 090D8h, 0D00Ah, 0B801h, 0D005h, 06E1Fh, 08801h, 0D823h, 0E206h, 0501Fh, 0D827h, 0E203h, 0AA01h, 0EFE8h, 0F0F6
        ...........
    ENDASM
    In Main application I use GOTO StartBootloader to run bootloader.
    In last project I had lot of free space in pic, and use Flash to Flash bootloader.
    Code:
    DEFINE RESET_ORG .65024
    
    Adr VAR WORD
    Tmp VAR BYTE
    BlockSize       VAR BYTE BANKA SYSTEM
    @ MovLW BLOCK_SIZE
    @ MovWF BlockSize
    
    FOR Adr=0 TO 32255 STEP BlockSize
        ERASECODE Adr
    NEXT Adr
    
    FOR Adr=0 TO 32255 
        Adr.15=1
        READCODE Adr,Tmp
        Adr.15=0
        WRITECODE Adr,Tmp
    NEXT Adr
    @ RESET
    EDIT:
    I didn't point out that using this method all library for bootloader are stored after RESET_ORG vector.
    This way it uses little bit more code space(because you probably have same library in main application), but this makes implementation of bootloader and main app as simple as possible.
    Last edited by pedja089; - 2nd May 2016 at 23:01.

  3. #3


    Did you find this post helpful? Yes | No

    Default Re: bootloader that xfers code from I2C into program memory

    Thanks pedja089, that's awesome - is that using an 18F PIC? Do you know if this can be done using the 16F1xxx series? From what I've read, the DEFINE RESET ORG option is only functional on the 18's - does that sound right?

    Can anyone else confirm?

  4. #4
    Join Date
    Sep 2009
    Posts
    737


    Did you find this post helpful? Yes | No

    Default Re: bootloader that xfers code from I2C into program memory

    I use 18F series for all my project.
    You can just compile some code for 16F, something like
    DEFINE RESET_ORG 16
    pause 1
    And you should get 16 bytes FF then some other values. Also to view content of hex file you could use any programmer software, but I prefer MPLAB X, because I can export HEX values of just piece of hex to text file, and format it to ASM sintax.
    Then let us know
    I don't know what 16F part are able to do flash write. And also flash size is critical.
    So I think any way, better to go with some 18F that have twice flash you need, so you could use flash to flash bootloader. It's really neat, small, simple and doesn't require any external parts!
    All this can be done in ASM to get even smaller bootloaders, but I just didn't bother with that.
    By the way all 18F can do code erase, write and read eaven if it is locked. Code protect bit affect only ICSP.

  5. #5


    Did you find this post helpful? Yes | No

    Default Re: bootloader that xfers code from I2C into program memory

    This is definitely a great starting point. I'll test with some simple stuff and then see how it works out on the PIC I'm using. Might be just what I needed.

    picster

  6. #6


    Did you find this post helpful? Yes | No

    Default Re: bootloader that xfers code from I2C into program memory

    Ok so looking at the code, I had a couple of questions - I'm hoping you can clarify for me.

    1) Flashbyte2 and Flashbyte3 - redundant copies, with the same data 3 times in i2c eeprom?

    2) What is the purpose of EraseStr VAR BYTE[128]? I don't see it elsewhere in the pasted code.

    3) Lastly, in the ASM portion, you start each line with dw - is this just to "define words" (hex format little-endian) that will simply sit there in memory until you jump to the label?

    I really appreciate your posting this, it's hugely helpful.

    Picster
    Last edited by picster; - 5th May 2016 at 00:27. Reason: amended question

Similar Threads

  1. Program Code and Program Memory
    By DenFrod in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 8th February 2007, 14:51
  2. Use internal program memory like DATA memory
    By flotulopex in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 30th December 2006, 18:38
  3. using Flash Program Memory ?
    By muskut in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 11th October 2006, 15:17
  4. PIC16F88, Bootloader & I2C Memory
    By digilord in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 30th December 2005, 15:36
  5. program memory size
    By volcane in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 25th October 2005, 19:45

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