chiark / gitweb /
better manipulation of the per-pic led
[trains.git] / detpic / serout.asm
1 ;======================================================================
2 ; SERIAL PORT - TRANSMISSION TO HOST
3
4   include common.inc
5   code
6
7 ;======================================================================
8 ; QUEUEING MESSAGES FOR TRANSMISSION
9
10 ;----------
11 addbyte_toomany panic morse_HB
12
13 ;----------------------------------------
14 serial_addbyte @
15 ;  W                    byte to xmit to host    trashed
16 ;  FSR0                 any                     set for serial_addbyte_another
17 ;  outbuf, outmsg_*     buffer not full         adjusted appropriately
18 ;  STATUS               any                     trashed
19 ;  all others           any                     not interfered with
20 ;
21         mov_lfsr outbuf, 0
22         mov_ff  outmsg_end, FSR0L
23 ;...
24 ;----------------------------------------
25 serial_addbyte_another @
26 ;  W                    byte to xmit to host    trashed
27 ;  FSR0                 from _addbyte[_another] updated for ..._another again
28 ;  outbuf, outmsg_*     buffer not full         adjusted appropriately
29 ;  STATUS               any                     trashed
30 ;  all others           any                     not interfered with
31 ;
32         mov_wf  POSTINC0
33         bc_f    FSR0L, 7
34         mov_fw  FSR0L
35         mov_wf  outmsg_end
36         xor_wfw outmsg_begin
37         bra_z   addbyte_toomany
38         ; no, we're ok:
39
40         bt_f_if1 PIE1, TXIE
41         return ; don't bother messing about if tx is already enabled
42
43 ; we fall through to portb_read to reenable TXIE if appropriate
44 ;...
45 ;======================================================================
46 ; FLOW CONTROL BY HOST OF OUR TRANSMISSIONS
47
48 ;...
49 ;----------------------------------------
50 portb_read @
51 ;
52 ;  W                    undefined       value from PORTB
53 ;  TXIE                 any             enabled iff host allows us to xmit
54 ;
55 ; Note that this will reenable TXIE even if the serial buffer is empty,
56 ; every time portb_read is called.  This doesn't matter very much
57 ; because the serialtx_intrl routine will disable it again straight
58 ; away.
59 ;
60         mov_fw  PORTB
61         bc_f    INTCON, RBIF
62
63         bt_w_if1  p0_rs232_fcin >> 4
64         bra     txfc_disable
65         ; tx enable:
66
67  ;pin_h pall_perpicled
68         call    led_green ; we're transmitting
69         bs_f    PIE1, TXIE
70         return
71
72 ;----------
73 txfc_disable
74         bc_f    PIE1, TXIE
75  ;pin_l pall_perpicled
76         goto    led_red ; flow control forces us not to transmit
77
78 ;----------------------------------------------------------------------
79 serialtxfc_intrl @
80         bt_f_if0 INTCON, RBIF
81         return
82         ; yes, it's us:
83
84         rcall   portb_read ; check flow control
85         intrl_handled_nostack
86
87 ;----------------------------------------------------------------------
88 serialtxfc_init @
89         bc_f    INTCON2, RBIP
90         bs_f    INTCON, RBIE
91         rcall   portb_read
92         pin_l   p0_rs232_fcout ; set outgoing RTS/CTS active
93         return
94
95 ;======================================================================
96 ; ACTUAL TRANSMISSION
97
98 ;----------------------------------------------------------------------
99 serialtx_intrl @
100         ; are we ready to transmit ?
101         bt_f_if0 PIR1, TXIF
102         return
103         bt_f_if0 PIE1, TXIE
104         return
105         ; yes, it's us:
106
107         mov_lfsr outbuf, 0
108         mov_fw  outmsg_begin
109         mov_wf  FSR0L
110         xor_wfw outmsg_end
111         bra_z   tx_bufempty
112
113         mov_fw  INDF0
114         mov_wf  TXREG
115         bra_n   tx_justsent_noacknmra
116         ; we've just sent the last byte of some message:
117         ; maybe we push an NMRADONE message on the front
118         mov_fw  acknmra
119         bra_nz  tx_acknmra_insert
120 tx_justsent_noacknmra
121         inc_f   outmsg_begin
122         bc_f    outmsg_begin, outbuf_szln2
123         call    i2c_consider_restartread
124 tx_alliswell
125         intrl_handled_nostack
126
127 ;----------
128 tx_acknmra_insert
129         sub_wff acknmra ; we're acking these now
130         mov_wf  INDF0 ; write it over the message we just sent
131         bra     tx_alliswell
132
133 ;----------
134 tx_acknmra_send
135         sub_wff acknmra ; we're acking these now
136         mov_wf  TXREG
137         bra     tx_alliswell
138
139 ;----------
140 tx_bufempty
141         ; maybe we send an NMRADONE
142         mov_fw  acknmra
143         bra_nz  tx_acknmra_send
144         ; nothing at all to do:
145         bc_f    PIE1, TXIE
146  ;pin_z pall_perpicled
147         call    led_black ; we're not transmitting
148         intrl_handled_nostack
149
150 ;----------------------------------------------------------------------
151 serialtxbuf_init @
152         clr_f   outmsg_end
153         clr_f   outmsg_begin
154         mov_lw  b'00001001'
155         goto    serial_addbyte
156
157 ;======================================================================
158   include final.inc