Skip to content

Commit cf48269

Browse files
authored
Merge pull request #26 from sparkfun/main
merge main to adderr
2 parents 5b6509d + 702fec6 commit cf48269

18 files changed

+218
-48
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,29 @@
11
# SparkFun Toolkit Arduino Library
22

33
![Toolkit Tests Builds](https://github.com/sparkfun/SparkFun_Toolkit/actions/workflows/compile-sketch.yml/badge.svg)
4+
![GitHub issues](https://img.shields.io/github/issues/sparkfun/SparkFun_Toolkit)
5+
![GitHub release (with filter)](https://img.shields.io/github/v/release/sparkfun/SparkFun_Toolkit)
6+
![GitHub (Pre-)Release Date](https://img.shields.io/github/release-date-pre/sparkfun/SparkFun_Toolkit)
7+
8+
9+
10+
The SparkFun Toolkit provides a common set of core functionality for use across the SparkFun Arduino Driver library. Instead of each device driver library implementing it's own communication layers, error types and design, the SparkFun Toolkit library is used.
11+
12+
By using the SparkFun Toolkit, Arduino drivers achieve the following benefits:
13+
14+
* Use a well-tested and validated implementation
15+
* Reduce development effort
16+
* Implement functionality following a common structure
17+
* Set the foundation for future enhancements - as the capabilities of the toolkit grow, these features become available with little to any implementation effort.
18+
19+
## Current Status
20+
21+
### December 2023
22+
23+
The SparkFun Toolkit is available as a *Beta* release, with the intent of testing and validation by SparkFun. The community are free to use this toolkit with the understanding that interfaces, types and class structures could change.
24+
25+
### Documentation
26+
27+
|||
28+
|---|---|
29+
|[Bus Implementation](docs/ar_ibus.md) | The architecture and use of the Tookkit Communication Bus Interface

docs/ar_ibus.md

Lines changed: 61 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
1-
# Overview - Device Bus interface - sfeTkIBus
1+
# Overview - Device Bus Interface - sfeTkIBus
22

33
One of the foundational capabilities of the SparkFun Toolkit is bus communication with devices. This is a common task almost all libraries implement using their own implementation for I2C, SPI or UART bus communication.
44

55
For bus communication, the SparkFun Toolkit is designed to provide a common implementation for use across all SparkFun libraries. Additionally, the bus architecture is modeled on a *driver* pattern, separating the individual bus setup/configuration from data communication, enabling a single device implementation to easily support a variety of device bus types.
66

7-
The key goals set for the Bus implementation in the Toolkit include:
7+
### The Bus Interface Design Pattern
8+
9+
This pattern allows an application to develop against the common bus interface without regard to the underlying bus type or implementation. This *plug-in* nature of this model enables core application reuse across a range of bus devices. What to use a different bus type? Just use a different driver.
10+
11+
This pattern is show in the following diagram:
12+
13+
![Driver Pattern](images/tk_ibus_p1.png)
14+
15+
This pattern extends across different platforms, allowing a common platform independent application core to utilize platform specific bus drivers.
16+
17+
![Platform Independence](images/tk_ibus_p2.png)
18+
19+
The platform dependant drivers implement the core Bus Interface (IBus) for communication, with platform specific setup and management left to the underlying implementation. Since the application core only works with the Bus Interface, if implemented correctly, the same core works across different bus types and across different development environments.
20+
21+
## Goals
22+
23+
For the initial implementation the key goals set for the Bus implementation in the Toolkit include:
824

925
* Separate device setup from device communication
1026
* Define a common bus interface for use across a variety of common device bus types
@@ -13,7 +29,7 @@ The key goals set for the Bus implementation in the Toolkit include:
1329

1430
## Architecture Overview
1531

16-
To meet the goals for this subsystem, the Flux framework follows a ***Driver Pattern***, defining a common interface for bus communication. Device drivers are designed around this interface, leaving bus configuration and implementation to platform specific implementation.
32+
As outlined above, the SparkFun Toolkit follows a ***Driver Pattern***, defining a common interface for bus communication. Device drivers are designed around this interface, leaving bus configuration and implementation to platform specific implementation.
1733

1834
The key class to support this pattern are:
1935

@@ -41,9 +57,13 @@ The interface methods:
4157
> [!NOTE]
4258
> This interface only defines the methods to read and write data on the given bus. Any address, or bus specific settings is provided/implemented by the implementation/specialization of this interface.
4359
60+
The Inteface diagram for the ```sfeTkIBus``` is:
61+
62+
![IIBus Interface](images/tk_uml_ibus.png)
63+
4464
### The sfeTkII2C Implementation
4565

46-
This class sub-classes from the ```sfeTkIBus``` interface adding additional functionally focused on supporting an I2C implementation. This interface provides the additional functionality.
66+
This class sub-classes from the ```sfeTkIBus``` interface adding additional functionally focused on supporting an I2C implementation. This class does not implement the IIBus interface, so it's abstract, but the class adds the additional functionality.
4767

4868
| Method| Definition |
4969
|------|-------|
@@ -54,6 +74,10 @@ This class sub-classes from the ```sfeTkIBus``` interface adding additional func
5474
> [!NOTE]
5575
> The ```sfeTkII2C``` class manages the device address for the I2C bus. As such, each I2C device instantiates/uses an instance of the ```sfeTkII2C``` class.
5676
77+
The class diagram for the ```sfeTkII2C``` interface is the following:
78+
79+
![II2C Class Diagram](images/tk_uml_ii2c.png)
80+
5781
### The sfeTkISPI Implementation
5882

5983
This class sub-classes from the ```sfeTkIBus``` interface adding additional functionally focused on supporting an SPI implementation. This interface provides the additional functionality.
@@ -68,31 +92,35 @@ This class sub-classes from the ```sfeTkIBus``` interface adding additional func
6892
6993
The class diagram of these base class interfaces/implementation:
7094

71-
![IBus diagram](images/tk_IBUS.png)
95+
![ISPI Class Diagram](images/tk_uml_ispi.png)
7296

73-
## sfeTkIIBus - Arduino Implementation
97+
## sfeTkIBus - Arduino Implementation
7498

7599
The initial implementation of the toolkit IBus interface is for the Arduino environment. This implementation consists of two classes, ```sfeTkArdI2C``` and ```sfeTkArdSPI```, each of which sub-class from their respective bus type interfaces within the core toolkit.
76100

77101
These driver implementations provide the platform specific implementation for the toolkit bus interfaces, supporting the methods defined by the interfaces, as well as contain and manage the platform specific settings and attributes for each bus type.
78102

79103
> [!IMPORTANT]
80-
> The intent is that each user of an particular bus - a device in most cases - contains an instance of the specific bus object.
104+
> The intent is that each user of an particular - a device in most cases - contains an instance of the specific bus class.
81105
82-
The class diagram for the Arduino implementation is as follows:
106+
### The sfeTkArdI2C Class
83107

84-
![Arduino IBus Implementation](images/tk_ibus_ard.png)
108+
This class provides the Arduino implementation of I2C in the SparkFun Toolkit. It implements the methods of the ```sfeTkIBus``` and ```sfeTkII2C``` interfaces, as well as manages any Arduino specific state.
85109

86-
### The sfeTkArdI2C Class
110+
The class diagram for the sfeTkArdI2C class:
87111

88-
This class provides the Arduino implementation of I2C in the SparkFun Toolkit. It implements the methods of the ```sfeTkIIBus``` and ```sfeTkII2C``` interfaces, as well as manages any Arduino specific state.
112+
![Arduino I2C Class Diagram](images/tk_uml_ardi2c.png)
89113

90114
### The sfeTkArdSPI Class
91115

92-
This class provides the Arduino implementation of SPI in the SparkFun Toolkit. It implements the methods of the ```sfeTkIIBus``` and ```sfeTkISPI``` interfaces, as well as manages any Arduino specific state for the SPI bus - namely the SPISettings class.
116+
This class provides the Arduino implementation of SPI in the SparkFun Toolkit. It implements the methods of the ```sfeTkIBus``` and ```sfeTkISPI``` interfaces, as well as manages any Arduino specific state for the SPI bus - namely the SPISettings class.
93117

94118
Before each use of the SPI bus, the methods of the ```sfeTkArdSPI``` uses an internal SPISettings class to ensure the SPI bus is operating in the desired mode for the device.
95119

120+
The class diagram for the sfeTkArdSPI class:
121+
122+
![Arduino SPI Class Diagram](images/tk_uml_ardspi.png)
123+
96124
## sfeTkIBus Use
97125

98126
The general steps when using the sfeTkIBus in device development are outlined in the following steps. This example uses the Arduino implementation of the bus.
@@ -101,7 +129,7 @@ The general pattern for a device driver implementation that uses the SparkFun To
101129

102130
### Implement a Platform Independent Driver
103131

104-
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 ```sfeTkIIBus``` interface.
132+
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.
105133

106134
>[!IMPORTANT]
107135
> At this level, the driver is only using a ```sfeTkIBus``` interface, not any specific bus implementation.
@@ -111,7 +139,10 @@ This driver has the following unique functionality:
111139
1) A method to set the object that implements the ```sfeTkIBus``` interface object should use. Since
112140
1) If the device supports identification capabilities, the driver provides this functionality.
113141

114-
#### SImple Example of an Independent Driver Implementation
142+
#### Simple Example of an Independent Driver Implementation
143+
144+
>[!NOTE]
145+
> This code is **pseudo-code**, used to demonstrate the key concepts of the implementation pattern.
115146
116147
This implementation would take the following form:
117148

@@ -121,7 +152,7 @@ class myDriverClass
121152
{
122153
public:
123154

124-
myDriverClass(uint8_t address) : _addr{address}{}
155+
myDriverClass(uint8_t address) : _addr{address}, _theBus{nullptr}{}
125156

126157
bool begin()
127158
{
@@ -139,9 +170,9 @@ public:
139170
if (!_theBus || !data || len == 0)
140171
return false;
141172

142-
int status = _theBus->writeRegisterRegion(THE_REG, data, len);
173+
sfeTkError_t status = _theBus->writeRegisterRegion(THE_REG, data, len);
143174

144-
return (status == 0);
175+
return (status == kSTkErrOk);
145176
}
146177

147178
bool checkDeviceID()
@@ -150,6 +181,7 @@ public:
150181
return true;
151182
}
152183
private:
184+
uint8_t _addr;
153185
sfeTkIBus *_theBus;
154186
};
155187
```
@@ -180,7 +212,7 @@ class myArduinoDriverI2C : public myDriverClass
180212
181213
bool begin()
182214
{
183-
if (!_theI2CBus.init(MY_DEVICE_ADDRESS))
215+
if (_theI2CBus.init(MY_DEVICE_ADDRESS) != kSTkErrOk)
184216
return false;
185217
setCommunicationBus(&_theI2CBus);
186218
@@ -189,7 +221,7 @@ class myArduinoDriverI2C : public myDriverClass
189221
190222
bool isConnected()
191223
{
192-
if (!_theI2CBus.ping())
224+
if (_theI2CBus.ping() != kSTkErrOk)
193225
return false;
194226
195227
return checkDeviceID();
@@ -219,7 +251,7 @@ class myArduinoDriveSPI : public myDriverClass
219251
{
220252
SPISettings spiSettings = SPISettings(4000000, MSBFIRST, SPI_MODE3);
221253

222-
if (!_theSPIBus.init(SPI, spiSettings, MY_DEFAULT_CS, true))
254+
if (_theSPIBus.init(SPI, spiSettings, MY_DEFAULT_CS, true) != kSTkErrOk)
223255
return false;
224256
setCommunicationBus(&_theSPIBus);
225257

@@ -235,3 +267,12 @@ private:
235267
sfeTkArdSPI _theSPIBus;
236268
};
237269
```
270+
271+
## Summary
272+
273+
In summary, the SparkFun Toolkit Bus Interface sets a standard that device drivers can implement against without concern for platform or bus type. Using common interface implementation patterns, the implementation delivers on the goals for this subsystem - namely:
274+
275+
* Separate device setup from device communication
276+
* Define a common bus interface for use across a variety of common device bus types
277+
* Deliver support for both SPI and I2C bus types initially, focusing on Arduino
278+
* Structure the bus/toolkit implementation such that it's platform independent

docs/images/tk_IBUS.png

-100 KB
Binary file not shown.

docs/images/tk_ibus_ard.png

-198 KB
Binary file not shown.

docs/images/tk_ibus_p1.png

34.9 KB
Loading

docs/images/tk_ibus_p2.png

50.9 KB
Loading

docs/images/tk_uml_ardi2c.png

298 KB
Loading

docs/images/tk_uml_ardspi.png

276 KB
Loading

docs/images/tk_uml_ibus.png

64 KB
Loading

docs/images/tk_uml_ii2c.png

171 KB
Loading

0 commit comments

Comments
 (0)