TEST_CODE_ApplyTo2V1_API

Dependencies:   SDFileSystem max32630fthr USBDevice

Revision:
3:35b05d91568d
Parent:
1:7530b7eb757a
Child:
4:217334c3a5b2
--- a/DUT_RegConfig.cpp	Thu May 28 02:32:15 2020 +0000
+++ b/DUT_RegConfig.cpp	Mon Jun 22 05:27:48 2020 +0000
@@ -23,10 +23,14 @@
 Semaphore chip_int_semph(1);
 extern DigitalOut xSHUT;
 extern I2C i2c_v;
+extern DigitalOut TRIM_EN;
 
 uint16_t histogram_pos_num = 0;
 uint16_t histogram_per_pos = 0;
 
+uint16_t range_step_num = 0;
+uint16_t range_per_step = 0;
+
 extern const uint8_t reg_table[];
 extern const uint8_t Firmware_Ranging[];
 extern uint8_t  _uart_send_pbuff[CMD_BUF_LEN] ;
@@ -36,24 +40,42 @@
     xSHUT = 0;
     wait_ms(30);
     xSHUT = 1;
+    wait_ms(30);
 }
 
-void DUT_RegInit(void)
+void SetBitThree(uint8_t rco, uint8_t tdc, uint8_t dcr)
+{
+    WriteOneReg(0x37, rco);
+    WriteOneReg(0xE4, tdc);
+    WriteOneReg(0xC0, dcr);
+}
+
+void DUT_RegInit(uint8_t rco, uint8_t tdc, uint8_t dcr)
 {
     LoadRegTable();
-    for(uint16_t i = 0; i < 256; i++) {
-        //WriteOneReg(dut_reg[i].addr, dut_reg[i].value);
-        dut_reg[i].addr = i;
-        dut_reg[i].value = 0x00;
+    for(uint16_t i = 0; i < 256; i++) 
+    {
+        WriteOneReg(dut_reg[i].addr, dut_reg[i].value);        
     }
+
+    SetBitThree(rco, tdc, dcr);
 }
 
-
 void DUT_FirmwareInit(void)
 {
     WriteFW(LoadFirmware());
 }
 
+void DeviceAllInit(uint8_t rco, uint8_t tdc, uint8_t dcr)
+{
+    ChipInitReset();
+    wait_ms(100);
+    DUT_RegInit(rco, tdc, dcr);
+    wait_ms(100);
+    DUT_FirmwareInit();
+    wait_ms(100);
+}
+
 void Enable_DUT_Interrupt(void)
 {
     int_enable = 1;
@@ -77,19 +99,23 @@
     }
 }
 
+
+
 void HistogramReport()
 {
     uint8_t ret = 0;
-    uint16_t lsb, mili;
+    uint16_t lsb, mili, noise_level;
+    uint32_t peak;
 
 
     uint16_t histogram_pos = 0;
     uint16_t histogram_num = 0;
+    uint16_t range_step = 0;
     while(1) {
         if(histogram_mode == 1) {
             //ReadOneReg(REG_SYS_CFG, &sys_cfg_save);
             //WriteOneReg(REG_SYS_CFG, 0x00);
-            ret = OneTimeMeasure(&lsb, &mili);
+            ret = OneTimeMeasure(&lsb, &mili, &peak, &noise_level);
             if(ret != 0) {
                 histogram_mode = 0;
             } else {
@@ -104,7 +130,7 @@
         } else if(histogram_mode == 2) {
             //ReadOneReg(REG_SYS_CFG, &sys_cfg_save);
             //WriteOneReg(REG_SYS_CFG, 0x00);
-            ret = OneTimeMeasure(&lsb, &mili);
+            ret = OneTimeMeasure(&lsb, &mili, &peak, &noise_level);
             if(ret != 0) {
                 histogram_mode = 0;
             } else {
@@ -134,7 +160,7 @@
             }
             //ReadOneReg(REG_SYS_CFG, &sys_cfg_save);
             //WriteOneReg(REG_SYS_CFG, 0x00);
-            ret = OneTimeMeasure(&lsb, &mili);
+            ret = OneTimeMeasure(&lsb, &mili, &peak, &noise_level);
             if(ret != 0) {
                 histogram_mode = 0;
             } else {
@@ -154,7 +180,7 @@
         } else if(histogram_mode == 4) {
             //ReadOneReg(REG_SYS_CFG, &sys_cfg_save);
             //WriteOneReg(REG_SYS_CFG, 0x00);
-            ret = OneTimeMeasure(&lsb, &mili);
+            ret = OneTimeMeasure(&lsb, &mili, &peak, &noise_level);
             if(ret != 0) {
                 histogram_mode = 0;
             } else {
@@ -170,9 +196,43 @@
                 }
             }
             //WriteOneReg(REG_SYS_CFG, sys_cfg_save);
-        }else {
+        } else if(histogram_mode == 5) {
+
+            if(range_per_step == 0) {
+                range_step++;
+                if(range_step >= range_step_num) {
+                    histogram_mode = 0;
+                }
+                ServoRun(1, 10);
+                while(CheckUntil()) wait_ms(100);
+
+                range_per_step = 0;
+                ret = ContinuousMeasure();
+                if(ret != 0) {
+                    histogram_mode = 0;
+                } else {
+                    while(range_per_step<256 && histogram_mode == 5) {
+                        wait_ms(100);
+                    }
+                    StoreHistogram(range_step, 0, 0);
+                    wait_ms(5);
+                    StoreHistogram(range_step, 0, 1);
+                    wait_ms(5);
+                    StoreHistogram(range_step, 0, 2);
+                    wait_ms(5);
+                    StoreHistogram(range_step, 0, 3);
+                    wait_ms(5);
+                    range_per_step = 0;
+
+                }
+            }
+
+
+
+        } else {
             histogram_num = 0;
             histogram_pos = 0;
+            range_step = 0;
             histogram_pos_num = 0;
             histogram_per_pos = 0;
         }
@@ -180,18 +240,51 @@
     }
 }
 
+void StoreMeasureData(uint16_t lsb, uint16_t milimeter, uint32_t peak, uint16_t noise_level, uint16_t i)
+{
+    histogram[0][4*i] = 0;
+    histogram[0][4*i+1] = 0;
+    histogram[0][4*i+2] = lsb >> 8;
+    histogram[0][4*i+3] = lsb ;
+    
+    histogram[1][4*i] = 0;
+    histogram[1][4*i+1] = 0;
+    histogram[1][4*i+2] = milimeter >> 8;
+    histogram[1][4*i+3] = milimeter ;
+    
+    histogram[2][4*i]   = peak >> 24;
+    histogram[2][4*i+1] = peak >> 16;
+    histogram[2][4*i+2] = peak >> 8;
+    histogram[2][4*i+3] = peak ;
+    
+    histogram[3][4*i]   = 0;
+    histogram[3][4*i+1] = 0;
+    histogram[3][4*i+2] = noise_level >> 8;
+    histogram[3][4*i+3] = noise_level ;
+    
+}
+
 void ContinuousMeasureReport()
 {
-    uint16_t lsb, milimeter;
+    uint16_t lsb, milimeter, noise_level;
+    uint32_t peak;
     uint8_t int_flag = 0;
     uint16_t time_out = 0;
 
     while(1) {
 
         chip_int_semph.wait();
-
-        if(RaadContinuousMeasure(&lsb, &milimeter) == 0) {
-            HandleContinuousMeasureReport(lsb, milimeter);
+        if(histogram_mode == 5) {
+            if(RaadContinuousMeasure(&lsb, &milimeter, &peak, &noise_level) == 0) {
+                StoreMeasureData(lsb, milimeter, peak, noise_level, range_per_step++);
+                if(range_per_step == 256) {
+                    StopContinuousMeasure();
+                }
+            }
+        } else {
+            if(RaadContinuousMeasure(&lsb, &milimeter, &peak, &noise_level) == 0) {
+                HandleContinuousMeasureReport(lsb, milimeter, peak, noise_level);
+            }
         }
 
     }
@@ -290,6 +383,7 @@
     ret = ReadOneReg (REG_PW_CTRL, &reg_pw_ctrl);
     ret = WriteOneReg(REG_PW_CTRL, reg_pw_ctrl | (0x01<<3)); //set otp_ld_done
     ret = WriteOneReg(REG_PW_CTRL, reg_pw_ctrl | (0x01<<3) | (0x01<<1)); //set otp_ld_done, pw_ctrl_lp
+    ret = WriteOneReg(REG_MCU_CFG, 0x01);
     ret = WriteOneReg(REG_CMD, 0x01);
     ret = WriteOneReg(REG_SIZE, 0x02);
     ret = WriteOneReg(REG_SCRATCH_PAD_BASE+0x00, *((uint8_t*)(&ram_addr_base)  ));
@@ -303,19 +397,21 @@
             ret = ReadOneReg(0x0c+i, &histogram[tdc_index][32*j + i]);
         }
     }
+    ret = WriteOneReg(REG_MCU_CFG, 0x03);
     ret = WriteOneReg(REG_SYS_CFG, reg_sys_cfg & ~(0x01<<0)); //clear sc_en
     ret = WriteOneReg(REG_PW_CTRL, reg_pw_ctrl | (0x01<<1)); //restore power         control register
-    ret = WriteOneReg(REG_PW_CTRL, reg_pw_ctrl & ~(0x01<<1)); //clear pw_ctrl_lp
+    ret = WriteOneReg(REG_PW_CTRL, reg_pw_ctrl); //clear pw_ctrl_lp
 
     return ret;
 }
 
-uint8_t OneTimeMeasure(uint16_t *lsb, uint16_t *milimeter)
+uint8_t OneTimeMeasure(uint16_t *lsb, uint16_t *milimeter, uint32_t *peak, uint16_t *noise_level)
 {
     uint8_t ret = 0;
     uint8_t reg_pw_ctrl;
     uint8_t reg_sys_cfg;
     uint32_t timeout = 0;
+    uint8_t flag = 0;
 
     int_mark = 1;//One Time Measure
 
@@ -324,6 +420,7 @@
     //ret = ReadOneReg (REG_PW_CTRL, &reg_pw_ctrl);
     //ret = WriteOneReg(REG_PW_CTRL, reg_pw_ctrl | (0x01<<3)); //set otp_ld_done
     //ret = WriteOneReg(REG_PW_CTRL, (reg_pw_ctrl | (0x01<<3)) & ~(0x01<<1)); //set otp_ld_done, clear pw_ctrl_lp
+    //ret = WriteOneReg(0x08, 0x00);
     ret = WriteOneReg(REG_CMD, 0x0E);
     //ret = WriteOneReg(REG_SIZE, 0x00);
 
@@ -331,6 +428,11 @@
     while(int_mark == 1 && timeout != 0) {
         timeout--;
         wait_ms(5);
+        //ReadOneReg(0x08, &flag);
+        // if(flag == 0xFF)
+        //{
+        //     int_mark = 0;
+        //  }
     }
 
     if(timeout == 0) {
@@ -344,6 +446,14 @@
         ret = ReadOneReg(0x18, (uint8_t*)milimeter);
         ret = ReadOneReg(0x19, (uint8_t*)milimeter + 1);
 
+        ret = ReadOneReg(0x26, (uint8_t*)noise_level);
+        ret = ReadOneReg(0x27, (uint8_t*)noise_level + 1);
+
+        ret = ReadOneReg(0x28, (uint8_t*)peak );
+        ret = ReadOneReg(0x29, (uint8_t*)peak + 1);
+        ret = ReadOneReg(0x2a, (uint8_t*)peak + 2);
+        ret = ReadOneReg(0x2b, (uint8_t*)peak + 3);
+
     }
 
     //ret = WriteOneReg(REG_SYS_CFG, reg_sys_cfg & ~(0x01<<0)); //clear sc_en
@@ -374,7 +484,7 @@
     return ret;
 }
 
-uint8_t RaadContinuousMeasure(uint16_t *lsb, uint16_t *milimeter)
+uint8_t RaadContinuousMeasure(uint16_t *lsb, uint16_t *milimeter, uint32_t *peak, uint16_t *noise_level)
 {
     uint8_t ret = 0;
     uint8_t reg_pw_ctrl;
@@ -394,6 +504,14 @@
     ret = ReadOneReg(0x18, (uint8_t*)milimeter);
     ret = ReadOneReg(0x19, (uint8_t*)milimeter + 1);
 
+    ret = ReadOneReg(0x26, (uint8_t*)noise_level);
+    ret = ReadOneReg(0x27, (uint8_t*)noise_level + 1);
+
+    ret = ReadOneReg(0x28, (uint8_t*)peak );
+    ret = ReadOneReg(0x29, (uint8_t*)peak + 1);
+    ret = ReadOneReg(0x2a, (uint8_t*)peak + 2);
+    ret = ReadOneReg(0x2b, (uint8_t*)peak + 3);
+
     //ret = WriteOneReg(REG_SYS_CFG, reg_sys_cfg & ~(0x01<<0)); //clear sc_en
 
     return ret;
@@ -460,10 +578,11 @@
     uint8_t ret = 0;
     uint32_t timeout = 0;
     memset(dcr_matrix, 0x00, 17*9*2);
-    WriteOneReg(0x50, 0x01);
-    WriteOneReg(0xBD, 0x01);
-    wait_ms(100);
-    WriteOneReg(0xD8, 0xF0);
+    //WriteOneReg(0x50, 0x01);
+    //WriteOneReg(0xBD, 0x01);
+    //wait_ms(100);
+    //WriteOneReg(0xD8, 0xF0);
+    WriteOneReg(0xC0, vspad);
 
     for(uint8_t group = 0; group < 9; group++) {
         WriteOneReg(0x0C, group);
@@ -495,7 +614,7 @@
 uint8_t DelayLineTest(uint8_t phase, uint8_t* buf)
 {
     uint8_t ret = 0;
-    uint32_t timeout = 0;    
+    uint32_t timeout = 0;
 
     WriteOneReg(0xE4, phase);
     wait_ms(10);
@@ -511,12 +630,12 @@
         while(int_mark == 1 && timeout != 0) {
             timeout--;
             wait_ms(100);
-        }        
+        }
         for(uint8_t tdc = 0; tdc < 9; tdc++) {
             ret = ReadOneReg(0x0c + tdc*2, buf + step*18 + tdc*2 + 1);
             wait_ms(1);
-            ret = ReadOneReg(0x0c + tdc*2 + 1, buf + step*18 + tdc*2);           
-            wait_ms(1);            
+            ret = ReadOneReg(0x0c + tdc*2 + 1, buf + step*18 + tdc*2);
+            wait_ms(1);
         }
 
     }
@@ -552,4 +671,198 @@
     return 0;
 }
 
+uint8_t SetWindow(uint8_t* pd)
+{
+    uint8_t ret = 0;
+    uint8_t buf[14];
+    
+    memset(buf, 0x00, 14);
+    
+    buf[0] |= (pd[5]&0xF);
+    buf[0] |= ((pd[6] << 4)&0xF0);
+    
+    buf[1] |= (pd[7]&0xF);
+    buf[1] |= ((pd[8] << 4)&0xF0);
+    
+    buf[2] |= (pd[9]&0xF);
+    buf[2] |= ((pd[10] << 4)&0xF0);
+    
+    buf[3] |= (pd[11]&0xF);
+    buf[3] |= ((pd[12] << 4)&0xF0);
+    
+    buf[4] |= (pd[13]&0xF);
+    buf[4] |= ((pd[14] << 4)&0xF0);
+    
+    buf[5] |= (pd[15]&0xF);
+    buf[5] |= ((pd[16] << 4)&0xF0);
+    
+    buf[6] |= (pd[17]&0xF);
+    buf[6] |= ((pd[18] << 4)&0xF0);
+    
+    buf[7] |= (pd[19]&0xF);
+    buf[7] |= ((pd[20] << 4)&0xF0);
+    
+    buf[8] = 0x00;// Fisrt 16LSB cut off
+    buf[9] = 0xFF;
+    
+    buf[10] = 0x01;// Ref PAD switch
+    
+    buf[11] = 0x1F;// TDC Resolution 1F = 31
+    
+    buf[12] = 0x0C;// Length
+    buf[13] = 0x09;//CMD
+    
+    for(uint8_t i = 0; i < 12; i++)
+    {
+        WriteOneReg(0x0C + i, buf[i]);
+    }
+    
+    WriteOneReg(0x0B, 0x0C);
+    WriteOneReg(0x0A, 0x09);
+    
+    return 0;
+}
 
+uint8_t RCO_Trim(uint8_t *rco)
+{
+    uint8_t ret = 0;
+    uint8_t value = 0, result = 0;
+    
+    TRIM_EN = 1;
+
+    //==================First BAND=====================//
+    ChipInitReset();
+    ret = WriteOneReg(0xED    , 0x03);
+    ret = WriteOneReg(0xEC    , 0x36);
+    ret = WriteOneReg(0x3D    , 0xC0);
+    ret = WriteOneReg(0xE8    , 0x00);
+    ret = WriteOneReg(0xEA    , 0x3F);
+    ret = WriteOneReg(0x3D    , 0xC8);
+    ret = WriteOneReg(0xED    , 0x83);
+
+    wait_ms(1000);
+
+    ret = ReadOneReg(0xFA, &value);
+    ret = ReadOneReg(0xFB, &result);
+
+    if((result&0xC0) == 0xC0)
+    {
+        *rco = value;
+         ChipInitReset();
+         TRIM_EN = 0;
+        return 0;
+    }
+
+    //==================Second BAND=====================//
+    ChipInitReset();
+    ret = WriteOneReg(0xED    , 0x03);
+    ret = WriteOneReg(0xEC    , 0x36);
+    ret = WriteOneReg(0x3D    , 0xC0);
+    ret = WriteOneReg(0xE8    , 0x00);
+    ret = WriteOneReg(0xEA    , 0x7F);
+    ret = WriteOneReg(0xEB    , 0x40);
+    ret = WriteOneReg(0x3D    , 0xC8);
+    ret = WriteOneReg(0xED    , 0x83);
+
+    wait_ms(1000);
+
+    ret = ReadOneReg(0xFA, &value);
+    ret = ReadOneReg(0xFB, &result);
+
+    if((result&0xC0) == 0xC0)
+    {
+        *rco = value;
+         ChipInitReset();
+         TRIM_EN = 0;
+        return 0;
+    }    
+
+    //==================Third BAND=====================//
+    ChipInitReset();
+    ret = WriteOneReg(0xED    , 0x03);
+    ret = WriteOneReg(0xEC    , 0x36);
+    ret = WriteOneReg(0x3D    , 0xC0);
+    ret = WriteOneReg(0xE8    , 0x00);
+    ret = WriteOneReg(0xEA    , 0xBF);
+    ret = WriteOneReg(0xEB    , 0x80);
+    ret = WriteOneReg(0x3D    , 0xC8);
+    ret = WriteOneReg(0xED    , 0x83);
+
+    wait_ms(1000);
+
+    ret = ReadOneReg(0xFA, &value);
+    ret = ReadOneReg(0xFB, &result);
+
+    if((result&0xC0) == 0xC0)
+    {
+        *rco = value;
+         ChipInitReset();
+         TRIM_EN = 0;
+        return 0;
+    }    
+
+    //==================Fourth BAND=====================//
+    ChipInitReset();
+    ret = WriteOneReg(0xED    , 0x03);
+    ret = WriteOneReg(0xEC    , 0x36);
+    ret = WriteOneReg(0x3D    , 0xC0);
+    ret = WriteOneReg(0xE8    , 0x00);
+    ret = WriteOneReg(0xEA    , 0xFF);
+    ret = WriteOneReg(0xEB    , 0xC0);
+    ret = WriteOneReg(0x3D    , 0xC8);
+    ret = WriteOneReg(0xED    , 0x83);
+
+    wait_ms(1000);
+
+    ret = ReadOneReg(0xFA, &value);
+    ret = ReadOneReg(0xFB, &result);
+
+    if((result&0xC0) == 0xC0)
+    {
+        *rco = value;
+         ChipInitReset();
+         TRIM_EN = 0;
+        return 0;
+    }    
+
+    ChipInitReset();
+    TRIM_EN = 0;
+    return 1;
+}
+
+uint8_t BVD_Trim(uint8_t *bvd)
+{
+    uint8_t ret = 0;
+    uint32_t timeout = 0;
+        
+    WriteOneReg(0xC0, 0x00);    
+    wait_ms(1);
+
+    timeout = 600;
+    int_mark = 1;
+    WriteOneReg(0x0A, 0x08);
+    while(int_mark == 1 && timeout != 0) {
+        timeout--;
+        wait_ms(100);
+    }
+    //osDelay(80);   
+    
+    ret = ReadOneReg(0xC0, bvd);
+
+    if(timeout == 0)
+        return 1;
+
+    return 0;
+}
+
+uint8_t Pixel_Enable(uint8_t *buf)
+{
+    for(uint8_t i = 0; i < 18; i++)
+    {
+        WriteOneReg(0xC2 + i, buf[i]);  
+    }
+    
+    return 0;
+}
+
+