-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMCP4728.c
691 lines (566 loc) · 17.9 KB
/
MCP4728.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
/*
Marko Pinteric 2020-2025
GPIO communication based on Tiny GPIO Access on http://abyz.me.uk/rpi/pigpio/examples.html
Version 2:
- A more precise 10kHz timing;
- The SDA line has the 7% clock period delay, mimicking Raspberry Pi's I2C kernel performance.
To create C library execute 'make' (library in local directory) or 'make install' (shared library).
To remove C library execute 'make clear' (library in local directory) or 'make uninstall' (shared library).
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <fcntl.h>
#include <stdbool.h>
#include <time.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <linux/i2c-dev.h>
/* TINY GPIO VARIABLES */
#define GPSET0 7
#define GPSET1 8
#define GPCLR0 10
#define GPCLR1 11
#define GPLEV0 13
#define GPLEV1 14
#define GPPUD 37
#define GPPUDCLK0 38
#define GPPUDCLK1 39
/* GPIO address */
volatile static uint32_t *gpioReg = MAP_FAILED;
#define PI_BANK (gpio>>5)
#define PI_BIT (1<<(gpio&0x1F))
/* gpio modes */
#define PI_INPUT 0
#define PI_OUTPUT 1
#define PI_ALT0 4
#define PI_ALT1 5
#define PI_ALT2 6
#define PI_ALT3 7
#define PI_ALT4 3
#define PI_ALT5 2
/* values for pull-ups/downs off, pull-down && pull-up */
#define PI_PUD_OFF 0
#define PI_PUD_DOWN 1
#define PI_PUD_UP 2
/* LOCAL VARIABLES */
#define UNDEFINED 0xFFFF
/* data for single instance of the chip */
struct chip
{
unsigned sda;
unsigned scl;
unsigned ldac;
unsigned address;
unsigned bus;
};
struct chip *curchip;
/* time of the last clocked GPIO pin mode change */
struct timespec ttime;
/* communication initialised */
bool init_gpio=false, init_i2c_0=false, init_i2c_1=false;
/* I2C address */
int file_i2c_0, file_i2c_1;
/* I2C clients count */
int count_i2c_0=0, count_i2c_1=0;
/* TINY GPIO METHODS */
void gpioSetMode(unsigned gpio, unsigned mode)
{
int reg, shift;
reg = gpio/10;
shift = (gpio%10) * 3;
gpioReg[reg] = (gpioReg[reg] & ~(7<<shift)) | (mode<<shift);
}
int gpioGetMode(unsigned gpio)
{
int reg, shift;
reg = gpio/10;
shift = (gpio%10) * 3;
return (*(gpioReg + reg) >> shift) & 7;
}
void gpioSetPullUpDown(unsigned gpio, unsigned pud)
{
*(gpioReg + GPPUD) = pud;
usleep(20);
*(gpioReg + GPPUDCLK0 + PI_BANK) = PI_BIT;
usleep(20);
*(gpioReg + GPPUD) = 0;
*(gpioReg + GPPUDCLK0 + PI_BANK) = 0;
}
int gpioRead(unsigned gpio)
{
if ((*(gpioReg + GPLEV0 + PI_BANK) & PI_BIT) != 0) return 1;
else return 0;
}
void gpioWrite(unsigned gpio, unsigned level)
{
if (level == 0) *(gpioReg + GPCLR0 + PI_BANK) = PI_BIT;
else *(gpioReg + GPSET0 + PI_BANK) = PI_BIT;
}
int gpioInitialise(void)
{
int fd;
fd = open("/dev/gpiomem", O_RDWR | O_SYNC) ;
if (fd < 0)
{
fprintf(stderr, "failed to open /dev/gpiomem\n");
return -1;
}
gpioReg = (uint32_t *)mmap(NULL, 0xB4, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
close(fd);
if (gpioReg == MAP_FAILED)
{
fprintf(stderr, "Bad, mmap failed\n");
return -1;
}
return 0;
}
/* LOCAL METHODS FOR MANIPULATING GPIO PINS IN REGARD TO CLOCK */
/* write GPIO pin 7% clock period from from the last clocked GPIO pin mode change */
void gpioWriteShifted(unsigned gpio, unsigned mode)
{
struct timespec ctime;
uint64_t ntime;
int reg, shift;
reg = gpio/10;
shift = (gpio%10) * 3;
/* 700ns is 7% clock period for 10kHz */
ntime = ttime.tv_sec * (uint64_t)1000000000L + ttime.tv_nsec + 700;
while(1)
{
clock_gettime(CLOCK_MONOTONIC,&ctime);
if (ctime.tv_sec * (uint64_t)1000000000L + ctime.tv_nsec >= ntime) break;
}
gpioReg[reg] = (gpioReg[reg] & ~(7<<shift)) | (mode<<shift);
}
/* read GPIO pin 7% clock period from the last clocked GPIO pin mode change */
unsigned gpioReadShifted(unsigned gpio)
{
struct timespec ctime;
uint64_t ntime;
/* 700ns is 7% clock period for 10kHz */
ntime = ttime.tv_sec * (uint64_t)1000000000L + ttime.tv_nsec + 700;
while(1)
{
clock_gettime(CLOCK_MONOTONIC,&ctime);
if (ctime.tv_sec * (uint64_t)1000000000L + ctime.tv_nsec >= ntime) break;
}
if ((*(gpioReg + GPLEV0 + (gpio>>5)) & (1<<(gpio&0x1F))) != 0) return(1);
else return(0);
}
/* write GPIO pin 50% clock period from from the last clocked GPIO pin mode change */
void gpioWriteTarget(unsigned gpio, unsigned mode)
{
struct timespec ctime;
uint64_t ntime;
int reg, shift;
reg = gpio/10;
shift = (gpio%10) * 3;
clock_gettime(CLOCK_MONOTONIC,&ctime);
/* if less than 7% of clock period remains, the Raspberry Pi got distracted, make at least 7% */
/* 700ns is 7% clock period, 4300ns is (50%-7%) clock period for 10kHz */
if (ctime.tv_sec * (uint64_t)1000000000L + ctime.tv_nsec >= ttime.tv_sec * (uint64_t)1000000000L + ttime.tv_nsec + 4300)
{
ttime = ctime;
ttime.tv_nsec = ttime.tv_nsec + 700;
}
/* 5000ns is 50% clock period for 10kHz */
else
{
ttime.tv_nsec = ttime.tv_nsec + 5000;
}
if(ttime.tv_nsec >= 1000000000L)
{
ttime.tv_nsec = ttime.tv_nsec - 1000000000L;
ttime.tv_sec = ttime.tv_sec + 1;
}
ntime = ttime.tv_sec * (uint64_t)1000000000L + ttime.tv_nsec;
while(1)
{
clock_gettime(CLOCK_MONOTONIC,&ctime);
if (ctime.tv_sec * (uint64_t)1000000000L + ctime.tv_nsec >= ntime) break;
}
gpioReg[reg] = (gpioReg[reg] & ~(7<<shift)) | (mode<<shift);
}
/* LOCAL GPIO METHODS */
/* all assume && leave SCL low, except when specified differently */
/* start GPIO communication */
void start_gpio()
{
gpioWrite(curchip->sda,0);
gpioWrite(curchip->scl,0);
gpioWrite(curchip->ldac,0);
gpioSetPullUpDown(curchip->sda,PI_PUD_OFF);
gpioSetPullUpDown(curchip->scl,PI_PUD_OFF);
gpioSetPullUpDown(curchip->ldac,PI_PUD_OFF);
gpioSetMode(curchip->sda, PI_INPUT);
gpioSetMode(curchip->scl, PI_INPUT);
gpioSetMode(curchip->ldac, PI_INPUT);
}
/* stop GPIO communication */
void stop_gpio()
{
if ((curchip->bus == 0) || (curchip->bus == 1))
{
gpioSetPullUpDown(curchip->sda,PI_PUD_UP);
gpioSetPullUpDown(curchip->scl,PI_PUD_UP);
gpioSetMode(curchip->sda, PI_ALT0);
gpioSetMode(curchip->scl, PI_ALT0);
}
}
/* following method assumes SCL && SDA high */
void i2cstart()
{
gpioWriteTarget(curchip->sda, PI_OUTPUT);
gpioWriteTarget(curchip->scl, PI_OUTPUT);
}
void i2crestart()
{
gpioWriteTarget(curchip->sda, PI_INPUT);
gpioWriteTarget(curchip->scl, PI_INPUT);
gpioWriteTarget(curchip->sda, PI_OUTPUT);
gpioWriteTarget(curchip->scl, PI_OUTPUT);
}
/* following methods leave SCL && SDA high */
void i2cstop()
{
gpioWriteTarget(curchip->sda, PI_OUTPUT);
gpioWriteTarget(curchip->scl, PI_INPUT);
gpioWriteTarget(curchip->sda, PI_INPUT);
}
unsigned i2cgetbyte()
{
unsigned i;
unsigned res = 0;
/* release SDA line */
gpioWriteShifted(curchip->sda, PI_INPUT);
for (i=0; i<8; i++)
{
/* rise SCL line */
gpioWriteTarget(curchip->scl, PI_INPUT);
if (gpioReadShifted(curchip->sda) != 0) res = res | (0x80 >> i);
/* drop SCL line */
gpioWriteTarget(curchip->scl, PI_OUTPUT);
}
return(res);
}
void i2csendbyte(unsigned res)
{
unsigned mode;
unsigned i;
for (i=0; i<8; i++)
{
/* set SDA line */
if((res & 0x80) == 0x80) mode = PI_INPUT;
else mode = PI_OUTPUT;
gpioWriteShifted(curchip->sda, mode);
res = res << 1;
/* rise SCL line */
gpioWriteTarget(curchip->scl, PI_INPUT);
/* drop SCL line */
gpioWriteTarget(curchip->scl, PI_OUTPUT);
}
}
unsigned i2cgetack()
{
unsigned res;
/* release SDA line */
gpioWriteShifted(curchip->sda, PI_INPUT);
/* rise SCL line */
gpioWriteTarget(curchip->scl, PI_INPUT);
/* read SDA line */
if (gpioReadShifted(curchip->sda) != 0) res = 1;
else res = 0;
/* drop SCL line */
gpioWriteTarget(curchip->scl, PI_OUTPUT);
return(res);
}
void i2csendack()
{
/* rise SDA line */
gpioWriteShifted(curchip->sda, PI_OUTPUT);
/* rise SCL line */
gpioWriteTarget(curchip->scl, PI_INPUT);
/* drop SCL line */
gpioWriteTarget(curchip->scl, PI_OUTPUT);
}
void i2csendnack()
{
/* drop SDA line */
gpioWriteShifted(curchip->sda, PI_INPUT);
/* rise SCL line */
gpioWriteTarget(curchip->scl, PI_INPUT);
/* drop SCL line */
gpioWriteTarget(curchip->scl, PI_OUTPUT);
}
/* LOCAL I2C METHODS */
/* set I2C address */
int address_i2c()
{
int file_i2c;
if (curchip->bus == 0) file_i2c=file_i2c_0;
if (curchip->bus == 1) file_i2c=file_i2c_1;
if(ioctl(file_i2c, I2C_SLAVE, curchip->address) < 0) return(-1);
return(0);
}
/* write multiple raw values to the specified DAC channels - channels 1 to 4, EEPROM not affected */
int multiple_raw(unsigned size, unsigned channels[], unsigned reference, unsigned gains[], unsigned values[])
{
unsigned i;
unsigned char buffer[3*size];
int file_i2c;
if (curchip->bus == 0) file_i2c=file_i2c_0;
if (curchip->bus == 1) file_i2c=file_i2c_1;
for (i = 0; i < size; i++)
{
/* first byte is 0b01000XXU, where XX is channel address (0-3) */
buffer[3*i] = 0x40 | ((channels[i]-1) << 1);
/* second word is XPPYDDDD DDDDDDDD, where X is reference, Y is gain && D is data */
buffer[3*i+1] = (((values[i] >> 8) & 0x0F) | (reference << 7) | (gains[i] << 4));
buffer[3*i+2] = values[i] & 0xFF;
}
if (write(file_i2c, buffer, 3*size) != 3*size) return(-1);
return(0);
}
/* write four sequential raw values to the all DAC channels - channels 1 to 4, EEPROM affected */
int sequential_raw(unsigned reference, unsigned gains[], unsigned values[])
{
unsigned i;
unsigned char buffer[9];
int file_i2c;
if (curchip->bus == 0) file_i2c=file_i2c_0;
if (curchip->bus == 1) file_i2c=file_i2c_1;
/* first byte is 0101000U for writing starting at channel */
buffer[0]=0x50;
for (i = 0; i < 4; i++)
{
/* second word is XPPYDDDD DDDDDDDD, where X is reference, Y is gain && D is data */
buffer[2*i+1] = (((values[i] >> 8) & 0x0F) | (reference << 7) | (gains[i] << 4));
buffer[2*i+2] = values[i] & 0xFF;
}
if (write(file_i2c, buffer, 9) != 9) return(-1);
return(0);
}
/* write single raw value to the selected DAC channel - channels 1 to 4, EEPROM affected */
int single_raw(unsigned channel, unsigned reference, unsigned gain, unsigned value)
{
unsigned char buffer[3];
int file_i2c;
if (curchip->bus == 0) file_i2c=file_i2c_0;
if (curchip->bus == 1) file_i2c=file_i2c_1;
/* first byte is 01011XXU, where XX is channel address (0-3) */
buffer[0]=0x58 | ((channel-1) << 1);
/* second word is XPPYDDDD DDDDDDDD, where X is reference, Y is gain && D is data */
buffer[1] = (((value >> 8) & 0x0F) | (reference << 7) | (gain << 4));
buffer[2] = value & 0xFF;
if (write(file_i2c, buffer, 3) != 3) return(-1);
return(0);
}
/* GLOBAL INITIALIZATION METHODS */
/* initialise communications */
struct chip *initialise(int sda, int scl, int ldac, int address)
{
struct chip *tempchip = malloc(sizeof(struct chip));
if((sda>27) || (sda<0) || (scl>27) || (scl<0)) fprintf(stderr, "SDA and SCL out of range\n");
if((address>0x07) || (address<0x00)) address = 0x60 | address;
if((address>0x67) || (address<0x60)) address=UNDEFINED;
if((ldac>27) || (ldac<0)) ldac=UNDEFINED;
tempchip->sda=(unsigned)sda;
tempchip->scl=(unsigned)scl;
tempchip->ldac=(unsigned)ldac;
tempchip->address=(unsigned)address;
tempchip->bus=UNDEFINED;
if ((ldac !=UNDEFINED) && !init_gpio)
{
if (gpioInitialise() < 0) return(NULL);
init_gpio = true;
}
if ((sda == 0) && (scl == 1))
{
tempchip->bus=0;
count_i2c_0 = count_i2c_0 + 1;
if (!init_i2c_0)
{
char *filename = (char*)"/dev/i2c-0";
file_i2c_0 = open(filename, O_RDWR);
if (file_i2c_0 < 0) fprintf(stderr, "Failed to open the i2c-0 bus\n");
else init_i2c_0=true;
}
}
if ((sda == 2) && (scl == 3))
{
tempchip->bus=1;
count_i2c_1 = count_i2c_1 + 1;
if (!init_i2c_1)
{
char *filename = (char*)"/dev/i2c-1";
file_i2c_1 = open(filename, O_RDWR);
if (file_i2c_1 < 0) fprintf(stderr, "Failed to open the i2c-1 bus\n");
else init_i2c_1=true;
}
}
return(tempchip);
}
/* deinitialise communications */
int deinitialise(struct chip *tempchip)
{
if (tempchip->bus == 0) count_i2c_0 = count_i2c_0 - 1;
if (tempchip->bus == 1) count_i2c_1 = count_i2c_1 - 1;
if ((init_i2c_0) && (count_i2c_0 == 0))
{
close(file_i2c_0);
init_i2c_0 = false;
}
if ((init_i2c_1) && (count_i2c_1 == 0))
{
close(file_i2c_1);
init_i2c_1 = false;
}
free(tempchip);
return(0);
}
/* GLOBAL GPIO METHODS */
/* get the DAC address */
int getaddress(struct chip *tempchip)
{
unsigned i;
int ret;
unsigned errs[4], addr[2], res, err=0;
curchip=tempchip;
if (!init_gpio || (curchip->ldac==0)) return(0x1000);
start_gpio();
clock_gettime(CLOCK_MONOTONIC,&ttime);
i2cstart();
i2csendbyte(0x00);
errs[0]=i2cgetack();
i2csendbyte(0x0C);
gpioWriteShifted(curchip->ldac, PI_OUTPUT);
errs[1]=i2cgetack();
i2crestart();
i2csendbyte(0xC1);
gpioWriteShifted(curchip->ldac, PI_INPUT);
errs[2]=i2cgetack();
res=i2cgetbyte();
i2csendnack();
i2cstop();
addr[0] = (res & 0xE0) >> 5;
addr[1] = (res & 0x0E) >> 1;
if ((addr[0] != addr[1]) || ((res & 0x11) != 0x10)) errs[3] = 1;
else errs[3] = 0;
for (i=0; i<=3; i++)
{
if (errs[i]!=0) err = err | (0x08 >> i);
}
if (err>0)
{
ret=-(int)err;
curchip->address = UNDEFINED;
}
else
{
res = 0x60 | addr[0];
curchip->address = res;
ret=(int)res;
}
stop_gpio();
return(ret);
}
/* set the DAC address */
int setaddress(struct chip *tempchip, unsigned addr)
{
unsigned i;
int ret;
unsigned errs[4], err=0;
unsigned addr_cur, addr_new;
curchip=tempchip;
if (!init_gpio || (curchip->ldac==0) || (curchip->address==0)) return(0x1000);
start_gpio();
clock_gettime(CLOCK_MONOTONIC,&ttime);
addr_cur = curchip->address & 0x07;
addr_new = addr & 0x07;
i2cstart();
i2csendbyte(0xC0 | (addr_cur << 1));
errs[0]=i2cgetack();
i2csendbyte(0x61 | (addr_cur << 2));
gpioSetMode(curchip->ldac, PI_OUTPUT);
errs[1]=i2cgetack();
i2csendbyte(0x62 | (addr_new << 2));
errs[2]=i2cgetack();
i2csendbyte(0x63 | (addr_new << 2));
gpioSetMode(curchip->ldac, PI_INPUT);
errs[3]=i2cgetack();
i2cstop();
for (i=0; i<=3; i++)
{
if (errs[i]!=0) err = err | (0x08 >> i);
}
if (err>0) ret=-(int)err;
else
{
curchip->address = addr;
ret = 0;
}
stop_gpio();
return(ret);
}
/* GLOBAL I2C METHODS */
/* write single value to the selected DAC channel using internal reference - channels 1 to 4 */
int singleinternal(struct chip *tempchip, int channel, float volt, bool eeprom)
{
unsigned gain=1;
unsigned value;
curchip=tempchip;
if (curchip->bus == UNDEFINED) return(-1);
if (address_i2c() == -1) return(-2);
if(volt>2) gain=2;
value=(unsigned)(0x1000 * volt/2.048/gain);
if(eeprom) return(single_raw((unsigned)channel,1,gain-1,value));
else return(multiple_raw(1,(unsigned[]){(unsigned)channel},1,(unsigned[]){gain-1},(unsigned[]){value}));
}
/* write single value to the selected DAC channel using external reference - channels 1 to 4 */
int singleexternal(struct chip *tempchip, int channel, float rel, bool eeprom)
{
unsigned value;
curchip=tempchip;
if (curchip->bus == UNDEFINED) return(-1);
if (address_i2c() == -1) return(-2);
value=(unsigned)(0x1000 * rel);
if(eeprom) return(single_raw((unsigned)channel,0,0,value));
else return(multiple_raw(1,(unsigned[]){(unsigned)channel},0,(unsigned[]){0},(unsigned[]){value}));
}
/* write four values to the DAC channels using internal reference */
int multipleinternal(struct chip *tempchip, float volts[], bool eeprom)
{
unsigned i;
unsigned gain;
unsigned values[4];
unsigned gains[4];
curchip=tempchip;
if (curchip->bus == UNDEFINED) return(-1);
if (address_i2c() == -1) return(-2);
for (i = 0; i < 4; i++)
{
gain=1;
if(volts[i]>2) gain=2;
gains[i] = gain-1;
values[i] = (unsigned)(0x1000 * volts[i]/2.048/gain);
}
if(eeprom) return(sequential_raw(1,gains,values));
else return(multiple_raw(4,(unsigned[]){1,2,3,4},1,gains,values));
}
/* write four values to DAC channels using external reference */
int multipleexternal(struct chip *tempchip, float rels[], bool eeprom)
{
unsigned i;
unsigned values[4];
curchip=tempchip;
if (curchip->bus == UNDEFINED) return(-1);
if (address_i2c() == -1) return(-2);
for (i = 0; i < 4; i++)
{
values[i]=(unsigned)(0x1000 * rels[i]);
}
if(eeprom) return(sequential_raw(0,(unsigned[]){0,0,0,0},values));
else return(multiple_raw(4,(unsigned[]){1,2,3,4},0,(unsigned[]){0,0,0,0},values));
}