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:
Ayke van Laethem
2019-09-30 14:21:52 +02:00
committed by Ron Evans
parent 8fe126b1f6
commit d266e44bc5
7 changed files with 52 additions and 31 deletions
+4 -4
View File
@@ -110,10 +110,10 @@ const (
// I2C on the Arduino Nano 33. // I2C on the Arduino Nano 33.
var ( var (
I2C0 = I2C{Bus: sam.SERCOM4_I2CM, I2C0 = I2C{
SDA: SDA_PIN, Bus: sam.SERCOM4_I2CM,
SCL: SCL_PIN, SERCOM: 4,
PinMode: PinSERCOMAlt} }
) )
// SPI pins // SPI pins
+8 -8
View File
@@ -94,15 +94,15 @@ const (
// I2C on the Circuit Playground Express. // I2C on the Circuit Playground Express.
var ( var (
// external device // external device
I2C0 = I2C{Bus: sam.SERCOM5_I2CM, I2C0 = I2C{
SDA: SDA_PIN, Bus: sam.SERCOM5_I2CM,
SCL: SCL_PIN, SERCOM: 5,
PinMode: PinSERCOM} }
// internal device // internal device
I2C1 = I2C{Bus: sam.SERCOM1_I2CM, I2C1 = I2C{
SDA: SDA1_PIN, Bus: sam.SERCOM1_I2CM,
SCL: SCL1_PIN, SERCOM: 1,
PinMode: PinSERCOMAlt} }
) )
// SPI pins (internal flash) // SPI pins (internal flash)
+4 -4
View File
@@ -73,10 +73,10 @@ const (
// I2C on the Feather M0. // I2C on the Feather M0.
var ( var (
I2C0 = I2C{Bus: sam.SERCOM3_I2CM, I2C0 = I2C{
SDA: SDA_PIN, Bus: sam.SERCOM3_I2CM,
SCL: SCL_PIN, SERCOM: 3,
PinMode: PinSERCOM} }
) )
// SPI pins // SPI pins
+4 -4
View File
@@ -75,10 +75,10 @@ const (
// I2C on the ItsyBitsy M0. // I2C on the ItsyBitsy M0.
var ( var (
I2C0 = I2C{Bus: sam.SERCOM3_I2CM, I2C0 = I2C{
SDA: SDA_PIN, Bus: sam.SERCOM3_I2CM,
SCL: SCL_PIN, SERCOM: 3,
PinMode: PinSERCOM} }
) )
// SPI pins // SPI pins
+4 -4
View File
@@ -79,10 +79,10 @@ const (
// I2C on the Trinket M0. // I2C on the Trinket M0.
var ( var (
I2C0 = I2C{Bus: sam.SERCOM2_I2CM, I2C0 = I2C{
SDA: SDA_PIN, Bus: sam.SERCOM2_I2CM,
SCL: SCL_PIN, SERCOM: 2,
PinMode: PinSERCOMAlt} }
) )
// I2S pins // I2S pins
+2
View File
@@ -5,6 +5,8 @@ import "errors"
var ( var (
ErrInvalidInputPin = errors.New("machine: invalid input pin") ErrInvalidInputPin = errors.New("machine: invalid input pin")
ErrInvalidOutputPin = errors.New("machine: invalid output 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 { type PinConfig struct {
+26 -7
View File
@@ -519,10 +519,8 @@ func defaultUART1Handler() {
// I2C on the SAMD21. // I2C on the SAMD21.
type I2C struct { type I2C struct {
Bus *sam.SERCOM_I2CM_Type Bus *sam.SERCOM_I2CM_Type
SCL Pin SERCOM uint8
SDA Pin
PinMode PinMode
} }
// I2CConfig is used to store config info for I2C. // I2CConfig is used to store config info for I2C.
@@ -552,11 +550,30 @@ const (
const i2cTimeout = 1000 const i2cTimeout = 1000
// Configure is intended to setup the I2C interface. // 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. // Default I2C bus speed is 100 kHz.
if config.Frequency == 0 { if config.Frequency == 0 {
config.Frequency = TWI_FREQ_100KHZ 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 // reset SERCOM
i2c.Bus.CTRLA.SetBits(sam.SERCOM_I2CM_CTRLA_SWRST) i2c.Bus.CTRLA.SetBits(sam.SERCOM_I2CM_CTRLA_SWRST)
@@ -582,8 +599,10 @@ func (i2c I2C) Configure(config I2CConfig) {
} }
// enable pins // enable pins
i2c.SDA.Configure(PinConfig{Mode: i2c.PinMode}) config.SDA.Configure(PinConfig{Mode: sdaPinMode})
i2c.SCL.Configure(PinConfig{Mode: i2c.PinMode}) config.SCL.Configure(PinConfig{Mode: sclPinMode})
return nil
} }
// SetBaudRate sets the communication speed for the I2C. // SetBaudRate sets the communication speed for the I2C.