Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull in branch "Feature/16 bit data support" changes into develop - next step is a release #39

Merged
merged 22 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
ec4aaad
added methods to send 16bit works on the bus - using BIG endian byte …
gigapod Nov 4, 2024
e5fa8c1
add logic to detect system byte order at runtime; used this logic on …
gigapod Nov 7, 2024
a7e74d6
tweak var name
gigapod Nov 7, 2024
a81aefa
removing leading bit on transfer
lewispg228 Nov 7, 2024
c1e9004
bus type method
gigapod Nov 7, 2024
ec20fa5
moved the byte order function to an implementation file - the constex…
gigapod Nov 8, 2024
9846401
created a overloaded set of methods for reading and writing to registers
gigapod Nov 8, 2024
3a7adea
cleanup, re-org of byte order logic -move helpers into a namespace, m…
gigapod Nov 9, 2024
f1d7ac6
move to a byte swap method in the toolkit
gigapod Nov 9, 2024
66611b0
the template/constexpr was a cool/dynamic swap version, but not neede…
gigapod Nov 11, 2024
4cf2fb2
fix issue with outout of count - make sure it is words, not bytes
gigapod Nov 11, 2024
6542241
added abstract for millis and delay(); add include of the bus interfa…
gigapod Feb 7, 2025
749c052
move to c header files and rmeove type_traits include - helps support…
gigapod Feb 10, 2025
e42d0fb
moved/changed how to handle core platform functions - define funcs in…
gigapod Feb 10, 2025
453d46b
moved from using a namespace to simple C functions - cleaner
gigapod Feb 10, 2025
dc0f0ca
change function prefix to *sftk_* - which follows common patterns - 3…
gigapod Feb 11, 2025
6e53aae
add getter for byte order
gigapod Feb 12, 2025
b3c91be
remove include - not needed
gigapod Feb 12, 2025
9d0f0f7
fix issue with not checking endianess
gigapod Feb 12, 2025
89652af
moved the byte order logic to the core bus interface; #include cleanup
gigapod Feb 13, 2025
9aba53f
added banner image
gigapod Feb 13, 2025
8b20d91
fix comment issues, move to using toolkit functions for swaping - not…
gigapod Feb 13, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/SparkFun_Toolkit.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// Arduino Libraries.

// Just include the toolkit headers

#include <sfeTk/sfeToolkit.h>
#include "sfeTkArdI2C.h"
#include "sfeTkArdSPI.h"
#include "sfeTkArdSPI.h"
#include "sfeTkArduino.h"
92 changes: 90 additions & 2 deletions src/sfeTk/sfeTkIBus.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#pragma once

#include "sfeTkError.h"
#include "sfeToolkit.h"
#include <stddef.h>

/**
Expand Down Expand Up @@ -96,7 +96,7 @@ class sfeTkIBus
virtual sfeTkError_t writeByte(uint8_t data) = 0;

/**--------------------------------------------------------------------------
* @brief Send a word to the device.
* @brief Send a word to the device.
* @param data Data to write.
*
* @retval sfeTkError_t - kSTkErrOk on successful execution.
Expand Down Expand Up @@ -125,6 +125,12 @@ class sfeTkIBus
*/
virtual sfeTkError_t writeRegisterByte(uint8_t devReg, uint8_t data) = 0;

// Overload version
sfeTkError_t writeRegister(uint8_t devReg, uint8_t data)
{
return writeRegisterByte(devReg, data);
}

/**--------------------------------------------------------------------------
* @brief Write a single word (16 bit) to the given register
*
Expand All @@ -136,6 +142,12 @@ class sfeTkIBus
*/
virtual sfeTkError_t writeRegisterWord(uint8_t devReg, uint16_t data) = 0;

// Overload version
sfeTkError_t writeRegister(uint8_t devReg, uint16_t data)
{
return writeRegisterWord(devReg, data);
}

/**--------------------------------------------------------------------------
* @brief Writes a number of bytes starting at the given register's address.
*
Expand All @@ -148,6 +160,12 @@ class sfeTkIBus
*/
virtual sfeTkError_t writeRegisterRegion(uint8_t devReg, const uint8_t *data, size_t length) = 0;

// Overload version
sfeTkError_t writeRegister(uint8_t devReg, const uint8_t *data, size_t length)
{
return writeRegisterRegion(devReg, data, length);
}

/**--------------------------------------------------------------------------
* @brief Writes a number of bytes starting at the given register's 16-bit address.
*
Expand All @@ -160,6 +178,29 @@ class sfeTkIBus
*/
virtual sfeTkError_t writeRegister16Region(uint16_t devReg, const uint8_t *data, size_t length) = 0;

// Overload version
sfeTkError_t writeRegister(uint16_t devReg, const uint8_t *data, size_t length)
{
return writeRegister16Region(devReg, data, length);
}

/**--------------------------------------------------------------------------
* @brief Writes a number of uint16's starting at the given register's 16-bit address.
*
* @param devReg The device's register's address.
* @param data Data to write.
* @param length - length of data
*
* @retval sfeTkError_t kSTkErrOk on successful execution
*
*/
virtual sfeTkError_t writeRegister16Region16(uint16_t devReg, const uint16_t *data, size_t length) = 0;

// Overload version
sfeTkError_t writeRegister(uint16_t devReg, const uint16_t *data, size_t length)
{
return writeRegister16Region16(devReg, data, length);
}
/**--------------------------------------------------------------------------
* @brief Read a single byte from the given register
*
Expand All @@ -171,6 +212,12 @@ class sfeTkIBus
*/
virtual sfeTkError_t readRegisterByte(uint8_t devReg, uint8_t &data) = 0;

// Overload version
sfeTkError_t readRegister(uint8_t devReg, uint8_t &data)
{
return readRegisterByte(devReg, data);
}

/**--------------------------------------------------------------------------
* @brief Read a single word (16 bit) from the given register
*
Expand All @@ -181,6 +228,12 @@ class sfeTkIBus
*/
virtual sfeTkError_t readRegisterWord(uint8_t devReg, uint16_t &data) = 0;

// Overload version
sfeTkError_t readRegister(uint8_t devReg, uint16_t &data)
{
return readRegisterWord(devReg, data);
}

/**--------------------------------------------------------------------------
* @brief Reads a block of data from the given register.
*
Expand All @@ -194,6 +247,12 @@ class sfeTkIBus
*/
virtual sfeTkError_t readRegisterRegion(uint8_t reg, uint8_t *data, size_t numBytes, size_t &readBytes) = 0;

// Overload version
sfeTkError_t readRegister(uint8_t reg, uint8_t *data, size_t numBytes, size_t &readBytes)
{
return readRegisterRegion(reg, data, numBytes, readBytes);
}

/**--------------------------------------------------------------------------
* @brief Reads a block of data from the given 16-bit register address.
*
Expand All @@ -206,6 +265,35 @@ class sfeTkIBus
*
*/
virtual sfeTkError_t readRegister16Region(uint16_t reg, uint8_t *data, size_t numBytes, size_t &readBytes) = 0;

// Overload version
sfeTkError_t readRegister(uint16_t reg, uint8_t *data, size_t numBytes, size_t &readBytes)
{
return readRegister16Region(reg, data, numBytes, readBytes);
}
/**--------------------------------------------------------------------------
* @brief Reads a block of data from the given 16-bit register address.
*
* @param reg The device's 16 bit register's address.
* @param data Data to write.
* @param numBytes - length of data
* @param[out] readBytes - number of bytes read
*
* @retval int returns kSTkErrOk on success, or kSTkErrFail code
*
*/
virtual sfeTkError_t readRegister16Region16(uint16_t reg, uint16_t *data, size_t numBytes, size_t &readBytes) = 0;

// Overload version
sfeTkError_t readRegister(uint16_t reg, uint16_t *data, size_t numBytes, size_t &readBytes)
{
return readRegister16Region16(reg, data, numBytes, readBytes);
}

virtual uint8_t type(void)
{
return 0;
}
};

//};
7 changes: 7 additions & 0 deletions src/sfeTk/sfeTkII2C.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
* The I2C bus interface extends the IBus interface and adds the ability to set and get the I2C
* address and stop flag.
*/
const uint8_t kBusTypeI2C = 0x01;

class sfeTkII2C : public sfeTkIBus
{
public:
Expand Down Expand Up @@ -109,6 +111,11 @@ class sfeTkII2C : public sfeTkIBus
*/
static constexpr uint8_t kNoAddress = 0;

virtual uint8_t type(void)
{
return kBusTypeI2C;
}

private:
uint8_t _address;
bool _stop;
Expand Down
7 changes: 7 additions & 0 deletions src/sfeTk/sfeTkISPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* The SPI bus interface extends the IBus interface and adds the ability to set and get the CS pin.
*/

const uint8_t kBusTypeSPI = 0x02;
class sfeTkISPI : public sfeTkIBus
{
public:
Expand Down Expand Up @@ -82,6 +84,11 @@ class sfeTkISPI : public sfeTkIBus
*/
static constexpr uint8_t kNoCSPin = 0;

virtual uint8_t type(void)
{
return kBusTypeSPI;
}

private:
/** The internal storage of the _cs value*/
uint8_t _cs;
Expand Down
75 changes: 75 additions & 0 deletions src/sfeTk/sfeToolkit.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// File: sfeToolkit.cpp
//
// General impl file for the SparkFun Toolkit
/*

The MIT License (MIT)

Copyright (c) 2024 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.
*/
#include "sfeToolkit.h"

// THIS IS A PLACEHOLDER FILE for now
#ifdef ARDUINO
#include <Arduino.h>
#endif
//---------------------------------------------------------------------------------
/**
* @brief C function - Runtime check for system byte order
*/
sfeTKByteOrder sftk_system_byteorder(void)
{
uint16_t i = 1;
return *((uint8_t *)&i) == 0 ? sfeTKByteOrder::BigEndian : sfeTKByteOrder::LittleEndian;
}

//---------------------------------------------------------------------------------
/**
* @brief to catch 8 bit calls for byte swap
*
*/
uint8_t sftk_byte_swap(uint8_t i)
{
return i;
}
//---------------------------------------------------------------------------------
/**
* @brief function - Byte swap a 16 bit value
*/
uint16_t sftk_byte_swap(uint16_t i)
{
// Use the fast intrinsic if available
#if defined(__clang__) || defined(__GNUC__)
return __builtin_bswap16(i);
#else
return (i << 8) | (i >> 8);
#endif
}

//---------------------------------------------------------------------------------
/**
* @brief function - Byte swap a 32 bit value
*/
uint32_t sftk_byte_swap(uint32_t i)
{
#if defined(__clang__) || defined(__GNUC__)
return __builtin_bswap32(i);
#else
return ((i << 24) & 0xff000000) | ((i << 8) & 0x00ff0000) | ((i >> 8) & 0x0000ff00) | ((i >> 24) & 0x000000ff);
#endif
}
32 changes: 31 additions & 1 deletion src/sfeTk/sfeToolkit.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

The MIT License (MIT)

Copyright (c) 2023 SparkFun Electronics
Copyright (c) 2024 SparkFun Electronics

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
Expand All @@ -26,7 +26,37 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#pragma once

#include <limits.h>
SFE-Brudnerd marked this conversation as resolved.
Show resolved Hide resolved
#include <stdint.h>

/**
@brief Common include file for the core of the SparkFun Electronics Toolkit
*/
#include "sfeTkError.h"
#include "sfeTkIBus.h"

// byte order types/enum
enum class sfeTKByteOrder : uint8_t
{
BigEndian = 0x01,
LittleEndian = 0x02
};

// Note - toolkit *functions* start with sftk_ to avoid name collisions

// Function to determine the byte order of the system
sfeTKByteOrder sftk_system_byteorder(void);

uint8_t sftk_byte_swap(uint8_t i);
uint16_t sftk_byte_swap(uint16_t i);
uint32_t sftk_byte_swap(uint32_t i);


// Area for platform specific implementations. The interface/functions are
// defined here, with the expectation that the platform provides the implementation.

// delay in milliseconds
void sftk_delay_ms(uint32_t ms);

// ticks in milliseconds
uint32_t sftk_ticks_ms(void);
Loading