Menu

Bootloader for PIC18F97J60

Vu Hoang
2018-09-21
2018-09-26
  • Vu Hoang

    Vu Hoang - 2018-09-21

    Hello,

    I use pic18f97j60 device and Tiny Multi Bootloader+(v0.10.0 or v0.11.0).
    I use XC8 and mplabX.
    I has modified tinybld18F24J10_ext16MHz_19200.asm to use with 18f97j60, and add new entry to piccodes.ini.
    I build a simple program, set Memory model to 100-500, and try to use Tiny Multi Bootloader+ to send firmware to pic, but I get this errror:
    Error: hex file too large, writing bootloader firmware!!!
    ERROR: Problem in uploading HEX file to device

    Can you help me, please.

    Here is my .hex file

    :020000040000FA
    :0400000004EF02F017
    :100100000FC013F010C014F011C015F012C016F09B
    :10011000060ED8901336143615361636E82E89EFAB
    :1001200000F0131E141E151E161E132A000E142294
    :1001300015221622400E1324016E780E1420026E32
    :100140007D0E1520036E010E1620046E0FC017F0F1
    :1001500010C018F011C019F012C01AF0060ED89095
    :100160001736183619361A36E82EAFEF00F017C0DA
    :1001700005F018C006F019C007F01AC008F028EC06
    :1001800001F001C01BF002C01CF01C04D8A0CBEF92
    :1001900000F0CDEF00F015EF01F00FC013F010C02C
    :1001A00014F011C015F012C016F0040ED8901336DA
    :1001B000143615361636E82ED6EF00F0131E141E30
    :1001C000151E161E132A000E142215221622400E8A
    :1001D0001324016E780E1420026E7D0E1520036E1E
    :1001E000010E1620046E0FC017F010C018F011C0D9
    :1001F00019F012C01AF0040ED890173618361936B6
    :100200001A36E82EFCEF00F017C005F018C006F013
    :1002100019C007F01AC008F028EC01F001C01BF06B
    :1002200002C01CF0AC8415EF01F01C04D8B01BEF29
    :1002300001F01DEF01F027EF01F01BC0AFFFAC98FC
    :10024000AB8E948E949CAB88AC8A27EF01F01200A1
    :10025000000E0A6E08BE31EF01F02FEF01F03DEF06
    :1002600001F0081E071E061E056C000E062207225E
    :100270000822010E0A6E3DEF01F004BE44EF01F0CA
    :1002800042EF01F050EF01F0041E031E021E016C4C
    :10029000000E022203220422010E0A1A50EF01F07E
    :1002A000000E0B6E000E0C6E000E0D6E000E0E6E2C
    :1002B0000550061007100810D8B461EF01F063EF85
    :1002C00001F0A5EF01F0010E096E6FEF01F0D8907B
    :1002D0000536063607360836092A6FEF01F008AEF4
    :1002E00074EF01F076EF01F067EF01F07AEF01F0C3
    :1002F0007AEF01F0D8900B360C360D360E360550DD
    :10030000015C065002580750035808500458D8A002
    :100310008CEF01F08EEF01F099EF01F00550015ED6
    :100320000650025A0750035A0850045A0B8099EF9E
    :1003300001F0D8900832073206320532092E7AEFE2
    :1003400001F0A5EF01F0A5EF01F00A50D8B4ABEF32
    :1003500001F0ADEF01F0B7EF01F00E1E0D1E0C1E07
    :100360000B6C000E0C220D220E22B7EF01F00BC019
    :1003700001F00CC002F00DC003F00EC004F0C1EF9C
    :1003800001F012009998800E0F6E250E106E000E6F
    :10039000116E000E126E80EC00F0CFEF01F0878836
    :1003A000410EF5EC01F0200E236EB50E226ECB0E41
    :1003B000E82EFED7222EFCD7232EFAD700F08798FE
    :1003C000420EF5EC01F0200E236EB50E226ECB0E20
    :1003D000E82EFED7222EFCD7232EFAD700F0CFEF3F
    :1003E00001F0CFEF01F000EF00F0016EF8EF01F047
    :1003F000ACA2FDEF01F0FFEF01F0F8EF01F001EF2B
    :0E04000002F001C0ADFF12000001C2EF01F0DA
    :020000040001F9
    :06FFF800A0F4C4FFF8F7BD
    :00000001FF
    

    Here is tinybld18F24J10_ext16MHz_19200.asm after i modified

    radix DEC
    
    ; change these lines accordingly to your application    
    #include "p18f97j60.inc"
    IdTypePIC = 0x33        ; Please refer to the table below, must exists in "piccodes.ini"    
    #define max_flash  0x20000  ; in bytes, not WORDS!!! Please refer to the table below
    
    xtal    EQU     25000000    ; you may also want to change: _HS_OSC _XT_OSC
    baud    EQU     19200       ; standard TinyBld baud rates: 115200 or 19200
    
    ;   The above 5 lines can be changed and buid a bootloader for the desired frequency (and PIC type)
    
    #include "spbrgselect.inc"  ; RoundResult and baud_rate
    
        #define first_address max_flash-264     ;132 words
    
            config WDT = OFF        ;WDT is controlled by SWDTEN bit of the WDTCON register
            config STVR = OFF       ;Stack full/underflow will not cause Reset
            config XINST = OFF      ;Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
            config DEBUG = OFF      ;Background debugger disabled; RB6 and RB7 configured as general purpose I/O pins
            config CP0 = OFF        ;Program memory is not code-protected
            config FOSC = HS        ;HS oscillator
            config FOSC2 = ON       ;Clock selected by FOSC as system clock is enabled when OSCCON<1:0> = 00
            config FCMEN = OFF      ;Fail-Safe Clock Monitor disabled
            config IESO = OFF       ;Oscillator Switchover mode disabled
            config WDTPS = 1        ;Watchdog Timer Postscale Select bits 1:1
            config CCP2MX = ON      ;CCP2 is multiplexed with RC1
    
    ;----------------------------- PROGRAM ---------------------------------
    cblock 0
    crc
    cnt1
    cnt2
    cnt3
    ;;  flagU
    flagH
    flagL
    endc
    
    cblock 0x0100
    buffer:256
    endc
    
    count   equ crc
    
    ;0000000000000000000000000 RESET 00000000000000000000000000
    
        ORG     0x0000
        GOTO    IntrareBootloader
    
    ;view with TabSize=4
    ;&&&&&&&&&&&&&&&&&&&&&&&   START     &&&&&&&&&&&&&&&&&&&&&&
    ;----------------------  Bootloader  ----------------------
    ;PC_flash:      C1h     U       H       L       64  ...  <64 bytes>   ...  crc
    ;PC_cfg:        C1h     U OR 80h    H       L        8  ...  < 8 bytes>   ...  crc
    ;PC_cfg_unnecessary:    C1h     0       crc
    ;PIC_response:     type `J`
    
    ORG first_address           ;space to deposit first 4 instr. of user prog.
    nop
    nop
    nop
    nop
    
    org first_address+8
    IntrareBootloader:
                        ;skip TRIS to 0 C6
    movlw   ((1<<TXEN) | (1<<BRGH))     ;init serial port
    movwf   TXSTA
    ;use only SPBRG (8 bit mode default) not using BAUDCON
    movlw   spbrg_value
    movwf   SPBRG
    movlw   ((1<<SPEN) | (1<<CREN))
    movwf   RCSTA
                        ;wait for computer
    rcall   Receive
    sublw   0xC1                ;Expect C1h
    bnz     way_to_exit
    
    movlw   IdTypePIC           ;send PIC type
    movwf   TXREG
    movlw   'J'             ; "-Everything OK, ready and waiting."
    movwf   TXREG
    
    ;;  movlw   ((IntrareBootloader>>16)&0xFF)  ;TBLPTR = IntrareBootloader
    ;;  movwf   TBLPTRU
    movlw   ((IntrareBootloader>>8)&0xFF)
    movwf   TBLPTRH
    ;;  movlw   (IntrareBootloader&0xFF)
    ;;  movwf   TBLPTRL
    clrf    crc             ;crc = 0
    incf    FSR0H               ;FSR0 = buffer TOP (0x0100)
    TBLRD*-
    cp_bl:                      ;copy Bootloader and Config to RAM
    TBLRD+*
    movff   TABLAT,POSTINC0
    tstfsz  FSR0L               ;FSR0 = 0x0200? (copy 256 bytes?)
    bra     cp_bl               ;
    
    rcall   Receive             ;Config Upper
    btfss   WREG,7              ;is Config?
    bra chk_crc
    rcall   Receive             ;Config Hi (Recive only)
    rcall   Receive             ;Config Lo
    decf    FSR0H               ;FSR0 = 0x0100
    movwf   FSR0L               ;FSR0 = 0x01F8
    rcall   Receive             ;count (Recive only)
    
    rcv_cfg:                    ;recive Config data
    rcall   Receive
    movwf   POSTINC0
    tstfsz  FSR0L               ;FSR0 = 0x0200? (recive 8 bytes?)
    bra     rcv_cfg
    
    chk_crc:
    rcall   Receive             ;get crc
    bnz way_to_exit         ;crc error
    
    wr_bl1:                     ;TBLPTR = 0x003B00-1(18F24/44J10)
    rcall   wr_bld              ;TBLPTR = 0x007B00-1(18F25/45J10)
    bra (wr_bl2 - 1024)
    wr_bl2:                     ;TBLPTR = 0x003F00-1(18F24/44J10)
    rcall   wr_bld              ;TBLPTR = 0x007F00-1(18F25/45J10)
    bra (set_FSR + 1024)
    set_FSR:
    decf    FSR0H               ;FSR0 = 0x0100
    
    MainLoop:
    movlw   'J'             ; "-Everything OK, ready and waiting."
    mainl:
    movwf   TXREG
    clrf    crc             ;crc = 0
    rcall   Receive             ;Upper
    movwf   TBLPTRU
    ;;  movwf   flagU               ;(for ERASE)
    rcall   Receive             ;Hi
    movwf   TBLPTRH
    movwf   flagH               ;(for ERASE)
    rcall   Receive             ;Lo
    movwf   TBLPTRL
    movwf   flagL               ;(for ERASE)
    rcall   Receive             ;count (Recive only)
    clrf    FSR0L               ;FSR0 = 0x0100
    rcvoct:                     ;read 64 bytes
    rcall   Receive
    movwf   POSTINC0
    btfss   FSR0L,6             ;FSR0 = 0x0140?
    bra     rcvoct
    
    clrf    FSR0L               ;FSR0 = 0x0100
    rcall   Receive             ;get crc
    ziieroare:                  ;CRC failed
    movlw   'N'
    bnz mainl
                        ;write
    eraseloop:
    ;   bcf STATUS,C            ;Carry=0
    ;;  rrcf    flagU               ;flagU:flagH:flagL=(flagU:flagH:flagL>>2)
    rrcf    flagH
    rrcf    flagL
    ;   bcf STATUS,C            ;Carry=0
    ;;  rrcf    flagU
    rrcf    flagH
    rrcf    flagL
    bnz noerase             ;Each 1024bytes?
    movlw   ((IntrareBootloader>>10)&0xFF)  ;0x0F=0x3C00>>10,0x1F=0x7C00>>10
    subwf   flagH,w
    bz  noerase             ;Last Erase Page?
    bsf EECON1,FREE         ;Erase
    rcall   Write
    
    noerase:
    TBLRD*-                 ;point to adr-1
    rcall   write64bytes
    
    waitwre:
    ;btfsc  EECON1,WR           ;for eeprom writes (wait to finish write)
    ;bra    waitwre             ;no need: round trip time with PC bigger than 4ms
    
    bra     MainLoop
    
    ;******** procedures ******************
    
    wr_bld:
    btg TBLPTRH,2           ;TBLPTR-1024.orTBLPTR+1024
    bsf EECON1,FREE         ;Erase
    rcall   Write
    clrf    TBLPTRL
    TBLRD*-                 ;point to adr-1
    decf    FSR0H               ;FSR0 = 0x0100
    wr_bld_lp:
    rcall   write64bytes
    tstfsz  FSR0L               ;FSR0 = 0x0200? (write 256 bytes?)
    bra     wr_bld_lp
    return
    
    write64bytes:
    bsf count,6             ;count = 64
    write64_lp:
    movff   POSTINC0,TABLAT         ;put 1 byte
    tblwt+*
    decfsz  count               ;count = 0?
    bra     write64_lp
    Write:
    bsf EECON1,WREN
    movlw   0x55
    movwf   EECON2
    movlw   0xAA
    movwf   EECON2
    bsf     EECON1,WR           ;WRITE
    nop
    clrf    EECON1
    return
    
    Receive:
    movlw   (xtal/2000000+1)        ; for 20MHz => 11 => 1second delay
    movwf   cnt1
    rpt2:
    clrf    cnt2
    rpt3:
    clrf    cnt3
    rptc:
    btfss   PIR1,RCIF           ;test RX
    bra     notrcv
    movf    RCREG,w             ;return read data in W
    addwf   crc             ;compute crc
    return
    notrcv:
    decfsz  cnt3
    bra     rptc
    decfsz  cnt2
    bra     rpt3
    decfsz  cnt1
    bra     rpt2
    ;timeout:
    way_to_exit:
    bcf RCSTA,SPEN          ; deactivate UART
    bra first_address
    
    ;*************************************************************
    ; After reset
    ; Do not expect the memory to be zero,
    ; Do not expect registers to be initialised like in catalog.
    
            END
    

    This is new line I add to picodes.ini

    $33, J, 18F(J) w/128KB flash & 0B EEPROM,  $20000,    0, default, default,
    
     
  • Dan

    Dan - 2018-09-23

    Hi,Vu Hoang
    Although it can not be guaranteed to operate, it is necessary to change the following part.

     
  • Vu Hoang

    Vu Hoang - 2018-09-26

    Hi, Dan,

    Thankyou for your reply,
    I had change the bootloader follow your guide, but it doesn't resolve the the error with Tiny Multi Bootloader+.
    Error: hex file too large, writing bootloader firmware!!!
    ERROR: Problem in uploading HEX file to device.

    Do I need any change to picodes.ini to make it work,
    Thanks,

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.