-
Notifications
You must be signed in to change notification settings - Fork 4
/
PIC18 Bootloader.asm
1079 lines (925 loc) · 38 KB
/
PIC18 Bootloader.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
; Copyright (c) 2002-2011, Microchip Technology Inc.
;
; Microchip licenses this software to you solely for use with Microchip
; products. The software is owned by Microchip and its licensors, and
; is protected under applicable copyright laws. All rights reserved.
;
; SOFTWARE IS PROVIDED "AS IS." MICROCHIP EXPRESSLY DISCLAIMS ANY
; WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED, INCLUDING BUT
; NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
; FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. IN NO EVENT SHALL
; MICROCHIP BE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR
; CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, HARM TO YOUR
; EQUIPMENT, COST OF PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY
; OR SERVICES, ANY CLAIMS BY THIRD PARTIES (INCLUDING BUT NOT LIMITED
; TO ANY DEFENSE THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION,
; OR OTHER SIMILAR COSTS.
;
; To the fullest extent allowed by law, Microchip and its licensors
; liability shall not exceed the amount of fees, if any, that you
; have paid directly to Microchip to use this software.
;
; MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE
; OF THESE TERMS.
;
; Author Date Comment
; ************************************************************************
; E. Schlunder 07/20/2010 Software Boot Block Write Protect code
; improved. 96KB memory size devices should
; work now.
; E. Schlunder 02/26/2010 Changed order of start up code so that PLLEN
; is enabled before we wait for RXD IDLE state.
; This improves connection time/reliablity on
; J devices.
; E. Schlunder 08/28/2009 Software Boot Block Write Protect option.
; E. Schlunder 07/09/2009 Brought back support for bootloader at
; address 0 for hardware boot block write
; protection on certain devices.
; E. Schlunder 05/07/2009 Replaced the simple checksum with a
; 16-bit CCIT CRC checksum.
; Added ReadFlashCrc command for quick verify.
; E. Schlunder 05/02/2009 Improved autobaud code to handle 1Mbps
; and BRG16/BRGH.
; E. Schlunder 05/01/2009 Added support for DEVICES.INC generated
; from Device Database tool.
; E. Schlunder 04/29/2009 Added support for locating the bootloader
; at the end of program memory instead of
; the beginning. This will eventually let us
; use normal application firmware code without
; linker script modifications.
; E. Schlunder 04/26/2009 Optimized Config Write routine to avoid
; re-writing values matching existing config
; data.
; E. Schlunder 04/24/2009 Optimized EEPROM Write routine a little bit.
; Optimized FLASH Read routine to stream data
; directly from FLASH instead of using RAM
; buffer.
; Added option for faster STX acknowledgements.
; E. Schlunder 04/17/2009 Added code to enter bootloader mode if
; a serial break condition is detected on
; RXD as we come out of reset. This will
; make it possible to re-enter the bootloader
; even if the application firmware is missing
; code to re-enter bootloader mode.
; This also simplies the bootloader, as
; we do not need a boot flag any more.
; E. Schlunder 04/15/2009 Removed EOF command 8, new PC software
; does 64 byte block aligned writes at all
; times on J device, so there is no need for
; this command going forward.
; E. Schlunder 04/14/2009 Added a BootloadMode vector back at the
; beginning of program memory so that user
; applications can jump back into the boot
; loader without having to erase the boot flag.
; E. Schlunder 04/08/2009 Now initializes FSR2 to 0 so that the code
; can operate under Extended Instruction Set
; mode if necessary.
; E. Schlunder 04/01/2009 Fixed bug in J_FLASH erase address increment.
; Added support for enabling PLL.
; Added support for inverted UART signaling.
; Added support for fixed (non-autobaud)
; operation, helps with debugging code under ICD.
; E. Schlunder 03/25/2009 No longer attempts to use EEADRH on PIC18F4321.
;
; UART Bootloader for PIC18F by Ross Fosler
; 09/01/2006 Modified to support PIC18xxJxx & 160k PIC18Fxxx Flash Devices
; 03/01/2002 ... First full implementation
; 03/07/2002 Changed entry method to use last byte of EEDATA.
; Also removed all possible infinite loops w/ clrwdt.
; 03/07/2002 Added code for direct boot entry. I.E. boot vector.
; 03/09/2002 Changed the general packet format, removed the LEN field.
; 03/09/2002 Modified the erase command to do multiple row erase.
; 03/12/2002 Fixed write problem to CONFIG area. Write was offset by a byte.
; 03/15/2002 Added work around for 18F8720 tblwt*+ problem.
; 03/20/2002 Modified receive & parse engine to vector to autobaud on a checksum
; error since a chechsum error could likely be a communications problem.
; 03/22/2002 Removed clrwdt from the startup. This instruction affects the TO and
; PD flags. Removing this instruction should have no affect on code
; operation since the wdt is cleared on a reset and boot entry is always
; on a reset.
; 03/22/2002 Modified the protocol to incorporate the autobaud as part of the
; first received <STX>. Doing this improves robustness by allowing
; re-sync under any condition. Previously it was possible to enter a
; state where only a hard reset would allow re-syncing.
; 03/27/2002 Removed the boot vector and related code. This could lead to customer
; issues. There is a very minute probability that errent code execution
; could jump into the boot area and cause artificial boot entry.
; *****************************************************************************
#include <p18cxxx.inc>
#include "devices.inc"
#include "bootconfig.inc"
#include "preprocess.inc"
; *****************************************************************************
__idlocs _IDLOC0, 0x0B
__idlocs _IDLOC1, 0x04
__idlocs _IDLOC2, 0x05
__idlocs _IDLOC3, 0x0D
__idlocs _IDLOC4, 0x02
__idlocs _IDLOC5, 0x06
__idlocs _IDLOC6, 0x0E
__idlocs _IDLOC7, 0x09
#ifdef __DEBUG
CONFIG FOSC = HS2, PLLCFG = OFF, CANMX = PORTC, MCLRE = ON, IESO = OFF, PWRTEN = OFF, BOREN = OFF, WDTEN = OFF, SOSCSEL = HIGH, XINST = OFF
#else
CONFIG FOSC = HS1, PLLCFG = ON, CANMX = PORTB, MCLRE = ON, IESO = OFF, PWRTEN = OFF, BOREN = OFF, WDTEN = OFF, SOSCSEL = HIGH, XINST = OFF
#endif
; *****************************************************************************
#define STX 0x0F
#define ETX 0x04
#define DLE 0x05
#define NTX 0xFF
; *****************************************************************************
; *****************************************************************************
; RAM Address Map
CRCL equ 0x00
CRCH equ 0x01
RXDATA equ 0x02
TXDATA equ 0x03
; Framed Packet Format
; <STX>[<COMMAND><ADDRL><ADDRH><ADDRU><0x00><DATALEN><...DATA...>]<CRCL><CRCH><ETX>
COMMAND equ 0x05 ; receive buffer
ADDRESS_L equ 0x06
ADDRESS_H equ 0x07
ADDRESS_U equ 0x08
ADDRESS_X equ 0x09
DATA_COUNTL equ 0x0A
PACKET_DATA equ 0x0B
DATA_COUNTH equ 0x0B ; only for certain commands
; *****************************************************************************
; *****************************************************************************
errorlevel -311 ; don't warn on HIGH() operator values >16-bits
#ifdef USE_SOFTBOOTWP
#ifndef SOFTWP
#define SOFTWP
#endif
#endif
#ifdef USE_SOFTCONFIGWP
#ifdef CONFIG_AS_FLASH
#ifndef SOFTWP
#define SOFTWP
#endif
#endif
#endif
#ifndef AppVector
; The application startup GOTO instruction will be written just before the Boot Block,
; courtesy of the host PC bootloader application.
#define AppVector (BootloaderStart-.4)
#endif
; *****************************************************************************
; *****************************************************************************
#if BOOTLOADER_ADDRESS != 0
ORG 0
; The following GOTO is not strictly necessary, but may startup faster
; for large microcontrollers running at extremely slow clock speeds.
;goto BootloaderBreakCheck
ORG BOOTLOADER_ADDRESS
BootloaderStart:
bra BootloadMode
; *****************************************************************************
; Determine if the application is supposed to be started or if we should
; go into bootloader mode.
;
; If RX pin is in BREAK state when we come out of MCLR reset, immediately
; enter bootloader mode, even if there exists some application firmware in
; program memory.
BootloaderBreakCheck:
DigitalInput ; set RX pin as digital input on certain parts
#ifdef INVERT_UART
btfss RXPORT, RXPIN
GotoAppVector:
goto AppVector ; no BREAK state, attempt to start application
#else
btfsc RXPORT, RXPIN
GotoAppVector:
goto AppVector ; no BREAK state, attempt to start application
#endif
BootloadMode:
DigitalInput ; set RX pin as digital input on certain parts
#else ; BOOTLOADER_ADDRESS == 0 ****************************************************************
ORG 0
BootloaderStart:
DigitalInput ; set RX pin as digital input on certain parts
movlw low(AppVector) ; load address of application reset vector
bra BootloaderBreakCheck
ORG 0x0008
HighPriorityInterruptVector:
goto AppHighIntVector ; Re-map Interrupt vector
BootloaderBreakCheck:
#ifdef INVERT_UART
btfsc RXPORT, RXPIN
bra BootloadMode
#else
btfss RXPORT, RXPIN
bra BootloadMode
#endif
CheckAppVector:
; Read instruction at the application reset vector location.
; If we read 0xFFFF, assume that the application firmware has
; not been programmed yet, so don't try going into application mode.
movwf TBLPTRL
movlw high(AppVector)
movwf TBLPTRH
bra CheckAppVector2
ORG 0x0018
LowPriorityInterruptVector:
goto AppLowIntVector ; Re-map Interrupt vector
CheckAppVector2:
movlw upper(AppVector)
movwf TBLPTRU
tblrd *+ ; read instruction from program memory
incfsz TABLAT, W ; if the lower byte != 0xFF,
GotoAppVector:
goto AppVector ; run application.
tblrd *+ ; read instruction from program memory
incfsz TABLAT, W ; if the lower byte == 0xFF but upper byte != 0xFF,
bra GotoAppVector ; run application.
; otherwise, assume application firmware is not present because we read a NOP (0xFFFF).
; fall through to bootloader mode...
BootloadMode:
#endif ; end BOOTLOADER_ADDRESS == 0 ******************************************
lfsr FSR2, 0 ; for compatibility with Extended Instructions mode.
#ifdef USE_MAX_INTOSC
movlw b'01110000' ; set INTOSC to maximum speed (usually 8MHz)
iorwf OSCCON, f
#endif
#ifdef USE_PLL
#ifdef PLLEN
#ifdef OSCTUNE
bsf OSCTUNE, PLLEN ; enable PLL for faster internal clock
#else
; 18F8680, 18F8585, 18F6680, and 18F6585 doesn't have OSCTUNE register.
; Instead, PLLEN bit is in OSCCON.
bsf OSCCON, PLLEN ; enable PLL for faster internal clock
#endif
#else
#ifdef SPLLEN
bsf OSCTUNE, SPLLEN ; PIC18F14K50 has SPLLEN at bit 6
#endif
#endif
#endif
#ifdef INVERT_UART
btfsc RXPORT, RXPIN ; wait for RX pin to go IDLE
bra $-2
#else
btfss RXPORT, RXPIN ; wait for RX pin to go IDLE
bra $-2
#endif
#ifdef PPS_UTX_PIN
banksel PPSCON
; unlock PPS registers
movlw 0x55
movwf EECON2, ACCESS
movlw 0xAA
movwf EECON2, ACCESS
bcf PPSCON, IOLOCK, BANKED
; assign UART RX/TX to PPS remappable pins
movlw PPS_UTX
movwf PPS_UTX_PIN, BANKED
movlw PPS_URX_PIN
movwf PPS_URX, BANKED
; lock PPS registers from inadvertent changes
movlw 0x55
movwf EECON2, ACCESS
movlw 0xAA
movwf EECON2, ACCESS
bsf PPSCON, IOLOCK, BANKED
movlb 0x0F
#endif
movlw b'10010000' ; Setup UART
movwf UxRCSTA
movlw b'00100110' ; BRGH = 1, TXEN = 1
movwf UxTXSTA
#ifdef INVERT_UART
bsf UxBAUDCON, RXDTP
bsf UxBAUDCON, TXCKP
#endif
#ifdef BRG16
bsf UxBAUDCON, BRG16
movlw b'00000010' ; 1:8 prescaler - no division required later (but no rounding possible)
#else
movlw b'00000011' ; 1:16 prescaler - thus we only have to divide by 2 later on.
#endif
movwf T0CON
#ifdef PICDEM_LCD2
bsf LATB, LATB0 ; PICDEM LCD 2 demoboard requires RB0 high to enable MAX3221 TX output to PC.
bcf TRISB, TRISB0
#endif
; *****************************************************************************
; *****************************************************************************
#ifdef USE_AUTOBAUD
DoAutoBaud:
; ___ __________ ________
; \__/ \__________/
; | |
; |-------- p ----------|
;
; p = The number of instructions between the first and last
; rising edge of the RS232 control sequence 0x0F. Other
; possible control sequences are 0x01, 0x03, 0x07, 0x1F,
; 0x3F, 0x7F.
;
; SPBRG = (p / 32) - 1 BRGH = 1, BRG16 = 0
; SPBRG = (p / 8) - 1 BRGH = 1, BRG16 = 1
bcf UxRCSTA, CREN ; Stop receiving
movf UxRCREG, W ; Empty the buffer
movf UxRCREG, W
RetryAutoBaud:
clrf TMR0H ; reset timer count value
clrf TMR0L
bcf INTCON, TMR0IF
rcall WaitForRise ; wait for a start bit to pass by
bsf T0CON, TMR0ON ; start timer counting for entire D7..D0 data bit period.
rcall WaitForRise ; wait for stop bit
bcf T0CON, TMR0ON ; stop the timer from counting further.
btfsc INTCON, TMR0IF ; if TMR0 overflowed, we did not get a good baud capture
bra RetryAutoBaud ; try again
#ifdef BRG16
; save new baud rate generator value
movff TMR0L, UxSPBRG ; warning: must read TMR0L before TMR0H holds real data
movff TMR0H, UxSPBRGH
#else
movff TMR0L, UxSPBRG ; warning: must read TMR0L before TMR0H holds real data
; TMR0H:TMR0L holds (p / 16).
rrcf TMR0H, w ; divide by 2
rrcf UxSPBRG, F
btfss STATUS, C ; rounding
decf UxSPBRG, F
#endif
bsf UxRCSTA, CREN ; start receiving
WaitForHostCommand:
rcall ReadHostByte ; get start of transmission <STX>
xorlw STX
bnz DoAutoBaud ; got something unexpected, perform autobaud
#else ; not using autobaud
movlw low(BAUDRG) ; set fixed baud rate generator value
movwf UxSPBRG
#ifdef UxSPBRGH
#if high(BAUDRG) != 0
movlw high(BAUDRG)
movwf UxSPBRGH
#endif
#endif
bsf UxRCSTA, CREN ; start receiving
DoAutoBaud:
WaitForHostCommand:
rcall ReadHostByte ; get start of transmission <STX>
xorlw STX
bnz WaitForHostCommand ; got something unexpected, keep waiting for <STX>
#endif ; end #ifdef USE_AUTOBAUD
; *****************************************************************************
; *****************************************************************************
; Read and parse packet data.
StartOfLine:
movlw STX ; send back start of response
rcall SendHostByte
lfsr FSR0, COMMAND-1 ; Point to the buffer
ReceiveDataLoop:
rcall ReadHostByte ; Get the data
xorlw STX ; Check for an unexpected STX
bz StartOfLine ; unexpected STX: abort packet and start over.
NoSTX:
movf RXDATA, W
xorlw ETX ; Check for a ETX
bz VerifyPacketCRC ; Yes, verify CRC
NoETX:
movf RXDATA, W
xorlw DLE ; Check for a DLE
bnz AppendDataBuffer
rcall ReadHostByte ; DLE received, get the next byte and store it
AppendDataBuffer:
movff RXDATA, PREINC0 ; store the data to the buffer
bra ReceiveDataLoop
VerifyPacketCRC:
lfsr FSR1, COMMAND
clrf CRCL
clrf CRCH
movff POSTDEC0, PRODH ; Save host packet's CRCH to PRODH for later comparison
; CRCL is now available as INDF0
VerifyPacketCrcLoop:
movf POSTINC1, w
rcall AddCrc ; add new data to the CRC
movf FSR1H, w
cpfseq FSR0H
bra VerifyPacketCrcLoop ; we aren't at the end of the received data yet, loop
movf FSR1L, w
cpfseq FSR0L
bra VerifyPacketCrcLoop ; we aren't at the end of the received data yet, loop
movf CRCH, w
cpfseq PRODH
bra DoAutoBaud ; invalid CRC, reset baud rate generator to re-sync with host
movf CRCL, w
cpfseq INDF0
bra DoAutoBaud ; invalid CRC, reset baud rate generator to re-sync with host
; ***********************************************
; Pre-setup, common to all commands.
clrf CRCL
clrf CRCH
movf ADDRESS_L, W ; Set all possible pointers
movwf TBLPTRL
#ifdef EEADR
movwf EEADR
#endif
movf ADDRESS_H, W
movwf TBLPTRH
#ifdef EEADRH
movwf EEADRH
#endif
movff ADDRESS_U, TBLPTRU
lfsr FSR0, PACKET_DATA
; ***********************************************
; ***********************************************
; Test the command field and sub-command.
CheckCommand:
movlw .10
cpfslt COMMAND
bra DoAutoBaud ; invalid command - reset baud generator to re-sync with host
; This jump table must exist entirely within one 256 byte block of program memory.
#if ($ & 0xFF) > (0xFF - .24)
; Too close to the end of a 256 byte boundary, push address forward to get code
; into the next 256 byte block.
messg "Wasting some code space to ensure jump table is aligned."
ORG $+(0x100 - ($ & 0xFF))
#endif
JUMPTABLE_BEGIN:
movf PCL, w ; 0 do a read of PCL to set PCLATU:PCLATH to current program counter.
rlncf COMMAND, W ; 2 multiply COMMAND by 2 (each BRA instruction takes 2 bytes on PIC18)
addwf PCL, F ; 4 Jump in command jump table based on COMMAND from host
bra BootloaderInfo ; 6 00h
bra ReadFlash ; 8 01h
bra VerifyFlash ; 10 02h
bra EraseFlash ; 12 03h
bra WriteFlash ; 14 04h
bra ReadEeprom ; 16 05h
bra WriteEeprom ; 18 06h
bra WriteConfig ; 20 07h
bra GotoAppVector ; 22 08h
reset ; 24 09h
#if (JUMPTABLE_BEGIN & 0xFF) > ($ & 0xFF)
error "Jump table is not aligned to fit within a single 256 byte address range."
#endif
; *****************************************************************************
#ifdef INVERT_UART
WaitForRise:
clrwdt
WaitForRiseLoop:
btfsc INTCON, TMR0IF ; if TMR0 overflowed, we did not get a good baud capture
return ; abort
btfss RXPORT, RXPIN ; Wait for a falling edge
bra WaitForRiseLoop
WtSR:
btfsc RXPORT, RXPIN ; Wait for starting edge
bra WtSR
return
#else ; not inverted UART pins
WaitForRise:
clrwdt
WaitForRiseLoop
btfsc INTCON, TMR0IF ; if TMR0 overflowed, we did not get a good baud capture
return ; abort
btfsc RXPORT, RXPIN ; Wait for a falling edge
bra WaitForRiseLoop
WtSR:
btfss RXPORT, RXPIN ; Wait for rising edge
bra WtSR
return
#endif ; end #ifdef INVERT_UART
; *****************************************************************************
; 16-bit CCITT CRC
; Adds WREG byte to the CRC checksum CRCH:CRCL. WREG destroyed on return.
AddCrc: ; Init: CRCH = HHHH hhhh, CRCL = LLLL llll
xorwf CRCH, w ; Pre: HHHH hhhh WREG = IIII iiii
movff CRCL, CRCH ; Pre: LLLL llll CRCH = LLLL llll
movwf CRCL ; Pre: IIII iiii CRCL = IIII iiii
swapf WREG ; Pre: IIII iiii WREG = iiii IIII
andlw 0x0F ; Pre: iiii IIII WREG = 0000 IIII
xorwf CRCL, f ; Pre: IIII iiii CRCL = IIII jjjj
swapf CRCL, w ; Pre: IIII jjjj WREG = jjjj IIII
andlw 0xF0 ; Pre: jjjj IIII WREG = jjjj 0000
xorwf CRCH, f ; Pre: LLLL llll CRCH = MMMM llll
swapf CRCL, w ; Pre: IIII jjjj WREG = jjjj IIII
rlncf WREG, w ; Pre: jjjj IIII WREG = jjjI IIIj
xorwf CRCH, f ; Pre: MMMM llll CRCH = XXXN mmmm
andlw b'11100000' ; Pre: jjjI IIIj WREG = jjj0 0000
xorwf CRCH, f ; Pre: jjj0 0000 CRCH = MMMN mmmm
xorwf CRCL, f ; Pre: MMMN mmmm CRCL = JJJI jjjj
return
; ***********************************************
; Commands
; ***********************************************
; Provides information about the Bootloader to the host PC software.
BootInfoBlock:
db low(BOOTBLOCKSIZE), high(BOOTBLOCKSIZE)
db MAJOR_VERSION, MINOR_VERSION
db 0xFF, 0x84 ; command mask : family id
db low(BootloaderStart), high(BootloaderStart)
db upper(BootloaderStart), 0
BootInfoBlockEnd:
; In: <STX>[<0x00>]<CRCL><CRCH><ETX>
; Out: <STX><BOOTBYTESL><BOOTBYTESH><VERL><VERH><STARTBOOTL><STARTBOOTH><STARTBOOTU><0x00><CRCL><CRCH><ETX>
BootloaderInfo:
movlw low(BootInfoBlock)
movwf TBLPTRL
movlw high(BootInfoBlock)
movwf TBLPTRH
movlw upper(BootInfoBlock)
movwf TBLPTRU
movlw (BootInfoBlockEnd - BootInfoBlock)
movwf DATA_COUNTL
clrf DATA_COUNTH
;; fall through to ReadFlash code -- send Bootloader Information Block from FLASH.
; In: <STX>[<0x01><ADDRL><ADDRH><ADDRU><0x00><BYTESL><BYTESH>]<CRCL><CRCH><ETX>
; Out: <STX>[<DATA>...]<CRCL><CRCH><ETX>
ReadFlash:
tblrd *+ ; read from FLASH memory into TABLAT
movf TABLAT, w
rcall SendEscapeByte
rcall AddCrc
decf DATA_COUNTL, f ; decrement counter
movlw 0
subwfb DATA_COUNTH, f
movf DATA_COUNTL, w ; DATA_COUNTH:DATA_COUNTH == 0?
iorwf DATA_COUNTH, w
bnz ReadFlash ; no, loop
bra SendChecksum ; yes, send end of packet
; In: <STX>[<0x02><ADDRL><ADDRH><ADDRU><0x00><BLOCKSL><BLOCKSH>]<CRCL><CRCH><ETX>
; Out: <STX>[<CRCL1><CRCH1>...<CRCLn><CRCHn>]<ETX>
VerifyFlash:
tblrd *+
movf TABLAT, w
rcall AddCrc
movf TBLPTRL, w ; have we crossed into the next block?
#if ERASE_FLASH_BLOCKSIZE > .255
bnz VerifyFlash
movf TBLPTRH, w
andlw high(ERASE_FLASH_BLOCKSIZE-1)
#else
andlw (ERASE_FLASH_BLOCKSIZE-1)
#endif
bnz VerifyFlash
movf CRCL, w
call SendEscapeByte
movf CRCH, w
call SendEscapeByte
decf DATA_COUNTL, f ; decrement counter
movlw 0
subwfb DATA_COUNTH, f
movf DATA_COUNTL, w ; DATA_COUNTH:DATA_COUNTH == 0?
iorwf DATA_COUNTH, w
bnz VerifyFlash ; no, loop
bra SendETX ; yes, send end of packet
#ifdef SOFTWP
reset ; this code -should- never be executed, but
reset ; just in case of errant execution or buggy
reset ; firmware, these reset instructions may protect
reset ; against accidental erases.
#endif
; In: <STX>[<0x03><ADDRL><ADDRH><ADDRU><0x00><PAGESL>]<CRCL><CRCH><ETX>
; Out: <STX>[<0x03>]<CRCL><CRCH><ETX>
EraseFlash:
#ifdef SOFTWP
#define ERASE_ADDRESS_MASK (~(ERASE_FLASH_BLOCKSIZE-1))
#if upper(ERASE_ADDRESS_MASK) != 0xFF
movlw upper(ERASE_ADDRESS_MASK) ; force starting address to land on a FLASH Erase Block boundary
andwf TBLPTRU, f
#endif
#if high(ERASE_ADDRESS_MASK) != 0xFF
movlw high(ERASE_ADDRESS_MASK) ; force starting address to land on a FLASH Erase Block boundary
andwf TBLPTRH, f
#endif
#if low(ERASE_ADDRESS_MASK) != 0xFF
movlw low(ERASE_ADDRESS_MASK) ; force starting address to land on a FLASH Erase Block boundary
andwf TBLPTRL, f
#endif
; Verify Erase Address does not attempt to erase beyond the end of FLASH memory
movlw low(END_FLASH)
subwf TBLPTRL, w
movlw high(END_FLASH)
subwfb TBLPTRH, w
movlw upper(END_FLASH)
subwfb TBLPTRU, w
bn EraseEndFlashAddressOkay
clrf EECON1 ; inhibit writes for this block
bra NextEraseBlock ; move on to next erase block
#endif ; end #ifdef USE_SOFTBOOTWP
EraseEndFlashAddressOkay:
#ifdef USE_SOFTCONFIGWP
#ifdef CONFIG_AS_FLASH
movlw low(END_FLASH - ERASE_FLASH_BLOCKSIZE)
subwf TBLPTRL, w
movlw high(END_FLASH - ERASE_FLASH_BLOCKSIZE)
subwfb TBLPTRH, w
movlw upper(END_FLASH - ERASE_FLASH_BLOCKSIZE)
subwfb TBLPTRU, w
bn EraseConfigAddressOkay
clrf EECON1 ; inhibit writes for this block
bra NextEraseBlock ; move on to next erase block
EraseConfigAddressOkay:
#endif ; end CONFIG_AS_FLASH
#endif ; end USE_SOFTCONFIGWP
#ifdef USE_SOFTBOOTWP
movlw low(BOOTLOADER_ADDRESS)
subwf TBLPTRL, w
movlw high(BOOTLOADER_ADDRESS)
subwfb TBLPTRH, w
movlw upper(BOOTLOADER_ADDRESS)
subwfb TBLPTRU, w
bn EraseAddressOkay
movlw low(BOOTLOADER_ADDRESS + BOOTBLOCKSIZE)
subwf TBLPTRL, w
movlw high(BOOTLOADER_ADDRESS + BOOTBLOCKSIZE)
subwfb TBLPTRH, w
movlw upper(BOOTLOADER_ADDRESS + BOOTBLOCKSIZE)
subwfb TBLPTRU, w
bnn EraseAddressOkay
clrf EECON1 ; inhibit writes for this block
bra NextEraseBlock ; move on to next erase block
reset ; this code -should- never be executed, but
reset ; just in case of errant execution or buggy
reset ; firmware, these reset instruction may protect
reset ; against accidental writes.
#endif
EraseAddressOkay:
#ifdef EEADR
movlw b'10010100' ; setup FLASH erase
#else
movlw b'00010100' ; setup FLASH erase for J device (no EEPROM bit)
#endif
movwf EECON1
rcall StartWrite ; erase the page
NextEraseBlock:
; Decrement address by erase block size
#if ERASE_FLASH_BLOCKSIZE >= .256
movlw high(ERASE_FLASH_BLOCKSIZE)
subwf TBLPTRH, F
clrf WREG
subwfb TBLPTRU, F
#else
movlw ERASE_FLASH_BLOCKSIZE
subwf TBLPTRL, F
clrf WREG
subwfb TBLPTRH, F
subwfb TBLPTRU, F
#endif
decfsz DATA_COUNTL, F
bra EraseFlash
bra SendAcknowledge ; All done, send acknowledgement packet
#ifdef SOFTWP
reset ; this code -should- never be executed, but
reset ; just in case of errant execution or buggy
reset ; firmware, these reset instructions may protect
reset ; against accidental writes.
#endif
; In: <STX>[<0x04><ADDRL><ADDRH><ADDRU><0x00><BLOCKSL><DATA>...]<CRCL><CRCH><ETX>
; Out: <STX>[<0x04>]<CRCL><CRCH><ETX>
WriteFlash:
#ifdef SOFTWP
#define WRITE_ADDRESS_MASK (~(WRITE_FLASH_BLOCKSIZE-1))
#if upper(WRITE_ADDRESS_MASK) != 0xFF
movlw upper(WRITE_ADDRESS_MASK) ; force starting address to land on a FLASH Write Block boundary
andwf TBLPTRU, f
#endif
#if high(WRITE_ADDRESS_MASK) != 0xFF
movlw high(WRITE_ADDRESS_MASK) ; force starting address to land on a FLASH Write Block boundary
andwf TBLPTRH, f
#endif
#if low(WRITE_ADDRESS_MASK) != 0xFF
movlw low(WRITE_ADDRESS_MASK) ; force starting address to land on a FLASH Write Block boundary
andwf TBLPTRL, f
#endif
; Verify Write Address does not attempt to write beyond the end of FLASH memory
movlw low(END_FLASH)
subwf TBLPTRL, w
movlw high(END_FLASH)
subwfb TBLPTRH, w
movlw upper(END_FLASH)
subwfb TBLPTRU, w
bn WriteEndFlashAddressOkay
clrf EECON1 ; inhibit writes for this block
bra LoadHoldingRegisters; fake the write so we can move on to real writes
#endif ; end #ifdef SOFTWP
WriteEndFlashAddressOkay:
#ifdef USE_SOFTCONFIGWP
#ifdef CONFIG_AS_FLASH
movlw low(END_FLASH - ERASE_FLASH_BLOCKSIZE)
subwf TBLPTRL, w
movlw high(END_FLASH - ERASE_FLASH_BLOCKSIZE)
subwfb TBLPTRH, w
movlw upper(END_FLASH - ERASE_FLASH_BLOCKSIZE)
subwfb TBLPTRU, w
bn WriteConfigAddressOkay
clrf EECON1 ; inhibit writes for this block
bra LoadHoldingRegisters; fake the write so we can move on to real writes
WriteConfigAddressOkay:
#endif ; end CONFIG_AS_FLASH
#endif ; end USE_SOFTCONFIGWP
#ifdef USE_SOFTBOOTWP
movlw low(BOOTLOADER_ADDRESS)
subwf TBLPTRL, w
movlw high(BOOTLOADER_ADDRESS)
subwfb TBLPTRH, w
movlw upper(BOOTLOADER_ADDRESS)
subwfb TBLPTRU, w
bn WriteAddressOkay
movlw low(BOOTLOADER_ADDRESS + BOOTBLOCKSIZE)
subwf TBLPTRL, w
movlw high(BOOTLOADER_ADDRESS + BOOTBLOCKSIZE)
subwfb TBLPTRH, w
movlw upper(BOOTLOADER_ADDRESS + BOOTBLOCKSIZE)
subwfb TBLPTRU, w
bnn WriteAddressOkay
clrf EECON1 ; inhibit writes for this block
bra LoadHoldingRegisters ; fake the write so we can move on to real writes
reset ; this code -should- never be executed, but
reset ; just in case of errant execution or buggy
reset ; firmware, these reset instruction may protect
reset ; against accidental writes.
#endif
WriteAddressOkay:
#ifdef EEADR
movlw b'10000100' ; Setup FLASH writes
#else
movlw b'00000100' ; Setup FLASH writes for J device (no EEPROM bit)
#endif
movwf EECON1
LoadHoldingRegisters:
movff POSTINC0, TABLAT ; Load the holding registers
pmwtpi ; Same as tblwt *+
movf TBLPTRL, w ; have we crossed into the next write block?
andlw (WRITE_FLASH_BLOCKSIZE-1)
bnz LoadHoldingRegisters; Not finished writing holding registers, repeat
tblrd *- ; Point back into the block to write data
rcall StartWrite ; initiate a page write
tblrd *+ ; Restore pointer for loading holding registers with next block
decfsz DATA_COUNTL, F
bra WriteFlash ; Not finished writing all blocks, repeat
bra SendAcknowledge ; all done, send ACK packet
; In: <STX>[<0x05><ADDRL><ADDRH><0x00><0x00><BYTESL><BYTESH>]<CRCL><CRCH><ETX>
; Out: <STX>[<DATA>...]<CRCL><CRCH><ETX>
#ifdef EEADR ; some devices do not have EEPROM, so no need for this code
ReadEeprom:
clrf EECON1
ReadEepromLoop:
bsf EECON1, RD ; Read the data
movf EEDATA, w
#ifdef EEADRH
infsnz EEADR, F ; Adjust EEDATA pointer
incf EEADRH, F
#else
incf EEADR, F ; Adjust EEDATA pointer
#endif
rcall SendEscapeByte
rcall AddCrc
#ifdef EEADRH
decf DATA_COUNTL, f ; decrement counter
movlw 0
subwfb DATA_COUNTH, f
movf DATA_COUNTL, w ; DATA_COUNTH:DATA_COUNTH == 0?
iorwf DATA_COUNTH, w
bnz ReadEepromLoop ; no, loop
bra SendChecksum ; yes, send end of packet
#else
decfsz DATA_COUNTL, F
bra ReadEepromLoop ; Not finished then repeat
bra SendChecksum
#endif
#endif ; end #ifdef EEADR
; In: <STX>[<0x06><ADDRL><ADDRH><0x00><0x00><BYTESL><BYTESH><DATA>...]<CRCL><CRCH><ETX>
; Out: <STX>[<0x06>]<CRCL><CRCH><ETX>
#ifdef EEADR ; some devices do not have EEPROM, so no need for this code
WriteEeprom:
movlw b'00000100' ; Setup for EEPROM data writes
movwf EECON1
WriteEepromLoop:
movff PREINC0, EEDATA
rcall StartWrite
btfsc EECON1, WR ; wait for write to complete before moving to next address
bra $-2
#ifdef EEADRH
infsnz EEADR, F ; Adjust EEDATA pointer
incf EEADRH, F
#else
incf EEADR, f ; Adjust EEDATA pointer
#endif
#ifdef EEADRH
decf DATA_COUNTL, f ; decrement counter
movlw 0
subwfb DATA_COUNTH, f
movf DATA_COUNTL, w ; DATA_COUNTH:DATA_COUNTH == 0?
iorwf DATA_COUNTH, w
bnz WriteEepromLoop ; no, loop
bra SendAcknowledge ; yes, send end of packet
#else
decfsz DATA_COUNTL, f
bra WriteEepromLoop
bra SendAcknowledge
#endif
#endif ; end #ifdef EEADR
; In: <STX>[<0x07><ADDRL><ADDRH><ADDRU><0x00><BYTES><DATA>...]<CRCL><CRCH><ETX>
; Out: <STX>[<0x07>]<CRCL><CRCH><ETX>
#ifndef CONFIG_AS_FLASH ; J flash devices store config words in FLASH, so no need for this code
#ifndef USE_SOFTCONFIGWP
WriteConfig:
movlw b'11000100'
movwf EECON1
tblrd * ; read existing value from config memory
WriteConfigLoop:
movf POSTINC0, w
cpfseq TABLAT ; is the proposed value already the same as existing value?
rcall TableWriteWREG ; write config memory only if necessary (save time and endurance)
tblrd +* ; increment table pointer to next address and read existing value
decfsz DATA_COUNTL, F
bra WriteConfigLoop ; If more data available in packet, keep looping
bra SendAcknowledge ; Send acknowledge
#endif ; end #ifndef USE_SOFTCONFIGWP
#endif ; end #ifndef CONFIG_AS_FLASH
;************************************************
; ***********************************************
; Send an acknowledgement packet back
;
; <STX><COMMAND><CRCL><CRCH><ETX>
; Some devices only have config words as FLASH memory. Some devices don't have EEPROM.
; For these devices, we can save code by jumping directly to sending back an
; acknowledgement packet if the PC application erroneously requests them.
#ifdef CONFIG_AS_FLASH
WriteConfig:
#else
#ifdef USE_SOFTCONFIGWP
WriteConfig:
#endif
#endif ; end #ifdef CONFIG_AS_FLASH
#ifndef EEADR
ReadEeprom:
WriteEeprom:
#endif
SendAcknowledge:
clrf EECON1 ; inhibit write cycles to FLASH memory
movf COMMAND, w
rcall SendEscapeByte ; Send only the command byte (acknowledge packet)
rcall AddCrc
SendChecksum:
movf CRCL, W
rcall SendEscapeByte
movf CRCH, W
rcall SendEscapeByte
SendETX:
movlw ETX ; Send stop condition
rcall SendHostByte
bra WaitForHostCommand
; *****************************************************************************