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