comparison code_part1/OSTC_code_c_part2/p2_deco.c @ 184:016c45a0caaf

Enable to compile p2_deco.c on other platforms.
author JeanDo
date Sun, 06 Feb 2011 19:32:11 +0100
parents 7f3e1bf588bc
children ae3a83982ab2
comparison
equal deleted inserted replaced
183:f720ce017244 184:016c45a0caaf
120 // *********************************************** 120 // ***********************************************
121 // ** V A R I A B L E S D E F I N I T I O N S ** 121 // ** V A R I A B L E S D E F I N I T I O N S **
122 // *********************************************** 122 // ***********************************************
123 123
124 #include "p2_definitions.h" 124 #include "p2_definitions.h"
125 #include "../OSTC_code_c_part2/shared_definitions.h" 125 #include "shared_definitions.h"
126 126
127 //---- Bank 3 parameters ----------------------------------------------------- 127 //---- Bank 3 parameters -----------------------------------------------------
128 #pragma udata bank4=0x400 128 #pragma udata bank4=0x400
129 129
130 static unsigned char lock_GF_depth_list; 130 static unsigned char lock_GF_depth_list;
405 && int_I_pres_respiration > 3000 // And we hit the 20m limit ? 405 && int_I_pres_respiration > 3000 // And we hit the 20m limit ?
406 ) 406 )
407 { 407 {
408 char_O_NDL_at_20mtr = char_O_nullzeit; // change to max bottom time. 408 char_O_NDL_at_20mtr = char_O_nullzeit; // change to max bottom time.
409 if( char_O_NDL_at_20mtr == 255) // and avoid confusion. 409 if( char_O_NDL_at_20mtr == 255) // and avoid confusion.
410 char_O_NDL_at_20mtr == 254; 410 char_O_NDL_at_20mtr = 254;
411 } 411 }
412 } 412 }
413 413
414 ////////////////////////////////////////////////////////////////////////////// 414 //////////////////////////////////////////////////////////////////////////////
415 // DBG - multi main during dive 415 // DBG - multi main during dive
416 // 416 //
417 static void check_dbg(static char is_post_check) 417 static void check_dbg(PARAMETER char is_post_check)
418 { 418 {
419 overlay unsigned int temp_DBS = 0; 419 overlay unsigned int temp_DBS = 0;
420 overlay char i; // Local loop index. 420 overlay char i; // Local loop index.
421 421
422 if( (DBG_N2_ratio != N2_ratio) || (DBG_He_ratio != He_ratio) ) 422 if( (DBG_N2_ratio != N2_ratio) || (DBG_He_ratio != He_ratio) )
482 ////////////////////////////////////////////////////////////////////////////// 482 //////////////////////////////////////////////////////////////////////////////
483 /////////////////////// U T I L I T I E S ///////////////////////////////// 483 /////////////////////// U T I L I T I E S /////////////////////////////////
484 ////////////////////////////////////////////////////////////////////////////// 484 //////////////////////////////////////////////////////////////////////////////
485 ////////////////////////////////////////////////////////////////////////////// 485 //////////////////////////////////////////////////////////////////////////////
486 486
487 static int read_custom_function(static unsigned char cf) 487 static int read_custom_function(PARAMETER unsigned char cf)
488 { 488 {
489 #ifdef CROSS_COMPILE
490 extern unsigned short custom_functions[];
491 return custom_functions[cf];
492 #else
489 extern unsigned char hi, lo; 493 extern unsigned char hi, lo;
490 extern void getcustom15(); 494 extern void getcustom15();
491 _asm 495 _asm
492 movff cf,WREG 496 movff cf,WREG
493 call getcustom15,0 497 call getcustom15,0
494 movff lo,PRODL 498 movff lo,PRODL
495 movff hi,PRODH 499 movff hi,PRODH
496 _endasm 500 _endasm
501 #endif
497 } 502 }
498 503
499 ////////////////////////////////////////////////////////////////////////////// 504 //////////////////////////////////////////////////////////////////////////////
500 // read buhlmann tables for compatriment ci 505 // read buhlmann tables for compatriment ci
501 // If period == 0 : 2sec interval 506 // If period == 0 : 2sec interval
502 // 1 : 1 min interval 507 // 1 : 1 min interval
503 // 2 : 10 min interval. 508 // 2 : 10 min interval.
504 static void read_buhlmann_coeffifients(static char period) 509 static void read_buhlmann_coefficients(PARAMETER char period)
505 { 510 {
511 #ifndef CROSS_COMPILE
506 // Note: we don't use far rom pointer, because the 512 // Note: we don't use far rom pointer, because the
507 // 24 bits is to complex, hence we have to set 513 // 24 bits is to complex, hence we have to set
508 // the UPPER page ourself... 514 // the UPPER page ourself...
509 // --> Set zero if tables are moved to lower pages ! 515 // --> Set zero if tables are moved to lower pages !
510 _asm 516 _asm
511 movlw 1 517 movlw 1
512 movwf TBLPTRU,0 518 movwf TBLPTRU,0
513 _endasm 519 _endasm
520 #endif
514 521
515 var_N2_a = buhlmann_a[ci]; 522 var_N2_a = buhlmann_a[ci];
516 var_N2_b = buhlmann_b[ci]; 523 var_N2_b = buhlmann_b[ci];
517 var_He_a = (buhlmann_a+16)[ci]; 524 var_He_a = (buhlmann_a+16)[ci];
518 var_He_b = (buhlmann_b+16)[ci]; 525 var_He_b = (buhlmann_b+16)[ci];
780 ////////////////////////////////////////////////////////////////////////////// 787 //////////////////////////////////////////////////////////////////////////////
781 ////////////////////////////////////////////////////////////////////////////// 788 //////////////////////////////////////////////////////////////////////////////
782 789
783 void fillDataStack(void) 790 void fillDataStack(void)
784 { 791 {
792 #ifndef CROSS_COMPILE
785 _asm 793 _asm
786 LFSR 1,C_STACK 794 LFSR 1,C_STACK
787 MOVLW 0xCC 795 MOVLW 0xCC
788 loop: MOVWF POSTINC1,0 796 loop: MOVWF POSTINC1,0
789 TSTFSZ FSR1L,0 797 TSTFSZ FSR1L,0
790 BRA loop 798 BRA loop
791 799
792 LFSR 1,C_STACK 800 LFSR 1,C_STACK
793 LFSR 2,C_STACK 801 LFSR 2,C_STACK
794 _endasm 802 _endasm
803 #endif
795 } 804 }
796 805
797 ////////////////////////////////////////////////////////////////////////////// 806 //////////////////////////////////////////////////////////////////////////////
798 // When calling C code from ASM context, the data stack pointer and 807 // When calling C code from ASM context, the data stack pointer and
799 // frames should be reset. Bank3 is dedicated to the stack (see the 808 // frames should be reset. Bank3 is dedicated to the stack (see the
800 // .lkr script). 809 // .lkr script).
801 #ifdef __DEBUG 810 #ifdef CROSS_COMPILE
802 # define RESET_C_STACK fillDataStack(); 811 # define RESET_C_STACK
803 #else 812 #else
804 # define RESET_C_STACK \ 813 # ifdef __DEBUG
805 _asm \ 814 # define RESET_C_STACK fillDataStack();
806 LFSR 1, C_STACK \ 815 # else
807 LFSR 2, C_STACK \ 816 # define RESET_C_STACK \
808 _endasm 817 _asm \
818 LFSR 1, C_STACK \
819 LFSR 2, C_STACK \
820 _endasm
821 # endif
809 #endif 822 #endif
810 823
811 ////////////////////////////////////////////////////////////////////////////// 824 //////////////////////////////////////////////////////////////////////////////
812 // Called every 2 seconds during diving. 825 // Called every 2 seconds during diving.
813 // update tissues every time. 826 // update tissues every time.
827 //
814 // Every 6 seconds (or slower when TTS > 16): 828 // Every 6 seconds (or slower when TTS > 16):
815 // - update deco table (char_O_deco_time/depth) with new values. 829 // - update deco table (char_O_deco_time/depth) with new values.
816 // - update ascent time, 830 // - update ascent time,
817 // - set status to zero (so we can check there is new results). 831 // - set status to zero (so we can check there is new results).
818 // 832 //
831 calc_without_deco(); 845 calc_without_deco();
832 deco_calc_desaturation_time(); 846 deco_calc_desaturation_time();
833 } 847 }
834 848
835 ////////////////////////////////////////////////////////////////////////////// 849 //////////////////////////////////////////////////////////////////////////////
836 850 // Reset decompression model:
851 // + Set all tissues to equilibrium with Air at ambient pressure.
852 // + Reset last stop to 0m
853 // + Reset all model output.
837 void deco_clear_tissue(void) 854 void deco_clear_tissue(void)
838 { 855 {
839 RESET_C_STACK 856 RESET_C_STACK
840 clear_tissue(); 857 clear_tissue();
841 char_I_depth_last_deco = 0; // for compatibility with v.101pre_no_last_deco
842 } 858 }
843 859
844 ////////////////////////////////////////////////////////////////////////////// 860 //////////////////////////////////////////////////////////////////////////////
845 861
846 void deco_calc_wo_deco_step_1_min(void) 862 void deco_calc_wo_deco_step_1_min(void)
883 { 899 {
884 // cycle through the 16 b"uhlmann tissues 900 // cycle through the 16 b"uhlmann tissues
885 overlay float p = N2_ratio * (pres_respiration - 0.0627); 901 overlay float p = N2_ratio * (pres_respiration - 0.0627);
886 pres_tissue[ci] = p; 902 pres_tissue[ci] = p;
887 903
888 read_buhlmann_coeffifients(-1); 904 read_buhlmann_coefficients(-1);
889 905
890 p = (p - var_N2_a) * var_N2_b ; 906 p = (p - var_N2_a) * var_N2_b ;
891 if( p < 0.0 ) 907 if( p < 0.0 )
892 p = 0.0; 908 p = 0.0;
893 pres_tissue_limit[ci] = p; 909 pres_tissue_limit[ci] = p;
900 char_O_deco_status = 0; 916 char_O_deco_status = 0;
901 char_O_nullzeit = 0; 917 char_O_nullzeit = 0;
902 int_O_ascenttime = 0; 918 int_O_ascenttime = 0;
903 char_O_gradient_factor = 0; 919 char_O_gradient_factor = 0;
904 char_O_relative_gradient_GF = 0; 920 char_O_relative_gradient_GF = 0;
921 char_I_depth_last_deco = 0; // for compatibility with v.101pre_no_last_deco
905 } 922 }
906 923
907 ////////////////////////////////////////////////////////////////////////////// 924 //////////////////////////////////////////////////////////////////////////////
908 // calc_without_deco 925 // calc_without_deco
909 // 926 //
1021 deco_He_ratio3 = char_I_deco_He_ratio3 / 100.0; 1038 deco_He_ratio3 = char_I_deco_He_ratio3 / 100.0;
1022 deco_N2_ratio4 = char_I_deco_N2_ratio4 / 100.0; 1039 deco_N2_ratio4 = char_I_deco_N2_ratio4 / 100.0;
1023 deco_He_ratio4 = char_I_deco_He_ratio4 / 100.0; 1040 deco_He_ratio4 = char_I_deco_He_ratio4 / 100.0;
1024 deco_N2_ratio5 = char_I_deco_N2_ratio5 / 100.0; 1041 deco_N2_ratio5 = char_I_deco_N2_ratio5 / 100.0;
1025 deco_He_ratio5 = char_I_deco_He_ratio5 / 100.0; 1042 deco_He_ratio5 = char_I_deco_He_ratio5 / 100.0;
1026 float_deco_distance = char_I_deco_distance / 100.0; 1043 float_deco_distance = char_I_deco_distance / 100.0; // Get offset is in mbar.
1027 1044
1028 // ____________________________________________________ 1045 // ____________________________________________________
1029 // 1046 //
1030 // _____________ G A S _ C H A N G E S ________________ 1047 // _____________ G A S _ C H A N G E S ________________
1031 // ____________________________________________________ 1048 // ____________________________________________________
1084 } 1101 }
1085 } 1102 }
1086 1103
1087 const_ppO2 = (float)char_I_const_ppO2 / 100.0; 1104 const_ppO2 = (float)char_I_const_ppO2 / 100.0;
1088 deco_ppO2_change = (float)char_I_deco_ppO2_change / 99.95 + pres_surface; 1105 deco_ppO2_change = (float)char_I_deco_ppO2_change / 99.95 + pres_surface;
1089 deco_ppO2_change = deco_ppO2_change + float_deco_distance; 1106 deco_ppO2_change += float_deco_distance;
1090 deco_ppO2 = (float)char_I_deco_ppO2 / 100.0; 1107 deco_ppO2 = (float)char_I_deco_ppO2 / 100.0;
1091 float_desaturation_multiplier = char_I_desaturation_multiplier / 100.0; 1108 float_desaturation_multiplier = char_I_desaturation_multiplier / 100.0;
1092 float_saturation_multiplier = char_I_saturation_multiplier / 100.0; 1109 float_saturation_multiplier = char_I_saturation_multiplier / 100.0;
1093 GF_low = (float)char_I_GF_Low_percentage / 100.0; 1110 GF_low = (float)char_I_GF_Low_percentage / 100.0;
1094 GF_high = (float)char_I_GF_High_percentage / 100.0; 1111 GF_high = (float)char_I_GF_High_percentage / 100.0;
1124 calc_tissue_2_secs(); 1141 calc_tissue_2_secs();
1125 else 1142 else
1126 calc_tissue_1_min(); 1143 calc_tissue_1_min();
1127 1144
1128 int_O_gtissue_limit = (int)(pres_tissue_limit[char_O_gtissue_no] * 1000); 1145 int_O_gtissue_limit = (int)(pres_tissue_limit[char_O_gtissue_no] * 1000);
1129 int_O_gtissue_press = (int)((pres_tissue[char_O_gtissue_no] + pres_tissue[char_O_gtissue_no+16]) * 1000); 1146 int_O_gtissue_press = (int)((pres_tissue[char_O_gtissue_no] + (pres_tissue+16)[char_O_gtissue_no]) * 1000);
1130 if (char_I_deco_model == 1) 1147 if (char_I_deco_model == 1)
1131 { 1148 {
1132 temp1 = temp1 * GF_high; 1149 temp1 = temp1 * GF_high;
1133 } 1150 }
1134 else 1151 else
1296 ////////////////////////////////////////////////////////////////////////////// 1313 //////////////////////////////////////////////////////////////////////////////
1297 // calc_tissue 1314 // calc_tissue
1298 // 1315 //
1299 // optimized in v.101 1316 // optimized in v.101
1300 // 1317 //
1301 static void calc_tissue(static unsigned char period) 1318 static void calc_tissue(PARAMETER unsigned char period)
1302 { 1319 {
1303 char_O_gtissue_no = 255; 1320 char_O_gtissue_no = 255;
1304 pres_gtissue_limit = 0.0; 1321 pres_gtissue_limit = 0.0;
1305 1322
1306 for (ci=0;ci<16;ci++) 1323 for (ci=0;ci<16;ci++)
1307 { 1324 {
1308 read_buhlmann_coeffifients(period); // 2 sec or 1 min period. 1325 read_buhlmann_coefficients(period); // 2 sec or 1 min period.
1309 1326
1310 // N2 1327 // N2
1311 temp_tissue = (temp_atem - pres_tissue[ci]) * var_N2_e; 1328 temp_tissue = (temp_atem - pres_tissue[ci]) * var_N2_e;
1312 temp_tissue_safety(); 1329 temp_tissue_safety();
1313 pres_tissue[ci] += temp_tissue; 1330 pres_tissue[ci] += temp_tissue;
1481 // Function very simular to calc_tissue, but: 1498 // Function very simular to calc_tissue, but:
1482 // + Use a 1min or 10min period. 1499 // + Use a 1min or 10min period.
1483 // + Do it on sim_pres_tissue, instead of pres_tissue. 1500 // + Do it on sim_pres_tissue, instead of pres_tissue.
1484 // + Update GF_low state for GF decompression model. 1501 // + Update GF_low state for GF decompression model.
1485 // 1502 //
1486 static void sim_tissue(static unsigned char period) 1503 static void sim_tissue(PARAMETER unsigned char period)
1487 { 1504 {
1488 sim_pres_gtissue_limit = 0.0; 1505 sim_pres_gtissue_limit = 0.0;
1489 sim_gtissue_no = 255; 1506 sim_gtissue_no = 255;
1490 1507
1491 for (ci=0;ci<16;ci++) 1508 for (ci=0;ci<16;ci++)
1492 { 1509 {
1493 read_buhlmann_coeffifients(period); // 1 or 10 minute(s) interval 1510 read_buhlmann_coefficients(period); // 1 or 10 minute(s) interval
1494 1511
1495 // N2 1512 // N2
1496 temp_tissue = (temp_atem - sim_pres_tissue[ci]) * var_N2_e; 1513 temp_tissue = (temp_atem - sim_pres_tissue[ci]) * var_N2_e;
1497 temp_tissue_safety(); 1514 temp_tissue_safety();
1498 sim_pres_tissue[ci] += temp_tissue; 1515 sim_pres_tissue[ci] += temp_tissue;
1665 // FIXED N2_ratio 1682 // FIXED N2_ratio
1666 // unchanged in v.101 1683 // unchanged in v.101
1667 // 1684 //
1668 void deco_calc_desaturation_time(void) 1685 void deco_calc_desaturation_time(void)
1669 { 1686 {
1670 overlay int desat_time; // For a particular compartiment, in min. 1687 overlay unsigned int desat_time; // For a particular compartiment, in min.
1671 1688
1672 RESET_C_STACK 1689 RESET_C_STACK
1673 1690
1674 N2_ratio = 0.7902; // FIXED sum as stated in b"uhlmann 1691 N2_ratio = 0.7902; // FIXED sum as stated in b"uhlmann
1675 pres_surface = (float)int_I_pres_surface / 1000.0; 1692 pres_surface = (float)int_I_pres_surface / 1000.0;
1734 temp4 = 0; 1751 temp4 = 0;
1735 } 1752 }
1736 1753
1737 // saturation_time (for flight) 1754 // saturation_time (for flight)
1738 if (temp4 > temp2) 1755 if (temp4 > temp2)
1739 desat_time = (int)temp4; 1756 desat_time = (unsigned int)temp4;
1740 else 1757 else
1741 desat_time = (int)temp2; 1758 desat_time = (unsigned int)temp2;
1742 if(desat_time > int_O_desaturation_time) 1759 if(desat_time > int_O_desaturation_time)
1743 int_O_desaturation_time = desat_time; 1760 int_O_desaturation_time = desat_time;
1744 1761
1745 // N2 saturation in multiples of halftime for display purposes 1762 // N2 saturation in multiples of halftime for display purposes
1746 temp2 = temp1 * 20.0; // 0 = 1/8, 120 = 0, 249 = 8 1763 temp2 = temp1 * 20.0; // 0 = 1/8, 120 = 0, 249 = 8
1798 ////////////////////////////////////////////////////////////////////////////// 1815 //////////////////////////////////////////////////////////////////////////////
1799 ////////////////////////////////// deco_hash ///////////////////////////////// 1816 ////////////////////////////////// deco_hash /////////////////////////////////
1800 ////////////////////////////////////////////////////////////////////////////// 1817 //////////////////////////////////////////////////////////////////////////////
1801 ////////////////////////////////////////////////////////////////////////////// 1818 //////////////////////////////////////////////////////////////////////////////
1802 1819
1820 #ifndef CROSS_COMPILE
1803 void deco_hash(void) 1821 void deco_hash(void)
1804 { 1822 {
1805 overlay unsigned char md_i, md_j; // Loop index. 1823 overlay unsigned char md_i, md_j; // Loop index.
1806 overlay unsigned char md_t; 1824 overlay unsigned char md_t;
1807 overlay unsigned char md_buffer[16]; 1825 overlay unsigned char md_buffer[16];
1880 char_O_hash[md_i] ^= md_pi_subst[(md_buffer[md_i] ^ md_t)]; 1898 char_O_hash[md_i] ^= md_pi_subst[(md_buffer[md_i] ^ md_t)];
1881 md_t = char_O_hash[md_i]; 1899 md_t = char_O_hash[md_i];
1882 } // for md_i 16 1900 } // for md_i 16
1883 } // for md_pointer 1901 } // for md_pointer
1884 } // void deco_hash(void) 1902 } // void deco_hash(void)
1903 #endif
1885 1904
1886 ////////////////////////////////////////////////////////////////////////////// 1905 //////////////////////////////////////////////////////////////////////////////
1887 // deco_clear_CNS_fraction 1906 // deco_clear_CNS_fraction
1888 // 1907 //
1889 // new in v.101 1908 // new in v.101
2014 pres_tissue[x] = pres_tissue_vault[x]; 2033 pres_tissue[x] = pres_tissue_vault[x];
2015 } 2034 }
2016 2035
2017 ////////////////////////////////////////////////////////////////////////////// 2036 //////////////////////////////////////////////////////////////////////////////
2018 // 2037 //
2038 #ifndef CROSS_COMPILE
2019 void main() {} 2039 void main() {}
2040 #endif