Advertisement
mike2545

Bootloader18F

Aug 31st, 2014
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     radix DEC
  2.     LIST      P=18F1320 ; change also: Configure->SelectDevice from Mplab
  3. xtal EQU 8000000        ; you may want to change:
  4.     ;oscil: _HS_OSC_1H  HS extern
  5.     ;   _INTIO1_OSC_1H  Internal RC, OSC1 as RA7, OSC2 as Fosc/4
  6.     ;   _INTIO2_OSC_1H  Internal RC, OSC1 as RA7, OSC2 as RA6
  7. baud EQU 9600           ; standard TinyBld baud rates: 115200 or 19200
  8.     ; The above 3 lines can be changed and buid a bootloader for the desired frequency and PIC type
  9. #define _XTAL_FREQ 4000000 ;//set your xtal frequency here
  10. #define _UART_BAUD 9600  ;//set your baud rate here
  11. #define _UART_BRG ((_XTAL_FREQ / _UART_BAUD) / 64) - 1  ;//variable for setting SPBRG  
  12. ;SPBRG = _UART_BRG   ;//set baud rate
  13.      ; TXSTA = 0b00100000;   ; //tx enable, low speed baud rate generator
  14.      ;RCSTA = 0b10010000;    //serial port enable, continuous rx enable
  15.  
  16.     ;********************************************************************
  17.     ;   Tiny Bootloader     for 18F series      Size=100words
  18.     ;   claudiu.chiculita@ugal.ro
  19.     ;   http://www.ac.ugal.ro/staff/ckiku/software/picbootloader.htm
  20.     ;********************************************************************
  21.    
  22.  
  23.     #include "../icdpictypes.inc"   ;takes care of: #include "p18fxxx.inc",  max_flash, IdTypePIC
  24.     #include "../spbrgselect.inc"   ; RoundResult and baud_rate
  25.  
  26.         #define first_address max_flash-200     ;100 words
  27.  
  28.     __CONFIG _CONFIG1H, _IESO_OFF_1H & _FSCM_OFF_1H & _INTIO2_OSC_1H    
  29.  
  30.     __CONFIG _CONFIG2L, _PWRT_ON_2L & _BOR_ON_2L ;& _BORV_20_2L
  31.     __CONFIG _CONFIG2H, _WDT_OFF_2H & _WDTPS_32K_2H ;
  32.     __CONFIG _CONFIG3H, _MCLRE_ON_3H
  33.     __CONFIG _CONFIG4L, _DEBUG_OFF_4L & _LVP_OFF_4L & _STVR_ON_4L
  34.     __CONFIG _CONFIG5L, _CP0_OFF_5L & _CP1_OFF_5L
  35.     __CONFIG _CONFIG5H, _CPB_OFF_5H & _CPD_OFF_5H
  36.     __CONFIG _CONFIG6L, _WRT0_OFF_6L & _WRT1_OFF_6L
  37.     __CONFIG _CONFIG6H, _WRTC_OFF_6H & _WRTB_OFF_6H & _WRTD_OFF_6H
  38.     __CONFIG _CONFIG7L, _EBTR0_OFF_7L & _EBTR1_OFF_7L
  39.     __CONFIG _CONFIG7H, _EBTRB_OFF_7H
  40.                                
  41.  
  42. ;----------------------------- PROGRAM ---------------------------------
  43.     cblock 0
  44.     crc
  45.     i
  46.     cnt1
  47.     cnt2
  48.     cnt3
  49.     counter_hi
  50.     counter_lo
  51.     flag
  52.     endc
  53.     cblock 10
  54.     buffer:64
  55.     dummy4crc
  56.     endc
  57.    
  58. SendL macro car
  59.     movlw car
  60.     movwf TXREG
  61.     endm
  62.    
  63. ;0000000000000000000000000 RESET 00000000000000000000000000
  64.  
  65.         ORG     0x0000
  66.         GOTO    IntrareBootloader
  67.  
  68. ;&&&&&&&&&&&&&&&&&&&&&&&   START     &&&&&&&&&&&&&&&&&&&&&&
  69. ;----------------------  Bootloader  ----------------------
  70. ;PC_flash:      C1h             U       H       L       x  ...  <64 bytes>   ...  crc  
  71. ;PC_eeprom:     C1h             40h   EEADR   EEDATA    0       crc                
  72. ;PC_cfg         C1h         U OR 80h    H       L       1       byte    crc
  73. ;PIC_response:     type `K`
  74.    
  75.     ORG first_address       ;space to deposit first 4 instr. of user prog.
  76.     nop
  77.     nop
  78.     nop
  79.     nop
  80.     org first_address+8
  81. IntrareBootloader
  82.     movlw b'01110000'       ;switch internal oscillator to 8MHz
  83.     movwf OSCCON
  84.     ; Don't I have to wait for the clock to stabilize ?
  85.     movlw b'01100000'      
  86.     movwf ADCON1            ;disable analog on tx/rx
  87.                             ;init serial port
  88.     movlw b'00100100'
  89.     movwf TXSTA
  90.     movlw spbrg_value
  91.     movwf SPBRG
  92.     movlw b'10010000'
  93.     movwf RCSTA
  94.                             ;wait for computer
  95.     rcall Receive          
  96.     sublw 0xC1              ;Expect C1h
  97.     bnz way_to_exit
  98.     SendL IdTypePIC         ;send PIC type
  99. MainLoop
  100.     SendL 'K'               ; "-Everything OK, ready and waiting."
  101. mainl
  102.     clrf crc
  103.     rcall Receive           ;Upper
  104.     movwf TBLPTRU
  105.         movwf flag          ;(for EEPROM and CFG cases)
  106.     rcall Receive           ;Hi
  107.     movwf TBLPTRH
  108.         movwf EEADR         ;(for EEPROM case)
  109.     rcall Receive           ;Lo
  110.     movwf TBLPTRL
  111.         movwf EEDATA        ;(for EEPROM case)
  112.  
  113.     rcall Receive           ;count
  114.     movwf i
  115.     incf i
  116.     lfsr FSR0, (buffer-1)
  117. rcvoct                      ;read 64+1 bytes
  118.         movwf TABLAT        ;prepare for cfg; => store byte before crc
  119.     rcall Receive
  120.     movwf PREINC0
  121.     decfsz i
  122.     bra rcvoct
  123.    
  124.     tstfsz crc              ;check crc
  125.     bra ziieroare
  126.         btfss flag,6        ;EEPROM data is not supported
  127.         bra noeeprom
  128.         ;;;movlw b'00000100'    ;Setup eeprom
  129.         ;;;rcall Write
  130.         ;;;bra waitwre
  131.         bra ziieroare
  132.  
  133. noeeprom
  134.         btfss flag,7        ;is CFG data?
  135.         bra noconfig
  136.         tblwt*              ;write TABLAT(byte before crc) to TBLPTR***
  137.         movlw b'11000100'   ;Setup cfg
  138.         rcall Write
  139.         bra waitwre
  140. noconfig
  141.                             ;write
  142. eraseloop
  143.     movlw   b'10010100'     ; Setup erase
  144.     rcall Write
  145.     TBLRD*-                 ; point to adr-1
  146.    
  147. writebigloop   
  148.     movlw 8                 ; 8groups
  149.     movwf counter_hi
  150.     lfsr FSR0,buffer
  151. writesloop
  152.     movlw 8                 ; 8bytes = 4instr
  153.     movwf counter_lo
  154. writebyte
  155.     movf POSTINC0,w         ; put 1 byte
  156.     movwf TABLAT
  157.     tblwt+*
  158.     decfsz counter_lo
  159.     bra writebyte
  160.    
  161.     movlw   b'10000100'     ; Setup writes
  162.     rcall Write
  163.     decfsz counter_hi
  164.     bra writesloop
  165. waitwre
  166.     ;btfsc EECON1,WR        ;for eeprom writes (wait to finish write)
  167.     ;bra waitwre            ;no need: round trip time with PC bigger than 4ms
  168.    
  169.     bcf EECON1,WREN         ;disable writes
  170.     bra MainLoop
  171.    
  172. ziieroare                   ;CRC failed
  173.     SendL 'N'
  174.     bra mainl
  175.      
  176. ;******** procedures ******************
  177.  
  178. Write
  179.     movwf EECON1
  180.     movlw 0x55
  181.     movwf EECON2
  182.     movlw 0xAA
  183.     movwf EECON2
  184.     bsf EECON1,WR           ;WRITE
  185.     nop
  186.     ;nop
  187.     return
  188.  
  189.  
  190. Receive
  191.     movlw xtal/2000000+1    ; for 20MHz => 11 => 1second delay
  192.     movwf cnt1
  193. rpt2                       
  194.     clrf cnt2
  195. rpt3
  196.     clrf cnt3
  197. rptc
  198.         btfss PIR1,RCIF         ;test RX
  199.         bra notrcv
  200.         movf RCREG,w            ;return read data in W
  201.         addwf crc,f             ;compute crc
  202.         return
  203. notrcv
  204.     decfsz cnt3
  205.     bra rptc
  206.     decfsz cnt2
  207.     bra rpt3
  208.     decfsz cnt1
  209.     bra rpt2
  210.     ;timeout:
  211. way_to_exit
  212.     bcf RCSTA,  SPEN            ; deactivate UART
  213.     bra first_address
  214. ;*************************************************************
  215. ; After reset
  216. ; Do not expect the memory to be zero,
  217. ; Do not expect registers to be initialised like in catalog.
  218.  
  219.             END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement