mike2545

Bootloader18F

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