mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-07-26 06:38:42 +00:00
machine/samd21: use pins specified in I2CConfig
Instead of configuring machine.I2C0, machine.I2C1, etc. statically, allow the pins to be set using machine.I2CConfig. This will also automatically configure the correct pin mode for each pin instead of having to specify that manually.
This commit is contained in:
committed by
Ron Evans
parent
8fe126b1f6
commit
d266e44bc5
@@ -110,10 +110,10 @@ const (
|
||||
|
||||
// I2C on the Arduino Nano 33.
|
||||
var (
|
||||
I2C0 = I2C{Bus: sam.SERCOM4_I2CM,
|
||||
SDA: SDA_PIN,
|
||||
SCL: SCL_PIN,
|
||||
PinMode: PinSERCOMAlt}
|
||||
I2C0 = I2C{
|
||||
Bus: sam.SERCOM4_I2CM,
|
||||
SERCOM: 4,
|
||||
}
|
||||
)
|
||||
|
||||
// SPI pins
|
||||
|
||||
@@ -94,15 +94,15 @@ const (
|
||||
// I2C on the Circuit Playground Express.
|
||||
var (
|
||||
// external device
|
||||
I2C0 = I2C{Bus: sam.SERCOM5_I2CM,
|
||||
SDA: SDA_PIN,
|
||||
SCL: SCL_PIN,
|
||||
PinMode: PinSERCOM}
|
||||
I2C0 = I2C{
|
||||
Bus: sam.SERCOM5_I2CM,
|
||||
SERCOM: 5,
|
||||
}
|
||||
// internal device
|
||||
I2C1 = I2C{Bus: sam.SERCOM1_I2CM,
|
||||
SDA: SDA1_PIN,
|
||||
SCL: SCL1_PIN,
|
||||
PinMode: PinSERCOMAlt}
|
||||
I2C1 = I2C{
|
||||
Bus: sam.SERCOM1_I2CM,
|
||||
SERCOM: 1,
|
||||
}
|
||||
)
|
||||
|
||||
// SPI pins (internal flash)
|
||||
|
||||
@@ -73,10 +73,10 @@ const (
|
||||
|
||||
// I2C on the Feather M0.
|
||||
var (
|
||||
I2C0 = I2C{Bus: sam.SERCOM3_I2CM,
|
||||
SDA: SDA_PIN,
|
||||
SCL: SCL_PIN,
|
||||
PinMode: PinSERCOM}
|
||||
I2C0 = I2C{
|
||||
Bus: sam.SERCOM3_I2CM,
|
||||
SERCOM: 3,
|
||||
}
|
||||
)
|
||||
|
||||
// SPI pins
|
||||
|
||||
@@ -75,10 +75,10 @@ const (
|
||||
|
||||
// I2C on the ItsyBitsy M0.
|
||||
var (
|
||||
I2C0 = I2C{Bus: sam.SERCOM3_I2CM,
|
||||
SDA: SDA_PIN,
|
||||
SCL: SCL_PIN,
|
||||
PinMode: PinSERCOM}
|
||||
I2C0 = I2C{
|
||||
Bus: sam.SERCOM3_I2CM,
|
||||
SERCOM: 3,
|
||||
}
|
||||
)
|
||||
|
||||
// SPI pins
|
||||
|
||||
@@ -79,10 +79,10 @@ const (
|
||||
|
||||
// I2C on the Trinket M0.
|
||||
var (
|
||||
I2C0 = I2C{Bus: sam.SERCOM2_I2CM,
|
||||
SDA: SDA_PIN,
|
||||
SCL: SCL_PIN,
|
||||
PinMode: PinSERCOMAlt}
|
||||
I2C0 = I2C{
|
||||
Bus: sam.SERCOM2_I2CM,
|
||||
SERCOM: 2,
|
||||
}
|
||||
)
|
||||
|
||||
// I2S pins
|
||||
|
||||
@@ -5,6 +5,8 @@ import "errors"
|
||||
var (
|
||||
ErrInvalidInputPin = errors.New("machine: invalid input pin")
|
||||
ErrInvalidOutputPin = errors.New("machine: invalid output pin")
|
||||
ErrInvalidClockPin = errors.New("machine: invalid clock pin")
|
||||
ErrInvalidDataPin = errors.New("machine: invalid data pin")
|
||||
)
|
||||
|
||||
type PinConfig struct {
|
||||
|
||||
@@ -519,10 +519,8 @@ func defaultUART1Handler() {
|
||||
|
||||
// I2C on the SAMD21.
|
||||
type I2C struct {
|
||||
Bus *sam.SERCOM_I2CM_Type
|
||||
SCL Pin
|
||||
SDA Pin
|
||||
PinMode PinMode
|
||||
Bus *sam.SERCOM_I2CM_Type
|
||||
SERCOM uint8
|
||||
}
|
||||
|
||||
// I2CConfig is used to store config info for I2C.
|
||||
@@ -552,11 +550,30 @@ const (
|
||||
const i2cTimeout = 1000
|
||||
|
||||
// Configure is intended to setup the I2C interface.
|
||||
func (i2c I2C) Configure(config I2CConfig) {
|
||||
func (i2c I2C) Configure(config I2CConfig) error {
|
||||
// Default I2C bus speed is 100 kHz.
|
||||
if config.Frequency == 0 {
|
||||
config.Frequency = TWI_FREQ_100KHZ
|
||||
}
|
||||
if config.SDA == 0 && config.SCL == 0 {
|
||||
config.SDA = SDA_PIN
|
||||
config.SCL = SCL_PIN
|
||||
}
|
||||
|
||||
sclPinMode, sclPad, ok := findPinPadMapping(i2c.SERCOM, config.SCL)
|
||||
if !ok || sclPad != 1 {
|
||||
// SCL must be on pad 1, according to section 27.5 of the datasheet.
|
||||
// Note: this is not an exhaustive test for I2C support on the pin: not
|
||||
// all pins support I2C.
|
||||
return ErrInvalidClockPin
|
||||
}
|
||||
sdaPinMode, sdaPad, ok := findPinPadMapping(i2c.SERCOM, config.SDA)
|
||||
if !ok || sdaPad != 0 {
|
||||
// SDA must be on pad 0, according to section 27.5 of the datasheet.
|
||||
// Note: this is not an exhaustive test for I2C support on the pin: not
|
||||
// all pins support I2C.
|
||||
return ErrInvalidDataPin
|
||||
}
|
||||
|
||||
// reset SERCOM
|
||||
i2c.Bus.CTRLA.SetBits(sam.SERCOM_I2CM_CTRLA_SWRST)
|
||||
@@ -582,8 +599,10 @@ func (i2c I2C) Configure(config I2CConfig) {
|
||||
}
|
||||
|
||||
// enable pins
|
||||
i2c.SDA.Configure(PinConfig{Mode: i2c.PinMode})
|
||||
i2c.SCL.Configure(PinConfig{Mode: i2c.PinMode})
|
||||
config.SDA.Configure(PinConfig{Mode: sdaPinMode})
|
||||
config.SCL.Configure(PinConfig{Mode: sclPinMode})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetBaudRate sets the communication speed for the I2C.
|
||||
|
||||
Reference in New Issue
Block a user