-
Notifications
You must be signed in to change notification settings - Fork 0
/
I2C.c
78 lines (56 loc) · 1.47 KB
/
I2C.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
#include<stdio.h>
#include<regex.h>
#include<fcntl.h>
#include<unistd.h>
#include<bcm2835.h>
#include"I2C.h"
#define BUFFERSIZE 4096
extern int write8(unsigned char reg, unsigned char value){
char buff[2];
buff[0] = reg;
buff[1] = value;
uint8_t result;
result = bcm2835_i2c_write(buff, 2);
return result;
}
extern unsigned int read8(unsigned char reg){
unsigned char buff[2];
buff[0] = reg;
uint8_t code = bcm2835_i2c_read(buff,2);
return buff[0];
}
// void write8(unsigned char reg, unsigned char value){
// char buff[2];
// buff[0] = reg;
// buff[1] = value;
// uint8_t result;
// result = bcm2835_i2c_write(buff,2);
// }
/*
def write8(self, reg, value):
"Writes an 8-bit value to the specified register/address"
try:
self.bus.write_byte_data(self.address, reg, value)
if self.debug:
print "I2C: Wrote 0x%02X to register 0x%02X" % (value, reg)
except IOError, err:
return self.errMsg()
*/
extern int i2cInit(int address){
Address *theAddress;
theAddress = (Address*)malloc(sizeof(Address));
theAddress->address = address;
//i2c initialization should also go here.
if (!bcm2835_init())
{
printf("bcm2835_init failed. Are you running as root??\n");
return 1;
}//if bcm_init
if (!bcm2835_i2c_begin())
{
printf("bcm2835_i2c_begin failed. Are you running as root??\n");
return 1;
}//if i2c_begin
bcm2835_i2c_setSlaveAddress(address); //set HAT address to 0x41
return 0;
}