-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathi2c.cpp
1291 lines (1069 loc) · 34.7 KB
/
i2c.cpp
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
// SPDX-License-Identifier: GPL-3.0-or-later
//
// Copyright (c) 2013-2023 plan44.ch / Lukas Zeller, Zurich, Switzerland
//
// Author: Lukas Zeller <luz@plan44.ch>
//
// This file is part of p44utils.
//
// p44utils is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// p44utils is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with p44utils. If not, see <http://www.gnu.org/licenses/>.
//
// File scope debugging options
// - Set ALWAYS_DEBUG to 1 to enable DBGLOG output even in non-DEBUG builds of this file
#define ALWAYS_DEBUG 0
// - set FOCUSLOGLEVEL to non-zero log level (usually, 5,6, or 7==LOG_DEBUG) to get focus (extensive logging) for this file
// Note: must be before including "logger.hpp" (or anything that includes "logger.hpp")
#define FOCUSLOGLEVEL 7
#include "i2c.hpp"
// locally disable actual functionality on unsupported platforms (but still provide console output dummies)
#if !defined(DISABLE_I2C) && (defined(__APPLE__) || P44_BUILD_DIGI) && !P44_BUILD_RPI && !P44_BUILD_RB && !P44_BUILD_OW
#define DISABLE_I2C 1
#endif
#if !DISABLE_I2C
#include <sys/ioctl.h>
#if P44_BUILD_OW
// former i2c-dev.h SMBUS static inlines are now functions in libi2c (available since OpenWrt 19.07)
extern "C" {
#include <i2c/smbus.h>
}
#include <linux/i2c-dev.h>
#else
// TODO: check if this is still the best way to go on standard linux
#include <linux/i2c-dev.h>
#ifndef LIB_I2CDEV_H
#warning "Extended i2c-dev.h header not available - including local i2c-dev-extensions.h to augment existing i2c-dev.h"
#include "i2c-dev-extensions.h"
#endif
#endif
#else
#warning "No i2C supported on this platform - just showing calls in focus debug output"
#endif
#if !defined(ESP_PLATFORM)
#if ENABLE_APPLICATION_SUPPORT
#include "application.hpp" // we need it for user level, syscmd is only allowed with userlevel>=2
#endif
#endif
using namespace p44;
#if ENABLE_I2C_SCRIPT_FUNCS
using namespace P44Script;
#endif
// MARK: - I2C Manager
static I2CManager *sharedI2CManager = NULL;
I2CManager::I2CManager()
{
}
I2CManager::~I2CManager()
{
}
I2CManager &I2CManager::sharedManager()
{
if (!sharedI2CManager) {
sharedI2CManager = new I2CManager();
}
return *sharedI2CManager;
}
I2CBusPtr I2CManager::getBus(int aBusNumber)
{
// find or create bus
I2CBusMap::iterator pos = busMap.find(aBusNumber);
I2CBusPtr bus;
if (pos!=busMap.end()) {
bus = pos->second;
}
else {
// bus does not exist yet, create it
bus = I2CBusPtr(new I2CBus(aBusNumber));
busMap[aBusNumber] = bus;
}
return bus;
}
I2CDevicePtr I2CManager::getDevice(int aBusNumber, const char *aDeviceID)
{
// get the bus
I2CBusPtr bus = getBus(aBusNumber);
// dissect device ID into type and busAddress
// - type string
// consists of Chip name plus optional options suffix. Like "PCA9685" or "PCA9685-TP" (TP=options)
string typeString = "generic";
string deviceOptions = ""; // no options
string s = aDeviceID;
size_t i = s.find("@");
if (i!=string::npos) {
typeString = s.substr(0,i);
s.erase(0,i+1);
// extract device options, if any (appended to device name after a dash)
size_t j = typeString.find("-");
if (j!=string::npos) {
deviceOptions = typeString.substr(j+1);
typeString.erase(j);
}
}
// - device address (hex)
int deviceAddress = 0;
sscanf(s.c_str(), "%x", &deviceAddress);
// reconstruct fully qualified device name for searching
s = string_format("%s@%02X", typeString.c_str(), deviceAddress);
// get possibly already existing device of correct type at that address
I2CDevicePtr dev = bus->getDevice(s.c_str());
if (!dev) {
// create device from typestring
if (typeString=="TCA9555")
dev = I2CDevicePtr(new TCA9555(deviceAddress, bus.get(), deviceOptions.c_str()));
else if (typeString=="MCP23017")
dev = I2CDevicePtr(new MCP23017(deviceAddress, bus.get(), deviceOptions.c_str()));
else if (typeString=="PCF8574")
dev = I2CDevicePtr(new PCF8574(deviceAddress, bus.get(), deviceOptions.c_str()));
else if (typeString=="PCA9685")
dev = I2CDevicePtr(new PCA9685(deviceAddress, bus.get(), deviceOptions.c_str()));
else if (typeString=="LM75")
dev = I2CDevicePtr(new LM75(deviceAddress, bus.get(), deviceOptions.c_str()));
else if (typeString=="MCP3021")
dev = I2CDevicePtr(new MCP3021(deviceAddress, bus.get(), deviceOptions.c_str()));
else if (typeString=="MAX1161x")
dev = I2CDevicePtr(new MAX1161x(deviceAddress, bus.get(), deviceOptions.c_str()));
else if (typeString=="generic")
dev = I2CDevicePtr(new I2CDevice(deviceAddress, bus.get(), deviceOptions.c_str()));
// TODO: add more device types
// Register new device
if (dev) {
bus->registerDevice(dev);
}
}
return dev;
}
// MARK: - I2CBus
I2CBus::I2CBus(int aBusNumber) :
busFD(-1),
busNumber(aBusNumber),
lastDeviceAddress(-1)
{
}
I2CBus::~I2CBus()
{
closeBus();
}
void I2CBus::registerDevice(I2CDevicePtr aDevice)
{
deviceMap[aDevice->deviceID()] = aDevice;
}
I2CDevicePtr I2CBus::getDevice(const char *aDeviceID)
{
I2CDeviceMap::iterator pos = deviceMap.find(aDeviceID);
if (pos!=deviceMap.end())
return pos->second;
return I2CDevicePtr();
}
bool I2CBus::I2CReadByte(I2CDevice *aDeviceP, uint8_t &aByte)
{
if (!accessDevice(aDeviceP)) return false; // cannot read
#if !DISABLE_I2C
int res = i2c_smbus_read_byte(busFD);
#else
int res = 0x42; // dummy
#endif
// read is shown only in real Debug log, because button polling creates lots of accesses
DBGFOCUSLOG("i2c_smbus_read_byte() = %d / 0x%02X", res, res);
if (res<0) return false;
aByte = (uint8_t)res;
return true;
}
bool I2CBus::I2CReadBytes(I2CDevice *aDeviceP, uint8_t aCount, uint8_t *aBufferP)
{
if (!accessDevice(aDeviceP)) return false; // cannot read
#if !DISABLE_I2C
ssize_t res = read(busFD, aBufferP, aCount);
#else
for (int n=0; n<aCount; n++) {
aBufferP[n] = 0x42; // dummy
}
ssize_t res = aCount;
#endif
// read is shown only in real Debug log, because button polling creates lots of accesses
DBGFOCUSLOG("i2c device read(): first byte = %d / 0x%02X, res=%zd", *aBufferP, *aBufferP, res);
if (res<0 || res!=aCount) return false;
return true;
}
bool I2CBus::I2CWriteByte(I2CDevice *aDeviceP, uint8_t aByte)
{
if (!accessDevice(aDeviceP)) return false; // cannot write
#if !DISABLE_I2C
int res = i2c_smbus_write_byte(busFD, aByte);
#else
int res = 1; // ok
#endif
FOCUSLOG("i2c_smbus_write_byte(byte=0x%02X) = %d", aByte, res);
return (res>=0);
}
bool I2CBus::SMBusReadByte(I2CDevice *aDeviceP, uint8_t aRegister, uint8_t &aByte)
{
if (!accessDevice(aDeviceP)) return false; // cannot read
#if !DISABLE_I2C
int res = i2c_smbus_read_byte_data(busFD, aRegister);
#else
int res = 0x42; // dummy
#endif
// read is shown only in real Debug log, because button polling creates lots of accesses
DBGFOCUSLOG("i2c_smbus_read_byte_data(cmd=0x%02X) = %d / 0x%02X", aRegister, res, res);
if (res<0) return false;
aByte = (uint8_t)res;
return true;
}
bool I2CBus::SMBusReadWord(I2CDevice *aDeviceP, uint8_t aRegister, uint16_t &aWord, bool aMSBFirst)
{
if (!accessDevice(aDeviceP)) return false; // cannot read
#if !DISABLE_I2C
int res = i2c_smbus_read_word_data(busFD, aRegister);
if (res<0) return false;
if (aMSBFirst) {
// swap
res = ((res&0xFF)<<8) + ((res>>8)&0xFF);
}
#else
int res = 0x4242; // dummy
#endif
// read is shown only in real Debug log, because button polling creates lots of accesses
DBGFOCUSLOG("i2c_smbus_read_word_data(cmd=0x%02X) = %d / 0x%04X", aRegister, res, res);
if (res<0) return false;
aWord = (uint16_t)res;
return true;
}
bool I2CBus::SMBusReadBlock(I2CDevice *aDeviceP, uint8_t aRegister, uint8_t &aCount, smbus_block_t &aData)
{
if (!accessDevice(aDeviceP)) return false; // cannot read
#if !DISABLE_I2C
int res = i2c_smbus_read_block_data(busFD, aRegister, aData);
if (res<0) return false;
#else
int res = 0; // no data
#endif
if (FOCUSLOGENABLED) {
string data;
for (uint8_t i=0; i<res; i++) string_format_append(data, ", 0x%02X", aData[i]);
FOCUSLOG("i2c_smbus_read_block_data(cmd=0x%02X) = %d / 0x%02X%s", aRegister, res, res, data.c_str());
}
if (res<0) return false;
aCount = (uint16_t)res;
return true;
}
bool I2CBus::SMBusWriteByte(I2CDevice *aDeviceP, uint8_t aRegister, uint8_t aByte)
{
if (!accessDevice(aDeviceP)) return false; // cannot write
#if !DISABLE_I2C
int res = i2c_smbus_write_byte_data(busFD, aRegister, aByte);
#else
int res = 1; // ok
#endif
FOCUSLOG("i2c_smbus_write_byte_data(cmd=0x%02X, byte=0x%02X) = %d", aRegister, aByte, res);
return (res>=0);
}
bool I2CBus::SMBusWriteWord(I2CDevice *aDeviceP, uint8_t aRegister, uint16_t aWord, bool aMSBFirst)
{
if (!accessDevice(aDeviceP)) return false; // cannot write
#if !DISABLE_I2C
if (aMSBFirst) {
// swap
aWord = ((aWord&0xFF)<<8) + ((aWord>>8)&0xFF);
}
int res = i2c_smbus_write_word_data(busFD, aRegister, aWord);
#else
int res = 1; // ok
#endif
FOCUSLOG("i2c_smbus_write_word_data(cmd=0x%02X, word=0x%04X) = %d", aRegister, aWord, res);
return (res>=0);
}
bool I2CBus::SMBusWriteBlock(I2CDevice *aDeviceP, uint8_t aRegister, uint8_t aCount, const uint8_t *aDataP)
{
if (!accessDevice(aDeviceP)) return false; // cannot write
#if !DISABLE_I2C
int res = i2c_smbus_write_block_data(busFD, aRegister, aCount, (uint8_t *)aDataP);
#else
int res = 1; // ok
#endif
if (FOCUSLOGENABLED) {
string data;
if (res>=0) {
for (uint8_t i=0; i<aCount; i++) string_format_append(data, ", 0x%02X", aDataP[i]);
}
FOCUSLOG("i2c_smbus_write_block_data(cmd=0x%02X, count=0x%02X%s) = %d", aRegister, aCount, data.c_str(), res);
}
return (res>=0);
}
// Not proper block, but just some bytes in a row, count byte not sent
bool I2CBus::SMBusWriteBytes(I2CDevice *aDeviceP, uint8_t aRegister, uint8_t aCount, const uint8_t *aDataP)
{
if (!accessDevice(aDeviceP)) return false; // cannot write
#if !DISABLE_I2C
int res = i2c_smbus_write_i2c_block_data(busFD, aRegister, aCount, (uint8_t *)aDataP);
#else
int res = 1; // ok
#endif
if (FOCUSLOGENABLED) {
string data;
if (res>=0) {
for (uint8_t i=0; i<aCount; i++) string_format_append(data, ", 0x%02X", aDataP[i]);
}
FOCUSLOG("i2c_smbus_write_i2c_block_data(cmd=0x%02X, count=0x%02X%s) = %d", aRegister, aCount, data.c_str(), res);
}
return (res>=0);
}
bool I2CBus::accessDevice(I2CDevice *aDeviceP)
{
if (!accessBus())
return false;
if (aDeviceP->deviceAddress == lastDeviceAddress)
return true; // already set to access that device
// address the device
#if !DISABLE_I2C
if (ioctl(busFD, I2C_SLAVE, aDeviceP->deviceAddress) < 0) {
LOG(LOG_ERR, "Error: Cannot access device '%s' on bus %d", aDeviceP->deviceID().c_str(), busNumber);
lastDeviceAddress = -1; // invalidate
return false;
}
#endif
FOCUSLOG("ioctl(busFD, I2C_SLAVE, 0x%02X)", aDeviceP->deviceAddress);
// remember
lastDeviceAddress = aDeviceP->deviceAddress;
return true; // ok
}
bool I2CBus::accessBus()
{
if (busFD>=0)
return true; // already open
// need to open
lastDeviceAddress = -1; // invalidate
string busDevName = string_format("/dev/i2c-%d", busNumber);
#if !DISABLE_I2C
busFD = open(busDevName.c_str(), O_RDWR);
if (busFD<0) {
LOG(LOG_ERR, "Error: Cannot open i2c bus device '%s'",busDevName.c_str());
return false;
}
#else
busFD = 1; // dummy, signalling open
#endif
FOCUSLOG("open(\"%s\", O_RDWR) = %d", busDevName.c_str(), busFD);
return true;
}
void I2CBus::closeBus()
{
if (busFD>=0) {
#ifndef __APPLE__
close(busFD);
#endif
busFD = -1;
}
}
// MARK: - I2CDevice
I2CDevice::I2CDevice(uint8_t aDeviceAddress, I2CBus *aBusP, const char *aDeviceOptions)
{
i2cbus = aBusP;
deviceAddress = aDeviceAddress;
}
string I2CDevice::deviceID()
{
return string_format("%s@%02X", deviceType(), deviceAddress);
}
bool I2CDevice::isKindOf(const char *aDeviceType)
{
return (strcmp(deviceType(),aDeviceType)==0);
}
// MARK: - I2CBitPortDevice
I2CBitPortDevice::I2CBitPortDevice(uint8_t aDeviceAddress, I2CBus *aBusP, const char *aDeviceOptions) :
inherited(aDeviceAddress, aBusP, aDeviceOptions),
outputEnableMask(0),
pinStateMask(0),
pullUpMask(0)
{
}
bool I2CBitPortDevice::isKindOf(const char *aDeviceType)
{
if (strcmp(deviceType(),aDeviceType)==0)
return true;
else
return inherited::isKindOf(aDeviceType);
}
bool I2CBitPortDevice::getBitState(int aBitNo)
{
uint32_t bitMask = 1<<aBitNo;
if (outputEnableMask & bitMask) {
// is output, just return the last set state
return (outputStateMask & bitMask)!=0;
}
else {
// is input, get actual input state
updateInputState(aBitNo); // update
return (pinStateMask & bitMask)!=0;
}
}
void I2CBitPortDevice::setBitState(int aBitNo, bool aState)
{
uint32_t bitMask = 1<<aBitNo;
if (outputEnableMask & bitMask) {
// is output, set new state (always, even if seemingly already set)
if (aState)
outputStateMask |= bitMask;
else
outputStateMask &= ~bitMask;
// update hardware
updateOutputs(aBitNo);
}
}
void I2CBitPortDevice::setAsOutput(int aBitNo, bool aOutput, bool aInitialState, bool aPullUp)
{
uint32_t bitMask = 1<<aBitNo;
// Input or output
if (aOutput)
outputEnableMask |= bitMask;
else
outputEnableMask &= ~bitMask;
// Pullup or not
if (aPullUp)
pullUpMask |= bitMask;
else {
pullUpMask &= ~bitMask;
}
// before actually updating direction, set initial value
setBitState(aBitNo, aInitialState);
// now update direction
updateDirection(aBitNo);
}
// MARK: - TCA9555
TCA9555::TCA9555(uint8_t aDeviceAddress, I2CBus *aBusP, const char *aDeviceOptions) :
inherited(aDeviceAddress, aBusP, aDeviceOptions)
{
// make sure we have all inputs
updateDirection(0); // port 0
updateDirection(8); // port 1
// reset polarity inverter
i2cbus->SMBusWriteByte(this, 4, 0); // reset polarity inversion port 0
i2cbus->SMBusWriteByte(this, 5, 0); // reset polarity inversion port 1
}
bool TCA9555::isKindOf(const char *aDeviceType)
{
if (strcmp(deviceType(),aDeviceType)==0)
return true;
else
return inherited::isKindOf(aDeviceType);
}
void TCA9555::updateInputState(int aForBitNo)
{
if (aForBitNo>15) return;
uint8_t port = aForBitNo >> 3; // calculate port No
uint8_t shift = 8*port;
uint8_t data;
i2cbus->SMBusReadByte(this, port, data); // get input byte
pinStateMask = (pinStateMask & ~(((uint32_t)0xFF) << shift)) | ((uint32_t)data << shift);
}
void TCA9555::updateOutputs(int aForBitNo)
{
if (aForBitNo>15) return;
uint8_t port = aForBitNo >> 3; // calculate port No
uint8_t shift = 8*port;
i2cbus->SMBusWriteByte(this, port+2, (outputStateMask >> shift) & 0xFF); // write output byte
}
void TCA9555::updateDirection(int aForBitNo)
{
if (aForBitNo>15) return;
updateOutputs(aForBitNo); // make sure output register has the correct value
uint8_t port = aForBitNo >> 3; // calculate port No
uint8_t shift = 8*port;
uint8_t data = ~((outputEnableMask >> shift) & 0xFF); // TCA9555 config register has 1 for inputs, 0 for outputs
i2cbus->SMBusWriteByte(this, port+6, data); // set input enable flags in reg 6 or 7
}
// MARK: - PCF8574
PCF8574::PCF8574(uint8_t aDeviceAddress, I2CBus *aBusP, const char *aDeviceOptions) :
inherited(aDeviceAddress, aBusP, aDeviceOptions)
{
// make sure we have all inputs
updateDirection(0); // port 0
}
bool PCF8574::isKindOf(const char *aDeviceType)
{
if (strcmp(deviceType(),aDeviceType)==0)
return true;
else
return inherited::isKindOf(aDeviceType);
}
void PCF8574::updateInputState(int aForBitNo)
{
if (aForBitNo>7) return;
uint8_t data;
if (i2cbus->I2CReadByte(this, data)) {
pinStateMask = data;
}
}
void PCF8574::updateOutputs(int aForBitNo)
{
if (aForBitNo>7) return;
// PCF8574 does not have a direction register, but reading just senses the pin level.
// With output set to H, the pin is OC and can be set to Low
// -> pins to be used as inputs must always be high
uint8_t b =
((~outputEnableMask) & 0xFF) | // pins used as input must have output state High
(outputStateMask & 0xFF); // pins used as output will have the correct state from beginning
i2cbus->I2CWriteByte(this, b);
}
void PCF8574::updateDirection(int aForBitNo)
{
// There is no difference in updating outputs or updating direction for the primitive PCF8574
updateOutputs(aForBitNo);
}
// MARK: - MCP23017
MCP23017::MCP23017(uint8_t aDeviceAddress, I2CBus *aBusP, const char *aDeviceOptions) :
inherited(aDeviceAddress, aBusP, aDeviceOptions)
{
// initially, IOCON==0 -> IOCON.BANK==0 -> A/B interleaved register access
// enable hardware addressing if selected
if (strchr(aDeviceOptions, 'A')) {
i2cbus->SMBusWriteByte(this, 0x0A, 0x08); // set HAEN (hardware address enable) in IOCON
}
// make sure we have all inputs
updateDirection(0); // port 0
updateDirection(8); // port 1
// reset polarity inverter
i2cbus->SMBusWriteByte(this, 0x02, 0); // reset polarity inversion A
i2cbus->SMBusWriteByte(this, 0x03, 0); // reset polarity inversion B
}
bool MCP23017::isKindOf(const char *aDeviceType)
{
if (strcmp(deviceType(),aDeviceType)==0)
return true;
else
return inherited::isKindOf(aDeviceType);
}
void MCP23017::updateInputState(int aForBitNo)
{
if (aForBitNo>15) return;
uint8_t port = aForBitNo >> 3; // calculate port No
uint8_t shift = 8*port;
uint8_t data;
i2cbus->SMBusReadByte(this, port+0x12, data); // get current port state from GPIO reg 12/13
pinStateMask = (pinStateMask & ~(((uint32_t)0xFF) << shift)) | ((uint32_t)data << shift);
}
void MCP23017::updateOutputs(int aForBitNo)
{
if (aForBitNo>15) return;
uint8_t port = aForBitNo >> 3; // calculate port No
uint8_t shift = 8*port;
i2cbus->SMBusWriteByte(this, port+0x14, (outputStateMask >> shift) & 0xFF); // write to output latch (OLAT) A/B reg 14/15
}
void MCP23017::updateDirection(int aForBitNo)
{
if (aForBitNo>15) return;
updateOutputs(aForBitNo); // make sure output register has the correct value
uint8_t port = aForBitNo >> 3; // calculate port No
uint8_t shift = 8*port;
uint8_t data;
// configure pullups
data = (pullUpMask >> shift) & 0xFF; // MCP23S17 GPPU register has 1 for pullup enabled
i2cbus->SMBusWriteByte(this, port+0x0C, data); // set pullup enable flags in GPPU reg C or D
// configure direction
data = ~((outputEnableMask >> shift) & 0xFF); // MCP23S17 IODIR register has 1 for inputs, 0 for outputs
i2cbus->SMBusWriteByte(this, port+0x00, data); // set input enable flags in IODIR reg 0 or 1
}
// MARK: - I2Cpin
/// create i2c based digital input or output pin (or use an analog pin as digital I/O)
I2CPin::I2CPin(int aBusNumber, const char *aDeviceId, int aPinNumber, bool aOutput, bool aInitialState, Tristate aPull) :
output(false),
lastSetState(false)
{
pinNumber = aPinNumber;
output = aOutput;
I2CDevicePtr dev = I2CManager::sharedManager().getDevice(aBusNumber, aDeviceId);
bitPortDevice = boost::dynamic_pointer_cast<I2CBitPortDevice>(dev);
analogPortDevice = boost::dynamic_pointer_cast<I2CAnalogPortDevice>(dev);
if (bitPortDevice) {
// bitport device, which is configurable for I/O and pullup
bitPortDevice->setAsOutput(pinNumber, output, aInitialState, aPull==yes);
}
else if (analogPortDevice) {
// analog device used as digital signal
setState(aInitialState); // just set the state
}
lastSetState = aInitialState;
}
/// get state of pin
/// @return current state (from actual GPIO pin for inputs, from last set state for outputs)
bool I2CPin::getState()
{
if (bitPortDevice) {
if (output)
return lastSetState;
else
return bitPortDevice->getBitState(pinNumber);
}
else if (analogPortDevice) {
// use analog pin as digital input
double min=0, max=100, res=1;
analogPortDevice->getPinRange(pinNumber, min, max, res);
return analogPortDevice->getPinValue(pinNumber)>min+(max-min)/2; // above the middle
}
return false;
}
/// set state of pin (NOP for inputs)
/// @param aState new state to set output to
void I2CPin::setState(bool aState)
{
if (output) {
if (bitPortDevice) {
bitPortDevice->setBitState(pinNumber, aState);
}
else if (analogPortDevice) {
// use analog pin as digital output
double min=0, max=100, res=1;
analogPortDevice->getPinRange(pinNumber, min, max, res);
analogPortDevice->setPinValue(pinNumber, aState ? max : min);
}
}
lastSetState = aState;
}
// MARK: - I2CAnalogPortDevice
I2CAnalogPortDevice::I2CAnalogPortDevice(uint8_t aDeviceAddress, I2CBus *aBusP, const char *aDeviceOptions) :
inherited(aDeviceAddress, aBusP, aDeviceOptions)
{
}
bool I2CAnalogPortDevice::isKindOf(const char *aDeviceType)
{
if (strcmp(deviceType(),aDeviceType)==0)
return true;
else
return inherited::isKindOf(aDeviceType);
}
// MARK: - PCA9685
PCA9685::PCA9685(uint8_t aDeviceAddress, I2CBus *aBusP, const char *aDeviceOptions) :
inherited(aDeviceAddress, aBusP, aDeviceOptions)
{
// Initalize
// device options:
// - 'I' : output invert (Low when active)
// - 'O' : open drain (pull to low only, vs. totem pole)
// - 'Sxxxx' : PWM speed in Hz (max is 2kHz, min is 24Hz)
bool inverted = strchr(aDeviceOptions, 'I');
bool opendrain = strchr(aDeviceOptions, 'O');
// Internal OSC is 25Mhz, pre_scale = (25MHz/4096/PWMfreq)-1
uint8_t pre_scale = 30; // default reset value of PCA9685 PRE_SCALE register = 200Hz
const char *p = strchr(aDeviceOptions, 'S');
if (p) {
int speed = atoi(p+1);
if (speed<24) speed=24; // limit to >=24Hz
pre_scale = (int)6103/speed; // approx, no rounding but omitting -1 instead
if (pre_scale<3) pre_scale=3; // PRE_SCALE does not accept < 3
}
// - prepare for setting PRE_SCALE: control register 0 = MODE1: SLEEP=1
i2cbus->SMBusWriteByte(this, 0, 0x10);
// - set speed (can only be set when SLEEP=1)
i2cbus->SMBusWriteByte(this, 0xFE, pre_scale); // set PRE_SCALE register
// - control register 0 = MODE1: normal operation, SLEEP=0, auto-increment register address, no subadresses
i2cbus->SMBusWriteByte(this, 0, 0x20);
// - control register 1 = MODE2: when OE is 1, outputs are high impedance, plus invert and opendrain/totempole according to options
i2cbus->SMBusWriteByte(this, 1, 0x03 + (inverted ? 0x10 : 0) + (opendrain ? 0 : 0x04));
// - turn off all LEDs
i2cbus->SMBusWriteByte(this, 0xFB, 0x00); // none full on
i2cbus->SMBusWriteByte(this, 0xFD, 0x10); // all full off
}
bool PCA9685::isKindOf(const char *aDeviceType)
{
if (strcmp(deviceType(),aDeviceType)==0)
return true;
else
return inherited::isKindOf(aDeviceType);
}
double PCA9685::getPinValue(int aPinNo)
{
// get current ratio
uint8_t h,l;
uint16_t onTime, offTime;
// get off time
i2cbus->SMBusReadByte(this, 9+aPinNo*4, h);
if (h & 0x10) {
// full off
return 0;
}
i2cbus->SMBusReadByte(this, 8+aPinNo*4, l);
offTime = (h & 0xF)<<8 | l;
// get on time
i2cbus->SMBusReadByte(this, 7+aPinNo*4, h);
if (h & 0x10) {
// full on
return 100;
}
i2cbus->SMBusReadByte(this, 6+aPinNo*4, l);
onTime = (h & 0xF)<<8 | l;
// calculate on ratio in percent
onTime = (offTime-onTime) & 0xFFF;
return (double)(onTime)/40.96; // %
}
void PCA9685::setPinValue(int aPinNo, double aValue)
{
uint8_t pwmdata[4];
// check special full on and full off cases first
// TODO: when we use shift, for some unknown reason, the PWM output flickers when the off time wraps around.
// So for now, we do NOT shift the on time.
//int shift = aPinNo; // minimize current by distributing switch on time of the pins
int shift = 0; // no on-time shifting, all starting at 0
uint16_t v = (uint16_t)(aValue*40.96+0.5);
if (v==0) {
pwmdata[0] = 0;
pwmdata[1] = 0x00; // not full ON
pwmdata[2] = 0;
pwmdata[3] = 0x10; // but full OFF
}
else if (v>=0x0FFF) {
pwmdata[0] = 0; // LSB of start time is 0
pwmdata[1] = 0x10+shift; // full ON, starting at pin's regular ON time
pwmdata[2] = 0;
pwmdata[3] = 0x00; // but not full OFF
}
else {
// set on time, possibly shifted
pwmdata[0] = 0x00; // LSB of start time is 0
pwmdata[1] = shift; // each pin offsets onTime by 1/16 of 12-bit range: upper 4 bits = pinNo
uint16_t t = shift<<8; // on time
t = (t+v) & 0xFFF; // off time with wrap around
// set off time
pwmdata[2] = t & 0xFF; // LSB of end time
pwmdata[3] = (t>>8) & 0xF; // 4 MSB of end time
}
// send as one transaction
i2cbus->SMBusWriteBytes(this, 6+aPinNo*4, 4, pwmdata);
}
bool PCA9685::getPinRange(int aPinNo, double &aMin, double &aMax, double &aResolution)
{
aMin = 0;
aMax = 100;
aResolution = 100.0/4095;
return true;
}
// MARK: - LM75 (A,B,C...)
LM75::LM75(uint8_t aDeviceAddress, I2CBus *aBusP, const char *aDeviceOptions) :
inherited(aDeviceAddress, aBusP, aDeviceOptions),
bits(9) // default to 9 valid bits, most LM75 variants seem to have 9 bits, but some have 10 or 11
{
// device options are number of bits
int b = atoi(aDeviceOptions);
if (b!=0) bits = b;
}
bool LM75::isKindOf(const char *aDeviceType)
{
if (strcmp(deviceType(),aDeviceType)==0)
return true;
else
return inherited::isKindOf(aDeviceType);
}
double LM75::getPinValue(int aPinNo)
{
uint16_t raw;
i2cbus->SMBusReadWord(this, 0x00, raw, true); // LM75A delivers MSB first
// result is a signed 16 bit value covering a range of -128..127 degree celsius, in 1/256 degree steps
// However, of these 16 bits, 5..7 LSBits (depending on LM75A versions with different ADC precision) are invalid
// For example LM75A from NXP has 11 bit precision, while most other LM75 variants (TI, Maxim...) have 9 bits
// So we mask out the invalid bits here
uint16_t mask = ~((1<<(16-bits))-1);
// see as 16 bit signed value of 1/256 degree celsius
int16_t temp256th = (int16_t)(raw & mask);
// return as celsius
return ((double)temp256th)/256;
}
bool LM75::getPinRange(int aPinNo, double &aMin, double &aMax, double &aResolution)
{
aMin = -127;
aMax = 127;
aResolution = 256.0/(1<<bits);
return true;
}
// MARK: - MCP3021 (5 pin, 10bit ADC, Microchip)
MCP3021::MCP3021(uint8_t aDeviceAddress, I2CBus *aBusP, const char *aDeviceOptions) :
inherited(aDeviceAddress, aBusP, aDeviceOptions)
{
}
bool MCP3021::isKindOf(const char *aDeviceType)
{
if (strcmp(deviceType(),aDeviceType)==0)
return true;
else
return inherited::isKindOf(aDeviceType);
}
double MCP3021::getPinValue(int aPinNo)
{
uint8_t buf[2];
uint16_t raw;
i2cbus->I2CReadBytes(this, 2, buf); // MCP3021 delivers MSB first
// discard two LSBs, limit to actual 10 bit result
raw = (((uint16_t)buf[0]<<6)) + (buf[1]>>2);
return raw;
}
bool MCP3021::getPinRange(int aPinNo, double &aMin, double &aMax, double &aResolution)
{
// as we don't know what will be connected to the inputs, we return raw A/D value.
aMin = 0;
aMax = 1023;
aResolution = 1;
return true;
}
// MARK: - MAX11612-617 (12bit ADCs, Maxim)
MAX1161x::MAX1161x(uint8_t aDeviceAddress, I2CBus *aBusP, const char *aDeviceOptions) :
inherited(aDeviceAddress, aBusP, aDeviceOptions)
{
i2cbus->I2CWriteByte(this,