-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
spi.cpp
1110 lines (937 loc) · 32.6 KB
/
spi.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) 2016-2023 plan44.ch / Lukas Zeller, Zurich, Switzerland
//
// Author: Lukas Zeller <[email protected]>
//
// 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 0
#include "spi.hpp"
// locally disable actual functionality on unsupported platforms (but still provide console output dummies)
#if !defined(DISABLE_SPI) && (defined(__APPLE__) || P44_BUILD_DIGI) && !P44_BUILD_RPI && !P44_BUILD_RB && !P44_BUILD_OW
#define DISABLE_SPI 1
#endif
#if !DISABLE_SPI
extern "C" {
#include <sys/ioctl.h>
#include <linux/spi/spidev.h>
}
#else
#warning "No SPI 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_SPI_SCRIPT_FUNCS
using namespace P44Script;
#endif
#define SPI_MAX_SPEED_HZ 100000 // 1MHz seems reasonable, faster sometimes does not work ok e.g. on RPi
// MARK: - I2C Manager
static SPIManager *sharedSPIManager = NULL;
SPIManager::SPIManager()
{
}
SPIManager::~SPIManager()
{
}
SPIManager &SPIManager::sharedManager()
{
if (!sharedSPIManager) {
sharedSPIManager = new SPIManager();
}
return *sharedSPIManager;
}
SPIDevicePtr SPIManager::getDevice(int aBusNumber, const char *aDeviceID)
{
// find or create bus
SPIBusMap::iterator pos = busMap.find(aBusNumber);
SPIBusPtr bus;
if (pos!=busMap.end()) {
bus = pos->second;
}
else {
// bus does not exist yet, create it
bus = SPIBusPtr(new SPIBus(aBusNumber));
busMap[aBusNumber] = bus;
}
// dissect device ID into type and busAddress
// - type string
// consists of Chip name plus optional options suffix. Like "MCP23S17" or "MCP23S17-xy" (xy=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
SPIDevicePtr dev = bus->getDevice(s.c_str());
if (!dev) {
// create device from typestring
if (typeString=="MCP23S17")
dev = SPIDevicePtr(new MCP23S17(deviceAddress, bus.get(), deviceOptions.c_str()));
else if (typeString=="MCP3008")
dev = SPIDevicePtr(new MCP3008(deviceAddress, bus.get(), deviceOptions.c_str()));
else if (typeString=="MCP3002")
dev = SPIDevicePtr(new MCP3002(deviceAddress, bus.get(), deviceOptions.c_str()));
else if (typeString=="generic")
dev = SPIDevicePtr(new SPIDevice(deviceAddress, bus.get(), deviceOptions.c_str()));
// TODO: add more device types
// Register new device
if (dev) {
bus->registerDevice(dev);
}
}
return dev;
}
// MARK: - SPIBus
SPIBus::SPIBus(int aBusNumber) :
busFD(-1),
busNumber(aBusNumber),
lastSpiMode(0xFF) // invalid mode -> force setting it on first use
{
}
SPIBus::~SPIBus()
{
closeBus();
}
void SPIBus::registerDevice(SPIDevicePtr aDevice)
{
deviceMap[aDevice->deviceID()] = aDevice;
}
SPIDevicePtr SPIBus::getDevice(const char *aDeviceID)
{
SPIDeviceMap::iterator pos = deviceMap.find(aDeviceID);
if (pos!=deviceMap.end())
return pos->second;
return SPIDevicePtr();
}
// Note that "cs_change" functionality is described in a misleading way
// in other places, e.g. in spidev.h - suggesting cs_change must be SET
// to make CS go inactive at the end of the transfer.
// However, truth is that cs_change just INVERTS the normal way of operation:
// - between multiple transfers, CS is normally kept active -> cs_change
// causes CS to go high quickly between transfers
// - after the last transfer, CS is normally made inactive -> cs_change
// causes CS to REMAIN ACTIVE. This apparently had no effect with
// spi-bcm2708 (old RPi SPI driver) but did completely mess up
// SPI communication with spi-bcm2835 (one single never ending transaction).
//
// Bottom line: under normal circumstances (i.e. one SPI_IOC_MESSAGE ioctl call
// per transaction), cs_change must not be set to do the right thing.
// The following is from include/linux/spi/spi.h:
//
// All SPI transfers start with the relevant chipselect active. Normally
// it stays selected until after the last transfer in a message. Drivers
// can affect the chipselect signal using cs_change.
//
// (i) If the transfer isn't the last one in the message, this flag is
// used to make the chipselect briefly go inactive in the middle of the
// message. Toggling chipselect in this way may be needed to terminate
// a chip command, letting a single spi_message perform all of group of
// chip transactions together.
//
// (ii) When the transfer is the last one in the message, the chip may
// stay selected until the next transfer. On multi-device SPI busses
// with nothing blocking messages going to other devices, this is just
// a performance hint; starting a message to another device deselects
// this one. But in other cases, this can be used to ensure correctness.
// Some devices need protocol transactions to be built from a series of
// spi_message submissions, where the content of one message is determined
// by the results of previous messages and where the whole transaction
// ends when the chipselect goes inactive.
int SPIBus::spidev_write_read(
SPIDevice *aDeviceP,
unsigned int num_out_bytes,
const uint8_t *out_buffer,
unsigned int num_in_bytes,
uint8_t *in_buffer,
bool writeWrite,
bool fullDuplex,
bool keepCSActive
)
{
#if SPI_SIMULATION
string s;
if (out_buffer && num_out_bytes>0) {
// show data written
s = "writes:";
while(num_out_bytes-- > 0) {
string_format_append(s, " %02X", *out_buffer++);
}
if (writeWrite && in_buffer) {
while(num_in_bytes-- > 0) {
string_format_append(s, " %02X", *in_buffer++);
}
}
}
if (in_buffer && !writeWrite) {
// feed back SPI_SIMULATION_READDATA
s += " - reads sim data:";
while(num_in_bytes-- > 0) {
// get simulated data
if (mSimReadData.size()>0) {
*in_buffer = mSimReadData[mSimDataIdx++];
if (mSimDataIdx>mSimReadData.size()) mSimDataIdx=0;
}
else {
*in_buffer = 0x42;
}
// show it
string_format_append(s, " %02X", *in_buffer++);
}
}
FOCUSLOG("SPI: %s", s.c_str());
if (!keepCSActive) mSimDataIdx=0; // reset read index at end of transaction
#elif !DISABLE_SPI
struct spi_ioc_transfer mesg[2];
uint8_t num_tr = 0;
int ret;
// init all fields of the struct, important for spi_bcm2835 driver (not relevant for spi_bcm2708)
for (int i=0; i<2; ++i) {
memset(&mesg[i], 0, sizeof (spi_ioc_transfer));
mesg[i].bits_per_word = 0; // means 8 -> From SPI_IOC_WR_BITS_PER_WORD docs: "The value zero signifies eight bits"
mesg[i].speed_hz = aDeviceP->speedHz; // current speed
}
// prepare output transfer, if any data provided
if((out_buffer != NULL) && (num_out_bytes != 0)) {
mesg[num_tr].tx_buf = (unsigned long)out_buffer;
mesg[num_tr].len = num_out_bytes;
if (fullDuplex) {
mesg[num_tr].rx_buf = (unsigned long)in_buffer;
if (num_in_bytes<num_out_bytes) return -1; // must be at least same number of input bytes as output
}
else {
mesg[num_tr].rx_buf = (unsigned long)NULL;
}
num_tr++;
}
// prepare input (or second write) transfer, if buffer provided
if(!fullDuplex && (in_buffer != NULL) && (num_in_bytes != 0)) {
if (writeWrite) {
mesg[num_tr].tx_buf = (unsigned long)in_buffer;
mesg[num_tr].rx_buf = (unsigned long)NULL;
}
else {
mesg[num_tr].tx_buf = (unsigned long)NULL;
mesg[num_tr].rx_buf = (unsigned long)in_buffer;
}
mesg[num_tr].len = num_in_bytes;
num_tr++;
}
// execute
if(num_tr > 0) {
// set cs_change on last transaction if CS should be KEPT ACTIVE
if (keepCSActive) mesg[num_tr-1].cs_change = 1;
// run the transfer(s)
ret = ioctl(busFD, SPI_IOC_MESSAGE(num_tr), mesg);
if(ret == 1) {
return 1;
}
}
#else
DBGFOCUSLOG("SPI_IOC_MESSAGE writing %d bytes and reading %d bytes", num_out_bytes, num_in_bytes);
#endif
return 0;
}
#define SPI_RD(dev) (((dev&0x7F)<<1) + 0x01)
#define SPI_WR(dev) ((dev&0x7F)<<1)
bool SPIBus::SPIRegReadByte(SPIDevice *aDeviceP, uint8_t aRegister, uint8_t &aByte)
{
if (!accessDevice(aDeviceP)) return false; // cannot read
uint8_t msg[2];
msg[0] = SPI_RD(aDeviceP->deviceAddress);
msg[1] = aRegister;
uint8_t ans = 0;
int res = spidev_write_read(aDeviceP, 2, msg, 1, &ans);
// read is shown only in real Debug log, because button polling creates lots of accesses
DBGFOCUSLOG("SPIRegReadByte(devaddr=0x%02X, reg=0x%02X) = %d / 0x%02X (res=%d)", aDeviceP->deviceAddress, aRegister, ans, ans, res);
if (res<0) return false;
aByte = (uint8_t)ans;
return true;
}
bool SPIBus::SPIRegReadWord(SPIDevice *aDeviceP, uint8_t aRegister, uint16_t &aWord)
{
if (!accessDevice(aDeviceP)) return false; // cannot read
uint8_t msg[2];
msg[0] = SPI_RD(aDeviceP->deviceAddress);
msg[1] = aRegister;
uint16_t ans = 0;
int res = spidev_write_read(aDeviceP, 2, msg, 2, (uint8_t *)&ans);
// read is shown only in real Debug log, because button polling creates lots of accesses
DBGFOCUSLOG("SPIRegReadWord(devaddr=0x%02X, reg=0x%02X) = %d / 0x%02X (res=%d)", aDeviceP->deviceAddress, aRegister, ans, ans, res);
if (res<0) return false;
aWord = ans;
return true;
}
bool SPIBus::SPIRegReadBytes(SPIDevice *aDeviceP, uint8_t aRegister, uint8_t aCount, uint8_t *aDataP)
{
if (!accessDevice(aDeviceP)) return false; // cannot read
uint8_t msg[2];
msg[0] = SPI_RD(aDeviceP->deviceAddress);
msg[1] = aRegister;
int res = spidev_write_read(aDeviceP, 2, msg, aCount, aDataP);
// read is shown only in real Debug log, because button polling creates lots of accesses
DBGFOCUSLOG("SPIRegReadBytes(devaddr=0x%02X, reg=0x%02X), %d bytes read (res=%d)", aDeviceP->deviceAddress, aRegister, aCount, res);
return (res>=0);
}
bool SPIBus::SPIRegWriteByte(SPIDevice *aDeviceP, uint8_t aRegister, uint8_t aByte)
{
if (!accessDevice(aDeviceP)) return false; // cannot write
uint8_t msg[3];
msg[0] = SPI_WR(aDeviceP->deviceAddress);
msg[1] = aRegister;
msg[2] = aByte;
int res = spidev_write_read(aDeviceP, 3, msg, 0, NULL);
FOCUSLOG("SPIRegWriteByte(devaddr=0x%02X, reg=0x%02X, byte=0x%02X), res=%d", aDeviceP->deviceAddress, aRegister, aByte, res);
return (res>=0);
}
bool SPIBus::SPIRegWriteWord(SPIDevice *aDeviceP, uint8_t aRegister, uint16_t aWord)
{
if (!accessDevice(aDeviceP)) return false; // cannot write
uint8_t msg[4];
msg[0] = SPI_WR(aDeviceP->deviceAddress);
msg[1] = aRegister;
*((uint16_t *)&(msg[2])) = aWord;
int res = spidev_write_read(aDeviceP, 4, msg, 0, NULL);
FOCUSLOG("SPIRegWriteWord(devaddr=0x%02X, reg=0x%02X, word=0x%04X), res=%d", aDeviceP->deviceAddress, aRegister, aWord, res);
return (res>=0);
}
bool SPIBus::SPIRegWriteBytes(SPIDevice *aDeviceP, uint8_t aRegister, uint8_t aCount, const uint8_t *aDataP)
{
if (!accessDevice(aDeviceP)) return false; // cannot write
uint8_t msg[2];
msg[0] = SPI_WR(aDeviceP->deviceAddress);
msg[1] = aRegister;
int res = spidev_write_read(aDeviceP, 2, msg, aCount, (uint8_t *)aDataP, true);
FOCUSLOG("SPIRegWriteBytes(devaddr=0x%02X, reg=0x%02X), %d bytes written (res=%d)", aDeviceP->deviceAddress, aRegister, aCount, res);
return (res>=0);
}
bool SPIBus::SPIRawWriteRead(SPIDevice *aDeviceP, unsigned int aOutSz, const uint8_t *aOutP, unsigned int aInSz, uint8_t *aInP, bool aFullDuplex, bool aKeepCSActive)
{
if (!accessDevice(aDeviceP)) return false; // cannot access
int res = spidev_write_read(aDeviceP, aOutSz, aOutP, aInSz, aInP, false, aFullDuplex, aKeepCSActive);
// shown only in real Debug log, because polling creates lots of accesses
DBGFOCUSLOG("SPIRawWriteRead(devaddr=0x%02X), %d bytes written, %d bytes read (res=%d)", aDeviceP->deviceAddress, aOutSz, aFullDuplex ? aOutSz : aInSz, res);
return (res>=0);
}
bool SPIBus::accessDevice(SPIDevice *aDeviceP)
{
if (!accessBus())
return false;
if (aDeviceP->spimode == lastSpiMode)
return true; // already prepared to access that device
// set the SPI mode
#if !DISABLE_SPI
if (ioctl(busFD, SPI_IOC_WR_MODE, &aDeviceP->spimode) < 0) {
LOG(LOG_ERR, "Error: Cannot SPI_IOC_WR_MODE for device '%s' on bus %d", aDeviceP->deviceID().c_str(), busNumber);
lastSpiMode = 0; // assume default mode
return false;
}
#endif
FOCUSLOG("ioctl(busFD, SPI_IOC_WR_MODE, 0x%02X)", aDeviceP->spimode);
// remember
lastSpiMode = aDeviceP->spimode;
return true; // ok
}
bool SPIBus::accessBus()
{
if (busFD>=0)
return true; // already open
// need to open
lastSpiMode = 0; // assume default mode
string busDevName = string_format("/dev/spidev%d.%d", busNumber/10, busNumber%10);
#if !DISABLE_SPI
busFD = open(busDevName.c_str(), O_RDWR);
if (busFD<0) {
LOG(LOG_ERR, "Error: Cannot open SPI device '%s'",busDevName.c_str());
return false;
}
// - limit max speed
uint32_t speed = SPI_MAX_SPEED_HZ;
if (ioctl(busFD, SPI_IOC_WR_MAX_SPEED_HZ, &speed) < 0) {
LOG(LOG_ERR, "Error: Cannot SPI_IOC_WR_MAX_SPEED_HZ for bus %d", busNumber);
return false;
}
// - at this time, we only support 8-bit words
uint8_t bpw = 0; // means 8 -> From SPI_IOC_WR_BITS_PER_WORD docs: "The value zero signifies eight bits"
if (ioctl(busFD, SPI_IOC_WR_BITS_PER_WORD, &bpw) < 0) {
LOG(LOG_ERR, "Error: Cannot SPI_IOC_WR_BITS_PER_WORD for bus %d", busNumber);
return false;
}
#else
busFD = 1; // dummy, signalling open
#endif
FOCUSLOG("open(\"%s\", O_RDWR) = %d", busDevName.c_str(), busFD);
return true;
}
void SPIBus::closeBus()
{
if (busFD>=0) {
#ifndef __APPLE__
close(busFD);
#endif
busFD = -1;
}
}
// MARK: - SPIDevice
// #define SPI_CPHA 0x01
// #define SPI_CPOL 0x02
//
// #define SPI_MODE_0 (0|0)
// #define SPI_MODE_1 (0|SPI_CPHA)
// #define SPI_MODE_2 (SPI_CPOL|0)
// #define SPI_MODE_3 (SPI_CPOL|SPI_CPHA)
//
// #define SPI_CS_HIGH 0x04
// #define SPI_LSB_FIRST 0x08
// #define SPI_3WIRE 0x10
// #define SPI_LOOP 0x20
// #define SPI_NO_CS 0x40
// #define SPI_READY 0x80
SPIDevice::SPIDevice(uint8_t aDeviceAddress, SPIBus *aBusP, const char *aDeviceOptions)
{
spibus = aBusP;
deviceAddress = aDeviceAddress;
// evaluate SPI mode options
spimode = 0;
speedHz = SPI_MAX_SPEED_HZ; // use bus' default max speed
#if !DISABLE_SPI
if (strchr(aDeviceOptions, 'H')) spimode |= SPI_CPHA; // inverted phase (compared to original microwire SPI)
if (strchr(aDeviceOptions, 'P')) spimode |= SPI_CPOL; // inverted polarity (compared to original microwire SPI)
if (strchr(aDeviceOptions, 'C')) spimode |= SPI_CS_HIGH; // chip select high
if (strchr(aDeviceOptions, 'N')) spimode |= SPI_NO_CS; // no chip select
if (strchr(aDeviceOptions, '3')) spimode |= SPI_3WIRE; // 3 wire
if (strchr(aDeviceOptions, 'R')) spimode |= SPI_READY; // slave pulls low to pause
// reduced speeds
if (strchr(aDeviceOptions, 'S')) speedHz /= 10; // slow speed - 1/10 of normal
if (strchr(aDeviceOptions, 's')) speedHz /= 100; // very slow speed - 1/100 of normal
#endif
}
string SPIDevice::deviceID()
{
return string_format("%s@%02X", deviceType(), deviceAddress);
}
bool SPIDevice::isKindOf(const char *aDeviceType)
{
return (strcmp(deviceType(),aDeviceType)==0);
}
bool SPIDevice::SPIRawWriteRead(unsigned int aOutSz, const uint8_t *aOutP, unsigned int aInSz, uint8_t *aInP, bool aFullDuplex, bool aKeepCSActive)
{
return spibus->SPIRawWriteRead(this, aOutSz, aOutP, aInSz, aInP, aFullDuplex, aKeepCSActive);
}
// MARK: - SPIBitPortDevice
SPIBitPortDevice::SPIBitPortDevice(uint8_t aDeviceAddress, SPIBus *aBusP, const char *aDeviceOptions) :
inherited(aDeviceAddress, aBusP, aDeviceOptions),
outputEnableMask(0),
pinStateMask(0),
pullUpMask(0)
{
}
bool SPIBitPortDevice::isKindOf(const char *aDeviceType)
{
if (strcmp(deviceType(),aDeviceType)==0)
return true;
else
return inherited::isKindOf(aDeviceType);
}
bool SPIBitPortDevice::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 SPIBitPortDevice::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 SPIBitPortDevice::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: - MCP23S17
MCP23S17::MCP23S17(uint8_t aDeviceAddress, SPIBus *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')) {
spibus->SPIRegWriteByte(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
spibus->SPIRegWriteByte(this, 0x02, 0); // reset polarity inversion A
spibus->SPIRegWriteByte(this, 0x03, 0); // reset polarity inversion B
}
bool MCP23S17::isKindOf(const char *aDeviceType)
{
if (strcmp(deviceType(),aDeviceType)==0)
return true;
else
return inherited::isKindOf(aDeviceType);
}
void MCP23S17::updateInputState(int aForBitNo)
{
if (aForBitNo>15) return;
uint8_t port = aForBitNo >> 3; // calculate port No
uint8_t shift = 8*port;
uint8_t data;
spibus->SPIRegReadByte(this, port+0x12, data); // get current port state from GPIO reg 0x12/0x13
pinStateMask = (pinStateMask & ~(((uint32_t)0xFF) << shift)) | ((uint32_t)data << shift);
}
void MCP23S17::updateOutputs(int aForBitNo)
{
if (aForBitNo>15) return;
uint8_t port = aForBitNo >> 3; // calculate port No
uint8_t shift = 8*port;
spibus->SPIRegWriteByte(this, port+0x14, (outputStateMask >> shift) & 0xFF); // write to output latch (OLAT) A/B reg 0x14/0x15
}
void MCP23S17::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
spibus->SPIRegWriteByte(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
spibus->SPIRegWriteByte(this, port+0x00, data); // set input enable flags in IODIR reg 0 or 1
}
// MARK: - SPIpin
/// create SPI based digital input or output pin (or use an analog pin as digital I/O)
SPIPin::SPIPin(int aBusNumber, const char *aDeviceId, int aPinNumber, bool aOutput, bool aInitialState, Tristate aPull) :
output(false),
lastSetState(false)
{
pinNumber = aPinNumber;
output = aOutput;
SPIDevicePtr dev = SPIManager::sharedManager().getDevice(aBusNumber, aDeviceId);
bitPortDevice = boost::dynamic_pointer_cast<SPIBitPortDevice>(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 SPIPin::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 SPIPin::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: - SPIAnalogPortDevice
SPIAnalogPortDevice::SPIAnalogPortDevice(uint8_t aDeviceAddress, SPIBus *aBusP, const char *aDeviceOptions) :
inherited(aDeviceAddress, aBusP, aDeviceOptions)
{
}
bool SPIAnalogPortDevice::isKindOf(const char *aDeviceType)
{
if (strcmp(deviceType(),aDeviceType)==0)
return true;
else
return inherited::isKindOf(aDeviceType);
}
// MARK: - MCP3008
MCP3008::MCP3008(uint8_t aDeviceAddress, SPIBus *aBusP, const char *aDeviceOptions) :
inherited(aDeviceAddress, aBusP, aDeviceOptions)
{
// currently no device options
// int b = atoi(aDeviceOptions);
}
bool MCP3008::isKindOf(const char *aDeviceType)
{
if (strcmp(deviceType(),aDeviceType)==0)
return true;
else
return inherited::isKindOf(aDeviceType);
}
double MCP3008::getPinValue(int aPinNo)
{
// MCP3008 needs to transfer 3 bytes in and out for one conversion
uint8_t out[3];
uint8_t in[3];
uint16_t raw = 0;
// - first byte is 7 zero dummy bits plus LSB==1==start bit
out[0] = 0x01;
// - second byte is 4 bit channel selection/differential vs single, plus 4 bit dummy
// Bit 7 Bit 6 Bit 5 Bit 4
// D/S CHSEL3 CHSEL2 CHSEL1
// 0=Diff
// 1=Single
// - we invert the D/S bit to have 1:1 PinNo->Single ended channel assignments (0..7).
// PinNo 8..15 then represent the differential modes, see data sheet.
out[1] = (aPinNo^0x08)<<4;
// - third byte is dummy
out[2] = 0;
if (spibus->SPIRawWriteRead(this, 3, out, 3, in, true)) {
// A/D output data are 10 LSB of data read back
raw = ((uint16_t)(in[1] & 0x03)<<8) + in[2];
}
// return raw value (no physical unit at this level known, and no scaling or offset either)
return raw;
}
bool MCP3008::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: - MCP3002
MCP3002::MCP3002(uint8_t aDeviceAddress, SPIBus *aBusP, const char *aDeviceOptions) :
inherited(aDeviceAddress, aBusP, aDeviceOptions)
{
// currently no device options
// int b = atoi(aDeviceOptions);
}
bool MCP3002::isKindOf(const char *aDeviceType)
{
if (strcmp(deviceType(),aDeviceType)==0)
return true;
else
return inherited::isKindOf(aDeviceType);
}
double MCP3002::getPinValue(int aPinNo)
{
// MCP3002 needs to transfer 3 bytes in and out for one conversion
// Note: with a correctly working SPI (not the case in MT7688),
// 2 bytes would be sufficient. But as the first returned byte
// is flawed in MT7688 (see @wdu's comment in Onion forum:
// "In full duplex, I have observed errors in the second bit (always
// 1 or 0, don't remember exactly), depending on the state of the
// first bit. This makes the first transmitted byte unreliable."),
// this implementation shifts the bit such that first returned byte
// can be discarded entirely.
uint8_t out[3];
uint8_t in[3];
uint16_t raw = 0;
// - first byte is 4 zero dummy bits, then 1==start bit, then:
// Bit 2 Bit 1 Bit 0
// D/S CHSEL MSBFirst
// - we invert the D/S bit to have 1:1 PinNo->Single ended channel assignments (0,1).
// PinNo 2,3 then represent the differential modes, see data sheet.
out[0] =
0x08 | // start bit
(((aPinNo&0x03)^0x02)<<1) | // channel and mode selection
0x01; // MSB first
// - second and third byte is dummy
out[1] = 0;
out[2] = 0;
DBGFOCUSLOG("MCP3002 write: 0x%02X, 0x%02X, 0x%02X", out[0], out[1], out[2]);
if (spibus->SPIRawWriteRead(this, 3, out, 3, in, true)) {
// first byte returned is broken anyway on MT7688, no data there
// second byte contains a 0 in Bit7, Bit6..0 = Bit9..3 of result
// third byte contains Bit7..5 = Bit2..0 of result, rest is dummy
DBGFOCUSLOG("MCP3002 read: 0x%02X, 0x%02X, 0x%02X", in[0], in[1], in[2]);
raw = ((uint16_t)(in[1] & 0x7F)<<3) + (in[2]>>5);
}
// return raw value (no physical unit at this level known, and no scaling or offset either)
return raw;
}
bool MCP3002::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: - AnalogSPIpin
/// create spi based digital input or output pin
AnalogSPIPin::AnalogSPIPin(int aBusNumber, const char *aDeviceId, int aPinNumber, bool aOutput, double aInitialValue) :
output(false)
{
pinNumber = aPinNumber;
output = aOutput;
SPIDevicePtr dev = SPIManager::sharedManager().getDevice(aBusNumber, aDeviceId);
analogPortDevice = boost::dynamic_pointer_cast<SPIAnalogPortDevice>(dev);
if (analogPortDevice && output) {
analogPortDevice->setPinValue(pinNumber, aInitialValue);
}
}
/// get state of pin
/// @return current state (from actual GPIO pin for inputs, from last set state for outputs)
double AnalogSPIPin::getValue()
{
if (analogPortDevice) {
return analogPortDevice->getPinValue(pinNumber);
}
return 0;
}
/// set state of pin (NOP for inputs)
/// @param aState new state to set output to
void AnalogSPIPin::setValue(double aValue)
{
if (analogPortDevice && output) {
analogPortDevice->setPinValue(pinNumber, aValue);
}
}
bool AnalogSPIPin::getRange(double &aMin, double &aMax, double &aResolution)
{
if (analogPortDevice) {
return analogPortDevice->getPinRange(pinNumber, aMin, aMax, aResolution);
}
return false;
}
#if ENABLE_SPI_SCRIPT_FUNCS
// MARK: - SPI scripting
SPIDeviceObjPtr SPIDevice::representingScriptObj()
{
if (!mRepresentingObj) {
mRepresentingObj = new SPIDeviceObj(this);
}
return mRepresentingObj;
}
// regread(reg [,type, [, count])
FUNC_ARG_DEFS(regread, { numeric }, { text|optionalarg } );
static void regread_func(BuiltinFunctionContextPtr f)
{
SPIDeviceObj* o = dynamic_cast<SPIDeviceObj*>(f->thisObj().get());
assert(o);
SPIDevice* dev = o->spidevice().get();
SPIBus& bus = dev->getBus();
uint8_t reg = f->arg(0)->intValue();
string ty;
if (f->arg(1)->defined()) ty = f->arg(1)->stringValue();
if (ty=="word") {
// 16 bit word
uint16_t w;
if (bus.SPIRegReadWord(dev, reg, w)) {
f->finish(new IntegerValue(w));
return;
}
}
else if (ty=="bytes") {
// a number of bytes
uint8_t count = 1;
if (f->arg(2)->defined()) count = f->arg(2)->intValue();
uint8_t buf[256];
if (bus.SPIRegReadBytes(dev, reg, count, buf)) {
string data((char *)buf, (size_t)count);
f->finish(new StringValue(data));
return;
}
}
else {
// byte
uint8_t b;
if (bus.SPIRegReadByte(dev, reg, b)) {
f->finish(new IntegerValue(b));
return;
}
}
// no success
f->finish(new ErrorValue(TextError::err("i2c smbus read error")));
}
// regwrite(reg, value [,type])
FUNC_ARG_DEFS(regwrite, { numeric }, { text|numeric }, { text|optionalarg } );
static void regwrite_func(BuiltinFunctionContextPtr f)
{
SPIDeviceObj* o = dynamic_cast<SPIDeviceObj*>(f->thisObj().get());
assert(o);
SPIDevice* dev = o->spidevice().get();
SPIBus& bus = dev->getBus();
uint8_t reg = f->arg(0)->intValue();
string ty;