-
Notifications
You must be signed in to change notification settings - Fork 51
mpu9250: Add i2c interface support #101
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
Merged
+156
−136
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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,148 @@ | ||
| // Copyright 2020 The Periph Authors. All rights reserved. | ||
| // Use of this source code is governed under the Apache License, Version 2.0 | ||
| // that can be found in the LICENSE file. | ||
|
|
||
| package mpu9250 | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "periph.io/x/conn/v3/gpio" | ||
| "periph.io/x/conn/v3/i2c" | ||
| "periph.io/x/conn/v3/physic" | ||
| "periph.io/x/conn/v3/spi" | ||
| "periph.io/x/conn/v3/spi/spireg" | ||
| ) | ||
|
|
||
| // DebugF the debug function type. | ||
| type DebugF func(string, ...interface{}) | ||
|
|
||
| // Transport Encapsulates the SPI transport parameters. | ||
| type Transport struct { | ||
| device spi.Conn | ||
| d *i2c.Dev | ||
| cs gpio.PinOut | ||
| debug DebugF | ||
| } | ||
|
|
||
| // NewSpiTransport Creates the SPI transport using the provided device path and chip select pin reference. | ||
| func NewSpiTransport(path string, cs gpio.PinOut) (*Transport, error) { | ||
| dev, err := spireg.Open(path) | ||
| if err != nil { | ||
| return nil, wrapf("can't open SPI %v", err) | ||
| } | ||
| conn, err := dev.Connect(1*physic.MegaHertz, spi.Mode0, 8) | ||
| if err != nil { | ||
| return nil, wrapf("can't initialize SPI %v", err) | ||
| } | ||
| return &Transport{device: conn, cs: cs, debug: noop}, nil | ||
| } | ||
|
|
||
| func NewI2cTransport(bus i2c.Bus, address uint16) (*Transport, error) { | ||
| return &Transport{d: &i2c.Dev{Bus: bus, Addr: address}, debug: noop}, nil | ||
| } | ||
|
|
||
| // EnableDebug Sets the debugging output using the local print function. | ||
| func (t *Transport) EnableDebug(f DebugF) { | ||
| t.debug = f | ||
| } | ||
|
|
||
| func (t *Transport) writeByte(address byte, value byte) error { | ||
| if t.d == nil { | ||
| return t.writeByteSPI(address, value) | ||
| } | ||
| return t.writeByteI2C(address, value) | ||
| } | ||
|
|
||
| func (t *Transport) writeByteSPI(address, value byte) error { | ||
| t.debug("write register %x value %x", address, value) | ||
| var ( | ||
| buf = [...]byte{address, value} | ||
| res [2]byte | ||
| ) | ||
| if err := t.cs.Out(gpio.Low); err != nil { | ||
| return err | ||
| } | ||
| if err := t.device.Tx(buf[:], res[:]); err != nil { | ||
| return err | ||
| } | ||
| return t.cs.Out(gpio.High) | ||
| } | ||
|
|
||
| func (t *Transport) writeByteI2C(address, value byte) error { | ||
| w := []byte{address, value} | ||
| return t.d.Tx(w, nil) | ||
| } | ||
|
|
||
| func (t *Transport) writeMaskedReg(address byte, mask byte, value byte) error { | ||
| t.debug("write masked %x, mask %x, value %x", address, mask, value) | ||
| maskedValue := mask & value | ||
| t.debug("masked value %x", maskedValue) | ||
| regVal, err := t.readByte(address) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| t.debug("current register %x", regVal) | ||
| regVal = (regVal &^ maskedValue) | maskedValue | ||
| t.debug("new value %x", regVal) | ||
| return t.writeByte(address, regVal) | ||
| } | ||
|
|
||
| func (t *Transport) readMaskedReg(address byte, mask byte) (byte, error) { | ||
| t.debug("read masked %x, mask %x", address, mask) | ||
| reg, err := t.readByte(address) | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
| t.debug("masked value %x", reg) | ||
| return reg & mask, nil | ||
| } | ||
|
|
||
| func (t *Transport) readByte(address byte) (byte, error) { | ||
| if t.d == nil { | ||
| return t.readByteSPI(address) | ||
| } | ||
| return t.readByteI2C(address) | ||
| } | ||
|
|
||
| func (t *Transport) readByteSPI(address byte) (byte, error) { | ||
| t.debug("read register %x", address) | ||
| var ( | ||
| buf = [...]byte{0x80 | address, 0} | ||
| res [2]byte | ||
| ) | ||
| if err := t.cs.Out(gpio.Low); err != nil { | ||
| return 0, err | ||
| } | ||
| if err := t.device.Tx(buf[:], res[:]); err != nil { | ||
| return 0, err | ||
| } | ||
| t.debug("register content %x:%x", res[0], res[1]) | ||
| if err := t.cs.Out(gpio.High); err != nil { | ||
| return 0, err | ||
| } | ||
| return res[1], nil | ||
| } | ||
|
|
||
| func (t *Transport) readByteI2C(address byte) (byte, error) { | ||
| r := make([]byte, 1) | ||
| err := t.d.Tx([]byte{address}, r) | ||
| return r[0], err | ||
| } | ||
|
|
||
| func (t *Transport) readUint16(address ...byte) (uint16, error) { | ||
| if len(address) != 2 { | ||
| return 0, fmt.Errorf("only 2 bytes per read") | ||
| } | ||
| h, err := t.readByte(address[0]) | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
| l, err := t.readByte(address[1]) | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
| return uint16(h)<<8 | uint16(l), nil | ||
| } | ||
|
|
||
| func noop(string, ...interface{}) {} | ||
Oops, something went wrong.
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.
Side note: we should probably standardize on the log/slog package. It's actually pretty good.