comparison src/ms5541.asm @ 646:5b7fe7777425

3.16 release
author heinrichs weikamp
date Thu, 14 Oct 2021 12:03:24 +0200
parents 4050675965ea
children 357341239438
comparison
equal deleted inserted replaced
642:a9a0188091e4 646:5b7fe7777425
9 ; HISTORY 9 ; HISTORY
10 ; 2011-08-03 : [mH] moving from OSTC code 10 ; 2011-08-03 : [mH] moving from OSTC code
11 11
12 #include "hwos.inc" ; Mandatory header 12 #include "hwos.inc" ; Mandatory header
13 #include "math.inc" ; Math routines 13 #include "math.inc" ; Math routines
14 14 #include "i2c.inc"
15 #include "shared_definitions.h" ; mailbox from/to p2_deco.c
15 16
16 ms5541 CODE 17 ms5541 CODE
17 18
18 19
19 ; Expose internal variables to ease debug 20 ; Expose internal variables to ease debug
28 ; called from ISR and from sleep mode, returns in bank isr_data 29 ; called from ISR and from sleep mode, returns in bank isr_data
29 ; 30 ;
30 global calculate_compensation 31 global calculate_compensation
31 calculate_compensation: 32 calculate_compensation:
32 banksel isr_backup ; select bank ISR data 33 banksel isr_backup ; select bank ISR data
34
35 btfsc press_sensor_type ; New sensor found?
36 bra press_comp_ms5837 ; Yes, use MS5837
33 37
34 ;---- pressure sensor compensation 38 ;---- pressure sensor compensation
35 39
36 ; xdT = D2 - C5 (s16 range -11.400 .. +12.350) 40 ; xdT = D2 - C5 (s16 range -11.400 .. +12.350)
37 movf C5+0,W ; get value to be subtracted 41 movf C5+0,W ; get value to be subtracted
129 subwfb D1+1,W ; ... 133 subwfb D1+1,W ; ...
130 movwf isr_xA+1 ; ... 134 movwf isr_xA+1 ; ...
131 135
132 MOVII SENS,isr_xB ; sens --> B 136 MOVII SENS,isr_xB ; sens --> B
133 call isr_signed_mult16x16 ; C = A*B 137 call isr_signed_mult16x16 ; C = A*B
134 movlw .13 ; 12 * 256 = 3328 138 movlw .15 ; 15 * 256 = 3840
135 cpfslt C1+1 ; C1 > 3328 ? 139 cpfslt C1+1 ; C1 > 3328 ?
136 bra isr_shift_ms5541_30 ; YES - MS5541-30 140 bra isr_shift_ms5541_30 ; YES - MS5541-30
137 movlw .12-.8 ; NO - MS5541: 12 bit shift = 1 byte + 4 bits 141 movlw .12-.8 ; NO - MS5541: 12 bit shift = 1 byte + 4 bits
138 bra isr_shift_ms5541_common ; - continue 142 bra isr_shift_ms5541_common ; - continue
139 isr_shift_ms5541_30: 143 isr_shift_ms5541_30:
142 call isr_shift_C31 ; special shift 146 call isr_shift_C31 ; special shift
143 movlw LOW .1000 ; add 1000 147 movlw LOW .1000 ; add 1000
144 addwf isr_xC+1,F ; ... 148 addwf isr_xC+1,F ; ...
145 movlw HIGH .1000 ; ... 149 movlw HIGH .1000 ; ...
146 addwfc isr_xC+2,F ; ... 150 addwfc isr_xC+2,F ; ...
147 151 bra press_comp_done_common
152
153 press_comp_ms5837:
154 ; xdT = D2 - C5*2^8
155 movlw .0 ; Low
156 subwf D2+0,W
157 movwf xdT+0
158 movf C5+0,W ; High
159 subwfb D2+1,W
160 movwf xdT+1
161 movf C5+1,W ; Upper
162 subwfb D2+2,W
163 movwf xdT+2
164 ; xdT equals D2 - (C5*2^8)
165
166 ; Calculate OFF = C2*2^16+(C4*xdT)/2^7
167 movff C4+0,isr_xA+0
168 movff C4+1,isr_xA+1
169 clrf isr_xA+2
170 movff xdT+0,isr_xB+0
171 movff xdT+1,isr_xB+1
172 movff xdT+2,isr_xB+2
173 call isr_signed_mult24x24 ; C = A*B
174 ; isr_xC:6 equals (C4*xdT)
175 rlcf isr_xC+0,F
176 rlcf isr_xC+1,F
177 rlcf isr_xC+2,F
178 rlcf isr_xC+3,F
179 rlcf isr_xC+4,F
180 rlcf isr_xC+5,F
181 movff isr_xC+1,OFF+0
182 movff isr_xC+2,OFF+1
183 movff isr_xC+3,OFF+2
184 movff isr_xC+4,OFF+3
185 ; OFF equals (C4*xdT)/2^7
186 movf C2+0,W
187 addwf OFF+2,F
188 movf C2+1,W
189 addwfc OFF+3,F
190 ; OFF equals C2*2^16+((C4*xdT)/2^7)
191
192 ; Calculate SENS = C1*2^15 + (C3*xdT)/2^8
193 MOVII C1,isr_xA
194 movlw LOW (.32768)
195 movwf isr_xB+0
196 movlw HIGH (.32768)
197 movwf isr_xB+1
198 call isr_unsigned_mult16x16 ; C = A*B
199 movff isr_xC+0,SENS+0
200 movff isr_xC+1,SENS+1
201 movff isr_xC+2,SENS+2
202 movff isr_xC+3,SENS+3 ; 32bit copy
203 ; SENS equals C1*2^15
204 movff C3+0,isr_xA+0
205 movff C3+1,isr_xA+1
206 clrf isr_xA+2
207 movff xdT+0,isr_xB+0
208 movff xdT+1,isr_xB+1
209 movff xdT+2,isr_xB+2
210 call isr_signed_mult24x24 ; C = A*B
211 ; isr_xC:6 equals (C3*xdT)
212 movf isr_xC+1,W
213 addwf SENS+0,F
214 movf isr_xC+2,W
215 addwfc SENS+1,F
216 movf isr_xC+3,W
217 addwfc SENS+2,F
218 movf isr_xC+4,W
219 addwfc SENS+3,F
220 ; SENS equals C1*2^15 + (C3*xdT)/2^8
221
222 ; Calculate PRESSURE = ((D1 * SENS) / 2^21) - OFF) / 2^13
223 movff SENS+0,isr_xA+0
224 movff SENS+1,isr_xA+1
225 movff SENS+2,isr_xA+2
226 movff SENS+3,isr_xA+3
227 movff D1+0,isr_xB+0
228 movff D1+1,isr_xB+1
229 movff D1+2,isr_xB+2
230 call isr_unsigned_mult32x24 ; C = A*B
231 ; isr_xC:7 equals D1*SENS
232
233 ; devide isr_xC:7 / 2^21 -> shift right 21 times
234 movlw .21
235 press_comp_ms5837_2:
236 bcf STATUS,C ; clear carry, needed for MS5837 use
237 rrcf isr_xC+6,F
238 rrcf isr_xC+5,F
239 rrcf isr_xC+4,F
240 rrcf isr_xC+3,F
241 rrcf isr_xC+2,F
242 rrcf isr_xC+1,F
243 rrcf isr_xC+0,F
244 decfsz WREG ; decrement loop counter, done?
245 bra press_comp_ms5837_2 ; NO - loop
246 ; isr_xC:6 equals (D1*SENS)/2^21
247
248 ; Calculate isr_xC:6-OFF
249 movf OFF+0,W ; Low
250 subwf isr_xC+0,F
251 movf OFF+1,W ; High
252 subwfb isr_xC+1,F
253 movf OFF+2,W ; Upper
254 subwfb isr_xC+2,F
255 movf OFF+3,W ; Extra
256 subwfb isr_xC+3,F
257 movlw .0 ; ultra
258 subwfb isr_xC+4,F
259 movlw .0 ; Hyper
260 subwfb isr_xC+5,F
261 ; isr_xC:5 is now isr_xC:4-OFF
262
263 ; devide by 2^13
264 ; devide isr_xC:6 / 2^13 -> shift right 13 times
265 movlw .13
266 press_comp_ms5837_3:
267 bcf STATUS,C ; clear carry
268 rrcf isr_xC+5,F
269 rrcf isr_xC+4,F
270 rrcf isr_xC+3,F
271 rrcf isr_xC+2,F
272 rrcf isr_xC+1,F
273 rrcf isr_xC+0,F
274 decfsz WREG ; decrement loop counter, done?
275 bra press_comp_ms5837_3 ; NO - loop
276 ; isr_xC:4 equals pressure in .1 mbar
277
278 movlw .10
279 movwf isr_xB+0
280 clrf isr_xB+1
281 call isr_div32x16 ; isr_xC:4 = isr_xC:4 / isr_xB:2 with isr_xA as remainder
282 ; isr_xC:2 equals pressure in 1mbar
283
284 ; copy for compatibility of the next routines
285 movff isr_xC+1,isr_xC+2
286 movff isr_xC+0,isr_xC+1
287
288 press_comp_done_common: ; continue here even with MS5837
148 ; add opt_pressure_adjust to result (SIGNED!) 289 ; add opt_pressure_adjust to result (SIGNED!)
149 clrf isr_xC+0 ; prepare high byte for adjustment 290 clrf isr_xC+0 ; prepare high byte for adjustment
150 movff opt_pressure_adjust,WREG ; get low byte for adjustment (signed) 291 movff opt_pressure_adjust,WREG ; get low byte for adjustment (signed)
151 movf WREG,W ; excite flags 292 movf WREG,W ; excite flags
152 bz calc_compensation_1 ; opt_pressure_adjust = 0 -> skip adjustment 293 bz calc_compensation_1 ; opt_pressure_adjust = 0 -> skip adjustment
254 ; add current absolute pressure to averaging buffer 395 ; add current absolute pressure to averaging buffer
255 movf isr_xC+1,W ; copy current absolute pressure to WREG, low byte 396 movf isr_xC+1,W ; copy current absolute pressure to WREG, low byte
256 addwf pressure_abs_avg+0,F ; pressure_abs_avg += current pressure, low byte 397 addwf pressure_abs_avg+0,F ; pressure_abs_avg += current pressure, low byte
257 movf isr_xC+2,W ; copy current absolute pressure to WREG, high byte 398 movf isr_xC+2,W ; copy current absolute pressure to WREG, high byte
258 addwfc pressure_abs_avg+1,F ; pressure_abs_avg += current pressure, high byte 399 addwfc pressure_abs_avg+1,F ; pressure_abs_avg += current pressure, high byte
400 movlw .0
401 addwfc pressure_abs_avg+2,F ; pressure_abs_avg += current pressure, upper byte
259 402
260 ;---- temperature sensor compensation 403 ;---- temperature sensor compensation
261 404
405 btfsc press_sensor_type ; New sensor found?
406 bra temp_comp_ms5837 ; Yes, use MS5837
407
262 ; calculate temp = 200 + dT*(C6+100)/2^11 408 ; calculate temp = 200 + dT*(C6+100)/2^11
263 movlw LOW(.100) ; C6 + 100 --> A 409 movlw LOW(.100) ; C6 + 100 --> A
264 addwf C6+0,W ; ... 410 addwf C6+0,W ; ...
265 movwf isr_xA+0 ; ... 411 movwf isr_xA+0 ; ...
266 movlw HIGH(.100) ; ... 412 movlw HIGH(.100) ; ...
274 420
275 movlw LOW(.200) ; add 200 421 movlw LOW(.200) ; add 200
276 addwf isr_xC+1,F ; ... 422 addwf isr_xC+1,F ; ...
277 movlw HIGH(.200) ; ... 423 movlw HIGH(.200) ; ...
278 addwfc isr_xC+2,F ; ... 424 addwfc isr_xC+2,F ; ...
279 425 bra temp_comp_done_common
426
427 temp_comp_ms5837:
428 ; TEMP = 2000+((xdT * C6) / 2^23)
429
430 ; xdT * C6 -> isr_xC:6
431 movff C6+0,isr_xA+0
432 movff C6+1,isr_xA+1
433 clrf isr_xA+2
434 movff xdT+0,isr_xB+0
435 movff xdT+1,isr_xB+1
436 movff xdT+2,isr_xB+2
437 call isr_signed_mult24x24 ; A*B
438
439 ; devide isr_xC:6 / 2^23 -> shift right 23 times
440 ; a special variant (again): We loose isr_xC+0 and isr_xC+1 anyway
441 ; and do not care about the inserted carry since we won't use isr_xC+5
442 ; and we shift less then 8 bytes
443 movlw .23-.16
444 temp_comp_ms5837_2:
445 rrcf isr_xC+5,F ;
446 rrcf isr_xC+4,F ;
447 rrcf isr_xC+3,F ;
448 rrcf isr_xC+2,F ; ...
449 decfsz WREG ; decrement loop counter, done?
450 bra temp_comp_ms5837_2 ; NO - loop
451
452 ; only use +2 and +3
453 movlw LOW(.2000) ; add 2000
454 addwf isr_xC+2,F ; ...
455 movlw HIGH(.2000) ; ...
456 addwfc isr_xC+3,F ; ...
457
458 ; devide by 10 for .1°C resolution
459 movlw .10
460 movwf isr_xB+0
461 clrf isr_xB+1
462 movff isr_xC+2,isr_xA+0
463 movff isr_xC+3,isr_xA+1
464
465 btfss isr_xC+3,7 ; check sign bit
466 bra temp_comp_ms5837_3 ; not negative...
467 ; negate isr_xA:2 to make it positive
468 comf isr_xA+1 ; 16 bit sign change
469 negf isr_xA+0
470 btfsc STATUS,C ; - carry to propagate?
471 incf isr_xA+1,F ; YES - do it
472
473 temp_comp_ms5837_3:
474 call isr_div16x16 ; isr_xC:2 = isr_xA:2 / isr_xB:2
475
476 btfss isr_xC+3,7 ; check sign bit again
477 bra temp_comp_ms5837_4 ; not negative...
478 ; negate result isr_xC:2 to make it negative again
479 comf isr_xC+1 ; 16 bit sign change
480 negf isr_xC+0
481 btfsc STATUS,C ;- carry to propagate?
482 incf isr_xC+1,F ; YES - do it
483
484 temp_comp_ms5837_4:
485 ; copy for compatibility of the next routines
486 movff isr_xC+1,isr_xC+2
487 movff isr_xC+0,isr_xC+1
488
489 temp_comp_done_common: ; continue here even with MS5837
280 ; add opt_temperature_adjust to result (SIGNED!) 490 ; add opt_temperature_adjust to result (SIGNED!)
281 clrf isr_xC+0 ; prepare high byte for adjustment 491 clrf isr_xC+0 ; prepare high byte for adjustment
282 movff opt_temperature_adjust,WREG ; get low byte for adjustment (signed) 492 movff opt_temperature_adjust,WREG ; get low byte for adjustment (signed)
283 movf WREG,W ; excite flags 493 movf WREG,W ; excite flags
284 bz calc_temperature_add_avg ; opt_temperature_adjust = 0 -> skip adjustment 494 bz calc_temperature_add_avg ; opt_temperature_adjust = 0 -> skip adjustment
307 ; 517 ;
308 ; called from ISR and sleep mode, needs to be called in bank isr_backup 518 ; called from ISR and sleep mode, needs to be called in bank isr_backup
309 ; 519 ;
310 global get_pressure_start 520 global get_pressure_start
311 get_pressure_start: 521 get_pressure_start:
522 btfsc press_sensor_type ; New sensor found?
523 return ; Yes, ignore routine
312 rcall reset_MS5541 ; reset the chip 524 rcall reset_MS5541 ; reset the chip
313 movlw b'10100000' ; +3*high as start and 1+low as stop 525 movlw b'10100000' ; +3*high as start and 1+low as stop
314 movwf dbuffer ; .... 526 movwf dbuffer ; ....
315 movlw d'12' ; send start command 527 movlw d'12' ; send start command
316 rcall send_data_MS5541 ; ... 528 bra send_data_MS5541 ; ... (And return)
317 return ; done
318 529
319 530
320 ;----------------------------------------------------------------------------- 531 ;-----------------------------------------------------------------------------
321 ; Read Pressure Measurement Result 532 ; Read Pressure Measurement Result
322 ; 533 ;
323 ; called from ISR and sleep mode, needs to be called in bank isr_backup 534 ; called from ISR and sleep mode, needs to be called in bank isr_backup
324 ; 535 ;
325 global get_pressure_value 536 global get_pressure_value
326 get_pressure_value: 537 get_pressure_value:
538 btfsc press_sensor_type ; New sensor found?
539 bra get_pressure_value2
327 btfsc MS5541_miso ; conversion done? 540 btfsc MS5541_miso ; conversion done?
328 return ; NO - abort 541 return ; NO - abort
329 rcall get_2bytes_MS5541 ; YES - read result 542 rcall get_2bytes_MS5541 ; YES - read result
330 movff dMSB,D1+1 ; copy result to D1, high byte first 543 movff dMSB,D1+1 ; copy result to D1, high byte first
331 movff dLSB,D1+0 ; ... low byte second 544 movff dLSB,D1+0 ; ... low byte second
332 return ; done 545 return ; done
333 546
547 get_pressure_value2:
548 btfsc i2c_busy_pressure ; currently updating pressure?
549 return ; Yes, skip update...
550 movff D1_buffer+0,D1+0
551 movff D1_buffer+1,D1+1
552 movff D1_buffer+2,D1+2
553 return
554
334 555
335 ;----------------------------------------------------------------------------- 556 ;-----------------------------------------------------------------------------
336 ; Start Temperature Measurement 557 ; Start Temperature Measurement
337 ; 558 ;
338 ; called from ISR and sleep mode, needs to be called in bank isr_backup 559 ; called from ISR and sleep mode, needs to be called in bank isr_backup
339 ; 560 ;
340 global get_temperature_start 561 global get_temperature_start
341 get_temperature_start: 562 get_temperature_start:
563 btfsc press_sensor_type ; New sensor found?
564 return ; Yes, ignore routine
342 rcall reset_MS5541 ; reset chip 565 rcall reset_MS5541 ; reset chip
343 movlw b'10010000' ; +3*high as start and 1+low as stop 566 movlw b'10010000' ; +3*high as start and 1+low as stop
344 movwf dbuffer ; ... 567 movwf dbuffer ; ...
345 movlw d'12' ; send start command 568 movlw d'12' ; send start command
346 rcall send_data_MS5541 ; ... 569 bra send_data_MS5541 ; ... (And return)
347 return ; done
348 570
349 571
350 ;----------------------------------------------------------------------------- 572 ;-----------------------------------------------------------------------------
351 ; Read Pressure Measurement Result 573 ; Read Pressure Measurement Result
352 ; 574 ;
353 ; called from ISR and sleep mode, needs to be called in bank isr_backup 575 ; called from ISR and sleep mode, needs to be called in bank isr_backup
354 ; 576 ;
355 global get_temperature_value 577 global get_temperature_value
356 get_temperature_value: 578 get_temperature_value:
579 btfsc press_sensor_type ; New sensor found?
580 bra get_temperature_value2
357 btfsc MS5541_miso ; conversion done? 581 btfsc MS5541_miso ; conversion done?
358 return ; NO - done 582 return ; NO - done
359 rcall get_2bytes_MS5541 ; YES - read result 583 rcall get_2bytes_MS5541 ; YES - read result
360 movff dMSB,D2+1 ; copy result to D2, high byte first 584 movff dMSB,D2+1 ; copy result to D2, high byte first
361 movff dLSB,D2+0 ; ... low byte second 585 movff dLSB,D2+0 ; ... low byte second
362 return ; done 586 return ; done
363 587
588 get_temperature_value2:
589 btfsc i2c_busy_temperature ; currently updating temperature?
590 return ; Yes, skip update...
591 movff D2_buffer+0,D2+0
592 movff D2_buffer+1,D2+1
593 movff D2_buffer+2,D2+2
594 return
595
364 596
365 ;----------------------------------------------------------------------------- 597 ;-----------------------------------------------------------------------------
366 ; Retrieve Calibration Data 598 ; Retrieve Calibration Data
367 ; 599 ;
368 ; called by start, returns in bank common 600 ; called by start, returns in bank common
369 ; 601 ;
370 global get_calibration_data 602 global get_calibration_data
371 get_calibration_data: 603 get_calibration_data:
372 banksel isr_backup ; select bank ISR data 604 banksel isr_backup ; select bank ISR data
373 bsf block_sensor_interrupt ; disable sensor interrupts 605 bsf block_sensor_interrupt ; disable sensor interrupts
374 606
607 btfsc press_sensor_type ; New sensor found?
608 call I2C_get_calib_MS5837 ; Yes, read C1 to C6
609 banksel isr_backup ; select bank ISR data
610 ; double-check sensor type
611 movlw 0xED
612 cpfseq C1+0
613 bra get_calibration_data1 ; Not 0xED (I2C address false reading)
614 movlw 0xED
615 cpfseq C1+1
616 bra get_calibration_data1 ; Not 0xED (I2C address false reading)
617 movlw 0xED
618 cpfseq C5+0
619 bra get_calibration_data1 ; Not 0xED (I2C address false reading)
620 movlw 0xED
621 cpfseq C5+1
622 bra get_calibration_data1 ; Not 0xED (I2C address false reading)
623
624 ; C1 and C5 are 0xEDED -> NOT Sensor 1
625 bcf press_sensor_type
626
627 get_calibration_data1:
628 btfsc press_sensor_type ; New sensor found?
629 bra get_calibration_data2 ; Yes, skip to the end
630
375 rcall reset_MS5541 ; reset chip 631 rcall reset_MS5541 ; reset chip
376 632
377 movlw b'01010100' ; +3*high as start and 1+low as stop 633 movlw b'01010100' ; +3*high as start and 1+low as stop
378 movwf dbuffer ; ... 634 movwf dbuffer ; ...
379 movlw d'13' ; send command 635 movlw d'13' ; send command
543 ; calculate C6 (16Bit) 799 ; calculate C6 (16Bit)
544 clrf C6+1 800 clrf C6+1
545 movff ir_s8_buffer+6,C6+0 801 movff ir_s8_buffer+6,C6+0
546 bcf C6+0,7 802 bcf C6+0,7
547 803
804 get_calibration_data2:
548 clrf sensor_state_counter ; reset state counter 805 clrf sensor_state_counter ; reset state counter
549 bcf block_sensor_interrupt ; re-enable sensor interrupts 806 bcf block_sensor_interrupt ; re-enable sensor interrupts
550 banksel common ; back to bank common 807 banksel common ; back to bank common
551 return ; done 808 return ; done
552 809