forked from marko-pi/MCP4728
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MCP4728.c
586 lines (505 loc) · 15.4 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
/*
Marko Pinteric 2020
GPIO communication based on Tiny GPIO Access on http://abyz.me.uk/rpi/pigpio/examples.html
Header for MCP4728.c
*/
#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>
#include "MCP4728.h"
/* GPIO address */
static volatile uint32_t *gpioReg = MAP_FAILED;
/* Current Chip Data */
struct chip *curchip;
/* struct chip mychipdata;
struct chip *mychip = &mychipdata; */
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 GPIO METHODS */
/* all assume && leave SCL low, except when specified differently */
/* starts 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);
}
/* stops 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);
}
}
void clock_wait()
{
struct timespec ctime;
uint64_t ntime;
ntime = ttime.tv_sec * (uint64_t)1000000000L + ttime.tv_nsec + 5000;
while(1)
{
clock_gettime(CLOCK_MONOTONIC,&ctime);
if (ctime.tv_sec * (uint64_t)1000000000L + ctime.tv_nsec >= ntime) return;
}
}
/* following method assumes SCL && SDA high */
void i2cstart()
{
gpioSetMode(curchip->sda, PI_OUTPUT);
clock_gettime(CLOCK_MONOTONIC,&ttime);
clock_wait();
gpioSetMode(curchip->scl, PI_OUTPUT);
clock_gettime(CLOCK_MONOTONIC,&ttime);
clock_wait();
clock_gettime(CLOCK_MONOTONIC,&ttime);
}
void i2crestart()
{
gpioSetMode(curchip->sda, PI_INPUT);
clock_gettime(CLOCK_MONOTONIC,&ttime);
clock_wait();
gpioSetMode(curchip->scl, PI_INPUT);
clock_gettime(CLOCK_MONOTONIC,&ttime);
clock_wait();
gpioSetMode(curchip->sda, PI_OUTPUT);
clock_gettime(CLOCK_MONOTONIC,&ttime);
clock_wait();
gpioSetMode(curchip->scl, PI_OUTPUT);
clock_gettime(CLOCK_MONOTONIC,&ttime);
clock_wait();
clock_gettime(CLOCK_MONOTONIC,&ttime);
}
/* following methods leave SCL && SDA high */
void i2cstop()
{
gpioSetMode(curchip->sda, PI_OUTPUT);
clock_gettime(CLOCK_MONOTONIC,&ttime);
clock_wait();
gpioSetMode(curchip->scl, PI_INPUT);
clock_gettime(CLOCK_MONOTONIC,&ttime);
clock_wait();
gpioSetMode(curchip->sda, PI_INPUT);
clock_gettime(CLOCK_MONOTONIC,&ttime);
clock_wait();
clock_gettime(CLOCK_MONOTONIC,&ttime);
}
unsigned i2cgetbyte()
{
unsigned data=0x00;
unsigned i;
gpioSetMode(curchip->sda, PI_INPUT);
clock_gettime(CLOCK_MONOTONIC,&ttime);
for (i=1; i<=8; i++)
{
/* slave sends bit */
clock_wait();
gpioSetMode(curchip->scl, PI_INPUT);
clock_gettime(CLOCK_MONOTONIC,&ttime);
data = data << 1;
clock_wait();
/* I2C clock stretching */
while(gpioRead(curchip->scl) == 0) ;
if (gpioRead(curchip->sda) == 1) data = data | 0x01;
gpioSetMode(curchip->scl, PI_OUTPUT);
clock_gettime(CLOCK_MONOTONIC,&ttime);
}
return(data);
}
void i2csendbyte(unsigned data)
{
unsigned i;
for (i=1; i<=8; i++)
{
if ((data & 0x80) == 0) gpioSetMode(curchip->sda, PI_OUTPUT);
else gpioSetMode(curchip->sda, PI_INPUT);
clock_gettime(CLOCK_MONOTONIC,&ttime);
data = data << 1;
clock_wait();
gpioSetMode(curchip->scl, PI_INPUT);
clock_gettime(CLOCK_MONOTONIC,&ttime);
clock_wait();
gpioSetMode(curchip->scl, PI_OUTPUT);
}
clock_gettime(CLOCK_MONOTONIC,&ttime);
}
unsigned i2cgetack()
{
unsigned result;
gpioSetMode(curchip->sda, PI_INPUT);
/* slave sends bit */
clock_gettime(CLOCK_MONOTONIC,&ttime);
clock_wait();
gpioSetMode(curchip->scl, PI_INPUT);
clock_gettime(CLOCK_MONOTONIC,&ttime);
clock_wait();
if (gpioRead(curchip->sda) == 0) result=1;
else result=0;
/* read SDA */
gpioSetMode(curchip->scl, PI_OUTPUT);
clock_gettime(CLOCK_MONOTONIC,&ttime);
return(result);
}
void i2csendack()
{
gpioSetMode(curchip->sda, PI_OUTPUT);
clock_gettime(CLOCK_MONOTONIC,&ttime);
clock_wait();
gpioSetMode(curchip->scl, PI_INPUT);
clock_gettime(CLOCK_MONOTONIC,&ttime);
clock_wait();
gpioSetMode(curchip->scl, PI_OUTPUT);
}
void i2csendnack()
{
gpioSetMode(curchip->sda, PI_INPUT);
clock_gettime(CLOCK_MONOTONIC,&ttime);
clock_wait();
gpioSetMode(curchip->scl, PI_INPUT);
clock_gettime(CLOCK_MONOTONIC,&ttime);
clock_wait();
gpioSetMode(curchip->scl, PI_OUTPUT);
}
/* LOCAL I2C METHODS */
/* sets 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);
}
/* writes 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);
}
/* writes 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);
}
/* writes 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 */
/* initialises communications */
struct chip *mcp4728_initialize(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 mcp4728_deinitialize(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 */
/* gets the DAC address */
int mcp4728_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);
clock_gettime(CLOCK_MONOTONIC,&ttime);
start_gpio();
i2cstart();
i2csendbyte(0x00);
errs[0]=i2cgetack();
i2csendbyte(0x0C);
gpioSetMode(curchip->ldac, PI_OUTPUT);
errs[1]=i2cgetack();
i2crestart();
i2csendbyte(0xC1);
gpioSetMode(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] = 0;
else errs[3] = 1;
for (i=0; i<=3; i++)
{
if (errs[i]==0) err = err | (0x04 >> 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);
}
/* sets the DAC address */
int mcp4728_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 | (0x04 >> i);
}
if (err>0) ret=-(int)err;
else
{
curchip->address = addr;
ret = 0;
}
stop_gpio();
return(ret);
}
/* GLOBAL I2C METHODS */
/* writes single value to the selected DAC channel using internal reference - channels 1 to 4 */
int mcp4728_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}));
}
/* writes single value to the selected DAC channel using external reference - channels 1 to 4 */
int mcp4728_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}));
}
/* writes four values to the DAC channels using internal reference */
int mcp4728_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));
}
/* writes four values to DAC channels using external reference */
int mcp4728_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));
}