adwa12

GP22

Jun 8th, 2025
32
0
174 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.03 KB | Source Code | 0 0
  1. uint8_t   Init =                0x70;
  2. uint8_t   Power_On_Reset =      0x50;
  3. uint8_t   Start_TOF =           0x01;
  4. uint8_t   Start_Temp =          0x02;
  5. uint8_t   Start_Cal_Resonator = 0x03;
  6. uint8_t   Start_Cal_TDC =       0x04;
  7. uint8_t   Start_TOF_Restart =   0x05;
  8. uint8_t   Start_Temp_Restart =  0x06;
  9.  
  10. float     CLKHS_freq = 4.000;           // Clock frequency in MHz
  11. float     CLKHS_freq_cal = 4.000;       // Calibrated Clock frequency in MHz
  12. float     CLKHS_freq_corr_fact = 1.000; // Correction factor for Clock frequency
  13. float     CLKHS_read;
  14.  
  15.  
  16. float     Result_0_up;
  17. float     Result_1_up;
  18. float     Result_2_up;
  19.  
  20. float     Result_0_down;
  21. float     Result_1_down;
  22. float     Result_2_down;
  23.  
  24. float     average_Result_up;
  25. float     average_Result_up2;
  26. float     average_Result_down;
  27.  
  28. float     Time_of_flight_diff, Time_of_flight_sum;
  29.  
  30. float     PW1ST;
  31. float     TOF_up;
  32. float     TOF_down;
  33.  
  34. float     TOF_diff_avg=0, TOF_diff_sum=0,TOF_sum_avg=0, TOF_sum_sum=0;
  35.  
  36. float     TOF_diff_square_sum=0, Std_Dev_of_Diff=0;
  37.  
  38. uint16_t  no_of_avg = 10;
  39. uint16_t  sum_counter = 1; //sum_counter starts with 1
  40.  
  41. uint8_t   Error_Bit = 0;
  42. uint16_t  status_byte_UP, status_byte_DOWN;
  43.  
  44. float     distance = 0;
  45. uint16_t  STAT_REG;
  46.  
  47.  
  48. void GP22_TOF_Test2(void)
  49. {
  50.     unsigned int ticks;
  51.  
  52.     gp22_send_1byte(Power_On_Reset);      // Power on Reset to GP22
  53.     Dly100us((void*)5);              // 500 us wait for GP22
  54.  
  55.     // Writing to the configuration registers
  56.  
  57. //      gp22_wr_config_reg(0x80, 0xE30BE800); // CR0 according Frontpanel Software Start options (START_CLKHS)=480us
  58.     gp22_wr_config_reg(0x80, 0x430BE800); // CR0 0xE303E800 //Osc ON/OFF //(START_CLKHS)=Osc continously ON
  59.     gp22_wr_config_reg(0x81, 0x21444000); // CR1 0x21444512
  60.     gp22_wr_config_reg(0x82, 0xA0138800); // CR2 0x28138800
  61.     gp22_wr_config_reg(0x83, 0xD0A24800); // CR3 En_AutoCalc_MB2
  62. //      gp22_wr_config_reg(0x83, 0x50710100); // CR3 according Frontpanel Software
  63.     gp22_wr_config_reg(0x84, 0x10004000); // CR4 0x10014A00
  64.     //gp22_wr_config_reg(0x85, 0x40000000); // CR5 Fire_up
  65.     //gp22_wr_config_reg(0x85, 0x20000000); // CR5 Fire_down
  66.     gp22_wr_config_reg(0x86, 0xC0C06100); // CR6 0x40006000
  67.  
  68. //    Std_Dev_of_Diff = 0;
  69. //    TOF_diff_sum = 0;
  70. //    TOF_diff_square_sum = 0;
  71. //    sum_counter = 1;
  72. //    PW1ST = 0;
  73.  
  74.     //calibrate
  75.     CLKHS_freq_cal = 4.000;
  76.     gp22_send_1byte(Init);
  77.     gp22_send_1byte(Start_Cal_Resonator);
  78.     ticks = HAL_GetTick();
  79.     while ((GET_DIO0() == GPIO_PIN_SET))// && (timeout_counter != 0))
  80.     {
  81.         if (HAL_GetTick() > (ticks + 200)) return;
  82.     }
  83.     CLKHS_read = read_n_bytes_2(4, 0xB0, 0x00, 16);
  84.     CLKHS_freq_corr_fact = 61.035/CLKHS_read * CLKHS_freq;
  85.  
  86.     CLKHS_freq_cal *= CLKHS_freq_corr_fact;
  87.  
  88.  
  89.     while (1)
  90.     {
  91.         //---------------- Rewriting Config reg 1 + 5  --------------------
  92.         gp22_wr_config_reg(0x85, 0x40000000); // CR5 4=Fire_up
  93. //      gp22_wr_config_reg(0x83, 0xD0710100);
  94.         gp22_wr_config_reg(0x81, 0x21444000); // Config reg 1 0x21444512
  95.         gp22_send_1byte(Init);
  96.         gp22_send_1byte(Start_TOF_Restart);
  97.         // minimum delay = 0.75ms
  98.         Error_Bit = 0;
  99.         // Wait for INT Slot_x
  100.         ticks = HAL_GetTick();
  101.         while ((GET_DIO0() == GPIO_PIN_SET))// && (timeout_counter != 0))
  102.         {
  103.             if (HAL_GetTick() > (ticks + 200)) return;
  104.         }
  105.  
  106.         Error_Bit += gp22_status_count_error();
  107.  
  108.         Result_0_up = read_n_bytes_2(4, 0xB0, 0x00, 16)/(CLKHS_freq_cal*1000000.0);
  109.         Result_1_up = read_n_bytes_2(4, 0xB0, 0x01, 16)/(CLKHS_freq_cal*1000000.0);
  110.         Result_2_up = read_n_bytes_2(4, 0xB0, 0x02, 16)/(CLKHS_freq_cal*1000000.0);
  111.  
  112.         average_Result_up = read_n_bytes_2(4, 0xB0, 0x03, 16)/CLKHS_freq_cal*1000000.0;
  113.  
  114.         //---------------- Rewriting Config reg 1 + 5 --------------------
  115. //        gp22_wr_config_reg(0x85, 0x20000000); // CR5 2=Fire_down
  116. //        gp22_wr_config_reg(0x81, 0x21444000); // Config reg 1 0x21444512
  117.         gp22_send_1byte(Init);
  118.         // minimum delay = 0.65ms
  119.  
  120.         // Wait for INT Slot_x
  121.         ticks = HAL_GetTick();
  122.         while ((GET_DIO0() == GPIO_PIN_SET))// && (timeout_counter != 0))
  123.         {
  124.             if (HAL_GetTick() > (ticks + 200))
  125.                 return;
  126.         }
  127.  
  128.         Error_Bit += gp22_status_count_error();
  129.         Result_0_down = read_n_bytes_2(4, 0xB0, 0x00, 16)/(CLKHS_freq_cal*1000000.0);
  130.         Result_1_down = read_n_bytes_2(4, 0xB0, 0x01, 16)/(CLKHS_freq_cal*1000000.0);
  131.         Result_2_down = read_n_bytes_2(4, 0xB0, 0x02, 16)/(CLKHS_freq_cal*1000000.0);
  132.         average_Result_down = read_n_bytes_2(4, 0xB0, 0x03, 16)/(CLKHS_freq_cal*1000000.0);
  133.         char str3[256];
  134.         memset(str3, 0, sizeof(str3));
  135.  
  136.         sprintf(str3, "\nRes0 %1.10\nRes1 %10.10\nRes2 %1.10 ",Result_0_down, Result_1_down, Result_2_down);
  137.         USB_Transmit_Data((unsigned char *) &str3, strlen(str3));
  138. //      char str3[256];
  139. //      memset(str3, 0, sizeof(str3));
  140. //
  141. //      sprintf(str3, "\n CLKHS_freq_corr_fact = %1.6f; 61.035 /CLKHS_read= %1.6f;  * CLKHS_freq= %1.10f; \n CLKHS_freq_cal %1.6f",CLKHS_freq_corr_fact,CLKHS_read, CLKHS_freq ,CLKHS_freq_cal,Result_0_down, Result_1_down, Result_2_down);
  142. //      USB_Transmit_Data((unsigned char *) &str3, strlen(str3));
  143.         //----------------------------------------------------------------
  144.         // Result after two measurements (first Result_down then Result_up)
  145.       if (Error_Bit > 0) {
  146.           char error_msg[256] = {0}; // Puffer initialisieren
  147.           STAT_REG = GP22_read_2byte_status(); // Direkter Status-Lesevorgang
  148.  
  149.           // Fehlerbits sammeln
  150.           char errors[128] = "";
  151.           if (STAT_REG & 0x0200) strcat(errors, "Timeout_TDC|");
  152.           if (STAT_REG & 0x0400) strcat(errors, "Timeout_Precounter|");
  153.           if (STAT_REG & 0x0800) strcat(errors, "Error_open|");
  154.           if (STAT_REG & 0x1000) strcat(errors, "Error_short|");
  155.           if (STAT_REG & 0x2000) strcat(errors, "EEPROM_eq_CREG|");
  156.           if (STAT_REG & 0x4000) strcat(errors, "EEPROM_DED|");
  157.           if (STAT_REG & 0x8000) strcat(errors, "EEPROM_error|");
  158.  
  159.           // Letztes '|' entfernen
  160.           if (strlen(errors) > 0) errors[strlen(errors)-1] = '\0';
  161.  
  162.           // Komplette Meldung generieren
  163.           snprintf(error_msg, sizeof(error_msg),
  164.               "\nTOF_Messung skipped!\n"
  165.               "Fehler: %s\n"
  166.               "CLK-Faktor: %.3f\n"
  167.               "Status-Reg: 0x%04X",
  168.               errors, CLKHS_freq_corr_fact, STAT_REG);
  169.  
  170.           USB_Transmit_Data((uint8_t*)error_msg, strlen(error_msg));
  171.         }else
  172.         {
  173.  
  174.         average_Result_up /= 3;
  175.         average_Result_down /= 3;
  176.  
  177.         // Zeit in ns
  178.         Result_0_up *= 1000;
  179.         Result_1_up *= 1000;
  180.         Result_2_up *= 1000;
  181.         average_Result_up *= 1000;
  182.  
  183.         Result_0_down *= 1000;
  184.         Result_1_down *= 1000;
  185.         Result_2_down *= 1000;
  186.         average_Result_down *= 1000;
  187.  
  188.         average_Result_up2= Result_0_up+Result_1_up+Result_2_up;
  189.         TOF_up = average_Result_up;
  190.         TOF_down = average_Result_down;
  191.         PW1ST += read_n_bytes_2(1, 0xB0, 0x08, 7);
  192.  
  193.  
  194.     //    printf("\n Result Up= %6.3f ns / Down= %6.3f ns",TOF_up, TOF_down);
  195.         Time_of_flight_diff = TOF_up - TOF_down;
  196.         //printf("\n Difference Up-Down = %6.3f ns", Time_of_flight_diff);
  197.  
  198.         Time_of_flight_sum = TOF_up + TOF_down;
  199.  
  200.         // to add up
  201.         TOF_diff_sum += Time_of_flight_diff;
  202.         TOF_diff_square_sum = TOF_diff_square_sum + (Time_of_flight_diff * Time_of_flight_diff);
  203.         TOF_sum_sum += Time_of_flight_sum;
  204.  
  205.  
  206.         char str1[256];
  207.         sprintf(str1, "\nUP=%6.3f ns Down= %6.3f ns\n Sum        RES_3(Up+Down)= %6.3f ns\n Sum_avg= %2.6\n AVg upR3= %6.3f und AVG_UP3TEST = %6.3f",TOF_up, TOF_down, Time_of_flight_sum, TOF_sum_sum,average_Result_up, average_Result_up2);
  208.         USB_Transmit_Data((unsigned char *) &str1, strlen(str1));
  209.  
  210.  
  211.         sum_counter ++;
  212.  
  213.         if (sum_counter > no_of_avg)    // Output after no_of_avg measurements
  214.         {
  215.               TOF_diff_avg = TOF_diff_sum / no_of_avg;
  216.               TOF_sum_avg = TOF_sum_sum / no_of_avg;
  217.               //printf("\n\n Avg. value of difference = %6.3f ns", TOF_diff_avg);
  218.               Std_Dev_of_Diff = sqrt( (TOF_diff_square_sum - (TOF_diff_square_sum/no_of_avg)) / (no_of_avg-1));
  219.               //printf("\n Std.Dev. of Diff. = %6.1f ps", Std_Dev_of_Diff*1000);
  220.               //printf("\n PW1ST = %1.2f ", PW1ST/(2*no_of_avg));
  221.               distance_cm = (TOF_sum_avg * 1e-9f * 1480.0f) * 100.0f / 2.0f;
  222.  
  223.             char str[256];
  224.  
  225.             memset(str, 0, sizeof(str));
  226.  
  227.               //printf("\n Std.Dev. of Diff. = %6.1f ps", Std_Dev_of_Diff*1000);
  228.               //printf("\n PW1ST = %1.2f ", PW1ST/(2*no_of_avg));
  229.  
  230.             sprintf(str, "Std.Dev. of Diff. = %6.1f ps\r\n Cal=  %1.3f \r\n TOF_UP=  %1.6f \r\n TOF_DOWN=  %1.6f \r\n\nDIstanz=%6.3f cm clock = %1.3f\n PW1ST = %1.2f\n Sum_avg = %0.6f, no_of_avg = %lu", Std_Dev_of_Diff * 1000, CLKHS_freq_corr_fact, TOF_up, TOF_down,distance_cm, CLKHS_freq_corr_fact, PW1ST/(2*no_of_avg), TOF_sum_sum, no_of_avg);
  231.  
  232.             USB_Transmit_Data((unsigned char *) &str, strlen(str));
  233.  
  234.               Std_Dev_of_Diff = 0;
  235.               TOF_diff_sum = 0;
  236.               TOF_diff_square_sum = 0;
  237.               sum_counter = 1;
  238.               PW1ST = 0;
  239.               TOF_sum_avg = 0;
  240.               distance_cm = 0;
  241.               TOF_sum_sum = 0;
  242.  
  243. //              char str2[256];
  244. //               sprintf(str2, "\nDIstanz=%6.3f cm clock = %1.3f\n", distance_cm, CLKHS_freq_corr_fact);
  245. //               USB_Transmit_Data((unsigned char *) &str2, strlen(str2));
  246.               break;
  247.         }
  248.         }
  249.  
  250.     } // End while TOF-RESTART
  251. }
  252.  
  253.  
  254. // ........................................... //
  255.  
  256. float Calculate_PT_Temperature(float pt, float resistance)
  257. {
  258.     //  (3.9083 * pt - ((3.9083 * pt)^2 + (2.31 * pt) * (pt - adc))^0.5) * 1000 / 1.155 / pt;
  259.  
  260.     // Konstanten für PT100 (IEC 60751, für Temperaturen zwischen 0°C und 100°C)
  261.     double R0 = pt;//100.0;// Widerstand bei 0°C
  262.     double A  = 3.9083e-3; // Koeffizient A
  263.     double B  = -5.775e-7; // Koeffizient B
  264.  
  265.     // Quadratische Formel: T = (-A + sqrt(A^2 - 4*B*(1 - R/R0))) / (2*B)
  266.     double temp = (-A + sqrt(A * A - 4 * B * (1 - resistance / R0))) / (2 * B);
  267.     return (float)temp;
  268. }
  269.  
  270. void GP22_Example(void)
  271. {
  272.     LED_GREEN_ON();
  273.  
  274.     Init_DIO0_as_Input(true);
  275.  
  276.     if (GET_DIO0() == GPIO_PIN_RESET)
  277.     {
  278.  
  279.     }
  280.  
  281. //    while ((GET_DIO0() == GPIO_PIN_SET) && (timeout_counter != 0))
  282. //    {
  283. //      timeout_counter--;
  284. //    }
  285.  
  286.     SPI2_ReInit();
  287.  
  288.     USB_Install_RX_Callback(USB_GP22_Example_RX);
  289.  
  290.     BOARD_DELAY(200);
  291.  
  292.     GP22_reset();
  293.  
  294.     if (GP22_testCommunication())
  295.     {
  296.         LED_BLUE_ON();
  297.  
  298.         // in "Time_of_flight" is the result
  299.         GP22_TOF_Test();
  300.  
  301.         // only high 4 Byte
  302.         // unsigned int ids = GP22_readNBytes(OPCODE_READ_ID, 7);
  303.     }
  304.  
  305.     Task_Typedef test_task;
  306.  
  307.     InitTask(&test_task, 500);
  308.  
  309.     LED_GREEN_OFF();
  310.  
  311.     while (1)
  312.     {
  313.         if (IsTask(&test_task))
  314.         {
  315.             LED_GREEN_TOGGLE();
  316.  
  317.             // in "Time_of_flight" is the result
  318. //          GP22_TOF_Test();
  319. //
  320. //          char str[256];
  321. //          memset(str, 0, sizeof(str));
  322. //
  323. //          sprintf(str, "Tcold: %3.3f, Thot: %3.3f\r\n", Calculate_PT_Temperature(1000.0, (Rcold_by_Rref * 1000.0)), Calculate_PT_Temperature(1000.0, (Rhot_by_Rref * 1000.0)));
  324. //
  325. //          USB_Transmit_Data((unsigned char *) &str, strlen(str));
  326.  
  327. //          memset(str, 0, sizeof(str));
  328. //
  329. //          sprintf(str, "TOF: %f\r\n", Time_of_flight);
  330. //
  331. //          USB_Transmit_Data((unsigned char *) &str, strlen(str));
  332.  
  333.             GP22_TOF_Test2();
  334.  
  335. //          memset(str, 0, sizeof(str));
  336. //
  337. //            //printf("\n Std.Dev. of Diff. = %6.1f ps", Std_Dev_of_Diff*1000);
  338. //            //printf("\n PW1ST = %1.2f ", PW1ST/(2*no_of_avg));
  339. //
  340. //          sprintf(str, "Std.Dev. of Diff. = %6.1f ps\r\n Cal=  %1.3f \r\n TOF_UP=  %1.6f \r\n TOF_DOWN=  %1.6f \r\n\nDIstanz=%6.3f cm clock = %1.3f\n", Std_Dev_of_Diff * 1000, CLKHS_freq_corr_fact, TOF_up, TOF_down,distance_cm, CLKHS_freq_corr_fact);
  341. //
  342. //          USB_Transmit_Data((unsigned char *) &str, strlen(str));
  343.  
  344.  
  345.         }
  346.     }
  347. }
Add Comment
Please, Sign In to add comment