-
Notifications
You must be signed in to change notification settings - Fork 200
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
Initial support for PCA9548A 8-channel I2C-bus #644
Open
conejoninja
wants to merge
4
commits into
tinygo-org:dev
Choose a base branch
from
conejoninja:pca9548a
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
cf5ad2f
Initial support for PCA9548A 8-channel I2C-bus
conejoninja 7c1774e
Merge branch 'dev' into pca9548a
conejoninja 25450cf
added InvalidPort const
conejoninja dcfbdc0
Merge branch 'pca9548a' of github.com:conejoninja/drivers into pca9548a
conejoninja File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package main | ||
|
||
import ( | ||
"machine" | ||
"time" | ||
"tinygo.org/x/drivers/bme280" | ||
"tinygo.org/x/drivers/pca9548a" | ||
) | ||
|
||
func main() { | ||
time.Sleep(5 * time.Second) | ||
err := machine.I2C0.Configure(machine.I2CConfig{}) | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
mux := pca9548a.New(machine.I2C0, pca9548a.Address) | ||
if !mux.IsConnected() { | ||
println("NO DEVICE DETECTED") | ||
return | ||
} | ||
|
||
port := mux.GetPortState() | ||
println("GET PORT", port) | ||
mux.DisablePort() | ||
mux.SetPort(0) | ||
port = mux.GetPortState() | ||
println("GET PORT", port) | ||
mux.SetPort(1) | ||
port = mux.GetPortState() | ||
println("GET PORT", port) | ||
|
||
tmpSensors := make([]bme280.Device, 2) | ||
|
||
for i := uint8(0); i < 2; i++ { | ||
mux.SetPort(i) | ||
time.Sleep(10 * time.Millisecond) | ||
tmpSensors[i] = bme280.New(machine.I2C0) | ||
tmpSensors[i].Configure() | ||
|
||
connected := tmpSensors[i].Connected() | ||
if !connected { | ||
println("\nBME280 Sensor not detected\n", i) | ||
} else { | ||
println("\nBME280 Sensor detected\n", i) | ||
} | ||
} | ||
time.Sleep(10000 * time.Millisecond) | ||
|
||
for { | ||
for i := uint8(0); i < 2; i++ { | ||
mux.SetPort(i) | ||
t, err := tmpSensors[i].ReadTemperature() | ||
if err != nil { | ||
println(i, "Error reading temperature") | ||
} | ||
println(i, "temperature", t) | ||
|
||
p, err := tmpSensors[i].ReadPressure() | ||
if err != nil { | ||
println("Error reading pressure") | ||
} | ||
println(i, "pressure", p) | ||
|
||
} | ||
time.Sleep(40 * time.Millisecond) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Package pca9548a provides a driver for the PCA9548A 8-channel I2C-bus. | ||
// | ||
// Datasheet: | ||
// https://www.nxp.com/docs/en/data-sheet/PCA9548A.pdf | ||
package pca9548a // import "tinygo.org/x/drivers/pca9548a" | ||
|
||
import ( | ||
"tinygo.org/x/drivers" | ||
) | ||
|
||
// Device is a handle to the PCA9548A device given an address | ||
type Device struct { | ||
addr uint8 | ||
bus drivers.I2C | ||
buf [4]byte | ||
} | ||
|
||
// New creates a new instance of a PCA9548A device. It performs | ||
// no IO on the i2c bus. | ||
func New(bus drivers.I2C, addr uint8) Device { | ||
return Device{ | ||
bus: bus, | ||
addr: addr, | ||
} | ||
} | ||
|
||
// Connected returns whether a PCA9548A has been found. | ||
// It does a "who am I" request and checks the response. | ||
func (d *Device) IsConnected() bool { | ||
d.SetPortState(0xA5) | ||
response := d.GetPortState() | ||
d.SetPortState(0x00) | ||
return response == 0xA5 | ||
} | ||
|
||
// SetPort enables the given port to send data. | ||
func (d *Device) SetPort(portNumber byte) { | ||
portValue := uint8(0) | ||
if portNumber <= 7 { | ||
portValue = 1 << portNumber | ||
} | ||
d.bus.Tx(uint16(d.addr), []byte{portValue}, nil) | ||
} | ||
|
||
// GetPort gets the first port enabled. | ||
func (d *Device) GetPort() byte { | ||
portBits := d.GetPortState() | ||
for i := uint8(0); i < 8; i++ { | ||
if (portBits & (1 << i)) != 0x00 { | ||
return i | ||
} | ||
} | ||
return 99 | ||
} | ||
|
||
// SetPortState set the states of all the ports at the same time, this could cause some issues if | ||
// you enable two (or more) ports with the same devices at the same time. | ||
func (d *Device) SetPortState(portBits byte) { | ||
d.bus.Tx(uint16(d.addr), []byte{portBits}, nil) | ||
} | ||
|
||
// GetPortState get the state of all the ports. | ||
func (d *Device) GetPortState() byte { | ||
portBits := make([]byte, 1) | ||
d.bus.Tx(uint16(d.addr), nil, portBits) | ||
return portBits[0] | ||
} | ||
|
||
// EnablePort enables the given port without modifying any other, this could cause some issues if | ||
// you enable two (or more) ports with the same devices at the same time. | ||
func (d *Device) EnablePort(portNumber byte) { | ||
if portNumber > 7 { | ||
portNumber = 7 | ||
} | ||
|
||
settings := d.GetPortState() | ||
settings |= 1 << portNumber | ||
|
||
d.SetPortState(settings) | ||
} | ||
|
||
// DisablePort disables the given port without modifying any other. | ||
func (d *Device) DisablePort() byte { | ||
portBits := make([]byte, 1) | ||
d.bus.Tx(uint16(d.addr), nil, portBits) | ||
return portBits[0] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package pca9548a | ||
|
||
const ( | ||
Address = 0x70 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
99
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't sure what value return ... that should be in case no port is enable. 0 is the first port and can not be used. Any value >= 16 is an invalid port and could be used.
Other idea is to return (port byte, err error) but again, a value must be used when error 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In that case how about:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added, thanks