-
Notifications
You must be signed in to change notification settings - Fork 2
/
PIC16F884_HDLC_S2A_A2S.asm
2747 lines (2642 loc) · 50.4 KB
/
PIC16F884_HDLC_S2A_A2S.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
;************************************************************************
; *
; Filename: PIC16F884_HDLC_S2A_A2S_v46.asm *
; Date: Nov 30, 2019. *
; File Version: 4.6.1 *
; *
; Author: Juan Carlos Pérez De Castro (Wodie) KM4NNO / XE1F *
; Project advisor: Bryan Fiels W9CR *
; *
; This software is licenced under the GPL v3 license. *
; *
;************************************************************************
; *
; Files Required: P16F884.INC *
; *
;************************************************************************
; *
; Notes: *
; This is the reduced memory version for PIC16F884. *
; S>A HDLC & A>S HDLC versions merged for only one PIC. *
; Memory extended to support TMS. *
; WDT structure Modified. *
; RS-232 Reset CMD Bug fixed. *
; CTS Deprecated. *
; A.S Memory extended to 112 Bytes. *
; Hardware Reset will need a Not gate chip on PCB. *
; Testing ofset interruption for Async. *
; *
; *** Missing: *
; 2nd Bug patch deeper test on A>S (RS-232 to HDLC), seems to work. *
; *
;************************************************************************
Title "S>A HDLC & A>S HDLC interface for P25NX Quantar"
#include <p16f884.inc> ; processor specific variable definitions
__CONFIG _CONFIG1, _HS_OSC & _WDT_ON & _PWRTE_ON & _CP_OFF & _CPD_OFF & _LVP_OFF & _MCLRE_ON & _FCMEN_ON & _IESO_OFF & _BOR_OFF & _DEBUG_OFF
__CONFIG _CONFIG2, _WRT_OFF & _BOR21V
list p=16f887 ; list directive to define processor
RADIX DEC
;*****GLOBAL DECLARATIONS **************************************
#DEFINE BANK0 BCF STATUS,RP0 ; TO CHANGE RP0 TO BANK 0 OR BANK 2
#DEFINE BANK1 BSF STATUS,RP0 ; TO CHANGE RP0 TO BANK 1 OR BANK 3
#DEFINE BANK0X BCF STATUS,RP1 ; TO CHANGE RP1 TO SHIFT BANKS 0 & 1
#DEFINE BANK1X BSF STATUS,RP1 ; TO CHANGE RP1 TO SHIFT BANKS 2 & 3
#DEFINE CARRY STATUS,C ; MEMONIC FOR CARRY
#DEFINE ZERO STATUS,Z ; MEMONIC FOR ZERO
#DEFINE SRxGLED PORTA,2 ; Green LED
#DEFINE SRxRLED PORTA,3 ; Red LED
#DEFINE ARxGLED PORTA,0 ; Green LED
#DEFINE ARxRLED PORTA,1 ; Red LED
#DEFINE RxClock PORTB,0 ; Quantar Master Clock Output.
#DEFINE RxPin PORTB,1 ; Data Rx from Quantar.
;#DEFINE TxClock PORTB,2 ; Data Clock to Quantar.
#DEFINE TxPin PORTB,3 ; Data Tx to Quantar.
;#DEFINE CTS PORTC,5 ; Clear To Send. Tells computer when it can send new data.
#DEFINE ServiceFlag FLAGS_1,0 ; Developer debug mode flag.
#DEFINE SRxFiveOnes FLAGS_1,1 ; S to A HDLC flags.
#DEFINE SRxSixOnes FLAGS_1,2 ; /////
#DEFINE SRxSevenOnes FLAGS_1,3 ; ////
#DEFINE SDummyZeroFlag FLAGS_1,4 ; ///
#DEFINE SRxComplete FLAGS_1,5 ; //
#DEFINE SHeaderRx FLAGS_1,6 ; /
#DEFINE EscapeCharFlag FLAGS_2,0 ; A to S HDLC flags.
#DEFINE TxBitRAM FLAGS_2,1 ; //////
#DEFINE HeaderBeingTx FLAGS_2,2 ; /////
#DEFINE WritingBuffer FLAGS_2,3 ; ////
#DEFINE DataReady FLAGS_2,4 ; ///
#DEFINE AddZeroBit FLAGS_2,5 ; //
#DEFINE FooterWasTx FLAGS_2,6 ; /
;***** Define program constants
INT_RAM EQU H'20' ; 96 General Porpouse registers 0x20 thru 0x7F.
COMMON_RAM1 EQU H'30'
COMMON_RAM2 EQU H'A0' ; F0 thru FF are common Access RAM.
COMMON_RAM3 EQU H'110' ; //
COMMON_RAM4 EQU H'190' ; /
Osc_Freq EQU 20000000; 20 MHz
Baud_Rate EQU 19200; 19.200 kbauds
Baud_Rate_Const EQU (Osc_Freq/(16*Baud_Rate))-1
;***** SET UP RAM ***************************************
;*** Define interrupt handler variables
CBLOCK INT_RAM
W_TEMP
S_TEMP
P_TEMP
FSR_TEMP
ENDC
;*** DEFINE RAM 1
CBLOCK COMMON_RAM1
ABufferIn0 ; These are the RAM Bytes used to Rx
ABufferIn1 ; from Async HDLC.
ABufferIn2
ABufferIn3
ABufferIn4
ABufferIn5
ABufferIn6
ABufferIn7
ABufferIn8
ABufferIn9
ABufferIn10
ABufferIn11
ABufferIn12
ABufferIn13
ABufferIn14
ABufferIn15
ABufferIn16
ABufferIn17
ABufferIn18
ABufferIn19
ABufferIn20
ABufferIn21
ABufferIn22
ABufferIn23
ABufferIn24
ABufferIn25
ABufferIn26
ABufferIn27
ABufferIn28
ABufferIn29
ABufferIn30
ABufferIn31
SBufferOut0 ; These are the RAM Bytes used to Tx
SBufferOut1 ; to Sync HDLC.
SBufferOut2
SBufferOut3
SBufferOut4
SBufferOut5
SBufferOut6
SBufferOut7
SBufferOut8
SBufferOut9
SBufferOut10
SBufferOut11
SBufferOut12
SBufferOut13
SBufferOut14
SBufferOut15
SBufferOut16
SBufferOut17
SBufferOut18
SBufferOut19
SBufferOut20
SBufferOut21
SBufferOut22
SBufferOut23
SBufferOut24
SBufferOut25
SBufferOut26
SBufferOut27
SBufferOut28
SBufferOut29
SBufferOut30
SBufferOut31
ENDC
;*** DEFINE RAM 2
CBLOCK 0xA0 ;COMMON_RAM2
ABufferIn32
ABufferIn33
ABufferIn34
ABufferIn35
ABufferIn36
ABufferIn37
ABufferIn38
ABufferIn39
ABufferIn40
ABufferIn41
ABufferIn42
ABufferIn43
ABufferIn44
ABufferIn45
ABufferIn46
ABufferIn47
ABufferIn48
ABufferIn49
ABufferIn50
ABufferIn51
ABufferIn52
ABufferIn53
ABufferIn54
ABufferIn55
ABufferIn56
ABufferIn57
ABufferIn58
ABufferIn59
ABufferIn60
ABufferIn61
ABufferIn62
ABufferIn63
ABufferIn64
ABufferIn65
ABufferIn66
ABufferIn67
ABufferIn68
ABufferIn69
ABufferIn70
ABufferIn71
SBufferOut32
SBufferOut33
SBufferOut34
SBufferOut35
SBufferOut36
SBufferOut37
SBufferOut38
SBufferOut39
SBufferOut40
SBufferOut41
SBufferOut42
SBufferOut43
SBufferOut44
SBufferOut45
SBufferOut46
SBufferOut47
SBufferOut48
SBufferOut49
SBufferOut50
SBufferOut51
SBufferOut52
SBufferOut53
SBufferOut54
SBufferOut55
SBufferOut56
SBufferOut57
SBufferOut58
SBufferOut59
SBufferOut60
SBufferOut61
SBufferOut62
SBufferOut63
SBufferOut64
SBufferOut65
SBufferOut66
SBufferOut67
SBufferOut68
SBufferOut69
SBufferOut70
SBufferOut71
ENDC
;*** DEFINE RAM 3
CBLOCK 0x120 ;COMMON_RAM3
ABufferIn72
ABufferIn73
ABufferIn74
ABufferIn75
ABufferIn76
ABufferIn77
ABufferIn78
ABufferIn79
ABufferIn80
ABufferIn81
ABufferIn82
ABufferIn83
ABufferIn84
ABufferIn85
ABufferIn86
ABufferIn87
ABufferIn88
ABufferIn89
ABufferIn90
ABufferIn91
ABufferIn92
ABufferIn93
ABufferIn94
ABufferIn95
ABufferIn96
ABufferIn97
ABufferIn98
ABufferIn99
ABufferIn100
ABufferIn101
ABufferIn102
ABufferIn103
ABufferIn104
ABufferIn105
ABufferIn106
ABufferIn107
ABufferIn108
ABufferIn109
ABufferIn110
ABufferIn111
SBufferOut72
SBufferOut73
SBufferOut74
SBufferOut75
SBufferOut76
SBufferOut77
SBufferOut78
SBufferOut79
SBufferOut80
SBufferOut81
SBufferOut82
SBufferOut83
SBufferOut84
SBufferOut85
SBufferOut86
SBufferOut87
SBufferOut88
SBufferOut89
SBufferOut90
SBufferOut91
SBufferOut92
SBufferOut93
SBufferOut94
SBufferOut95
SBufferOut96
SBufferOut97
SBufferOut98
SBufferOut99
SBufferOut100
SBufferOut101
SBufferOut102
SBufferOut103
SBufferOut104
SBufferOut105
SBufferOut106
SBufferOut107
SBufferOut108
SBufferOut109
SBufferOut110
SBufferOut111
ENDC
CBLOCK 0x70 ;COMMON_RAM1
FLAGS_1 ; Bit variables
FLAGS_2 ; Bit variables
BitIndex ; HDLC Rx bit position
OnesCount ; Continous 1 counter
SRxByte ; Sync: RAM for the HDLC Rx Byte
ATxByte ; Async: Byte to be Tx via RS-232
ARxByte
TxBitIndex
TxOnesCount
TxByteIndex
SyncTxByte
ABufferInLen; Counter for Async Bytes Rx.
SBufferOutLen; Counter for Sync Bytes to Tx.
DataIndex ; Counter of Bytes Tx (table position index).
Testing
ENDC
;***** PROGRAM ***********************************************************
ORG 0x000
GOTO START
;***** INTERRUPT VECTOR *****************************
ORG 0x004 ; Interrupt vector location.
MOVWF W_TEMP ; Save PIC state.
SWAPF STATUS,W
CLRF STATUS
MOVWF S_TEMP
MOVF PCLATH,W
MOVWF P_TEMP
CLRF PCLATH
BCF STATUS,IRP
MOVF FSR,W
MOVWF FSR_TEMP
;***** INTERUPTIONS *****************************
BTFSC INTCON,INTF ; HDLCRx Clock INT flag Hi?
GOTO HDLCRx ; Yes, handle it.
BTFSC PIR1,RCIF ; RS-232 Byte Received.
GOTO AUSARTRx ; Yes, Handle it.
GOTO INTEND ; No, exit int.
HDLCRx:
BTFSC RxClock ; If Clock:
GOTO HDLC_Rx_Int ; Clock changed to High.
BTFSC TxBitRAM ; Clock changed to Low. If Data is a 1 set output.
BSF TxPin ; / Make it 1.
BTFSS TxBitRAM ; If Data is a 0 clear output.
BCF TxPin ; / Make it 0.
BCF INTCON,INTF ; Clear int RB0 flag.
BANK1
BSF OPTION_REG,INTEDG ; Set RB0 Edge trigger to High.
BANK0
CALL HDLC_Tx ; Prepare next TxBit (Pre-load).
GOTO INTEND
HDLC_Rx_Int:
BCF INTCON,INTF ; Clear int RB0 flag.
CALL HDLC_Rx ; Clock changed to High, read RxPin from Quantar and save it on Buffer.
BANK1
BCF OPTION_REG,INTEDG ; Set RB0 Edge trigger to Low.
BANK0
GOTO INTEND
AUSARTRx:
CALL RS232_Rx
GOTO INTEND
;***** INTERUPTION FINALIZE *****************************
INTEND: MOVF FSR_TEMP,W ;Restore PIC state
MOVWF FSR
MOVF P_TEMP,W
MOVWF PCLATH
SWAPF S_TEMP,W
MOVWF STATUS
SWAPF W_TEMP,F
SWAPF W_TEMP,W
RETFIE
;***** START *****************************
START: CLRWDT
BANK0
BANK0X
;*** CLEAR PORTS
CLRF PORTA ; Clear ports
CLRF PORTB ; ////
CLRF PORTC ; ///
CLRF PORTD ; //
CLRF PORTE ; /
CLRF FLAGS_1 ; Clear flags
CLRF FLAGS_2 ; /
; BSF ServiceFlag ; Flag for development use.
;*** CONFIGURE PORTS
BANK1
CLRF TRISA ; PORTA OUT (LEDs)
MOVLW B'00000011' ; PORTB HDLC port data directions.
BTFSC ServiceFlag ; Only for development test.
MOVLW B'00000001' ; PORTB HDLC port data directions only for Debug.
MOVWF TRISB ; PORTB IN/OUT
MOVLW B'10001111' ; PORTC High Nible = AUSART Port, Low Nible = ID.
MOVWF TRISC ; PORTC IN/OUT
CLRF TRISD ; PORTD IN/OUT (not used)
CLRF TRISE ; PORTE OUT (not used)
BANK1X
CLRF ANSEL ; SET PORTA AS DIGITAL PORTS
CLRF ANSELH ; SET PORTB AS DIGITAL PORTS
BANK0X
BANK0
;*** Startup LEDs.
MOVLW 0x0F ; Turn On all LEDs.
MOVWF PORTA
;*** Initialize AUSART RS-232 Tx
BANK1
MOVLW B'00100110' ; AUSART Config.
MOVWF TXSTA
MOVLW Baud_Rate_Const ; Set the Raspberry Baud Rate.
MOVWF SPBRG
BANK0
BSF RCSTA,SPEN ; Enable Serial Port.
;*** !RESET!n Message.
MOVLW 0x7E ; Start Flag to Tx.
BTFSS PIR1,TXIF ; Tx Modified Data Byte.
GOTO $-1 ; //
MOVWF TXREG ; /
MOVLW "!" ; Char to Tx.
BTFSS PIR1,TXIF ; Tx Modified Data Byte.
GOTO $-1 ; //
MOVWF TXREG ; /
MOVLW "R" ; Char to Tx.
BTFSS PIR1,TXIF ; Tx Modified Data Byte.
GOTO $-1 ; //
MOVWF TXREG ; /
MOVLW "E" ; Char to Tx.
BTFSS PIR1,TXIF ; Tx Modified Data Byte.
GOTO $-1 ; //
MOVWF TXREG ; /
MOVLW "S" ; Char to Tx.
BTFSS PIR1,TXIF ; Tx Modified Data Byte.
GOTO $-1 ; //
MOVWF TXREG ; /
MOVLW "E" ; Char to Tx.
BTFSS PIR1,TXIF ; Tx Modified Data Byte.
GOTO $-1 ; //
MOVWF TXREG ; /
MOVLW "T" ; Char to Tx.
BTFSS PIR1,TXIF ; Tx Modified Data Byte.
GOTO $-1 ; //
MOVWF TXREG ; /
MOVLW "!" ; Char to Tx.
BTFSS PIR1,TXIF ; Tx Modified Data Byte.
GOTO $-1 ; //
MOVWF TXREG ; /
MOVFW PORTC ; Board ID to Tx (jumper settings).
ANDLW 0x0F ; Only Low Nibble.
BTFSS PIR1,TXIF ; Tx Modified Data Byte.
GOTO $-1 ; //
MOVWF TXREG ; /
MOVLW 0x7E ; Footer Start Flag to Tx.
BTFSS PIR1,TXIF ; Tx Modified Data Byte.
GOTO $-1 ; //
MOVWF TXREG ; /
;*** Initialize AUSART RS-232 Rx
MOVLW B'10010000' ; AUSART Config.
MOVWF RCSTA
;*** Initialize interrupts
CLRF INTCON
BSF INTCON,INTE ; Enable RB0 External Interrupt.
BSF INTCON,PEIE ; Enable periferial Interrupts (AUSART Rx, Timer 1).
BANK1
BCF OPTION_REG,INTEDG ; Set RB0 Edge trigger to Low.
BSF PIE1,RCIE ; Enable AUSART Rx Interrupts.
BANK0
BCF INTCON,INTF ; Clear RB0 Interrupt flag.
MOVFW RCREG ; Clear AUSART Rx Interrupt Flag by reading Rx Buffer.
BTFSS ServiceFlag ; Flag for development use.
BSF INTCON,GIE ; Enable Global Interrupts.
;*** Last setings
CLRF BitIndex ; Clear HDLC_Rx Bit position counter.
CLRF OnesCount ; Clear HDLC_Rx ONEs received Counter.
CLRF SRxByte ; Clear HDLC_Rx Byte Rx Buffer.
BSF FooterWasTx ; Async Footer was Tx.
CLRF ABufferInLen ; Clear RS232_Rx Counter.
CLRF ARxByte ; Clear RS232_Rx RAM Byte.
CLRF SBufferOutLen
CLRF TxBitIndex
CLRF TxOnesCount
CLRF TxByteIndex
CLRF SyncTxByte
CLRF PORTA ; Turn Off all LEDs.
BSF HeaderBeingTx ; Enable continous Idle HDLC_Tx StartByte.
BCF TxPin ; Clear HDLC_Tx Pin.
CLRW ; Clear W register.
;***** Loops *****************************
LOOPS: ; Main loop
; CLRF PORTA ; Turn Off all LEDs.
BCF SRxGLED
BCF SRxRLED
BCF ARxGLED
; BTFSC ServiceFlag ; Flag for development use.
; CALL A2S_TestPat
BTFSC SRxComplete ; If a HDCL frame was received from Quantar,
CALL RS232_Tx ; Send it to the raspberry Pi.
NOP
GOTO LOOPS
;************************************************************
;***** HDLC Receive *************************************
;************************************************************
HDLC_Rx:
BTFSC RxPin ; Read HDLC pin, if ValueRx is ONE then:
GOTO OneOptions ; Goto see how many ONEs we have.
;0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
ZeroOptions: ; So it is a ZERO.
BTFSC SRxSixOnes ; If we are receiving a Header then
GOTO HeaderFinalZero ; This is the last Header Byte, the ZERO.
BTFSC SRxFiveOnes ; If We have just Rx five ONEs.
GOTO DummyZero ; Set a flag to remember for decide on next Bit arrival
CLRF OnesCount ; reset ONEs counter, because we Rx a ZERO.
CALL SaveZero ; Save the received Bit on Buffer word.
RETURN
DummyZero:
BCF SRxFiveOnes
CLRF OnesCount
RETURN
HeaderFinalZero:
BCF SRxFiveOnes
BCF SRxSixOnes
CLRF OnesCount
CLRF BitIndex
BSF SHeaderRx
BSF SRxComplete ; Rx Sync Frame is complete.
RETURN
;1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
OneOptions:
INCF OnesCount,F ; ONEs + 1.
MOVFW OnesCount ; Review last number of consecutive ONEs.
SUBLW 0x05
BTFSC ZERO ; If there were already five ONEs then this is ONE number six so:
GOTO FifthOneRx ; goto FiveOnesRx, which will activate 5 Ones flag.
MOVFW OnesCount ; Review last number of consecutive ONEs + valueRx(1).
SUBLW 0x06
BTFSC ZERO ; If there were already five ONEs then this is ONE number six so:
GOTO SixthOneRx ; goto FLAGRx, which will check if it is Header or Footer and resync our clock.
MOVFW OnesCount ; Review last number of consecutive ONEs + valueRx(1).
SUBLW 0x07
BTFSC ZERO ; If there were already six ONEs then this is ONE number seven so:
GOTO SeventhOneRx ; So AbortRx.
; Less than 5 ONES have been Rx so:
CALL SaveOne ; Save the received Bit on Buffer word.
BCF SDummyZeroFlag ; Clear flag that aim us to find inserted ZEROs.
RETURN
FifthOneRx:
BSF SRxFiveOnes
CALL SaveOne ; Save the received Bit on Buffer word.
RETURN
SixthOneRx:
BSF SRxGLED
BSF SRxSixOnes ; else, this flag help us ignore last ZERO on Header B'01111110'
RETURN
SeventhOneRx:
BSF SRxRLED
BSF SRxSevenOnes ; This flag help us know Abort happened.
BCF SRxComplete ; Ignore Rx Data.
DECF OnesCount,F ; ONEs - 1. Any way they are seven or more, so do not overrun our counter.
RETURN
;************************************************************
;***** RS-232 Tx *****************************************
;************************************************************
RS232_Tx:
BTFSC SHeaderRx
GOTO AHeaderTx
CALL TestEscapeCh ; Test for Escape Characters.
BCF SRxComplete ; Clear the flag to know data has been sent thru RS-232.
RETURN
AHeaderTx:
BTFSS FooterWasTx ; Was previuos Byte an Async footer?
GOTO TxHeader1 ; No so Tx Footer.
BCF SHeaderRx ; Flag to accept header.
BCF SRxComplete ; Clear the flag to know data has been sent thru RS-232.
RETURN
TxHeader1:
BSF FooterWasTx ; Flag to remember Start Flag was sent.
BTFSS PIR1,TXIF ; Transmit Start byte 0x7E.
GOTO $-1 ; ///
MOVLW 0x7E ; // Start Flag
MOVWF TXREG ; /
BCF SHeaderRx
BCF SRxComplete ; Clear the flag to know data has been sent thru RS-232.
RETURN
TestEscapeCh:
BTFSC FooterWasTx ; Previous Byte was Footer?
CALL AHeader ; New Data, so send its Header.
MOVFW ATxByte
SUBLW 0x7E ; StartFlag
BTFSS ZERO ; If Byte is equal to 0x7E then we need to add escape character.
GOTO NOT0x7E ; Not a 0x7E.
BTFSS CARRY ;
GOTO NOT0x7E ; Not a 0x7E.
GOTO InsEsc ; It is a 0x7E si insert the Escape Character.
NOT0x7E:
MOVFW ATxByte
SUBLW 0x7D ; Escape Char 0x7D.
BTFSS ZERO ; If Byte is equal to 0x7D then we need to add escape character.
GOTO NOT0x7D ; Not a 0x7D.
BTFSS CARRY ;
GOTO NOT0x7D ; Not a 0x7D.
InsEsc: ; EscapeCharInsert:
MOVLW 0x7D ; Insert Escape Character 0x7D.
BTFSS PIR1,TXIF ; /// Tx Char.
GOTO $-1 ; //
MOVWF TXREG ; /
MOVFW ATxByte ; Data to Tx. Make Bit stuffing:
BTFSS ATxByte,5 ; If Bit 5 != 1.
BSF ATxByte,5 ; Bit 5 = 1.
BTFSC ATxByte,5 ; If Bit 5 != 0.
BCF ATxByte,5 ; Bit 5 = 0.
MOVFW ATxByte ; Reload Modified Byte.
BTFSS PIR1,TXIF ; Tx Modified Data Byte.
GOTO $-1 ; //
MOVWF TXREG ; /
RETURN
NOT0x7D:
MOVFW ATxByte ; Byte is a normal Character so, send it.
BTFSS PIR1,TXIF ; ///
GOTO $-1 ; //
MOVWF TXREG ; /
RETURN
AHeader:
BTFSS PIR1,TXIF ; Transmit Start byte 0x7E.
GOTO $-1 ; ///
MOVLW 0x7E ; // StartFlag
MOVWF TXREG ; /
BCF FooterWasTx ; Start Byte Tx, so we will nedd a footer later.
RETURN
;************************************************************
;***** RS-232 Rx *****************************************
;************************************************************
RS232_Rx:
BSF ARxGLED ; Blink Green LED so we know RS-232 is Rx.
BTFSC RCSTA,FERR ; Look for errors on the Serial port Rx.
BSF ARxRLED ; ///
BTFSC RCSTA,OERR ; //
BSF ARxRLED ; /
MOVFW RCREG ; Read the Received byte.
BTFSC ServiceFlag ; This allow us insert a custom word to emulate we Rx data from RS-232 while debugging.
MOVFW Testing ; /
MOVWF ARxByte ; Save Rx data.
MOVFW ARxByte
SUBLW 0x7E ; Test for Start Flag B'01111110'
BTFSS ZERO ; If Byte is equal to 0x7E then this is the Header.
GOTO TestEscChar ; //// It is not 0x7E.
BTFSS CARRY ; ///
GOTO TestEscChar ; // It is not 0x7E.
GOTO A_FooterRx ; /
A_FooterRx:
BCF WritingBuffer ; Footer Flag received 0x7E.
MOVFW ABufferInLen ; Save the number of words Rx.
BTFSS ZERO ; Set a flag to know a frame was Rx.
BSF DataReady ; /
RETURN
TestEscChar:
MOVFW ARxByte ; Load Rx word.
SUBLW 0x7D ; Test for Escape Char 0x7D.
BTFSS ZERO ; If Byte is equal to 0x7D then this could be an escape character.
GOTO RxAData ; ///
BTFSS CARRY ; //
GOTO RxAData ; /
BSF EscapeCharFlag ; Escape Char Flag
RETURN
RxAData:
BTFSC EscapeCharFlag ; If previous byte was an Escape Character
GOTO EscChar ; /
BSF WritingBuffer ; So we know the Buffer contains some Data.
CALL SaveAsyncRx ; Save Byte on Buffer2.
RETURN
EscChar:
BCF EscapeCharFlag ; Clear the Escape Char Flag
MOVFW ARxByte
SUBLW 0x5E
BTFSS ZERO ; If Byte is equal to 0x5E then it is a modified
GOTO Char_5D ; /// Character.
BTFSS CARRY ; //
GOTO Char_5D ; /
MOVLW 0x7E ; Character 0x7E recovery.
MOVWF ARxByte
CALL SaveAsyncRx ; Save Byte on Buffer2.
RETURN
Char_5D:
MOVFW ARxByte ; If Byte is equal to 0x5D ten it is a modified Character preceded by a header.
SUBLW 0x5D ; /////
BTFSS ZERO ; ////
GOTO RS232Rx_Err ; ///
BTFSS CARRY ; //
GOTO RS232Rx_Err ; /
MOVLW 0x7D ; Character 0x7D recovery.
MOVWF ARxByte
BSF WritingBuffer ; So we know the Buffer contains some Data.
CALL SaveAsyncRx ; Save Byte on Buffer2.
RETURN
RS232Rx_Err:
GOTO $-0 ; Reset the PIC overloading WDT.
;************************************************************
;***** Copy Rx to Tx RAM. ***********************************
;************************************************************
SwapMem_A_S:
MOVFW ABufferIn0 ; Pass Rx Async Buffer data to Sync Data Tx RAM.
MOVWF SBufferOut0
MOVFW ABufferIn1
MOVWF SBufferOut1
MOVFW ABufferIn2
MOVWF SBufferOut2
MOVFW ABufferIn3
MOVWF SBufferOut3
MOVFW ABufferIn4
MOVWF SBufferOut4
MOVFW ABufferIn5
MOVWF SBufferOut5
MOVFW ABufferIn6
MOVWF SBufferOut6
MOVFW ABufferIn7
MOVWF SBufferOut7
MOVFW ABufferIn8
MOVWF SBufferOut8
MOVFW ABufferIn9
MOVWF SBufferOut9
MOVFW ABufferIn10
MOVWF SBufferOut10
MOVFW ABufferIn11
MOVWF SBufferOut11
MOVFW ABufferIn12
MOVWF SBufferOut12
MOVFW ABufferIn13
MOVWF SBufferOut13
MOVFW ABufferIn14
MOVWF SBufferOut14
MOVFW ABufferIn15
MOVWF SBufferOut15
MOVFW ABufferIn16
MOVWF SBufferOut16
MOVFW ABufferIn17
MOVWF SBufferOut17
MOVFW ABufferIn18
MOVWF SBufferOut18
MOVFW ABufferIn19
MOVWF SBufferOut19
MOVFW ABufferIn20
MOVWF SBufferOut20
MOVFW ABufferIn21
MOVWF SBufferOut21
MOVFW ABufferIn22
MOVWF SBufferOut22
MOVFW ABufferIn23
MOVWF SBufferOut23
MOVFW ABufferIn24
MOVWF SBufferOut24
MOVFW ABufferIn25
MOVWF SBufferOut25
MOVFW ABufferIn26
MOVWF SBufferOut26
MOVFW ABufferIn27
MOVWF SBufferOut27
MOVFW ABufferIn28
MOVWF SBufferOut28
MOVFW ABufferIn29
MOVWF SBufferOut29
MOVFW ABufferIn30
MOVWF SBufferOut30
MOVFW ABufferIn31
MOVWF SBufferOut31
BANK1
MOVFW ABufferIn32
MOVWF SBufferOut32
MOVFW ABufferIn33
MOVWF SBufferOut33
MOVFW ABufferIn34
MOVWF SBufferOut34
MOVFW ABufferIn35
MOVWF SBufferOut35
MOVFW ABufferIn36
MOVWF SBufferOut36
MOVFW ABufferIn37
MOVWF SBufferOut37
MOVFW ABufferIn38
MOVWF SBufferOut38
MOVFW ABufferIn39
MOVWF SBufferOut39
MOVFW ABufferIn40
MOVWF SBufferOut40
MOVFW ABufferIn41
MOVWF SBufferOut41
MOVFW ABufferIn42
MOVWF SBufferOut42
MOVFW ABufferIn43
MOVWF SBufferOut43
MOVFW ABufferIn44
MOVWF SBufferOut44
MOVFW ABufferIn45
MOVWF SBufferOut45
MOVFW ABufferIn46
MOVWF SBufferOut46
MOVFW ABufferIn47
MOVWF SBufferOut47
MOVFW ABufferIn48
MOVWF SBufferOut48
MOVFW ABufferIn49
MOVWF SBufferOut49
MOVFW ABufferIn50
MOVWF SBufferOut50
MOVFW ABufferIn51
MOVWF SBufferOut51
MOVFW ABufferIn52
MOVWF SBufferOut52
MOVFW ABufferIn53
MOVWF SBufferOut53
MOVFW ABufferIn54
MOVWF SBufferOut54
MOVFW ABufferIn55
MOVWF SBufferOut55
MOVFW ABufferIn56
MOVWF SBufferOut56
MOVFW ABufferIn57
MOVWF SBufferOut57
MOVFW ABufferIn58
MOVWF SBufferOut58
MOVFW ABufferIn59
MOVWF SBufferOut59
MOVFW ABufferIn60
MOVWF SBufferOut60
MOVFW ABufferIn61
MOVWF SBufferOut61
MOVFW ABufferIn62
MOVWF SBufferOut62
MOVFW ABufferIn63
MOVWF SBufferOut63
MOVFW ABufferIn64
MOVWF SBufferOut64
MOVFW ABufferIn65
MOVWF SBufferOut65
MOVFW ABufferIn66
MOVWF SBufferOut66
MOVFW ABufferIn67
MOVWF SBufferOut67
MOVFW ABufferIn68
MOVWF SBufferOut68
MOVFW ABufferIn69
MOVWF SBufferOut69
MOVFW ABufferIn70
MOVWF SBufferOut70
MOVFW ABufferIn71
MOVWF SBufferOut71
BANK0
BANK1X
MOVFW ABufferIn72
MOVWF SBufferOut72
MOVFW ABufferIn73
MOVWF SBufferOut73
MOVFW ABufferIn74
MOVWF SBufferOut74
MOVFW ABufferIn75
MOVWF SBufferOut75
MOVFW ABufferIn76
MOVWF SBufferOut76
MOVFW ABufferIn77
MOVWF SBufferOut77
MOVFW ABufferIn78
MOVWF SBufferOut78
MOVFW ABufferIn79
MOVWF SBufferOut79
MOVFW ABufferIn80
MOVWF SBufferOut80
MOVFW ABufferIn81
MOVWF SBufferOut81
MOVFW ABufferIn82
MOVWF SBufferOut82
MOVFW ABufferIn83
MOVWF SBufferOut83
MOVFW ABufferIn84
MOVWF SBufferOut84
MOVFW ABufferIn85
MOVWF SBufferOut85
MOVFW ABufferIn86
MOVWF SBufferOut86
MOVFW ABufferIn87
MOVWF SBufferOut87
MOVFW ABufferIn88
MOVWF SBufferOut88
MOVFW ABufferIn89
MOVWF SBufferOut89
MOVFW ABufferIn90
MOVWF SBufferOut90
MOVFW ABufferIn91
MOVWF SBufferOut91
MOVFW ABufferIn92
MOVWF SBufferOut92
MOVFW ABufferIn93
MOVWF SBufferOut93
MOVFW ABufferIn94
MOVWF SBufferOut94
MOVFW ABufferIn95
MOVWF SBufferOut95
MOVFW ABufferIn96
MOVWF SBufferOut96
MOVFW ABufferIn97
MOVWF SBufferOut97
MOVFW ABufferIn98
MOVWF SBufferOut98
MOVFW ABufferIn99
MOVWF SBufferOut99
MOVFW ABufferIn100
MOVWF SBufferOut100
MOVFW ABufferIn101
MOVWF SBufferOut101
MOVFW ABufferIn102
MOVWF SBufferOut102
MOVFW ABufferIn103
MOVWF SBufferOut103
MOVFW ABufferIn104
MOVWF SBufferOut104
MOVFW ABufferIn105
MOVWF SBufferOut105
MOVFW ABufferIn106
MOVWF SBufferOut106
MOVFW ABufferIn107
MOVWF SBufferOut107
MOVFW ABufferIn108
MOVWF SBufferOut108
MOVFW ABufferIn109