Skip to content

Commit 7f152ff

Browse files
authored
Merge pull request #39 from sparkfun/feature/16-bit-data-support
Merge in latest changes into development. From the above discussion - only missing the naming pattern change
2 parents b6be185 + 8b20d91 commit 7f152ff

14 files changed

+484
-13
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
![SparkFun ToolKit](docs/images/gh-banner-2025-banner-toolkit.png "SparkFun Toolkit")
12
# SparkFun Toolkit Arduino Library
23

34
![Toolkit Tests Builds](https://github.com/sparkfun/SparkFun_Toolkit/actions/workflows/compile-sketch.yml/badge.svg)
265 KB
Loading

src/SparkFun_Toolkit.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3131
// Arduino Libraries.
3232

3333
// Just include the toolkit headers
34-
35-
#include <sfeTk/sfeToolkit.h>
3634
#include "sfeTkArdI2C.h"
37-
#include "sfeTkArdSPI.h"
35+
#include "sfeTkArdSPI.h"
36+
#include "sfeTkArduino.h"

src/sfeTk/sfeTkIBus.h

Lines changed: 121 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2727

2828
#pragma once
2929

30-
#include "sfeTkError.h"
30+
#include "sfeToolkit.h"
3131
#include <stddef.h>
3232

3333
/**
@@ -52,7 +52,7 @@ const sfeTkError_t kSTkErrBusTimeout = kSTkErrFail * (kSTkErrBaseBus + 2);
5252
const sfeTkError_t kSTkErrBusNoResponse = kSTkErrFail * (kSTkErrBaseBus + 3);
5353

5454
/**
55-
* @brief Returned when the data to be sent is too long or recieved is too short.
55+
* @brief Returned when the data to be sent is too long or received is too short.
5656
*/
5757
const sfeTkError_t kSTkErrBusDataTooLong = kSTkErrFail * (kSTkErrBaseBus + 4);
5858

@@ -86,6 +86,12 @@ const sfeTkError_t kSTkErrBusNotEnabled = kSTkErrBaseBus + 8;
8686
class sfeTkIBus
8787
{
8888
public:
89+
/**
90+
* @brief Constructor
91+
*/
92+
sfeTkIBus() {
93+
_byteOrder = sftk_system_byteorder();
94+
}
8995
/**--------------------------------------------------------------------------
9096
* @brief Send a single byte to the device*
9197
* @param data Data to write.
@@ -96,7 +102,7 @@ class sfeTkIBus
96102
virtual sfeTkError_t writeByte(uint8_t data) = 0;
97103

98104
/**--------------------------------------------------------------------------
99-
* @brief Send a word to the device.
105+
* @brief Send a word to the device.
100106
* @param data Data to write.
101107
*
102108
* @retval sfeTkError_t - kSTkErrOk on successful execution.
@@ -125,6 +131,12 @@ class sfeTkIBus
125131
*/
126132
virtual sfeTkError_t writeRegisterByte(uint8_t devReg, uint8_t data) = 0;
127133

134+
// Overload version
135+
sfeTkError_t writeRegister(uint8_t devReg, uint8_t data)
136+
{
137+
return writeRegisterByte(devReg, data);
138+
}
139+
128140
/**--------------------------------------------------------------------------
129141
* @brief Write a single word (16 bit) to the given register
130142
*
@@ -136,6 +148,12 @@ class sfeTkIBus
136148
*/
137149
virtual sfeTkError_t writeRegisterWord(uint8_t devReg, uint16_t data) = 0;
138150

151+
// Overload version
152+
sfeTkError_t writeRegister(uint8_t devReg, uint16_t data)
153+
{
154+
return writeRegisterWord(devReg, data);
155+
}
156+
139157
/**--------------------------------------------------------------------------
140158
* @brief Writes a number of bytes starting at the given register's address.
141159
*
@@ -148,6 +166,12 @@ class sfeTkIBus
148166
*/
149167
virtual sfeTkError_t writeRegisterRegion(uint8_t devReg, const uint8_t *data, size_t length) = 0;
150168

169+
// Overload version
170+
sfeTkError_t writeRegister(uint8_t devReg, const uint8_t *data, size_t length)
171+
{
172+
return writeRegisterRegion(devReg, data, length);
173+
}
174+
151175
/**--------------------------------------------------------------------------
152176
* @brief Writes a number of bytes starting at the given register's 16-bit address.
153177
*
@@ -160,6 +184,29 @@ class sfeTkIBus
160184
*/
161185
virtual sfeTkError_t writeRegister16Region(uint16_t devReg, const uint8_t *data, size_t length) = 0;
162186

187+
// Overload version
188+
sfeTkError_t writeRegister(uint16_t devReg, const uint8_t *data, size_t length)
189+
{
190+
return writeRegister16Region(devReg, data, length);
191+
}
192+
193+
/**--------------------------------------------------------------------------
194+
* @brief Writes a number of uint16's starting at the given register's 16-bit address.
195+
*
196+
* @param devReg The device's register's address.
197+
* @param data Data to write.
198+
* @param length - length of data
199+
*
200+
* @retval sfeTkError_t kSTkErrOk on successful execution
201+
*
202+
*/
203+
virtual sfeTkError_t writeRegister16Region16(uint16_t devReg, const uint16_t *data, size_t length) = 0;
204+
205+
// Overload version
206+
sfeTkError_t writeRegister(uint16_t devReg, const uint16_t *data, size_t length)
207+
{
208+
return writeRegister16Region16(devReg, data, length);
209+
}
163210
/**--------------------------------------------------------------------------
164211
* @brief Read a single byte from the given register
165212
*
@@ -171,6 +218,12 @@ class sfeTkIBus
171218
*/
172219
virtual sfeTkError_t readRegisterByte(uint8_t devReg, uint8_t &data) = 0;
173220

221+
// Overload version
222+
sfeTkError_t readRegister(uint8_t devReg, uint8_t &data)
223+
{
224+
return readRegisterByte(devReg, data);
225+
}
226+
174227
/**--------------------------------------------------------------------------
175228
* @brief Read a single word (16 bit) from the given register
176229
*
@@ -181,6 +234,12 @@ class sfeTkIBus
181234
*/
182235
virtual sfeTkError_t readRegisterWord(uint8_t devReg, uint16_t &data) = 0;
183236

237+
// Overload version
238+
sfeTkError_t readRegister(uint8_t devReg, uint16_t &data)
239+
{
240+
return readRegisterWord(devReg, data);
241+
}
242+
184243
/**--------------------------------------------------------------------------
185244
* @brief Reads a block of data from the given register.
186245
*
@@ -194,6 +253,12 @@ class sfeTkIBus
194253
*/
195254
virtual sfeTkError_t readRegisterRegion(uint8_t reg, uint8_t *data, size_t numBytes, size_t &readBytes) = 0;
196255

256+
// Overload version
257+
sfeTkError_t readRegister(uint8_t reg, uint8_t *data, size_t numBytes, size_t &readBytes)
258+
{
259+
return readRegisterRegion(reg, data, numBytes, readBytes);
260+
}
261+
197262
/**--------------------------------------------------------------------------
198263
* @brief Reads a block of data from the given 16-bit register address.
199264
*
@@ -206,6 +271,58 @@ class sfeTkIBus
206271
*
207272
*/
208273
virtual sfeTkError_t readRegister16Region(uint16_t reg, uint8_t *data, size_t numBytes, size_t &readBytes) = 0;
274+
275+
// Overload version
276+
sfeTkError_t readRegister(uint16_t reg, uint8_t *data, size_t numBytes, size_t &readBytes)
277+
{
278+
return readRegister16Region(reg, data, numBytes, readBytes);
279+
}
280+
/**--------------------------------------------------------------------------
281+
* @brief Reads a block of data from the given 16-bit register address.
282+
*
283+
* @param reg The device's 16 bit register's address.
284+
* @param data Data to write.
285+
* @param numBytes - length of data
286+
* @param[out] readBytes - number of bytes read
287+
*
288+
* @retval int returns kSTkErrOk on success, or kSTkErrFail code
289+
*
290+
*/
291+
virtual sfeTkError_t readRegister16Region16(uint16_t reg, uint16_t *data, size_t numBytes, size_t &readBytes) = 0;
292+
293+
// Overload version
294+
sfeTkError_t readRegister(uint16_t reg, uint16_t *data, size_t numBytes, size_t &readBytes)
295+
{
296+
return readRegister16Region16(reg, data, numBytes, readBytes);
297+
}
298+
299+
virtual uint8_t type(void)
300+
{
301+
return 0;
302+
}
303+
/**
304+
* @brief Set the byte order for multi-byte data transfers
305+
*
306+
* @param order The byte order to set - set to either SFTK_MSBFIRST or SFTK_LSBFIRST. The default is SFTK_LSBFIRST
307+
*
308+
*/
309+
void setByteOrder(sfeTKByteOrder order)
310+
{
311+
_byteOrder = order;
312+
}
313+
314+
/**
315+
* @brief Get the current byte order
316+
*
317+
* @retval The current byte order
318+
*/
319+
sfeTKByteOrder byteOrder(void)
320+
{
321+
return _byteOrder;
322+
}
323+
324+
protected:
325+
/** flag to manage byte swapping */
326+
sfeTKByteOrder _byteOrder;
209327
};
210328

211-
//};

src/sfeTk/sfeTkII2C.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3434
* The I2C bus interface extends the IBus interface and adds the ability to set and get the I2C
3535
* address and stop flag.
3636
*/
37+
const uint8_t kBusTypeI2C = 0x01;
38+
3739
class sfeTkII2C : public sfeTkIBus
3840
{
3941
public:
@@ -109,6 +111,11 @@ class sfeTkII2C : public sfeTkIBus
109111
*/
110112
static constexpr uint8_t kNoAddress = 0;
111113

114+
virtual uint8_t type(void)
115+
{
116+
return kBusTypeI2C;
117+
}
118+
112119
private:
113120
uint8_t _address;
114121
bool _stop;

src/sfeTk/sfeTkISPI.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3434
*
3535
* The SPI bus interface extends the IBus interface and adds the ability to set and get the CS pin.
3636
*/
37+
38+
const uint8_t kBusTypeSPI = 0x02;
3739
class sfeTkISPI : public sfeTkIBus
3840
{
3941
public:
@@ -82,6 +84,11 @@ class sfeTkISPI : public sfeTkIBus
8284
*/
8385
static constexpr uint8_t kNoCSPin = 0;
8486

87+
virtual uint8_t type(void)
88+
{
89+
return kBusTypeSPI;
90+
}
91+
8592
private:
8693
/** The internal storage of the _cs value*/
8794
uint8_t _cs;

src/sfeTk/sfeToolkit.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// File: sfeToolkit.cpp
2+
//
3+
// General impl file for the SparkFun Toolkit
4+
/*
5+
6+
The MIT License (MIT)
7+
8+
Copyright (c) 2024 SparkFun Electronics
9+
10+
Permission is hereby granted, free of charge, to any person obtaining a
11+
copy of this software and associated documentation files (the "Software"),
12+
to deal in the Software without restriction, including without limitation
13+
the rights to use, copy, modify, merge, publish, distribute, sublicense,
14+
and/or sell copies of the Software, and to permit persons to whom the
15+
Software is furnished to do so, subject to the following conditions: The
16+
above copyright notice and this permission notice shall be included in all
17+
copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED
18+
"AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
19+
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
20+
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22+
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24+
*/
25+
#include "sfeToolkit.h"
26+
27+
// THIS IS A PLACEHOLDER FILE for now
28+
#ifdef ARDUINO
29+
#include <Arduino.h>
30+
#endif
31+
//---------------------------------------------------------------------------------
32+
/**
33+
* @brief C function - Runtime check for system byte order
34+
*/
35+
sfeTKByteOrder sftk_system_byteorder(void)
36+
{
37+
uint16_t i = 1;
38+
return *((uint8_t *)&i) == 0 ? sfeTKByteOrder::BigEndian : sfeTKByteOrder::LittleEndian;
39+
}
40+
41+
//---------------------------------------------------------------------------------
42+
/**
43+
* @brief to catch 8 bit calls for byte swap
44+
*
45+
*/
46+
uint8_t sftk_byte_swap(uint8_t i)
47+
{
48+
return i;
49+
}
50+
//---------------------------------------------------------------------------------
51+
/**
52+
* @brief function - Byte swap a 16 bit value
53+
*/
54+
uint16_t sftk_byte_swap(uint16_t i)
55+
{
56+
// Use the fast intrinsic if available
57+
#if defined(__clang__) || defined(__GNUC__)
58+
return __builtin_bswap16(i);
59+
#else
60+
return (i << 8) | (i >> 8);
61+
#endif
62+
}
63+
64+
//---------------------------------------------------------------------------------
65+
/**
66+
* @brief function - Byte swap a 32 bit value
67+
*/
68+
uint32_t sftk_byte_swap(uint32_t i)
69+
{
70+
#if defined(__clang__) || defined(__GNUC__)
71+
return __builtin_bswap32(i);
72+
#else
73+
return ((i << 24) & 0xff000000) | ((i << 8) & 0x00ff0000) | ((i >> 8) & 0x0000ff00) | ((i >> 24) & 0x000000ff);
74+
#endif
75+
}

src/sfeTk/sfeToolkit.h

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
77
The MIT License (MIT)
88
9-
Copyright (c) 2023 SparkFun Electronics
9+
Copyright (c) 2024 SparkFun Electronics
1010
1111
Permission is hereby granted, free of charge, to any person obtaining a
1212
copy of this software and associated documentation files (the "Software"),
@@ -26,7 +26,36 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2626

2727
#pragma once
2828

29+
#include <stdint.h>
30+
2931
/**
3032
@brief Common include file for the core of the SparkFun Electronics Toolkit
3133
*/
3234
#include "sfeTkError.h"
35+
36+
37+
// byte order types/enum
38+
enum class sfeTKByteOrder : uint8_t
39+
{
40+
BigEndian = 0x01,
41+
LittleEndian = 0x02
42+
};
43+
44+
// Note - toolkit *functions* start with sftk_ to avoid name collisions
45+
46+
// Function to determine the byte order of the system
47+
sfeTKByteOrder sftk_system_byteorder(void);
48+
49+
uint8_t sftk_byte_swap(uint8_t i);
50+
uint16_t sftk_byte_swap(uint16_t i);
51+
uint32_t sftk_byte_swap(uint32_t i);
52+
53+
54+
// Area for platform specific implementations. The interface/functions are
55+
// defined here, with the expectation that the platform provides the implementation.
56+
57+
// delay in milliseconds
58+
void sftk_delay_ms(uint32_t ms);
59+
60+
// ticks in milliseconds
61+
uint32_t sftk_ticks_ms(void);

0 commit comments

Comments
 (0)