ds30 Loader free edition forums
PIC 18F4580 Problems [solved] - Printable Version

+- ds30 Loader free edition forums (https://picbootloader.com/forum)
+-- Forum: Support (https://picbootloader.com/forum/forumdisplay.php?fid=3)
+--- Forum: 8-bit firmwares (https://picbootloader.com/forum/forumdisplay.php?fid=7)
+--- Thread: PIC 18F4580 Problems [solved] (/showthread.php?tid=20)



PIC 18F4580 Problems [solved] - LMS_EE - 2010-11-23

Hello,

i am trying to use the ds30 Bootloader with a pic18f4580 mcu. below is my settings.inc and dsloader.asm files.

i added a small two line bit of code to turn on an led i have connected the the mcu so signify that the loader is running. it is just above the "wait for computer" part of the code. as a note i have tried with and without this piece of code to make sure it was not effecting the rest of the codes functionality.

i have also tried internal and external oscillator setups and i have the write configs tab NOT clicked in the advanced settings tab.

so when i try to download my application or simply ty to find the bootloader, the "searching for bootloader" times out.

i know the serial port and all the connections to my micro are correct because if i just load my applications without the bootloader i can use the rs232 communication just fine.



my configs are also listed below. could it have something to do with the bootblock size? this field has to be 1kW or 2kW. thoughts?

configuration bits:
http://img268.imageshack.us/img268/913/bootloaderconfigs.png

Code:
;------------------------------------------------------------------------------
;
; Title:            ds30 Loader for PIC18F
;
; File description:    user settings and configuration bits
;
; Copyright:         Copyright © 09-10, Mikael Gustafsson
;
; History:            2.0.3 Renamed fosc to oscf
;                    2.0.2 New setting: NODENR
;                          New setting: NODENR_BL
;                          New setting: PROT_GOTO
;                          New setting: PROT_BL
;                    2.0.1 New setting: HELLORETRIES
;                    2.0.0 CAN support
;                    1.0.2 -
;                    1.0.1 -
;                    1.0.0 Added BLINIT
;                    0.9.2 Added tx enable support
;                    0.9.1 -
;                    0.9.0 first version of this file
;                                                                             
;------------------------------------------------------------------------------

;------------------------------------------------------------------------------
;    This file is part of ds30 Loader.
;
;    ds30 Loader is free software: you can redistribute it and/or modify
;    it under the terms of the GNU General Public License as published by
;    the Free Software Foundation.
;
;    ds30 Loader is distributed in the hope that it will be useful,
;    but WITHOUT ANY WARRANTY; without even the implied warranty of
;    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;    GNU General Public License for more details.
;
;    You should have received a copy of the GNU General Public License
;    along with ds30 Loader. If not, see <http://www.gnu.org/licenses/>.
;------------------------------------------------------------------------------


;------------------------------------------------------------------------------
; Device
;------------------------------------------------------------------------------
        LIST          P=18F4580;                        ;xxx


;------------------------------------------------------------------------------
; Includes
;------------------------------------------------------------------------------
        #include    "devices.inc"


;------------------------------------------------------------------------------
; User preferences
;------------------------------------------------------------------------------
        radix DEC

        #define        OSCF            4000000             ;xxx oscillator frequency
        #define        BAUDRATE         9600            ;xxx baudrate
        #define        BLTIME            2000            ;xxx data receive timeout [ms]
        #define        BLINIT            2000            ;xxx hello receive timeout [ms]
        #define        HELLOTRIES        2                ;xxx number of non hello characters received before branching to the user application
        
        #define        USE_UART1        1                ;xxx uncomment to use uart1
        ;#define    USE_UART2        1                ;xxx uncomment to use uart2
        ;#define    USE_CAN                                ;xxx uncomment to use CAN instead of UART
        
        ;#define    USE_TXENABLE    1                ;xxx uncomment to use a tx enable pin        
        #ifdef USE_TXENABLE
            #define    TRISR_TXE        TRISC            ;xxx tris register containing tx enable
            #define    LATR_TXE        LATC            ;xxx port register containing tx enable
            #define    TRISB_TXE        TRISC6            ;xxx tris bit for tx enable
            #define LATB_TXE        RC6                ;xxx port bit for tx enable
        #endif


;------------------------------------------------------------------------------
; CAN settings
;------------------------------------------------------------------------------        
        #define        ID_PIC        1                    ;xxx node number for this device
        #define     ID_GUI     0x7ff                    ;xxx node number of the ds30 Loader gui


;------------------------------------------------------------------------------
; Advanced settings
;------------------------------------------------------------------------------        
        #define        PROT_GOTO    1                    ;xxx protect goto at 0x00
        #define        PROT_BL     1                    ;xxx protect bootloader
        
        #define        BLPLP        7                    ;bootloader placement, pages from end
        #define        BLSIZEP        7                    ;bootloader size [pages], used by bootloader protection    
        

;------------------------------------------------------------------------------
; Configuration bits, these macros can be found at the end of the inc-files located in
; C:\Program Files\Microchip\MPASM Suite\
;
; These can also be set in MPLAB IDE instead, they are found in Configure->Configuration bits...
;------------------------------------------------------------------------------
;        config     OSC     = XT
;        config     FCMEN     = OFF        ;failsafe clock monitor
;        config     IESO     = OFF        ;internal external switchover mode
;        config     PWRT     = OFF        ;power-up timer
;        config     BOREN     = OFF        ;brown-out reset
;        config     BORV    = 3         ;brown-out reset value
;        config     WDT     = OFF        ;watchdog timer
;        config     WDTPS     = 1            ;1:1 WDT prescalar
;        config     MCLRE     = ON        ;MCLR
;        config     STVREN     = ON        ;stack overflow reset
;        config     LVP     = ON        ;low voltage programming
;        config    DEBUG    = ON        ;debug
;        config     CP0     = OFF        ;
;        config     CP1     = OFF        ;
;        config     CPB     = OFF        ;
;        config     CPD     = OFF        ;
;        config     WRT0     = OFF        ;
;        config     WRT1     = OFF        ;
;        config     WRTB     = OFF        ;
;        config     WRTC     = OFF        ;
;        config     WRTD    = OFF        ;
;        config     EBTR0     = OFF        ;
;        config     EBTR1     = OFF        ;
;        config     EBTRB     = OFF        ;
        
        ;__CONFIG _CONFIG1L, _PLLDIV_5_1L & _CPUDIV_OSC3_PLL4_1L & _USBDIV_2_1L
        ;__CONFIG _CONFIG1H, _FOSC_HSPLL_HS_1H & _FCMEM_OFF_1H & _IESO_OFF_1H
        ;__CONFIG _CONFIG2L, _PWRT_ON_2L & _BOR_OFF_2L ; _VREGEN_OFF_2L
        ;__CONFIG _CONFIG2H, _WDT_OFF_2H & _WDTPS_1_2H
        ;__CONFIG _CONFIG3H, _MCLRE_ON_3H & _PBADEN_OFF_3H & _CCP2MX_OFF_3H
        ;__CONFIG _CONFIG4L, _DEBUG_OFF_4L & _LVP_OFF_4L & _STVREN_OFF_4L & _XINST_OFF_4L
        ;__CONFIG _CONFIG5L, _CP0_OFF_5L & _CP1_OFF_5L & _CP2_OFF_5L
        ;__CONFIG _CONFIG5H, _CPB_OFF_5H & _CPD_OFF_5H
        ;__CONFIG _CONFIG6L, _WRT0_OFF_6L & _WRT1_OFF_6L & _WRT2_OFF_6L & _WRT3_OFF_6L
        ;__CONFIG _CONFIG6H, _WRTB_OFF_6H & _WRTC_OFF_6H & _WRTD_OFF_6H
        ;__CONFIG _CONFIG7L, _EBTR0_OFF_7L & _EBTR1_OFF_7L & _EBTR2_OFF_7L & _EBTR3_OFF_7L
        ;__CONFIG _CONFIG7H, _EBTRB_OFF_7H

Code:
;------------------------------------------------------------------------------
;
; Title:            ds30 Loader for PIC18F
;
; File description:    Main firmwarefile
;
; Copyright:         Copyright © 09-10, Mikael Gustafsson
;
; Version:            2.0.3 september 2010
;
; Thanks to:        Claudio Chiculita, this code is based on the Tiny PIC bootloader
;                    Fabien Pieraldi for writing the CAN part
;
; History:            2.0.3 Bugfix: In some case, CAN engine won't start (bad state on RB2)
;                          Bugfix: goto protection not working for some devices
;                          Improvement: renamed fosc to oscf for compatibility with some devices
;                    2.0.2 Change: node ID configuration moved in settings.inc
;                          New feature: goto protection
;                          New feature: bl protection
;                          Change: size is 7 pages
;                          Bugfix: compatible with devices that has no eeprom (2450, 4450 and more)
;                    2.0.1 New feature: adjustable non hello discard, HELLORETRIES setting                     
;                          Improvement: compatible with include files containing both TXSTA and TXSTA1 definitions
;                          Improvement: 5 or 7 (see section Init) more instructions free to use (CAN)
;                    2.0.0 CAN support
;                    1.0.2 Replaced old baudrate calculator    
;                    1.0.1 Erase is now made just before write to increase reliability
;                    1.0.0 Separated boot timeout and receive timeout
;                          Added range check of times and brg
;                    0.9.2 Changed bootloader size to 5 pages (to make room for more user init code)
;                          Added tx enable support
;                    0.9.1 1 more instruction free to use
;                          Added watchdog clear
;                          Fixed baudrate error check
;                           Correct buffer size
;                    0.9.0 Initial release
;                                                                             
;------------------------------------------------------------------------------

;-----------------------------------------------------------------------------
;    This file is part of ds30 Loader.
;
;    ds30 Loader is free software: you can redistribute it and/or modify
;    it under the terms of the GNU General Public License as published by
;    the Free Software Foundation.
;
;    ds30 Loader is distributed in the hope that it will be useful,
;    but WITHOUT ANY WARRANTY; without even the implied warranty of
;    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;    GNU General Public License for more details.
;
;    You should have received a copy of the GNU General Public License
;    along with ds30 Loader. If not, see <http://www.gnu.org/licenses/>.
;------------------------------------------------------------------------------

;------------------------------------------------------------------------------
;
; USAGE  USAGE  USAGE  USAGE  USAGE  USAGE  USAGE  USAGE  USAGE  USAGE  USAGE
;
;------------------------------------------------------------------------------
;
; This MPLAB-project comes preconfigured for PIC18F2550 using internal RC-
; oscillator
;
; To use other chip and/or configuration you need to do:
; * Select correct device in MPLAB IDE, menu Configure->Select Device
; * Search for xxx in settings.inc and check/alter those lines
; * Add i/o config if necessary
;
; * Tabsize used is 4
;
;------------------------------------------------------------------------------


;------------------------------------------------------------------------------
; Includes
;------------------------------------------------------------------------------
        #include "settings.inc"
        

;------------------------------------------------------------------------------
; UARTs
;------------------------------------------------------------------------------
        #ifndef USE_UART1
            #ifndef USE_UART2
                #ifndef USE_CAN
                    error "No communication is specified"
                #endif
            #endif
        #endif
        
        #ifdef USE_CAN
            #ifndef HAS_CAN
                error "CanBus specified for a device that only has uart"
            #endif
            #ifdef USE_UART1
                error "UART1 and Canbus specified"
            #endif
            #ifdef USE_UART2
                error "UART2 and Canbus specified"
            #endif
            #ifdef USE_TXENABLE
                error "TX enable is not available for CAN"
            #endif
        #endif
        

        #ifdef USE_UART1
            #ifdef USE_UART2
                error "Both uarts are specified"
            #endif
            #ifdef TXSTA
                   #define TXSTA_    TXSTA                    ;uart status
                   #define    RCSTA_    RCSTA                    ;uart status
                   #define    SPBRG_    SPBRG                    ;uart baudrate
                   #define    TXREG_    TXREG                    ;uart transmit
                   #define    RCREG_    RCREG                    ;uart receive
            else
                   #define    TXSTA_    TXSTA1                    ;uart status
                   #define    RCSTA_    RCSTA1                    ;uart status
                   #define    SPBRG_    SPBRG1                    ;uart baudrate
                   #define    TXREG_    TXREG1                    ;uart transmit
                   #define    RCREG_    RCREG1                    ;uart receive
            #endif
        #endif

        #ifdef USE_UART2
            #ifdef USE_UART1
                error "Both uarts are specified"
            #endif        
            #ifndef HAS_UART2
                error "UART2 specified for a device that only has uart1"
            #endif

               #define    TXSTA_        TXSTA2                    ;uart status
               #define    RCSTA_        RCSTA2                    ;uart status
               #define    SPBRG_        SPBRG2                    ;uart baudrate
               #define    TXREG_        TXREG2                    ;uart transmit
               #define    RCREG_        RCREG2                    ;uart receive
        #endif
        
        
;------------------------------------------------------------------------------
; Defines
;------------------------------------------------------------------------------
        #define    VERMAJ        2                                        ;firmware version major
        #define    VERMIN        0                                        ;firmware version minor
        #define    VERREV        3                                        ;firmware version revision
        
        #define HELLO         0xC1
        #define OK             'K'                                        ;erase/write ok
        #define CHECKSUMERR    'N'                                        ;checksum error
        #define    VERFAIL        'V'                                        ;verification failed
        #define    BLPROT        'P'                                      ;bl protection tripped
        #define    UCMD         'U'                                      ;unknown command

                
        #define    BL10MS        ( 10 * (OSCF / 4000) / (255 * 7) )        ;10ms delay
        #define    BLSTART        ( BLINIT / 10 )                            ;count for boot receive delay
        #define    BLDELAY        ( BLTIME / 10 )                            ;count for receive delay
       
        #define    UBRG        ( (((OSCF / BAUDRATE) / 8) - 1) / 2 )    ;baudrate
        
        #define    PAGESIZEW    32                                        ;pagesize [words]
        #define    PAGESIZER    (PAGESIZEW/ROWSIZEW)                    ;pagesize [rows]
        #define    ROWSIZEB    (ROWSIZEW*2)                            ;rowsize [bytes]
        #define STARTADDR    ( MAX_FLASH - BLPLP * PAGESIZER * ROWSIZEW * 2 )            ;bootloader placement
        
        ; Debug output
        ;messg    STARTADDR_IS #(STARTADDR)
        ;messg    UBRG_IS        #v(UBRG)
        ;messg    bldelayis     #v(BLDELAY)
        ;messg    blstartis     #v(BLSTART)

                
;------------------------------------------------------------------------------
; Range check
;------------------------------------------------------------------------------
        if UBRG > 255
            error spbrg_value_ is out of range
        #endif
        if UBRG == 0
            error spbrg_value_ might be out of range
        #endif
                        
        if BLSTART > 255
            error BLSTART_ is out of range
        #endif
        if BLSTART == 0
            error spbrg_value_ might be out of range
        #endif            
        
        if BLDELAY > 255
            error BLDELAY_ is out of range
        #endif            
        if BLDELAY == 0
            error BLDELAY_ might be out of range
        #endif
                
        
;------------------------------------------------------------------------------
; Variables
;------------------------------------------------------------------------------
        cblock 0
            crc            ;receive checksum
            dcnt        ;datacount
            cnt1        ;receive timeout timer
            cnt2        ;receive timeout timer
            cnt3        ;receive timeout timer
            cntHello    ;
            rowcnt        ;row iterator
            rowcnt2        ;
            cmd            ;command
            doerase        ;do erase before next write
            ttblptru
            ttblptrh
            ttblptrl
        ;endc
        ;cblock 10
            buffer:65    ;receive buffer
        endc
        
        
;------------------------------------------------------------------------------
; Send macro
;------------------------------------------------------------------------------     
SendL     macro     sbyte
            movlw     sbyte
            rcall    Send
        endm

    
;------------------------------------------------------------------------------
; Reset vector
;------------------------------------------------------------------------------
        org     0x0000
        goto    blstart


;------------------------------------------------------------------------------
; GOTO user application
;------------------------------------------------------------------------------     
        org     STARTADDR - 4    ;space to deposit goto to user application
loadusr    nop
        nop
    
    
;------------------------------------------------------------------------------
; Start of bootloader code
;------------------------------------------------------------------------------     
        org     STARTADDR
blstart        
        
        ;----------------------------------------------------------------------
        ; User init
        ;----------------------------------------------------------------------         
        ; Enable digital i/o    
        #ifdef    ADCON1
            ;error Do you need to configura uart pins to be digital? If not, remove this line
        #endif
        ;movlw     b'01100000'        ;xxx disable
        ;movwf     ADCON1            ;xxx analog on tx/rx
        
        ; Set internal oscillator to 8MHz
        ;movlw b'01110000'    
        ;movwf OSCCON
        ;bsf   OSCTUNE, PLLEN
        
        ; Make tx enable pin output and set to 0
        #ifdef USE_TXENABLE
            bcf    TRISR_TXE, TRISB_TXE
            bcf    LATR_TXE, LATB_TXE
        #endif
        
        
        ;----------------------------------------------------------------------
        ; Init
        ;----------------------------------------------------------------------         
        clrf    doerase
        ; Uart
        #ifndef USE_CAN
            movlw     b'00100100'        ;enable transmit and
            movwf     TXSTA_            ; high speed mode
            movlw     UBRG            ;use only SPBRG_ (8 bit mode default) not using BAUDCON
            movwf     SPBRG_
            movlw     b'10010000'        ;enable serial port
            movwf     RCSTA_            ; and receive
        #endif
        #ifdef USE_CAN
            bsf        PORTB, RB2
            bcf        TRISB, RB2

            movlb    15
            movlw   b'00000001'
            movwf   TXB0DLC,BANKED
            
            ;xxx If transmit ID & receive ID are the same then comment lines marked with * (free 2 instructions)
            movlw    (ID_GUI<<5)&0xE0    ;Load SID2:SID0, EXIDE = 0 / User configure transmit ID        
            movwf   TXB0SIDL,BANKED
            movlw    (ID_PIC<<5)&0xE0    ;Load SID2:SID0, EXIDE = 0 / User configure receive ID *
            movwf    RXF0SIDL, BANKED
            
            movlw   ID_GUI>>3         ;Load SID10:SID3 / User configure transmit ID
            movwf   TXB0SIDH,BANKED
            movlw    ID_PIC>>3            ;Load SID10:SID3 / User configure receive ID *
            movwf    RXF0SIDH, BANKED
            
            movlw    b'11111111'     ;Prevent filter 1 from causing a
            movwf    RXF1SIDL        ; receive event
            movwf    RXM0SIDH        ;Set mask
            movlw    b'11100000'
            movwf    RXM0SIDL
            movlb    0                ;Set bit rate
            
            movlw    b'00000001'        ;xxx User configure Baudrate (Default 500kBd with HSPLL and 10MHz crystal)
            movwf    BRGCON1
            movlw    b'11111010'        ;xxx User configure Baudrate
            movwf    BRGCON2
            movlw    b'00000111'        ;xxx User configure Baudrate
            movwf    BRGCON3
            bsf     CIOCON, ENDRHI
            clrf    CANCON            ;Enter Normal mode
        #endif

        ; TURN ON LED ON BOARD TO SHOW BOOTLOADER IS RUNNING
        BCf        TRISD, RD7
        BCF        PORTD, RD7
        ;----------------------------------------------------------------------
        ; Wait for computer
        ;----------------------------------------------------------------------
        clrf    cntHello
        movlw    BLSTART
rhello    rcall     RcvIni
        sublw     HELLO
        bz         sendid        
        ; Not hello received
        incf    cntHello
        movf    cntHello
        sublw    HELLOTRIES
        bz        exit
        bra        rhello        
        
        
        ;----------------------------------------------------------------------
        ; Send device id and firmware version
        ;----------------------------------------------------------------------        
sendid    SendL     DEVICEID
        SendL    (VERMAJ + 128)    ;128 to indicate PIC18F
        SendL    ((VERMIN<<4) + VERREV)
        
        
        ;----------------------------------------------------------------------
        ; Main loop
        ;----------------------------------------------------------------------            
Main    SendL     OK                ; "-Everything OK, ready and waiting."
mainl    clrf     crc
        

        ;----------------------------------------------------------------------
        ; Receive address
        ;----------------------------------------------------------------------            
        ;Upper byte
        rcall     Receive            
        movwf     TBLPTRU
        ; High byte
        rcall     Receive
        movwf     TBLPTRH
        #ifdef    BIGEE
            movwf    EEADRH        ;for eeprom
        #endif        
        ; Low byte
        rcall     Receive
        movwf     TBLPTRL
        #ifdef    EEADR
            movwf     EEADR        ;for eeprom
        #endif

                    
        ;----------------------------------------------------------------------
        ; Receive command
        ;----------------------------------------------------------------------            
        rcall     Receive    
        movwf     cmd    
        
        
        ;----------------------------------------------------------------------
        ; Receive nr of data bytes that will follow
        ;----------------------------------------------------------------------            
        rcall     Receive    
        movwf     dcnt    
        
        
        ;----------------------------------------------------------------------
        ; Receive data
        ;----------------------------------------------------------------------    
        lfsr     FSR0, buffer    ;load buffer pointer to fsr0
rcvoct    rcall     Receive
        movwf     POSTINC0
        decfsz     dcnt
        bra     rcvoct
                
        
        ;----------------------------------------------------------------------
        ; Check checksum
        ;----------------------------------------------------------------------            
chksum    tstfsz     crc                
        bra     crcfail
                    
                
        ;----------------------------------------------------------------------
        ; 0x00 goto protection
        ;----------------------------------------------------------------------    
        #ifdef    PROT_GOTO
            ; Only for write row command
            ;btfss     cmd, 1            
            ;bra     ibufpt        
            ; Check for row 0
            tstfsz    TBLPTRU
            bra        ibufpt
            tstfsz    TBLPTRH
            bra        ibufpt
            tstfsz    TBLPTRL
            bra        ibufpt                        
            ; Init buffer pointer
            lfsr     FSR0, buffer    ;load buffer pointer to fsr0
            ; 1st word low byte = low address byte
            movlw     ((STARTADDR>>1)&0xff)
            movwf    POSTINC0
            ; 1st word high byte = goto instruction
            movlw    0xef
            movwf    POSTINC0
            ; 2nd word low byte = upper address byte
            movlw     (((STARTADDR>>1)&0xff00)>>8)
            movwf    POSTINC0
            ; 2nd word high byte = uppder address nibble + goto instruction            
            movlw     (0xf0 + (((STARTADDR>>1)&0xf0000)>>16))
            movwf    POSTINC0
        #endif
        
        
        ;----------------------------------------------------------------------
        ; Init buffer pointer
        ;----------------------------------------------------------------------            
ibufpt    lfsr     FSR0, buffer    ;load buffer pointer to fsr0
        
        
        ;----------------------------------------------------------------------
        ; Check command
        ;----------------------------------------------------------------------            
        ; Erase page, set do erase status flag
        btfss    cmd, 0
        bra        cmdrow
        setf    doerase
        bra        Main
        ; Write row
cmdrow    btfsc     cmd, 1            
        bra     blprot
        #ifdef    EEDATA
            ; Write eeprom word
            btfsc     cmd, 2            
            bra     eeprom
        #endif
        ; Write config
        btfsc     cmd, 3
        bra     cfg    
        ; Else, unknown command
        SendL   UCMD        
        bra     mainl    
        
                
        ;------------------------------------------------------------------------------
        ; Exit, placed here so it can be branched to from all code, max +-127
        ;------------------------------------------------------------------------------                
        #ifndef USE_CAN
exit        clrf    RCSTA_            ;reset receive status and control register
            clrf    TXSTA_            ;reset transmit status and control register
        #endif
        #ifdef USE_CAN
exit           bsf     CANCON, REQOP2  ;reset receive status and control register
        #endif
        bra     loadusr        
        
                            
        ;----------------------------------------------------------------------
        ; Bootloader protection
        ;----------------------------------------------------------------------
blprot    nop
        #ifdef PROT_BL
            ; Make a copy of address
            movff    TBLPTRU, ttblptru
            movff    TBLPTRH, ttblptrh
            movff    TBLPTRL, ttblptrl
            ; Calculate page number of received address
            movlw    6        ;2^6=64=pagesize[bytes]
            movwf    cnt1
            bcf        STATUS, C    ;clear carry bit
calcpage    rrcf    ttblptru, 1
            rrcf    ttblptrh, 1
            rrcf    ttblptrl, 1
            decfsz    cnt1
            bra        calcpage            
            ; Received page high < bl start page = OK
            movlw    ((STARTADDR/64)>>8)
            subwf    ttblptrh, 0
            bn        blprotok
            ; Received page = bl start page
            movlw    ((STARTADDR/64)>>8)
            subwf    ttblptrh, 0
            bnz        chkgt    
            ; Received page low < bl start page low = OK        
            movlw    ((STARTADDR/64)&0xff)
            subwf    ttblptrl, 0
            bn        blprotok
            ; Received page high > bl end page = OK
chkgt        movlw    (((STARTADDR/64)+BLSIZEP-1)>>8)
            subwf    ttblptrh, 0
            bz        chkgt2
            bn        chkgt2
            bra        blprotok
            ; Received page = bl end page
chkgt2        movlw    (((STARTADDR/64)+BLSIZEP-1)>>8)
            subwf    ttblptrh, 0
            bnz        proterr    
            ; Received page low > bl end page low = OK        
            movlw    (((STARTADDR/64)+BLSIZEP-1)&0xff)
            subwf    ttblptrl, 0
            bz        proterr
            bn        proterr
            bra        blprotok
            ; Protection tripped
proterr        SendL   BLPROT        
            bra     mainl
        #endif
        
        
        ;----------------------------------------------------------------------
        ; Erase page
        ;----------------------------------------------------------------------                    
blprotok
erase    btfss     doerase, 0
        bra     wrloop
        movlw    b'10010100'        ;setup erase
        rcall     Write
        clrf    doerase
        
        
        ;----------------------------------------------------------------------
        ; Write row
        ;----------------------------------------------------------------------            
wrloop    movlw     ROWSIZEB
        movwf     rowcnt    
        movwf    rowcnt2    
        ; Load latches
wrbyte    movff     POSTINC0, TABLAT
        tblwt    *+
        decfsz     rowcnt
        bra     wrbyte        
        ; Write
        tblrd    *-                ;point back into row
        movlw    b'10000100'        
        rcall     Write
        
        
        ;----------------------------------------------------------------------
        ; Write finished, verify row
        ;----------------------------------------------------------------------    
        lfsr     FSR0, (buffer+ROWSIZEB-1)    ;load buffer pointer to fsr0
        ; Read
verbyte    tblrd    *-        
        movf     POSTDEC0, w
        ; Compare
        cpfseq    TABLAT
        bra        verfail
        ; Loop?
        decfsz     rowcnt2
        bra     verbyte        
        ; Verify succesfull
        bra     Main
            
        
        ;----------------------------------------------------------------------
        ; Write eeprom byte
        ;----------------------------------------------------------------------            
        #ifdef    EEADR
            ; Load latch
eeprom        movff    INDF0, EEDATA
            ; Write
            movlw     b'00000100'
            rcall     Write
            ; Verify, read byte
            movlw     b'00000001'
            bsf        EECON1, RD
            movf    INDF0, w
            ; Compare
            cpfseq    EEDATA
            bra        verfail
            ; Verify succesfull
            bra     Main
        #endif
        
                
        ;----------------------------------------------------------------------
        ; Write config byte
        ;----------------------------------------------------------------------                    
        ; Load latch
cfg        movff     INDF0, TABLAT
        tblwt    *
        ; Write
        movlw     b'11000100'
        rcall     Write
        ; Write finished
        bra        Main
        
                
        ;----------------------------------------------------------------------
        ; Verify fail
        ;----------------------------------------------------------------------
verfail    SendL     VERFAIL
        bra     mainl
        
                
        ;----------------------------------------------------------------------
        ; Checksum error
        ;----------------------------------------------------------------------
crcfail    SendL     CHECKSUMERR
        bra     mainl
        
        
;------------------------------------------------------------------------------
; Write()
;------------------------------------------------------------------------------        
Write    movwf     EECON1
        movlw     0x55
        movwf     EECON2
        movlw     0xAA
        movwf     EECON2
        bsf     EECON1, WR
        ; Wait for write to finish, only needed for eeprom
waitwre    btfsc     EECON1, WR
        bra     waitwre
        bcf     EECON1,WREN        ;disable writes
        return
        
        
;------------------------------------------------------------------------------
; Send()
;------------------------------------------------------------------------------        
#ifndef USE_CAN
Send    ; Enable tx
        #ifdef USE_TXENABLE
            bsf    LATR_TXE, LATB_TXE
            nop        ;needed?
            nop
            nop
        #endif        
        ;Send byte
        movwf     TXREG_
        ; Wait for transmit shift register to get empty
txwait    btfss    TXSTA_, TRMT
        bra        txwait
        ; Disable tx
        #ifdef USE_TXENABLE
            bcf    LATR_TXE, LATB_TXE
        #endif
        ; Send complete
        return
#endif
#ifdef USE_CAN
Send
        movlb    15                        ;One BANKSEL
        movwf    TXB0D0,BANKED
        bsf     TXB0CON, TXREQ             ;Normal priority; Request transmission
        ;If required, wait for message to get transmitted
txwait    BTFSC   TXB0CON, TXREQ,BANKED    ;Is it transmitted?
        BRA     txwait                     ;No.  Continue to wait...
        movlb    0
        ; Send complete
        return
#endif

;------------------------------------------------------------------------------
; Receive()
;------------------------------------------------------------------------------    
#ifndef USE_CAN
Receive    movlw     BLDELAY
RcvIni    movwf    cnt1            ;
rpt2    movlw    BL10MS            ;
        movwf    cnt2            ;        
rpt3    clrf     cnt3        
rptc    clrwdt
        btfss     PIR1, RCIF        ;test RX
        bra     notrcv
        movf     RCREG_, w        ;return read data in W
        addwf     crc, f            ;compute crc
        return        
notrcv    decfsz     cnt3
        bra     rptc
        decfsz     cnt2
        bra     rpt3
        decfsz     cnt1
        bra     rpt2
        ; Receive timed out if we get here
        bra        exit
#endif
#ifdef USE_CAN
Receive    movlw     BLDELAY
RcvIni    movwf    cnt1            ;
rpt2    movlw    BL10MS            ;
        movwf    cnt2            ;        
rpt3    clrf     cnt3        
rptc    clrwdt
        btfss    RXB0CON, RXFUL     ;Does RXB0 contain a message?
        bra     notrcv
        movf     RXB0D0, w        ;Return read data in W
        addwf     crc, f            ;Compute crc
        bcf        RXB0CON, RXFUL
        return        
notrcv    decfsz     cnt3
        bra     rptc
        decfsz     cnt2
        bra     rpt3
        decfsz     cnt1
        bra     rpt2
        ; Receive timed out if we get here
        bra        exit
#endif    


;------------------------------------------------------------------------------
; End of code
;
; After reset
; Do not expect the memory to be zero,
; Do not expect registers to be initialised as described in datasheet.
;------------------------------------------------------------------------------            
        end



RE: PIC 18F4580 Problems - Mikael Gustavsson - 2010-11-24

Do you reset the device prior to clicking download?
Is it really a 4MHz crystal? Smile

The bootloader is not placed in the boot block.


RE: PIC 18F4580 Problems - Lotusx - 2010-11-24

Hello,

I don't see anything wrong in your config and I don't think it's deal with BBSIZ.
You use external crystal @ 4Mhz ?

Could you describe step-by-step what you're doing to load your program.

Lotusx

[Grilled by Mikael]


RE: PIC 18F4580 Problems - LMS_EE - 2010-11-24

Hey guys thanks for the responses.

First thing i did was build the .hex file and then load it into my chip. all went well and my LED turned on... so i assume the bootloader is running.

then cycled power.

then i opened ds30 loader and it says validating hex file... then hex file successfully parsed. it also says 0 eeprom words found and 14 config words found.

next i selected my chip. pic18F series and 4580 as the chip.

for the port i select "High-Speed PCIe Serial Port (COM8)" i have a PCIe serial add-on card with two serial ports. this port works fine communicating with the chip through any serial port program including one i wrote to validate parts of my program... so i assume it is functional.

i did notice that the baud rate box sometimes initiates to none standard baud rates ie. 5000, 50000, 10000 etc. instead of 9600, 125000 etc. the only way i can get it to show the correct baud rate options is to go into advanced mode and reset all settings to default, exit and then select the com port again.

once this is all done i select write program and hit download...

then it says:
initiating download..
searching for bl.............. and after 10 seconds gives me a timed out error.

should i be doing something different?

i have also tried the same thing through a laptop with a built in serial port but i get the same error...

Thanks again[/quote]


i have also looked at the serial line when the program is looking for the boot loader and the rx line to the chip does not seem to have "HELLO\r" as a continuous input. the data packet seems to be something to the effect of 11011111... a pulse followed by a small low level then a longer pulse... should it be something other than this?


RE: PIC 18F4580 Problems - Lotusx - 2010-11-24

Hello,

You have to reset your PIC device while the GUI is looking for the bootloader.

Lotusx


RE: PIC 18F4580 Problems - Mikael Gustavsson - 2010-11-24

Exactly,
Either reset the device and press download within 2 seconds (default value)
or
Press download and reset the device within 10 seconds (default value).

If I can reproduce your baudrate selection problem I'll fix that and send you a new version.


RE: PIC 18F4580 Problems - LMS_EE - 2010-11-29

It seems that it is very consistent in starting with the wrong baud rate options in the drop down menu. is this a result of it not connecting to my serial port correctly? have you seen this before?

when i press download and then reset my micro it says:

searching for bl....[discarded null byte].......[discarded null byte].....timed out
Tx 20 bytes / Rx 2 Bytes

the [discarded null byte] appears when i reset my micro. thoughts?

[/align]
Thanks again


RE: PIC 18F4580 Problems - Mikael Gustavsson - 2010-11-29

I have not seen that before.

* Please send me screen-shot just after start-up (or attach to this thread if possible).
* If you delete/move all settings files, what output do you get in the gui just after start-up?
You can open the settings directory from the view menu in advanced mode.

* Have you read the errata to make sure there are no uart related problems?
* Try some other baud rates.

* If you have an oscilloscope, have a look at the uart lines to make sure it looks ok and at correct baud rate.


RE: PIC 18F4580 Problems - LMS_EE - 2010-11-29

So ive noticed that the Baud rate options, upon start up, are reverting to the baud rates for the Canbus option. if i scroll to the canbus port the baud rate options are the same but then if i scroll back to the serial port, they go back to the standard serial port rates. deleting the setting files does not make a difference.

the only errata data for the uart talks about a potential error in 9 bit mode... i assume the boot loader is set up for 8 bit operation?

the attached scope picture is of 14400 baud rate transmitting to the chip. the same pattern appears every 500ms. does this look correct?


RE: PIC 18F4580 Problems - Mikael Gustavsson - 2010-11-29

Is that measured on the PIC rx?
From what I can see, the whole sequence is about 440us.
10 bits (start+8data+stop) gives baud rate 22727.
Please verify your oscillator frequency.


RE: PIC 18F4580 Problems - Lotusx - 2010-11-29

Strange scope screenshot, If it's on PIC RX, we should see 0xC1 ?


RE: PIC 18F4580 Problems - LMS_EE - 2010-11-30

the line i was measuring is the line from the computer to the chip. however i was measuring it before the transceiver chip so not at the PICRX pin. i tried measuring at both places and it seems that when i have data at the input to the transceiver chip (digikey pn ISL4221EIRZ-TTR-ND) i am getting no output to the pic and you can see this in the first attached picture. the top signal is at the PICRX and the bottom is the PCTX line going into the transciever. however, when i load my program directly into the chip and send a 0xC1 via a serial port program i get what is expected on the PICRX line as shown in the second picture.

This tells me that the serial port is not being set up correctly. is there something in the code i am missing specifically in the segment that sets up the serial port? can you guide me to where the uart is set up in the bootloader code?

oh and i am sending at 9600 baud.

Thank a lot in advance

update: i checked the datasheet and confirmed that the uart is set up properly. i have also tried internal and external oscillator settings so i believe it is setup correctly... can anyone think of a way i can check to make sure the internal oscillator is functioning?


RE: PIC 18F4580 Problems - Mikael Gustavsson - 2010-11-30

Maybe I found the solution in the datasheet:
• bit TRISC<7> must be set (= 1)
• bit TRISC<6> must be cleared (= 0) for Asynchronous and Synchronous Master modes

Try to add 2 lines to set/clear them.


RE: PIC 18F4580 Problems - LMS_EE - 2010-12-01

i added:
Code:
            BSF        TRISC, RC7
            BCF        TRISC, RC6

after where the uart is setup... still the same result however, it seems the PICRX line should be high at idle but it remains low after i load the boot loader.

as a reference i have this to set up the serial port:

Code:
            movlw     b'00100100'        ;enable transmit and
            movwf     TXSTA_            ; high speed mode
            movlw     UBRG            ;use only SPBRG_ (8 bit mode default) not using BAUDCON
            movwf     SPBRG_
            movlw     b'10010000'        ;enable serial port
            movwf     RCSTA_            ; and receive
            BANKSEL TRISC
            BSF        TRISC, RC7
            BCF        TRISC, RC6

and this for the oscillator and baud rate:

Code:
    #define        OSCF            4000000             ;xxx oscillator frequency
        #define        BAUDRATE         9600            ;xxx baudrate

with XT selected in the config bits. i also can measure a clock if i look at the clkin pin of the oscillator




RE: PIC 18F4580 Problems - Mikael Gustavsson - 2010-12-02

I'm sorry, I don't know what the problem is.
Since you have a scope at hand, it shouldn't be that hard to find the problem.
Is the baudrate correct?
Is the osciallator working as intended?
Is rx working at all?
Is tx working at all?
Is the rx data ok all the way from pc to pic?
Is the tx data ok all the way from pic to pc?


RE: PIC 18F4580 Problems - LMS_EE - 2010-12-09

So i figured it out! The Problem was not with your boot loader but with the initialization of the pic chip. i misread the schematic which has a 100k pull up resistor on the rs232 chips enable pin. i assumed that the pin was active high but the pin is actually active low and must be pulled low by the chip... my firmware application somehow does this automatically because i am not telling it to ground that pin connected to the enable pin... so after adding that bit of code to pull it low, success! it always turns out to be something easy and obvious in the end Big Grin

Thanks for the help and thanks for making a great boot loader!


RE: PIC 18F4580 Problems - Mikael Gustavsson - 2010-12-09

Great!

Thank you! Smile


RE: PIC 18F4580 Problems - oktayk - 2011-02-15

Hi Friends;

Do you try this bootloader via CANBUS? I'm trying PIC18F4580 with MCP2551 transcivier but I can not take any message from PIC. I can show GUI's message on CAN LINE but PIC is doing nothing. I think I can not organize parameters or adjustments in the firmware. I’m sending my files and my screenshot. I’m using PCanview Software from Peak System for checking CAN line. GUI is sending messages (ID:01 and Data: C1). But I can not see any message from PIC and if I try to write GUI is giving timeout. Could you please check my sending files? What do you think about this problem? If there is any problem in Firmware adjustment, could you please adjust and send correct file to me?






RE: PIC 18F4580 Problems - Mikael Gustavsson - 2011-02-15

Please start a separate thread and we will help you.