Skip to content

Commit 5e335ca

Browse files
committed
update for v1.0
1 parent c00b3d4 commit 5e335ca

File tree

1 file changed

+65
-92
lines changed

1 file changed

+65
-92
lines changed

docs/ar_ibus.md

Lines changed: 65 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,6 @@ For bus communication, the SparkFun Toolkit is designed to provide a common impl
88

99
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.
1010

11-
```mermaid
12-
---
13-
title: Design Pattern Bus Interface
14-
---
15-
classDiagram
16-
class MyApplication
17-
18-
class IBus
19-
<<Interface>> IBus
20-
IBus : writeData(...)
21-
IBus : readRegister(...)
22-
IBus : writeRegister(...)
23-
24-
MyApplication --> IBus
25-
26-
IBus <|-- IBusType
27-
28-
note for MyApplication "Application/library that <br>uses the IBus Interface"
29-
note for IBus "Defines the bus interface"
30-
note for IBusType "Specific implementation type <br> methods [I2C, SPI, UART,...]"
31-
32-
```
33-
3411
This pattern is show in the following diagram:
3512

3613
![Driver Pattern](images/tk_ibus_p1.png)
@@ -58,35 +35,32 @@ The key class to support this pattern are:
5835

5936
| | |
6037
|------|-------|
61-
|**sfeTkIBus** | A virtual C++ class that device the bus ```sfeTkIBus``` interface |
62-
|**sfeTkII2C** | Sub-class of the ```sfeTkIIBus``` interface, it provides an interface for I2C devices|
63-
|**sfeTkISPI** | Sub-class of the ```sfeTkIIBus``` interface, it provides an interface for SPI devices |
38+
|**sfTkIBus** | A virtual C++ class that device the bus ```sfeTkIBus``` interface |
39+
|**sfTkII2C** | Sub-class of the ```sfeTkIIBus``` interface, it provides an interface for I2C devices|
40+
|**sfTkISPI** | Sub-class of the ```sfeTkIIBus``` interface, it provides an interface for SPI devices |
6441

65-
### The sfeTkIBus Interface
42+
### The sfTkIBus Interface
6643

6744
The key to meeting the goals of the Toolkit is the IBus interface. This interface defines the methods used to communicate with a device. The setup, configuration and implementation of this interface is performed by platform specific implementations of the interface.
6845

69-
The interface methods:
46+
The bus implements methods that perform the following
7047

7148
| Method| Definition |
7249
|------|-------|
73-
|**writeRegisterByte** | Write a byte of data to a particular register of a device |
74-
|**writeRegisterWord** | Write a word of data to a particular register of a device |
75-
|**writeRegisterRegion** | Write an array of data to a particular register of a device|
76-
|**readRegisterByte** | Read a byte of data from a particular register of a device |
77-
|**readRegisterWord** | Read a word of data from a particular register of a device |
78-
|**readRegisterRegion** | Read an array of data from a particular register of a device |
50+
|**writeRegister(...)** | Overloaded set of methods to write data to a specified register|
51+
|**readRegister(...)** | Overloaded set of methods to read data from a specified register|
52+
|**writeData(...)** | An overloaded set of methods to write data directly to the device|
53+
54+
Additionally a set of explicitly named methods for different core types are provided.
7955

8056
> [!NOTE]
8157
> 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.
8258
83-
The Inteface diagram for the ```sfeTkIBus``` is:
84-
85-
![IIBus Interface](images/tk_uml_ibus.png)
59+
Specific details for this class are detailed [here](https://docs.sparkfun.com/SparkFun_Toolkit/classsf_tk_i_bus.html)
8660

87-
### The sfeTkII2C Implementation
61+
### The sfTkII2C Implementation
8862

89-
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.
63+
This class sub-classes from the ```sfTkIBus``` 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.
9064

9165
| Method| Definition |
9266
|------|-------|
@@ -95,15 +69,13 @@ This class sub-classes from the ```sfeTkIBus``` interface adding additional func
9569
|**address** | Returns the address used by this I2C object |
9670

9771
> [!NOTE]
98-
> The ```sfeTkII2C``` class manages the device address for the I2C bus. As such, each I2C device instantiates/uses an instance of the ```sfeTkII2C``` class.
72+
> The ```sfTkII2C``` class manages the device address for the I2C bus. As such, each I2C device instantiates/uses an instance of the ```sfTkII2C``` class.
9973
100-
The class diagram for the ```sfeTkII2C``` interface is the following:
101-
102-
![II2C Class Diagram](images/tk_uml_ii2c.png)
74+
The details for the ```sfeTkII2C``` interface are [here](https://docs.sparkfun.com/SparkFun_Toolkit/classsf_tk_i_i2_c.html)
10375

10476
### The sfeTkISPI Implementation
10577

106-
This class sub-classes from the ```sfeTkIBus``` interface adding additional functionally focused on supporting an SPI implementation. This interface provides the additional functionality.
78+
This class sub-classes from the ```sfTkIBus``` interface adding additional functionally focused on supporting an SPI implementation. This interface provides the additional functionality.
10779

10880
| Method| Definition |
10981
|------|-------|
@@ -113,53 +85,47 @@ This class sub-classes from the ```sfeTkIBus``` interface adding additional func
11385
> [!NOTE]
11486
> The ```sfeTkISPI``` class manages CS Pin for the SPI bus. As such, each SPI device instantiates/uses an instance of the ```sfeTkISPI``` class.
11587
116-
The class diagram of these base class interfaces/implementation:
117-
118-
![ISPI Class Diagram](images/tk_uml_ispi.png)
88+
The details for the ```sfTkII2C``` interface are [here](https://docs.sparkfun.com/SparkFun_Toolkit/classsf_tk_i_s_p_i.html)
11989

120-
## sfeTkIBus - Arduino Implementation
90+
## sfTkIBus - Arduino Implementation
12191

122-
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.
92+
The initial implementation of the toolkit IBus interface is for the Arduino environment. This implementation consists of two classes, ```sfTkArdI2C``` and ```sfTkArdSPI```, each of which sub-class from their respective bus type interfaces within the core toolkit.
12393

12494
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.
12595

12696
> [!IMPORTANT]
12797
> The intent is that each user of an particular - a device in most cases - contains an instance of the specific bus class.
12898
129-
### The sfeTkArdI2C Class
130-
131-
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.
99+
### The sfTkArdI2C Class
132100

133-
The class diagram for the sfeTkArdI2C class:
101+
This class provides the Arduino implementation of I2C in the SparkFun Toolkit. It implements the methods of the ```sfTkIBus``` and ```sfTkII2C``` interfaces, as well as manages any Arduino specific state.
134102

135-
![Arduino I2C Class Diagram](images/tk_uml_ardi2c.png)
103+
Details for this class are located [here](https://docs.sparkfun.com/SparkFun_Toolkit/classsf_tk_ard_i2_c.html)
136104

137-
### The sfeTkArdSPI Class
105+
### The sfTkArdSPI Class
138106

139-
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.
107+
This class provides the Arduino implementation of SPI in the SparkFun Toolkit. It implements the methods of the ```sfTkIBus``` and ```sfTkISPI``` interfaces, as well as manages any Arduino specific state for the SPI bus - namely the SPISettings class.
140108

141-
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.
109+
Before each use of the SPI bus, the methods of the ```sfTkArdSPI``` uses an internal SPISettings class to ensure the SPI bus is operating in the desired mode for the device.
142110

143-
The class diagram for the sfeTkArdSPI class:
111+
Details for this class are located [here](https://docs.sparkfun.com/SparkFun_Toolkit/classsf_tk_ard_s_p_i.html)
144112

145-
![Arduino SPI Class Diagram](images/tk_uml_ardspi.png)
113+
## sfTkIBus Use
146114

147-
## sfeTkIBus Use
148-
149-
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.
115+
The general steps when using the sfTkIBus in device development are outlined in the following steps. This example uses the Arduino implementation of the bus.
150116

151117
The general pattern for a device driver implementation that uses the SparkFun Toolkit is the following:
152118

153119
### Implement a Platform Independent Driver
154120

155-
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.
121+
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 ```sfTkIBus``` interface. By limiting use to the IBus interface, the core implementation can use any bus type or platform that implements the sfeTkIBus interface.
156122

157123
> [!IMPORTANT]
158-
> At this level, the driver is only using a ```sfeTkIBus``` interface, not any specific bus implementation.
124+
> At this level, the driver is only using a ```sfTkIBus``` interface, not any specific bus implementation.
159125
160126
This driver has the following unique functionality:
161127

162-
1) A method to set the object that implements the ```sfeTkIBus``` interface object should use. Since
128+
1) A method to set the object that implements the ```sfTkIBus``` interface object should use or sets the bus in a ```begin()``` method.
163129
1) If the device supports identification capabilities, the driver provides this functionality.
164130

165131
#### Simple Example of an Independent Driver Implementation
@@ -175,36 +141,42 @@ class myDriverClass
175141
{
176142
public:
177143

178-
myDriverClass(uint8_t address) : _addr{address}, _theBus{nullptr}{}
144+
myDriverClass() : _theBus{nullptr}{}
179145

180-
bool begin()
146+
sfTkError_t begin(sfTkBus *theBus = nullptr)
181147
{
182148
// initialize things ...
183149

184-
return true;
185-
}
186-
void setCommunicationBus(sfeTkIBus *theBus)
187-
{
150+
if (_theBus == nullptr)
151+
return ksfTkErrFail;
152+
188153
_theBus = theBus;
154+
return ksfTkErrOk;
189155
}
190156

191-
bool updateDeviceData(uint8_t *data, size_t len)
157+
sfTkError_t updateDeviceData(uint8_t *data, size_t len)
192158
{
193159
if (!_theBus || !data || len == 0)
194-
return false;
160+
return ksfTkErrFail;
195161

196-
sfeTkError_t status = _theBus->writeRegisterRegion(THE_REG, data, len);
162+
return _theBus->writeRegister(THE_REG, data, len);
197163

198164
return (status == kSTkErrOk);
199165
}
200166

201-
bool checkDeviceID()
167+
sfTkError_t checkDeviceID()
202168
{
203169
// do some device ID checks in registers ...etc
204-
return true;
170+
return kSTkErrOk;
205171
}
172+
// and additional methods for the driver ..
173+
sfTkError_t doSomethingA(int32_t);
174+
sfTkError_t getValueA(uint32_t&);
175+
sfTkError_t setPropertyB(double);
176+
177+
206178
private:
207-
uint8_t _addr;
179+
208180
sfeTkIBus *_theBus;
209181
};
210182
```
@@ -236,29 +208,27 @@ class myArduinoDriverI2C : public myDriverClass
236208
myArduinoDriverI2C()
237209
{}
238210
239-
bool begin()
211+
bool begin(const uint8_t address = SF_BMV080_DEFAULT_ADDRESS, TwoWire &wirePort = Wire)
240212
{
241-
if (_theI2CBus.init(MY_DEVICE_ADDRESS) != kSTkErrOk)
213+
if (_theI2CBus.init(wirePOrt, MY_DEVICE_ADDRESS) != ksfTkErrOk)
242214
return false;
243215
244216
// OPTIONAL: If your device supports repeat starts.
245217
_theI2CBus.setStop(false);
246218
247-
setCommunicationBus(&_theI2CBus);
248-
249-
return myDriverClass::begin();
219+
return myDriverClass::begin(&_theI2CBus);
250220
}
251221
252222
bool isConnected()
253223
{
254-
if (_theI2CBus.ping() != kSTkErrOk)
224+
if (_theI2CBus.ping() != ksfTkErrOk)
255225
return false;
256226
257-
return checkDeviceID();
227+
return checkDeviceID() == kstTkErrOk;
258228
}
259229
260230
private:
261-
sfeTkArdI2C _theI2CBus;
231+
sfTkArdI2C _theI2CBus;
262232
};
263233
```
264234

@@ -277,27 +247,30 @@ class myArduinoDriveSPI : public myDriverClass
277247
myArduinoDriverSPI()
278248
{}
279249

280-
bool begin()
250+
bool begin(uint8_t csPin, SPIClass &spiPort = SPI,
251+
SPISettings spiSettings = SPISettings(4000000, MSBFIRST, SPI_MODE0))
252+
281253
{
282-
SPISettings spiSettings = SPISettings(4000000, MSBFIRST, SPI_MODE3);
283254

284-
if (_theSPIBus.init(SPI, spiSettings, MY_DEFAULT_CS, true) != kSTkErrOk)
255+
if (_theSPIBus.init(spiPort, spiSettings, csPin, true) != ksfTkErrOk)
285256
return false;
286-
setCommunicationBus(&_theSPIBus);
287257

288-
return myDriverClass::begin();
258+
return myDriverClass::begin(&_theSPIBus) == ksfTkErrOk;
289259
}
290260

291261
bool isConnected()
292262
{
293-
return checkDeviceID();
263+
return checkDeviceID() == ksfTkErrOk;
294264
}
295265

296266
private:
297-
sfeTkArdSPI _theSPIBus;
267+
sfTkArdSPI _theSPIBus;
298268
};
299269
```
300270

271+
> [!IMPORTANT]
272+
>The I2C and SPI versions of the driver just manage the specific bus setup and initialization - beyond that, the core class, which ***only*** operates using the bus interface class forms the core of both implementations.
273+
301274
## Summary
302275

303276
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:

0 commit comments

Comments
 (0)