summaryrefslogtreecommitdiff
path: root/board/wmt/wmt_battery/gauge/upi/uG31xx_API_Measurement.c
blob: 903b8c171d80c7177b31a50b8398026755fb0575 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
/**
 * @filename  uG31xx_API_Measurement.cpp
 *
 *  guG31xx measurement API
 *
 * @author  AllenTeng <allen_teng@upi-semi.com>
 */

#include "stdafx.h"     //windows need this??
#include "uG31xx_API.h"

//#define MEAS_FAKE_INT_TEMP
#ifdef  MEAS_FAKE_INT_TEMP
  #define MEAS_FAKE_INT_TEMP_OFFSET     (200)
#endif  ///< end of MEAS_FAKE_INT_TEMP

typedef struct MeasDataInternalST {

  MeasDataType *info;
  
  _meas_s16_ adc1CodeT25V100;
  _meas_s16_ adc1CodeT25V200;
  _meas_s16_ adc1CodeT80V100;
  _meas_s16_ adc1CodeT80V200;

  _meas_s16_ adc2CodeT25V100;
  _meas_s16_ adc2CodeT25V200;
  _meas_s16_ adc2CodeT80V100;
  _meas_s16_ adc2CodeT80V200;

  _meas_u32_ currTime;

  _meas_u16_ codeBat1;
  _meas_s16_ codeCurrent;
  _meas_u16_ codeIntTemperature;
  _meas_u16_ codeExtTemperature;
  _meas_s16_ codeCharge;
  _meas_u16_ codeCounter;
  _meas_s16_ ccOffset;

#if defined(uG31xx_OS_ANDROID)
  } __attribute__ ((aligned(4))) MeasDataInternalType;
#else   ///< else of defined(uG31xx_OS_ANDROID)
  } MeasDataInternalType;
#endif  ///< end of defined(uG31xx_OS_ANDROID)

typedef struct AdcDeltaCodeMappingST {
  _meas_s32_ Adc1V100;
  _meas_s32_ Adc1V200;
  _meas_s32_ Adc2V100;
  _meas_s32_ Adc2V200;
#if defined(uG31xx_OS_ANDROID)
}__attribute__((aligned(4))) AdcDeltaCodeMappingType;
#else   ///< else of defined(uG31xx_OS_ANDROID)
} AdcDeltaCodeMappingType;
#endif  ///< end of defined(uG31xx_OS_ANDROID)

static AdcDeltaCodeMappingType AdcDeltaCodeMapping[] =
{
  { -12800, -25600, 1536,  0    },     ///< Index = 0
  { -12544, -25088, 1600,  128  },     ///< Index = 1
  { -13056, -26112, 1472, -128  },     ///< Index = 2
  { -12288, -24576, 1664,  256  },     ///< Index = 3
  { -13312, -26624, 1408, -256  },     ///< Index = 4
  { -12032, -24064, 1728,  384  },     ///< Index = 5
  { -13568, -27136, 1344, -384  },     ///< Index = 6
  { -11776, -23552, 1792,  512  },     ///< Index = 7
  { -13824, -27648, 1280, -512  },     ///< Index = 8
  { -11520, -23040, 1856,  640  },     ///< Index = 9
  { -14080, -28160, 1216, -640  },     ///< Index = 10
  { -11264, -22528, 1920,  768  },     ///< Index = 11
  { -14336, -28672, 1152, -768  },     ///< Index = 12
  { -11008, -22016, 1984,  896  },     ///< Index = 13
  { -14592, -29184, 1088, -896  },     ///< Index = 14
  { -10752, -21504, 2048,  1024 },     ///< Index = 15
  { -14848, -29696, 1024, -1024 },     ///< Index = 16
  { -10496, -20992, 2112,  1152 },     ///< Index = 17
  { -15104, -30208, 960,  -1152 },     ///< Index = 18
  { -10240, -20480, 2176,  1280 },     ///< Index = 19
  { -15360, -30720, 896,  -1280 },     ///< Index = 20
  { -9984,  -19968, 2240,  1408 },     ///< Index = 21
  { -15616, -31232, 832,  -1408 },     ///< Index = 22
  { -9728,  -19456, 2304,  1536 },     ///< Index = 23
  { -15872, -31744, 768,  -1536 },     ///< Index = 24
  { -9472,  -18944, 2368,  1664 },     ///< Index = 25
  { -16128, -32256, 704,  -1664 },     ///< Index = 26
  { -9216,  -18432, 2432,  1792 },     ///< Index = 27
  { -16384, -32768, 640,  -1792 },     ///< Index = 28
  { -8960,  -17920, 2496,  1920 },     ///< Index = 29
  { -16640, -33280, 576,  -1920 },     ///< Index = 30
  { 0,      0,      0,     0    },     ///< Index = 31
};

#define ADC_TEMPERATURE_GAIN_CONST            (1000)

#define ADC1_CODE_100MV_NEGATIVE              (0xFF00)
#define ADC1_CODE_200MV_NEGATIVE              (0xFE00)
#define ADC1_CP_CODE_25_100MV                 (12288)
#define ADC1_CP_CODE_25_200MV                 (24576)
#define ADC1_DELTA_CODE_25_100MV_SIGN_BIT     (1<<8)
#define ADC1_DELTA_CODE_25_200MV_SIGN_BIT     (1<<9)
#define ADC1_TEMPERATURE_GAIN_100MV           (869600)
#define ADC1_TEMPERATURE_GAIN_200MV           (-695680)

/**
 * @brief ConvertAdc1Data
 *
 *  Convert ADC1 data from OTP
 *
 * @para  obj address of MeasDataInternalType
 * @return  _UPI_NULL_
 */
void ConvertAdc1Data(MeasDataInternalType *obj)
{
  _meas_u16_ tmp16;
  _meas_s32_ tmp32;
  
  /// [AT-PM] : Get code T25 100mV ; 01/23/2013
  tmp16 = obj->info->otp->adc1DeltaCodeT25V100;
  if(tmp16 & ADC1_DELTA_CODE_25_100MV_SIGN_BIT)
  {
    tmp16 = tmp16 & (~ADC1_DELTA_CODE_25_100MV_SIGN_BIT);
    if(tmp16 != 0)
    {
      tmp16 = tmp16 + ADC1_CODE_100MV_NEGATIVE;
    }
  }
  tmp16 = tmp16 + ADC1_CP_CODE_25_100MV;
  tmp32 = (_meas_s32_)(_meas_s16_)tmp16;
  tmp32 = tmp32 + AdcDeltaCodeMapping[obj->info->otp->indexAdc1V100T25].Adc1V100;
  obj->adc1CodeT25V100 = (_meas_s16_)tmp32;
	#ifdef	UPI_UBOOT_DEBUG_MSG
		printf("[ConvertAdc1Data] adc1CodeT25V100 = %d\n", tmp32);
	#endif	///< end of UPI_UBOOT_DEBUG_MSG
  
  /// [AT-PM] : Get code T25 200mV ; 01/23/2013
  tmp16 = obj->info->otp->adc1DeltaCodeT25V200;
  if(tmp16 & ADC1_DELTA_CODE_25_200MV_SIGN_BIT)
  {
    tmp16 = tmp16 & (~ADC1_DELTA_CODE_25_200MV_SIGN_BIT);
    if(tmp16 != 0)
    {
      tmp16 = tmp16 + ADC1_CODE_200MV_NEGATIVE;
    }
  }
  tmp16 = tmp16 + ADC1_CP_CODE_25_200MV;
  tmp32 = (_meas_s32_)(_meas_s16_)tmp16;
  tmp32 = tmp32 + AdcDeltaCodeMapping[obj->info->otp->indexAdc1V200T25].Adc1V200;
  obj->adc1CodeT25V200 = (_meas_s16_)tmp32;
        #ifdef  UPI_UBOOT_DEBUG_MSG
                printf("[ConvertAdc1Data] adc1CodeT25V200 = %d\n", tmp32);
        #endif  ///< end of UPI_UBOOT_DEBUG_MSG
  
  tmp32 = (_meas_s32_)obj->info->otp->aveIT80;
  tmp32 = (tmp32 - obj->info->otp->aveIT25)*ADC_TEMPERATURE_GAIN_CONST;

  /// [AT-PM] : Get code T80 100mV ; 01/23/2013
  obj->adc1CodeT80V100 = (_meas_s16_)(tmp32/ADC1_TEMPERATURE_GAIN_100MV + obj->adc1CodeT25V100);
        #ifdef  UPI_UBOOT_DEBUG_MSG
                printf("[ConvertAdc1Data] adc1CodeT80V100 = %d\n", obj->adc1CodeT80V100);
        #endif  ///< end of UPI_UBOOT_DEBUG_MSG
  
  /// [AT-PM] : Get code T80 200mV ; 01/23/2013
  obj->adc1CodeT80V200 = (_meas_s16_)(tmp32/ADC1_TEMPERATURE_GAIN_200MV + obj->adc1CodeT25V200);
        #ifdef  UPI_UBOOT_DEBUG_MSG
                printf("[ConvertAdc1Data] adc1CodeT80V200 = %d\n", obj->adc1CodeT80V200);
        #endif  ///< end of UPI_UBOOT_DEBUG_MSG
}

#define ADC2_CODE_100MV_NEGATIVE              (0xFFC0)
#define ADC2_CODE_200MV_NEGATIVE              (0xFF80)
#define ADC2_CP_CODE_25_100MV                 (3072)
#define ADC2_CP_CODE_25_200MV                 (6144)
#define ADC2_DELTA_CODE_25_100MV_SIGN_BIT     (1<<6)
#define ADC2_DELTA_CODE_25_200MV_SIGN_BIT     (1<<7)
#define ADC2_TEMPERATURE_GAIN_100MV           (-149130)
#define ADC2_TEMPERATURE_GAIN_200MV           (-136937)

/**
 * @brief ConvertAdc2Data
 *
 *  Convert ADC2 data from OTP
 *
 * @para  obj address of MeasDataInternalType
 * @return  _UPI_NULL_
 */
void ConvertAdc2Data(MeasDataInternalType *obj)
{
  _meas_u16_ tmp16;
  _meas_s32_ tmp32;

  /// [AT-PM] : Get code T25 100mV ; 01/23/2013
  tmp16 = obj->info->otp->adc2DeltaCodeT25V100;
  if(tmp16 & ADC2_DELTA_CODE_25_100MV_SIGN_BIT)
  {
    tmp16 = tmp16 & (~ADC2_DELTA_CODE_25_100MV_SIGN_BIT);
    tmp16 = tmp16 + ADC2_CODE_100MV_NEGATIVE;
  }
  tmp16 = tmp16 + ADC2_CP_CODE_25_100MV;
  tmp32 = (_meas_s32_)(_meas_s16_)tmp16;
  tmp32 = tmp32 + AdcDeltaCodeMapping[obj->info->otp->indexAdc2V100T25].Adc2V100;
  obj->adc2CodeT25V100 = (_meas_s16_)tmp32;
        #ifdef  UPI_UBOOT_DEBUG_MSG
                printf("[ConvertAdc2Data] adc2CodeT25V100 = %d\n", tmp32);
        #endif  ///< end of UPI_UBOOT_DEBUG_MSG

  /// [AT-PM] : Get code T25 200mV ; 01/23/2013
  tmp16 = obj->info->otp->adc2DeltaCodeT25V200;
  if(tmp16 & ADC2_DELTA_CODE_25_200MV_SIGN_BIT)
  {
    tmp16 = tmp16 & (~ADC2_DELTA_CODE_25_200MV_SIGN_BIT);
    tmp16 = tmp16 + ADC2_CODE_200MV_NEGATIVE;
  }
  tmp16 = tmp16 + ADC2_CP_CODE_25_200MV;
  tmp32 = (_meas_s32_)(_meas_s16_)tmp16;
  tmp32 = tmp32 + AdcDeltaCodeMapping[obj->info->otp->indexAdc2V200T25].Adc2V200;
  obj->adc2CodeT25V200 = (_meas_s16_)tmp32;
        #ifdef  UPI_UBOOT_DEBUG_MSG
                printf("[ConvertAdc2Data] adc2CodeT25V200 = %d\n", tmp32);
        #endif  ///< end of UPI_UBOOT_DEBUG_MSG

  tmp32 = (_meas_s32_)obj->info->otp->aveIT80;
  tmp32 = (tmp32 - obj->info->otp->aveIT25)*ADC_TEMPERATURE_GAIN_CONST;

  /// [AT-PM] : Get code T80 100mV ; 01/23/2013
  obj->adc2CodeT80V100 = (_meas_s16_)(tmp32/ADC2_TEMPERATURE_GAIN_100MV + obj->adc2CodeT25V100);
        #ifdef  UPI_UBOOT_DEBUG_MSG
                printf("[ConvertAdc2Data] adc2CodeT80V100 = %d\n", obj->adc2CodeT80V100);
        #endif  ///< end of UPI_UBOOT_DEBUG_MSG

  /// [AT-PM] : Get code T80 200mV ; 01/23/2013
  obj->adc2CodeT80V200 = (_meas_s16_)(tmp32/ADC2_TEMPERATURE_GAIN_200MV + obj->adc2CodeT25V200);
        #ifdef  UPI_UBOOT_DEBUG_MSG
                printf("[ConvertAdc2Data] adc2CodeT80V200 = %d\n", obj->adc2CodeT80V200);
        #endif  ///< end of UPI_UBOOT_DEBUG_MSG
}

/**
 * @brief CalAdc1Factors
 *
 *  Calculate ADC1 gain slope and factor B
 *  Calculate ADC1 offset slope and factor O
 *
 * @para  obj address of MeasDataInternalType
 * @return  _UPI_NULL_
 */
void CalAdc1Factors(MeasDataInternalType *obj)
{
  _meas_s32_ delta25;
  _meas_s32_ delta80;
  _meas_s64_ tmp64;

  /// [AT-PM] : Calculate gain slope and factor B ; 01/23/2013
  delta25 = (_meas_s32_)obj->adc1CodeT25V200;
  delta25 = delta25 - obj->adc1CodeT25V100;
  delta80 = (_meas_s32_)obj->adc1CodeT80V200;
  delta80 = delta80 - obj->adc1CodeT80V100;

  obj->info->adc1GainSlope = delta80 - delta25;
  obj->info->adc1GainFactorB = delta25*(obj->info->otp->aveIT80) - delta80*(obj->info->otp->aveIT25);
        #ifdef  UPI_UBOOT_DEBUG_MSG
                printf("[CalAdc1Factors] adc1GainSlope / adc1GainFactorB  = %d / %d\n", obj->info->adc1GainSlope, obj->info->adc1GainFactorB);
        #endif  ///< end of UPI_UBOOT_DEBUG_MSG

  /// [AT-PM] : Calculate offset slope and factor O ; 01/23/2013
  delta25 = (_meas_s32_)obj->adc1CodeT25V100;
  delta25 = delta25*2 - obj->adc1CodeT25V200;
  delta80 = (_meas_s32_)obj->adc1CodeT80V100;
  delta80 = delta80*2 - obj->adc1CodeT80V200;

  obj->info->adc1OffsetSlope = delta80 - delta25;
  obj->info->adc1OffsetFactorO = delta25*(obj->info->otp->aveIT80) - delta80*(obj->info->otp->aveIT25);
        #ifdef  UPI_UBOOT_DEBUG_MSG
		printf("[CalAdc1Factors] adc1OffsetSlope / adc1OffsetFactorO = %d / %d\n", obj->info->adc1OffsetSlope, obj->info->adc1OffsetFactorO);
        #endif  ///< end of UPI_UBOOT_DEBUG_MSG

  /// [AT-PM] : Calculate current ADC1 gain ; 01/23/2013
  tmp64 = (_meas_s64_)obj->info->adc1GainSlope;
  tmp64 = tmp64*(obj->codeIntTemperature) + obj->info->adc1GainFactorB;
  obj->info->adc1Gain = (_meas_s32_)tmp64;

  /// [AT-PM] : Calculate current ADC1 offset ; 01/23/2013
  tmp64 = (_meas_s64_)obj->info->adc1OffsetSlope;
  tmp64 = tmp64*(obj->codeIntTemperature) + obj->info->adc1OffsetFactorO;
  obj->info->adc1Offset = (_meas_s32_)tmp64;
	#ifdef	UPI_UBOOT_DEBUG_MSG
		printf("[CalAdc1Factors] adc1Gain / adc1Offset = %d / %d\n", obj->info->adc1Gain, obj->info->adc1Offset);
	#endif	///< end of UPI_UBOOT_DEBUG_MSG
}

/**
 * @brief CalAdc2Factors
 *
 *  Calculate ADC2 gain slope and factor B
 *  Calculate ADC2 offset slope and factor O
 *
 * @para  obj address of MeasDataInternalType
 * @return  _UPI_NULL_
 */
void CalAdc2Factors(MeasDataInternalType *obj)
{
  _meas_s32_ delta25;
  _meas_s32_ delta80;
  _meas_s64_ tmp64;

  /// [AT-PM] : Calculate gain slope and factor B ; 01/23/2013
  delta25 = (_meas_s32_)obj->adc2CodeT25V200;
  delta25 = delta25 - obj->adc2CodeT25V100;
  delta80 = (_meas_s32_)obj->adc2CodeT80V200;
  delta80 = delta80 - obj->adc2CodeT80V100;

  obj->info->adc2GainSlope = delta80 - delta25;
  obj->info->adc2GainFactorB = delta25*(obj->info->otp->aveIT80) - delta80*(obj->info->otp->aveIT25);
	#ifdef	UPI_UBOOT_DEBUG_MSG
		printf("[CalAdc2Factors] adc2GainSlope / adc2GainFactorB = %d / %d\n", obj->info->adc2GainSlope, obj->info->adc2GainFactorB);
	#endif	///< end of UPI_UBOOT_DEBUG_MSG

  /// [AT-PM] : Calculate offset slope and factor O ; 01/23/2013
  delta25 = (_meas_s32_)obj->adc2CodeT25V100;
  delta25 = delta25*2 - obj->adc2CodeT25V200;
  delta80 = (_meas_s32_)obj->adc2CodeT80V100;
  delta80 = delta80*2 - obj->adc2CodeT80V200;

  obj->info->adc2OffsetSlope = delta80 - delta25;
  obj->info->adc2OffsetFactorO = delta25*(obj->info->otp->aveIT80) - delta80*(obj->info->otp->aveIT25);
        #ifdef  UPI_UBOOT_DEBUG_MSG
                printf("[CalAdc2Factors] adc2OffsetSlope / adc2OffsetFactorO = %d / %d\n", obj->info->adc2OffsetSlope, obj->info->adc2OffsetFactorO);
        #endif  ///< end of UPI_UBOOT_DEBUG_MSG

  /// [AT-PM] : Calculate current ADC1 gain ; 01/23/2013
  tmp64 = (_meas_s64_)obj->info->adc2GainSlope;
  tmp64 = tmp64*(obj->codeIntTemperature) + obj->info->adc2GainFactorB;
  obj->info->adc2Gain = (_meas_s32_)tmp64;

  /// [AT-PM] : Calculate current ADC1 offset ; 01/23/2013
  tmp64 = (_meas_s64_)obj->info->adc2OffsetSlope;
  tmp64 = tmp64*(obj->codeIntTemperature) + obj->info->adc2OffsetFactorO;
  obj->info->adc2Offset = (_meas_s32_)tmp64;
        #ifdef  UPI_UBOOT_DEBUG_MSG
                printf("[CalAdc2Factors] adc2Gain / adc2Offset = %d / %d\n", obj->info->adc2Gain, obj->info->adc2Offset);
        #endif  ///< end of UPI_UBOOT_DEBUG_MSG
}

#define ADC1_IDEAL_CODE_100MV     (614)
#define ADC1_IDEAL_CODE_200MV     (1229)
#define ADC1_IDEAL_CODE_DELTA     (ADC1_IDEAL_CODE_200MV - ADC1_IDEAL_CODE_100MV)

/**
 * @brief CalibrateAdc1Code
 *
 *  Calibrate ADC1 code
 *
 * @para  obj address of MeasDataInternalType
 * @para  code  ADC1 code to be calibrated
 * @return  calibrated code
 */
_meas_s32_ CalibrateAdc1Code(MeasDataInternalType *obj, _meas_s32_ code)
{
  _meas_s64_ tmp64;
  _meas_s32_ tmp32;
  _meas_s32_ deltaIT;
  _meas_s32_ gain;
  _meas_s32_ offset;

  deltaIT = (_meas_s32_)obj->info->otp->aveIT80;
  deltaIT = deltaIT - obj->info->otp->aveIT25;

  /// [AT-PM] : Pre-operation to avoid 64-bit division ; 01/23/2013
  gain = obj->info->adc1Gain;
  offset = obj->info->adc1Offset;
  while(1)
  {
    tmp64 = (_meas_s64_)code;
    tmp64 = tmp64*deltaIT - offset;
    tmp64 = tmp64*ADC1_IDEAL_CODE_DELTA;
    if((tmp64 < 2147483647) && (tmp64 > -2147483647))
    {
      break;
    }
    code = code/2;
    deltaIT = deltaIT/2;
    gain = gain/4;
    offset = offset/4;
  }

  tmp32 = (_meas_s32_)tmp64;
  tmp32 = tmp32/gain;
	#ifdef	UPI_UBOOT_DEBUG_MSG
		printf("[CalibrateAdc1Code] %d -> %d\n", code, tmp32);
	#endif	///< end of UPI_UBOOT_DEBUG_MSG
  return (tmp32);
}

#define ADC2_IDEAL_CODE_100MV     (ADC2_CP_CODE_25_100MV)
#define ADC2_IDEAL_CODE_200MV     (ADC2_CP_CODE_25_200MV)
#define ADC2_IDEAL_CODE_DELTA     (ADC2_IDEAL_CODE_200MV - ADC2_IDEAL_CODE_100MV)

/**
 * @brief CalibrateAdc2Code
 *
 *  Calibrate ADC2 code
 *
 * @para  obj address of MeasDataInternalType
 * @para  code  ADC2 code to be calibrated
 * @return  calibrated code
 */
_meas_s32_ CalibrateAdc2Code(MeasDataInternalType *obj, _meas_s32_ code)
{
  _meas_s64_ tmp64;
  _meas_s32_ tmp32;
  _meas_s32_ deltaIT;
  _meas_s32_ gain;
  _meas_s32_ offset;

  deltaIT = (_meas_s32_)obj->info->otp->aveIT80;
  deltaIT = deltaIT - obj->info->otp->aveIT25;

  /// [AT-PM] : Pre-operation to avoid 64-bit division ; 01/23/2013
  gain = obj->info->adc2Gain;
  offset = obj->info->adc2Offset;
  while(1)
  {
    tmp64 = (_meas_s64_)code;
    tmp64 = tmp64*deltaIT - offset;
    tmp64 = tmp64*ADC2_IDEAL_CODE_DELTA;
    if((tmp64 < 2147483647) && (tmp64 > -2147483647))
    {
      break;
    }
    code = code/2;
    deltaIT = deltaIT/2;
    gain = gain/4;
    offset = offset/4;
  }

  tmp32 = (_meas_s32_)tmp64;
  tmp32 = tmp32/gain;
        #ifdef  UPI_UBOOT_DEBUG_MSG
                printf("[CalibrateAdc2Code] %d -> %d\n", code, tmp32);
        #endif  ///< end of UPI_UBOOT_DEBUG_MSG
  return (tmp32);
}

#define IT_IDEAL_CODE_25      (24310)
#define IT_IDEAL_CODE_80      (28612)
#define IT_IDEAL_CODE_DELTA   (IT_IDEAL_CODE_80 - IT_IDEAL_CODE_25)

/**
 * @brief CalibrateITCode
 *
 *  Calibrate internal temperature code
 *
 * @para  obj address of MeasDataInternalType
 * @para  itCode  raw IT code
 * @return  calibrated IT code
 */
_meas_u16_ CalibrateITCode(MeasDataInternalType *obj, _meas_u16_ itCode)
{
  _meas_s32_ tmp32;

  tmp32 = (_meas_s32_)itCode;
  tmp32 = tmp32 - obj->info->otp->aveIT25;
  tmp32 = tmp32*IT_IDEAL_CODE_DELTA;
  tmp32 = tmp32/(obj->info->otp->aveIT80 - obj->info->otp->aveIT25);
  tmp32 = tmp32 + IT_IDEAL_CODE_25;
  return ((_meas_u16_)tmp32);
}

#define NORMAL_REGISTER     (NORMAL)

/**
 * @brief CalibrateChargeCode
 *
 *  Calibrate charge code
 *
 * @para  obj address of MeasDataInternalType
 * @return  _UPI_NULL_
 */
void CalibrateChargeCode(MeasDataInternalType *obj)
{
  _meas_s32_ tmp32;
  _meas_s64_ tmp64;
  _meas_s32_ gain;
  _meas_s32_ offset;

  UG31_LOGI("[%s]: Raw Code = %d\n", __func__, obj->codeCharge);
  
  /// [AT-PM] : Calibrate charge code ; 01/23/2013
  obj->info->codeCharge = CalibrateAdc1Code(obj, ((_meas_s32_)obj->codeCharge)*2);

  /// [AT-PM] : Calculate coulomb counter offset ; 01/23/2013
  tmp32 = obj->info->adc1Offset/(obj->info->otp->aveIT80 - obj->info->otp->aveIT25)*(-1);
  obj->info->ccOffset = (_meas_s16_)tmp32;

  UG31_LOGI("[%s]: Calibrated Code = %d, Offset = %d\n", __func__, obj->info->codeCharge, obj->info->ccOffset);

  /// [AT-PM] : Set coulomb counter offset ; 01/27/2013
  API_I2C_Write(NORMAL_REGISTER, 
                UG31XX_I2C_HIGH_SPEED_MODE, 
                UG31XX_I2C_TEM_BITS_MODE, 
                REG_ADC1_OFFSET_LOW, 
                REG_ADC1_OFFSET_HIGH - REG_ADC1_OFFSET_LOW + 1, 
                (unsigned char *)&obj->info->ccOffset);

  /// [AT-PM] : Remove the offset in calibrated charge code ; 01/23/2013
  gain = obj->info->adc1Gain;
  offset = obj->info->adc1Offset;
  while(1)
  {
    tmp64 = (_meas_s64_)offset;
    tmp64 = tmp64*ADC1_IDEAL_CODE_DELTA;
    if((tmp64 < 2147483647) && (tmp64 > -2147483647))
    {
      break;
    }
    gain = gain/2;
    offset = offset/2;
  }
  tmp32 = (_meas_s32_)tmp64;
  tmp32 = tmp32/gain;
  UG31_LOGI("[%s]: Compensation = %d x %d / %d\n", __func__, 
            obj->info->adc1Offset, ADC1_IDEAL_CODE_DELTA, obj->info->adc1Gain);
  obj->info->codeCharge = obj->info->codeCharge + tmp32;
  UG31_LOGI("[%s]: Charge = %d\n", __func__, obj->info->codeCharge);
}

#define ADC2_VOLTAGE_100MV    (3000)                                    ///< [AT-PM] : Unit in mV ; 01/25/2013
#define ADC2_VOLTAGE_200MV    (4000)                                    ///< [AT-PM] : Unit in mV ; 01/25/2013
#define ADC2_VOLTAGE_DELTA    (ADC2_VOLTAGE_200MV - ADC2_VOLTAGE_100MV)

/**
 * @brief ConvertBat1
 *
 *  Convert code of BAT1
 *
 * @para  obj address of MeasDataInternalType
 * @return  _UPI_NULL_
 */
void ConvertBat1(MeasDataInternalType *obj)
{
  _meas_s32_ tmp32;

  /// [AT-PM] : Convert from calibrated ADC code ; 01/25/2013
  tmp32 = (_meas_s32_)obj->info->codeBat1;
  tmp32 = tmp32 - ADC2_IDEAL_CODE_100MV;
  tmp32 = tmp32*ADC2_VOLTAGE_DELTA/ADC2_IDEAL_CODE_DELTA;
  tmp32 = tmp32 + ADC2_VOLTAGE_100MV;

  /// [AT-PM] : Apply board factor ; 01/25/2013
  tmp32 = tmp32 - BOARD_FACTOR_VOLTAGE_OFFSET;
  tmp32 = tmp32*BOARD_FACTOR_CONST/BOARD_FACTOR_VOLTAGE_GAIN;

  /// [AT-PM] : Apply calibration parameter ; 01/25/2013
  tmp32 = tmp32 - obj->info->sysData->ggbParameter->adc2_offset;
  tmp32 = tmp32*CALIBRATION_FACTOR_CONST/obj->info->sysData->ggbParameter->adc2_gain;
  obj->info->bat1Voltage = (_meas_u16_)tmp32;
}

#define ADC1_VOLTAGE_100MV    (-5000)                                   ///< [AT-PM] : Unit in uV ; 01/25/2013
#define ADC1_VOLTAGE_200MV    (-10000)                                  ///< [AT-PM] : Unit in uV ; 01/25/2013
#define ADC1_VOLTAGE_DELTA    (ADC1_VOLTAGE_200MV - ADC1_VOLTAGE_100MV)
/**
 * @brief ConvertCurrent
 *
 *  Convert code of Current
 *
 * @para  obj address of MeasDataInternalType
 * @return  _UPI_NULL_
 */
void ConvertCurrent(MeasDataInternalType *obj)
{
  _meas_s32_ tmp32;

  /// [AT-PM] : Convert from calibrated ADC code ; 01/25/2013
  tmp32 = (_meas_s32_)obj->info->codeCurrent;
  tmp32 = tmp32 - ADC1_IDEAL_CODE_100MV;
  tmp32 = tmp32*ADC1_VOLTAGE_DELTA/ADC1_IDEAL_CODE_DELTA;
  tmp32 = tmp32 + ADC1_VOLTAGE_100MV;
  tmp32 = tmp32/obj->info->sysData->ggbParameter->rSense;

  /// [AT-PM] : Apply board factor ; 01/25/2013
  tmp32 = tmp32 - BOARD_FACTOR_CURR_OFFSET;
  tmp32 = tmp32*BOARD_FACTOR_CONST/BOARD_FACTOR_CURR_GAIN;

  /// [AT-PM] : Apply calibration factor ; 01/25/2013
  tmp32 = tmp32 - obj->info->sysData->ggbParameter->adc1_pos_offset;
  if(tmp32 > 0)
  {
    tmp32 = tmp32*CALIBRATION_FACTOR_CONST/obj->info->sysData->ggbParameter->adc1_pgain;
  }
  else
  {
    tmp32 = tmp32*CALIBRATION_FACTOR_CONST/obj->info->sysData->ggbParameter->adc1_ngain;
  }
  obj->info->curr = (_meas_s16_)tmp32;
}

#define AMBIENT_TEMPERATURE_IN_FT (220)
#define IT_CONST                  (100)
#define IT_GAIN                   (392)
#define IT_OFFSET                 (11172)

static _meas_s16_ FTAmbientMappingTable[] = 
{
  AMBIENT_TEMPERATURE_IN_FT,            ///< Index = 0
  AMBIENT_TEMPERATURE_IN_FT + 10,       ///< Index = 1
  AMBIENT_TEMPERATURE_IN_FT - 10,       ///< Index = 2
  AMBIENT_TEMPERATURE_IN_FT + 20,       ///< Index = 3
  AMBIENT_TEMPERATURE_IN_FT - 20,       ///< Index = 4
  AMBIENT_TEMPERATURE_IN_FT + 30,       ///< Index = 5
  AMBIENT_TEMPERATURE_IN_FT - 30,       ///< Index = 6
  AMBIENT_TEMPERATURE_IN_FT + 40,       ///< Index = 7
  AMBIENT_TEMPERATURE_IN_FT - 40,       ///< Index = 8
  AMBIENT_TEMPERATURE_IN_FT + 50,       ///< Index = 9
  AMBIENT_TEMPERATURE_IN_FT - 50,       ///< Index = 10
  AMBIENT_TEMPERATURE_IN_FT + 60,       ///< Index = 11
  AMBIENT_TEMPERATURE_IN_FT - 60,       ///< Index = 12
  AMBIENT_TEMPERATURE_IN_FT + 70,       ///< Index = 13
  AMBIENT_TEMPERATURE_IN_FT - 70,       ///< Index = 14
  AMBIENT_TEMPERATURE_IN_FT,            ///< Index = 15
};

/**
 * @brief ConvertIntTemperature
 *
 *  Convert code of internal temperature
 *
 * @para  obj address of MeasDataInternalType
 * @return  _UPI_NULL_
 */
void ConvertIntTemperature(MeasDataInternalType *obj)
{
  _meas_s32_ tmp32;
  _meas_s32_ ftIT;

  /// [AT-PM] : Convert from calibrated ADC code ; 01/25/2013
  tmp32 = (_meas_s32_)obj->info->codeIntTemperature;
  tmp32 = tmp32/2;
  tmp32 = tmp32 - IT_OFFSET;
  tmp32 = tmp32*IT_CONST/IT_GAIN;

  /// [AT-PM] : Apply FT information ; 01/25/2013
  ftIT = (_meas_s32_)CalibrateITCode(obj, obj->info->otp->ftIT);
  ftIT = ftIT/2;
  ftIT = ftIT - IT_OFFSET;
  ftIT = ftIT*IT_CONST/IT_GAIN;
  tmp32 = tmp32 - (ftIT - FTAmbientMappingTable[obj->info->otp->deltaET]);

  /// [AT-PM] : Apply board factor ; 01/25/2013
  tmp32 = tmp32 - BOARD_FACTOR_INTT_OFFSET;

  /// [AT-PM] : Apply calibration factor ; 01/25/2013
  tmp32 = tmp32 - obj->info->sysData->ggbParameter->adc_d5;
  obj->info->intTemperature = (_meas_s16_)tmp32;
}

static _meas_s16_ ExtTemperatureTable[] = {
  -100,       ///< Index = 0
  -50,        ///< Index = 1
  0,          ///< Index = 2
  50,         ///< Index = 3
  100,        ///< Index = 4
  150,        ///< Index = 5
  200,        ///< Index = 6
  250,        ///< Index = 7
  300,        ///< Index = 8
  350,        ///< Index = 9
  400,        ///< Index = 10
  450,        ///< Index = 11
  500,        ///< Index = 12
  550,        ///< Index = 13
  600,        ///< Index = 14
  650,        ///< Index = 15
  700,        ///< Index = 16
  750,        ///< Index = 17
  800,        ///< Index = 18
};

/**
 * @brief ConvertExtTemperature
 *
 *  Convert code of external temperature
 *
 * @para  obj address of MeasDataInternalType
 * @return  _UPI_NULL_
 */
void ConvertExtTemperature(MeasDataInternalType *obj)
{
  _meas_u8_ idx;
  _meas_s32_ tmp32;

  idx = 0;
  while(idx < ET_NUMS)
  {
    if(obj->info->codeExtTemperature >= obj->info->sysData->ggbParameter->rtTable[idx])
    {
      break;
    }
    idx = idx + 1;
  }
  
  if(idx == 0)
  {
    /// [AT-PM] : Minimum measurable temperature ; 01/25/2013
    tmp32 = (_meas_s32_)ExtTemperatureTable[0];
  }
  else if(idx == ET_NUMS)
  {
    /// [AT-PM] : Maximum measurable temperature ; 01/25/2013
    tmp32 = (_meas_s32_)ExtTemperatureTable[ET_NUMS - 1];
  }
  else
  {
    /// [AT-PM] : Calculate external temperature ; 01/25/2013
    tmp32 = (_meas_s32_)obj->info->codeExtTemperature;
    tmp32 = tmp32 - obj->info->sysData->ggbParameter->rtTable[idx];
    tmp32 = tmp32*(ExtTemperatureTable[idx - 1] - ExtTemperatureTable[idx]);
    tmp32 = tmp32/(obj->info->sysData->ggbParameter->rtTable[idx - 1] - obj->info->sysData->ggbParameter->rtTable[idx]);
    tmp32 = tmp32 + ExtTemperatureTable[idx];
  }

  /// [AT-PM] : Apply board factor ; 01/25/2013
  tmp32 = tmp32 - BOARD_FACTOR_EXTT_OFFSET;

  /// [AT-PM] : Apply calibration factor ; 01/25/2013
  tmp32 = tmp32 - obj->info->sysData->ggbParameter->adc_d4;
  obj->info->extTemperature = (_meas_s16_)tmp32;
}

#define TIME_DEFAULT_ADC1_CONVERT_TIME        (1253)
#define MINIMUM_ADC1_COUNTER_FOR_CONVERT_TIME (10)
#define MAXIMUM_ADC1_CONVERSION_TIME          (0xf8)
#define MINIMUM_ADC1_CONVERSION_TIME          (0x08)

/**
 * @brief CalculateAdc1ConvertTime
 *
 *  Calculate ADC1 conversion time
 *
 * @para  obj address of MeasDataInternalType
 * @return  _UPI_NULL_
 */
void CalculateAdc1ConvertTime(MeasDataInternalType *obj)
{
  _meas_u32_ tmp32;

  UG31_LOGI("[%s]: Initial conversion time = %d\n", __func__, obj->info->adc1ConvertTime);
  
  /// [AT-PM] : First time to calculate ADC1 conversion time ; 01/25/2013
  if(obj->info->adc1ConvertTime == 0)
  {
    obj->info->adc1ConvertTime = TIME_DEFAULT_ADC1_CONVERT_TIME;
    obj->info->lastCounter = obj->codeCounter;    
    return;
  }

  #ifdef  uG31xx_BOOT_LOADER
    /// [AT-PM] : In bootloader, ADC1 converstion time is not calculated ; 02/12/2013
    return;
  #endif  ///< end of uG31xx_BOOT_LOADER
  
  /// [AT-PM] : Check counter overflow or time overflow; 01/25/2013
  if((obj->codeCounter <= obj->info->lastCounter) || (obj->info->deltaTime == 0))
  {
    obj->info->lastCounter = obj->codeCounter;
    return;
  }

  /// [AT-PM] : Limit the minimum counter ; 02/11/2013
  tmp32 = (_meas_u32_)obj->codeCounter;
  tmp32 = tmp32 - obj->info->lastCounter;
  if(tmp32 < MINIMUM_ADC1_COUNTER_FOR_CONVERT_TIME)
  {
    obj->info->lastCounter = obj->codeCounter;
    return;
  }
  
  /// [AT-PM] : Average  ADC1 conversion time ; 01/25/2013
  tmp32 = obj->info->deltaTime;
  tmp32 = tmp32*TIME_CONVERT_TIME_TO_MSEC/(obj->codeCounter - obj->info->lastCounter);
  tmp32 = tmp32 + obj->info->adc1ConvertTime;
  tmp32 = tmp32/2;

  /// [AT-PM] : Check conversion time is valid or not ; 02/13/2013
  if((tmp32 > (MAXIMUM_ADC1_CONVERSION_TIME*TIME_CONVERT_TIME_TO_MSEC)) || 
     (tmp32 < (MINIMUM_ADC1_CONVERSION_TIME*TIME_CONVERT_TIME_TO_MSEC)))
  {
    UG31_LOGI("[%s]: ***************************************************************************************\n", __func__);
    UG31_LOGI("[%s]: ***************************************************************************************\n", __func__);
    UG31_LOGI("[%s]:  ####  #####  ##  ##  ####  #####  ##   ##  ####  ##       ###### ###### ##   ## ######\n", __func__);
    UG31_LOGI("[%s]: ##  ## ##  ## ### ## ##  ## ##  ## ### ### ##  ## ##       ######   ##   ### ### ##\n", __func__);
    UG31_LOGI("[%s]: ###### #####  ###### ##  ## #####  ####### ###### ##         ##     ##   ####### ###\n", __func__);
    UG31_LOGI("[%s]: ##  ## ##  ## ## ### ##  ## ##  ## ##   ## ##  ## ##         ##     ##   ##   ## ##\n", __func__);
    UG31_LOGI("[%s]: ##  ## #####  ##  ##  ####  ##  ## ##   ## ##  ## ######     ##   ###### ##   ## ######\n", __func__);
    UG31_LOGI("[%s]:\n", __func__);
    UG31_LOGI("[%s]: Previous Time Tag    = %d\n", __func__, obj->info->lastTimeTick - obj->info->deltaTime);
    UG31_LOGI("[%s]: Current Time Tag     = %d\n", __func__, obj->info->lastTimeTick);
    UG31_LOGI("[%s]: Delta Time           = %d\n", __func__, obj->info->deltaTime);
    UG31_LOGI("[%s]: Previous ADC Count   = %d\n", __func__, obj->info->lastCounter);
    UG31_LOGI("[%s]: Current ADC Count    = %d\n", __func__, obj->codeCounter);
    UG31_LOGI("[%s]: Delta ADC Count      = %d\n", __func__, obj->codeCounter - obj->info->lastCounter);
    UG31_LOGI("[%s]: Old ADC Convert Time = %d\n", __func__, obj->info->adc1ConvertTime);
    UG31_LOGI("[%s]: New ADC Convert Time = %d\n", __func__, tmp32);
    UG31_LOGI("[%s]: ***************************************************************************************\n", __func__);
    UG31_LOGI("[%s]: ***************************************************************************************\n", __func__);
    tmp32 = (_meas_u32_)obj->info->adc1ConvertTime;
  }
  UG31_LOGI("[%s]: Conversion Time = %d ((%d - %d)/%d)\n", __func__, 
            tmp32, obj->codeCounter, obj->info->lastCounter, obj->info->deltaTime);
  obj->info->adc1ConvertTime = (_meas_u16_)tmp32;
  obj->info->lastCounter = obj->codeCounter;
}

#define TIME_MSEC_TO_SEC          (1000)
#define TIME_SEC_TO_HOUR          (3600)
#define COULOMB_COUNTER_LSB       (4096)

/**
 * @brief ConvertCharge
 *
 *  Convert code of charge
 *
 * @para  obj address of MeasDataInternalType
 * @return  _UPI_NULL_
 */
void ConvertCharge(MeasDataInternalType *obj)
{
  _meas_s16_ tmp16;
  _meas_s32_ tmp32;
  _meas_s64_ tmp64;

  /// [AT-PM] : Convert from calibrated ADC code ; 01/25/2013
  tmp16 = ADC1_IDEAL_CODE_DELTA;
  tmp32 = (_meas_s32_)obj->info->codeCharge;
  tmp32 = tmp32 - ADC1_IDEAL_CODE_100MV;
  while(1)
  {
    tmp64 = (_meas_s64_)tmp32;
    tmp64 = tmp64*ADC1_VOLTAGE_DELTA;
    if((tmp64 < 2147483647) && (tmp64 > -2147483647))
    {
      break;
    }
    tmp16 = tmp16/2;
    tmp32 = tmp32/2;
  }
  tmp32 = (_meas_s32_)tmp64;
  tmp32 = tmp32/tmp16;
  tmp32 = tmp32 + ADC1_VOLTAGE_100MV;
  tmp32 = tmp32/obj->info->sysData->ggbParameter->rSense;
  UG31_LOGI("[%s]: ((%d - %d) x %d / %d + %d) / %d = %d\n", __func__,
            obj->info->codeCharge, ADC1_IDEAL_CODE_100MV, ADC1_VOLTAGE_DELTA, 
            ADC1_IDEAL_CODE_DELTA, ADC1_VOLTAGE_100MV, obj->info->sysData->ggbParameter->rSense, tmp32);
  /// [AT-PM] : Apply board factor ; 01/25/2013
  tmp32 = tmp32 - BOARD_FACTOR_CURR_OFFSET;
  tmp32 = tmp32*BOARD_FACTOR_CONST/BOARD_FACTOR_CURR_GAIN;
  UG31_LOGI("[%s]: Board Factor (%d/%d) -> %d\n", __func__, 
            BOARD_FACTOR_CURR_GAIN, BOARD_FACTOR_CURR_OFFSET, tmp32);

  /// [AT-PM] : Apply calibration factor ; 01/25/2013
  tmp32 = tmp32 - obj->info->sysData->ggbParameter->adc1_pos_offset;
  if(tmp32 > 0)
  {
    tmp32 = tmp32*CALIBRATION_FACTOR_CONST/obj->info->sysData->ggbParameter->adc1_pgain;
  }
  else
  {
    tmp32 = tmp32*CALIBRATION_FACTOR_CONST/obj->info->sysData->ggbParameter->adc1_ngain;
  }
  UG31_LOGI("[%s]: Calibration Factor (%d|%d/%d) -> %d\n", __func__,
            obj->info->sysData->ggbParameter->adc1_pgain, obj->info->sysData->ggbParameter->adc1_ngain, 
            obj->info->sysData->ggbParameter->adc1_pos_offset, tmp32);

  /// [AT-PM] : Apply time information ; 01/25/2013
  if(obj->info->inSuspendMode == _UPI_FALSE_)
  {
    CalculateAdc1ConvertTime(obj);
  }
  tmp32 = tmp32*(obj->info->adc1ConvertTime)/TIME_MSEC_TO_SEC*COULOMB_COUNTER_LSB/TIME_SEC_TO_HOUR;
  tmp32 = tmp32/TIME_CONVERT_TIME_TO_MSEC;

  /// [AT-PM] : Update capacity information ; 01/25/2013
  obj->info->deltaCap = (_meas_s16_)tmp32;
  obj->info->stepCap = obj->info->deltaCap - obj->info->lastDeltaCap;
  obj->info->lastDeltaCap = obj->info->deltaCap;
  UG31_LOGI("[%s]: Capacity = %d (%d)\n", __func__, obj->info->deltaCap, obj->info->stepCap);
}

/**
 * @brief TimeTick
 *
 *  Get the time tick and calculate delta time
 *
 * @para  obj address of MeasDataInternalType
 * @return  _UPI_NULL_
 */
void TimeTick(MeasDataInternalType *obj)
{
  if(obj->info->inSuspendMode == _UPI_TRUE_)
  {
    /// [AT-PM] : Prevent adc conversion count overflow ; 06/11/2013
    if(obj->codeCounter < obj->info->lastCounter)
    {
      return;
    }
    
    /// [AT-PM] : Use conversion count to estimate delta time ; 06/11/2013
    obj->info->deltaTime = (_meas_u32_)obj->codeCounter;
    obj->info->deltaTime = obj->info->deltaTime - obj->info->lastCounter;
    obj->info->deltaTime = obj->info->deltaTime*obj->info->adc1ConvertTime;
    return;
  }

  obj->currTime = GetTickCount();

  /// [AT-PM] : Prevent time tick overflow ; 01/25/2013
  if(obj->currTime <= obj->info->lastTimeTick)
  {
    obj->info->deltaTime = 0;
    obj->info->lastTimeTick = obj->currTime;
    UG31_LOGI("[%s]: OVERFLOW -> %d < %d\n", __func__, 
              obj->currTime, obj->info->lastTimeTick);
    return;
  }

  /// [AT-PM] : Calculate delta time ; 01/25/2013
  obj->info->deltaTime = obj->currTime - obj->info->lastTimeTick;
  UG31_LOGI("[%s]: Delta Time = %d - %d = %d\n", __func__,
            obj->currTime, obj->info->lastTimeTick, obj->info->deltaTime);
  obj->info->lastTimeTick = obj->currTime;  
}

/**
 * @brief ReadRegister
 *
 *  Read measurement registers
 *
 * @para  obj address of MeasDataInternalType
 * @return  _UPI_NULL_
 */
void ReadRegister(MeasDataInternalType *obj)
{
  /// [AT-PM] : Read VBat1Ave ; 01/27/2013
  API_I2C_Read(NORMAL_REGISTER, 
               UG31XX_I2C_HIGH_SPEED_MODE, 
               UG31XX_I2C_TEM_BITS_MODE, 
               REG_AVE_VBAT1_LOW, 
               REG_AVE_VBAT1_HIGH - REG_AVE_VBAT1_LOW + 1, 
               (unsigned char *)&obj->codeBat1);

  /// [AT-PM] : Read CurrentAve ; 01/27/2013
  API_I2C_Read(NORMAL_REGISTER, 
               UG31XX_I2C_HIGH_SPEED_MODE, 
               UG31XX_I2C_TEM_BITS_MODE, 
               REG_AVE_CURRENT_LOW, 
               REG_AVE_CURRENT_HIGH - REG_AVE_CURRENT_LOW + 1, 
               (unsigned char *)&obj->codeCurrent);

  /// [AT-PM] : Read ITAve ; 01/27/2013
  API_I2C_Read(NORMAL_REGISTER, 
               UG31XX_I2C_HIGH_SPEED_MODE, 
               UG31XX_I2C_TEM_BITS_MODE, 
               REG_AVE_IT_LOW, 
               REG_AVE_IT_HIGH - REG_AVE_IT_LOW + 1, 
               (unsigned char *)&obj->codeIntTemperature);

  /// [AT-PM] : Read ETAve ; 01/27/2013
  API_I2C_Read(NORMAL_REGISTER, 
               UG31XX_I2C_HIGH_SPEED_MODE, 
               UG31XX_I2C_TEM_BITS_MODE, 
               REG_AVE_ET_LOW, 
               REG_AVE_ET_HIGH - REG_AVE_ET_LOW + 1, 
               (unsigned char *)&obj->codeExtTemperature);

  /// [AT-PM] : Read Charge ; 01/27/2013
  API_I2C_Read(NORMAL_REGISTER, 
               UG31XX_I2C_HIGH_SPEED_MODE, 
               UG31XX_I2C_TEM_BITS_MODE, 
               REG_CHARGE_LOW, 
               REG_CHARGE_HIGH - REG_CHARGE_LOW + 1, 
               (unsigned char *)&obj->codeCharge);

  /// [AT-PM] : Read Counter ; 01/27/2013
  API_I2C_Read(NORMAL_REGISTER, 
               UG31XX_I2C_HIGH_SPEED_MODE, 
               UG31XX_I2C_TEM_BITS_MODE, 
               REG_COUNTER_LOW, 
               REG_COUNTER_HIGH - REG_COUNTER_LOW + 1, 
               (unsigned char *)&obj->codeCounter);

  /// [AT-PM] : Read Offset ; 01/27/2013
  API_I2C_Read(NORMAL_REGISTER, 
               UG31XX_I2C_HIGH_SPEED_MODE, 
               UG31XX_I2C_TEM_BITS_MODE, 
               REG_ADC1_OFFSET_LOW, 
               REG_ADC1_OFFSET_HIGH - REG_ADC1_OFFSET_LOW + 1, 
               (unsigned char *)&obj->ccOffset);
}

/**
 * @brief ResetCoulombCounter
 *
 *  Reset coulomb counter
 *
 * @para  obj address of MeasDataInternalType
 * @return  _UPI_NULL_
 */
void ResetCoulombCounter(MeasDataInternalType *obj)
{
  _meas_u8_ tmp8;
  
  API_I2C_Read(NORMAL_REGISTER, 
               UG31XX_I2C_HIGH_SPEED_MODE, 
               UG31XX_I2C_TEM_BITS_MODE, 
               REG_CTRL1, 
               1,
               &tmp8);
  tmp8 = tmp8 | CTRL1_GG_RST;
  API_I2C_Write(NORMAL_REGISTER, 
                UG31XX_I2C_HIGH_SPEED_MODE, 
                UG31XX_I2C_TEM_BITS_MODE, 
                REG_CTRL1, 
                1,
                &tmp8);    
}

/**
 * @brief RevertCalibrateAdc2Code
 *
 *  Revert calibrated ADC2 code
 *
 * @para  data  address of MeasDataType
 * @para  caliCode  calibrated ADC2 code
 * @return  raw ADC2 code
 */
_meas_s32_ RevertCalibrateAdc2Code(MeasDataType *data, _meas_s32_ caliCode)
{
  _meas_s64_ tmp64;
  _meas_s32_ tmp32;
  _meas_s32_ deltaIT;
  _meas_s32_ gain;
  _meas_s32_ offset;
  _meas_s32_ constant;

  /// [AT-PM] : tmp32 = ( caliCode x gain / constant + offset ) / deltaIT ; 04/08/2013
  gain = data->adc2Gain;
  offset = data->adc2Offset;
  deltaIT = (_meas_s32_)data->otp->aveIT80;
  deltaIT = deltaIT - data->otp->aveIT25;
  constant = ADC2_IDEAL_CODE_DELTA;
  while(1)
  {
    tmp64 = (_meas_s64_)caliCode;
    tmp64 = tmp64*gain;
    if((tmp64 < 2147483647) && (tmp64 > -2147483647))
    {
      break;
    }
    caliCode = caliCode/2;
    gain = gain/2;
    constant = constant/4;
  }
  tmp32 = (_meas_s32_)tmp64;
  tmp32 = tmp32/constant;
  tmp32 = tmp32 + offset;
  tmp32 = tmp32/deltaIT;
  return (tmp32);
}

/**
 * @brief RevertBat1Code
 *
 *  Revert VBat1 code
 *
 * @para  data  address of MeasDataType
 * @para  volt  voltage in mV to be reverted
 * @return  adc2 vbat1 code
 */
_meas_u16_ RevertBat1Code(MeasDataType *data, _upi_s16_ volt)
{
  _meas_s32_ tmp32;

  tmp32 = (_meas_s32_)volt;

  /// [AT-PM] : Revert calibration parameter ; 04/08/2013
  tmp32 = tmp32*data->sysData->ggbParameter->adc2_gain/CALIBRATION_FACTOR_CONST;
  tmp32 = tmp32 + data->sysData->ggbParameter->adc2_offset;

  /// [AT-PM] : Revert board factor ; 04/08/2013
  tmp32 = tmp32*BOARD_FACTOR_VOLTAGE_GAIN/BOARD_FACTOR_CONST;
  tmp32 = tmp32 + BOARD_FACTOR_VOLTAGE_OFFSET;

  /// [AT-PM] : Revert to calibrated ADC code ; 04/08/2013
  tmp32 = tmp32 - ADC2_VOLTAGE_100MV;
  tmp32 = tmp32*ADC2_IDEAL_CODE_DELTA/ADC2_VOLTAGE_DELTA;
  tmp32 = tmp32 + ADC2_IDEAL_CODE_100MV;

  /// [AT-PM] : Revert to raw code ; 04/08/2013
  tmp32 = RevertCalibrateAdc2Code(data, tmp32);
  return ((_meas_u16_)tmp32);
}

/**
 * @brief RevertETCode
 *
 *  Revert ET code
 *
 * @para  data  address of MeasDataType
 * @para  et  external temperature in 0.1oC to be reverted
 * @return  adc1 et code
 */
_meas_u16_ RevertETCode(MeasDataType *data, _upi_s16_ et)
{
  _meas_s32_ tmp32;
  _meas_u8_ idx;

  tmp32 = (_meas_s32_)et;

  /// [AT-PM] : Revert calibration factor ; 04/08/2013
  tmp32 = tmp32 + data->sysData->ggbParameter->adc_d4;

  /// [AT-PM] : Revert board factor ; 04/08/2013
  tmp32 = tmp32 + BOARD_FACTOR_EXTT_OFFSET;

  /// [AT-PM] : Revert external temperature calculation ; 04/08/2013
  idx = 0;
  while(idx < ET_NUMS)
  {
    if(tmp32 < ExtTemperatureTable[idx])
    {
      break;
    }
    idx = idx + 1;
  }
  if(idx == 0)
  {
    tmp32 = (_meas_s32_)data->sysData->ggbParameter->rtTable[0];
  }
  else if(idx >= ET_NUMS)
  {
    tmp32 = (_meas_s32_)data->sysData->ggbParameter->rtTable[ET_NUMS - 1];
  }
  else
  {
    tmp32 = tmp32 - ExtTemperatureTable[idx - 1];
    tmp32 = tmp32*(data->sysData->ggbParameter->rtTable[idx] - data->sysData->ggbParameter->rtTable[idx - 1]);
    tmp32 = tmp32/(ExtTemperatureTable[idx] - ExtTemperatureTable[idx - 1]);
    tmp32 = tmp32 + data->sysData->ggbParameter->rtTable[idx - 1];
  }
  return ((_meas_u16_)tmp32);
}

#define MAX_ET_CODE_DIFF  (200)
#define MIN_ET_CODE_DIFF  (-200)

/**
 * @brief CalibrateETCode
 *
 *  Calibrate external temperature code
 *
 * @para  obj address of MeasDataInternalType
 * @return  MEAS_RTN_CODE
 */
_meas_u16_ CalibrateETCode(MeasDataInternalType *obj)
{
  _meas_s16_ tmp16;

  tmp16 = (_meas_s16_)obj->codeExtTemperature;
  tmp16 = tmp16 - (obj->codeCurrent + obj->ccOffset);
  if(obj->info->codeExtTemperature != 0)
  {
	#ifdef	UPI_UBOOT_DEBUG_MSG
		printf("[CalibrateETCode]: Last = %d, Current = %d\n", obj->info->codeExtTemperature, tmp16);
	#endif	///< end of UPI_UBOOT_DEBUG_MSG
    tmp16 = tmp16 - obj->info->codeExtTemperature;
    if(tmp16 > MAX_ET_CODE_DIFF)
    {
      tmp16 = MAX_ET_CODE_DIFF;
      UG31_LOGI("[%s]: Exceed maximum ET code difference (%d > %d)\n", __func__, tmp16, MAX_ET_CODE_DIFF);
    }
    if(tmp16 < MIN_ET_CODE_DIFF)
    {
      tmp16 = MIN_ET_CODE_DIFF;
      UG31_LOGI("[%s]: Exceed minimum ET code difference (%d < %d)\n", __func__, tmp16, MIN_ET_CODE_DIFF);
    }
    tmp16 = tmp16 + obj->info->codeExtTemperature;
  }
	#ifdef	UPI_UBOOT_DEBUG_MSG
		printf("[CalibrateETCode]: ET code = %d\n", tmp16);
	#endif	///< end of UPI_UBOOT_DEBUG_MSG
  return ((_meas_u16_)tmp16);
}

/// =============================================
/// [AT-PM] : Extern function region
/// =============================================

/**
 * @brief UpiResetCoulombCounter
 *
 *  Reset coulomb counter
 *
 * @para  data  address of MeasDataType
 * @return  _UPI_NULL_
 */
void UpiResetCoulombCounter(MeasDataType *data)
{
  MeasDataInternalType *obj;
  
  #if defined(uG31xx_OS_ANDROID)
    #ifdef  uG31xx_BOOT_LOADER
      obj = (MeasDataInternalType *)malloc(sizeof(MeasDataInternalType));
    #else   ///< else of uG31xx_BOOT_LOADER
      obj = (MeasDataInternalType *)kmalloc(sizeof(MeasDataInternalType), GFP_KERNEL);
    #endif  ///< end of uG31xx_BOOT_LOADER
  #else   ///< else of defined(uG31xx_OS_ANDROID)
	  obj = (MeasDataInternalType *)malloc(sizeof(MeasDataInternalType));
  #endif  ///< end of defined(uG31xx_OS_ANDROID)
  memset(obj, 0x00, sizeof(MeasDataInternalType));
  
  obj->info = data;

  /// [AT-PM] : Get delta time ; 01/25/2013
  TimeTick(obj);

  /// [AT-PM] : Read ADC code ; 01/27/2013
  ReadRegister(obj);

  /// [AT-PM] : Reset coulomb counter ; 01/30/2013
  ResetCoulombCounter(obj);

  /// [AT-PM] : Convert ADC characteristic from OTP ; 01/23/2013
  ConvertAdc1Data(obj);

  /// [AT-PM] : Calculate ADC gain and offset ; 01/23/2013
  CalAdc1Factors(obj);
  
  /// [AT-PM] : Calibrate ADC code ; 01/23/2013
  CalibrateChargeCode(obj);

  /// [AT-PM] : Convert into physical value ; 01/23/2013
  ConvertCharge(obj);
  data->lastDeltaCap = 0;

  #if defined(uG31xx_OS_ANDROID)
    #ifdef  uG31xx_BOOT_LOADER
      free(obj);
    #else   ///< else of uG31xx_BOOT_LOADER
      kfree(obj);
    #endif  ///< end of uG31xx_BOOT_LOADER
  #else   ///< else of defined(uG31xx_OS_ANDROID)
    free(obj);
  #endif  ///< end of defined(uG31xx_OS_ANDROID)
}

#define MAXIMUM_RETRY_CNT             (10)
#define MINIMUM_VBAT1_CODE            (ADC2_IDEAL_CODE_100MV/2)
#define MAXIMUM_CURRENT_CODE          (ADC1_IDEAL_CODE_200MV*6)
#define MINIMUM_CURRENT_CODE          (ADC1_IDEAL_CODE_200MV*(-6))
#define MINIMUM_IT_CODE               (IT_IDEAL_CODE_25/2)
#define MAXIMUM_IT_CODE               (IT_IDEAL_CODE_80*11/10)
#define MINIMUM_ET_CODE               (1000)
#define MAXIMUM_ET_CODE               (28000)
#define RESET_CC_CURRENT_MAGIC_NUMBER (2)

/**
 * @brief UpiMeasurement
 *
 *  Measurement routine
 *
 * @para  data  address of MeasDataType
 * @return  MEAS_RTN_CODE
 */
MEAS_RTN_CODE UpiMeasurement(MeasDataType *data)
{
  MeasDataInternalType *obj;
  _meas_u8_ retry;
  MEAS_RTN_CODE rtn;
  _meas_s16_ standbyUpper;
  _meas_s16_ standbyLower;
  
  UG31_LOGI("[%s]: Measurement version : %d\n", __func__, UG31XX_MEAS_VERSION);

  #if defined(uG31xx_OS_ANDROID)
    #ifdef  uG31xx_BOOT_LOADER
      obj = (MeasDataInternalType *)malloc(sizeof(MeasDataInternalType));
    #else   ///< else of uG31xx_BOOT_LOADER
      obj = (MeasDataInternalType *)kmalloc(sizeof(MeasDataInternalType), GFP_KERNEL);
    #endif  ///< end of uG31xx_BOOT_LOADER
  #else   ///< else of defined(uG31xx_OS_ANDROID)
	  obj = (MeasDataInternalType *)malloc(sizeof(MeasDataInternalType));
  #endif  ///< end of defined(uG31xx_OS_ANDROID)
  memset(obj, 0x00, sizeof(MeasDataInternalType));
  
  obj->info = data;
  rtn = MEAS_RTN_PASS;

  /// [AT-PM] : Get delta time ; 01/25/2013
  TimeTick(obj);

  /// [AT-PM] : Read ADC code ; 01/27/2013
  retry = 0;
  while(retry < MAXIMUM_RETRY_CNT)
  {
    ReadRegister(obj);
        #ifdef  UPI_UBOOT_DEBUG_MSG
                printf("[UpiMeasurement] Retry = %d\n", retry);
                printf("[UpiMeasurement] VBAT1 Code = %d\n", obj->codeBat1);
		printf("[UpiMeasurement] CURRENT Code = %d\n", obj->codeCurrent);
		printf("[UpiMeasurement] IT Code = %d\n", obj->codeIntTemperature);
		printf("[UpiMeasurement] ET Code = %d\n", obj->codeExtTemperature);
		printf("[UpiMeasurement] COULOMB COUNTER Code = %d - %d\n", obj->codeCharge, obj->codeCounter);
        #endif  ///< end of UPI_UBOOT_DEBUG_MSG

    if(obj->codeBat1 < MINIMUM_VBAT1_CODE)
    {
      UG31_LOGE("[%s]: Voltage code %d < %d -> Retry %d\n", __func__, 
                obj->codeBat1, MINIMUM_VBAT1_CODE, retry);
      rtn = MEAS_RTN_BATTERY_REMOVED;
    }
    else if(obj->codeCurrent < MINIMUM_CURRENT_CODE)
    {
      UG31_LOGE("[%s]: Current code %d < %d\n", __func__,
                obj->codeCurrent, MINIMUM_CURRENT_CODE);
      obj->codeCurrent = MINIMUM_CURRENT_CODE;
      break;
    }
    else if(obj->codeCurrent > MAXIMUM_CURRENT_CODE)
    {
      UG31_LOGE("[%s]: Current code %d > %d\n", __func__,
                obj->codeCurrent, MAXIMUM_CURRENT_CODE);
      obj->codeCurrent = MAXIMUM_CURRENT_CODE;
      break;
    }
    else if(obj->codeIntTemperature < MINIMUM_IT_CODE)
    {
      UG31_LOGE("[%s]: Internal Temperature code %d < %d -> Retry %d\n", __func__,
                obj->codeIntTemperature, MINIMUM_IT_CODE, retry);
      rtn = MEAS_RTN_ADC_ABNORMAL;
    }
    else if(obj->codeIntTemperature > MAXIMUM_IT_CODE)
    {
      UG31_LOGE("[%s]: Internal Temperature code %d > %d -> Retry %d\n", __func__,
                obj->codeIntTemperature, MAXIMUM_IT_CODE, retry);
      rtn = MEAS_RTN_ADC_ABNORMAL;
    }
    else if(obj->codeExtTemperature < MINIMUM_ET_CODE)
    {
      UG31_LOGE("[%s]: External Temperature code %d < %d -> Retry %d\n", __func__,
                obj->codeExtTemperature, MINIMUM_ET_CODE, retry);
      rtn = MEAS_RTN_NTC_SHORT;
      #ifndef ENABLE_NTC_CHECK
        break;
      #endif  ///< end of ENABLE_NTC_CHECK
    }
    else if(obj->codeExtTemperature > MAXIMUM_ET_CODE)
    {
      UG31_LOGE("[%s]: External Temperature code %d > %d -> Retry %d\n", __func__,
                obj->codeExtTemperature, MAXIMUM_ET_CODE, retry);
      rtn = MEAS_RTN_BATTERY_REMOVED;
      #ifndef ENABLE_NTC_CHECK
        break;
      #endif  ///< end of ENABLE_NTC_CHECK
    }
    else
    {
      break;
    }
    retry = retry + 1;
    SleepMiniSecond(1000);
  }
  if(retry >= MAXIMUM_RETRY_CNT)
  {
    #if defined(uG31xx_OS_ANDROID)
      #ifdef  uG31xx_BOOT_LOADER
        free(obj);
      #else   ///< else of uG31xx_BOOT_LOADER
        kfree(obj);
      #endif  ///< end of uG31xx_BOOT_LOADER
    #else   ///< else of defined(uG31xx_OS_ANDROID)
      free(obj);
    #endif  ///< end of defined(uG31xx_OS_ANDROID)
    return (rtn);
  }
  rtn = MEAS_RTN_PASS;
  
  /// [AT-PM] : Convert ADC characteristic from OTP ; 01/23/2013
  ConvertAdc1Data(obj);
  ConvertAdc2Data(obj);

  /// [AT-PM] : Calculate ADC gain and offset ; 01/23/2013
  CalAdc1Factors(obj);
  CalAdc2Factors(obj);
  
  /// [AT-PM] : Calibrate ADC code ; 01/23/2013
  data->codeBat1 = (_meas_u16_)CalibrateAdc2Code(obj, (_meas_s32_)obj->codeBat1);
  UG31_LOGI("[%s]: VBat1 Code = %d -> %d\n", __func__, obj->codeBat1, data->codeBat1);
  data->codeCurrent = (_meas_s16_)CalibrateAdc1Code(obj, (_meas_s32_)obj->codeCurrent);
  UG31_LOGI("[%s]: Current Code = %d -> %d\n", __func__, obj->codeCurrent, data->codeCurrent);
  CalibrateChargeCode(obj);
  data->codeIntTemperature = CalibrateITCode(obj, obj->codeIntTemperature);
  UG31_LOGI("[%s]: Internal Temperature Code = %d -> %d\n", __func__,
            obj->codeIntTemperature, data->codeIntTemperature);
  data->codeExtTemperature = CalibrateETCode(obj);
  UG31_LOGI("[%s]: External Temperature Code = %d -> %d\n", __func__, obj->codeExtTemperature, data->codeExtTemperature);

  /// [AT-PM] : Convert into physical value ; 01/23/2013
  ConvertBat1(obj);
  ConvertCurrent(obj);
  ConvertIntTemperature(obj);
  ConvertExtTemperature(obj);
  ConvertCharge(obj);

  /// [AT-PM] : Reset coulomb counter if necessary ; 01/27/2013
  standbyUpper = (_meas_s16_)obj->info->sysData->ggbParameter->standbyCurrent;
  standbyUpper = standbyUpper/RESET_CC_CURRENT_MAGIC_NUMBER;
  standbyLower = standbyUpper*(-1);
  if((obj->codeCounter > COULOMB_COUNTER_RESET_THRESHOLD_COUNTER) ||
     (obj->codeCharge > COULOMB_COUNTER_RESET_THRESHOLD_CHARGE_CHG) ||
     (obj->codeCharge < COULOMB_COUNTER_RESET_THREDHOLD_CHARGE_DSG) ||
     ((obj->info->curr < standbyUpper) && 
      (obj->info->curr > standbyLower) && 
      (obj->codeCounter > CONST_CONVERSION_COUNT_THRESHOLD*RESET_CC_CURRENT_MAGIC_NUMBER)))
  {
    ResetCoulombCounter(obj);
    data->lastDeltaCap = 0;
  }

  #ifdef  MEAS_FAKE_INT_TEMP
    data->extTemperature = data->intTemperature;
    data->intTemperature = MEAS_FAKE_INT_TEMP_OFFSET + data->intTemperature%100;
  #endif  ///< end of MEAS_FAKE_INT_TEMP
  
  UG31_LOGI("[%s]: %d mV / %d mA / %d 0.1oC / %d 0.1oC / %d mAh\n", __func__,
            data->bat1Voltage, data->curr, data->intTemperature, data->extTemperature, data->deltaCap);
  #if defined(uG31xx_OS_ANDROID)
    #ifdef  uG31xx_BOOT_LOADER
      free(obj);
    #else   ///< else of uG31xx_BOOT_LOADER
      kfree(obj);
    #endif  ///< end of uG31xx_BOOT_LOADER
  #else   ///< else of defined(uG31xx_OS_ANDROID)
    free(obj);
  #endif  ///< end of defined(uG31xx_OS_ANDROID)
  return (rtn);
}

/**
 * @brief UpiMeasAlarmThreshold
 *
 *  Get alarm threshold
 *
 * @para  data  address of MeasDataType
 * @return  MEAS_RTN_CODE
 */
MEAS_RTN_CODE UpiMeasAlarmThreshold(MeasDataType *data)
{
  MEAS_RTN_CODE rtn;
  
  rtn = MEAS_RTN_PASS;

  /// [AT-PM] : Calculate UV alarm and release threshold ; 04/08/2013
  data->sysData->uvAlarm.alarmThrd = RevertBat1Code(data, data->sysData->ggbParameter->uvAlarm);
  data->sysData->uvAlarm.releaseThrd = RevertBat1Code(data, data->sysData->ggbParameter->uvRelease);
  UG31_LOGI("[%s]: UV Alarm -> %d / %d\n", __func__, 
            data->sysData->uvAlarm.alarmThrd, data->sysData->uvAlarm.releaseThrd);
  
  /// [AT-PM] : Calculate UET alarm and release threshold ; 04/08/2013
  data->sysData->uetAlarm.alarmThrd = RevertETCode(data, data->sysData->ggbParameter->uetAlarm);
  data->sysData->uetAlarm.releaseThrd = RevertETCode(data, data->sysData->ggbParameter->uetRelease);
  UG31_LOGI("[%s]: UET Alarm -> %d / %d\n", __func__, 
            data->sysData->uetAlarm.alarmThrd, data->sysData->uetAlarm.releaseThrd);
  
  /// [AT-PM] : Calculate OET alarm and release threshold ; 04/08/2013
  data->sysData->oetAlarm.alarmThrd = RevertETCode(data, data->sysData->ggbParameter->oetAlarm);
  data->sysData->oetAlarm.releaseThrd = RevertETCode(data, data->sysData->ggbParameter->oetRelease);
  UG31_LOGI("[%s]: OET Alarm -> %d / %d\n", __func__, 
            data->sysData->oetAlarm.alarmThrd, data->sysData->oetAlarm.releaseThrd);

  return (rtn);
}