Skip to content

Commit b6be185

Browse files
authored
Merge pull request #38 from sparkfun/main
Bring develop up to main
2 parents 45f4c65 + 97a2ab1 commit b6be185

File tree

8 files changed

+188
-44
lines changed

8 files changed

+188
-44
lines changed

docs/ar_ibus.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ The general pattern for a device driver implementation that uses the SparkFun To
131131

132132
The first step is to implement a core, platform independent version of the driver that communicates to the target device using the methods of a ```sfeTkIBus``` interface. By limiting use to the IBus interface, the core implementation can use any bus type or platform that implements the sfeTkIBus interface.
133133

134-
>[!IMPORTANT]
134+
> [!IMPORTANT]
135135
> At this level, the driver is only using a ```sfeTkIBus``` interface, not any specific bus implementation.
136136
137137
This driver has the following unique functionality:
@@ -141,12 +141,12 @@ This driver has the following unique functionality:
141141

142142
#### Simple Example of an Independent Driver Implementation
143143

144-
>[!NOTE]
144+
> [!NOTE]
145145
> This code is **pseudo-code**, used to demonstrate the key concepts of the implementation pattern.
146146
147147
This implementation would take the following form:
148148

149-
```c++
149+
```cpp
150150

151151
class myDriverClass
152152
{
@@ -205,7 +205,7 @@ The following is an example of an I2C class in Arduino based on the previous pla
205205
> [!NOTE]
206206
> If your device supports repeated starts, make sure to include ```_theI2CBus.setStop(false)``` in your begin function. Otherwise this can cause issues with your device.
207207
208-
```c++
208+
```cpp
209209
210210
class myArduinoDriverI2C : public myDriverClass
211211
{
@@ -246,7 +246,7 @@ The following is a SPI version of the driver implemented in Arduino. While simil
246246
> [!NOTE]
247247
> This class implements a ```isConnected()``` method that just calls the superclasses ```checkDeviceID()``` method to determine if the device is available on the bus.
248248
249-
```c++
249+
```cpp
250250

251251
class myArduinoDriveSPI : public myDriverClass
252252
{

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=SparkFun Toolkit
2-
version=0.9.1
2+
version=0.9.2
33
author=SparkFun Electronics
44
maintainer=SparkFun Electronics
55
sentence=A utility library that other SparkFun libraries can take advantage of.

src/sfeTk/sfeTkError.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3232
* General Concept
3333
* A SparkFun Toolkit error system. The goal is to keep this simple.
3434
*
35-
* This mimics a vareity of systems, using an int type for error codes,
35+
* This mimics a variety of systems, using an int type for error codes,
3636
* where:
3737
* 0 = okay
3838
* -1 = general failure

src/sfeTk/sfeTkIBus.h

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,33 @@ class sfeTkIBus
8787
{
8888
public:
8989
/**--------------------------------------------------------------------------
90-
* @brief Write a single byte to the device*
91-
* @param data Data to write.*
90+
* @brief Send a single byte to the device*
91+
* @param data Data to write.
9292
*
9393
* @retval sfeTkError_t - kSTkErrOk on successful execution.
9494
*
9595
*/
9696
virtual sfeTkError_t writeByte(uint8_t data) = 0;
9797

98+
/**--------------------------------------------------------------------------
99+
* @brief Send a word to the device.
100+
* @param data Data to write.
101+
*
102+
* @retval sfeTkError_t - kSTkErrOk on successful execution.
103+
*
104+
*/
105+
virtual sfeTkError_t writeWord(uint16_t data) = 0;
106+
107+
/**--------------------------------------------------------------------------
108+
* @brief Send an array of data to the device.
109+
* @param data Data to write.
110+
* @param length - length of data.
111+
*
112+
* @retval sfeTkError_t - kSTkErrOk on successful execution.
113+
*
114+
*/
115+
virtual sfeTkError_t writeRegion(const uint8_t *data, size_t length) = 0;
116+
98117
/**--------------------------------------------------------------------------
99118
* @brief Write a single byte to the given register
100119
*
@@ -120,9 +139,9 @@ class sfeTkIBus
120139
/**--------------------------------------------------------------------------
121140
* @brief Writes a number of bytes starting at the given register's address.
122141
*
123-
* @param devAddr The device's address/pin
124-
* param devReg The device's register's address.
125-
* @param data Data to write.
142+
* @param devReg The device's register's address.
143+
* @param data Data to write.
144+
* @param length - length of data
126145
*
127146
* @retval sfeTkError_t kSTkErrOk on successful execution
128147
*
@@ -132,9 +151,9 @@ class sfeTkIBus
132151
/**--------------------------------------------------------------------------
133152
* @brief Writes a number of bytes starting at the given register's 16-bit address.
134153
*
135-
* @param devAddr The device's 16-bit address/pin
136-
* param devReg The device's register's address.
137-
* @param data Data to write.
154+
* @param devReg The device's register's address.
155+
* @param data Data to write.
156+
* @param length - length of data
138157
*
139158
* @retval sfeTkError_t kSTkErrOk on successful execution
140159
*
@@ -145,7 +164,7 @@ class sfeTkIBus
145164
* @brief Read a single byte from the given register
146165
*
147166
* @param devReg The device's register's address.
148-
* @param data Data to read.
167+
* @param data Data to read.
149168
*
150169
* @retval sfeTkError_t - kSTkErrOk on successful execution.
151170
*
@@ -165,8 +184,7 @@ class sfeTkIBus
165184
/**--------------------------------------------------------------------------
166185
* @brief Reads a block of data from the given register.
167186
*
168-
* @param devAddr The device's I2C address.
169-
* @param devReg The device's register's address.
187+
* @param reg The device's register's address.
170188
* @param data Data to write.
171189
* @param numBytes - length of data
172190
* @param[out] readBytes - number of bytes read

src/sfeTkArdI2C.cpp

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ sfeTkError_t sfeTkArdI2C::ping()
8787
//---------------------------------------------------------------------------------
8888
// writeByte()
8989
//
90-
// Writes a single byte to the device.
90+
// Writes a single byte to the device, without indexing to a register.
9191
//
9292
// Returns true on success, false on failure
9393
//
@@ -102,6 +102,33 @@ sfeTkError_t sfeTkArdI2C::writeByte(uint8_t dataToWrite)
102102
return _i2cPort->endTransmission() == 0 ? kSTkErrOk : kSTkErrFail;
103103
}
104104

105+
//---------------------------------------------------------------------------------
106+
// writeWord()
107+
//
108+
// Writes a word to the device, without indexing to a register.
109+
//
110+
// Returns true on success, false on failure
111+
//
112+
sfeTkError_t sfeTkArdI2C::writeWord(uint16_t dataToWrite)
113+
{
114+
if (!_i2cPort)
115+
return kSTkErrBusNotInit;
116+
117+
return writeRegion((uint8_t *)&dataToWrite, sizeof(uint16_t));
118+
}
119+
120+
//---------------------------------------------------------------------------------
121+
// writeRegion()
122+
//
123+
// Writes a word to the device, without indexing to a register.
124+
//
125+
// Returns true on success, false on failure
126+
//
127+
sfeTkError_t sfeTkArdI2C::writeRegion(const uint8_t *data, size_t length)
128+
{
129+
return writeRegisterRegionAddress(nullptr, 0, data, length) == 0 ? kSTkErrOk : kSTkErrFail;
130+
}
131+
105132
//---------------------------------------------------------------------------------
106133
// writeRegisterByte()
107134
//
@@ -152,7 +179,10 @@ sfeTkError_t sfeTkArdI2C::writeRegisterRegionAddress(uint8_t *devReg, size_t reg
152179
return kSTkErrBusNotInit;
153180

154181
_i2cPort->beginTransmission(address());
155-
_i2cPort->write(devReg, regLength);
182+
183+
if(devReg != nullptr && regLength > 0)
184+
_i2cPort->write(devReg, regLength);
185+
156186
_i2cPort->write(data, (int)length);
157187

158188
return _i2cPort->endTransmission() ? kSTkErrFail : kSTkErrOk;
@@ -183,7 +213,7 @@ sfeTkError_t sfeTkArdI2C::writeRegister16Region(uint16_t devReg, const uint8_t *
183213
return writeRegisterRegionAddress((uint8_t *)&devReg, 2, data, length);
184214
}
185215

186-
//---------------------------------------------------------------------------------
216+
187217

188218
/**
189219
* @brief Reads an array of bytes to a register on the target address. Supports any address size

src/sfeTkArdI2C.h

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,16 @@ class sfeTkArdI2C : public sfeTkII2C
8484

8585
/**
8686
@brief - address version of the init method
87+
88+
@param addr The address of the device
8789
*/
8890
sfeTkError_t init(uint8_t addr);
8991

9092
/**
9193
@brief Method sets up the required I2C settings.
9294
9395
@param wirePort Port for I2C communication.
96+
@param addr The address of the device
9497
@param bInit This flag tracks whether the bus has been initialized.
9598
9699
@retval kSTkErrOk on successful execution.
@@ -106,7 +109,7 @@ class sfeTkArdI2C : public sfeTkII2C
106109
sfeTkError_t ping();
107110

108111
/**
109-
@brief Write a single byte to the device
112+
@brief Sends a single byte to the device
110113
@note sfeTkIBus interface method
111114
112115
@param data Data to write.
@@ -115,6 +118,27 @@ class sfeTkArdI2C : public sfeTkII2C
115118
*/
116119
sfeTkError_t writeByte(uint8_t data);
117120

121+
/**
122+
@brief Sends a word to the device.
123+
@note sfeTkIBus interface method
124+
125+
@param data Data to write.
126+
127+
@retval returns kStkErrOk on success
128+
*/
129+
sfeTkError_t writeWord(uint16_t data);
130+
131+
/**
132+
@brief Sends a block of data to the device.
133+
@note sfeTkIBus interface method
134+
135+
@param data Data to write.
136+
@param length - length of data
137+
138+
@retval returns kStkErrOk on success
139+
*/
140+
sfeTkError_t writeRegion(const uint8_t *data, size_t length);
141+
118142
/**
119143
@brief Write a single byte to the given register
120144
@note sfeTkIBus interface method
@@ -145,6 +169,7 @@ class sfeTkArdI2C : public sfeTkII2C
145169
146170
@param devReg The device's register's address.
147171
@param data Data to write.
172+
@param length - length of data
148173
149174
@retval kStkErrOk on success
150175
*/
@@ -153,9 +178,9 @@ class sfeTkArdI2C : public sfeTkII2C
153178
/**
154179
@brief Writes a number of bytes starting at the given register's 16-bit address.
155180
156-
@param devAddr The device's 16-bit address/pin
157-
param devReg The device's register's address.
181+
@param devReg The device's register's address - 16 bit.
158182
@param data Data to write.
183+
@param length - length of data
159184
160185
@retval sfeTkError_t kSTkErrOk on successful execution
161186
@@ -168,7 +193,7 @@ class sfeTkArdI2C : public sfeTkII2C
168193
@note sfeTkIBus interface method
169194
170195
@param devReg The device's register's address.
171-
@param data Data to read.
196+
@param[out] data Data to read.
172197
173198
@retval kStkErrOk on success
174199
*/
@@ -180,7 +205,7 @@ class sfeTkArdI2C : public sfeTkII2C
180205
@note sfeTkIBus interface method
181206
182207
@param devReg The device's register's address.
183-
@param data Data to read.
208+
@param[out] data Data to read.
184209
185210
@retval kSTkErrOk on success
186211
*/
@@ -193,8 +218,8 @@ class sfeTkArdI2C : public sfeTkII2C
193218
@note This method is virtual to allow it to be overridden to support a device that requires a unique impl
194219
195220
@param devReg The device's register's address.
196-
@param data Data being read.
197-
@param numBytes Number of bytes to read.
221+
@param[out] data Data buffer to read into
222+
@param numBytes Number of bytes to read/length of data buffer
198223
@param[out] readBytes - Number of bytes read
199224
200225
@@ -206,8 +231,8 @@ class sfeTkArdI2C : public sfeTkII2C
206231
@brief Reads a block of data from the given 16-bit register address.
207232
208233
@param reg The device's 16 bit register's address.
209-
@param data Data to write.
210-
@param numBytes - length of data
234+
@param data Data buffer to read into
235+
@param numBytes - Number of bytes to read/length of data buffer
211236
@param[out] readBytes - number of bytes read
212237
213238
@retval int returns kSTkErrOk on success, or kSTkErrFail code

src/sfeTkArdSPI.cpp

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ sfeTkError_t sfeTkArdSPI::init(bool bInit)
8282
}
8383

8484
//---------------------------------------------------------------------------------
85-
// writeRegisterByte()
85+
// writeByte()
8686
//
8787
// Writes a single byte to the device.
8888
//
@@ -108,6 +108,46 @@ sfeTkError_t sfeTkArdSPI::writeByte(uint8_t dataToWrite)
108108
return kSTkErrOk;
109109
}
110110

111+
//---------------------------------------------------------------------------------
112+
// writeWord()
113+
//
114+
// Writes a word to the device without indexing to a register.
115+
//
116+
// Returns kSTkErrOk on success
117+
//
118+
sfeTkError_t sfeTkArdSPI::writeWord(uint16_t dataToWrite)
119+
{
120+
return writeRegion((uint8_t *)&dataToWrite, sizeof(uint8_t)) > 0;
121+
}
122+
123+
124+
//---------------------------------------------------------------------------------
125+
// writeRegion()
126+
//
127+
// Writes an array of data to the device without indexing to a register.
128+
//
129+
// Returns kSTkErrOk on success
130+
//
131+
sfeTkError_t sfeTkArdSPI::writeRegion(const uint8_t *dataToWrite, size_t length)
132+
{
133+
134+
if (!_spiPort)
135+
return kSTkErrBusNotInit;
136+
137+
_spiPort->beginTransaction(_sfeSPISettings);
138+
// Signal communication start
139+
digitalWrite(cs(), LOW);
140+
141+
for (size_t i = 0; i < length; i++)
142+
_spiPort->transfer(*dataToWrite++);
143+
144+
// End communication
145+
digitalWrite(cs(), HIGH);
146+
_spiPort->endTransaction();
147+
148+
return kSTkErrOk;
149+
}
150+
111151
//---------------------------------------------------------------------------------
112152
// writeRegisterByte()
113153
//
@@ -164,6 +204,7 @@ sfeTkError_t sfeTkArdSPI::writeRegisterRegion(uint8_t devReg, const uint8_t *dat
164204

165205
// Signal communication start
166206
digitalWrite(cs(), LOW);
207+
167208
_spiPort->transfer(devReg);
168209

169210
for (size_t i = 0; i < length; i++)
@@ -278,4 +319,4 @@ sfeTkError_t sfeTkArdSPI::readRegister16Region(uint16_t devReg, uint8_t *data, s
278319
readBytes = numBytes;
279320

280321
return kSTkErrOk;
281-
}
322+
}

0 commit comments

Comments
 (0)