Menu

16F1786 can't get serial to respond

Doug C
2016-03-13
2016-03-18
  • Doug C

    Doug C - 2016-03-13

    I'm migrating from the ds30Loader and moving from 16F1783 to 16F1786. I'm using a manual reset (for now) and a FT232RL USB/serial converter. I have not been able to get the pic to respond to the 0xC1. I'm blinking an LED while waiting in the serial receive. The dc30Loader works in this configuration for the 16F1783 so I think the hardware is okay. Also, I expanded the bootloader size 10 words to fit my extra code. Does anyone see what I need to change / add to get the serial connection going?
    Thanks,

        radix   DEC
    
        ; change these lines accordingly to your application    
    #include "p16f1786.inc"
    IdTypePIC = 0x2A        ; Please refer to the table below, must exists in "piccodes.ini"
    #define max_flash  0x2000   ; in WORDS, not bytes!!! (= 'max flash memory' from "piccodes.ini" divided by 2), Please refer to the table below
    
    xtal    EQU     16000000    ; you may also want to change: _HS_OSC _XT_OSC
    baud    EQU     19200       ; standard TinyBld baud rates: 115200 or 19200
    
    ;   #define CHANGE_TX   ;TX Bit Change from default, Please refer to the table
    ;   #define CHANGE_RX   ;RX Bit Change from default, Please refer to the table
    
    ;   The above 7 lines can be changed and buid a bootloader for the desired frequency (and PIC type)
    
    ; +---------+--------+------------+-------------+-------------+-----------+-----------+--------+-----------+------+
    ; |IdTypePIC| Device | Erase_Page |   TX bit    :   RX bit    |   TXSEL   |   RXSEL   | EEPROM | max_flash | PDIP |
    ; |         |        |            | default(Pin)/changed(Pin) |           |           |        |           |      |
    ; +---------+--------+------------+-------------+-------------+-----------+-----------+--------+-----------+------+
    ; |   0x26  |16F1782 |  32 words  |C6(17)/B6(27)|C7(18)/B7(28)| APFCON ,2 | APFCON ,1 |   256  |  0x0800   |  28  |
    ; |   0x28  |16F1783 |  32 words  |C6(17)/B6(27)|C7(18)/B7(28)| APFCON ,2 | APFCON ,1 |   256  |  0x1000   |  28  |
    ; |   0x28  |16F1784 |  32 words  |C6(25)/B6(39)|C7(26)/B7(40)| APFCON1,2 | APFCON1,1 |   256  |  0x1000   |  40  |
    ; |   0x2A  |16F1786 |  32 words  |C6(17)/B6(27)|C7(18)/B7(28)| APFCON1,2 | APFCON1,1 |   256  |  0x2000   |  28  |
    ; |   0x2A  |16F1787 |  32 words  |C6(25)/B6(39)|C7(26)/B7(40)| APFCON1,2 | APFCON1,1 |   256  |  0x2000   |  40  |
    ; |   0x2C  |16F1788 |  32 words  |C6(17)/B6(27)|C7(18)/B7(28)| APFCON1,2 | APFCON1,1 |   256  |  0x4000   |  28  |
    ; |   0x2C  |16F1789 |  32 words  |C6(25)/B6(39)|C7(26)/B7(40)| APFCON1,2 | APFCON1,1 |   256  |  0x4000   |  40  |
    ; +---------+--------+------------+-------------+-------------+-----------+-----------+--------+-----------+------+
    
            ;********************************************************************
        ;   Tiny Bootloader     16F17xx series      Size=100words
            ;       claudiu.chiculita@ugal.ro
            ;       http://www.etc.ugal.ro/cchiculita/software/picbootloader.htm
        ;
        ;   (2014.02.07 Revision 6)
        ;
        ;   This program is only available in Tiny PIC Bootloader +.
        ;
        ;   Tiny PIC Bootloader +
        ;   https://sourceforge.net/projects/tinypicbootload/
        ;
            ;********************************************************************
    
        #include "../spbrgselect.inc"
    
        #define first_address max_flash-110 ; 100 word in size
    
           __CONFIG    _CONFIG1, _FOSC_INTOSC & _BOREN_OFF & _WDTE_OFF & _CP_OFF & _PWRTE_ON & _CPD_OFF & _MCLRE_OFF    
           __CONFIG    _CONFIG2, _LVP_OFF &_STVREN_OFF &_BORV_LO &_PLLEN_ON
    
        errorlevel 1, -305      ; suppress warning msg that takes f as default
    
        cblock 0x20
        buffer:80
        endc
    
        cblock 0x78
        crc
        contor
        i
        cnt1
        cnt2
        cnt3
        flag
        endc
    
    ;0000000000000000000000000 RESET 00000000000000000000000000
    
        org 0x0000
    ;   pagesel IntrareBootloader
    ;   goto    IntrareBootloader
        DW  0x339F      ;bra $-0x60
    
    ;view with TabSize=4
    ;&&&&&&&&&&&&&&&&&&&&&&&   START     &&&&&&&&&&&&&&&&&
    ;----------------------  Bootloader  ----------------------
    ;
    ;PC_flash:    C1h          AddrH  AddrL  nr  ...(DataLo DataHi)...  crc
    ;PC_EEPROM:   C1h          EEADRH  EEADRL  2  EEDATL  EEDATH(=0)    crc
    ;PIC_response:   id   K                                                 K
    
        org     first_address
    ;   nop
    ;   nop
    ;   nop
    ;   nop
    
        org     first_address+4
    IntrareBootloader:
                        ;init int clock & serial port
        movlp   (max_flash>>8)-1 ;this is the page select
        movlw   PIR1            ;FSR1=PIR1
        movwf   FSR1L
    
        banksel OSCCON ;movlb   0x01            ;BANK1
        ;this next line is wrong!
        ;bsf    OSCCON,6        ;internal clock 16MHz
        ; should be 0b01111000
        movlw   0x78 ;0b'01111000'
        movwf   OSCCON
    
        ; set up i/o
        banksel TRISC
        bsf TRISC, 7 ;1=input, set as input for serial
        bcf TRISC, 0 ;0=output, make sure we can drive an led on RC0
    
        movlb   0x02            ;BANK2
        bsf LATC, 0
     #ifdef CHANGE_TX
        bsf FVRCON+6,TXSEL      ;APFCON.or.APFCON1
     #else
        bcf FVRCON+6,TXSEL
     #endif
    
     #ifdef CHANGE_RX
        bsf FVRCON+6,RXSEL      ;APFCON.or.APFCON1
     #else
        bcf FVRCON+6,RXSEL
     #endif
    
        movlb   0x03            ;BANK3
        clrf    ANSELA ;turn off analog
        clrf    ANSELB
    
        ;debug led on LATC0
        banksel LATC
        bsf LATC, 0
    
        ;baud
        banksel SPBRG
        movlw   spbrg_value
        movwf   SPBRG
    
        ;transmit
        movlw   b'00100100' ; BRGH=1, TXEN=1
        movwf   TXSTA
    
        ; receive
        movlw   b'10010000'
        movwf   RCSTA
    
        ;all setup and ready to go
        ;wait for computer
        call    Receive         
        sublw   0xC1            ;Expect C1
        skpz
        bra way_to_exit
        movlw   IdTypePIC       ;PIC type
        movwf   TXREG
    ;   SendL   IdSoftVer       ;firmware ver x
    
    MainLoop:
        movlw   'B'
    mainl:
        movwf   TXREG
        clrf    crc
        call    Receive         ;H
        movwf   EEADRH
        movwf   flag
        call    Receive         ;L
        movwf   EEADR
    
        call    FSReceive       ;count
        movwf   contor
        movwf   i
    ;   movlw   buffer
    ;   movwf   FSR0L
    
    rcvoct:
        call    Receive         ;data
        movwi   INDF0++
        decfsz  i,f
        bra rcvoct
    
        call    FSReceive       ;crc
    ziieroare:
        movlw   'N'
        skpz
        bra mainl
                        ;write
    ;   movlw   buffer
    ;   movwf   FSR0L
    
    writeloop:              ;write 2 bytes = 1 instruction
        moviw   INDF0++
        movwf   EEDATL
        moviw   INDF0++
        movwf   EEDATH
    
        btfsc   contor,6        ;if writing to EEPROM, skip erase operation.
        call    wr_e            ;erase operation
        call    wr_w            ;write operation
        incf    EEADRL,f        ;does not cross zones
        decf    contor,f
        decfsz  contor,f
        bra writeloop
        bra MainLoop
    
    FSReceive
        movlw   buffer
        movwf   FSR0L
    Receive
        movlw   xtal/2000000+1      ;for 20MHz => 11 => 1second
        movwf   cnt1
    rpt2
        clrf    cnt2
        ;debug led
        banksel LATC
        movlw   0x01    ; set up xor for toggling RC0
        xorwf   LATC, f ; toggle RC0 - XOR W with the PORTC register and store in PORTC
        banksel RCREG
    rpt3
        clrf    cnt3
    rptc
        ;btfss  INDF1,RCIF      ;test RX
        btfss   PIR1,RCIF       ;test RX
        bra     $+4         ;not recive
        movf    RCREG,w         ;return in w
        addwf   crc,f           ;compute checksum
        return
    
        decfsz  cnt3,f
        bra rptc
        decfsz  cnt2,f
        bra rpt3
        decfsz  cnt1,f
        bra rpt2
                        ;timeout:
    way_to_exit             ;exit in all other cases; must be BANK3
        bcf RCSTA,SPEN      ;deactivate UART
        movlb   0x00            ;BANK0
        clrf    PCLATH
        bra first_address       ; PCLATH=0, Please do not change the GOTO instruction.
    
    wr_e
        bsf     EECON1,FREE
    wr_w
    ;   bcf     EECON1,CFGS         ;Deselect Config space
    ;   bcf EECON1,EEPGD
        btfss   flag,6          ;is eeprom (or flash)
        bsf EECON1,EEPGD
        bsf EECON1,WREN
        movlw   0x55
        movwf   EECON2
        movlw   0xaa
        movwf   EECON2  
        bsf EECON1,WR
        nop
        nop
        clrf    EECON1
        return
    ;*************************************************************
    ; After reset
    ; Do not expect the memory to be zero,
    ; Do not expect registers to be initialised like in catalog.
    
             end
    
     
  • Dan

    Dan - 2016-03-14

    Hi,Doug C

    define first_address max_flash-110 ; 100 word in size

    16F1786's "Erase Page" is "32Words" So 110 is not allowed.

    reference

    PIC18F26k22 hex file not sent

    https://sourceforge.net/p/tinypicbootload/discussion/help/thread/9b344cbe/#d7f6

    Best regards,
    Dan

     
  • Doug C

    Doug C - 2016-03-14

    Thank you Dan. I will make that change tonight. I have enough flash to hold my program and a larger bootloader. More questions though ... of course...

    The calculations for the PIC18F26K22 are
    (32x2)x3+4x2=200 (bytes)
    (32x2)x4+4x2=264 (bytes)
    What are the correct calculations for the 16F178x series? (32)x3+4=100, or (32)x4+4=132, or (32)x5+4=164?

    Also, would using the wrong first_address affect the initial serial communications?
    Thanks.

     
  • Dan

    Dan - 2016-03-15

    Hi,Doug C

    (32x2)×3+4×2=200 (bytes)

    The configuration of the firmware is a combination of "the not write-protected area" and "the write-protected area".

    4 words of the beginning of the firmware will be rewritten when writing the user program.

    Since the second half of the 96 words is the body of the boot loader, rewrite the boot loader is broken.

    reference
    http://tinypicbootload.sourceforge.net/pic16_firmwares.html

    Correct formula for PIC16F is shown by the following formula when the size of the erase page P.

    nP+4 (words)

    PIC16 will PIC18F equivalent and regarded as a 14-bit = 2 bytes because it is 14-bit.

    Best regards,
    Dan

     

    Last edit: Dan 2016-03-15
  • Doug C

    Doug C - 2016-03-17

    Just to follow up for anyone who reads this thread.
    The problem with the serial connection was in code I changed (of course). Dan uses the FSR register to get to the PIR register in the serial read. I tried to directly address the PIR1 register without first setting the bank. The fix is to leave Dan's code alone. And now I know how to use the FSR registers.

    The bootloader connects and loads a hex file, but there are still a few issues in that my user code (written in C) won't run and the bootloader runs very slow after a bootload. I'll keep working and will eventually figure it out. I only have about an hour/day to work on this so it will be slow progress.

    Dan, thank you for publishing your bootloader. It is extremely helpful.

     
  • Doug C

    Doug C - 2016-03-17

    Dan, another question on the size calculation...
    the PIC16 formula is nP+4 words.
    The math for three pages is 32×3+4=100 (words) or (32x2)x3+(4x2)=200 (bytes)
    the code for the default size loader first address is
    max_flash - 100 (this is words, yes?)
    so to increase the bootloader size by one page I need to use
    (32x2)x4+(4x2) = 264 (bytes) and the calculation in the code should be
    max_flash - 132 (words). Yes?
    Thanks.

     
  • Doug C

    Doug C - 2016-03-18

    I answered my own questions...
    the size in the .asm file is in words (132 words)
    the size in the .ini file is in bytes (264 bytes)
    It's all good now.

    The only change I had to make from the baseline was setting OSCCON. Setting only bit6 does not generate 16MHz. Need to set bits <6:3> to get 16MHz. And I turned PLL OFF.

    Thank you again Dan.

     

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.