87
87
*
88
88
*/
89
89
class MCP3x6x {
90
- const uint8_t _DEFAULT_CONFIG0 = 0xC0 ; // !< default value
91
- const uint8_t _DEFAULT_CONFIG1 = 0x0C ; // !< default value
92
- const uint8_t _DEFAULT_CONFIG2 = 0x8B ; // !< default value
93
- const uint8_t _DEFAULT_CONFIG3 = 0x00 ; // !< default value
94
- const uint8_t _DEFAULT_IRQ = 0x73 ; // !< default value
95
- const uint8_t _DEFAULT_MUX = 0x01 ; // !< default value
96
- const uint8_t _DEFAULT_SCAN[3 ] = {0x00 , 0x00 , 0x00 }; // !< default value
97
- const uint8_t _DEFAULT_TIMER[3 ] = {0x00 , 0x00 , 0x00 }; // !< default value
98
- const uint8_t _DEFAULT_OFFSET[3 ] = {0x00 , 0x00 , 0x00 }; // !< default value
99
- const uint8_t _DEFAULT_GAIN[3 ] = {0x80 , 0x00 , 0x00 }; // !< default value
100
- const uint8_t _DEFAULT_RESERVED1[3 ] = {0x90 , 0x00 , 0x00 }; // !< default value
101
- const uint8_t _DEFAULT_RESERVED2 = 0x50 ; // !< default value
102
- const uint8_t _DEFAULT_LOCK = 0xA5 ; // !< default value
103
- const uint8_t _DEFAULT_CRCCFG[2 ] = {0x00 , 0x00 }; // !< default value
104
- const uint8_t _DEFAULTS[27 ] = {_DEFAULT_CONFIG0, _DEFAULT_CONFIG1, _DEFAULT_CONFIG2,
105
- _DEFAULT_CONFIG3, _DEFAULT_IRQ, _DEFAULT_MUX,
106
- *_DEFAULT_SCAN, *_DEFAULT_TIMER, *_DEFAULT_OFFSET,
107
- *_DEFAULT_GAIN, *_DEFAULT_RESERVED1, _DEFAULT_RESERVED2,
108
- _DEFAULT_LOCK, (uint16_t )0x0000 , *_DEFAULT_CRCCFG};
109
-
110
90
typedef union __attribute__ ((__packed__)) {
111
91
struct {
112
92
struct {
@@ -134,8 +114,7 @@ class MCP3x6x {
134
114
135
115
bool _differential = false ;
136
116
float _reference = 3.3 ;
137
- size_t _resolution, _resolution_max;
138
- size_t _channels_max;
117
+ size_t _resolution;
139
118
uint16_t _channel_mask;
140
119
const uint8_t _channelID[16 ] = {MCP_CH0, MCP_CH1, MCP_CH2, MCP_CH3, MCP_CH4, MCP_CH5,
141
120
MCP_CH6, MCP_CH7, MCP_DIFFA, MCP_DIFFB, MCP_DIFFC, MCP_DIFFD,
@@ -326,6 +305,7 @@ class MCP3x6x {
326
305
* href=https://ww1.microchip.com/downloads/aemDocuments/documents/APID/ProductDocuments/DataSheets/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf#G1.1576710>MCP346x.pdf</a>
327
306
*/
328
307
typedef union Config0 {
308
+ Config0 (uint8_t data) : raw (data) {}
329
309
struct {
330
310
enum adc_mode adc : 2 ; // !< ADC Operating Mode Selection
331
311
enum cs_sel bias : 2 ; // !< Current Source/Sink Selection Bits for Sensor Bias
@@ -343,6 +323,7 @@ class MCP3x6x {
343
323
* href=https://ww1.microchip.com/downloads/aemDocuments/documents/APID/ProductDocuments/DataSheets/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf#G1.1269089>MCP346x.pdf</a>
344
324
*/
345
325
typedef union Config1 {
326
+ Config1 (uint8_t data) : raw (data) {}
346
327
struct {
347
328
uint8_t : 2 ; // !< reserved
348
329
enum osr osr : 4 ; // !< Oversampling Ratio for Delta-Sigma A/D Conversion
@@ -358,6 +339,7 @@ class MCP3x6x {
358
339
* href=https://ww1.microchip.com/downloads/aemDocuments/documents/APID/ProductDocuments/DataSheets/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf#G1.1269283>MCP346x.pdf</a>
359
340
*/
360
341
typedef union Config2 {
342
+ Config2 (uint8_t data) : raw (data) {}
361
343
struct {
362
344
uint8_t : 2 ; // !< reserved // Should always be equal to ‘11’
363
345
bool az_mu : 1 ; // !< Auto-Zeroing MUX Setting
@@ -374,6 +356,7 @@ class MCP3x6x {
374
356
* href=https://ww1.microchip.com/downloads/aemDocuments/documents/APID/ProductDocuments/DataSheets/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf#G1.1269504>MCP346x.pdf</a>
375
357
*/
376
358
typedef union Config3 {
359
+ Config3 (uint8_t data) : raw (data) {}
377
360
struct {
378
361
bool en_gaincal : 1 ; // !< Enable Digital Gain Calibration
379
362
bool en_offcal : 1 ; // !< Enable Digital Offset Calibration
@@ -392,6 +375,7 @@ class MCP3x6x {
392
375
* href=https://ww1.microchip.com/downloads/aemDocuments/documents/APID/ProductDocuments/DataSheets/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf#G1.1269747>MCP346x.pdf</a>
393
376
*/
394
377
typedef union Irq {
378
+ Irq (uint8_t data) : raw (data) {}
395
379
struct {
396
380
bool en_stp : 1 ; // !< Enable Conversion Start Interrupt Output
397
381
bool en_fastcmd : 1 ; // !< Enable Fast Commands in the COMMAND Byte
@@ -411,11 +395,6 @@ class MCP3x6x {
411
395
* href=https://ww1.microchip.com/downloads/aemDocuments/documents/APID/ProductDocuments/DataSheets/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf#G1.1273028>MCP346x.pdf</a>
412
396
*/
413
397
typedef union Mux {
414
- /* *
415
- * @brief Construct a new Mux object
416
- *
417
- * @param data
418
- */
419
398
Mux (uint8_t data) : raw (data) {}
420
399
struct {
421
400
enum mux vin_minus : 4 ; // !< MUX_VIN- Input Selection
@@ -431,6 +410,7 @@ class MCP3x6x {
431
410
* href=https://ww1.microchip.com/downloads/aemDocuments/documents/APID/ProductDocuments/DataSheets/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf#G1.1270252>MCP346x.pdf</a>
432
411
*/
433
412
typedef union Scan {
413
+ Scan (const uint8_t data[3 ]) : raw{data[0 ], data[1 ], data[2 ]} {}
434
414
struct {
435
415
union {
436
416
struct {
@@ -457,6 +437,7 @@ class MCP3x6x {
457
437
* href=https://ww1.microchip.com/downloads/aemDocuments/documents/APID/ProductDocuments/DataSheets/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf#G1.1270583>MCP346x.pdf</a>
458
438
*/
459
439
typedef union Timer {
440
+ Timer (const uint8_t data[3 ]) : raw{data[0 ], data[1 ], data[2 ]} {}
460
441
uint8_t raw[3 ]; // !< Selection Bits for the Time Interval Between Two Consecutive Scan Cycles
461
442
} timer_t ;
462
443
@@ -467,6 +448,7 @@ class MCP3x6x {
467
448
* href=https://ww1.microchip.com/downloads/aemDocuments/documents/APID/ProductDocuments/DataSheets/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf#G1.1270742>MCP346x.pdf</a>
468
449
*/
469
450
typedef union Offset {
451
+ Offset (const uint8_t data[3 ]) : raw{data[0 ], data[1 ], data[2 ]} {}
470
452
uint8_t raw[3 ]; // !< Offset Error Digital Calibration Code (two’s complement, MSb first coding)
471
453
} offset_t ;
472
454
@@ -477,6 +459,7 @@ class MCP3x6x {
477
459
* href=https://ww1.microchip.com/downloads/aemDocuments/documents/APID/ProductDocuments/DataSheets/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf#G1.1270900>MCP346x.pdf</a>
478
460
*/
479
461
typedef union Gain {
462
+ Gain (const uint8_t data[3 ]) : raw{data[0 ], data[1 ], data[2 ]} {}
480
463
uint8_t raw[3 ]; // !< Gain Error Digital Calibration Code (unsigned, MSb first coding)
481
464
} gain_t ;
482
465
@@ -487,6 +470,7 @@ class MCP3x6x {
487
470
* href=https://ww1.microchip.com/downloads/aemDocuments/documents/APID/ProductDocuments/DataSheets/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf#G1.1271641>MCP346x.pdf</a>
488
471
*/
489
472
typedef union Lock {
473
+ Lock (uint8_t data) : raw (data) {}
490
474
uint8_t raw; // !< Write Access Password Entry Code
491
475
} lock_t ;
492
476
@@ -497,32 +481,86 @@ class MCP3x6x {
497
481
* href=https://ww1.microchip.com/downloads/aemDocuments/documents/APID/ProductDocuments/DataSheets/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf#G1.1272118>MCP346x.pdf</a>
498
482
*/
499
483
typedef union Crccfg {
500
- uint8_t raw[2 ]; // !< CRC-16 Checksum Value
484
+ Crccfg (const uint8_t data[2 ]) : raw{data[0 ], data[1 ]} {}
485
+ union {
486
+ // uint16_t val;
487
+ uint8_t raw[2 ]; // !< CRC-16 Checksum Value
488
+ };
501
489
} crccfg_t ;
502
490
503
491
/* *
504
492
* @brief settings
505
493
*
506
494
*/
507
- struct Settings {
495
+ class Settings {
496
+ size_t getMaxResolution () {
497
+ switch (id) {
498
+ case MCP3461_DEVICE_TYPE:
499
+ case MCP3462_DEVICE_TYPE:
500
+ case MCP3464_DEVICE_TYPE:
501
+ return 16 ;
502
+ case MCP3561_DEVICE_TYPE:
503
+ case MCP3562_DEVICE_TYPE:
504
+ case MCP3564_DEVICE_TYPE:
505
+ return 24 ;
506
+ default :
507
+ return 0 ;
508
+ }
509
+ }
510
+
511
+ size_t getChannelCount () {
512
+ switch (id) {
513
+ case MCP3461_DEVICE_TYPE:
514
+ case MCP3561_DEVICE_TYPE:
515
+ return 2 ;
516
+ case MCP3462_DEVICE_TYPE:
517
+ case MCP3562_DEVICE_TYPE:
518
+ return 4 ;
519
+ case MCP3464_DEVICE_TYPE:
520
+ case MCP3564_DEVICE_TYPE:
521
+ return 8 ;
522
+ default :
523
+ return 0 ;
524
+ }
525
+ }
526
+
508
527
public:
509
- config0_t config0; // !< register setting
510
- config1_t config1; // !< register setting
511
- config2_t config2; // !< register setting
512
- config3_t config3; // !< register setting
513
- irq_t irq; // !< register setting
514
- mux_t mux; // !< register setting
515
- scan_t scan; // !< register setting
516
- timer_t timer; // !< register setting
517
- offset_t offsetcal; // !< register setting
518
- gain_t gaincal; // !< register setting
519
- uint8_t reserverd1[3 ] = {0 , 0 , 0 }; // !< register setting
520
- uint8_t reserverd2 = 0 ; // !< register setting
521
- lock_t lock; // !< register setting
522
- uint16_t id = 0 ; // !< register setting
523
- crccfg_t crccfg; // !< register setting
524
-
525
- Settings () : mux(0x01 ) {}
528
+ struct {
529
+ const uint8_t CONFIG0 = 0xC0 ; // !< default value
530
+ const uint8_t CONFIG1 = 0x0C ; // !< default value
531
+ const uint8_t CONFIG2 = 0x8B ; // !< default value
532
+ const uint8_t CONFIG3 = 0x00 ; // !< default value
533
+ const uint8_t IRQ = 0x73 ; // !< default value
534
+ const uint8_t MUX = 0x01 ; // !< default value
535
+ const uint8_t SCAN[3 ] = {0x00 , 0x00 , 0x00 }; // !< default value
536
+ const uint8_t TIMER[3 ] = {0x00 , 0x00 , 0x00 }; // !< default value
537
+ const uint8_t OFFSET[3 ] = {0x00 , 0x00 , 0x00 }; // !< default value
538
+ const uint8_t GAIN[3 ] = {0x80 , 0x00 , 0x00 }; // !< default value
539
+ // const uint8_t RESERVED1[3] = {0x90, 0x00, 0x00}; //!< default value
540
+ // const uint8_t RESERVED2 = 0x50; //!< default value
541
+ const uint8_t LOCK = 0xA5 ; // !< default value
542
+ const uint8_t CRCCFG[2 ] = {0x00 , 0x00 }; // !< default value
543
+ } DEFAULT;
544
+
545
+ config0_t config0 = DEFAULT.CONFIG0; // !< register setting
546
+ config1_t config1 = DEFAULT.CONFIG1; // !< register setting
547
+ config2_t config2 = DEFAULT.CONFIG2; // !< register setting
548
+ config3_t config3 = DEFAULT.CONFIG3; // !< register setting
549
+ irq_t irq = DEFAULT.IRQ; // !< register setting
550
+ mux_t mux = DEFAULT.MUX; // !< register setting
551
+ scan_t scan = DEFAULT.SCAN; // !< register setting
552
+ timer_t timer = DEFAULT.TIMER; // !< register setting
553
+ offset_t offsetcal = DEFAULT.OFFSET; // !< register setting
554
+ gain_t gaincal = DEFAULT.GAIN; // !< register setting
555
+ // const uint8_t reserverd1[3] = _DEFAULT.RESERVED1; //!< register setting
556
+ // const uint8_t reserverd2 = _DEFAULT.RESERVED2; //!< register setting
557
+ uint16_t id; // !< register setting
558
+ lock_t lock = DEFAULT.LOCK; // !< register setting
559
+ crccfg_t crccfg = DEFAULT.CRCCFG; // !< register setting
560
+
561
+ Settings (const uint16_t MCP3x6x_DEVICE_TYPE) : id(MCP3x6x_DEVICE_TYPE) {}
562
+
563
+ friend MCP3x6x;
526
564
} settings;
527
565
528
566
/* *
@@ -651,7 +689,7 @@ class MCP3x6x {
651
689
* @return status_t
652
690
*/
653
691
inline status_t reset () {
654
- // memcpy( settings, _DEFAULTS, 27) ;
692
+ // todo: reset settings values ;
655
693
return _fastcmd (MCP3x6x_CMD_RESET);
656
694
}
657
695
0 commit comments