Advertisement
mike2545

Untitled

Aug 28th, 2014
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;------------------------------------------------------------------------------
  2. ;
  3. ; Title:            ds30 Loader for PIC18F
  4. ;
  5. ; File description: Main firmwarefile
  6. ;
  7. ; Copyright:        Copyright 2009-2012 Mikael Gustafsson
  8. ;
  9. ; Version:          3.0.2 March 2012
  10. ;
  11. ; Webpage:          http://mrmackey.no-ip.org/elektronik/ds30loader/
  12. ;
  13. ; Thanks to:        Claudio Chiculita, this code is based on the Tiny PIC bootloader
  14. ;
  15. ; History:          3.0.2 Improvement: added delay for tx enable pin
  16. ;                   3.0.1 -
  17. ;                   3.0.0 New feature: compatible with ds30 Secure Loader
  18. ;                         New feature: auto baud rate detection
  19. ;                         Bugifx: bootloader protection was calculated wrong on PIC18F devices with 64 byte pagesize                         
  20. ;                         Bugfix: receiving non hello command on start-up caused long delay
  21. ;                         Improvement: merged PIC18FJ firmware
  22. ;                         Improvement: increased range of timeout values
  23. ;                   2.0.5 Bugfix: devices with pagesize != 32 words
  24. ;                   2.0.4 Bugfix: uart 1 rx interupt flag was checked also for uart 2
  25. ;                         New feature: 16-bit brg
  26. ;                   2.0.3 Bugfix: In some case, CAN engine won't start (bad state on RB2)
  27. ;                         Bugfix: goto protection not working for some devices
  28. ;                         Improvement: renamed fosc to oscf for compatibility with some devices
  29. ;                   2.0.2 Change: node ID configuration moved in settings.inc
  30. ;                         New feature: goto protection
  31. ;                         New feature: bl protection
  32. ;                         Change: size is 7 pages
  33. ;                         Bugfix: compatible with devices that has no eeprom (2450, 4450 and more)
  34. ;                   2.0.1 New feature: adjustable non hello discard, HELLORETRIES setting                    
  35. ;                         Improvement: compatible with include files containing both TXSTA and TXSTA1 definitions
  36. ;                         Improvement: 5 or 7 (see section Init) more instructions free to use (CAN)
  37. ;                   2.0.0 CAN support
  38. ;                   1.0.2 Replaced old baudrate calculator 
  39. ;                   1.0.1 Erase is now made just before write to increase reliability
  40. ;                   1.0.0 Separated boot timeout and receive timeout
  41. ;                         Added range check of times and brg
  42. ;                   0.9.2 Changed bootloader size to 5 pages (to make room for more user init code)
  43. ;                         Added tx enable support
  44. ;                   0.9.1 1 more instruction free to use
  45. ;                         Added watchdog clear
  46. ;                         Fixed baudrate error check
  47. ;                         Correct buffer size
  48. ;                   0.9.0 Initial release                                                                            
  49. ;------------------------------------------------------------------------------
  50.  
  51. ;-----------------------------------------------------------------------------
  52. ;    This file is part of ds30 Loader.
  53. ;
  54. ;    ds30 Loader is free software: you can redistribute it and/or modify
  55. ;    it under the terms of the GNU General Public License as published by
  56. ;    the Free Software Foundation.
  57. ;
  58. ;    ds30 Loader is distributed in the hope that it will be useful,
  59. ;    but WITHOUT ANY WARRANTY; without even the implied warranty of
  60. ;    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  61. ;    GNU General Public License for more details.
  62. ;
  63. ;    You should have received a copy of the GNU General Public License
  64. ;    along with ds30 Loader. If not, see <http://www.gnu.org/licenses/>.
  65. ;------------------------------------------------------------------------------
  66.  
  67.  
  68. ;------------------------------------------------------------------------------
  69. ; Includes
  70. ;------------------------------------------------------------------------------
  71.         #include "settings.inc"
  72.         #include "user_code.inc"
  73.        
  74.        
  75. ;------------------------------------------------------------------------------
  76. ; Defines
  77. ;------------------------------------------------------------------------------
  78.         #define VERMAJ              3           ;firmware version major
  79.         #define VERMIN              0           ;firmware version minor
  80.         #define VERREV              2           ;firmware version revision
  81.        
  82.         #define HELLO               0xC1
  83.         #define OK                  'K'         ;erase/write ok
  84.         #define CHECKSUMERR         'N'         ;checksum error
  85.         #define VERFAIL             'V'         ;verification failed
  86.         #define BLPROT              'P'         ;bl protection tripped
  87.         #define UCMD                'U'         ;unknown command       
  88.        
  89.         #define PWOK                0xFE   
  90.  
  91.        
  92.         if BLINIT < 2500
  93.             #define TMRBASE 10
  94.         else
  95.             #define TMRBASE 30
  96.         endif
  97.         #define DELBASE     ( (TMRBASE * OSCF) / (3+(4000 * 255 * 6)) )             ;delay
  98.         #define BLSTART     ( BLINIT / TMRBASE )                                ;count for boot receive delay
  99.         #define BLDELAY     ( BLTIME / TMRBASE )                                ;count for receive delay
  100.        
  101.         ifndef USE_BRG16
  102.             #define UBRG    ( (((OSCF / BAUDRATE) / 8) - 1) / 2 )               ;baudrate
  103.         else
  104.             #define UBRG    ( (((OSCF / BAUDRATE) / 2) - 1) / 2 )               ;baudrate
  105.         endif
  106.        
  107.         #define PAGESIZER   (PAGESIZEW/ROWSIZEW)                                ;pagesize [rows]
  108.         #define ROWSIZEB    (ROWSIZEW*2)                                        ;rowsize [bytes]
  109.         #define STARTADDR   ( MAX_FLASH - BLPLP * PAGESIZER * ROWSIZEW * 2 )    ;bootloader placement
  110.        
  111.         ifdef   IS_PIC18F
  112.             #define ERASE_CODE  b'10010100'
  113.             #define WRITE_CODE  b'10000100'
  114.         else
  115.             #define ERASE_CODE  b'00010100'
  116.             #define WRITE_CODE  b'00000100'
  117.         endif
  118.        
  119.         ; Debug output
  120.         ;messg  STARTADDR_IS    #v(STARTADDR)
  121.         ;messg  UBRG_IS         #v(UBRG)
  122.         ;messg  delbaesis       #v(DELBASE)
  123.         ;messg  blstartis       #v(BLSTART)
  124.         ;messg  bldelayis       #v(BLDELAY)
  125.  
  126.                
  127. ;------------------------------------------------------------------------------
  128. ; Range check
  129. ;------------------------------------------------------------------------------
  130.         if DELBASE > 255
  131.             error overflow in delay calculation
  132.         endif
  133.         if BLSTART > 255
  134.             error BLSTART_ is out of range
  135.         endif
  136.         if BLSTART == 0
  137.             error BLSTART might be out of range
  138.         endif          
  139.        
  140.         if BLDELAY > 255
  141.             error BLDELAY_ is out of range
  142.         endif          
  143.         if BLDELAY == 0
  144.             error BLDELAY_ might be out of range
  145.         endif
  146.        
  147.        
  148. ;------------------------------------------------------------------------------
  149. ; Variables
  150. ;------------------------------------------------------------------------------    
  151.             #define BUFFERSIZE (ROWSIZEB + 1 ) ;row + checksum
  152.             cblock 0x0
  153.                 buffer      :   BUFFERSIZE
  154.                 crc         :   1   ;receive checksum
  155.                 dcnt        :   1   ;datacount
  156.                 dcnt2       :   1   ;datacount
  157.                 cnt1        :   1   ;receive timeout timer
  158.                 cnt2        :   1   ;receive timeout timer
  159.                 cnt3        :   1   ;receive timeout timer
  160.                 cntHello    :   1   ;
  161.                 rowcnt      :   1   ;row iterator
  162.                 rowcnt2     :   1   ;
  163.                 cmd         :   1   ;command
  164.                 doerase     :   1   ;do erase before next write
  165.                 ttblptru    :   1
  166.                 ttblptrh    :   1
  167.                 ttblptrl    :   1  
  168.                 Delay1      :   1
  169.                 Delay2      :   1
  170.                 serbuf      :   1
  171.                 TEMP        :   1          
  172.             endc
  173.                
  174.        
  175. ;------------------------------------------------------------------------------
  176. ; Globals & externals
  177. ;------------------------------------------------------------------------------
  178.        
  179.        
  180. ;------------------------------------------------------------------------------
  181. ; Macros
  182. ;------------------------------------------------------------------------------    
  183. SendL   macro sbyte
  184.             movlw   sbyte
  185.             rcall   Send
  186.         endm
  187.        
  188.  
  189.    
  190. ;------------------------------------------------------------------------------
  191. ; Reset vector
  192. ;------------------------------------------------------------------------------
  193.         org     0x0000
  194.         goto    blstart
  195.  
  196.  
  197. ;------------------------------------------------------------------------------
  198. ; GOTO user application
  199. ;------------------------------------------------------------------------------    
  200.         org     STARTADDR - 4   ;space to deposit goto to user application
  201. loadusr nop
  202.         nop
  203.    
  204.    
  205. ;------------------------------------------------------------------------------
  206. ; Start of bootloader code
  207. ;------------------------------------------------------------------------------    
  208.         org     STARTADDR
  209. blstart
  210.  
  211.        
  212. ;------------------------------------------------------------------------------
  213. ; Trouble shooting tools
  214. ;------------------------------------------------------------------------------            
  215.         ; Toggle pin, frequency on pin = PIC frequency / 16
  216.         if 0
  217.             bcf TRISx, TRISxx
  218. tsfloop     bsf LATx, LATxx
  219.             bcf LATx, LATxx
  220.             bra tsfloop
  221.         endif
  222.        
  223.        
  224. ;------------------------------------------------------------------------------
  225. ; Init
  226. ;------------------------------------------------------------------------------
  227.         UserInit                ;macro in user_code.inc
  228.         clrf    doerase
  229.         rcall   CommInit
  230.        
  231.        
  232. ;----------------------------------------------------------------------
  233. ; Wait for computer
  234. ;----------------------------------------------------------------------
  235.         clrf    cntHello
  236. rhello  movlw   BLSTART
  237.         rcall   RcvIni
  238.         sublw   HELLO
  239.         bz      rhellook       
  240.         ; Not hello received
  241.         incf    cntHello
  242.         movf    cntHello, w
  243.         sublw   HELLOTRIES
  244.         bz      exit
  245.         bra     rhello     
  246. rhellook
  247.  
  248.        
  249. ;----------------------------------------------------------------------
  250. ; Send device id and firmware version
  251. ;----------------------------------------------------------------------    
  252. sendid  SendL   ( DEVICEID & 0xff )
  253.         SendL   ( ((DEVICEID&0x100)>>1) + VERMAJ )
  254.         SendL   ( ((DEVICEID&0x200)>>2) + (VERMIN<<4) + VERREV )
  255.        
  256.        
  257. ;----------------------------------------------------------------------
  258. ; Main loop
  259. ;----------------------------------------------------------------------        
  260. Main    SendL   OK              ;last command was successfull, waiting for next
  261. mainl   clrf    crc
  262.        
  263.  
  264.         ;----------------------------------------------------------------------
  265.         ; Receive address
  266.         ;----------------------------------------------------------------------        
  267.         ;Upper byte
  268.         rcall   Receive        
  269.         movwf   TBLPTRU
  270.         ; High byte
  271.         rcall   Receive
  272.         movwf   TBLPTRH
  273.         ifdef   BIGEE
  274.             movwf   EEADRH      ;for eeprom
  275.         endif      
  276.         ; Low byte
  277.         rcall   Receive
  278.         movwf   TBLPTRL
  279.         ifdef   EEADR
  280.             movwf   EEADR       ;for eeprom
  281.         endif
  282.  
  283.                    
  284.         ;----------------------------------------------------------------------
  285.         ; Receive command
  286.         ;----------------------------------------------------------------------        
  287.         rcall   Receive
  288.         movwf   cmd
  289.        
  290.        
  291.         ;----------------------------------------------------------------------
  292.         ; Receive nr of data bytes that will follow
  293.         ;----------------------------------------------------------------------        
  294.         rcall   Receive
  295.         movwf   dcnt   
  296.         movwf   dcnt2  
  297.  
  298.  
  299.        
  300.        
  301.         ;----------------------------------------------------------------------
  302.         ; Receive data
  303.         ;----------------------------------------------------------------------
  304.         lfsr    FSR0, buffer    ;load buffer pointer to fsr0
  305. rcvoct  rcall   Receive
  306.         movwf   POSTINC0
  307.         decfsz  dcnt
  308.         bra     rcvoct
  309.                
  310.        
  311.         ;----------------------------------------------------------------------
  312.         ; Check checksum
  313.         ;----------------------------------------------------------------------        
  314.         tstfsz  crc            
  315.         bra     crcfail        
  316. chksumok
  317.        
  318.        
  319.    
  320.  
  321.                    
  322.                
  323.         ;----------------------------------------------------------------------
  324.         ; 0x00 goto protection
  325.         ;----------------------------------------------------------------------
  326.         ifdef   PROT_GOTO
  327.             ; Only for write row command
  328.             btfss   cmd, 1         
  329.             bra     protgotook     
  330.             ; Check for row 0
  331.             tstfsz  TBLPTRU
  332.             bra     protgotook
  333.             tstfsz  TBLPTRH
  334.             bra     protgotook
  335.             tstfsz  TBLPTRL
  336.             bra     protgotook                     
  337.             ; Init buffer pointer
  338.             lfsr    FSR0, buffer    ;load buffer pointer to fsr0
  339.             ; 1st word low byte = low address byte
  340.             movlw   ((STARTADDR>>1)&0xff)
  341.             movwf   POSTINC0
  342.             ; 1st word high byte = goto instruction
  343.             movlw   0xef
  344.             movwf   POSTINC0
  345.             ; 2nd word low byte = upper address byte
  346.             movlw   (((STARTADDR>>1)&0xff00)>>8)
  347.             movwf   POSTINC0
  348.             ; 2nd word high byte = uppder address nibble + goto instruction        
  349.             movlw   (0xf0 + (((STARTADDR>>1)&0xf0000)>>16))
  350.             movwf   POSTINC0
  351. protgotook
  352.         endif
  353.        
  354.        
  355.         ;----------------------------------------------------------------------
  356.         ; Init buffer pointer
  357.         ;----------------------------------------------------------------------        
  358.         lfsr    FSR0, buffer    ;load buffer pointer to fsr0
  359.        
  360.        
  361.         ;----------------------------------------------------------------------
  362.         ; Check command
  363.         ;----------------------------------------------------------------------
  364.        
  365.         ; Erase page, set do erase status flag
  366.         btfss   cmd, 0
  367.         bra     cmdrow
  368.         setf    doerase
  369.         bra     Main
  370.         ; Write row
  371. cmdrow  btfsc   cmd, 1         
  372.         bra     blprot
  373.         ; Write eeprom word
  374.         ifdef   HAS_EE         
  375.             btfsc   cmd, 2         
  376.             bra     eeprom
  377.         endif
  378.         ; Write config
  379.         btfsc   cmd, 3
  380.         bra     cfg
  381.         ; Else, unknown command
  382.         SendL   UCMD       
  383.         bra     mainl              
  384.        
  385.                
  386.         ;------------------------------------------------------------------------------
  387.         ; Exit, placed here so it can be branched to from all code, max +-127
  388.         ;------------------------------------------------------------------------------            
  389. exit
  390.         rcall   CommExit
  391.         UserExit                        ;macro in user_code.inc
  392.         bra     loadusr    
  393.        
  394.                            
  395.         ;----------------------------------------------------------------------
  396.         ; Bootloader protection
  397.         ;----------------------------------------------------------------------
  398. blprot  nop
  399.         ifdef PROT_BL
  400.             if PAGESIZEW == 16
  401.                 #define ROTROUNDS   5
  402.             endif
  403.             if PAGESIZEW == 32          ;most PIC18F
  404.                 #define ROTROUNDS   6
  405.             endif
  406.             if PAGESIZEW == 64          ;some PIC18F
  407.                 #define ROTROUNDS   7
  408.             endif
  409.             if PAGESIZEW == 128
  410.                 #define ROTROUNDS   8
  411.             endif
  412.             if PAGESIZEW == 256
  413.                 #define ROTROUNDS   9
  414.             endif
  415.             if PAGESIZEW == 512         ;PIC18FJ
  416.                 #define ROTROUNDS   10
  417.             endif
  418.             ; Make a copy of address
  419.             movff   TBLPTRU, ttblptru
  420.             movff   TBLPTRH, ttblptrh
  421.             movff   TBLPTRL, ttblptrl
  422.             ; Calculate page number of received address
  423.             movlw   ROTROUNDS       ;2^6=64=pagesize[bytes]
  424.             #undefine   ROTROUNDS
  425.             movwf   cnt1
  426.             bcf     STATUS, C   ;clear carry bit
  427. calcpage    rrcf    ttblptru, 1
  428.             rrcf    ttblptrh, 1
  429.             rrcf    ttblptrl, 1
  430.             decfsz  cnt1
  431.             bra     calcpage           
  432.             ; Received page high < bl start page = OK
  433.             movlw   ((STARTADDR/(PAGESIZEW*2))>>8)
  434.             subwf   ttblptrh, 0
  435.             bn      blprotok
  436.             ; Received page = bl start page
  437.             movlw   ((STARTADDR/(PAGESIZEW*2))>>8)
  438.             subwf   ttblptrh, 0
  439.             bnz     chkgt  
  440.             ; Received page low < bl start page low = OK       
  441.             movlw   ((STARTADDR/(PAGESIZEW*2))&0xff)
  442.             subwf   ttblptrl, 0
  443.             bn      blprotok
  444.             ; Received page high > bl end page = OK
  445. chkgt       movlw   (((STARTADDR/(PAGESIZEW*2))+BLSIZEP-1)>>8)
  446.             subwf   ttblptrh, 0
  447.             bz      chkgt2
  448.             bn      chkgt2
  449.             bra     blprotok
  450.             ; Received page = bl end page
  451. chkgt2      movlw   (((STARTADDR/(PAGESIZEW*2))+BLSIZEP-1)>>8)
  452.             subwf   ttblptrh, 0
  453.             bnz     proterr
  454.             ; Received page low > bl end page low = OK     
  455.             movlw   (((STARTADDR/(PAGESIZEW*2))+BLSIZEP-1)&0xff)
  456.             subwf   ttblptrl, 0
  457.             bz      proterr
  458.             bn      proterr
  459.             bra     blprotok
  460.             ; Protection tripped
  461. proterr     SendL   BLPROT     
  462.             bra     mainl
  463.         endif
  464.        
  465.        
  466.         ;----------------------------------------------------------------------
  467.         ; Erase page
  468.         ;----------------------------------------------------------------------                
  469. blprotok
  470. erase   btfss   doerase, 0
  471.         bra     wrloop
  472.         movlw   ERASE_CODE      ;setup erase
  473.         rcall   Write
  474.         clrf    doerase
  475.        
  476.        
  477.         ;----------------------------------------------------------------------
  478.         ; Write row
  479.         ;----------------------------------------------------------------------        
  480. wrloop  movlw   ROWSIZEB
  481.         movwf   rowcnt 
  482.         movwf   rowcnt2
  483.         ; Load latches
  484. wrbyte  movff   POSTINC0, TABLAT
  485.         tblwt   *+
  486.         decfsz  rowcnt
  487.         bra     wrbyte     
  488.         ; Write
  489.         tblrd   *-              ;point back into row
  490.         movlw   WRITE_CODE     
  491.         rcall   Write
  492.        
  493.        
  494.         ;----------------------------------------------------------------------
  495.         ; Verify row
  496.         ;----------------------------------------------------------------------
  497.         ifdef WRITE_VER
  498.             lfsr    FSR0, (buffer+(ROWSIZEB-1)) ;load buffer pointer to fsr0
  499.             ; Read
  500. verbyte     tblrd   *-     
  501.             movf    POSTDEC0, w
  502.             ; Compare
  503.             cpfseq  TABLAT
  504.             bra     verfail
  505.             ; Loop?
  506.             decfsz  rowcnt2
  507.             bra     verbyte    
  508.             ; Verify succesfull
  509.         endif
  510.         bra     Main
  511.            
  512.        
  513.         ;----------------------------------------------------------------------
  514.         ; Erase, write & verify eeprom word
  515.         ;----------------------------------------------------------------------        
  516.         ifdef   EEADR
  517.             ; Load latch
  518. eeprom      movff   INDF0, EEDATA
  519.             ; Write
  520.             movlw   b'00000100'
  521.             rcall   Write
  522.             ifdef EWRITE_VER
  523.                 ; Verify, read byte
  524.                 movlw   b'00000001'
  525.                 bsf     EECON1, RD
  526.                 movf    INDF0, w
  527.                 ; Compare
  528.                 cpfseq  EEDATA
  529.                 bra     verfail
  530.                 ; Verify succesfull
  531.             endif
  532.             bra     Main
  533.         endif
  534.        
  535.                
  536.         ;----------------------------------------------------------------------
  537.         ; Write config byte
  538.         ;----------------------------------------------------------------------                
  539.         ; Load latch
  540. cfg     movff   INDF0, TABLAT
  541.         tblwt   *
  542.         ; Write
  543.         movlw   b'11000100'
  544.         rcall   Write
  545.         ; Write finished
  546.         bra     Main
  547.        
  548.                
  549.         ;----------------------------------------------------------------------
  550.         ; Verify fail
  551.         ;----------------------------------------------------------------------
  552. verfail SendL   VERFAIL
  553.         bra     mainl
  554.        
  555.                
  556.         ;----------------------------------------------------------------------
  557.         ; Checksum error
  558.         ;----------------------------------------------------------------------
  559. crcfail SendL   CHECKSUMERR
  560.         bra     mainl
  561.        
  562.        
  563. ;------------------------------------------------------------------------------
  564. ; Write()
  565. ;------------------------------------------------------------------------------    
  566. Write   movwf   EECON1
  567.         movlw   0x55
  568.         movwf   EECON2
  569.         movlw   0xAA
  570.         movwf   EECON2
  571.         bsf     EECON1, WR
  572.         ; Wait for write to finish, only needed for eeprom
  573. waitwre btfsc   EECON1, WR
  574.         bra     waitwre
  575.         bcf     EECON1,WREN     ;disable writes
  576.         return     
  577.        
  578.  
  579. ;------------------------------------------------------------------------------
  580. ; Functions
  581. ;------------------------------------------------------------------------------
  582.         #include "uart.inc"
  583.  
  584.  
  585. ;------------------------------------------------------------------------------
  586. ; End of code
  587. ;
  588. ; After reset
  589. ; Do not expect the memory to be zero,
  590. ; Do not expect registers to be initialised as described in datasheet.
  591. ;------------------------------------------------------------------------------        
  592.         end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement