-
Notifications
You must be signed in to change notification settings - Fork 0
/
tmag5170_ex.c
2423 lines (2012 loc) · 99.4 KB
/
tmag5170_ex.c
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
/**
* \copyright Copyright (C) 2022 Texas Instruments Incorporated - http://www.ti.com/
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
*
* Neither the name of Texas Instruments Incorporated nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "tmag5170.h"
/*
* These code examples were written with the objective of minimizing the amount
* of static variables tracking the states of the sensor they are used with. Because
* of this, functions in this code will send additional SPI read commands to get
* mostly unchanging data (such as the device version or set ranges) needed for
* other operations.
*
* The SYSTEM_CONFIG register must be stored and kept current with the sensor
* to keep track of the DATA_TYPE field so that Special Read Mode operation can
* be implemented successfully.
*/
//****************************************************************************
//
// Internal variables
//
//****************************************************************************
// These two arrays are used with the CORDIC function to convert the integer output
// into floating point values
const uint32_t magArray[16] = {1518500250,1358187913,1317635818,1307460871,
1304914694,1304277995,1304118810,1304079014,
1304069065,1304066577,1304065955,1304065800,
1304065761,1304065751,1304065749,1304065748};
const uint32_t atanArray32[16] = {536870912,316933406,167458907,85004756,42667331,
21354465,10679838,5340245,2670163,1335087,667544,
333772,166886,83443,41722,20861};
//****************************************************************************
//! This variable tracks the state of the SYSTEM_CONFIG register (0x02)
//! Since any special read state cannot read out registers when called,
//! keeping track of the SYSTEM_CONFIG register is needed in order to
//! switch to and from normal read mode without altering other bits besides
//! DATA_TYPE's (0x028-6).
//!
//! The macro 'DATA_TYPE_RESULTS' defined in the header can be used to access
//! the DATA_TYPE bits contained in the stored register.
//!
//! This variable also must be initialized using a function like the one
//! below before the rest of any code implementation using the given example
//! functions can work.
//****************************************************************************
static uint16_t SYSTEM_CONFIG_stored = 0;
#define DATA_TYPE_RESULTS ((SYSTEM_CONFIG_stored & ~(SYSTEM_CONFIG_DATA_TYPE_MASK)) >> 6)
#ifdef GLOBAL_CRC_ERROR_VAR_ENABLED
uint8_t crc_error_occurence = 0;
#endif
#ifdef GLOBAL_INPUT_ERROR_VAR_ENABLED
uint8_t fcn_input_error_occurence = 0;
#endif
//****************************************************************************
//! Initialization function
//!
//! This function MUST be ran at the beginning of any implementation using this
//! example code. This is needed because the SYSTEM_CONFIG register is needed to
//! be tracked and updated accordingly to prevent losing track of the DATA_TYPE
//! in use by the device.
//!
//! If CRC is being disabled per the header definition being enabled, then this
//! function also sends the command to disable CRC on the device as well.
//! NOTE: if CRC is being disabled then the RESET_DEVICE_IN_STARTUP should be
//! enabled as well
//****************************************************************************
void TMAG5170startup()
{
// (OPTIONAL) Provide additional delay time for power supply settling
delay_ms(50);
#ifdef RESET_DEVICE_IN_STARTUP
// SYSTEM_CONFIG_stored will be set to default in this function
resetDevice();
#else
// The default implementation of this initialize function is resetting only
// the SYSTEM_CONFIG register and leaving the other registers unchanged.
writeToRegister( SYSTEM_CONFIG_ADDRESS, SYSTEM_CONFIG_DEFAULT );
SYSTEM_CONFIG_stored = SYSTEM_CONFIG_DEFAULT;
#endif
#ifdef DISABLE_CRC_IN_STARTUP
// Sends command to disable CRC verfication and calculation on device-side
// MUST be called first in implementation for commands from other functions
// to be accepted when DISABLE_CRC_IN_STARTUP is used.
uint8_t dataTx[4] = {0x0F,0x00,0x04,0x07};
uint8_t dataRx[4] = {0};
sendAndReceiveFrame(dataTx,dataRx);
#endif
}
//****************************************************************************
//! Reset Device to Factory Settings
//!
//! This function uses the DeepSleep functions to reset the device's registers back to the
//! default settings outlined in the datasheet. This function also resets the
//! SYSTEM_CONFIG_stored variable to the default value in the enterDeepSleepMode function.
//****************************************************************************
void resetDevice()
{
enterDeepSleepMode(); // Deep Sleep Mode resets the device to its default settings
exitDeepSleepMode();
}
//****************************************************************************
//! Getter Functions for the SYSTEM_CONFIG_stored static variable
//!
//! These two functions return the stored SYSTEM_CONFIG register on the MCU or the
//! DATA_TYPE field within that stored register, respectively.
//****************************************************************************
uint16_t getSYSTEM_CONFIG_stored() { return SYSTEM_CONFIG_stored; }
uint16_t getDATA_TYPE() { return DATA_TYPE_RESULTS; }
//****************************************************************************
//****************************************************************************
//
// SPI fucntions
//
//****************************************************************************
//****************************************************************************
//****************************************************************************
//! Send and Receive Frame
//!
//! Takes in the frame to transmit by SPI and the array to put the received frame in
//!
//! dataTx[4] - unint8_t array of length 4 containing the bytes frame to transmit
//! ordered with CRC-containing byte last
//! dataRx[4] - unint8_t array of length 4 of zeroes on input, will have the received
//! frame bytes assigned to it ordered with CRC bits last
//****************************************************************************
void sendAndReceiveFrame(uint8_t dataTx[], uint8_t dataRx[])
{
#ifdef DISABLE_CRC_IN_STARTUP
// When CRC is disabled upon system initialization, the need to calculate
// the dataTx CRC for the TMAG5170 to accept commands is gone at the risk
// of transmission errors being passed through.
spiSendReceiveArrays(dataTx, dataRx, TMAG5170_FRAME_NUM_BYTES);
// Check if SYSTEM_CONFIG was written to and update SYSTEM_CONFIG_stored if so
if ( dataTx[0] == SYSTEM_CONFIG_ADDRESS ) SYSTEM_CONFIG_stored = (dataTx[1] << 8) | dataTx[2];
SYSTEM_CONFIG_stored &= ~(0xC818); // Reserved bits cannot be 1, this ensures the
// stored variable doesn't have them changed
#else
// When CRC is enabled (it is by default), the CRC must be calculated and
// included in dataTx for the TMAG5170 to accept commands.
uint8_t crc = calculateCRC( dataTx );
dataTx[3] = dataTx[3] | crc;
spiSendReceiveArrays(dataTx, dataRx, TMAG5170_FRAME_NUM_BYTES);
// The TMAG5170 also calculates the CRC for the frame it sends back to its
// MCU. The TMAG5170 will purposely return a bad CRC if it received a
// transmitted command with a bad CRC as well. Verifying the CRC of dataRx
// helps confirm if dataTx was received successfully and if dataRx is valid.
if (verifyCRC(dataRx) == 0)
{
// Without GLOBAL_CRC_ERROR_VAR_ENABLED defined or another error response
// implemented, this function will not give any indication that the
// received data has an error
#ifdef SEND_RECIEVE_REDUNDANCY_ENABLED
for ( i = 0; i<4; i++ ) { dataRx[i] = 0 };
spiSendReceiveArrays(dataTx, dataRx, TMAG5170_FRAME_NUM_BYTES);
if ( verifyCRC(dataRx) )
{
// Check if SYSTEM_CONFIG was written to and update SYSTEM_CONFIG_stored if so
if ( dataTx[0] == SYSTEM_CONFIG_ADDRESS ) SYSTEM_CONFIG_stored = (dataTx[1] << 8) | dataTx[2];
SYSTEM_CONFIG_stored &= ~(0xC818); // Reserved bits cannot be 1, this ensures the
// stored variable doesn't have them changed
return;
}
#endif
#ifdef GLOBAL_CRC_ERROR_VAR_ENABLED
crc_error_occurence = 1; // If redundancy is enabled too, the error bit
// only flips if both sent TXs receive bad CRCs
#endif
return; // If a received data CRC error is detected, SYSTEM_CONFIG_stored
// will not be updated under the assumption that whatever command
// sent was not accepted.
}
// Check if SYSTEM_CONFIG was written to and update SYSTEM_CONFIG_stored if so
if ( dataTx[0] == SYSTEM_CONFIG_ADDRESS ) SYSTEM_CONFIG_stored = (dataTx[1] << 8) | dataTx[2];
SYSTEM_CONFIG_stored &= ~(0xC818); // Reserved bits cannot be 1, this ensures the
// stored variable doesn't have them changed
#endif
}
//****************************************************************************
//! Write to Register Function (no CMD sent or value returned)
//!
//! Takes in the address of the register to edit and the 16-bit frame to overwrite it with and writes
//! the input frame to the register.
//!
//! This function replaces the whole register with the input data, make sure the values desired to be
//! unchanged are in the input data_to_write!
//!
//! This function will work in Normal and Special Read Mode.
//!
//! address - uint8_t value from 0x00 to 0x14 containing the register address to write over
//! data_to_write - the 16-bit frame to be written to the register at address.
//****************************************************************************
void writeToRegister(uint8_t address, uint16_t data_to_write)
{
// Check that the input address is in range
assert(address < NUM_REGISTERS);
// Build TX and RX byte arrays
uint8_t dataTx[4] = { 0 };
uint8_t dataRx[4] = { 0 };
// MSB is 0 for WRITE command
dataTx[0] = (address);
dataTx[1] = (data_to_write >> 8);
dataTx[2] = (data_to_write);
dataTx[3] = 0x00;
sendAndReceiveFrame(dataTx, dataRx);
}
//****************************************************************************
//! Write to Register Function (CMD0 sent)
//!
//! Takes in the address of the register to edit and the 16-bit frame to overwrite it with and writes
//! the frame to the register.
//! Sends CMD0 function (cmd_bits = 0x01), triggering conversion if device trigger is set to
//! 'start at SPI command' [TRIGGER_MODE (0x02A-9) == 00b]
//!
//! This function replaces the whole register with the input data, make sure the values desired to be
//! unchanged are in the input data_to_write!
//!
//! This function will work in Normal and Special Read Mode.
//!
//! address - uint8_t value from 0x00 to 0x14 containing the register address to write over
//! data_to_write - the 16-bit frame to be written to the register at address.
//****************************************************************************
void writeToRegisterWithCMD0(uint8_t address, uint16_t data_to_write)
{
// Check that the input address is in range
assert(address < NUM_REGISTERS);
// Build TX and RX byte arrays
uint8_t dataTx[4] = { 0 };
uint8_t dataRx[4] = { 0 };
// MSB is 0 for WRITE command
dataTx[0] = (address);
dataTx[1] = (data_to_write >> 8);
dataTx[2] = (data_to_write);
dataTx[3] = 0x10;
sendAndReceiveFrame(dataTx, dataRx);
}
//****************************************************************************
//! Write to Register Function (Send CMD bits and return STAT)
//!
//! Takes in the address of the register to edit and the 16-bit frame to overwrite it with and writes
//! the frame to the register.
//! Sends CMD0 and CMD1 depending on cmd_bits setting and returns the STAT values returned by the device.
//! (STAT4 - STAT11 are not returned by the device in Special Read Mode)
//!
//! This function replaces the whole register with the input data, make sure the values desired to be
//! unchanged are in the input data_to_write!
//!
//! This function will work in Normal and Special Read Mode.
//!
//! address - uint8_t value from 0x00 to 0x14 containing the register address to write over
//! data_to_write - the 16-bit frame to be written to the register at address.
//! cmd_bits - uint8_t value from 0x00 to 0x03 containing the CMD0 and CMD1 bits that will be sent
//! in dataTx. LSB is CMD0, next bit is CMD1. (see header file or datasheet pg. 29 for CMD functions)
//****************************************************************************
uint16_t writeToRegisterWithSTAT(uint8_t address, uint16_t data_to_write, uint8_t cmd_bits)
{
// Check that the input address is in range
assert(address < NUM_REGISTERS);
// Build TX and RX byte arrays
uint8_t dataTx[4] = { 0 };
uint8_t dataRx[4] = { 0 };
// MSB is 0 for WRITE command
dataTx[0] = (address);
dataTx[1] = (data_to_write >> 8);
dataTx[2] = (data_to_write);
dataTx[3] = (cmd_bits << 4);
sendAndReceiveFrame(dataTx, dataRx);
if ( DATA_TYPE_RESULTS == 0x00 )
{
return (cmd_bits << 12) | (dataRx[0] << 4) | (dataRx[3] >> 4);
}
else
{
return (cmd_bits << 12) | (dataRx[3] >> 4);
}
// returned bits: | 15 | 14 | 13 | 12 | 11 | 10 | 09 | 08 | 07 | 06 | 05 | 04 | 03 | 02 | 01 | 00 |
// | 0 | 0 | cmd_bits| -------- STAT11 - STAT0 -------- |
// if DATA_TYPE != 000b (Special Read Mode) then only STAT0 to STAT3 will be returned
// (all other STAT bits will be returned as zero)
}
//****************************************************************************
//! Full Read Function for Normal Data Mode (DATA_TYPE = 000b)
//!
//! Takes in an output array of length 2, address to read from, and CMD bits to send along,
//! then creates the dataTx array and calls the sendAndReturnFrame function, interpreting
//! dataRx and putting the read register in output[0] and status bits in output[1]
//!
//! output[2] - empty uint16_t array of length 2, output[0] will be assigned the returned register
//! at the given address, output[1] will be assigned the returned status bits.
//! address - uint8_t value from 0x00 to 0x14 containing the register address to read from
//! cmd_bits - uint8_t value from 0x00 to 0x03 containing the CMD0 and CMD1 bits that will be sent
//! in dataTx. LSB is CMD0, next bit is CMD1. (see header file or datasheet pg. 29 for CMD functions)
//****************************************************************************
void normalRead( uint16_t output[], uint8_t address, uint8_t cmd_bits )
{
// Check that the input address is in range
assert(address < NUM_REGISTERS);
// Build TX and RX byte arrays
uint8_t dataTx[4] = { 0 };
uint8_t dataRx[4] = { 0 };
// MSB is 1 for READ command
dataTx[0] = (address | 0x80);
dataTx[1] = 0x00;
dataTx[2] = 0x00;
dataTx[3] = cmd_bits << 4;
sendAndReceiveFrame(dataTx, dataRx);
output[0] = (dataRx[1] << 8) | dataRx[2];
output[1] = (dataRx[0] << 4) | (dataRx[3] >> 4);
}
//****************************************************************************
//! Register-only Read Function for Normal Data Mode (DATA_TYPE = 000b)
//!
//! Takes in address to read from and returns register at address without the status bits or
//! triggering any CMD function (cmd_bits = 0x00).
//!
//! address - uint8_t value from 0x00 to 0x14 containing the register address to read from
//****************************************************************************
uint16_t normalReadRegister( uint8_t address )
{
uint16_t output[2] = { 0 };
normalRead( output, address, 0x00 );
return output[0];
}
//****************************************************************************
//! Register-only Read Function for Normal Data Mode with CMD0 sent (DATA_TYPE = 000b)
//!
//! Takes in address to read from and returns register at address without the status bits.
//! Sends CMD0 function (cmd_bits = 0x01), triggering conversion if device trigger is set to
//! 'start at SPI command' [TRIGGER_MODE (0x02A-9) == 00b]
//!
//! address - uint8_t value from 0x00 to 0x14 containing the register address to read from
//****************************************************************************
uint16_t normalReadRegisterWithCMD0( uint8_t address )
{
uint16_t output[2] = { 0 };
normalRead( output, address, 0x01 );
return output[0];
}
//****************************************************************************
//! Status-bits-only Read Function for Normal Data Mode (DATA_TYPE = 000b)
//!
//! Takes in address to read from and CMD bits and returns the used CMD bits and
//! status bits WITHOUT a read register. (by default this function sends a read command for the
//! DEVICE_CONFIG register)
//!
//! cmd_bits - uint8_t value from 0x00 to 0x03 containing the CMD0 and CMD1 bits that will be sent
//! in dataTx. LSB is CMD0, next bit is CMD1. (see header file or datasheet pg. 29 for CMD functions)
//****************************************************************************
uint16_t normalReadSTAT( uint8_t cmd_bits )
{
uint16_t output[2] = { 0 };
normalRead( output, DEVICE_CONFIG_ADDRESS, cmd_bits );
return (cmd_bits << 12) | output[0];
// returned bits: | 15 | 14 | 13 | 12 | 11 | 10 | 09 | 08 | 07 | 06 | 05 | 04 | 03 | 02 | 01 | 00 |
// | 0 | 0 | cmd_bits| -------- STAT11 - STAT0 -------- |
}
//****************************************************************************
//! Read Function for Special Data Mode (DATA_TYPE != 000b)
//!
//! Takes in an output array of length 3, address to read from, and CMD bits to send along,
//! then creates the dataTx array and calls the sendAndReturnFrame function, interpreting
//! dataRx according to Special Data Mode and putting the first measurement channel in output[0],
//! second measurement channel in output[1], and status bits in output[2]
//!
//! output[3] - empty uint16_t array of length 3, output[0] will be assigned the CH1 measurement,
//! output[1] will be assigned the CH2 measurement, and output[2] will be assigned the
//! returned status bits.
//! cmd_bits - uint8_t value from 0x00 to 0x03 containing the CMD0 and CMD1 bits that will be sent
//! in dataTx. LSB is CMD0, next bit is CMD1. (see header file or datasheet pg. 29 for CMD functions)
//****************************************************************************
void specialRead( uint16_t output[], uint8_t cmd_bits )
{
// Build TX and RX byte arrays
uint8_t dataTx[4] = { 0 };
uint8_t dataRx[4] = { 0 };
dataTx[0] = 0x80;
dataTx[1] = 0x00;
dataTx[2] = 0x00;
dataTx[3] = cmd_bits << 4;
sendAndReceiveFrame(dataTx, dataRx);
// The output values have their respective bits assembled from the RX frame
output[0] = output[1] = 0x0000;
output[0] = ((dataRx[1] & 0x0F) << 8);
output[0] |= (dataRx[2] & 0xF0);
output[0] |= (dataRx[3] >> 4);
output[1] = ((dataRx[0] & 0x0F) << 8);
output[1] |= (dataRx[1] & 0xF0);
output[1] |= (dataRx[2] & 0x0F);
output[2] = 0x0000;
output[2] = (dataRx[0] >> 4);
}
//****************************************************************************
//! Status-bits-only Read Function for Special Data Mode (DATA_TYPE != 000b)
//!
//! Takes in CMD bits to send on dataTx and returns the received status bits without
//! the measurement channel values.
//!
//! cmd_bits - uint8_t value from 0x00 to 0x03 containing the CMD0 and CMD1 bits that will be sent
//! in dataTx. LSB is CMD0, next bit is CMD1. (see header file or datasheet pg. 29 for CMD functions)
//****************************************************************************
uint16_t specialReadSTAT( uint8_t cmd_bits )
{
uint16_t output[3];
specialRead( output, cmd_bits );
return output[2];
}
//****************************************************************************
//****************************************************************************
//
// Change Device Operation Mode
//
//****************************************************************************
//****************************************************************************
//****************************************************************************
//! Enter Configuration Mode
//!
//! DOES NOT WORK IN SPECIAL READ MODE [DATA_TYPE field at 0x028-6 does not equal 000b]
//****************************************************************************
void enterConfigurationMode()
{
// To prevent undefined behavior, this function does not perform its operation
// when the DATA_TYPE field (address: 0x028-6) is not set to Normal Read Mode (000b)
if ( DATA_TYPE_RESULTS != DATA_TYPE_RESULTS_NormalMode ) return;
uint16_t input;
// Set OPERATING_MODE (address: 0x006-4) to Configuration Mode (0h)
input = normalReadRegister(DEVICE_CONFIG_ADDRESS);
input = ( input & ~(DEVICE_CONFIG_OPERATING_MODE_MASK) ) | DEVICE_CONFIG_OPERATING_MODE_ConfigurationMode;
writeToRegister( DEVICE_CONFIG_ADDRESS, input );
#ifdef MAX_DELAYS_IN_OPMODE_CHANGES
delay_us(50); // max expected delay as given by t_start_sleep (datasheet pg. 10)
#endif
}
//****************************************************************************
//! Enter Stand-by Mode
//!
//! DOES NOT WORK IN SPECIAL READ MODE [DATA_TYPE field at 0x028-6 does not equal 000b]
//****************************************************************************
void enterStandbyMode()
{
// To prevent undefined behavior, this function does not perform its operation
// when the DATA_TYPE field (address: 0x028-6) is not set to Normal Read Mode (000b)
if ( DATA_TYPE_RESULTS != DATA_TYPE_RESULTS_NormalMode ) return;
uint16_t input;
// Set OPERATING_MODE (address: 0x006-4) to Standby Mode (1h)
input = normalReadRegister(DEVICE_CONFIG_ADDRESS);
input = ( input & ~(DEVICE_CONFIG_OPERATING_MODE_MASK) ) | DEVICE_CONFIG_OPERATING_MODE_StandbyMode;
writeToRegister( DEVICE_CONFIG_ADDRESS, input );
#ifdef MAX_DELAYS_IN_OPMODE_CHANGES
delay_us(140); // max expected delay as given by t_start_sleep + t_stand_by (datasheet pg. 10)
#endif
}
//****************************************************************************
//! Enter Active Measure Mode (continuous conversion)
//!
//! DOES NOT WORK IN SPECIAL READ MODE [DATA_TYPE field at 0x028-6 does not equal 000b]
//****************************************************************************
void enterActiveMeasureMode()
{
// To prevent undefined behavior, this function does not perform its operation
// when the DATA_TYPE field (address: 0x028-6) is not set to Normal Read Mode (000b)
if ( DATA_TYPE_RESULTS != DATA_TYPE_RESULTS_NormalMode ) return;
uint16_t input;
// Set OPERATING_MODE (0x006-4) to Active Measure Mode (2h)
input = normalReadRegister(DEVICE_CONFIG_ADDRESS);
input = ( input & ~(DEVICE_CONFIG_OPERATING_MODE_MASK) ) | DEVICE_CONFIG_OPERATING_MODE_ActiveMeasureMode;
writeToRegister( DEVICE_CONFIG_ADDRESS, input );
#ifdef MAX_DELAYS_IN_OPMODE_CHANGES
delay_us(140); // max expected delay as given by t_start_sleep + t_stand_by (datasheet pg. 10)
#endif
}
//****************************************************************************
//! Enter Active Trigger Mode
//!
//! DOES NOT WORK IN SPECIAL READ MODE [DATA_TYPE field at 0x028-6 does not equal 000b]
//****************************************************************************
void enterActiveTriggerMode()
{
// To prevent undefined behavior, this function does not perform its operation
// when the DATA_TYPE field (0x028-6) is not set to Normal Read Mode (000b)
if ( DATA_TYPE_RESULTS != DATA_TYPE_RESULTS_NormalMode ) return;
uint16_t input;
// Set OPERATING_MODE (0x006-4) to Active Trigger Mode (3h)
input = normalReadRegister(DEVICE_CONFIG_ADDRESS);
input = ( input & ~(DEVICE_CONFIG_OPERATING_MODE_MASK) ) | DEVICE_CONFIG_OPERATING_MODE_ActiveTriggerMode;
writeToRegister( DEVICE_CONFIG_ADDRESS, input );
#ifdef MAX_DELAYS_IN_OPMODE_CHANGES
delay_us(140); // max expected delay as given by t_start_sleep + t_stand_by (datasheet pg. 10)
#endif
}
//****************************************************************************
//! Enter Sleep Mode
//!
//! DOES NOT WORK IN SPECIAL READ MODE [DATA_TYPE field at 0x028-6 does not equal 000b]
//****************************************************************************
void enterSleepMode()
{
// To prevent undefined behavior, this function does not perform its operation
// when the DATA_TYPE field (0x028-6) is not set to Normal Read Mode (000b)
if ( DATA_TYPE_RESULTS != DATA_TYPE_RESULTS_NormalMode ) return;
uint16_t input;
// Set OPERATING_MODE (0x006-4) to Sleep Mode (5h)
input = normalReadRegister(DEVICE_CONFIG_ADDRESS);
input = ( input & ~(DEVICE_CONFIG_OPERATING_MODE_MASK) ) | DEVICE_CONFIG_OPERATING_MODE_SleepMode;
writeToRegister( DEVICE_CONFIG_ADDRESS, input );
#ifdef MAX_DELAYS_IN_OPMODE_CHANGES
delay_us(60); // max expected delay as given by t_go_sleep (datasheet pg. 10)
#endif
}
//****************************************************************************
//! Enter Wake and Sleep Mode
//! Does not change the SLEEPTIME bits
//!
//! DOES NOT WORK IN SPECIAL READ MODE [DATA_TYPE field at 0x028-6 does not equal 000b]
//****************************************************************************
void enterWakeUpAndSleepMode()
{
// To prevent undefined behavior, this function does not perform its operation
// when the DATA_TYPE field (0x028-6) is not set to Normal Read Mode (000b)
if ( DATA_TYPE_RESULTS != DATA_TYPE_RESULTS_NormalMode ) return;
uint16_t input;
// Set OPERATING_MODE (0x006-4) to Wake-Up and Sleep Mode (4h)
input = normalReadRegister(DEVICE_CONFIG_ADDRESS);
input = ( input & ~(DEVICE_CONFIG_OPERATING_MODE_MASK) ) | DEVICE_CONFIG_OPERATING_MODE_WakeupandSleepMode;
writeToRegister( DEVICE_CONFIG_ADDRESS, input );
#ifdef MAX_DELAYS_IN_OPMODE_CHANGES
delay_us(60); // max expected delay as given by t_go_sleep (datasheet pg. 10)
#endif
}
//****************************************************************************
//! Set SLEEPTIME
//! Takes in input from 0x00 to 0x09 to determine SLEEPTIME field according to datasheet (pg. 35)
//! Does not change OPERATING_MODE and will not begin Wake and Sleep Mode
//!
//! SLEEPTIME determines the amount of time spend in low power mode between device conversions
//! while in Wake Up and Sleep mode (OPERATING_MODE = 4h)
//! |
//! SLEEPTIME bits | time between convs.
//! _________________|_______________________
//! 0x00 1ms
//! 0x01 5ms
//! 0x02 10ms
//! 0x03 15ms
//! 0x04 20ms
//! 0x05 30ms
//! 0x06 50ms
//! 0x07 100ms
//! 0x08 500ms
//! 0x09 1000ms
//!
//! DOES NOT WORK IN SPECIAL READ MODE [DATA_TYPE field at 0x028-6 does not equal 000b]
//****************************************************************************
void setSLEEPTIME( uint8_t sleeptime )
{
// To prevent undefined behavior, this function does not perform its operation
// when the DATA_TYPE field (0x028-6) is not set to Normal Read Mode (000b)
if ( DATA_TYPE_RESULTS != DATA_TYPE_RESULTS_NormalMode ) return;
// Check that inputs are valid
if ( !(sleeptime <= 0x09) ) return;
uint16_t input;
// Set SLEEPTIME (0x01D-A) to input value (corresponding time values are on datasheet pg. 35)
input = normalReadRegister(SENSOR_CONFIG_ADDRESS);
input = ( input & ~(SENSOR_CONFIG_SLEEPTIME_MASK) ) | (sleeptime << 10);
writeToRegister( SENSOR_CONFIG_ADDRESS, input );
}
//****************************************************************************
//! Wake Up and Sleep Mode with SLEEPTIME set
//! Takes in input from 0x00 to 0x0F to set the SLEEPTIME field according to datasheet
//!
//! DOES NOT WORK IN SPECIAL READ MODE [DATA_TYPE field at 0x028-6 does not equal 000b]
//****************************************************************************
void setWakeUpAndSleepMode( uint8_t sleeptime )
{
setSLEEPTIME(sleeptime);
enterWakeUpAndSleepMode();
}
//****************************************************************************
//! Enter Deep Sleep Mode
//!
//! Make sure to use the exitDeepSleepMode function to properly exit Deep Sleep Mode.
//! Deep Sleep Mode can be alternately exited with a short pulse on the CS pin.
//!
//! WILL WORK IN SPECIAL READ MODE, DEEP SLEEP MODE RESETS DEVICE TO FACTORY SETTINGS
//! (EXITS SPECIAL READ MODE)
//****************************************************************************
void enterDeepSleepMode()
{
// Send Write command, Deep Sleep resets device to factory settings so
// an initial register read is not needed (DEVICE_CONFIG default is 0x0000)
writeToRegister( DEVICE_CONFIG_ADDRESS, DEVICE_CONFIG_OPERATING_MODE_DeepSleepMode );
SYSTEM_CONFIG_stored = SYSTEM_CONFIG_DEFAULT;
delay_us(100);
}
//****************************************************************************
//! Exit Sleep Mode
//!
//! Exits Sleep Mode by setting the OP_MODE field to Configuration Mode.
//!
//! DOES NOT WORK IN SPECIAL READ MODE [DATA_TYPE field at 0x028-6 does not equal 000b]
//****************************************************************************
void exitSleepMode()
{
// To prevent undefined behavior, this function does not perform its operation
// when the DATA_TYPE field (0x028-6) is not set to Normal Read Mode (000b)
if ( DATA_TYPE_RESULTS != DATA_TYPE_RESULTS_NormalMode ) return;
uint16_t input;
// Set OPERATING_MODE (0x006-4) to Configuration Mode (0h)
input = normalReadRegister(DEVICE_CONFIG_ADDRESS);
input = ( input & ~(DEVICE_CONFIG_OPERATING_MODE_MASK) ) | DEVICE_CONFIG_OPERATING_MODE_ConfigurationMode;
writeToRegister( DEVICE_CONFIG_ADDRESS, input );
#ifdef MAX_DELAYS_IN_OPMODE_CHANGES
delay_us(140); // max expected delay as given by t_start_sleep + t_stand_by (datasheet pg. 10)
#endif
}
//****************************************************************************
//! Exit Wake Up and Sleep Mode
//!
//! Exits Wake Up and Sleep Mode by setting the OP_MODE field to Configuration Mode.
//!
//! DOES NOT WORK IN SPECIAL READ MODE [DATA_TYPE field at 0x028-6 does not equal 000b]
//****************************************************************************
void exitWakeAndSleepMode()
{
// To prevent undefined behavior, this function does not perform its operation
// when the DATA_TYPE field (0x028-6) is not set to Normal Read Mode (000b)
if ( DATA_TYPE_RESULTS != DATA_TYPE_RESULTS_NormalMode ) return;
uint16_t input;
// Set OPERATING_MODE (0x006-4) to Configuration Mode (0h)
input = normalReadRegister(DEVICE_CONFIG_ADDRESS);
input = ( input & ~(DEVICE_CONFIG_OPERATING_MODE_MASK) ) | DEVICE_CONFIG_OPERATING_MODE_ConfigurationMode;
writeToRegister( DEVICE_CONFIG_ADDRESS, input );
#ifdef MAX_DELAYS_IN_OPMODE_CHANGES
delay_us(140); // max expected delay as given by t_start_sleep + t_stand_by (datasheet pg. 10)
#endif
}
//****************************************************************************
//! Exit Deep Sleep Mode
//! (Use this function instead of a different operation mode change function to
//! exit Deep Sleep mode properly)
//!
//! Exits Deep Sleep Mode by pulsing LOW on the CS pin and waiting for the chip
//! to start up. (t_start_deep_sleep)
//!
//! DOES NOT WORK IN SPECIAL READ MODE [DATA_TYPE field at 0x028-6 does not equal 000b]
//****************************************************************************
void exitDeepSleepMode()
{
// A LOW pulse is needed on CS to exit Deep Sleep Mode (enters Configuration Mode)
csPulse();
#ifdef MAX_DELAYS_IN_OPMODE_CHANGES
delay_us(500); // max expected delay as given by t_start_deep_sleep (datasheet pg. 10)
#else
delay_us(260); // typical delay for t_start_deep_sleep at Vcc = 2.3V (datasheet pg. 10)
#endif
}
//****************************************************************************
//****************************************************************************
//
// Functions to Configure Trigger Settings
//
//****************************************************************************
//****************************************************************************
//****************************************************************************
//! SPI to to Trigger Conversion
//!
//! Configures the conversion trigger to be activated on a SPI read or write command with CMD0 enabled.
//! (Use normalReadRegisterWithCMD0, writeToRegisterWithCMD0, or any R/W function with a 'cmd_bits'
//! input to send CMD0 in sent frame)
//!
//! DOES NOT WORK IN SPECIAL READ MODE [DATA_TYPE field at 0x028-6 does not equal 000b]
//****************************************************************************
void spiTriggersConversion()
{
// To prevent undefined behavior, this function does not perform its operation
// when the DATA_TYPE field (address: 0x028-6) is not set to Normal Read Mode (000b)
if ( DATA_TYPE_RESULTS != DATA_TYPE_RESULTS_NormalMode ) return;
uint16_t input;
// SET TRIGGER_MODE (address: 0x02A-9) to 'trigger at SPI CMD0' (0h)
input = normalReadRegister(SYSTEM_CONFIG_ADDRESS);
input &= ~(SYSTEM_CONFIG_TRIGGER_MODE_MASK);
input |= SYSTEM_CONFIG_TRIGGER_MODE_AtSPIcommandbits;
writeToRegister( SYSTEM_CONFIG_ADDRESS, input );
}
//****************************************************************************
//! CS pulse to Trigger Conversion
//!
//! Configures the conversion trigger to be activated on a LOW pulse (1us to 25us in length) to CS.
//!
//! DOES NOT WORK IN SPECIAL READ MODE [DATA_TYPE field at 0x028-6 does not equal 000b]
//****************************************************************************
void csTriggersConversion()
{
// To prevent undefined behavior, this function does not perform its operation
// when the DATA_TYPE field (address: 0x028-6) is not set to Normal Read Mode (000b)
if ( DATA_TYPE_RESULTS != DATA_TYPE_RESULTS_NormalMode ) return;
uint16_t input;
// SET TRIGGER_MODE (address: 0x02A-9) to at CS pulse (1h)
input = normalReadRegister(SYSTEM_CONFIG_ADDRESS);
input &= ~(SYSTEM_CONFIG_TRIGGER_MODE_MASK);
input |= SYSTEM_CONFIG_TRIGGER_MODE_AtnCSSyncPulse;
writeToRegister( SYSTEM_CONFIG_ADDRESS, input );
}
//****************************************************************************
//! ALERT to Trigger Conversion
//!
//! Configures the conversion trigger to be activated on a LOW pulse (1us to 25us in length) to ALERT.
//! ALERT_MODE must be in Interrupt & Trigger Mode for this functionality to work.
//!
//! DOES NOT WORK IN SPECIAL READ MODE [DATA_TYPE field at 0x028-6 does not equal 000b]
//****************************************************************************
void alertTriggersConversion()
{
// To prevent undefined behavior, this function does not perform its operation
// when the DATA_TYPE field (0x028-6) is not set to Normal Read Mode (000b)
if ( DATA_TYPE_RESULTS != DATA_TYPE_RESULTS_NormalMode ) return;
uint16_t input;
// SET TRIGGER_MODE (address: 0x02A-9) to at ALERT pulse (2h)
input = normalReadRegister(SYSTEM_CONFIG_ADDRESS);
input &= ~(SYSTEM_CONFIG_TRIGGER_MODE_MASK);
input |= SYSTEM_CONFIG_TRIGGER_MODE_AtALERTSyncPulse;
writeToRegister( SYSTEM_CONFIG_ADDRESS, input );
// SET ALERT_MODE (address: 0x03C) to Interrupt & Trigger Mode (0h)
input = normalReadRegister(ALERT_CONFIG_ADDRESS);
input &= ~(ALERT_CONFIG_ALERT_MODE_MASK);
input |= ALERT_CONFIG_ALERT_MODE_InterruptandTriggerMode;
writeToRegister( ALERT_CONFIG_ADDRESS, input );
}
//****************************************************************************
//****************************************************************************
//
// Threshold Detection + ALERT output Settings
//
//****************************************************************************
//****************************************************************************
//****************************************************************************
//! Enable ALERT to Indicate Conversion
//!
//! Configures the device so when its magnetic measurements are complete, the device will
//! output LOW on the ALERT pin.
//!
//! NOTE: This configures ALERT as an output pin, the respective GPIO pin it is
//! connected to will have to be set as an input as well. Please ensure none
//! of the input functions of ALERT are being used as well (such as the ALERT trigger)
//!
//! DOES NOT WORK IN SPECIAL READ MODE [DATA_TYPE field at 0x028-6 does not equal 000b]
//****************************************************************************
void alertIndicatesConversionEnable()
{
// To prevent undefined behavior, this function does not perform its operation
// when the DATA_TYPE field (address: 0x028-6) is not set to Normal Read Mode (000b)
if ( DATA_TYPE_RESULTS != DATA_TYPE_RESULTS_NormalMode ) return;
uint16_t input;
// SET ALERT_MODE (address: 0x03C) to Interrupt Mode (0h)
input = normalReadRegister(ALERT_CONFIG_ADDRESS);
input &= ~(ALERT_CONFIG_ALERT_MODE_MASK);
input |= ALERT_CONFIG_ALERT_MODE_InterruptandTriggerMode;
// SET RSLT_ALRT (address: 0x038) to ALERT output asserted LOW to indicate conversion completion (1h)
// (Register already grabbed so no new READ command is needed)
input |= ALERT_CONFIG_RSLT_ALRT_ALERToutputsignalsconversioncomplete;
writeToRegister( ALERT_CONFIG_ADDRESS, input );
}
//****************************************************************************
//! Disable ALERT to Indicate Conversion
//!
//! Turns off the ALERT pin assertion when device measurements are complete.
//!
//! DOES NOT WORK IN SPECIAL READ MODE [DATA_TYPE field at 0x028-6 does not equal 000b]
//****************************************************************************
void alertIndicatesConversionDisable()
{
// To prevent undefined behavior, this function does not perform its operation
// when the DATA_TYPE field (address: 0x028-6) is not set to Normal Read Mode (000b)
if ( DATA_TYPE_RESULTS != DATA_TYPE_RESULTS_NormalMode ) return;
uint16_t input;
// SET RSLT_ALRT (address: 0x038) ALERT output is not asserted to indicate conversion completion (1h)
input = normalReadRegister(ALERT_CONFIG_ADDRESS);
input &= ~(ALERT_CONFIG_RSLT_ALRT_MASK);
input |= ALERT_CONFIG_RSLT_ALRT_ALERTisnotusedtosignalconversioncompleteion;
writeToRegister( ALERT_CONFIG_ADDRESS, input );
}
//****************************************************************************
//! Setup Magnetic Switch Mode
//!
//! Enables Magnetic Switch Mode for the ALERT pin and the respective high/low
//! threshold triggers. The thresholds are not set using this function, use
//! magThreshSet to change the high/low thresholds for each measurement.
//!
//! thresholds_en - input determining the state of each threshold
//! (4 LSBs of input int correlate to which thresholds are enabled in
//! order TZYX, cannot be greater than 0x0F)
//! examples:
//! thresholds_en = 0x0A (1010b) ==> T and Y are enabled, Z and X are disabled
//! thresholds_en = 0x04 (0100b) ==> Z is enabled, T, Y, and X are disabled
//! --inputs of 0x00 and 0x0F can be used to disable or enable all thresholds, respectively
//!
//! NOTE: This configures ALERT as an output pin, the respective GPIO pin it is
//! connected to will have to be set as an input as well. Please ensure none
//! of the input functions of ALERT are being used as well (such as the ALERT trigger)
//!
//! DOES NOT WORK IN SPECIAL READ MODE [DATA_TYPE field at 0x028-6 does not equal 000b]
//****************************************************************************
void setMagSwitch( uint8_t thresholds_en )
{
// To prevent undefined behavior, this function does not perform its operation