-
Notifications
You must be signed in to change notification settings - Fork 27
/
I2C.h
206 lines (184 loc) · 7.75 KB
/
I2C.h
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
/*
The MIT License (MIT)
Copyright (c) 2017 Lancaster University.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#ifndef CODAL_I2C_H
#define CODAL_I2C_H
#include "CodalConfig.h"
#include "ErrorNo.h"
#include "Pin.h"
namespace codal
{
/**
* Class definition for an I2C interface.
*/
enum AcknowledgeType {ACK, NACK};
class I2C : public PinPeripheral
{
public:
I2C(Pin &sda, Pin &scl);
/** Set the frequency of the I2C interface
*
* @param frequency The bus frequency in hertz
*/
virtual int setFrequency(uint32_t frequency);
protected:
/**
* Issues a START condition on the I2C bus
* @return DEVICE_OK on success, or an error code
*/
virtual int start();
/**
* Issues a STOP condition on the I2C bus
* @return DEVICE_OK on success, or an error code
*/
virtual int stop();
/**
* Writes the given byte to the I2C bus.
*
* The CPU will busy wait until the transmission is complete.
*
* @param data The byte to write.
* @return DEVICE_OK on success, DEVICE_I2C_ERROR if the the write request failed.
*/
virtual int write(uint8_t data);
/**
* Reads a single byte from the I2C bus.
* The CPU will busy wait until the transmission is complete.
*
* @return the byte read from the I2C bus, or DEVICE_I2C_ERROR if the the write request failed.
*/
virtual int read(AcknowledgeType ack = ACK);
public:
/**
* Change the pins used by this I2C peripheral to those provided.
*
* @param sda the Pin to use for the I2C SDA line.
* @param scl the Pin to use for the I2C SCL line.
* @return DEVICE_OK on success, or DEVICE_NOT_IMPLEMENTED / DEVICE_NOT_SUPPORTED if the request cannot be performed.
*/
virtual int redirect(Pin &sda, Pin &scl);
/**
* Issues a standard, 2 byte I2C command write to the I2C bus.
* This consists of:
* - Asserting a Start condition on the bus
* - Selecting the Slave address (as an 8 bit address)
* - Writing the raw 8 bit data provided
* - Asserting a Stop condition on the bus
*
* The CPU will busy wait until the transmission is complete.
*
* @param address The 8bit I2C address of the device to write to
* @param data the byte command to write
*
* @return DEVICE_OK on success, DEVICE_I2C_ERROR if the the write request failed.
*/
virtual int write(uint16_t address, uint8_t data);
/**
* Issues a standard, I2C command write to the I2C bus.
* This consists of:
* - Asserting a Start condition on the bus
* - Selecting the Slave address (as an 8 bit address)
* - Writing a number of raw data bytes provided
* - Asserting a Stop condition on the bus
*
* The CPU will busy wait until the transmission is complete.
*
* @param address The 8bit I2C address of the device to write to
* @param data pointer to the bytes to write
* @param len the number of bytes to write
* @param repeated Suppresses the generation of a STOP condition if set. Default: false;
*
* @return DEVICE_OK on success, DEVICE_I2C_ERROR if the the write request failed.
*/
virtual int write(uint16_t address, uint8_t *data, int len, bool repeated = false);
int write(int address, char *data, int len, bool repeated = false);
/**
* Performs a typical register write operation to the I2C slave device provided.
* This consists of:
* - Asserting a Start condition on the bus
* - Selecting the Slave address (as an 8 bit address)
* - Writing the 8 bit register address provided
* - Writing the 8 bit value provided
* - Asserting a Stop condition on the bus
*
* The CPU will busy wait until the transmission is complete..
*
* @param address 8bit address of the device to write to
* @param reg The 8bit address of the register to write to.
* @param value The value to write.
*
* @return DEVICE_OK on success, DEVICE_I2C_ERROR if the the write request failed.
*/
virtual int writeRegister(uint16_t address, uint8_t reg, uint8_t value);
/**
* Issues a standard, 2 byte I2C command read to the I2C bus.
* This consists of:
* - Asserting a Start condition on the bus
* - Selecting the Slave address (as an 8 bit address)
* - reading "len" bytes of raw 8 bit data into the buffer provided
* - Asserting a Stop condition on the bus
*
* The CPU will busy wait until the transmission is complete.
*
* @param address The 8bit I2C address of the device to read from
* @param data pointer to store the the bytes read
* @param len the number of bytes to read into the buffer
* @param repeated Suppresses the generation of a STOP condition if set. Default: false;
*
* @return DEVICE_OK on success, DEVICE_I2C_ERROR if the the read request failed.
*/
virtual int read(uint16_t address, uint8_t *data, int len, bool repeated = false);
int read(int address, char *data, int len, bool repeated = false);
/**
* Performs a typical register read operation to the I2C slave device provided.
* This consists of:
* - Asserting a Start condition on the bus
* - Selecting the Slave address (as an 8 bit address, I2C WRITE)
* - Selecting a RAM register address in the slave
* - Asserting a Stop condition on the bus
* - Asserting a Start condition on the bus
* - Selecting the Slave address (as an 8 bit address, I2C READ)
* - Performing an 8 bit read operation (of the requested register)
* - Asserting a Stop condition on the bus
*
* The CPU will busy wait until the transmission is complete..
*
* @param address 8bit I2C address of the device to read from
* @param reg The 8bit register address of the to read.
* @param data A pointer to a memory location to store the result of the read operation
* @param length The number of mytes to read
* @param repeated Use a repeated START/START/STOP transaction if true, or independent START/STOP/START/STOP transactions if fasle. Default: true
*
* @return DEVICE_OK or DEVICE_I2C_ERROR if the the read request failed.
*/
virtual int readRegister(uint16_t address, uint8_t reg, uint8_t *data, int length, bool repeated = true);
/**
* Issues a single byte read command, and returns the value read, or an error.
*
* Blocks the calling thread until complete.
*
* @param address The address of the I2C device to write to.
* @param reg The address of the register to access.
*
* @return the byte read on success, DEVICE_INVALID_PARAMETER or DEVICE_I2C_ERROR if the the read request failed.
*/
virtual int readRegister(uint8_t address, uint8_t reg);
};
}
#endif