604
|
1 ;=============================================================================
|
|
2 ;
|
623
|
3 ; File rx_ops.asm combined next generation V3.03.1
|
604
|
4 ;
|
|
5 ; RX (Tank Pressure Transmitter) Routines.
|
|
6 ;
|
|
7 ; Copyright (c) 2018, heinrichs weikamp, all right reserved.
|
|
8 ;=============================================================================
|
|
9
|
|
10 #include "hwos.inc" ; mandatory header
|
|
11 #include "shared_definitions.h" ; mailbox to p2_deco.c
|
|
12 #include "i2c.inc"
|
|
13 #include "math.inc"
|
623
|
14 #include "wait.inc"
|
|
15
|
|
16 ;=============================================================================
|
|
17
|
|
18 IFDEF _rx_functions
|
|
19
|
604
|
20
|
|
21 extern get_first_gas_to_WREG
|
623
|
22 extern I2C_update_OSTC_rx
|
|
23 extern I2C_probe_OSTC_rx
|
|
24 extern rx_firmware_storage
|
|
25
|
|
26 IFDEF _ccr_pscr
|
604
|
27 extern get_first_dil_to_WREG
|
623
|
28 ENDIF
|
604
|
29
|
|
30
|
|
31 ; SAC calculation averaging settings
|
|
32 #DEFINE time_accu_target_OC .60 ; [s] target time accumulator filling level in OC modes
|
|
33 #DEFINE time_accu_target_CCR .180; [s] target time accumulator filling level in CCR mode
|
|
34
|
|
35
|
|
36 rx_ops CODE
|
|
37
|
623
|
38 ;=============================================================================
|
604
|
39
|
623
|
40
|
|
41 ;-----------------------------------------------------------------------------
|
604
|
42 ; Get configured pressure readings
|
|
43 ;
|
|
44 ; input : opt_TR_1st_pres - selector for first pressure reading, in normal mode
|
|
45 ; opt_TR_Bail_pres - selector for first pressure reading, in bailout mode
|
|
46 ; opt_TR_2nd_pres - selector for second pressure reading, all modes
|
|
47 ;
|
|
48 ; output: int_IO_pressure_value+0/+2 - pressure in 0.1 bar, low byte
|
|
49 ; int_IO_pressure_value+1/+3 - pressure in 0.1 bar, high byte, including data not available flag
|
|
50 ; char_I_pressure_gas +0/+1 - associated gas (1-5) / diluent (6-10)
|
|
51 ; char_I_pressure_age +0/+1 - age of data in seconds (max 60)
|
|
52 ; char_I_pressure_stat +0/+1 - status data
|
|
53 ;
|
|
54 global get_pressure_readings
|
|
55 get_pressure_readings:
|
|
56 call I2C_get_tankdata ; get raw pressure data
|
|
57 get_pressure_readings_1:
|
|
58 movff opt_TR_1st_pres,ul ; 1st pressure to read
|
623
|
59 btfsc bailout_mode ; in bailout mode?
|
604
|
60 movff opt_TR_Bail_pres,ul ; YES - replace with assigned bailout selection
|
|
61 tstfsz ul ; disabled?
|
|
62 bra get_pressure_readings_1a ; NO - get received pressure data
|
|
63 rcall get_pressure_readings_H1 ; YES - set pressure data to not available
|
|
64 bra get_pressure_readings_1c ; - continue with copying to result vars
|
|
65 get_pressure_readings_1a:
|
623
|
66 IFDEF _ccr_pscr
|
604
|
67 movlw .11 ; first code for "special" pressures
|
|
68 cpfslt ul ; opt_TR_1st_pres < 11 ?
|
|
69 rcall get_pressure_readings_H2 ; NO - pre-process measurement selection
|
623
|
70 ENDIF
|
604
|
71 rcall get_pressure_readings_H3 ; get transmitter ID
|
|
72 rcall get_pres_by_transmitter_id ; get data from transmitter with ID in hi:lo into hi:lo (pressure) and up (status)
|
|
73 get_pressure_readings_1c:
|
|
74 movff lo,int_IO_pressure_value+0 ; copy pressure, low byte
|
|
75 movff hi,int_IO_pressure_value+1 ; copy pressure, high byte
|
|
76 movff ul,char_I_pressure_gas+0 ; copy associated gas number
|
|
77 movff up,char_I_pressure_age+0 ; copy age of data
|
|
78 movff ex,char_I_pressure_stat+0 ; copy status data
|
|
79 get_pressure_readings_2:
|
|
80 movff opt_TR_2nd_pres,ul ; 2nd pressure to read
|
|
81 tstfsz ul ; disabled?
|
|
82 bra get_pressure_readings_2a ; NO - get received pressure data
|
|
83 rcall get_pressure_readings_H1 ; YES - set pressure data to not available
|
|
84 bra get_pressure_readings_2c ; - continue with copying to result vars
|
|
85 get_pressure_readings_2a:
|
623
|
86 IFDEF _ccr_pscr
|
604
|
87 movlw .11 ; first code for "special" pressures
|
|
88 cpfslt ul ; opt_TR_2nd_pres < 11 ?
|
|
89 rcall get_pressure_readings_H2 ; NO - pre-process measurement selection
|
623
|
90 ENDIF
|
604
|
91 btfss ul,7 ; gas selector >= 127 (special treatment)?
|
|
92 bra get_pressure_readings_2b ; NO - proceed reading a pressure
|
|
93 rcall get_pressure_readings_H1 ; YES - set pressure data to not available
|
|
94 bra get_pressure_readings_2c ; - continue wit copying to output data
|
|
95 get_pressure_readings_2b:
|
|
96 rcall get_pressure_readings_H3 ; get transmitter ID
|
|
97 rcall get_pres_by_transmitter_id ; get data from transmitter with ID in hi:lo into hi:lo (pressure) and up (status)
|
|
98 get_pressure_readings_2c:
|
|
99 movff lo,int_IO_pressure_value+2 ; copy pressure, low byte
|
|
100 movff hi,int_IO_pressure_value+3 ; copy pressure, high byte
|
|
101 movff ul,char_I_pressure_gas+1 ; copy associated gas number
|
|
102 movff up,char_I_pressure_age+1 ; copy age of data
|
|
103 movff ex,char_I_pressure_stat+1 ; copy status data
|
|
104 return
|
|
105
|
|
106 get_pressure_readings_H1:
|
|
107 clrf lo ; set pressure to 0 (low byte)
|
|
108 clrf hi ; set pressure to 0 (high byte)
|
|
109 clrf up ; set age to 0
|
|
110 clrf ex ; set status to 0
|
|
111 bsf hi,int_not_avail_flag ; set flag for data not available
|
|
112 return
|
|
113
|
623
|
114
|
|
115 IFDEF _ccr_pscr
|
604
|
116 get_pressure_readings_H2:
|
|
117 movlw .11
|
|
118 subwf ul,F
|
|
119 bnz get_pressure_readings_H2a
|
|
120 call get_first_gas_to_WREG ; ul = 11 -> ul = (get_first_gas_to_WREG)
|
|
121 movwf ul
|
|
122 return
|
|
123 get_pressure_readings_H2a:
|
|
124 decfsz ul,F
|
|
125 bra get_pressure_readings_H2b
|
|
126 movff active_gas,ul ; ul = 12 -> ul = active_gas
|
|
127 return
|
|
128 get_pressure_readings_H2b:
|
|
129 decfsz ul,F
|
|
130 bra get_pressure_readings_H2c
|
|
131 call get_first_dil_to_WREG ; ul = 13 -> ul = (get_first_dil_to_WREG) + 5
|
|
132 movwf ul
|
|
133 movlw .5
|
|
134 addwf ul,F
|
|
135 return
|
|
136 get_pressure_readings_H2c:
|
|
137 decfsz ul,F
|
|
138 bra get_pressure_readings_H2d
|
|
139 movff active_dil,ul ; ul = 14 -> ul = active_dil + 5
|
|
140 movlw .5
|
|
141 addwf ul,F
|
|
142 return
|
|
143 get_pressure_readings_H2d:
|
|
144 movlw .1
|
|
145 movwf ul ; ul >= 15 -> should not happen, default to ul = 1
|
|
146 return
|
623
|
147 ENDIF ; _ccr_pscr
|
|
148
|
604
|
149
|
|
150 get_pressure_readings_H3:
|
|
151 lfsr FSR1,opt_transmitter_id_1 ; load base address of transmitter ID array
|
|
152 decf ul,W ; (1-10) -> (0-9)
|
|
153 mullw .2 ; IDs are 2 byte in size
|
|
154 movf PRODL,W ; WREG is index now
|
|
155 movff PLUSW1,lo ; get transmitter ID (low byte)
|
|
156 incf WREG,W ; increment index
|
|
157 movff PLUSW1,hi ; get transmitter ID (high byte)
|
|
158 return
|
|
159
|
|
160
|
623
|
161 ;-----------------------------------------------------------------------------
|
604
|
162 ; Get data from transmitter with ID in (hi:lo)
|
|
163 ;
|
|
164 ; input: hi:lo = transmitter ID
|
|
165 ;
|
|
166 ; output: hi:lo = pressure in 0.1 bar if return code = 0, else 0 + int_not_avail_flag
|
|
167 ; up = packet age if return code = 0, else 0
|
|
168 ; ex = status data if return code = 0, else 0
|
|
169 ;
|
|
170 ; return code: 0 transmitter found
|
|
171 ; 254 illegal transmitter ID
|
|
172 ; 255 transmitter not found
|
|
173 ;
|
|
174 ; RX slot data: Byte 0 : transmitter ID high byte
|
|
175 ; 1 : transmitter ID low byte
|
|
176 ; 2 (-> hi) : pressure in 0.1 bar, high byte
|
|
177 ; 3 (-> lo) : pressure in 0.1 bar, low byte
|
|
178 ; 4 (-> ex) : status data:
|
|
179 ; bit 2..0: battery status
|
|
180 ; 5..3: sequence counter
|
|
181 ; 6: =1 if pressure changed (usage in RX slot data)
|
|
182 ; =1 if transmitter not found (usage in output data)
|
|
183 ; 7: =1 if pressure sensed (usage in RX slot data)
|
|
184 ; =1 if low battery (< 3.0V) (usage in output data)
|
|
185 ; 5 (-> up) : age of data in seconds, 0 if slot is unused
|
|
186 ;
|
|
187 global get_pres_by_transmitter_id
|
|
188 get_pres_by_transmitter_id:
|
|
189 ; hi:lo zero?
|
|
190 tstfsz hi ; hi <> zero ?
|
|
191 bra output_pressure_1 ; YES - search transmitter
|
|
192 tstfsz lo ; lo <> zero ?
|
|
193 bra output_pressure_1 ; YES - search transmitter
|
|
194 movlw .254 ; NO to both - set return code for invalid transmitter ID
|
|
195 bra output_pressure_6 ; - clear result vars, set data as not available and return
|
|
196 output_pressure_1:
|
|
197 lfsr FSR1,rx_buffer ; load base address of RX buffer
|
|
198 movlw .8 ; 8 RX slots to look at
|
|
199 movwf up ; up will be the loop counter
|
|
200 output_pressure_2:
|
|
201 movf POSTINC1,W ; get high byte of received transmitter ID
|
|
202 cpfseq hi ; match?
|
|
203 bra output_pressure_3 ; NO - check next slot
|
|
204 movf POSTINC1,W ; get low byte byte received transmitter ID
|
|
205 cpfseq lo ; match?
|
|
206 bra output_pressure_4 ; NO - check next slot
|
|
207 ; transmitter found, gather data ; YES - transmitter found, copy:
|
|
208 movff POSTINC1,hi ; - pressure high byte,
|
|
209 movff POSTINC1,lo ; - pressure low byte,
|
|
210 movff POSTINC1,ex ; - status byte, and
|
|
211 movff POSTINC1,up ; - packet age.
|
|
212 movlw rx_packet_overdue_timeout ; - load overdue time
|
|
213 subwf up,W ; - subtract overdue time from packet age
|
|
214 btfss STATUS,N ; - result negative (packet younger than overdue time)?
|
|
215 bsf hi,int_outdated_flag ; NO - set outdated flag
|
|
216 bcf ex,char_transmitter_lost ; - clear transmitter lost flag
|
|
217 bsf ex,char_transmitter_low_bat ; - set low battery warning by default
|
|
218 btfsc ex,1 ; - bit 1 of battery voltage set?
|
|
219 bcf ex,char_transmitter_low_bat ; - YES - revoke low battery warning
|
|
220 btfsc ex,2 ; - bit 2 of battery voltage set?
|
|
221 bcf ex,char_transmitter_low_bat ; - YES - revoke low battery warning
|
|
222 ;bsf ex,char_transmitter_low_bat ; DEBUG CODE TO FAKE A LOW BAT WARNING
|
|
223 retlw .0 ; - return with success code 0
|
|
224 output_pressure_3:
|
|
225 movf POSTINC1,W ; dummy read to advance FSR0 to byte 2 position
|
|
226 output_pressure_4:
|
|
227 dcfsnz up,F ; decrement loop counter, last slot searched?
|
|
228 bra output_pressure_5 ; YES - return with error code
|
|
229 movf POSTINC1,W ; NO - dummy reads to advance FSR0 to byte 3 (any other code would not be more compact...)
|
|
230 movf POSTINC1,W ; - ... byte 4
|
|
231 movf POSTINC1,W ; - ... byte 5
|
|
232 movf POSTINC1,W ; - ... byte 6 = first byte of next slot
|
|
233 bra output_pressure_2 ; - loop
|
|
234 output_pressure_5:
|
|
235 movlw .255 ; return code for transmitter not found
|
|
236 clrf ex ; clear ex (status data)
|
|
237 bsf ex,char_transmitter_lost ; set transmitter status to lost
|
|
238 bra output_pressure_7
|
|
239 output_pressure_6:
|
|
240 clrf ex ; clear ex (status data)
|
|
241 output_pressure_7:
|
|
242 clrf lo ; clear lo (pressure, low byte)
|
|
243 clrf hi ; clear hi (pressure, high byte)
|
|
244 clrf up ; clear up (age of data)
|
|
245 bsf hi,int_not_avail_flag ; set flag for data not available
|
|
246 return ; done
|
|
247
|
|
248
|
623
|
249 ;-----------------------------------------------------------------------------
|
604
|
250 ; Get transmitter ID in given slot
|
|
251 ;
|
|
252 ; input : WREG = slot (0-7)
|
|
253 ; output : hi:lo = transmitter ID
|
|
254 ;
|
|
255 global get_transmitter_id_by_slot
|
|
256 get_transmitter_id_by_slot:
|
|
257 lfsr FSR1,rx_buffer ; load base address of RX buffer
|
|
258 mullw .6 ; multiply WREG with 6 because each slot is 6 bytes in size
|
|
259 movf PRODL,W ; get result into WREG to be used as index (product is 42 at max)
|
|
260 movff PLUSW1,hi ; read transmitter ID high byte
|
|
261 incf WREG,W ; increment index
|
|
262 movff PLUSW1,lo ; read transmitter ID low byte
|
|
263 return
|
|
264
|
|
265
|
623
|
266 ;-----------------------------------------------------------------------------
|
604
|
267 ; Compute average pressure drop from 1st / 2nd reading
|
|
268 ;
|
|
269 ; Memory Map:
|
|
270 ; ------------------------------------------------------------------------------------
|
|
271 ;
|
|
272 ; pres_accu_1st res 4 ; accumulator for pressure drop in 1/(160 * 2^16) bar
|
|
273 ; pres_accu_2nd res 4 ; accumulator for pressure drop in 1/(160 * 2^16) bar
|
|
274 ; time_accu_1st res 1 ; accumulator for reading periods in seconds
|
|
275 ; gas__last_1st res 1 ; last gas assignment
|
|
276 ; time_accu_2nd res 1 ; accumulator for reading periods in seconds
|
|
277 ; gas__last_2nd res 1 ; last gas assignment
|
|
278 ; pres_last_1st res 2 ; last pressure reading pressure in 1/160 bar
|
|
279 ; pres_last_2nd res 2 ; last pressure reading pressure in 1/160 bar
|
|
280 ; time_last_1st res 2 ; last pressure reading time in seconds
|
|
281 ; time_last_2nd res 2 ; last pressure reading time in seconds
|
|
282 ;
|
|
283 ; int_IO_pressure_value [1] res 2 ; current pressure reading in 1/10 bar
|
|
284 ; int_IO_pressure_value [2] res 2 ; current pressure reading in 1/10 bar
|
|
285 ; int_I_pressure_drop [1] res 2 ; calculated average pressure drop in 1/5120 bar/sec
|
|
286 ; int_I_pressure_drop [2] res 2 ; calculated average pressure drop in 1/5120 bar/sec
|
|
287
|
|
288 ; relative positioning of 16 bit ASM variables
|
|
289 #DEFINE offset_FSR1_time_accu .0 ; offset 0 == base address of 1st/2nd
|
|
290 #DEFINE offset_FSR1_gas__last .1 ; offset 1 to base address of 1st/2nd
|
|
291 #DEFINE offset_FSR1_pres_last .4 ; offset 4 ...
|
|
292 #DEFINE offset_FSR1_time_last .8 ; offset 8 ...
|
|
293
|
|
294 ; relative positioning of shared integer variables
|
|
295 #DEFINE offset_FSR2_press_curr .0 ; offset 0 == base address of 1st/2nd
|
|
296 #DEFINE offset_FSR2_press_drop .4 ; offset 4 to base address of 1st/2nd
|
|
297
|
|
298
|
|
299 calc_pres_drop_1st:
|
|
300 ; set up base addresses
|
|
301 lfsr FSR0,pres_accu_1st-1 ; load base address - 1 of pressure accumulator
|
|
302 lfsr FSR1,time_accu_1st ; load base address of other ASM variables
|
|
303 lfsr FSR2,int_IO_pressure_value+0; load base address of the shared variables
|
|
304
|
|
305 ; get the currently assigned gas into lo
|
|
306 movff char_I_pressure_gas+0,lo
|
|
307
|
|
308 ; get the age of the current reading into hi
|
|
309 movff char_I_pressure_age+0,hi
|
|
310
|
|
311 ; continue with common part
|
|
312 bra calc_pres_drop_common
|
|
313
|
|
314 calc_pres_drop_2nd:
|
|
315 ; set up base addresses
|
|
316 lfsr FSR0,pres_accu_2nd-1 ; load base address - 1 of pressure accumulator
|
|
317 lfsr FSR1,time_accu_2nd ; load base address of other ASM variables
|
|
318 lfsr FSR2,int_IO_pressure_value+2; load base address of the shared variables
|
|
319
|
|
320 ; get the currently assigned gas into lo
|
|
321 movff char_I_pressure_gas+1,lo
|
|
322
|
|
323 ; get the age of the current reading into hi
|
|
324 movff char_I_pressure_age+1,hi
|
|
325
|
|
326 calc_pres_drop_common:
|
|
327 ; load the pressure accumulator into xC - FSR0 has been initialized to base address -1
|
|
328 movff PREINC0,xC+0 ; copy pressure accumulator to xC, lowest byte
|
|
329 movff PREINC0,xC+1 ; copy pressure accumulator to xC, second byte
|
|
330 movff PREINC0,xC+2 ; copy pressure accumulator to xC, third byte
|
|
331 movff PREINC0,xC+3 ; copy pressure accumulator to xC, highest byte
|
|
332
|
|
333 ; check if the assigned gas has changed
|
|
334 movlw offset_FSR1_gas__last ; load index of last assigned gas
|
|
335 movf PLUSW1,W ; copy last gas to WREG
|
|
336 cpfseq lo ; is current gas = last gas ?
|
|
337 bra calc_pres_drop_reset ; NO - reset everything
|
|
338
|
|
339 ; check if the pressure reading is activated at all
|
|
340 tstfsz lo ; is there no gas (0=off) assigned to the reading?
|
|
341 bra calc_pres_drop_common_1 ; NO - continue
|
|
342 bra calc_pres_drop_reset ; YES - reset everything
|
|
343
|
|
344 calc_pres_drop_common_1:
|
|
345 ; load the time accumulator into xB
|
|
346 movff INDF1,xB+0 ; load time accumulator, low byte (had been stored)
|
|
347 clrf xB+1 ; clear time accumulator, high byte (will be reset to 0 each round)
|
|
348
|
|
349 ; get the current pressure value into divA - after copying, FSR2 will have been restored to initial address
|
|
350 movff POSTINC2,divA+0 ; copy current pressure value to divA, low byte
|
|
351 movff POSTDEC2,divA+1 ; copy current pressure value to divA, high byte
|
|
352
|
|
353 ; check if the current pressure value is available and not outdated
|
|
354 bsf aux_flag ; set the current pressure value as not available or outdated by default
|
|
355 btfsc divA+1,int_not_avail_flag ; current pressure value not available?
|
|
356 bra calc_pres_drop_common_2 ; YES - skip updating the accumulators with new data
|
|
357 btfsc divA+1,int_outdated_flag ; current pressure outdated?
|
|
358 bra calc_pres_drop_common_2 ; YES - skip updating the accumulators with new data
|
|
359 bcf aux_flag ; NO to both - set current pressure value as available and up-to-date
|
|
360
|
|
361 ; check if a new reading has been received
|
|
362 movlw .2 ; capture new reading at an age of 2 (age 1 sometimes slips through...)
|
|
363 subwf hi,W ; subtract capture-age from reading age, dump result to WREG
|
|
364 bnz calc_pres_drop_common_2 ; result <> 0 ? YES - skip updating the accumulators
|
|
365
|
|
366 ; multiply the current pressure with 16 (pre-scaling), this will also remove the flags
|
|
367 movlw .4 ; multiply with 16 = 2^4
|
|
368 call mult16 ; divA = divA * 2^WREG
|
|
369
|
|
370 ; get the last pressure value and store the current pressure as the new last pressure value
|
|
371 movlw offset_FSR1_pres_last+0 ; load index of last pressure value, low byte
|
|
372 movff PLUSW1,sub_b+0 ; copy last pressure value to sub_b, low byte
|
|
373 movff divA+0,PLUSW1 ; store current pressure value as new last pressure value, low byte
|
|
374 movlw offset_FSR1_pres_last+1 ; load index of last pressure value, high byte
|
|
375 movff PLUSW1,sub_b+1 ; copy last pressure value to sub_b, high byte
|
|
376 movff divA+1,PLUSW1 ; store current pressure value as new last pressure value, high byte
|
|
377
|
|
378 ; add the last pressure value to the pressure accumulator: pres_accu (xC) += pres_last (sub_b) * 2^16
|
|
379 movf sub_b+0,W ; copy pres_last (sub_b) to WREG, low byte
|
|
380 addwf xC+2,F ; add to pres_accu (xC), third byte
|
|
381 movf sub_b+1,W ; copy pres_last (sub_b) to WREG, high byte
|
|
382 addwfc xC+3,F ; add to pres_accu (xC), highest byte
|
|
383
|
|
384 ; subtract the current pressure from the pressure accumulator: pres_accu (xC) -= pres_curr (divA) * 2^16
|
|
385 ; -> effectively, the pressure drop during the last measurement period has been added now
|
|
386 bcf neg_flag ; clear neg_flag by default
|
|
387 movf divA+0,W ; copy pres_curr (divA) to WREG, low byte
|
|
388 subwf xC+2,F ; subtract from pres_accu (xC), third byte
|
|
389 movf divA+1,W ; copy pres_curr (divA) to WREG, high byte
|
|
390 subwfb xC+3,F ; subtract from pres_accu (xC), highest byte
|
|
391 btfss STATUS,C ; did the accumulator under-run (result negative)?
|
|
392 bsf neg_flag ; YES - memorize this
|
|
393
|
623
|
394 ; get the current time into the multi-purpose register
|
|
395 SMOVII total_divetime_secs,mpr ; ISR-safe 2 byte copy of current total dive time into hi:lo
|
604
|
396
|
|
397 ; get the last pressure time and store the current time as the new last pressure time
|
|
398 movlw offset_FSR1_time_last+0 ; load index of last pressure time, low byte
|
|
399 movff PLUSW1,sub_b+0 ; copy last pressure time to sub_b, low byte
|
623
|
400 movff lo,PLUSW1 ; store current time as new last pressure time, low byte
|
604
|
401 movlw offset_FSR1_time_last+1 ; load index of last pressure time, high byte
|
|
402 movff PLUSW1,sub_b+1 ; copy last pressure time to sub_b, high byte
|
623
|
403 movff hi,PLUSW1 ; store current time as new last pressure time, high byte
|
604
|
404
|
|
405 ; did the pressure accumulator under-run before because the current pressure is higher than the accumulator value was?
|
|
406 ; we can not check & abort earlier because the current time needs to be stored along with the current pressure
|
|
407 btfsc neg_flag ; did the pressure accumulator under-run?
|
|
408 bra calc_pres_drop_restart ; YES - reset both accumulators and set average pressure drop to not available
|
|
409
|
623
|
410 ; add the current time to the time accumulator: time_accu (xB) += time_curr (mpr)
|
|
411 movf lo,W ; copy time_curr (mpr) to WREG, low byte
|
|
412 addwf xB+0,F ; add to time_accu (xB), low byte
|
|
413 movf hi,W ; copy time_curr (mpr) to WREG, high byte
|
|
414 addwfc xB+1,F ; add to time_accu (xB), high_byte
|
604
|
415
|
|
416 ; subtract the last pressure time from time accumulator: time_accu (xB) -= time_last (sub_b)
|
|
417 movf sub_b+0,W ; copy time_last (sub_b) to WREG, low byte
|
|
418 subwf xB+0,F ; subtract from time_accu (xB), low byte
|
|
419 movf sub_b+1,W ; copy time_last (sub_b) to WREG, high byte
|
|
420 subwfb xB+1,F ; subtract from time_accu (xB), high byte
|
|
421 btfss STATUS,C ; did the accumulator under-run (result negative) because of a wrap-around of the current time?
|
|
422 bra calc_pres_drop_restart ; YES - reset both accumulators and set average pressure drop to not available
|
|
423
|
|
424 ; check if the time accumulator (xB) is or has become too large
|
|
425 ; this will happen if the last valid pressure reading is older than (256 - time_accu target) seconds
|
|
426 tstfsz xB+1 ; is the time accumulator < 256 [seconds], i.e. high byte = 0 ?
|
|
427 bra calc_pres_drop_restart ; NO - reset both accumulators and set average pressure drop to not available
|
|
428
|
|
429 calc_pres_drop_common_2:
|
|
430 ; check if the time accumulator is or has become zero to avoid a div/0
|
|
431 ; as long as no valid pressure value is available, the time accumulator will stay at 0 and the pressure drop calculation kept in reset
|
|
432 movf xB+0,W ; copy time accumulator low byte to WREG, does it set the zero flag?
|
|
433 bz calc_pres_drop_restart ; YES - reset both accumulators and set average pressure drop to not available
|
|
434
|
|
435 ; duplicate pressure and time accumulators to other variables because xC and xB will get destroyed in div32x16 operation
|
623
|
436 movff xC+0,mpr+0 ; duplicate pres_accu to mpr, lowest byte
|
|
437 movff xC+1,mpr+1 ; duplicate pres_accu to mpr, second byte
|
|
438 movff xC+2,mpr+3 ; duplicate pres_accu to mpr, third byte
|
|
439 movff xC+3,mpr+4 ; duplicate pres_accu to mpr, highest byte
|
604
|
440 movff xB+0,divA+0 ; duplicate time_accu to divA, low byte
|
|
441 movff xB+1,divA+1 ; duplicate time_accu to divA, high byte
|
|
442
|
|
443 ; calculate average pressure drop: pres_drop (xC) = pres_accu (xC) / time_accu (xB)
|
|
444 call div32x16 ; xC = xC / xB, xC is average pressure drop in 1/(160 * 2^16) bar/sec
|
|
445
|
|
446 ; is the time accumulator above target level? (only the low byte needs to be evaluated)?
|
|
447 incf ul,W ; load the target threshold, +1 (incf) transforms cpfslt from < to <= operation
|
|
448 cpfslt divA+0 ; is the time accumulator > target threshold ?
|
|
449 rcall calc_pres_drop_reduce_accus ; YES - do an accumulator reduction
|
|
450
|
|
451 ; do an additional half-rate (every 2nd second) accumulator reduction
|
623
|
452 btfsc timebase_1sec ; are we on an even second?
|
604
|
453 rcall calc_pres_drop_reduce_accus ; YES - do an additional accumulator reduction
|
|
454
|
623
|
455 ; store pressure accumulator (mpr:4) - FSR0 was left pointing to address of highest byte
|
|
456 movff mpr+3,POSTDEC0 ; store pressure accumulator, highest byte
|
|
457 movff mpr+2,POSTDEC0 ; store pressure accumulator, third byte
|
|
458 movff mpr+1,POSTDEC0 ; store pressure accumulator, second byte
|
|
459 movff mpr+0,POSTDEC0 ; store pressure accumulator, lowest byte
|
604
|
460
|
|
461 ; store the time accumulator
|
|
462 movff divA+0,INDF1 ; store time accumulator (only the low byte will be stored)
|
|
463
|
|
464 ; check if the average pressure drop for transfer to p2deco needs to be limited
|
|
465 tstfsz xC+3 ; check if the highest byte is all zero
|
|
466 bra calc_pres_drop_limit ; NO - limit output
|
|
467 btfsc xC+2,7 ; check if the highest bit of the third byte is zero
|
|
468 bra calc_pres_drop_limit ; NO - limit output
|
|
469
|
|
470 ; pick and adjust the bytes from pres_drop for transfer to p2deco
|
|
471 movff xC+1,divA+0 ; pick second byte of average pressure drop for transfer to p2deco as low byte
|
|
472 movff xC+2,divA+1 ; pick third byte of average pressure drop for transfer to p2deco as high byte
|
|
473 movlw .3 ; divide by 8 = 2^3
|
|
474 call div16 ; divA = divA / 2^WREG
|
|
475 bra calc_pres_drop_common_3
|
|
476
|
|
477 calc_pres_drop_limit:
|
|
478 ; limit output to 0x0FFF
|
623
|
479 MOVLI 0x0FFF,divA ; set output to 0x0FFF
|
604
|
480 bsf divA+1,int_warning_flag ; set warning flag indicating out-of-range
|
|
481
|
|
482 calc_pres_drop_common_3:
|
|
483 ; set the average pressure drop as not available if the last pressure reading is outdated or not available
|
|
484 btfsc aux_flag ; is the last pressure reading outdated or not available?
|
|
485 bsf divA+1,int_not_avail_flag ; YES - set pressure drop to not available, too
|
|
486
|
|
487 ; set the average pressure drop as outdated if time_accu (divA) < (target threshold / 2)
|
|
488 rrncf ul,W ; load time accumulator target value / 2 into WREG
|
|
489 decf WREG,W ; subtract 1 to transform cpfsgt from > to >= operation
|
|
490 cpfsgt INDF1 ; time accumulator < (target threshold / 2) ?
|
|
491 bsf divA+1,int_outdated_flag ; YES - set outdated flag
|
|
492
|
|
493 ; write average pressure drop to p2deco interface
|
|
494 movlw offset_FSR2_press_drop+0 ; load index of average pressure drop, low byte
|
|
495 movff divA+0,PLUSW2 ; store average pressure drop, low byte
|
|
496 movlw offset_FSR2_press_drop+1 ; load index of average pressure drop, high byte
|
|
497 movff divA+1,PLUSW2 ; store average pressure drop, high byte
|
|
498
|
|
499 ; done
|
|
500 return
|
|
501
|
|
502
|
|
503 calc_pres_drop_reduce_accus:
|
|
504 ; subtract 1 second from the time accumulator: time_accu (divA) -= 1 (only the low byte needs to be processed)
|
|
505 decf divA+0,F ; decrement low byte of time_accu
|
|
506
|
623
|
507 ; subtract average pressure drop per second from pressure accumulator: press_accu (mpr) -= press_drop (xC)
|
604
|
508 movf xC+0,W ; copy press_drop(xC) to WREG, lowest byte
|
623
|
509 subwf mpr+0,F ; subtract from pres_accu, lowest byte
|
604
|
510 movf xC+1,W ; copy press_drop(xC) to WREG, second byte
|
623
|
511 subwfb mpr+1,F ; subtract from pres_accu, second byte
|
604
|
512 movf xC+2,W ; copy press_drop(xC) to WREG, third byte
|
623
|
513 subwfb mpr+2,F ; subtract from pres_accu, third byte
|
604
|
514 movf xC+3,W ; copy press_drop(xC) to WREG, highest byte
|
623
|
515 subwfb mpr+3,F ; subtract from pres_accu, highest byte
|
604
|
516 btfsc STATUS,C ; did the buffer under-run (result negative)?
|
|
517 return ; NO - done
|
623
|
518 clrf mpr+0 ; YES - clear pressure accumulator, lowest byte
|
|
519 clrf mpr+1 ; - clear pressure accumulator, second byte
|
|
520 clrf mpr+2 ; - clear pressure accumulator, third byte
|
|
521 clrf mpr+3 ; - clear pressure accumulator, highest byte
|
604
|
522 return ; - done
|
|
523
|
|
524 calc_pres_drop_reset:
|
|
525 ; store the current gas as the last gas
|
|
526 movlw offset_FSR1_gas__last ; load index of last gas
|
|
527 movff lo,PLUSW1 ; store current gas as new last gas
|
|
528
|
|
529 ; clear last pressure value
|
|
530 movlw offset_FSR1_pres_last+0 ; load index of last pressure value, low byte
|
|
531 clrf PLUSW1 ; clear last pressure value, low byte
|
|
532 movlw offset_FSR1_pres_last+1 ; load index of last pressure value, high byte
|
|
533 clrf PLUSW1 ; clear last pressure value, high byte
|
|
534
|
|
535 ; clear last pressure time
|
|
536 movlw offset_FSR1_time_last+0 ; load index of last pressure time, low byte
|
|
537 clrf PLUSW1 ; clear last pressure time, low byte
|
|
538 movlw offset_FSR1_time_last+1 ; load index of last pressure time, high byte
|
|
539 clrf PLUSW1 ; clear last pressure time, high byte
|
|
540
|
|
541 calc_pres_drop_restart:
|
|
542 ; clear pressure accumulator - FSR0 was left pointing to address of highest byte
|
|
543 clrf POSTDEC0 ; clear pressure accumulator, highest byte
|
|
544 clrf POSTDEC0 ; clear pressure accumulator, third byte
|
|
545 clrf POSTDEC0 ; clear pressure accumulator, second byte
|
|
546 clrf POSTDEC0 ; clear pressure accumulator, lowest byte
|
|
547
|
|
548 ; clear time accumulator
|
|
549 clrf INDF1 ; clear time accumulator
|
|
550
|
|
551 ; clear pressure drop and set it to not available
|
|
552 movlw offset_FSR2_press_drop+0 ; load index of average pressure drop, low byte
|
|
553 clrf PLUSW2 ; clear average pressure drop, low byte
|
|
554 movlw offset_FSR2_press_drop+1 ; load index of average pressure drop, high byte
|
|
555 clrf PLUSW2 ; clear average pressure drop, high byte
|
|
556 bsf PLUSW2,int_not_avail_flag ; set flag for data not available
|
|
557
|
|
558 return ; done
|
|
559
|
|
560
|
623
|
561 ;-----------------------------------------------------------------------------
|
604
|
562 ; set up SAC calculation dependent on TR mode
|
|
563 ;
|
|
564 global configure_sac_calculation
|
|
565 configure_sac_calculation:
|
|
566 movlw time_accu_target_OC ; load time accumulator target value for OC as default
|
|
567 movwf ul ; store it in ul
|
|
568 movff opt_TR_mode,WREG ; get TR mode
|
|
569 dcfsnz WREG,W ; TR mode = 1 (on)?
|
|
570 bra configure_sac_calculation_1 ; YES
|
|
571 dcfsnz WREG,W ; TR mode = 2 (ind.double)?
|
|
572 bra configure_sac_calculation_2 ; YES
|
|
573 dcfsnz WREG,W ; TR mode = 3 (CCR Dil+O2)?
|
|
574 bra configure_sac_calculation_3 ; YES
|
|
575 bra configure_sac_calculation_4 ; NO to all - was disabled then (or invalid)
|
|
576
|
|
577 configure_sac_calculation_1: ; TR mode 1: calculate SAC on 1st reading
|
|
578 rcall calc_pres_drop_1st ; calculate pressure drop on 1st reading
|
|
579 movlw .1 ; select SAC mode 1: SAC on 1st reading
|
|
580 bra configure_sac_calculation_5 ; goto exit
|
|
581
|
|
582 configure_sac_calculation_2: ; TR mode 2: independent double
|
|
583 rcall calc_pres_drop_1st ; calculate pressure drop on 1st reading
|
|
584 rcall calc_pres_drop_2nd ; calculate pressure drop on 2nd reading
|
|
585 movlw .3 ; select SAC mode 3: SAC on higher of both readings
|
|
586 bra configure_sac_calculation_5 ; goto exit
|
|
587
|
|
588 configure_sac_calculation_3: ; TR mode 3: CCR Dil+O2
|
623
|
589 btfsc bailout_mode ; in bailout?
|
604
|
590 bra configure_sac_calculation_1 ; YES - handle alike TR mode 1
|
|
591 movlw time_accu_target_CCR ; load time accumulator target value for CCR mode
|
|
592 movwf ul ; store it in ul
|
|
593 rcall calc_pres_drop_2nd ; calculate pressure drop on 2nd reading
|
|
594 movlw .4 ; select SAC mode 4: SAC on 2nd reading, O2 usage
|
|
595 bra configure_sac_calculation_5 ; goto exit
|
|
596
|
|
597 configure_sac_calculation_4:
|
|
598 clrf WREG ; select SAC mode 0 (disabled)
|
|
599
|
|
600 configure_sac_calculation_5:
|
|
601 movff WREG,char_I_SAC_mode ; write SAC mode selection
|
|
602 return
|
|
603
|
623
|
604
|
|
605 ;-----------------------------------------------------------------------------
|
|
606 ; Update TR module
|
|
607 ;
|
|
608 global update_tr_module
|
|
609 update_tr_module:
|
|
610 movlw LOW rx_firmware_storage ; setup program memory read for embedded TR firmware
|
|
611 movwf TBLPTRL
|
|
612 movlw HIGH rx_firmware_storage
|
|
613 movwf TBLPTRH
|
|
614 movlw UPPER rx_firmware_storage
|
|
615 movwf TBLPTRU
|
|
616
|
|
617 bsf active_reset_ostc_rx ; apply a reset to the RX co-processor
|
|
618 WAITMS .200 ; wait 200 ms
|
|
619 bcf active_reset_ostc_rx ; release reset
|
|
620 WAITMS .100 ; wait 100 ms, the RX co-processor will be in bootloader stage by then
|
|
621
|
|
622 bcf INTCON,GIE ; halt all interrupts
|
|
623
|
|
624 movlw .64 ; load loop counter
|
|
625 movwf lo ; ...
|
|
626
|
|
627 update_tr_module_loop: ; (loop 64 times)
|
|
628 call I2C_update_OSTC_rx ; send a batch of 64 bytes to the RX co-processor
|
|
629 tstfsz WREG ; WREG = 0, i.e. data sent successfully?
|
|
630 bra update_tr_module_fail ; NO - transfer error, abort
|
|
631 decfsz lo,F ; YES - decrement loop counter, became zero?
|
|
632 bra update_tr_module_loop ; NO - loop
|
|
633 bra update_tr_module_done ; YES - transfer complete & successful
|
|
634
|
|
635 update_tr_module_fail:
|
|
636 bsf INTCON,GIE ; re-enable interrupts
|
|
637 bcf ostc_rx_present ; flag TR module as unserviceable
|
|
638 return ; done
|
|
639
|
|
640 update_tr_module_done:
|
|
641 bsf INTCON,GIE ; re-enable interrupts
|
|
642
|
|
643 call wait_1s ; wait (up to) 1 second
|
|
644 call wait_1s ; wait (another full) 1 second
|
|
645
|
|
646 bsf active_reset_ostc_rx ; apply a reset to the RX co-processor
|
|
647 WAITMS .200 ; wait 200 ms
|
|
648 bcf active_reset_ostc_rx ; release reset
|
|
649 call wait_1s ; wait (up to) 1 second
|
|
650 call wait_1s ; wait (another full) 1 second
|
|
651 call wait_1s ; wait (another full) 1 second
|
|
652
|
|
653 call I2C_probe_OSTC_rx ; check if RX co-processor is alive, will set ostc_rx_present if so (1st try)
|
|
654 btfsc ostc_rx_present ; RX co-processor up & running?
|
|
655 return ; YES
|
|
656 call wait_1s ; NO - wait (up to) 1 second
|
|
657 call I2C_probe_OSTC_rx ; - give it a 2nd try
|
|
658 return ; - finally done (whatever result on 2nd tray)
|
|
659
|
|
660
|
604
|
661 ;=============================================================================
|
|
662
|
623
|
663 ENDIF ; _rx_functions
|
604
|
664
|
623
|
665 END
|