Publishing for sharing with Harald

Dependencies:   csi059

Files at this revision

API Documentation at this revision

Comitter:
CactusSemi
Date:
Fri May 22 19:19:45 2020 +0000
Parent:
3:3e8c16b6620c
Commit message:
Publishing for sharing with Harald

Changed in this revision

EasyFuse_Prog.cpp Show annotated file Show diff for this revision Revisions of this file
I2C_hot_read.cpp Show annotated file Show diff for this revision Revisions of this file
I2C_read.cpp Show annotated file Show diff for this revision Revisions of this file
I2C_write.cpp Show annotated file Show diff for this revision Revisions of this file
adc_test.cpp Show annotated file Show diff for this revision Revisions of this file
csi066_rev0p2.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/EasyFuse_Prog.cpp	Thu Jun 21 22:11:25 2018 +0000
+++ b/EasyFuse_Prog.cpp	Fri May 22 19:19:45 2020 +0000
@@ -23,28 +23,49 @@
 
 void print_nvm_reg(uint8_t *max_Vout, uint8_t *max_Vout_trim, uint8_t *high_time, uint8_t *low_time, uint8_t *adc_samp_rate, 
 uint8_t *adc_range, uint8_t *adc_time_out, uint8_t *comp_offset, uint8_t *range_trim, uint8_t *PMOS_off, uint8_t *Driver2, uint8_t *Driver3, 
-uint8_t *DMOS, uint8_t *CCM_threshold, uint8_t *DMOS_ctrl_trim, uint8_t *adc_op_mode) {
+uint8_t *neg_hlf_rng, uint8_t *CCM_threshold, uint8_t *DMOS_ctrl_trim, uint8_t *adc_op_mode) {
 
     pc.printf("\033[%dm ", 35);//change text color to red
     pc.printf("\n\n\n\r\t    Register contents :\n\r\t   ");
     for ( int i = 0; i<26; i++) pc.printf("\304"); 
-    pc.printf("\n\n\r\t    1. max_Vout        = 0x%2.2X",*max_Vout);
-    pc.printf("\n\n\r\t    2. max_Vout_trim   = 0x%2.2X",*max_Vout_trim);
-    pc.printf("\n\n\r\t    3. high_time       = 0x%2.2X",*high_time);
-    pc.printf("\n\n\r\t    4. low_time        = 0x%2.2X",*low_time);
-    pc.printf("\n\n\r\t    5. adc_samp_rate   = 0x%2.2X",*adc_samp_rate);
-    pc.printf("\n\n\r\t    6. adc_range       = 0x%2.2X",*adc_range);
-    pc.printf("\n\n\r\t    7. adc_time_out    = 0x%2.2X",*adc_time_out);
-    pc.printf("\n\n\r\t    8. comp_offset     = 0x%2.2X",*comp_offset);
-    pc.printf("\n\n\r\t    9. range_trim      = 0x%2.2X",*range_trim);
-    pc.printf("\n\n\r\t   10. PMOS_off        = 0x%2.2X",*PMOS_off);
-    pc.printf("\n\n\r\t   11. Driver2         = 0x%2.2X",*Driver2);
-    pc.printf("\n\n\r\t   12. Driver3         = 0x%2.2X",*Driver3);
-    pc.printf("\n\n\r\t   13. DMOS            = 0x%2.2X",*DMOS);
-    pc.printf("\n\n\r\t   14. CCM_threshold   = 0x%2.2X",*CCM_threshold);
-    pc.printf("\n\n\r\t   15. DMOS_ctrl_trim  = 0x%2.2X",*DMOS_ctrl_trim);
-    pc.printf("\n\n\r\t   16. adc_op_mode     = 0x%2.2X",*adc_op_mode);
-    pc.printf("\n\n\r\t   ");
+    if (*PMOS_off == 0x0f) // ACP Mode
+    {
+        pc.printf("\n\n\r\t\t    ACP Mode \n\n");
+        pc.printf("\n\n\r\t    1. max_Vout        = 0x%2.2X",*max_Vout);
+        pc.printf("\n\n\r\t    2. max_Vout_trim   = 0x%2.2X",*max_Vout_trim);
+        pc.printf("\n\n\r\t    3. high_time       = 0x%2.2X",*high_time);
+        pc.printf("\n\n\r\t    4. low_time        = 0x%2.2X",*low_time);
+        pc.printf("\n\n\r\t    5. adc_samp_rate   = 0x%2.2X",*adc_samp_rate);
+        pc.printf("\n\n\r\t    6. adc_range       = 0x%2.2X",*adc_range);
+        pc.printf("\n\n\r\t    7. adc_time_out    = 0x%2.2X",*adc_time_out);
+        pc.printf("\n\n\r\t    8. comp_offset     = 0x%2.2X",*comp_offset);
+        pc.printf("\n\n\r\t    9. range_trim      = 0x%2.2X",*range_trim);
+        pc.printf("\n\n\r\t   10. PMOS_off        = 0x%2.2X",*PMOS_off);
+        pc.printf("\n\n\r\t   13. SkipPwr         = 0x%2.2X",*neg_hlf_rng);
+        pc.printf("\n\n\r\t   16. adc_op_mode     = 0x%2.2X",*adc_op_mode);
+        pc.printf("\n\n\r\t   ");
+    }
+    else
+    {
+        pc.printf("\n\n\r\t\t    BiP Mode \n\n");       
+        pc.printf("\n\n\r\t    1. max_Vout        = 0x%2.2X",*max_Vout);
+        pc.printf("\n\n\r\t    2. max_Vout_trim   = 0x%2.2X",*max_Vout_trim);
+        pc.printf("\n\n\r\t    3. high_time       = 0x%2.2X",*high_time);
+        pc.printf("\n\n\r\t    4. low_time        = 0x%2.2X",*low_time);
+        pc.printf("\n\n\r\t    5. adc_samp_rate   = 0x%2.2X",*adc_samp_rate);
+        pc.printf("\n\n\r\t    6. adc_range       = 0x%2.2X",*adc_range);
+        pc.printf("\n\n\r\t    7. adc_time_out    = 0x%2.2X",*adc_time_out);
+        pc.printf("\n\n\r\t    8. comp_offset     = 0x%2.2X",*comp_offset);
+        pc.printf("\n\n\r\t    9. range_trim      = 0x%2.2X",*range_trim);
+        pc.printf("\n\n\r\t   10. PMOS_off        = 0x%2.2X",*PMOS_off);
+        pc.printf("\n\n\r\t   11. Driver2         = 0x%2.2X",*Driver2);
+        pc.printf("\n\n\r\t   12. Driver3         = 0x%2.2X",*Driver3);
+        pc.printf("\n\n\r\t   13. neg_hlf_rng     = 0x%2.2X",*neg_hlf_rng);
+        pc.printf("\n\n\r\t   14. CCM_threshold   = 0x%2.2X",*CCM_threshold);
+        pc.printf("\n\n\r\t   15. DMOS_ctrl_trim  = 0x%2.2X",*DMOS_ctrl_trim);
+        pc.printf("\n\n\r\t   16. adc_op_mode     = 0x%2.2X",*adc_op_mode);
+        pc.printf("\n\n\r\t   ");
+    }                
     for ( int i = 0; i<26; i++) pc.printf("\315");
     pc.printf("\033[%dm", 32);//change text color to green
     pc.printf("\n\r");    
@@ -52,20 +73,20 @@
 
 void edit_nvm_reg(uint8_t *max_Vout, uint8_t *max_Vout_trim, uint8_t *high_time, uint8_t *low_time, uint8_t *adc_samp_rate, 
 uint8_t *adc_range, uint8_t *adc_time_out, uint8_t *comp_offset, uint8_t *range_trim, uint8_t *PMOS_off, uint8_t *Driver2, uint8_t *Driver3, 
-uint8_t *DMOS, uint8_t *CCM_threshold, uint8_t *DMOS_ctrl_trim, uint8_t *adc_op_mode) {
+uint8_t *neg_hlf_rng, uint8_t *CCM_threshold, uint8_t *DMOS_ctrl_trim, uint8_t *adc_op_mode) {
 
     char temp;
     int flag1;
     char count;
     char reg_data;
     
-    char reg_name[16][15] = {"max_Vout", "max_Vout_trim", "high_time", "low_time", "adc_samp_rate", 
+    char reg_name[16][20] = {"max_Vout", "max_Vout_trim", "high_time", "low_time", "adc_samp_rate", 
                             "adc_range", "adc_time_out", "comp_offset", "range_trim", "PMOS_off", "Driver2", "Driver3", 
-                            "DMOS", "CCM_threshold", "DMOS_ctrl_trim", "adc_op_mode" };
+                            "neg_hlf_rng_SkipPwr", "CCM_threshold", "DMOS_ctrl_trim", "adc_op_mode" };
 
     uint8_t *reg_addr[16] =  {   max_Vout, max_Vout_trim, high_time, low_time, adc_samp_rate, 
                             adc_range, adc_time_out, comp_offset, range_trim, PMOS_off, Driver2, Driver3, 
-                            DMOS, CCM_threshold, DMOS_ctrl_trim, adc_op_mode};
+                            neg_hlf_rng, CCM_threshold, DMOS_ctrl_trim, adc_op_mode};
                             
     pc.printf("\n\n\n \r\tDo you want to edit the registers? (y/n) : ");  
     temp = 0;
@@ -73,7 +94,7 @@
         temp = pc.getc();
     }
     if (temp == 'n') {
-        pc.printf("\n\n \r\tContinue with above contents");
+        pc.printf("\n\n \r\tContinuing with above contents");
         return;
     }
     temp = 'y';
@@ -112,7 +133,7 @@
             
             print_nvm_reg(  max_Vout, max_Vout_trim, high_time, low_time, adc_samp_rate, 
                         adc_range, adc_time_out, comp_offset, range_trim, PMOS_off, Driver2, Driver3, 
-                        DMOS, CCM_threshold, DMOS_ctrl_trim, adc_op_mode);
+                        neg_hlf_rng, CCM_threshold, DMOS_ctrl_trim, adc_op_mode);
             
             pc.printf("\n\n \r\tDo you want to make more changes? (y/n) : ");
             temp = pc.getc();
@@ -121,7 +142,7 @@
         
 void i2c_write_fn(I2C *i2c_obj, char ptr, char write_data) {
     int flag1;
-    wait_us(10);
+    wait_us(20);
     (*i2c_obj).start();
     flag1 = (*i2c_obj).write(dev_addr);
     if (flag1 != 1) pc.printf("\n\n\r\tNo Ack for dev addr :(");
@@ -129,8 +150,9 @@
     if (flag1 != 1) pc.printf("\n\n\r\tNo Ack reg pointer :(");    
     flag1 = (*i2c_obj).write(write_data); 
     if (flag1 != 1) pc.printf("\n\n\r\tNo Ack data :(");           
-    wait_us(5);
+    wait_us(20);
     (*i2c_obj).stop();
+    wait_us(300);
 }
 
 char i2c_read_fn(I2C *i2c_obj, char ptr) {
@@ -143,14 +165,14 @@
     flag1 = (*i2c_obj).write(ptr);
     if (flag1 != 1) pc.printf("\n\n\r\tNo Ack reg pointer :(");
     (*i2c_obj).stop();
-    wait_us(10);
+    wait_us(300);
     (*i2c_obj).start();
     flag1 = (*i2c_obj).write(dev_addr | 0x01); //lsb 1 for read
     if (flag1 != 1) pc.printf("\n\n\r\tNo Ack for dev addr :(");         
     read_data = (*i2c_obj).read(0); //0- donot send ack after read is done         
     wait_us(5);
     (*i2c_obj).stop();      
-    wait_us(120);
+    wait_us(300);
     return read_data;
 }
 
@@ -158,7 +180,8 @@
     char reg_ptr = 0x00;
     char reg_data[10];
     char data;
-    uint8_t NVM_READ        = 0x70 ;  
+    uint8_t NVM_PWR_ON      = 0x20 ;
+    uint8_t NVM_READ        = 0x90 ;  //force load shadow register
     uint8_t NVM_CTLR_ADR    = 0x0a ;  //Register address for NVM CTRL
     uint64_t NVM_data = 0x0000000000000000;
     
@@ -175,7 +198,7 @@
     uint8_t PMOS_off;
     uint8_t Driver2;
     uint8_t Driver3;
-    uint8_t DMOS;
+    uint8_t neg_hlf_rng;
     uint8_t CCM_threshold;
     uint8_t DMOS_ctrl_trim;
     uint8_t adc_op_mode;
@@ -189,21 +212,10 @@
     pc.printf("\n\n\r\tSwitching to faster clock before reading the NVM");    
     i2c_write_fn(i2c_obj,0x02,0x01); //switching to faster clock for NVM read
     i2c_write_fn(i2c_obj,0x03,0x01); //switching to faster clock for NVM read
+    i2c_write_fn(i2c_obj,NVM_CTLR_ADR,NVM_PWR_ON);
     wait_ms(1);
-    i2c_write_fn(i2c_obj,NVM_CTLR_ADR,NVM_READ); //Perform a complete read; will be sucessful only if NVM is not empty    
-    wait_ms(100);
-
-    //check if tm_nvm_read bit has cleared itself
-    temp = 0;
-    while (temp == 0){
-        data = i2c_read_fn(i2c_obj,NVM_CTLR_ADR);
-        data = data & 0x40;
-        if (data == 0x00) {
-                pc.printf("\n\n\r\ttm_nvm_read has cleared; so loading Register with NVM complete");
-                temp = 1;
-        }
-        else wait_ms(100);
-    }  
+    i2c_write_fn(i2c_obj,NVM_CTLR_ADR,(NVM_PWR_ON + NVM_READ)); //Force load shadow register from NVM    
+    wait_ms(200);
 
     pc.printf("\n\n\n\r\tReading register 0x00 to 0x09");
     for( reg_ptr = 0x00;  reg_ptr <= 0x09; reg_ptr = reg_ptr+1) {
@@ -223,7 +235,7 @@
     PMOS_off        = reg_data[7] & 0x0f;
     Driver2         = reg_data[8] & 0x0f;
     Driver3         = (reg_data[8]>>4) & 0x0f;
-    DMOS            = (reg_data[9]>>7) & 0x01;
+    neg_hlf_rng     = (reg_data[9]>>7) & 0x01;
     CCM_threshold   = (reg_data[7]>>4) & 0x0f;
     DMOS_ctrl_trim  = reg_data[9] & 0x1f;
     adc_op_mode     = (reg_data[5]>>7) & 0x01;
@@ -232,10 +244,10 @@
 
     print_nvm_reg(&max_Vout, &max_Vout_trim, &high_time, &low_time, &adc_samp_rate, 
     &adc_range, &adc_time_out, &comp_offset, &range_trim, &PMOS_off, &Driver2, &Driver3, 
-    &DMOS, &CCM_threshold, &DMOS_ctrl_trim, &adc_op_mode);
+    &neg_hlf_rng, &CCM_threshold, &DMOS_ctrl_trim, &adc_op_mode);
     
     //push data into NVM_data
-    NVM_data = NVM_data | (max_Vout+1); //max_Vout(register) = max_Vout(NVM) - 1
+    NVM_data =  NVM_data | (max_Vout); //max_Vout(register) = max_Vout(NVM) - 1// this needs to be changed
     NVM_data = (NVM_data<<5) | max_Vout_trim; 
     NVM_data = (NVM_data<<7) | high_time; // <<x : x corresponds to no. of bits
     NVM_data = (NVM_data<<7) | low_time;
@@ -247,7 +259,7 @@
     NVM_data = (NVM_data<<4) | PMOS_off;
     NVM_data = (NVM_data<<4) | Driver2;
     NVM_data = (NVM_data<<4) | Driver3;
-    NVM_data = (NVM_data<<1) | DMOS;
+    NVM_data = (NVM_data<<1) | neg_hlf_rng;
     NVM_data = (NVM_data<<4) | CCM_threshold;
     NVM_data = (NVM_data<<5) | DMOS_ctrl_trim;
     NVM_data = (NVM_data<<1) | adc_op_mode;    
@@ -262,36 +274,37 @@
     uint64_t NVM_read_data;
     
     // Register variables
-    uint8_t max_Vout = 0x07;
-    uint8_t max_Vout_trim = 0x00;
-    uint8_t high_time = 0x12;
-    uint8_t low_time = 0x29;
-    uint8_t adc_samp_rate = 0x06;
-    uint8_t adc_range = 0x01;
-    uint8_t adc_time_out = 0x02;
-    uint8_t comp_offset = 0x00;
-    uint8_t range_trim = 0x0f;
-    uint8_t PMOS_off = 0x08;
-    uint8_t Driver2 = 0x08;
-    uint8_t Driver3 = 0x08;
-    uint8_t DMOS = 0x01;
-    uint8_t CCM_threshold = 0x0f;
-    uint8_t DMOS_ctrl_trim = 0x0c;
-    uint8_t adc_op_mode = 0x01;
+    uint8_t max_Vout = 0x00; //4.2V
+    uint8_t max_Vout_trim = 0x03; //0%
+    uint8_t high_time = 0x11; //12us 
+    uint8_t low_time = 0x46; //38us 
+    uint8_t adc_samp_rate = 0x05; //1 sample/1.35s
+    uint8_t adc_range = 0x01; //2mV
+    uint8_t adc_time_out = 0x02; // 1/256 = 5.3ms
+    uint8_t comp_offset = 0x00; //0% 
+    uint8_t range_trim = 0x16; //0% 
+    uint8_t PMOS_off = 0x07; //7 -> 18mV 
+    uint8_t Driver2 = 0x08; //8 -> 36mV
+    uint8_t Driver3 = 0x08; // 8 -> 54mV
+    uint8_t neg_hlf_rng = 0x00; //disabled
+    uint8_t CCM_threshold = 0x00; //off 
+    uint8_t DMOS_ctrl_trim = 0x16; //<14us
+    uint8_t adc_op_mode = 0x00; //Transmit measurements continously 
 
-    // NVM Register Control Bit locations; PWR_ON set in all the definitions
-    uint8_t NVM_RESET       = 0x31 ;
+    // NVM Register Control Bit locations; 
+    uint8_t NVM_RESET       = 0x01 ;
     uint8_t NVM_PWE1        = 0x02 ;
-    uint8_t NVM_CLK1        = 0x34 ;
-    uint8_t NVM_PWR_ON      = 0x30 ;
-    uint8_t NVM_READ        = 0x70 ;
+    uint8_t NVM_CLK1        = 0x04 ;
+    uint8_t NVM_PWR_ON      = 0x20 ;
+    uint8_t NVM_LD_SHDW     = 0x90 ;//unpdated in Rev1.0
+    uint8_t NVM_READ        = 0x50 ;    
     
-    uint8_t NVM_CTLR_ADR     = 0x0a ;  //Register address for NVM CTRL
+    uint8_t NVM_CTLR_ADR    = 0x0a ;  //Register address for NVM CTRL
      
     char reg_data = 0xaa;
     char temp;
     char high_time_prog = 0x04;
-    char low_time_prog = 0x04;
+    char low_time_prog = 0x05;
     int flag1 = 0;
     int i = 0;
     
@@ -306,14 +319,14 @@
     pc.printf("\n\n\r\tFollowing are the default register data that will be programmed: \n\r ");
     print_nvm_reg(&max_Vout, &max_Vout_trim, &high_time, &low_time, &adc_samp_rate, 
     &adc_range, &adc_time_out, &comp_offset, &range_trim, &PMOS_off, &Driver2, &Driver3, 
-    &DMOS, &CCM_threshold, &DMOS_ctrl_trim, &adc_op_mode);    
+    &neg_hlf_rng, &CCM_threshold, &DMOS_ctrl_trim, &adc_op_mode);    
 
     edit_nvm_reg(&max_Vout, &max_Vout_trim, &high_time, &low_time, &adc_samp_rate, 
     &adc_range, &adc_time_out, &comp_offset, &range_trim, &PMOS_off, &Driver2, &Driver3, 
-    &DMOS, &CCM_threshold, &DMOS_ctrl_trim, &adc_op_mode);       
+    &neg_hlf_rng, &CCM_threshold, &DMOS_ctrl_trim, &adc_op_mode);       
       
     //push data into NVM_data
-    NVM_data = NVM_data | (max_Vout+1); //max_Vout(register) = max_Vout(NVM) - 1
+    NVM_data =  NVM_data | max_Vout; //no more addition of 1 as in Jacaranda
     NVM_data = (NVM_data<<5) | max_Vout_trim; 
     NVM_data = (NVM_data<<7) | high_time; // <<x : x corresponds to no. of bits
     NVM_data = (NVM_data<<7) | low_time;
@@ -325,12 +338,11 @@
     NVM_data = (NVM_data<<4) | PMOS_off;
     NVM_data = (NVM_data<<4) | Driver2;
     NVM_data = (NVM_data<<4) | Driver3;
-    NVM_data = (NVM_data<<1) | DMOS;
+    NVM_data = (NVM_data<<1) | neg_hlf_rng;
     NVM_data = (NVM_data<<4) | CCM_threshold;
     NVM_data = (NVM_data<<5) | DMOS_ctrl_trim;
     NVM_data = (NVM_data<<1) | adc_op_mode;
     pc.printf("\n\n\n\r\tData to be written into NVM = 0x%016llX",NVM_data);
-    pc.printf("\n\n\r\tPlease note that max_Vout has been incremented. max_Vout(NVM) = max_Vout(register) + 1");  
     pc.printf("\n\n\r\t"); 
     for ( i = 0; i<80; i++) pc.printf("\304");  
     pc.printf("\n\n\r\tContinue EasyFuse Programming? (y/n): ");  
@@ -342,31 +354,26 @@
         pc.printf("\n\n\r\tAborting... :(");
         return;
     }
+    temp = 0;
+    pc.printf("\n\n\n\r\tConnect PROG to 4.5V & VINA to 4.5V and press 'c' to continue"); 
+    while ( temp!='c') {
+        temp = pc.getc();
+    }
     //EasyFuse Empty check
-    pc.printf("\n\n\r\tPerforming NVM Empty check by trying to load register with NVM contents");
+    pc.printf("\n\n\r\tPerforming NVM Empty check by trying to load register with NVM contents.");
     i2c_write_fn(&i2c,0x02,0x01); //switching to faster clock for NVM read
     i2c_write_fn(&i2c,0x03,0x01); //switching to faster clock for NVM read
     wait_ms(1);
-    i2c_write_fn(&i2c,NVM_CTLR_ADR,NVM_READ); //Perform a complete read; will be sucessful only if NVM is not empty    
-    wait_ms(100);
-    
-    //check if tm_nvm_read bit has cleared itself
-    temp = 0;
-    while (temp == 0){
-        reg_data = i2c_read_fn(&i2c,NVM_CTLR_ADR);
-        reg_data = reg_data & 0x40;
-        if (reg_data == 0x00) {
-                pc.printf("\n\n\r\ttm_nvm_read has cleared; so loading Register with NVM complete");
-                temp = 1;
-        }
-        else wait_ms(100);
-    }   
-        
+    i2c_write_fn(&i2c,NVM_CTLR_ADR,NVM_PWR_ON); 
+    wait_ms(1);       
+    i2c_write_fn(&i2c,NVM_CTLR_ADR,(NVM_READ + NVM_PWR_ON)); //Initiate NVM read cycle    
+    wait_ms(200);
+          
     reg_data = i2c_read_fn(&i2c,0x02);
     reg_data = reg_data<<4;
     reg_data = reg_data | i2c_read_fn(&i2c,0x03);
     if (reg_data == 0xff) pc.printf("  |\033[%dm  Register read might not be sucessfull \033[%dm ",45,40);
-    if (reg_data == 0x11) pc.printf("\n\n\r\t\033[%dm EasyFuse empty \033[%dm",44,40); //no change at address 0x02 & 0x03
+    if (reg_data == 0x11) pc.printf("\n\n\r\t\033[%dm EasyFuse empty \033[%dm",44,40); //NVM load will be successful only when NVM is not empty
     else {
         pc.printf("\n\n\r\t\033[%dm EasyFuse not empty \033[%dm",45,40);
         pc.printf("\n\n\r\t Better to abort the test.. :(");
@@ -383,18 +390,20 @@
         pc.printf("\n\n\r\tAborting.. :(");
         return;
     }
-    pc.printf("\n\n\r\tEnabled 'fc_enable' & driver1. Monitor the oscillator at the output of comparator");     
-    i2c_write_fn(&i2c,0x0c,0x22);   
+    
+    i2c_write_fn(&i2c,0x17,0x01); //internal clock is muxed out through DATA2_CLK
+    pc.printf("\n\n\r\tMonitor Clock at DATA2_CLK pin");       
+
 osc_trim:
     i2c_write_fn(&i2c,0x02,high_time_prog); //Recommended clocks period 
     i2c_write_fn(&i2c,0x03,low_time_prog); // for programming is 10us
     wait_ms(1);
 
     pc.printf("\n\n\n\r\t\033[%dm", 44);//change backround to blue
-    pc.printf("Please check if Oscillator period is approx 10us (9.4us to 10.4us) \033[%dm\n\n\r\tPress 'y' to continue; Press 'm' to modify clock trim settings : ",40); 
+    pc.printf("Please check if Oscillator period is approx 10us (9.5us to 10.5us) \033[%dm\n\n\r\tPress 'y' to continue; Press 'm' to modify clock trim settings : ",40); 
 
     temp = 0;
-    while ( temp!='y' && temp!='m' && temp!='m' ) {
+    while ( temp!='y' && temp!='m' && temp!='n' ) {
         temp = pc.getc();
     }
     if (temp == 'n') {
@@ -403,6 +412,7 @@
     }
     if (temp == 'm') {
         pc.printf("\n\n\n\n\r\tCurrent high_time trim = 0x%2.2X  & low_time trim = 0x%2.2X",high_time_prog,low_time_prog);
+        pc.printf("\n\n\r\tSuggestion - Change high_trim first then low_trim");
         flag1 = 0;
         while (!flag1) {
             pc.printf("\n\n\r\tEnter new high_time trim in hex (00 to ff): 0x");
@@ -418,43 +428,62 @@
         pc.printf("\n\n\r\tProgramming new high_time trim = 0x%2.2X  & new low_time trim = 0x%2.2X",high_time_prog,low_time_prog);
         goto osc_trim;
     }
-    
-    pc.printf("\n\n\r\tDisabled 'fc_enable' & driver1.");     
-    i2c_write_fn(&i2c,0x0c,0x00); 
+    i2c_write_fn(&i2c,0x17,0x00); //DATA2_CLK pulled to GND 
+    i2c_write_fn(&i2c,0x10,0x04); //tm_ProgSamp_en = 1, needed to disable pull-up device between VINA & PROG        
+    pc.printf("\n\n\r\tDATA2_CLK pulled low");     
     wait_ms(delay2); 
     pc.printf("\n\n\n\r\tAll set to program the EasyFuse, time to blow up some ploy fuses! ..**BOOOM**\n\n\r"); 
+
+    temp = 0;
+    pc.printf("\n\n\n\r\tConnect PROG to 4.5V & VINA to 5V and press 'c' to continue"); 
+    while ( temp!='c') {
+        temp = pc.getc();
+    }
     i2c_write_fn(&i2c,NVM_CTLR_ADR,NVM_PWR_ON); //turn ON supply to EasyFuse
     wait_ms(100);    
-    i2c_write_fn(&i2c,NVM_CTLR_ADR,NVM_RESET); //turn ON supply to EasyFuse & RESETN = 1
+    i2c_write_fn(&i2c,NVM_CTLR_ADR,(NVM_RESET + NVM_PWR_ON)); //turn ON supply to EasyFuse & RESETN = 1
     wait_ms(1);
     i2c_write_fn(&i2c,NVM_CTLR_ADR,NVM_PWR_ON); //RESETN = 0, Prog mode enabled: CKL1 = 0 during RESETN = 0,
-    temp = 0;
-    pc.printf("\n\n\n\r\tConnect PROG to 5V and press 'c' to continue"); 
-    while ( temp!='c') {
-        temp = pc.getc();
-    }    
+    
+    pc.printf("\n\n\n\r\t");
     wait_us(delay2);
-    i2c_write_fn(&i2c,NVM_CTLR_ADR,NVM_RESET);  //RESETN = 1,
+    i2c_write_fn(&i2c,NVM_CTLR_ADR,(NVM_RESET + NVM_PWR_ON));  //RESETN = 1
     wait_us(delay2);           
     for ( i = 0; i < 64; i = i+1) {
-        temp =  NVM_RESET + (((NVM_data>>i) & 0x01)?(NVM_PWE1):0); //NVM_PWE1 if NVM_data[i]=1 & CLK1 de-asserted
+        if ( ((NVM_data>>i) & 0x0000000000000001) == 1 ) {
+            temp = NVM_PWE1;
+            pc.printf("1");
+        }
+        else { 
+            temp = 0x00;
+            pc.printf("0");
+        }
+        temp =  (temp | NVM_RESET | NVM_PWR_ON); //NVM_PWE1 if NVM_data[i]=1 & CLK1 de-asserted
+        //pc.printf("\n\n\n\n\r\ttemp = 0x%2.2X",temp);
         i2c_write_fn(&i2c,NVM_CTLR_ADR,temp);
-        wait_us(delay2);
-        temp =  NVM_RESET;
+        //wait_us(delay2);
+        temp =  (NVM_RESET | NVM_PWR_ON);
         i2c_write_fn(&i2c,NVM_CTLR_ADR,temp); //de-assert NVM_PWE1
         wait_us(delay2);          
-        temp =  NVM_RESET + NVM_CLK1;
+        temp =  (NVM_RESET | NVM_CLK1 | NVM_PWR_ON);
         i2c_write_fn(&i2c,NVM_CTLR_ADR,temp); //assert CLK1
         wait_us(delay2); 
     }
+
     pc.printf("\n\n\n\r\t\a\033[%dm", 44);//change backround to blue
-    pc.printf("Finished programming. Let us confirm if the NVM has been programmed correctly by reading the NVM. Proceed (y/n): ");
+    //pc.printf("Finished programming. Let us confirm if the NVM has been programmed correctly by reading the NVM. Proceed (y/n): ");
+    pc.printf("Finished programming. Let us confirm if the NVM has been programmed correctly by reading the NVM.");    
     pc.printf("\033[%dm", 40);//change backround to black 
-          
-    temp = 0;
-    while ( temp!='y' ) {
+    pc.printf("\n\n\n\r\tReduce PROG to 4.2V & VINA to 4.2V and press 'c' to continue");     
+    while ( temp!='c') {
         temp = pc.getc();
-    }
+    }        
+    i2c_write_fn(&i2c,0x10,0x00); //tm_ProgSamp_en = 0,    
+    wait_ms(1);          
+    //temp = 0;
+    //while ( temp!='y' ) {
+    //    temp = pc.getc();
+    //}
     NVM_read_data = EasyFuse_read(&i2c);
     if ( NVM_data == NVM_read_data ) pc.printf("\n\n\r\tData written into NVM matches data read from NVM\n\n\n\r\t\033[%dm NVM PROGRAMMING SUCESSFULL. Hoorayyy!\a \033[%dm\n\r",44,40);
     else  pc.printf("\n\n\r\tuh-oh.. Data written into NVM does not match data read from NVM\n\n\n\r\t\033[%dm NVM PROGRAMMING FAILED!\a \033[%dm\n\r",41,40);    
--- a/I2C_hot_read.cpp	Thu Jun 21 22:11:25 2018 +0000
+++ b/I2C_hot_read.cpp	Fri May 22 19:19:45 2020 +0000
@@ -62,7 +62,7 @@
         scl = 0;
         wait_us(delay2);
     }
-    pc.printf("\n\n\n\r\tWaiting at bit position %d ..... Monitor adc_op_clk pin\n\n\r\tPress 'q' to quit hot read : ", i);    
+    pc.printf("\n\n\n\r\tWaiting at bit position %d ..... Monitor DATA1_clk_pin pin\n\n\r\tPress 'q' to quit hot read : ", i);    
     while(temp!='q') {
         temp = pc.getc();
     }
@@ -106,8 +106,13 @@
     flag1 = 0;
     //Read Pointer to the Register that needs to be read
     while (!flag1) {
-        pc.printf("\n\n\r\tEnter Register Address in hex (0 to f): ");
+        pc.printf("\n\n\r\tEnter Register Address in hex (0 to 17): ");
         reg_ptr = get_half_byte(&flag1);
+        if (flag1 == 1) {
+            reg_ptr = reg_ptr << 4;
+            temp = get_half_byte(&flag1);
+            reg_ptr = (reg_ptr | temp);
+        }
         if (flag1==0) pc.printf(" \n\r\t\033[%dm Invalid Chracter!! No worries, let us try again \033[%dm",41,40);
     }
     //Read the bit at which hot read is to be performed
--- a/I2C_read.cpp	Thu Jun 21 22:11:25 2018 +0000
+++ b/I2C_read.cpp	Fri May 22 19:19:45 2020 +0000
@@ -4,6 +4,8 @@
 extern int frequ = 50; //unit kHz
 extern char dev_addr = 0x00; //fixed
 
+char get_byte(int *); //defined in EasyFuse_Prog.cpp
+
 char get_half_byte(int *flag) {
     char cur_char;
     cur_char = pc.getc();
@@ -34,7 +36,7 @@
     pc.printf("\n\n\n \r\tYahoooo! Lets read a Register");
     //Read the number of Registers
     while (!flag1) {
-            pc.printf("\n\n\n\r\tEnter the number of registers to read (1 - 16) and hit 'enter' key: ");
+            pc.printf("\n\n\n\r\tEnter the number of registers to read (1 - 24) and hit 'enter' key: ");
             temp = '0';
             count = 0;
             while (temp != '\r') {  //look for enter key
@@ -45,15 +47,15 @@
                     break;
                 }
             }
-            if (count >= 1 && count <= 16 && temp == '\r') flag1 = 1;
+            if (count >= 1 && count <= 24 && temp == '\r') flag1 = 1;
             else if (temp == '\r') pc.printf(" \n\r\t\033[%dm Invalid Chracter!! No worries, let us try again \033[%dm",41,40);
     }          
 Re_enter:
     flag1 = 0;
     //Read Pointer to the Register that needs to be read
     while (!flag1) {
-        pc.printf("\n\n \r\tEnter Register Address in hex (0 to f): ");
-        reg_ptr = get_half_byte(&flag1);
+        pc.printf("\n\n \r\tEnter Register Address in hex (0 to 17): ");
+        reg_ptr = get_byte(&flag1);
         if (flag1==0) pc.printf(" \033[%dm Invalid Chracter!! No worries, let us try again \033[%dm",41,40);
     }          
 
@@ -77,14 +79,14 @@
     i2c.frequency(frequ*1000);
     i2c.stop(); //add a stop after hot-read
     wait_us(10);
-    for ( ; (count > 0) && (reg_ptr < 0x10); count = count - 1) {
+    for ( ; (count > 0) && (reg_ptr < 0x18); count = count - 1) {
         i2c.start();
         flag1 = i2c.write(dev_addr); 
         if (flag1 != 1) pc.printf("\n\n\n \r\tNo Ack for dev addr :(");
         flag1 = i2c.write(reg_ptr);
         if (flag1 != 1) pc.printf("\n\n \r\tNo Ack reg pointer :(");
         i2c.stop();
-        wait_us(10);
+        wait_us(300);
         i2c.start();
         flag1 = i2c.write(dev_addr | 0x01); //lsb 1 for read
         if (flag1 != 1) pc.printf("\n\n \r\tNo Ack for dev addr :(");         
@@ -94,7 +96,7 @@
         i2c.stop();      
         if (reg_data == 0xff) pc.printf("  |\033[%dm  Register read might not be sucessfull \033[%dm ",45,40);
         reg_ptr = reg_ptr + 1;
-        wait_us(120);        
+        wait_us(300);        
     }
     pc.printf("\n\n\n \r\tYeahhh! Register Read Complete\n\n\r");
     i2c.~I2C();   
--- a/I2C_write.cpp	Thu Jun 21 22:11:25 2018 +0000
+++ b/I2C_write.cpp	Fri May 22 19:19:45 2020 +0000
@@ -16,8 +16,13 @@
     //Read Register Pointer
     pc.printf("\n\n\n \r\tYahoooo! Lets program a Register");
     while (!flag1) {
-        pc.printf("\n\n\n \r\tEnter Register Address in hex (0 to f): ");
+        pc.printf("\n\n\n \r\tEnter Register Address in hex (0 to 17): ");
         reg_ptr = get_half_byte(&flag1);
+        if (flag1 == 1) {
+            reg_ptr = reg_ptr << 4;
+            temp = get_half_byte(&flag1);
+            reg_ptr = (reg_ptr | temp);
+        }
         if (flag1==0) pc.printf(" \033[%dm Invalid Chracter!! No worries, let us try again \033[%dm",41,40);
     } 
     //Read Register Data
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/adc_test.cpp	Fri May 22 19:19:45 2020 +0000
@@ -0,0 +1,541 @@
+#include "mbed.h"
+
+extern Serial pc;
+extern int frequ; //unit kHz
+extern char dev_addr; //fixed
+
+char get_half_byte(int *); //defined in I2C_read.cpp
+char get_byte(int *); //defined in EasyFuse_Prog.cpp
+void i2c_write_fn(I2C *, char , char );//defined in Easy Fuse Prog
+char i2c_read_fn(I2C *, char ); //defined in Easy Fuse Prog
+void read_bit(int ); // defined in I2C_hot_read
+
+void i2c_hotread_fn(I2C *i2c_obj, char ptr, int bit_position) {
+    int flag1;
+    char temp;
+    wait_us(10);
+    (*i2c_obj).start();
+    flag1 = (*i2c_obj).write(dev_addr); 
+    if (flag1 != 1) pc.printf("\n\n\n\r\tNo Ack for dev addr :(");
+    flag1 = (*i2c_obj).write(ptr);
+    if (flag1 != 1) pc.printf("\n\n\r\tNo Ack reg pointer :(");
+    (*i2c_obj).stop();
+    wait_us(300);
+    pc.printf("\n\n\n\r\tPress 's' to start hot read : ");    
+    while(temp!='s') {
+        temp = pc.getc();
+    }
+    //(*i2c_obj).start();
+    //flag1 = (*i2c_obj).write(dev_addr | 0x01); //lsb 1 for read
+    //if (flag1 != 1) pc.printf("\n\n\r\tNo Ack for dev addr :(");         
+    read_bit(bit_position); // hot-read function
+    (*i2c_obj).stop();    
+}
+
+void draw_sig_line() {
+    pc.printf("\033[%dm ", 33);//change text color to yellow
+    pc.printf("\n\n\n\n\r   \304");   
+    for ( int i = 0; i<80; i++) pc.printf("\304");
+    pc.printf("\033[%dm ", 32);//change text color to green
+    pc.printf("\n\n\r");
+} 
+
+void get_new_val (char *reg_val06) {
+    int flag1 = 0;
+    pc.printf("\n\n\n\r\t\033[%dmChoose a Range trim from the above table (\033[%dmRecommended values are highlighted in red\033[%dm)",36,31,36);
+    while (!flag1) {
+        pc.printf("\n\n\r\tEnter new Range trim in hex (00 to ff): 0x");
+        *reg_val06 = get_byte(&flag1);
+        if ( *reg_val06 > 0x1F ) flag1 = 0; //Range trim is only 5 bits
+        if (flag1 == 0) pc.printf(" \033[%dm Invalid Chracter!! No worries, let us try again \033[%dm",41,40);
+    }
+    pc.printf("\033[%dm",32);
+}
+
+void adc_meas (I2C *i2c_obj, char count, char lsb) {
+    char adc_op;
+    int voc;
+    char temp;
+    
+    voc = lsb * count;
+    
+    temp = 0;
+    pc.printf("\n\n\n\r\t\033[%dmSet Voc = %d mV (%d mV x %d). Press 'c' to continue: \033[%dm", 36, voc, lsb, count, 32);  
+    while ( temp!='c') {
+        temp = pc.getc();
+    }
+    i2c_write_fn(i2c_obj,0x14,count - 2); //Writing starting value in ADC
+    wait_ms(5);                    
+    i2c_write_fn(i2c_obj,0x0D,0x03); //tm_adc_en = tm_adc_go = 1; start conversion
+    wait_ms(20); //to make sure conversion is complete
+    adc_op = i2c_read_fn(i2c_obj,0x14);
+    pc.printf("\n\n\n\r\t\033[%dmadc_op%d_%dmv = 0x%2.2X\033[%dm", 45, count, lsb, adc_op, 40);//displaying in Magenta background    
+    return;         
+}
+    
+
+void adc_test() {
+    
+    I2C i2c(p9,p10);
+    char temp;
+    char reg_val06;
+    char reg_val06_max = 0x1F; //for debug, change to 0x1F    
+    char reg_val05;
+    char reg_val05_strt;
+    char reg_val04;
+    char reg_val07;  
+    char adc_op31[32];
+    char adc_op223[32];
+    char d_adc_op192[32] = {0x00};
+    char adc_op8 = 0x07;
+    char adc_op8_old;
+    char adc_op8_disp[32];    
+    char d_adc_op = 0x00;
+    char d_adc_op_old = 0x00;    
+    int i = 0;
+    int x = 0;
+    
+    i2c.frequency((frequ/2)*1000);
+    LPC_PINCON->PINMODE_OD0 = (LPC_PINCON->PINMODE_OD0  | 0x0003); // To make p9 & P10 open_drain
+    i2c.stop(); //add a stop after hot-read
+
+    pc.printf("\033[%dm ", 31);//change text color to red    
+    pc.printf("\n\n\n\r\tStarting adc test \n\r\t");
+    for ( i = 0; i<18; i++) pc.printf("\334");  
+    pc.printf("\n\n\r\033[%dm", 32);//change text color to blue        
+
+    i2c_write_fn(&i2c,0x17,0x03); //dmux op = adc_conversion. DATA2_CLK will be high when adc conversion is ongoing       
+
+    pc.printf("\033[%dm", 34);//change text color to blue
+    pc.printf("\n\n\n\r\tConditions: \n\r\t");
+    for ( i = 0; i<13; i++) pc.printf("\304");    
+    pc.printf("\n\n\r\t  1. ASIC in test-mode "); 
+    pc.printf("\n\n\r\t  2. Short input Rin (5ohms) resistor ");            
+    pc.printf("\n\n\r\t  3. Please Monitor DATA2_CLK on oscilloscope. DATA2_CLK = 'adc_conversion'");
+    pc.printf("\n\n\r\t  4. Positive input polarity");        
+    pc.printf("\033[%dm ", 32);//change text color to green
+    temp = 0;
+    pc.printf("\n\n\n\r\t\033[%dmPress 'c' to continue: \033[%dm",36,32);  
+    while ( temp!='c') {
+        temp = pc.getc();
+    }     
+    i2c_write_fn(&i2c,0x0F,0x00); //Disabling Flyback
+    
+    //reg_val06 = i2c_read_fn(&i2c,0x06); //reading the initial range trim
+    i2c_write_fn(&i2c,0x0D,0x01); //tm_adc_en = 1
+
+    draw_sig_line();
+    
+    //*****************************************************************
+    //******   ADC Range trim test
+    //*****************************************************************
+        
+    pc.printf("\033[%dm ", 31);//change text color to red 
+    pc.printf("\n\n\n\r\tStarting Range trim \n\r\t");  
+    for ( i = 0; i<19; i++) pc.printf("\315");
+    pc.printf("\033[%dm ", 32);//change text color to green     
+
+    temp = 0;
+    pc.printf("\n\n\n\r\t\033[%dmSet Voc = 63mV (2mV x 31.5). Press 'c' to continue: \033[%dm",36,32);  
+    while ( temp!='c') {
+        temp = pc.getc();
+    } 
+    //Making sure input polarity detector outputs are correct.
+    i2c_write_fn(&i2c,0x10,0x10); //tm_PolComp_en = 1
+    wait_ms(1);
+    i2c_write_fn(&i2c,0x10,0x00); //tm_PolComp_en = 0
+    pc.printf("\n\n\r\tFinding ADC outputs for all adc range with input = 63mV");    
+    //Finding ADC outputs for all adc range with input = 63mV
+    
+    for ( reg_val06 = 0x00; reg_val06 <= reg_val06_max; reg_val06 = (reg_val06 + 1)) {
+        
+        i2c_write_fn(&i2c,0x06,reg_val06); 
+        
+        //Write initial value into ADC register
+        if ( reg_val06 == 0x00 ) i2c_write_fn(&i2c,0x14,0x64); //starting value = 100. ADC reading will reduce as range trim increases
+        else  i2c_write_fn(&i2c,0x14,adc_op31[(reg_val06 - 1)]); //starting with previous measurement value
+
+        wait_ms(5);                    
+        i2c_write_fn(&i2c,0x0D,0x03); //tm_adc_en = tm_adc_go = 1; start conversion
+        wait_ms(20); //to make sure conversion is complete
+        adc_op31[reg_val06] = i2c_read_fn(&i2c,0x14);
+        pc.printf(".");        
+    }                
+
+    temp = 0;
+    pc.printf("\n\n\n\r\t\033[%dmSet Voc = 447mV (2mV x 223.5). Press 'c' to continue: \033[%dm",36,32);  
+    while ( temp!='c') {
+        temp = pc.getc();
+    } 
+    pc.printf("\n\n\r\tFinding ADC outputs for all adc range with input = 447mV");       
+    for ( reg_val06 = 0x00; reg_val06 <= reg_val06_max; reg_val06 = (reg_val06 + 1)) {
+        
+        i2c_write_fn(&i2c,0x06,reg_val06); 
+        
+        //Write initial value into ADC register
+        if ( reg_val06 == 0x00 ) i2c_write_fn(&i2c,0x14,0x64); //starting value = 100. ADC reading will reduce as range trim increases
+        else  i2c_write_fn(&i2c,0x14,adc_op223[(reg_val06 - 1)]); //starting with previous measurement value
+
+        wait_ms(5);                    
+        i2c_write_fn(&i2c,0x0D,0x03); //tm_adc_en = tm_adc_go = 1; start conversion
+        wait_ms(20); //to make sure conversion is complete
+        adc_op223[reg_val06] = i2c_read_fn(&i2c,0x14);
+        d_adc_op192[reg_val06] = adc_op223[reg_val06] - adc_op31[reg_val06];
+        pc.printf("."); 
+    }
+    //Printing results
+    pc.printf("\n\n\n\r\t\332");
+    for ( i = 0; i < 4; i++ ) {
+        for ( x = 0; x < 23; x++ ) pc.printf("\304");
+        pc.printf("\302");
+    }
+   
+    pc.printf("\n\r\t\263\t\033[%dmRange Trim\033[%dm\t\263\t\033[%dmadc_op31\033[%dm\t\263\t\033[%dmadc_op223\033[%dm\t\263\t\033[%dmdelta_adc_op\033[%dm\t\263",34,32,34,32,34,32,34,32);
+
+    //d_adc_op192[3] = 0xC0; //added for debug. Remove!
+
+    for ( reg_val06 = 0x00; reg_val06 <= reg_val06_max; reg_val06 = (reg_val06 + 1)) {
+
+        pc.printf("\n\r\t\303");
+        for ( i = 0; i < 4; i++ ) {
+            for ( x = 0; x < 23; x++ ) pc.printf("\304");
+            if ( i < 3) pc.printf("\305");
+            else  pc.printf("\264");
+        }
+        if ( (d_adc_op192[reg_val06] > 0xBE) && (d_adc_op192[reg_val06] < 0xC2))
+                    pc.printf("\n\r\t\263\t\033[%dm   0x%2.2X\t\t\263\t   0x%2.2X\t\t\263\t   0x%2.2X\t\t\263\t    0x%2.2X\033[%dm\t\263", 31, reg_val06, adc_op31[reg_val06], adc_op223[reg_val06], d_adc_op192[reg_val06], 32);
+        else 
+            pc.printf("\n\r\t\263\t   0x%2.2X\t\t\263\t   0x%2.2X\t\t\263\t   0x%2.2X\t\t\263\t    0x%2.2X\t\263", reg_val06, adc_op31[reg_val06], adc_op223[reg_val06], d_adc_op192[reg_val06]);             
+    }
+
+    pc.printf("\n\r\t\300");
+    for ( i = 0; i < 4; i++ ) {
+        for ( x = 0; x < 23; x++ ) pc.printf("\304");
+        if ( i < 3) pc.printf("\301");
+        else  pc.printf("\331");
+    }
+       
+    get_new_val(&reg_val06);      
+    i2c_write_fn(&i2c,0x06,reg_val06);
+    
+
+    pc.printf("\n\n\n\r\t\033[%dmFinal Range trim = 0x%2.2X\033[%dm ", 45, reg_val06, 40); 
+
+    pc.printf("\n\n\n\r\t\033[%dmDo you want to continue with offset trim. Press ",36);  
+    pc.printf("\n\n\r\t  'c' - Continue ");
+    pc.printf("\n\n\r\t  'e' - Exit ");
+    pc.printf("\n\n\n\r\tYour Selection : \033[%dm",32);                          
+    temp = 0;
+    while ( temp!='c' && temp!='e') {
+        temp = pc.getc();
+    }
+    if (temp == 'e') return;
+
+    //*****************************************************************
+    //******   ADC Offset trim test
+    //*****************************************************************    
+
+
+    draw_sig_line();
+    
+    pc.printf("\033[%dm ", 31);//change text color to red 
+    pc.printf("\n\n\n\n\r\tStarting Offset trim \n\r\t");  
+    for ( i = 0; i<19; i++) pc.printf("\315");
+    pc.printf("\033[%dm ", 32);//change text color to green   
+
+    temp = 0;
+    pc.printf("\n\n\n\r\t\033[%dmSet Voc = 16mV (2mV x 8). Press 'c' to continue: \033[%dm",36,32);  
+    while ( temp!='c') {
+        temp = pc.getc();
+    }
+    // determine trim direction
+ 
+    i2c_write_fn(&i2c,0x14,adc_op8); //Writing starting value in ADC
+    pc.printf("\n\n\r\tAbout to start ADC conversion. 0 -> 1 -> 0 transition should be seen on DATA2_CLK");
+    wait_ms(5);                    
+    i2c_write_fn(&i2c,0x0D,0x03); //tm_adc_en = tm_adc_go = 1; start conversion
+    wait_ms(20); //to make sure conversion is complete
+    adc_op8 = i2c_read_fn(&i2c,0x14);        
+    pc.printf("\n\n\r\tadc_op8 = 0x%2.2X ",adc_op8);
+
+    temp = 0;    
+    if (adc_op8 == 0x08) {
+        temp = 1; // no trimming procedure
+        pc.printf("\n\n\r\tNo Trimming Required. Woohoo");
+        reg_val05 = i2c_read_fn(&i2c,0x05);
+        adc_op8_disp[reg_val05] = adc_op8; // for displaying
+    }
+    
+  
+    adc_op8_old = adc_op8; //storing previous value in case the trim loop is not entered
+    
+              
+    if (adc_op8_old < 0x08) {
+        pc.printf("\n\n\r\tadc_op08 < 0x08; need to perform positive trim");
+        reg_val05 = 0x10;
+        adc_op8 = 0x10;//decimal 16. Set for proper function of the trim procedure      
+    }
+    
+    if (adc_op8_old > 0x08) {
+        pc.printf("\n\n\r\tadc_op08 > 0x08; need to perform negative trim");
+        reg_val05 = 0x00;
+        //adc_op8 = 0x00;
+    }
+    
+    reg_val05_strt = reg_val05; //storing the starting value
+    
+    //Start trim
+
+    while (temp != 1) {
+        i2c_write_fn(&i2c,0x05,reg_val05); //programming offset trim
+        adc_op8_old = adc_op8; //storing previous value
+        i2c_write_fn(&i2c,0x14,adc_op8); //Writing starting value in ADC
+        wait_ms(5);                    
+        i2c_write_fn(&i2c,0x0D,0x03); //tm_adc_en = tm_adc_go = 1; start conversion
+        wait_ms(20); //to make sure conversion is complete
+        adc_op8 = i2c_read_fn(&i2c,0x14);
+        adc_op8_disp[reg_val05] = adc_op8; // for displaying 
+               
+        if ((adc_op8 == 0x08) || ((adc_op8 > 0x08) && (adc_op8_old < 0x08)) || ((adc_op8 < 0x08) && (adc_op8_old > 0x08))) {
+          //  pc.printf("\n\n\r\tI'm exiting.");
+          //  pc.printf("\n\r\t\t\263\t adc_op8 =  0x%2.2X\t\t\263\t  adc_op8_old = 0x%2.2X\t\t\263 ", adc_op8, adc_op8_old);
+             break; // exit loop
+        }
+        if ((reg_val05 == 0x0F) || (reg_val05 == 0x1F)) {
+            pc.printf("\n\n\r\tTest Failed."); 
+            break; // exit loop
+        }
+        reg_val05 = reg_val05 + 0x01;
+      //  pc.printf("\n\r\t\t\263\t reg_val05 =  0x%2.2X\t\t\263\t  adc_op8 = 0x%2.2X\t\t\263 ", reg_val05, adc_op8);
+                                           
+    }
+    //Printing Result table
+
+    pc.printf("\n\n\n\r\t\t\332");
+    for ( i = 0; i < 2; i++ ) {
+        for ( x = 0; x < 23; x++ ) pc.printf("\304");
+        pc.printf("\302");
+    }    
+    pc.printf("\n\r\t\t\263\t\033[%dmOffset Trim\033[%dm\t\263\t\033[%dmadc_op8\033[%dm\t\t\263", 34, 32, 34, 32);    
+    for ( temp = reg_val05_strt; temp <= reg_val05; temp = (temp + 1) ) {
+           
+        pc.printf("\n\r\t\t\303");
+        for ( i = 0; i < 2; i++ ) {
+            for ( x = 0; x < 23; x++ ) pc.printf("\304");
+            if ( i < 1) pc.printf("\305");
+            else  pc.printf("\264");
+        }
+        pc.printf("\n\r\t\t\263\t  0x%2.2X\t\t\263\t  0x%2.2X\t\t\263 ", temp, adc_op8_disp[temp]);
+    }
+        
+    pc.printf("\n\r\t\t\300");
+    for ( i = 0; i < 2; i++ ) {
+        for ( x = 0; x < 23; x++ ) pc.printf("\304");
+        if ( i < 1) pc.printf("\301");
+        else  pc.printf("\331");
+    }
+        
+    if (adc_op8 > 0x08) d_adc_op = adc_op8 - 0x08;
+    else d_adc_op = 0x08 - adc_op8;
+
+    if (adc_op8_old > 0x08) d_adc_op_old = adc_op8_old - 0x08;
+    else d_adc_op_old = 0x08 - adc_op8_old; 
+    
+    if (d_adc_op > d_adc_op_old) {
+        reg_val05 = reg_val05 - 0x01;
+        i2c_write_fn(&i2c,0x05,reg_val05); //programming offset trim
+    }
+    pc.printf("\n\n\n\r\t\033[%dmFinal Offset trim = 0x%2.2X\033[%dm", 45, reg_val05, 40);//displaying in Magenta background
+    
+    pc.printf("\n\n\n\r\t\033[%dmDo you want to continue with rest of the adc test. Press ",36);  
+    pc.printf("\n\n\r\t  'c' - Continue ");
+    pc.printf("\n\n\r\t  'e' - Exit ");
+    pc.printf("\n\n\n\r\tYour Selection : \033[%dm",32);                          
+    temp = 0;
+    while ( temp!='c' && temp!='e') {
+        temp = pc.getc();
+    }
+    if (temp == 'e') return;    
+    draw_sig_line();
+        
+    //*****************************************************************
+    //******   measuring adc outputs different inputs
+    //*****************************************************************
+    
+    pc.printf("\033[%dm ", 31);//change text color to red 
+    pc.printf("\n\n\n\n\r\tMeasure ADC output for different inputs \n\r\t");  
+    for ( i = 0; i<39; i++) pc.printf("\315");
+    pc.printf("\033[%dm ", 32);//change text color to green 
+
+    adc_meas(&i2c,16,2); //adc measurement for 16 x 2mV
+    adc_meas(&i2c,24,2);    
+    adc_meas(&i2c,32,2);
+    adc_meas(&i2c,64,2);
+    adc_meas(&i2c,97,2);
+    adc_meas(&i2c,128,2);
+    adc_meas(&i2c,196,2);
+    adc_meas(&i2c,250,2);
+    
+    pc.printf("\n\n\n\r\t\033[%dmDo you want to continue with rest of the adc test. Press ",36);  
+    pc.printf("\n\n\r\t  'c' - Continue ");
+    pc.printf("\n\n\r\t  'e' - Exit ");
+    pc.printf("\n\n\n\r\tYour Selection : \033[%dm",32);                          
+    temp = 0;
+    while ( temp!='c' && temp!='e') {
+        temp = pc.getc();
+    }
+    if (temp == 'e') return;    
+    draw_sig_line();     
+    
+    //*****************************************************************
+    //******   measuring adc outputs different Range Settings
+    //*****************************************************************
+        
+    pc.printf("\033[%dm ", 31);//change text color to red 
+    pc.printf("\n\n\n\n\r\tMeasure ADC output for different range settings \n\r\t");  
+    for ( i = 0; i<47; i++) pc.printf("\315");
+    pc.printf("\033[%dm ", 32);//change text color to green 
+
+    reg_val04 = i2c_read_fn(&i2c,0x04); //Bits [3:2] of 0x04 contains range settings
+    
+    i2c_write_fn(&i2c,0x04,(reg_val04 & 0xF3)); //Range = 00 => 1mV
+    adc_meas(&i2c,128,1); //adc measurement for 16 x 2mV
+    
+    i2c_write_fn(&i2c,0x04,(reg_val04 | 0x0C)); //Range = 11 => 4mV
+    adc_meas(&i2c,32,4); //adc measurement for 32 x 4mV    
+
+    i2c_write_fn(&i2c,0x04,reg_val04); //Setting back the original value 
+    
+    pc.printf("\n\n\n\r\t\033[%dmDo you want to continue with rest of the adc test. Press ",36);  
+    pc.printf("\n\n\r\t  'c' - Continue ");
+    pc.printf("\n\n\r\t  'e' - Exit ");
+    pc.printf("\n\n\n\r\tYour Selection : \033[%dm",32);                          
+    temp = 0;
+    while ( temp!='c' && temp!='e') {
+        temp = pc.getc();
+    }
+    if (temp == 'e') return;    
+    draw_sig_line(); 
+
+    //*****************************************************************
+    //******   measuring adc outputs for negative inputs
+    //*****************************************************************    
+
+    pc.printf("\033[%dm ", 31);//change text color to red 
+    pc.printf("\n\n\n\n\r\tMeasure ADC output for negative inputs \n\r\t");  
+    for ( i = 0; i<39; i++) pc.printf("\315");
+    pc.printf("\033[%dm ", 32);//change text color to green
+    
+    pc.printf("\033[%dm", 34);//change text color to blue
+    pc.printf("\n\n\n\r\tInstructions: \n\r\t");
+    for ( i = 0; i<13; i++) pc.printf("\304");    
+    pc.printf("\n\n\r\t  1. Set Voc = 0. Wait for a second "); 
+    pc.printf("\n\n\r\t  2. Change the input connnection such that the input is negative ");            
+    pc.printf("\n\n\r\t  3. Increase Voc to 450mV");
+    pc.printf("\033[%dm ", 32);//change text color to green
+    temp = 0;
+    pc.printf("\n\n\n\r\t\033[%dmPress 'c' to continue: \033[%dm",36,32);  
+    while ( temp!='c') {
+        temp = pc.getc();
+    }
+    //Making sure input polarity detector outputs are correct.
+    i2c_write_fn(&i2c,0x10,0x10); //tm_PolComp_en = 1
+    pc.printf("\n\n\n\r\tPolarity comparitor was enabled to have correct outputs (vinB_high = 1)");  
+    wait_ms(1);
+    i2c_write_fn(&i2c,0x10,0x00); //tm_PolComp_en = 0 
+    pc.printf("\n\n\r\tPolarity comparitor disabled");
+    adc_meas(&i2c,16,2); //adc measurement for 16 x 2mV  
+    adc_meas(&i2c,128,2);
+    adc_meas(&i2c,250,2);
+
+    pc.printf("\n\n\n\r\t\033[%dmDo you want to continue to ADC ACP mode test. Press ",36);  
+    pc.printf("\n\n\r\t  'c' - Continue ");
+    pc.printf("\n\n\r\t  'e' - Exit ");
+    pc.printf("\n\n\n\r\tYour Selection : \033[%dm",32);                          
+    temp = 0;
+    while ( temp!='c' && temp!='e') {
+        temp = pc.getc();
+    }
+    if (temp == 'e') return;    
+    draw_sig_line(); 
+
+    //*****************************************************************
+    //******   Verify ADC operation for ACP mode
+    //*****************************************************************    
+
+    pc.printf("\033[%dm ", 31);//change text color to red 
+    pc.printf("\n\n\n\n\r\tVerify ADC operation in ACP mode \n\r\t");  
+    for ( i = 0; i<32; i++) pc.printf("\315");
+    pc.printf("\033[%dm ", 32);//change text color to green
+    reg_val07 = i2c_read_fn(&i2c,0x07); //reading the initial pmos off setting
+    i2c_write_fn(&i2c,0x07,(reg_val07 | 0x0F)); //PMOS_off = 4’hF → ACP mode. In this mode ADC operates as a comparator checking input against 250*lsb
+    pc.printf("\n\n\r\tCSI066a switched to ACP mode by setting PMOS_off = 4'hF");          
+
+    pc.printf("\033[%dm", 34);//change text color to blue
+    pc.printf("\n\n\n\r\tInstructions: \n\r\t");
+    for ( i = 0; i<13; i++) pc.printf("\304");    
+    pc.printf("\n\n\r\t  1. Set Voc to 480mV (240 x 2mV)");            
+    pc.printf("\033[%dm ", 32);//change text color to green 
+    temp = 0;
+    pc.printf("\n\n\n\r\t\033[%dmPress 'c' to continue: \033[%dm",36,32);  
+    while ( temp!='c') {
+        temp = pc.getc();
+    }    
+    pc.printf("\n\n\n\r\tPerforming hot-read on adc_comp_out (0x13[3])"); 
+    pc.printf("\033[%dm", 34);//change text color to blue
+    pc.printf("\n\n\n\r\tInstructions: \n\r\t");
+    for ( i = 0; i<13; i++) pc.printf("\304");    
+    pc.printf("\n\n\r\t  1. Monitor DATA1_clk on oscilloscope. DATA1_clk should be low in the beginning ");           
+    pc.printf("\n\n\r\t  2. Ramp Voc from 480mV --> 520mV (240lsb --> 260lsb). Stop when DATA1_CLK transitions from 0 --> 1 ");
+    pc.printf("\n\n\r\t  3. Record Voc / 2mV. Record it as\033[%dm ACP_neg_fb_dis \033[%dm", 47, 40); //background white    
+    pc.printf("\033[%dm ", 32);//change text color to green     
+      
+    i2c_hotread_fn(&i2c,0x13,3); //Performing hot-read on [3] of 0x13  
+
+    pc.printf("\033[%dm", 34);//change text color to blue
+    pc.printf("\n\n\n\r\tInstructions: \n\r\t");
+    for ( i = 0; i<13; i++) pc.printf("\304");    
+    pc.printf("\n\n\r\t  1. Reduce Voc to 0V. "); 
+    pc.printf("\n\n\r\t  2. Switch input connections such that input is positive");
+    pc.printf("\n\n\r\t  3. Set Voc to 480mV (240 x 2mV)");                 
+    pc.printf("\033[%dm ", 32);//change text color to green 
+    temp = 0;
+    pc.printf("\n\n\n\r\t\033[%dmPress 'c' to continue: \033[%dm",36,32);  
+    while ( temp!='c') {
+        temp = pc.getc();
+    }
+    i2c.~I2C();
+    I2C i2c2(p9,p10); //Creating new object after hot-read
+    i2c2.frequency((frequ/2)*1000);
+    LPC_PINCON->PINMODE_OD0 = (LPC_PINCON->PINMODE_OD0  | 0x0003); // To make p9 & P10 open_drain       
+    i2c2.stop(); //add a stop after hot-read
+    //Making sure input polarity detector outputs are correct.
+    i2c_write_fn(&i2c2,0x10,0x10); //tm_PolComp_en = 1
+    pc.printf("\n\n\n\r\tPolarity comparitor was enabled to have correct outputs (vinB_high = 1)");  
+    wait_ms(1);
+    i2c_write_fn(&i2c2,0x10,0x00); //tm_PolComp_en = 0 
+    pc.printf("\n\n\r\tPolarity comparitor disabled");
+    
+    pc.printf("\n\n\n\r\tPerforming hot-read on adc_comp_out (0x13[3])");
+
+    pc.printf("\033[%dm", 34);//change text color to blue
+    pc.printf("\n\n\n\r\tInstructions: \n\r\t");
+    for ( i = 0; i<13; i++) pc.printf("\304");    
+    pc.printf("\n\n\r\t  1. Monitor DATA1_CLk on the oscillscope "); 
+    pc.printf("\n\n\r\t  2. DATA1_CLk should be low initially");            
+    pc.printf("\n\n\r\t  3. Ramp Voc from 480mV --> 520mV (240lsb --> 260lsb). Stop when DATA1_CLK transitions from 0 --> 1 ");
+    pc.printf("\n\n\r\t  4. Record Voc / 2mV. Record it as\033[%dm ACP_pos_fb_dis \033[%dm", 47, 40); //yellow background   
+    pc.printf("\033[%dm ", 32);//change text color to green 
+
+    i2c_hotread_fn(&i2c2,0x13,3); //Performing hot-read on [3] of 0x13 
+
+    i2c2.~I2C();
+    I2C i2c3(p9,p10); //Creating new object after hot-read
+    i2c3.frequency((frequ/2)*1000);
+    LPC_PINCON->PINMODE_OD0 = (LPC_PINCON->PINMODE_OD0  | 0x0003); // To make p9 & P10 open_drain       
+    i2c3.stop(); //add a stop after hot-read     
+    i2c_write_fn(&i2c3,0x07,reg_val07); //Restoring Reg 0x07 value
+    pc.printf("\n\n\n\r\t\033[%dmADC Test finally done!.. Phew\033[%dm", 45, 40);//displaying in Magenta background    
+                         
+}                   
+    
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/csi066_rev0p2.lib	Fri May 22 19:19:45 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/teams/lab_cactus/code/csi059/#3e8c16b6620c
--- a/main.cpp	Thu Jun 21 22:11:25 2018 +0000
+++ b/main.cpp	Fri May 22 19:19:45 2020 +0000
@@ -3,13 +3,14 @@
 Serial pc(USBTX, USBRX);
 const int frequ = 50; //unit kHz
 const char dev_addr = 0x00; //fixed
-const char version[4] = "0.0";
+const char version[4] = "0.3";
 
 void testmode(void);
 void i2c_read(void);
 void i2c_write(void);
 void EasyFuse_prog(void);
 void hot_read(void);
+void adc_test(void);
   
 void clear_screen() {
     int i=0;
@@ -22,11 +23,11 @@
     for ( i = 0; i<46; i++) pc.printf("\304");
     pc.printf("\304\302");            
     pc.printf("\n\r     \263\t\t\t\t\t\t      \263");
-    pc.printf("\n\r     \263   \334\334\334\t \334\334\334\t\334\334\334\t \334\334 \t\334\334\334\334\t \334\334   \263");
-    pc.printf("\n\r     \263  \333    \t\333   \t \333 \t\333  \333\t\333   \t\333  \333  \263");
-    pc.printf("\n\r     \263  \333    \t\337\337\334\334\t \333 \t\333  \333\t\337\337\337\334\t\337\334\334\333  \263");
-    pc.printf("\n\r     \263  \333    \t   \333\t \333 \t\333  \333\t   \333\t   \333  \263");
-    pc.printf("\n\r     \263   \337\337\337\t\337\337\337 \t\337\337\337\t \337\337 \t\337\337\337 \t\337\337\337   \263");
+    pc.printf("\n\r     \263   \334\334\334\t \334\334\334\t\334\334\334\t \334\334 \t \334\334 \t \334\334   \263");
+    pc.printf("\n\r     \263  \333    \t\333   \t \333 \t\333  \333\t\333   \t\333     \263");
+    pc.printf("\n\r     \263  \333    \t\337\337\334\334\t \333 \t\333  \333\t\333\337\337\334\t\333\337\337\334  \263");
+    pc.printf("\n\r     \263  \333    \t   \333\t \333 \t\333  \333\t\333  \333\t\333  \333  \263");
+    pc.printf("\n\r     \263   \337\337\337\t\337\337\337 \t\337\337\337\t \337\337 \t \337\337 \t \337\337   \263");
     pc.printf("\n\r     \263\t\t\t\t\t\t      \263\n\r     \300\304");
     for ( i = 0; i<46; i++) pc.printf("\304");
     pc.printf("\304\331");
@@ -56,6 +57,8 @@
     pc.printf("\n\r\t\263\t\t\t\t\t\t\263");
     pc.printf("\n\r\t\263  Press 'h' : to Perform Hot Register Read  \t\263");
     pc.printf("\n\r\t\263\t\t\t\t\t\t\263"); 
+    pc.printf("\n\r\t\263  Press 'a' : to Perform ADC tests          \t\263");
+    pc.printf("\n\r\t\263\t\t\t\t\t\t\263");    
     pc.printf("\n\r\t\263  Press 'c' : to clear screen               \t\263");
     pc.printf("\n\r\t\263\t\t\t\t\t\t\263"); 
     pc.printf("\n\r\t\300\304");
@@ -97,7 +100,7 @@
     int flag = 0;
     while (flag == 0) {
         current_char = pc.getc();
-        if ( current_char == 't' || current_char == 'r' || current_char == 'w' || current_char == 'p' || current_char == 'c' || current_char == 'h' ) flag = 1;
+        if ( current_char == 't' || current_char == 'r' || current_char == 'w' || current_char == 'p' || current_char == 'c' || current_char == 'h' || current_char == 'a' ) flag = 1;
         else {
             pc.printf(" \033[%dm Invalid Chracter!! No worries, let us try again \033[%dm",41,40); 
             pc.printf("\n\n\r\tYour Selection : "); 
@@ -152,10 +155,14 @@
                 clear_screen_ques();
                 break;
                                 
+            case 'a':
+                adc_test();
+                clear_screen_ques();
+                break; 
+                                                                          
             case 'c':
                 clear_screen();
-                break;                                                           
-        
+                break;           
         }      
     }//end of while     
 }
\ No newline at end of file