Skip to content

Commit c01a08c

Browse files
committed
Initial commit
0 parents  commit c01a08c

File tree

8 files changed

+413
-0
lines changed

8 files changed

+413
-0
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

LICENSE.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
SparkFun License Information
2+
============================
3+
4+
SparkFun uses two different licenses for our files — one for hardware and one for code.
5+
6+
Hardware
7+
---------
8+
9+
**SparkFun hardware is released under [Creative Commons Share-alike 4.0 International](http://creativecommons.org/licenses/by-sa/4.0/).**
10+
11+
Note: This is a human-readable summary of (and not a substitute for) the [license](http://creativecommons.org/licenses/by-sa/4.0/legalcode).
12+
13+
You are free to:
14+
15+
Share — copy and redistribute the material in any medium or format
16+
Adapt — remix, transform, and build upon the material
17+
for any purpose, even commercially.
18+
The licensor cannot revoke these freedoms as long as you follow the license terms.
19+
Under the following terms:
20+
21+
Attribution — You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
22+
ShareAlike — If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original.
23+
No additional restrictions — You may not apply legal terms or technological measures that legally restrict others from doing anything the license permits.
24+
Notices:
25+
26+
You do not have to comply with the license for elements of the material in the public domain or where your use is permitted by an applicable exception or limitation.
27+
No warranties are given. The license may not give you all of the permissions necessary for your intended use. For example, other rights such as publicity, privacy, or moral rights may limit how you use the material.
28+
29+
30+
Code
31+
--------
32+
33+
**SparkFun code, firmware, and software is released under the MIT License(http://opensource.org/licenses/MIT).**
34+
35+
The MIT License (MIT)
36+
37+
Copyright (c) 2016 SparkFun Electronics
38+
39+
Permission is hereby granted, free of charge, to any person obtaining a copy
40+
of this software and associated documentation files (the "Software"), to deal
41+
in the Software without restriction, including without limitation the rights
42+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
43+
copies of the Software, and to permit persons to whom the Software is
44+
furnished to do so, subject to the following conditions:
45+
46+
The above copyright notice and this permission notice shall be included in all
47+
copies or substantial portions of the Software.
48+
49+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
50+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
51+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
52+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
53+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
54+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
55+
SOFTWARE.

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
SparkFun Qwiic I2C Mux Arduino Library
2+
===========================================================
3+
4+
[![SparkFun Qwiic Mux - TCA9548 (BOB-14685)](https://cdn.sparkfun.com//assets/parts/1/2/8/9/0/14685-SparkFun_Qwiic_Mux_Breakout_-_8_Channel__TCA9548A_-01.jpg)](https://www.sparkfun.com/products/15242)
5+
6+
[*SparkFun Qwiic Mux - TCA9548 (BOB-14685)*](https://www.sparkfun.com/products/14685)
7+
8+
Do you have too many sensors with the same I<sup>2</sup>C address? Put them on the SparkFun Qwiic Mux Breakout to get them all talking on the same bus! The Qwiic Mux Breakout enables communication with multiple I<sup>2</sup>C devices that have the same address that makes it simple to interface with. The Qwiic Mux also has eight configurable addresses of its own, allowing for up to 64 I<sup>2</sup>C buses on a connection. To make it even easier to use this multiplexer, all communication is enacted exclusively via I<sup>2</sup>C, utilizing our handy Qwiic system.
9+
10+
The Qwiic Mux also allows you to change the last three bits of the address byte, allowing for eight jumper selectable addresses if you happen to need to put more than one Qwiic Mux Breakout on the same I<sup>2</sup>C port. The address can be changed by adding solder to any of the three ADR jumpers. Each SparkFun Qwiic Mux Breakout operate between 1.65V and 5.5V making it ideal for all of the Qwiic boards we produce in house.
11+
12+
SparkFun labored with love to create this code. Feel like supporting open source hardware?
13+
Buy a [board](https://www.sparkfun.com/products/14685) from SparkFun!
14+
15+
Repository Contents
16+
-------------------
17+
18+
* **/examples** - Example sketches for the library (.ino). Run these from the Arduino IDE.
19+
* **/src** - Source files for the library (.cpp, .h).
20+
* **keywords.txt** - Keywords from this library that will be highlighted in the Arduino IDE.
21+
* **library.properties** - General library properties for the Arduino package manager.
22+
23+
Documentation
24+
--------------
25+
26+
* **[Installing an Arduino Library Guide](https://learn.sparkfun.com/tutorials/installing-an-arduino-library)** - Basic information on how to install an Arduino library.
27+
* **[Product Repository](https://github.com/sparkfun/Qwiic_Mux_TCA9548A)** - Main repository (including hardware files)
28+
29+
License Information
30+
-------------------
31+
32+
This product is _**open source**_!
33+
34+
Please use, reuse, and modify these files as you see fit. Please maintain attribution to SparkFun Electronics and release anything derivative under the same license.
35+
36+
Distributed as-is; no warranty is given.
37+
38+
- Your friends at SparkFun.
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
Use the Qwiic Mux to access multiple I2C devices on seperate busses.
3+
By: Nathan Seidle @ SparkFun Electronics
4+
Date: May 17th, 2020
5+
License: This code is public domain but you buy me a beer if you use this
6+
and we meet someday (Beerware license).
7+
8+
Some I2C devices respond to only one I2C address. This can be a problem
9+
when you want to hook multiple of a device to the I2C bus. An I2C Mux
10+
solves this issue by allowing you to change the 'channel' or port that
11+
a device is connected to on the mux.
12+
13+
This example shows how to hook up two MMA8452Q accelerometers with the same address.
14+
You can read the MMA8452Q hookup guide and get the library from https://learn.sparkfun.com/tutorials/mma8452q-accelerometer-breakout-hookup-guide
15+
16+
The TCA9548A is a mux. This means when you enableMuxPort(2) then the SDA and SCL lines of the master (Arduino)
17+
are connected to port 2. Whatever I2C traffic you do, such as accel.init() will be communicated to whatever
18+
sensor you have on port 2. You don't need accel2.init() or accel5.init().
19+
20+
Outputs two sets of XYZ acceleration
21+
22+
Hardware Connections:
23+
Attach the Qwiic Mux Shield to your RedBoard or Uno.
24+
Plug two Qwiic MMA8452Q breakout boards into ports 0 and 1.
25+
Serial.print it out at 9600 baud to serial monitor.
26+
27+
SparkFun labored with love to create this code. Feel like supporting open
28+
source? Buy a board from SparkFun!
29+
https://www.sparkfun.com/products/14685
30+
31+
*/
32+
33+
#include <Wire.h>
34+
35+
TwoWire Wire1(1); //Will use pads 8/9
36+
37+
#include <SparkFun_I2C_Mux_Arduino_Library.h> //Click here to get the library: http://librarymanager/All#SparkFun_I2C_Mux
38+
QWIICMUX myMux;
39+
40+
#include <SparkFun_MMA8452Q.h> //Click here to get the library: http://librarymanager/All#SparkFun_MMA8452
41+
MMA8452Q accel;
42+
43+
#define NUMBER_OF_SENSORS 2
44+
45+
void setup()
46+
{
47+
Serial.begin(115200);
48+
Serial.println("Qwiic Mux Shield Read Example");
49+
50+
Wire1.begin();
51+
if (myMux.begin(0x70, Wire1) == false)
52+
{
53+
Serial.println("Mux not detected. Freezing...");
54+
while (1)
55+
;
56+
}
57+
Serial.println("Mux detected");
58+
59+
//myMux.setPort(8);
60+
byte currentPortNumber = myMux.getPort();
61+
Serial.print("CurrentPort: ");
62+
Serial.println(currentPortNumber);
63+
64+
while (1)
65+
;
66+
67+
//Initialize all the sensors
68+
for (byte x = 0; x < NUMBER_OF_SENSORS; x++)
69+
{
70+
myMux.setPort(x); //Tell mux to connect to port X
71+
accel.init(); //Init the sensor connected to this port
72+
}
73+
74+
Serial.println("Mux Shield online");
75+
}
76+
77+
void loop()
78+
{
79+
for (byte x = 0; x < NUMBER_OF_SENSORS; x++)
80+
{
81+
myMux.setPort(x); //Tell mux to connect to this port, and this port only
82+
83+
if (accel.available())
84+
{
85+
accel.read();
86+
87+
Serial.print("Accel ");
88+
Serial.print(x);
89+
Serial.print(": ");
90+
Serial.print(accel.cx, 2);
91+
Serial.print(" ");
92+
Serial.print(accel.cy, 2);
93+
Serial.print(" ");
94+
Serial.print(accel.cz, 2);
95+
Serial.print(" ");
96+
97+
Serial.println(); // Print new line every time.
98+
}
99+
}
100+
101+
delay(1); //Wait for next reading
102+
}

keywords.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#######################################
2+
# Syntax Coloring Map
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
QWIICMUX KEYWORD1
10+
11+
#######################################
12+
# Methods and Functions (KEYWORD2)
13+
#######################################
14+
15+
begin KEYWORD2
16+
isConnected KEYWORD2
17+
setPort KEYWORD2
18+
setPortState KEYWORD2
19+
getPort KEYWORD2
20+
getPortState KEYWORD2
21+
enablePort KEYWORD2
22+
disablePort KEYWORD2
23+
24+
#######################################
25+
# Constants (LITERAL1)
26+
#######################################
27+

library.properties

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=SparkFun I2C Mux Arduino Library
2+
version=1.0.0
3+
author=SparkFun Electronics
4+
maintainer=SparkFun Electronics
5+
sentence=Library to control I2C multiplexers including the TCA9548/PCA9548.
6+
paragraph=I2C multiplexers are useful for connecting multiple I2C devices that have only one address. This library makes it easy to work with the 8-channel TCA9548/PCA9548 I2C multiplexer but also works with smaller 4 and 2 bit multiplexers. This library support daisychaining multiple muxes so that you can get up to 64 devices on one I2C bus! It also supports generic Wire ports (Wire1, myWire, etc). Checkout the <a href="https://www.sparkfun.com/products/14685">Qwiic Mux</a> for more information.
7+
category=Device Control
8+
url=https://github.com/sparkfun/SparkFun_I2C_Mux_Arduino_Library
9+
architectures=*
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/*
2+
This is an Arduino library written for the TCA9548A/PCA9548A 8-bit multiplexer.
3+
By Nathan Seidle @ SparkFun Electronics, May 16th, 2020
4+
5+
The TCA9548A/PCA9548A allows for up to 8 devices to be attached to a single
6+
I2C bus. This is helpful for I2C devices that have a single I2C address.
7+
8+
https://github.com/sparkfun/SparkFun_I2C_Mux_Arduino_Library
9+
10+
SparkFun labored with love to create this code. Feel like supporting open
11+
source? Buy a board from SparkFun!
12+
https://www.sparkfun.com/products/14685
13+
*/
14+
15+
#include "SparkFun_I2C_Mux_Arduino_Library.h"
16+
17+
//Sets up the Mux for basic function
18+
//Returns true if device responded correctly. All ports will be disabled.
19+
bool QWIICMUX::begin(uint8_t deviceAddress, TwoWire &wirePort)
20+
{
21+
//Get user's options
22+
_i2cPort = &wirePort;
23+
_deviceAddress = deviceAddress;
24+
25+
//Valid addresses for the TCA9548 are 0x70 to 0x77.
26+
//We don't check the validity of the address. There may be other I2C mux's outside of this range.
27+
28+
//Check if the device ack's over I2C
29+
if (isConnected() == false)
30+
return (false);
31+
return (true);
32+
}
33+
34+
//Returns true if device is present
35+
//Tests for device ack to I2C address
36+
//Then tests if device behaves as we expect
37+
//Leaves with all ports disabled
38+
bool QWIICMUX::isConnected()
39+
{
40+
_i2cPort->beginTransmission(_deviceAddress);
41+
if (_i2cPort->endTransmission() != 0)
42+
return (false); //Device did not ACK
43+
44+
//Write to device, expect a return
45+
setPortState(0xA4); //Set port register to a known value
46+
uint8_t response = getPortState();
47+
setPortState(0x00); //Disable all ports
48+
if (response == 0xA4) //Make sure we got back what we expected
49+
return (true); //All good
50+
return (false);
51+
}
52+
53+
//Enables one port. Disables all others.
54+
//If port number if out of range, disable all ports
55+
bool QWIICMUX::setPort(uint8_t portNumber)
56+
{
57+
uint8_t portValue = 0;
58+
59+
if (portNumber > 7)
60+
portValue = 0; //If port number is out of range, turn off all ports
61+
else
62+
portValue = 1 << portNumber;
63+
64+
_i2cPort->beginTransmission(_deviceAddress);
65+
_i2cPort->write(portValue);
66+
if (_i2cPort->endTransmission() != 0)
67+
return (false); //Device did not ACK
68+
return (true);
69+
}
70+
71+
//Returns the first port number bit that is set
72+
//Returns 255 if no port is enabled
73+
uint8_t QWIICMUX::getPort()
74+
{
75+
//Read the current mux settings
76+
_i2cPort->beginTransmission(_deviceAddress);
77+
_i2cPort->requestFrom(_deviceAddress, 1);
78+
if (!_i2cPort->available())
79+
return (255); //Error
80+
uint8_t portBits = _i2cPort->read();
81+
82+
//Search for the first set bit, then return its location
83+
for (uint8_t x = 0; x < 8; x++)
84+
{
85+
if (portBits & (1 << x))
86+
return (x);
87+
}
88+
return (255); //Return no port set
89+
}
90+
91+
//Writes a 8-bit value to mux
92+
//Overwrites any other bits
93+
//This allows us to enable/disable multiple ports at same time
94+
bool QWIICMUX::setPortState(uint8_t portBits)
95+
{
96+
_i2cPort->beginTransmission(_deviceAddress);
97+
_i2cPort->write(portBits);
98+
if (_i2cPort->endTransmission() != 0)
99+
return (false); //Device did not ACK
100+
return (true);
101+
}
102+
103+
//Gets the current port state
104+
//Returns byte that may have multiple bits set
105+
uint8_t QWIICMUX::getPortState()
106+
{
107+
//Read the current mux settings
108+
_i2cPort->beginTransmission(_deviceAddress);
109+
_i2cPort->requestFrom(_deviceAddress, 1);
110+
return (_i2cPort->read());
111+
}
112+
113+
//Enables a specific port number
114+
//This allows for multiple ports to be 'turned on' at the same time. Use with caution.
115+
bool QWIICMUX::enablePort(uint8_t portNumber)
116+
{
117+
if (portNumber > 7)
118+
portNumber = 7; //Error check
119+
120+
//Read the current mux settings
121+
uint8_t settings = getPortState();
122+
123+
//Set the wanted bit to enable the port
124+
settings |= (1 << portNumber);
125+
126+
return (setPortState(settings));
127+
}
128+
129+
//Disables a specific port number
130+
bool QWIICMUX::disablePort(uint8_t portNumber)
131+
{
132+
if (portNumber > 7)
133+
portNumber = 7; //Error check
134+
135+
uint8_t settings = getPortState();
136+
137+
//Clear the wanted bit to disable the port
138+
settings &= ~(1 << portNumber);
139+
140+
return (setPortState(settings));
141+
}

0 commit comments

Comments
 (0)