-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsfe_bus.cpp
365 lines (289 loc) · 10.4 KB
/
sfe_bus.cpp
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
// sfe_bus.cpp
//
// This is a library written for SparkFun Qwiic KX132 and KX134 boards
//
// SparkFun sells these bpards at its website: www.sparkfun.com
//
// Do you like this library? Help support SparkFun. Buy a board!
//
// SparkFun Triple Axis Accelerometer - KX132/KX134 (Qwiic)
// * KX132 - https://www.sparkfun.com/products/17871
// * KX134 - https://www.sparkfun.com/products/17589
//
// Written by Kirk Benell @ SparkFun Electronics
// Modified by Elias Santistevan @ SparkFun Electronics, September 2022
//
// Repository:
// https://github.com/sparkfun/SparkFun_KX13X_Arduino_Library
//
//
// SparkFun code, firmware, and software is released under the MIT
// License(http://opensource.org/licenses/MIT).
//
// SPDX-License-Identifier: MIT
//
// The MIT License (MIT)
//
// Copyright (c) 2022 SparkFun Electronics
// 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.
// The following classes specify the behavior for communicating
// over the respective data buses: Inter-Integrated Circuit (I2C)
// and Serial Peripheral Interface (SPI).
#include "sfe_bus.h"
#include <Arduino.h>
namespace sfe_KX13X
{
#define kMaxTransferBuffer 32
#define SPI_READ 0x80
// What we use for transfer chunk size
const static uint16_t kChunkSize = kMaxTransferBuffer;
//////////////////////////////////////////////////////////////////////////////////////////////////
// Constructor
//
QwI2C::QwI2C(void) : _i2cPort{nullptr}
{
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// I2C init()
//
// Methods to init/setup this device. The caller can provide a Wire Port, or this class
// will use the default
bool QwI2C::init(TwoWire &wirePort, bool bInit)
{
// if we don't have a wire port already
if (!_i2cPort)
{
_i2cPort = &wirePort;
if (bInit)
_i2cPort->begin();
}
return true;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// I2C init()
//
// Methods to init/setup this device. The caller can provide a Wire Port, or this class
// will use the default
bool QwI2C::init()
{
if (!_i2cPort)
return init(Wire);
else
return false;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// ping()
//
// Is a device connected?
bool QwI2C::ping(uint8_t i2c_address)
{
if (!_i2cPort)
return false;
_i2cPort->beginTransmission(i2c_address);
return _i2cPort->endTransmission() == 0;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// writeRegisterByte()
//
// Write a byte to a register
bool QwI2C::writeRegisterByte(uint8_t i2c_address, uint8_t offset, uint8_t dataToWrite)
{
if (!_i2cPort)
return -1;
_i2cPort->beginTransmission(i2c_address);
_i2cPort->write(offset);
_i2cPort->write(dataToWrite);
return (_i2cPort->endTransmission() == 0); // true = success, false = error
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// writeRegisterRegion()
//
// Write a block of data to a device.
int QwI2C::writeRegisterRegion(uint8_t i2c_address, uint8_t offset, const uint8_t *data, uint16_t length)
{
if (!_i2cPort)
return -1;
_i2cPort->beginTransmission(i2c_address);
_i2cPort->write(offset);
_i2cPort->write(data, (int)length);
return _i2cPort->endTransmission() == 0 ? 0 : -1; // 0 = success, -1 = error
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
// readRegisterRegion()
//
// Reads a block of data from an i2c register on the devices.
//
// For large buffers, the data is chuncked over KMaxI2CBufferLength at a time
//
//
int QwI2C::readRegisterRegion(uint8_t addr, uint8_t reg, uint8_t *data, uint16_t numBytes)
{
uint8_t nChunk;
uint16_t nReturned;
if (!_i2cPort)
return -1;
int i; // counter in loop
int failCount = 0; // Keep track of how many times nReturned is != nChunk
while ((numBytes > 0) && (failCount < 2)) // Give up after 2 bad requests
{
_i2cPort->beginTransmission(addr);
_i2cPort->write(reg); // Write the register address we want to read from
if (_i2cPort->endTransmission() != 0)
return -1; // Fail immediately if the transmission isn't successful
// We're chunking in data - keeping the max chunk to kMaxI2CBufferLength
nChunk = numBytes > kChunkSize ? kChunkSize : numBytes;
nReturned = _i2cPort->requestFrom((int)addr, (int)nChunk, (int)true); // Always send a stop
// No data returned, no dice
if (nReturned == 0)
return -1; // error
// Check we got back as much data as was requested.
// (Fringe case. This should never happen... But, you know, it _could_...)
if (nReturned != nChunk)
failCount++; // Increment the failCount
// Copy the retrieved data chunk to the current index in the data segment
for (i = 0; i < nReturned; i++)
{
*data++ = _i2cPort->read();
}
// Decrement the amount of data recieved from the overall data request amount
numBytes = numBytes - nReturned;
// Increment reg by the same ammount
reg += nReturned;
} // end while
return (numBytes == 0 ? 0 : -1); // 0 = success (all bytes read), -1 = error
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// Constructor
//
SfeSPI::SfeSPI(void) : _spiPort{nullptr}
{
}
////////////////////////////////////////////////////////////////////////////////////////////////
// SPI init()
//
// Methods to init/setup this device. The caller can provide a SPI Port, or this class
// will use the default
bool SfeSPI::init(SPIClass &spiPort, SPISettings &kxSettings, uint8_t cs, bool bInit)
{
// if we don't have a SPI port already
if (!_spiPort)
{
_spiPort = &spiPort;
if (bInit)
_spiPort->begin();
}
// SPI settings are needed for every transaction
_sfeSPISettings = kxSettings;
// The chip select pin can vary from platform to platform and project to project
// and so it must be given by the user.
if (!cs)
return false;
_cs = cs;
return true;
}
////////////////////////////////////////////////////////////////////////////////////////////////
// SPI init()
//
// Methods to init/setup this device. The caller can provide a SPI Port, or this class
// will use the default
bool SfeSPI::init(uint8_t cs, bool bInit)
{
// If the transaction settings are not provided by the user they are built here.
SPISettings spiSettings = SPISettings(2000000, MSBFIRST, SPI_MODE0);
// In addition of the port is not provided by the user, it defaults to SPI here.
return init(SPI, spiSettings, cs, bInit);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// ping()
//
// Is a device connected? The SPI ping is not relevant but is defined here to keep consistency with
// I2C class i.e. provided for the interface.
//
bool SfeSPI::ping(uint8_t i2c_address)
{
return true;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// writeRegisterByte()
//
// Write a byte to a register
bool SfeSPI::writeRegisterByte(uint8_t i2c_address, uint8_t offset, uint8_t dataToWrite)
{
if (!_spiPort)
return false;
// Apply settings
_spiPort->beginTransaction(_sfeSPISettings);
// Signal communication start
digitalWrite(_cs, LOW);
_spiPort->transfer(offset);
_spiPort->transfer(dataToWrite);
// End communcation
digitalWrite(_cs, HIGH);
_spiPort->endTransaction();
return true;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// writeRegisterRegion()
//
// Write a block of data to a device.
int SfeSPI::writeRegisterRegion(uint8_t i2c_address, uint8_t offset, const uint8_t *data, uint16_t length)
{
if (!_spiPort)
return -1;
int i;
// Apply settings
_spiPort->beginTransaction(_sfeSPISettings);
// Signal communication start
digitalWrite(_cs, LOW);
_spiPort->transfer(offset);
for (i = 0; i < length; i++)
{
_spiPort->transfer(*data++);
}
// End communication
digitalWrite(_cs, HIGH);
_spiPort->endTransaction();
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
// readRegisterRegion()
//
// Reads a block of data from the register on the device.
//
//
//
int SfeSPI::readRegisterRegion(uint8_t addr, uint8_t reg, uint8_t *data, uint16_t numBytes)
{
if (!_spiPort)
return -1;
int i; // counter in loop
// Apply settings
_spiPort->beginTransaction(_sfeSPISettings);
// Signal communication start
digitalWrite(_cs, LOW);
// A leading "1" must be added to transfer with register to indicate a "read"
reg = (reg | SPI_READ);
_spiPort->transfer(reg);
for (i = 0; i < numBytes; i++)
{
*data++ = _spiPort->transfer(0x00);
}
// End transaction
digitalWrite(_cs, HIGH);
_spiPort->endTransaction();
return 0;
}
}