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