-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathi2c.c
176 lines (164 loc) · 4.48 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
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
//=========================================================================
// i2c.c
//
// An interface for the RM3100 3-axis magnetometer from PNI Sensor Corp.
// Derived in part from several sources:
// https://github.com/miguelrasteiro/RM3100.X
// Jeremiah Mattison / Rm3100: https://os.mbed.com/users/fwrawx/code/Rm3100/
// https://github.com/shannon-jia/rm3100
// Song Qiang <[email protected] (Linux driver):
// Linux kernel driver: https://github.com/torvalds/linux/tree/v5.3/drivers/iio/magnetometer
//
// Author: David Witten, KD0EAG
// Date: April 21, 2020
// License: GPL 3.0
//=========================================================================
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include "device_defs.h"
#include "i2c.h"
#include "main.h"
//------------------------------------------
// i2c_setAddress()
//
// set the I2C slave address for all
// subsequent I2C device transfers.
//------------------------------------------
void i2c_setAddress(int fd, int devAddr)
{
if (ioctl(fd, I2C_SLAVE, devAddr) < 0)
{
perror("i2c_SetAddress");
exit(1);
}
}
//
//No obvious API call available
////------------------------------------------
//// i2c_setBitRate()
////
//// set the I2C bus address for all
//// subsequent I2C device transfers.
////------------------------------------------
//void i2c_setBitRate(int fd, int devspeed)
//{
// if(ioctl(fd, I2C_SET_SPEED, devspeed) < 0)
// {
// perror("i2c_setBitRate");
// exit(1);
// }
//}
//------------------------------------------
// write an 8 bit value to a device register.
//------------------------------------------
int i2c_write(int fd, uint8_t reg, uint8_t value)
{
static uint8_t data[2];
data[0] = reg;
data[1] = value & 0xff;
int rv = 0;
if(write(fd, data, 2) != 2)
{
perror("i2c_write()");
}
return rv;
}
//------------------------------------------
// i2c_writebuf()
// write a buffer of values to the device
//------------------------------------------
int i2c_writebuf(int fd, uint8_t reg, char *buffer, short int length)
{
static uint8_t data[2];
data[0] = reg;
int rv = 0;
if((rv = write(fd, data, 2)) != 2)
{
perror("i2c_writebuf(): write()");
}
if((rv = write(fd, buffer, length)) != length)
{
perror("i2c_writebuf(): write(data)");
exit(1);
}
return rv;
}
//------------------------------------------
// read an 8 bit value from a register.
//------------------------------------------
uint8_t i2c_read(int fd, uint8_t reg)
{
static uint8_t data[2];
data[0] = reg;
int rv = 0;
if((rv = write(fd, data, 1)) != 1)
{
perror("i2c_writebuf(): write()");
}
if(read(fd, data + 1, 1) != 1)
{
perror("i2c_read read value.");
}
return data[1];
}
//------------------------------------------
// i2c_readbuf()
// write a buffer to the device
//------------------------------------------
int i2c_readbuf(int fd, uint8_t reg, uint8_t* buf, short int length)
{
int bytes_read;
static uint8_t data[2];
data[0] = reg;
int rv = 0;
if((rv = write(fd, data, 1)) != 1)
{
perror("i2c_readbuf(): write()");
}
if((bytes_read = read(fd, buf, length)) != length)
{
perror("i2c transaction i2c_readbuf() failed.\n");
}
return bytes_read;
}
///**
// * @fn SensorStatus mag_enable_interrupts();
// *
// * @brief Enables the interrupt request from the sensor.
// *
// * @returns Status of the sensor. Not supported in AKM8975
// */
//SensorStatus mag_enable_interrupts()
//{
// static char data[] = { RM3100_ENABLED };
//
// if (mSensorMode == SensorPowerModeActive)
// {
// rm3100_i2c_write(RM3100_BEACON_REG, data, sizeof(data)/sizeof(char));
// }
// return SensorOK;
//}
//
///**
// * @fn SensorStatus mag_disable_interrupts();
// *
// * @brief Disables the interrupt request from the sensor.
// *
// * @returns Status of the sensor.
// */
//SensorStatus mag_disable_interrupts()
//{
// static char data[] = { RM3100_DISABLED };
// rm3100_i2c_write(RM3100_BEACON_REG, data, sizeof(data)/sizeof(char));
// return SensorOK;
//}
//
///**
// * @fn unsigned short int mag_set_sample_rate(unsigned short int sample_rate);
// *
// * @brief Requests the hardware to perform sample conversions at the specified rate.
// *
// * @param sample_rate The requested sample rate of the sensor in Hz.
// *
// * @returns The actual sample rate of the sensor.
// */