Skip to content

Commit 65924a2

Browse files
committed
pushed all read/write of specific types [byte, word] to the bus interface, which calls general data methods; Finished out type coverage for the readRegister() and writeRegister() overloaded methods; Added a set of overloaded writeData() methods (no register); overall simplifies the impl requirements for a bus driver; Also completed/reviewed the endian byteswap logic
1 parent c7e4a55 commit 65924a2

File tree

5 files changed

+237
-354
lines changed

5 files changed

+237
-354
lines changed

src/sfTk/sfTkIBus.h

Lines changed: 235 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,69 @@ class sfTkIBus
8686
* @retval sfTkError_t - ksfTkErrOk on successful execution.
8787
*
8888
*/
89-
virtual sfTkError_t writeByte(uint8_t data) = 0;
89+
sfTkError_t writeByte(uint8_t data)
90+
{
91+
return writeRegion(&data, sizeof(uint8_t));
92+
}
93+
94+
/**--------------------------------------------------------------------------
95+
* @brief Writes a 8-bit data value.
96+
*
97+
* This function writes a 8-bit data value
98+
*
99+
* @param data The 8-bit data value to be written.
100+
* @return sfTkError_t The status of the write operation.
101+
*/
102+
sfTkError_t writeData(uint8_t data)
103+
{
104+
return writeByte(data);
105+
}
90106

91107
/**--------------------------------------------------------------------------
92108
* @brief Send a word to the device.
93-
* @param data Data to write.
109+
* @param data Data to write. If the byte order is different than system byteorder, data is byteswapped
94110
*
95111
* @retval sfTkError_t - ksfTkErrOk on successful execution.
96112
*
97113
*/
98-
virtual sfTkError_t writeWord(uint16_t data) = 0;
114+
sfTkError_t writeWord(uint16_t data)
115+
{
116+
// byte swap?
117+
if (sftk_system_byteorder() != _byteOrder)
118+
data = sftk_byte_swap(data);
119+
120+
return writeRegion((uint8_t *)&data, sizeof(uint16_t));
121+
}
122+
123+
/**--------------------------------------------------------------------------
124+
* @brief Writes a 16-bit data value.
125+
*
126+
* This function writes a 16-bit data value
127+
*
128+
* @param data The 16-bit data value to be written. The data is byteswapped if needed.
129+
* @return sfTkError_t The status of the write operation.
130+
*/
131+
sfTkError_t writeData(uint16_t data)
132+
{
133+
return writeWord(data);
134+
}
135+
136+
/**--------------------------------------------------------------------------
137+
* @brief Writes a 32-bit data value.
138+
*
139+
* This function writes a 32-bit data value
140+
*
141+
* @param data The 32-bit data value to be written. The data is byteswapped if needed.
142+
* @return sfTkError_t The status of the write operation.
143+
*/
144+
sfTkError_t writeData(uint32_t data)
145+
{
146+
// byte swap?
147+
if (sftk_system_byteorder() != _byteOrder)
148+
data = sftk_byte_swap(data);
149+
150+
return writeRegion((uint8_t *)&data, sizeof(uint32_t));
151+
}
99152

100153
/**--------------------------------------------------------------------------
101154
* @brief Send an array of data to the device.
@@ -116,7 +169,10 @@ class sfTkIBus
116169
* @retval sfTkError_t - ksfTkErrOk on successful execution.
117170
*
118171
*/
119-
virtual sfTkError_t writeRegisterByte(uint8_t devReg, uint8_t data) = 0;
172+
sfTkError_t writeRegisterByte(uint8_t devReg, uint8_t data)
173+
{
174+
return writeRegisterRegion(devReg, &data, sizeof(uint8_t));
175+
}
120176

121177
// Overload version
122178
sfTkError_t writeRegister(uint8_t devReg, uint8_t data)
@@ -128,19 +184,51 @@ class sfTkIBus
128184
* @brief Write a single word (16 bit) to the given register
129185
*
130186
* @param devReg The device's register's address.
131-
* @param data Data to write.
187+
* @param data Data to write. Note - if byte order differs, swap the data before writing.
132188
*
133189
* @retval sfTkError_t - ksfTkErrOk on successful execution.
134190
*
135191
*/
136-
virtual sfTkError_t writeRegisterWord(uint8_t devReg, uint16_t data) = 0;
192+
virtual sfTkError_t writeRegisterWord(uint8_t devReg, uint16_t data)
193+
{
194+
// swap the data?
195+
if (sftk_system_byteorder() != _byteOrder)
196+
data = sftk_byte_swap(data);
137197

138-
// Overload version
198+
return writeRegisterRegion(devReg, (uint8_t *)&data, sizeof(uint16_t));
199+
}
200+
201+
/**
202+
* @brief Writes a 16-bit data value to a specified device register.
203+
*
204+
* This function writes a 16-bit data value to a specified device register.
205+
* If the system byte order differs from the byte order expected by the device,
206+
* the data value is byte-swapped before being written.
207+
*
208+
* @param devReg The register address to write to.
209+
* @param data The 16-bit data value to write.
210+
* @return sfTkError_t Error code indicating the success or failure of the operation.
211+
*/
139212
sfTkError_t writeRegister(uint8_t devReg, uint16_t data)
140213
{
141214
return writeRegisterWord(devReg, data);
142215
}
143216

217+
/**
218+
* @brief Writes a 32-bit data value to a specified device register.
219+
*
220+
* This function writes a 32-bit data value to a specified device register.
221+
* If the system byte order differs from the byte order expected by the device,
222+
* the data value is byte-swapped before being written.
223+
*
224+
* @param devReg The register address to write to.
225+
* @param data The 32-bit data value to write.
226+
* @return sfTkError_t Error code indicating the success or failure of the operation.
227+
*/
228+
sfTkError_t writeRegister(uint8_t devReg, const uint32_t data)
229+
{
230+
return writeRegisterRegion(devReg, (uint8_t *)&data, sizeof(uint32_t));
231+
}
144232
/**--------------------------------------------------------------------------
145233
* @brief Writes a number of bytes starting at the given register's address.
146234
*
@@ -177,6 +265,46 @@ class sfTkIBus
177265
return writeRegister16Region(devReg, data, length);
178266
}
179267

268+
/**
269+
* @brief Writes a 16-bit data value to a specified device register.
270+
*
271+
* This function writes a 16-bit data value to a specified device register.
272+
* If the system byte order differs from the byte order expected by the device,
273+
* the data value is byte-swapped before being written.
274+
*
275+
* @param devReg The register address to write to.
276+
* @param data The 16-bit data value to write.
277+
* @return sfTkError_t Error code indicating the success or failure of the operation.
278+
*/
279+
sfTkError_t writeRegister(uint16_t devReg, const uint16_t data)
280+
{
281+
uint16_t value = data;
282+
// do we need to swap the data value? Note the address is swapped in the called writeReg16 method
283+
if (sftk_system_byteorder() != _byteOrder)
284+
value = sftk_byte_swap(data);
285+
286+
return writeRegister16Region(devReg, (const uint8_t *)&value, sizeof(uint16_t));
287+
}
288+
/**
289+
* @brief Writes a 32-bit data value to a specified device register.
290+
*
291+
* This function writes a 32-bit data value to a specified device register.
292+
* If the system byte order differs from the byte order expected by the device,
293+
* the data value is byte-swapped before being written.
294+
*
295+
* @param devReg The register address to write to.
296+
* @param data The 32-bit data value to write.
297+
* @return sfTkError_t Error code indicating the success or failure of the operation.
298+
*/
299+
sfTkError_t writeRegister(uint16_t devReg, const uint32_t data)
300+
{
301+
uint32_t value = data;
302+
// do we need to swap the data value? Note the address is swapped in the called writeReg16 method
303+
if (sftk_system_byteorder() != _byteOrder)
304+
value = sftk_byte_swap(data);
305+
306+
return writeRegister16Region(devReg, (const uint8_t *)&value, sizeof(uint32_t));
307+
}
180308
/**--------------------------------------------------------------------------
181309
* @brief Writes a number of uint16's starting at the given register's 16-bit address.
182310
*
@@ -203,7 +331,14 @@ class sfTkIBus
203331
* @retval sfTkError_t - ksfTkErrOk on successful execution.
204332
*
205333
*/
206-
virtual sfTkError_t readRegisterByte(uint8_t devReg, uint8_t &data) = 0;
334+
virtual sfTkError_t readRegisterByte(uint8_t devReg, uint8_t &data)
335+
{
336+
337+
size_t nRead;
338+
sfTkError_t retval = readRegisterRegion(devReg, (uint8_t *)&data, sizeof(uint8_t), nRead);
339+
340+
return (retval == ksfTkErrOk && nRead == sizeof(uint8_t) ? ksfTkErrOk : retval);
341+
}
207342

208343
// Overload version
209344
sfTkError_t readRegister(uint8_t devReg, uint8_t &data)
@@ -215,18 +350,58 @@ class sfTkIBus
215350
* @brief Read a single word (16 bit) from the given register
216351
*
217352
* @param devReg The device's register's address.
218-
* @param data Data to read.
353+
* @param data Data to read.-- the data value is byteswapped if needed
219354
*
220355
* @retval sfTkError_t - ksfTkErrOk on successful execution.
221356
*/
222-
virtual sfTkError_t readRegisterWord(uint8_t devReg, uint16_t &data) = 0;
357+
virtual sfTkError_t readRegisterWord(uint8_t devReg, uint16_t &data)
358+
{
359+
size_t nRead;
360+
sfTkError_t retVal = readRegisterRegion(devReg, (uint8_t *)&data, sizeof(uint16_t), nRead);
361+
362+
// The data is a uint32 - byte swap the result?
363+
if (retVal == ksfTkErrOk && sftk_system_byteorder() != _byteOrder)
364+
data = sftk_byte_swap(data);
365+
366+
return (retVal == ksfTkErrOk && nRead == sizeof(uint16_t) ? ksfTkErrOk : retVal);
367+
}
223368

224369
// Overload version
370+
/**
371+
* @brief Reads a 16-bit value from the specified device register.
372+
*
373+
* This function reads a 16-bit value from the register specified by `devReg`
374+
* and stores the result in the provided `data` reference.
375+
*
376+
* @param devReg The register address to read from.
377+
* @param data Reference to a variable where the read value will be stored. The data value is byteswapped if needed
378+
* @return sfTkError_t Error code indicating the success or failure of the operation.
379+
*/
225380
sfTkError_t readRegister(uint8_t devReg, uint16_t &data)
226381
{
227382
return readRegisterWord(devReg, data);
228383
}
229384

385+
/**--------------------------------------------------------------------------
386+
* @brief Read a 32 bit value from the given register
387+
*
388+
* @param devReg The device's register's address.
389+
* @param data Data to read.
390+
*
391+
* @retval sfTkError_t - ksfTkErrOk on successful execution.
392+
*/
393+
394+
virtual sfTkError_t readRegister(uint8_t devReg, uint32_t &data)
395+
{
396+
size_t nRead;
397+
sfTkError_t retVal = readRegisterRegion(devReg, (uint8_t *)&data, sizeof(uint32_t), nRead);
398+
399+
// The data is a uint32 - byte swap the result?
400+
if (retVal == ksfTkErrOk && sftk_system_byteorder() != _byteOrder)
401+
data = sftk_byte_swap(data);
402+
403+
return (retVal == ksfTkErrOk && nRead == sizeof(uint32_t) ? ksfTkErrOk : retVal);
404+
}
230405
/**--------------------------------------------------------------------------
231406
* @brief Reads a block of data from the given register.
232407
*
@@ -240,12 +415,48 @@ class sfTkIBus
240415
*/
241416
virtual sfTkError_t readRegisterRegion(uint8_t reg, uint8_t *data, size_t numBytes, size_t &readBytes) = 0;
242417

243-
// Overload version
418+
/**
419+
* @brief Reads data from a specified register.
420+
*
421+
* This function reads a specified number of bytes from a given register and stores the data in the provided buffer.
422+
*
423+
* @param reg The register address to read from.
424+
* @param data Pointer to the buffer where the read data will be stored.
425+
* @param numBytes The number of bytes to read from the register.
426+
* @param readBytes Reference to a variable where the number of bytes actually read will be stored.
427+
* @return sfTkError_t Error code indicating the success or failure of the read operation.
428+
*/
244429
sfTkError_t readRegister(uint8_t reg, uint8_t *data, size_t numBytes, size_t &readBytes)
245430
{
246431
return readRegisterRegion(reg, data, numBytes, readBytes);
247432
}
248433

434+
/**
435+
* @brief Overloaded function to read a 16-bit value from the given 16-bit register address.
436+
*
437+
* This function reads a 16-bit value from the specified 16-bit register address.
438+
*
439+
* @param reg The device's 16-bit register's address.
440+
* @param[out] value The 16-bit value read from the register.
441+
*
442+
* @return sfTkError_t Returns ksfTkErrOk on success, or an error code on failure.
443+
*/
444+
virtual sfTkError_t readRegister(uint16_t reg, uint16_t &value)
445+
{
446+
size_t readBytes = 0;
447+
448+
// this is a reg 16 address - so call the method to manage this. Note - it manages the
449+
// byte swapping of the address.
450+
451+
sfTkError_t retValue = readRegister16Region(reg, (uint8_t *)&value, sizeof(uint16_t), readBytes);
452+
453+
// The data is a uint16 - byte swap the result?
454+
if (retValue == ksfTkErrOk && sftk_system_byteorder() != _byteOrder)
455+
value = sftk_byte_swap(value);
456+
457+
return retValue;
458+
}
459+
249460
/**
250461
* @brief Overloaded function to read a 32-bit value from the given 16-bit register address.
251462
*
@@ -271,6 +482,7 @@ class sfTkIBus
271482

272483
return retValue;
273484
}
485+
274486
/**--------------------------------------------------------------------------
275487
* @brief Reads a block of data from the given 16-bit register address.
276488
*
@@ -285,10 +497,22 @@ class sfTkIBus
285497
virtual sfTkError_t readRegister16Region(uint16_t reg, uint8_t *data, size_t numBytes, size_t &readBytes) = 0;
286498

287499
// Overload version
500+
/**
501+
* @brief Reads a specified number of bytes from a given register.
502+
*
503+
* This function reads data from a 16-bit register and stores it in the provided buffer.
504+
*
505+
* @param reg The 16-bit register address to read from.
506+
* @param data Pointer to the buffer where the read data will be stored.
507+
* @param numBytes The number of bytes to read from the register.
508+
* @param readBytes Reference to a variable where the number of bytes actually read will be stored.
509+
* @return sfTkError_t Error code indicating the success or failure of the read operation.
510+
*/
288511
sfTkError_t readRegister(uint16_t reg, uint8_t *data, size_t numBytes, size_t &readBytes)
289512
{
290513
return readRegister16Region(reg, data, numBytes, readBytes);
291514
}
515+
292516
/**--------------------------------------------------------------------------
293517
* @brief Reads a block of data from the given 16-bit register address.
294518
*

0 commit comments

Comments
 (0)