-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMLX90640.c
More file actions
1547 lines (1335 loc) · 81.5 KB
/
Copy pathMLX90640.c
File metadata and controls
1547 lines (1335 loc) · 81.5 KB
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
/*!*****************************************************************************
* @file MLX90640.c
* @author Fabien 'Emandhal' MAILLY
* @version 1.1.0
* @date 27/02/2021
* @brief MLX90640 driver
* @details 32x24 IR array
* Follow datasheet 3901090640 Rev.12 (Dec 2019)
******************************************************************************/
//-----------------------------------------------------------------------------
#include "MLX90640.h"
//-----------------------------------------------------------------------------
#include <math.h>
#include <float.h>
//-----------------------------------------------------------------------------
#ifdef __cplusplus
# include <cstdint>
extern "C" {
#endif
//-----------------------------------------------------------------------------
#ifdef USE_DYNAMIC_INTERFACE
# define GET_I2C_INTERFACE pComp->I2C
#else
# define GET_I2C_INTERFACE &pComp->I2C
#endif
//-----------------------------------------------------------------------------
//=============================================================================
// Prototypes for private functions
//=============================================================================
// Detect the byte order of the MCU
static uint16_t __MLX90640_DetectByteOrder(void);
// Write 2-bytes address the MLX90640 (DO NOT USE DIRECTLY)
static eERRORRESULT __MLX90640_WriteAddress(MLX90640 *pComp, const uint8_t chipAddr, const uint16_t address, const bool useNonBlocking, const eI2C_TransferType transferType);
// Calculate and store Vdd parameters on the MLX90640 device
static void __MLX90640_ExtractVDDParameters(MLX90640 *pComp, MLX90640_EEPROM *eepromDump);
// Calculate and store Ambient Temperature coefficients parameters on the MLX90640 device
static void __MLX90640_ExtractPTATParameters(MLX90640 *pComp, MLX90640_EEPROM *eepromDump);
// Calculate and store Pixels Offset parameters on the MLX90640 device
static void __MLX90640_ExtractPixelsOffsetParameters(MLX90640 *pComp, MLX90640_EEPROM *eepromDump);
// Calculate and store ILchess parameters on the MLX90640 device
static void __MLX90640_ExtractILchessParameters(MLX90640 *pComp, MLX90640_EEPROM *eepromDump);
// Calculate and store Pixels Sensitivity parameters on the MLX90640 device
static void __MLX90640_ExtractPixelsSensitivityParameters(MLX90640 *pComp, MLX90640_EEPROM *eepromDump);
// Calculate and store Kta Coefficients Pixels parameters on the MLX90640 device
static void __MLX90640_ExtractKtaCoeffPixelsParameters(MLX90640 *pComp, MLX90640_EEPROM *eepromDump);
// Calculate and store Kv Coefficients Pixels parameters on the MLX90640 device
static void __MLX90640_ExtractKvCoeffPixelsParameters(MLX90640 *pComp, MLX90640_EEPROM *eepromDump);
// Calculate and store KsTo parameters on the MLX90640 device
static void __MLX90640_ExtractKsToParameters(MLX90640 *pComp, MLX90640_EEPROM *eepromDump);
// Calculate and store Miscellaneous parameters on the MLX90640 device
static void __MLX90640_ExtractMiscellaneousParameters(MLX90640 *pComp, MLX90640_EEPROM *eepromDump);
// Extract defective pixels from the MLX90640 device
static eERRORRESULT __MLX90640_ExtractDefectivePixels(MLX90640 *pComp, MLX90640_EEPROM *eepromDump);
#if !defined(MLX90640_PRECALCULATE_PIXELS_COEFFS)
// Calculate and return a Pixel Offset on the MLX90640 device
static float __MLX90640_Extract1PixelOffsetParameter(MLX90640 *pComp, size_t y, size_t x);
// Calculate and return a Pixel Sensitivity on the MLX90640 device
static float __MLX90640_Extract1PixelSensitivityParameter(MLX90640 *pComp, size_t y, size_t x);
// Calculate and return Kta Coefficient of a Pixel on the MLX90640 device
static float __MLX90640_ExtractKtaCoeff1PixelParameter(MLX90640 *pComp, size_t y, size_t x);
// Calculate and return Kv Coefficients of a Pixel on the MLX90640 device
static float __MLX90640_ExtractKvCoeff1PixelParameter(MLX90640 *pComp, size_t y, size_t x);
#endif
// Get the Vdd voltage an Ambient Temperature of the frame on the MLX90640 device
static eERRORRESULT __MLX90640_GetVddAndTa(MLX90640 *pComp, MLX90640_FrameData* frameData, float* deltaVdd3V3Frame, float* deltaTaFrame);
#if (MLX90640_MOVING_AVERAGE_FILTER_VALUES_COUNT > 1)
// Apply a moving average filter on the PixGain_Cp_SPx of the frame
static void __MLX90640_MovingAverageFilter(float *pixGainCPSP0, float *pixGainCPSP1, MLX90640_FrameTo *result);
#else
# define __MLX90640_MovingAverageFilter(a,b,c)
#endif
// Check the frame data received from the MLX90640 device
eERRORRESULT __MLX90640_CheckFrameData(MLX90640_FrameData* frameData);
//-----------------------------------------------------------------------------
// Choose one of the following for the fourth root
#define ftrtf(value) ( sqrtf(sqrtf(value)) ) // Apply a successive square root for the fourth root
//#define ftrtf(value) ( powf((value),0.25f) ) // Apply a x^(1/4) for the fourth root
//-----------------------------------------------------------------------------
//**********************************************************************************************************************************************************
//=============================================================================
// [STATIC] Detect the byte order of the MCU
//=============================================================================
uint16_t __MLX90640_DetectByteOrder(void)
{
uint16_t x = 1 | (((uint16_t)2) << (16 - 8));
uint8_t *cp = (uint8_t*)&x;
if (*cp == 2) return MLX90640_BIG_ENDIAN; // Big-endian
return MLX90640_LITTLE_ENDIAN; // By default, work with little-endian
}
//**********************************************************************************************************************************************************
//=============================================================================
// MLX90640 device initialization
//=============================================================================
eERRORRESULT Init_MLX90640(MLX90640 *pComp, const MLX90640_Config *pConf)
{
#ifdef CHECK_NULL_PARAM
if ((pComp == NULL) || (pConf == NULL)) return ERR__PARAMETER_ERROR;
#endif
I2C_Interface* pI2C = GET_I2C_INTERFACE;
#if defined(CHECK_NULL_PARAM)
# if defined(USE_DYNAMIC_INTERFACE)
if (pI2C == NULL) return ERR__PARAMETER_ERROR;
# endif
if (pI2C->fnI2C_Init == NULL) return ERR__PARAMETER_ERROR;
#endif
eERRORRESULT Error;
//--- Initialize internal config ---
pComp->InternalConfig = MLX90640_DEV_NOT_PARAMETERIZED | __MLX90640_DetectByteOrder();
//--- Check and adjust I2C SCL speed ---
if (pComp->I2CclockSpeed > MLX90640_I2CCLOCK_FMp_MAX) return ERR__I2C_FREQUENCY_ERROR; // Desired I2C SCL frequency too high for the device
uint32_t SCLfreq = pComp->I2CclockSpeed;
if ((SCLfreq > MLX90640_I2CCLOCK_FM_MAX) && pConf->I2C_FMpEnable) SCLfreq = MLX90640_I2CCLOCK_FM_MAX; // FM+ mode is wanted but the device is not yet configured for this so set the FM max frequency
if (SCLfreq > MLX90640_I2CCLOCK_FM_MAX) return ERR__I2C_FREQUENCY_ERROR; // If the SCL frequency is too high and the device will not be configured for FM+ mode then return an error
//--- Initialize the interface ---
Error = pI2C->fnI2C_Init(pI2C, SCLfreq); // Initialize the I2C with a safe SCL clock speed for configuration
if (Error != ERR_OK) return Error; // If there is an error while calling fnI2C_Init() then return the Error
if (MLX90640_PollDevice(pComp) != ERR_OK) return ERR__NO_DEVICE_DETECTED; // Check device presence
//--- Configure the I2C on device side ---
Error = MLX90640_ConfigureDeviceI2C(pComp, pConf->I2C_FMpEnable, pConf->SetThresholdTo1V8, pConf->SetSDAdriverCurrentLimit);
if (Error != ERR_OK) return Error; // If there is an error while calling MLX90640_ConfigureI2C() then return the Error
//--- Finally set the desired I2C SCL speed ---
if ((SCLfreq != pComp->I2CclockSpeed) && pConf->I2C_FMpEnable) // The working SCL frequency differ from the configuration frequency then
{
Error = pI2C->fnI2C_Init(pI2C, pComp->I2CclockSpeed); // Re-initialize the I2C with the desired SCL clock speed
if (Error != ERR_OK) return Error; // If there is an error while calling fnI2C_Init() then return the Error
}
//--- Configure the device ---
return MLX90640_ConfigureDevice(pComp, pConf->SubpageMode, pConf->RefreshRate, pConf->ReadingPattern, pConf->ADCresolution);
}
//=============================================================================
// Poll the MLX90640 device
//=============================================================================
eERRORRESULT MLX90640_PollDevice(MLX90640 *pComp)
{
#ifdef CHECK_NULL_PARAM
if (pComp == NULL) return ERR__PARAMETER_ERROR;
#endif
I2C_Interface* pI2C = GET_I2C_INTERFACE;
#if defined(CHECK_NULL_PARAM)
# if defined(USE_DYNAMIC_INTERFACE)
if (pI2C == NULL) return ERR__PARAMETER_ERROR;
# endif
if (pI2C->fnI2C_Transfer == NULL) return ERR__PARAMETER_ERROR;
#endif
I2CInterface_Packet PacketDesc =
{
I2C_MEMBER(Config.Value) I2C_BLOCKING | I2C_ENDIAN_TRANSFORM_SET(I2C_NO_ENDIAN_CHANGE) | I2C_TRANSFER_TYPE_SET(I2C_SIMPLE_TRANSFER),
I2C_MEMBER(ChipAddr ) pComp->I2Caddress | I2C_READ_ORMASK,
I2C_MEMBER(Start ) true,
I2C_MEMBER(pBuffer ) NULL,
I2C_MEMBER(BufferSize ) 0,
I2C_MEMBER(Stop ) true,
};
return pI2C->fnI2C_Transfer(pI2C, &PacketDesc); // Send only the chip address and get the Ack flag
}
//=============================================================================
// Is a frame available on the MLX90640 device
//=============================================================================
bool MLX90640_IsFrameAvailable(MLX90640 *pComp)
{
eERRORRESULT Error;
MLX90640_Status RegStatus;
Error = MLX90640_ReadRegister(pComp, RegMLX90640_Status, &RegStatus.Status);
if (Error != ERR_OK) return false; // If there is an error while calling MLX90640_ReadRegister() then return no frame available
if ((RegStatus.Status & MLX90640_NEW_DATA_AVAILABLE) > 0) return true;
return false;
}
//=============================================================================
// Get actual device of the MLX90640 device
//=============================================================================
eERRORRESULT MLX90640_GetDeviceID(MLX90640 *pComp, eMLX90640_Devices* device, uint16_t* deviceId1, uint16_t* deviceId2, uint16_t* deviceId3)
{
#ifdef CHECK_NULL_PARAM
if ((pComp == NULL) || (device == NULL)) return ERR__PARAMETER_ERROR;
#endif
eERRORRESULT Error;
uint16_t Value;
Error = MLX90640_ReadRegister(pComp, EepMLX90640_DeviceOption, &Value);
if (Error != ERR_OK) return Error; // If there is an error while calling MLX90640_ReadRegister() then return the error
Value &= MLX90640_FOV_Mask;
switch (Value)
{
case 0b1000: *device = MLX90640BAB; break;
case 0b1100: *device = MLX90640BAA; break;
default: return ERR__UNKNOWN_DEVICE;
}
if ((deviceId1 != NULL) && (deviceId2 != NULL) && (deviceId3 != NULL))
{
uint16_t DeviceID[3];
Error = MLX90640_ReadData(pComp, EepMLX90640_DeviceID, &DeviceID[0], sizeof(DeviceID) / sizeof(uint16_t)); // Read values of the DeviceID register
if (Error != ERR_OK) return Error; // If there is an error while calling MLX90640_ReadData() then return the error
*deviceId1 = DeviceID[0];
*deviceId2 = DeviceID[1];
*deviceId3 = DeviceID[2];
}
return ERR_OK;
}
//**********************************************************************************************************************************************************
// [STATIC] Write 2-bytes address the MLX90640 (DO NOT USE DIRECTLY)
static eERRORRESULT __MLX90640_WriteAddress(MLX90640 *pComp, const uint8_t chipAddr, const uint16_t address, const bool useNonBlocking, const eI2C_TransferType transferType)
{
#ifdef CHECK_NULL_PARAM
if (pComp == NULL) return ERR__PARAMETER_ERROR;
#endif
I2C_Interface* pI2C = GET_I2C_INTERFACE;
#if defined(CHECK_NULL_PARAM)
# if defined(USE_DYNAMIC_INTERFACE)
if (pI2C == NULL) return ERR__PARAMETER_ERROR;
# endif
if (pI2C->fnI2C_Transfer == NULL) return ERR__PARAMETER_ERROR;
#endif
eERRORRESULT Error;
//--- Create address ---
uint8_t Address[sizeof(uint16_t)];
for (int_fast8_t z = sizeof(uint16_t); --z >= 0;) Address[z] = (uint8_t)((address >> ((sizeof(uint16_t) - z - 1) * 8)) & 0xFF);
//--- Send the address ---
I2CInterface_Packet PacketDesc =
{
I2C_MEMBER(Config.Value) (useNonBlocking ? I2C_USE_NON_BLOCKING : I2C_BLOCKING) | I2C_ENDIAN_TRANSFORM_SET(I2C_NO_ENDIAN_CHANGE) | I2C_TRANSFER_TYPE_SET(transferType),
I2C_MEMBER(ChipAddr ) chipAddr,
I2C_MEMBER(Start ) true,
I2C_MEMBER(pBuffer ) &Address[0],
I2C_MEMBER(BufferSize ) sizeof(Address),
I2C_MEMBER(Stop ) false,
};
Error = pI2C->fnI2C_Transfer(pI2C, &PacketDesc); // Transfer the address
if (Error == ERR__I2C_NACK) return ERR__NOT_READY; // If the device receive a NAK, then the device is not ready
if (Error == ERR__I2C_NACK_DATA) return ERR__I2C_INVALID_ADDRESS; // If the device receive a NAK while transferring data, then this is an invalid address
return Error;
}
//**********************************************************************************************************************************************************
//=============================================================================
// Read data from the MLX90640 device
//=============================================================================
eERRORRESULT MLX90640_ReadData(MLX90640 *pComp, const uint16_t address, uint16_t* data, size_t size)
{
#ifdef CHECK_NULL_PARAM
if (pComp == NULL) return ERR__PARAMETER_ERROR;
#endif
I2C_Interface* pI2C = GET_I2C_INTERFACE;
#if defined(CHECK_NULL_PARAM)
# if defined(USE_DYNAMIC_INTERFACE)
if (pI2C == NULL) return ERR__PARAMETER_ERROR;
# endif
if (pI2C->fnI2C_Transfer == NULL) return ERR__PARAMETER_ERROR;
#endif
if (MLX90640_IS_DMA_TRANSFER_IN_PROGRESS(pComp->InternalConfig)) return ERR__BUSY;
I2CInterface_Packet PacketDesc;
eERRORRESULT Error;
uint8_t DataBuf[2];
const uint8_t ChipAddrW = (pComp->I2Caddress & MLX90640_CHIPADDRESS_MASK);
const uint8_t ChipAddrR = (ChipAddrW | I2C_READ_ORMASK);
//--- Read data ---
Error = __MLX90640_WriteAddress(pComp, ChipAddrW, address, false, I2C_WRITE_THEN_READ_FIRST_PART); // Start a read at address with the device
if (Error == ERR_OK) // If there is no error while writing address then
{
PacketDesc.Config.Value = I2C_BLOCKING | I2C_ENDIAN_TRANSFORM_SET(I2C_NO_ENDIAN_CHANGE) | I2C_TRANSFER_TYPE_SET(I2C_WRITE_THEN_READ_SECOND_PART);
PacketDesc.ChipAddr = ChipAddrR;
PacketDesc.pBuffer = &DataBuf[0];
PacketDesc.BufferSize = sizeof(DataBuf);
bool First = true;
while (size > 0)
{
PacketDesc.Start = First;
PacketDesc.Stop = (size == 1);
Error = pI2C->fnI2C_Transfer(pI2C, &PacketDesc); // Restart at first data read transfer, get the data and stop transfer at last data
if (Error == ERR__I2C_NACK_DATA) return ERR__I2C_COMM_ERROR; // If the device receive a NAK while transferring data, then this is a communication error
if (Error != ERR_OK) return Error; // If there is an error while calling fnI2C_PacketTransfer() then return the Error
if (MLX90640_IS_LITTLE_ENDIAN(pComp->InternalConfig))
*data = ((uint16_t)DataBuf[0] << 8) | (uint16_t)DataBuf[1]; // The device's communication is in little endian then MSB first & LSB last in little endian
else *data = ((uint16_t)DataBuf[1] << 8) | (uint16_t)DataBuf[0]; // else the device's communication is in big endian then LSB first & MSB last in big endian
First = false;
data++;
size--;
}
}
return Error;
}
//=============================================================================
// Read data with DMA from the MLX90640 device
//=============================================================================
eERRORRESULT MLX90640_ReadDataWithDMA(MLX90640 *pComp, const uint16_t address, uint8_t* data, size_t size, I2C_Conf *configResult)
{
#ifdef CHECK_NULL_PARAM
if (pComp == NULL) return ERR__PARAMETER_ERROR;
#endif
I2C_Interface* pI2C = GET_I2C_INTERFACE;
#if defined(CHECK_NULL_PARAM)
# if defined(USE_DYNAMIC_INTERFACE)
if (pI2C == NULL) return ERR__PARAMETER_ERROR;
# endif
if (pI2C->fnI2C_Transfer == NULL) return ERR__PARAMETER_ERROR;
#endif
I2CInterface_Packet PacketDesc;
eERRORRESULT Error;
const uint8_t ChipAddrW = (pComp->I2Caddress & MLX90640_CHIPADDRESS_MASK);
const uint8_t ChipAddrR = (ChipAddrW | I2C_READ_ORMASK);
//--- Check DMA ---
if (MLX90640_IS_DMA_TRANSFER_IN_PROGRESS(pComp->InternalConfig))
{
const uint16_t CurrTransactionNumber = MLX90640_TRANSACTION_NUMBER_GET(pComp->InternalConfig);
PacketDesc.Config.Value = I2C_USE_NON_BLOCKING | I2C_ENDIAN_TRANSFORM_SET(I2C_NO_ENDIAN_CHANGE) | I2C_TRANSFER_TYPE_SET(I2C_SIMPLE_TRANSFER) | I2C_TRANSACTION_NUMBER_SET(CurrTransactionNumber);
PacketDesc.ChipAddr = ChipAddrR;
PacketDesc.Start = true;
PacketDesc.pBuffer = NULL;
PacketDesc.BufferSize = 0;
PacketDesc.Stop = true;
Error = pI2C->fnI2C_Transfer(pI2C, &PacketDesc); // Send only the chip address and get the Ack flag, to return the status of the current transfer
if ((Error != ERR__I2C_BUSY) && (Error != ERR__I2C_OTHER_BUSY)) pComp->InternalConfig &= MLX90640_NO_DMA_TRANSFER_IN_PROGRESS_SET;
return Error;
}
//--- Read data ---
Error = __MLX90640_WriteAddress(pComp, ChipAddrW, address, true, I2C_WRITE_THEN_READ_FIRST_PART); // Start a read at address with the device
if (Error == ERR_OK) // If there is no error while writing address then
{
const eI2C_EndianTransform EndianTransform = (MLX90640_IS_LITTLE_ENDIAN(pComp->InternalConfig) ? I2C_SWITCH_ENDIAN_16BITS : I2C_NO_ENDIAN_CHANGE);
PacketDesc.Config.Value = I2C_USE_NON_BLOCKING | I2C_ENDIAN_TRANSFORM_SET(EndianTransform) | I2C_TRANSFER_TYPE_SET(I2C_WRITE_THEN_READ_SECOND_PART);
PacketDesc.ChipAddr = ChipAddrR;
PacketDesc.pBuffer = data;
PacketDesc.BufferSize = size;
PacketDesc.Start = true;
PacketDesc.Stop = true;
Error = pI2C->fnI2C_Transfer(pI2C, &PacketDesc); // Restart at first data read transfer, get the data and stop transfer at last data
configResult->Value = PacketDesc.Config.Value; // Return the packet configuration result
if (Error != ERR__I2C_OTHER_BUSY) pComp->InternalConfig &= MLX90640_NO_DMA_TRANSFER_IN_PROGRESS_SET;
if (Error == ERR__I2C_BUSY) pComp->InternalConfig |= MLX90640_DMA_TRANSFER_IN_PROGRESS;
MLX90640_TRANSACTION_NUMBER_CLEAR(pComp->InternalConfig);
pComp->InternalConfig |= MLX90640_TRANSACTION_NUMBER_SET(I2C_TRANSACTION_NUMBER_GET(PacketDesc.Config.Value));
}
return Error;
}
//=============================================================================
// Write data to the MLX90640 device
//=============================================================================
eERRORRESULT MLX90640_WriteData(MLX90640 *pComp, const uint16_t address, const uint16_t* data, size_t size)
{
#ifdef CHECK_NULL_PARAM
if (pComp == NULL) return ERR__PARAMETER_ERROR;
#endif
I2C_Interface* pI2C = GET_I2C_INTERFACE;
#if defined(CHECK_NULL_PARAM)
# if defined(USE_DYNAMIC_INTERFACE)
if (pI2C == NULL) return ERR__PARAMETER_ERROR;
# endif
if (pI2C->fnI2C_Transfer == NULL) return ERR__PARAMETER_ERROR;
#endif
if (MLX90640_IS_DMA_TRANSFER_IN_PROGRESS(pComp->InternalConfig)) return ERR__BUSY;
I2CInterface_Packet PacketDesc;
eERRORRESULT Error;
uint8_t DataBuf[2];
uint16_t* pData = (uint16_t*)data;
const uint8_t ChipAddrW = (pComp->I2Caddress & MLX90640_CHIPADDRESS_MASK);
//--- Program Address ---
Error = __MLX90640_WriteAddress(pComp, ChipAddrW, address, false, I2C_WRITE_THEN_WRITE_FIRST_PART); // Start a write at address with the device
if (Error == ERR_OK) // If there is no error while writing address then
{
PacketDesc.Config.Value = I2C_BLOCKING | I2C_ENDIAN_TRANSFORM_SET(I2C_NO_ENDIAN_CHANGE) | I2C_TRANSFER_TYPE_SET(I2C_WRITE_THEN_WRITE_SECOND_PART);
PacketDesc.ChipAddr = ChipAddrW;
PacketDesc.Start = false;
PacketDesc.pBuffer = &DataBuf[0];
PacketDesc.BufferSize = sizeof(DataBuf);
while (size > 0)
{
if (MLX90640_IS_LITTLE_ENDIAN(pComp->InternalConfig))
{
DataBuf[0] = (uint8_t)((*pData >> 8) & 0xFF); // The device's communication is in big endian then MSB first
DataBuf[1] = (uint8_t)((*pData >> 0) & 0xFF); // LSB last
}
else
{
DataBuf[0] = (uint8_t)((*pData >> 0) & 0xFF); // The device's communication is in big endian too so don't change endianness, then LSB first
DataBuf[1] = (uint8_t)((*pData >> 8) & 0xFF); // MSB last
}
PacketDesc.Stop = (size == 1);
Error = pI2C->fnI2C_Transfer(pI2C, &PacketDesc); // Continue the transfer by sending the data and stop transfer at last data (chip address will not be used)
if (Error != ERR_OK) return Error; // If there is an error while calling fnI2C_PacketTransfer() then return the Error
pData++;
size--;
}
}
return Error;
}
//=============================================================================
// Dump the entire EEPROM data from the MLX90640 device
//=============================================================================
eERRORRESULT MLX90640_DumpEEPROM(MLX90640 *pComp, MLX90640_EEPROM *eepromDump)
{
#ifdef CHECK_NULL_PARAM
if ((pComp == NULL) || (eepromDump == NULL)) return ERR__PARAMETER_ERROR;
#endif
I2C_Interface* pI2C = GET_I2C_INTERFACE;
#if defined(CHECK_NULL_PARAM)
# if defined(USE_DYNAMIC_INTERFACE)
if (pI2C == NULL) return ERR__PARAMETER_ERROR;
# endif
if (pI2C->fnI2C_Transfer == NULL) return ERR__PARAMETER_ERROR;
#endif
eERRORRESULT Error;
//--- Limit EEPROM I2C clock ---
if (pComp->I2CclockSpeed > MLX90640_I2CCLOCK_FM_MAX)
{
Error = pI2C->fnI2C_Init(pI2C, MLX90640_I2CCLOCK_FM_MAX); // Initialize the I2C with a safe SCL clock speed for EEPROM operations
if (Error != ERR_OK) return Error; // If there is an error while calling fnI2C_Init() then return the Error
}
//--- Read EEPROM data ---
Error = MLX90640_ReadData(pComp, EepMLX90640_StartAddress, &eepromDump->Words[0], sizeof(MLX90640_EEPROM) / sizeof(uint16_t));
if (Error != ERR_OK) return Error; // If there is an error while calling MLX90640_ReadData() then return the Error
//--- Return to the original I2C clock ---
if (pComp->I2CclockSpeed > MLX90640_I2CCLOCK_FM_MAX)
{
Error = pI2C->fnI2C_Init(pI2C, pComp->I2CclockSpeed); // Re-initialize the I2C with the desired SCL clock speed
}
return Error;
}
//=============================================================================
// Get the last frame data on the MLX90640 device
//=============================================================================
eERRORRESULT MLX90640_GetFrameData(MLX90640 *pComp, MLX90640_FrameData* frameData)
{
#ifdef CHECK_NULL_PARAM
if ((pComp == NULL) || (frameData == NULL)) return ERR__PARAMETER_ERROR;
#endif
eERRORRESULT Error;
//--- Read RAM data ---
Error = MLX90640_ReadData(pComp, RamMLX90640_StartAddress, &frameData->Words[0], (sizeof(MLX90640_FrameData) / sizeof(uint16_t)) - 1);
if (Error != ERR_OK) return Error; // If there is an error while calling MLX90640_ReadData() then return the Error
//--- Read status register ---
Error = MLX90640_ReadRegister(pComp, RegMLX90640_Status, &frameData->StatusReg.Status);
if (Error != ERR_OK) return Error; // If there is an error while calling MLX90640_ReadRegister() then return the Error
//--- Clear status register ---
Error = MLX90640_WriteRegister(pComp, RegMLX90640_Status, MLX90640_CLEAR_DEVICE_STATUS);
if (Error != ERR_OK) return Error; // If there is an error while calling MLX90640_WriteRegister() then return the Error
//--- Check data ---
return __MLX90640_CheckFrameData(frameData);
}
//**********************************************************************************************************************************************************
//=============================================================================
// Configure the I2C on the MLX90640 device
//=============================================================================
eERRORRESULT MLX90640_ConfigureDeviceI2C(MLX90640 *pComp, bool i2cFMpEnable, bool setThresholdTo1V8, bool setSDAdriverCurrentLimit)
{
MLX90640_I2Cconfig Reg;
Reg.I2Cconfig = MLX90640_FMp_ENABLE | MLX90640_THRESHOLD_VDD | MLX90640_SDA_CURRENT_LIMIT_ENABLE; // Default values
if (i2cFMpEnable == false) Reg.I2Cconfig |= MLX90640_FMp_DISABLE;
if (setThresholdTo1V8 ) Reg.I2Cconfig |= MLX90640_THRESHOLD_1V8;
if (setSDAdriverCurrentLimit == false) Reg.I2Cconfig |= MLX90640_SDA_CURRENT_LIMIT_DISABLE;
return MLX90640_WriteRegister(pComp, RegMLX90640_I2Cconfig, Reg.I2Cconfig);
}
//=============================================================================
// Configure the MLX90640 device
//=============================================================================
eERRORRESULT MLX90640_ConfigureDevice(MLX90640 *pComp, eMLX90640_SubpageMode subpageMode, eMLX90640_RefreshRate refreshRate, eMLX90640_ReadingPattern readingPattern, eMLX90640_ADCresolution adcResolution)
{
MLX90640_Control1 Reg;
Reg.Control1 = MLX90640_SUBPAGE_MODE_ENABLE | MLX90640_DATA_HOLD_DISABLE | MLX90640_SUBPAGE_REPEAT_DISABLE // Default values
| MLX90640_IR_REFRESH_RATE_SET(refreshRate) // Set refresh rate
| MLX90640_ADC_RESOLUTION_SET(adcResolution) // Set ADC resolution
| MLX90640_READING_INTERLEAVED; // Default value
switch (subpageMode)
{
case MLX90640_MEASURE_ONLY_SUBPAGE0:
Reg.Control1 |= MLX90640_SUBPAGE_REPEAT_ENABLE | MLX90640_SELECT_SUBPAGE_SET(MLX90640_SUBPAGE_0_IS_SELECTED); // Set subpage mode, enable subpage repeat and select subpage 0
break;
case MLX90640_MEASURE_ONLY_SUBPAGE1:
Reg.Control1 |= MLX90640_SUBPAGE_REPEAT_ENABLE | MLX90640_SELECT_SUBPAGE_SET(MLX90640_SUBPAGE_1_IS_SELECTED); // Set subpage mode, enable subpage repeat and select subpage 1
break;
case MLX90640_MEASURE_ALTERNATE_SUBPAGES:
Reg.Control1 |= MLX90640_SUBPAGE_REPEAT_DISABLE;
break;
default: break;
}
if (readingPattern == MLX90640_READING_CHESS_PATTERN) Reg.Control1 |= MLX90640_READING_CHESS_PATTERN; // If ask for reading chess pattern then set reading pattern
pComp->InternalConfig &= ~(MLX90640_READING_PATTERN_Mask | MLX90640_ADC_RESOLUTION_Mask); // Clear the config in the internal config
pComp->InternalConfig |= MLX90640_READING_PATTERN_SET(readingPattern) | MLX90640_ADC_RESOLUTION_SET(adcResolution); // Set the new configuration in the internal config
return MLX90640_WriteRegister(pComp, RegMLX90640_Control1, Reg.Control1);
}
//=============================================================================
// Change the I2C address of the MLX90640 device
//=============================================================================
eERRORRESULT MLX90640_ChangeI2Caddress(MLX90640 *pComp, uint8_t newAddress)
{
#ifdef CHECK_NULL_PARAM
if (pComp == NULL) return ERR__PARAMETER_ERROR;
if (pComp->fnGetCurrentms == NULL) return ERR__PARAMETER_ERROR;
#endif
if ((newAddress < 0x01) || (newAddress > 0x7F)) return ERR__I2C_INVALID_ADDRESS;
eERRORRESULT Error;
MLX90640_I2Caddress RegI2Caddr;
//--- Check previous value ---
Error = MLX90640_ReadRegister(pComp, EepMLX90640_I2Caddress, &RegI2Caddr.I2Caddress);
if (Error != ERR_OK) return Error; // If there is an error while calling MLX90640_ReadRegister() then return the Error
//--- Initialize register ---
Error = MLX90640_WriteRegister(pComp, EepMLX90640_I2Caddress, 0x0000);
if (Error != ERR_OK) return Error; // If there is an error while calling MLX90640_WriteRegister() then return the Error
//--- Sleep 10ms ---
uint32_t Timeout = pComp->fnGetCurrentms() + 10 + 1; // Wait at least 10 + 1ms because GetCurrentms can be 1 cycle before the new ms
while (pComp->fnGetCurrentms() < Timeout); // Wait until timeout
//--- Check value ---
uint16_t Value;
Error = MLX90640_ReadRegister(pComp, EepMLX90640_I2Caddress, &Value);
if (Error != ERR_OK) return Error; // If there is an error while calling MLX90640_ReadRegister() then return the Error
if (Value != 0x0000) return ERR__DATA_NOT_INITIALIZED; // The value of the register should be 0x0000
//--- Set new address ---
RegI2Caddr.I2Caddress = (RegI2Caddr.I2Caddress & 0xFF00) | newAddress; // Set new address value
Error = MLX90640_WriteRegister(pComp, EepMLX90640_I2Caddress, RegI2Caddr.I2Caddress);
if (Error != ERR_OK) return Error; // If there is an error while calling MLX90640_WriteRegister() then return the Error
//--- Sleep 10ms ---
Timeout = pComp->fnGetCurrentms() + 10 + 1; // Wait at least 10 + 1ms because GetCurrentms can be 1 cycle before the new ms
while (pComp->fnGetCurrentms() < Timeout); // Wait until timeout
//--- Check value ---
Error = MLX90640_ReadRegister(pComp, EepMLX90640_I2Caddress, &Value);
if (Error != ERR_OK) return Error; // If there is an error while calling MLX90640_ReadRegister() then return the Error
if (Value != RegI2Caddr.I2Caddress) return ERR__DATA_NOT_INITIALIZED; // The value of the register should be the same as the one written
return ERR_OK;
}
//**********************************************************************************************************************************************************
//=============================================================================
// [STATIC] Calculate and store Vdd parameters on the MLX90640 device
//=============================================================================
void __MLX90640_ExtractVDDParameters(MLX90640 *pComp, MLX90640_EEPROM *eepromDump)
{
int16_t Kv_Vdd;
int16_t Vdd_25;
uint8_t ResCtrlCalib;
//--- Get values from EEPROM ---
ResCtrlCalib = MLX90640_RES_CTRL_CAL_GET(eepromDump->K_Scale.Kscale);
Kv_Vdd = MLX90640_Kv_Vdd_GET(eepromDump->Vdd);
Vdd_25 = MLX90640_Vdd_25_GET(eepromDump->Vdd);
//--- Calculate and store values ---
pComp->Params->Resolution = ResCtrlCalib;
pComp->Params->Kv_Vdd = (Kv_Vdd * 32); // See §11.1.1
pComp->Params->Vdd_25 = ((Vdd_25 - 256) * 32) - 8192; // See §11.1.1
}
//=============================================================================
// [STATIC] Calculate and store Ambient Temperature coefficients parameters on the MLX90640 device
//=============================================================================
void __MLX90640_ExtractPTATParameters(MLX90640 *pComp, MLX90640_EEPROM *eepromDump)
{
int16_t Kv_PTAT = 0;
int16_t Kt_PTAT = 0;
int16_t PTAT_25 = 0;
uint8_t AlphaPTAT = 0u;
//--- Get values from EEPROM ---
Kv_PTAT = MLX90640_Kv_PTAT_GET(eepromDump->K_PTAT.KPTAT);
Kt_PTAT = MLX90640_Kt_PTAT_GET(eepromDump->K_PTAT.KPTAT);
PTAT_25 = eepromDump->PTAT_25;
AlphaPTAT = MLX90640_ALPHA_PTAT_GET(eepromDump->ScaleOCC.ScaleOCC);
//--- Calculate and store values ---
pComp->Params->KvPTAT = (float)Kv_PTAT / 4096.0f; // See §11.1.2
pComp->Params->KtPTAT = (float)Kt_PTAT / 8.0f; // See §11.1.2
pComp->Params->PTAT_25 = PTAT_25; // See §11.1.2
pComp->Params->AlphaPTAT = ((float)AlphaPTAT / 4.0f) + 8.0f; // See §11.1.2
}
//=============================================================================
// [STATIC] Calculate and store Pixels Offset parameters on the MLX90640 device
//=============================================================================
void __MLX90640_ExtractPixelsOffsetParameters(MLX90640 *pComp, MLX90640_EEPROM *eepromDump)
{
uint16_t OffsetCPsp0;
int16_t OffsetCPdelta;
//--- Get values from EEPROM ---
OffsetCPsp0 = MLX90640_OFFSET_CP_SUBPAGE_GET(eepromDump->Offset_CP.OffsetCPsubpage);
OffsetCPdelta = MLX90640_OFFSET_CP_P1P0_DELTA_GET(eepromDump->Offset_CP.OffsetCPsubpage);
//--- Calculate and store Offset CP subpages ---
pComp->Params->CPsubpageOffset[0] = OffsetCPsp0; // See §11.1.13
pComp->Params->CPsubpageOffset[1] = OffsetCPsp0 + OffsetCPdelta; // See §11.1.13
#ifdef MLX90640_PRECALCULATE_PIXELS_COEFFS
uint16_t OccScaleRem;
uint16_t OccScaleCol;
uint16_t OccScaleRow;
int16_t OffsetAverage;
uint16_t* OffsetRow;
uint16_t* OffsetCol;
int16_t OccRow[MLX90640_ROW_COUNT];
int16_t OccCol[MLX90640_COL_COUNT];
size_t Offset;
int16_t OffsetPixel;
OccScaleRem = MLX90640_SCALE_OCC_REM_GET(eepromDump->ScaleOCC.ScaleOCC);
OccScaleCol = MLX90640_SCALE_OCC_COL_GET(eepromDump->ScaleOCC.ScaleOCC);
OccScaleRow = MLX90640_SCALE_OCC_ROW_GET(eepromDump->ScaleOCC.ScaleOCC);
OffsetAverage = eepromDump->PixOsAvg;
OffsetRow = &eepromDump->Occ.Row.Row[0];
OffsetCol = &eepromDump->Occ.Column.Column[0];
//--- Calculate Row values ---
Offset = 0;
for (size_t z = 0; z < (MLX90640_ROW_COUNT / 4); ++z)
{
Offset = (z << 2);
OccRow[Offset + 0] = MLX90640_EXTRACT_NIBBLE_0(OffsetRow[z]);
OccRow[Offset + 1] = MLX90640_EXTRACT_NIBBLE_1(OffsetRow[z]);
OccRow[Offset + 2] = MLX90640_EXTRACT_NIBBLE_2(OffsetRow[z]);
OccRow[Offset + 3] = MLX90640_EXTRACT_NIBBLE_3(OffsetRow[z]);
}
//--- Calculate Column values ---
Offset = 0;
for (size_t z = 0; z < (MLX90640_COL_COUNT / 4); ++z)
{
Offset = (z << 2);
OccCol[Offset + 0] = MLX90640_EXTRACT_NIBBLE_0(OffsetCol[z]);
OccCol[Offset + 1] = MLX90640_EXTRACT_NIBBLE_1(OffsetCol[z]);
OccCol[Offset + 2] = MLX90640_EXTRACT_NIBBLE_2(OffsetCol[z]);
OccCol[Offset + 3] = MLX90640_EXTRACT_NIBBLE_3(OffsetCol[z]);
}
//--- Calculate and store Pixels values ---
for(int y = 0; y < MLX90640_ROW_COUNT; ++y)
{
//--- Calculate and store pixel value ---
for(int x = 0; x < MLX90640_COL_COUNT; ++x)
{
OffsetPixel = MLX90640_OFFSET_PIXEL_GET(eepromDump->PixelYXData[y][x].Pixel);
pComp->Params->OffsetsYX[y][x] = OffsetAverage + (OccRow[y] << OccScaleRow) + (OccCol[x] << OccScaleCol) + (OffsetPixel << OccScaleRem); // *** Pixel offset restoring. See §11.1.3
}
}
#endif
}
//=============================================================================
// [STATIC] Calculate and store ILchess parameters on the MLX90640 device
//=============================================================================
void __MLX90640_ExtractILchessParameters(MLX90640 *pComp, MLX90640_EEPROM *eepromDump)
{
int16_t ILchessC1;
int16_t ILchessC2;
int16_t ILchessC3;
uint8_t CalibrationMode;
//--- Get values from EEPROM ---
ILchessC1 = MLX90640_IL_CHESS_C1_GET(eepromDump->IL_Chess.ILchess);
ILchessC2 = MLX90640_IL_CHESS_C2_GET(eepromDump->IL_Chess.ILchess);
ILchessC3 = MLX90640_IL_CHESS_C3_GET(eepromDump->IL_Chess.ILchess);
CalibrationMode = (uint8_t)MLX90640_CALIBRATION_MODE_GET(eepromDump->DeviceOptions);
//--- Calculate and store values ---
pComp->Params->ILchess[MLX90640_C1] = (float)ILchessC1 / 16.0f; // See §11.1.3.1
pComp->Params->ILchess[MLX90640_C2] = (float)ILchessC2 / 2.0f; // See §11.1.3.1
pComp->Params->ILchess[MLX90640_C3] = (float)ILchessC3 / 8.0f; // See §11.1.3.1
pComp->Params->CalibrationPattern = CalibrationMode;
}
//=============================================================================
// [STATIC] Calculate and store Pixels Sensitivity parameters on the MLX90640 device
//=============================================================================
void __MLX90640_ExtractPixelsSensitivityParameters(MLX90640 *pComp, MLX90640_EEPROM *eepromDump)
{
uint16_t AlphaScale;
uint16_t AlphaCPsp0;
int16_t AlphaCPratio;
//--- Get values from EEPROM ---
AlphaScale = MLX90640_ALPHA_SCALE_GET(eepromDump->ScaleACC.ScaleACC);
AlphaCPsp0 = MLX90640_ALPHA_CP_SUBPAGE0_GET(eepromDump->Alpha_CP.AlphaCPsubpage);
AlphaCPratio = MLX90640_ALPHA_CP_P1P0_RATIO_GET(eepromDump->Alpha_CP.AlphaCPsubpage);
//--- Calculate and store Alpha CP subpages ---
pComp->Params->CPsubpageAlpha[0] = (float)AlphaCPsp0/(float)((uint64_t)1 << (AlphaScale + 27)); // See §11.1.12
pComp->Params->CPsubpageAlpha[1] = pComp->Params->CPsubpageAlpha[0] * (1.0 + ((float)AlphaCPratio/(float)(1 << 7))); // See §11.1.12
#ifdef MLX90640_PRECALCULATE_PIXELS_COEFFS
uint16_t AccScaleRem;
uint16_t AccScaleCol;
uint16_t AccScaleRow;
int16_t AlphaRef;
uint16_t* AlphaRow;
uint16_t* AlphaCol;
int16_t AccRow[MLX90640_ROW_COUNT];
int16_t AccCol[MLX90640_COL_COUNT];
size_t Offset;
int16_t AlphaPixel;
//--- Get values from EEPROM ---
AccScaleRem = MLX90640_SCALE_ACC_REM_GET(eepromDump->ScaleACC.ScaleACC);
AccScaleCol = MLX90640_SCALE_ACC_COL_GET(eepromDump->ScaleACC.ScaleACC);
AccScaleRow = MLX90640_SCALE_ACC_ROW_GET(eepromDump->ScaleACC.ScaleACC);
AlphaRef = eepromDump->PixSensitivityAvg;
AlphaRow = &eepromDump->Acc.Row.Row[0];
AlphaCol = &eepromDump->Acc.Column.Column[0];
//--- Calculate Row values ---
Offset = 0;
for (size_t z = 0; z < (MLX90640_ROW_COUNT / 4); ++z)
{
Offset = (z << 2);
AccRow[Offset + 0] = MLX90640_EXTRACT_NIBBLE_0(AlphaRow[z]);
AccRow[Offset + 1] = MLX90640_EXTRACT_NIBBLE_1(AlphaRow[z]);
AccRow[Offset + 2] = MLX90640_EXTRACT_NIBBLE_2(AlphaRow[z]);
AccRow[Offset + 3] = MLX90640_EXTRACT_NIBBLE_3(AlphaRow[z]);
}
//--- Calculate Column values ---
Offset = 0;
for (size_t z = 0; z < (MLX90640_COL_COUNT / 4); ++z)
{
Offset = (z << 2);
AccCol[Offset + 0] = MLX90640_EXTRACT_NIBBLE_0(AlphaCol[z]);
AccCol[Offset + 1] = MLX90640_EXTRACT_NIBBLE_1(AlphaCol[z]);
AccCol[Offset + 2] = MLX90640_EXTRACT_NIBBLE_2(AlphaCol[z]);
AccCol[Offset + 3] = MLX90640_EXTRACT_NIBBLE_3(AlphaCol[z]);
}
//--- Calculate and store Pixels values ---
AlphaScale += 30;
for(int y = 0; y < MLX90640_ROW_COUNT; ++y)
{
//--- Calculate and store pixel value ---
for(int x = 0; x < MLX90640_COL_COUNT; ++x)
{
AlphaPixel = MLX90640_ALPHA_PIXEL_GET(eepromDump->PixelYXData[y][x].Pixel);
pComp->Params->AlphasYX[y][x] = (float)((int32_t)AlphaRef + ((int32_t)AccRow[y] << AccScaleRow) + ((int32_t)AccCol[x] << AccScaleCol) + ((int32_t)AlphaPixel << AccScaleRem));
pComp->Params->AlphasYX[y][x] /= (float)((uint64_t)1 << AlphaScale); // *** Pixel alpha restoring. See §11.1.4
}
}
#endif
}
//=============================================================================
// [STATIC] Calculate and store Kta Coefficients Pixels parameters on the MLX90640 device
//=============================================================================
void __MLX90640_ExtractKtaCoeffPixelsParameters(MLX90640 *pComp, MLX90640_EEPROM *eepromDump)
{
int16_t KtaCP;
uint16_t KtaScale1;
KtaScale1 = MLX90640_Kta_SCALE1_GET(eepromDump->K_Scale.Kscale); // Get Kta Scale 1 value
KtaCP = MLX90640_Kta_CP_GET(eepromDump->K_CP); // Get KtaCP value
//--- Calculate and store KtaCP value ---
KtaScale1 += 8;
pComp->Params->KtaCP = (float)KtaCP / (float)(1 << KtaScale1); // See §11.1.15
#ifdef MLX90640_PRECALCULATE_PIXELS_COEFFS
int16_t KtaAvgTable[4];
uint16_t KtaScale2;
int16_t KtaPixel;
size_t Select;
//--- Get values from EEPROM ---
KtaAvgTable[0] = MLX90640_KtaAvg_ROW_EVEN_COL_EVEN_GET(eepromDump->KtaAvg_ColumnEven); // Get Kta Row Even Column Even value
KtaAvgTable[1] = MLX90640_KtaAvg_ROW_ODD_COL_EVEN_GET(eepromDump->KtaAvg_ColumnEven); // Get Kta Row Odd Column Even value
KtaAvgTable[2] = MLX90640_KtaAvg_ROW_EVEN_COL_ODD_GET(eepromDump->KtaAvg_ColumnOdd); // Get Kta Row Even Column Odd value
KtaAvgTable[3] = MLX90640_KtaAvg_ROW_ODD_COL_ODD_GET(eepromDump->KtaAvg_ColumnOdd); // Get Kta Row Odd Column Odd value
KtaScale2 = MLX90640_Kta_SCALE2_GET(eepromDump->K_Scale.Kscale); // Get Kta Scale 2 value
//--- Calculate and store Kta Pixels values ---
for(int y = 0; y < MLX90640_ROW_COUNT; ++y)
{
//--- Calculate and store pixel value ---
for(int x = 0; x < MLX90640_COL_COUNT; ++x)
{
Select = ((y & 0x1) << 1) + (x & 0x1); // Select the good Kta Average value in KtaAvgTable
KtaPixel = MLX90640_Kta_GET(eepromDump->PixelYXData[y][x].Pixel);
pComp->Params->KtaCoeffYX[y][x] = (float)((int32_t)KtaAvgTable[Select] + ((int32_t)KtaPixel << KtaScale2));
pComp->Params->KtaCoeffYX[y][x] /= (float)((uint32_t)1 << KtaScale1); // *** Pixel Kta restoring. See §11.1.6
}
}
#endif
}
//=============================================================================
// [STATIC] Calculate and store Kv Coefficients Pixels parameters on the MLX90640 device
//=============================================================================
void __MLX90640_ExtractKvCoeffPixelsParameters(MLX90640 *pComp, MLX90640_EEPROM *eepromDump)
{
int16_t KvCP;
uint16_t KvScale;
KvScale = MLX90640_Kv_SCALE_GET(eepromDump->K_Scale.Kscale); // Get Kv Scale value
KvCP = MLX90640_Kv_CP_GET(eepromDump->K_CP); // Get KvCP value
//--- Calculate and store KvCP value ---
pComp->Params->KvCP = (float)KvCP / (float)(1 << KvScale); // See §11.1.14
#ifdef MLX90640_PRECALCULATE_PIXELS_COEFFS
int16_t KvAvgTable[4];
size_t Select;
//--- Get values from EEPROM ---
KvAvgTable[0] = MLX90640_KvAVG_ROW_EVEN_COL_EVEN_GET(eepromDump->Kv_Avg.KvAvg); // Get Kv Row Even Column Even value
KvAvgTable[1] = MLX90640_KvAVG_ROW_ODD_COL_EVEN_GET(eepromDump->Kv_Avg.KvAvg); // Get Kv Row Odd Column Even value
KvAvgTable[2] = MLX90640_KvAVG_ROW_EVEN_COL_ODD_GET(eepromDump->Kv_Avg.KvAvg); // Get Kv Row Even Column Odd value
KvAvgTable[3] = MLX90640_KvAVG_ROW_ODD_COL_ODD_GET(eepromDump->Kv_Avg.KvAvg); // Get Kv Row Odd Column Odd value
//--- Calculate and store Kv Pixels values ---
for(int y = 0; y < MLX90640_ROW_COUNT; ++y)
{
//--- Calculate and store pixels values ---
for(int x = 0; x < MLX90640_COL_COUNT; ++x)
{
Select = ((y & 0x1) << 1) + (x & 0x1); // Select the good Kv Average value in KvAvgTable
pComp->Params->KvCoeffYX[y][x] = ((float)KvAvgTable[Select] / (float)(1 << KvScale)); // *** Pixel Kv restoring. See §11.1.5
}
}
#endif
}
//=============================================================================
// [STATIC] Calculate and store KsTo parameters on the MLX90640 device
//=============================================================================
void __MLX90640_ExtractKsToParameters(MLX90640 *pComp, MLX90640_EEPROM *eepromDump)
{
uint16_t Step;
uint16_t CT3;
uint16_t CT4;
uint16_t KsToScale;
int16_t KsToRange1;
int16_t KsToRange2;
int16_t KsToRange3;
int16_t KsToRange4;
//--- Get values from EEPROM ---
Step = MLX90640_TEMP_STEP_GET(eepromDump->CT_Temp.CTtemp); // Get Step value
CT3 = MLX90640_CT3_GET(eepromDump->CT_Temp.CTtemp); // Get CT3 value
CT4 = MLX90640_CT4_GET(eepromDump->CT_Temp.CTtemp); // Get CT4 value
KsToScale = MLX90640_KsTo_SCALE_OFFSET_GET(eepromDump->CT_Temp.CTtemp); // Get KsTo Scale Offset value
KsToRange1 = MLX90640_KsTo_CT_RANGE_1_GET(eepromDump->KsTo_Range1_2); // Get KsTo range 1 value
KsToRange2 = MLX90640_KsTo_CT_RANGE_2_GET(eepromDump->KsTo_Range1_2); // Get KsTo range 2 value
KsToRange3 = MLX90640_KsTo_CT_RANGE_3_GET(eepromDump->KsTo_Range3_4); // Get KsTo range 3 value
KsToRange4 = MLX90640_KsTo_CT_RANGE_4_GET(eepromDump->KsTo_Range3_4); // Get KsTo range 4 value
//--- Calculate and store CT values ---
Step *= 10;
pComp->Params->CT[MLX90640_CT1] = -40; // Fixed Corner Temperature 1. See §11.1.9
pComp->Params->CT[MLX90640_CT2] = 0; // Fixed Corner Temperature 2. See §11.1.9
pComp->Params->CT[MLX90640_CT3] = (int16_t)(CT3 * Step); // Calculate Corner Temperature 3. See §11.1.9
pComp->Params->CT[MLX90640_CT4] = (int16_t)((CT4 * Step) + pComp->Params->CT[MLX90640_CT3]); // Calculate Corner Temperature 4. See §11.1.9
//--- Calculate and store KsTo values ---
KsToScale += 8;
pComp->Params->KsTo[MLX90640_KsTo1] = (float)KsToRange1 / (float)(1 << KsToScale); // Calculate KsTo coefficient 1. See §11.1.10
pComp->Params->KsTo[MLX90640_KsTo2] = (float)KsToRange2 / (float)(1 << KsToScale); // Calculate KsTo coefficient 2. See §11.1.10
pComp->Params->KsTo[MLX90640_KsTo3] = (float)KsToRange3 / (float)(1 << KsToScale); // Calculate KsTo coefficient 3. See §11.1.10
pComp->Params->KsTo[MLX90640_KsTo4] = (float)KsToRange4 / (float)(1 << KsToScale); // Calculate KsTo coefficient 4. See §11.1.10
}
//=============================================================================
// [STATIC] Calculate and store Miscellaneous parameters on the MLX90640 device
//=============================================================================
void __MLX90640_ExtractMiscellaneousParameters(MLX90640 *pComp, MLX90640_EEPROM *eepromDump)
{
int16_t Gain;
int16_t TGC;
int16_t KsTa;
//--- Get values from EEPROM ---
Gain = eepromDump->Gain;
TGC = MLX90640_TGC_GET(eepromDump->TGC_KsTa);
KsTa = MLX90640_KsTa_GET(eepromDump->TGC_KsTa);
//--- Calculate and store values ---
pComp->Params->Gain = Gain; // See §11.1.7
pComp->Params->TGC = (float)TGC / (float)(1 << 5); // See §11.1.16
pComp->Params->KsTa = (float)KsTa / (float)(1 << 13); // See §11.1.8
}
//=============================================================================
// [STATIC] Extract defective pixels from the MLX90640 device
//=============================================================================
eERRORRESULT __MLX90640_ExtractDefectivePixels(MLX90640 *pComp, MLX90640_EEPROM *eepromDump)
{
size_t Current = 0;
for (size_t z = 0; z < MLX90640_MAX_DEFECT_PIXELS; ++z) pComp->Params->DefectivePixels[z].Type = MLX90640_NOT_DETECTIVE;
//--- Extract defective pixels ---
for(int y = 0; y < MLX90640_ROW_COUNT; ++y)
{
//--- Get pixel value ---
for(int x = 0; x < MLX90640_COL_COUNT; ++x)
{
//--- Check pixel ---
if (MLX90640_PIXEL_IS_BROKEN(eepromDump->PixelYXData[y][x].Pixel))
{
if (Current >= MLX90640_MAX_DEFECT_PIXELS) return ERR__TOO_MANY_BAD;
pComp->Params->DefectivePixels[Current].Type = MLX90640_BROKEN; // *** Set pixel. See note 1 page 21
pComp->Params->DefectivePixels[Current].X = x;
pComp->Params->DefectivePixels[Current].Y = y;
Current++;
}
else if (MLX90640_PIXEL_IS_OUTLIER(eepromDump->PixelYXData[y][x].Pixel))
{
if (Current >= MLX90640_MAX_DEFECT_PIXELS) return ERR__TOO_MANY_BAD;
pComp->Params->DefectivePixels[Current].Type = MLX90640_OUTLIER; // *** Set pixel. See §9
pComp->Params->DefectivePixels[Current].X = x;
pComp->Params->DefectivePixels[Current].Y = y;
Current++;
}
}
}
//--- Check if defective pixels are neighbors ---
for (size_t zCur = 0; zCur < Current; ++zCur)
{