machine: make machine.I2C0 and similar objects pointers

This makes it possible to assign I2C objects (machine.I2C0,
machine.I2C1, etc.) without needing to take a pointer.

This is important especially in the future when I2C may be driven using
DMA and the machine.I2C type needs to store some state.
This commit is contained in:
Ayke van Laethem
2021-02-06 12:37:15 +01:00
committed by Ron Evans
parent 71bbe93ab2
commit 90b42799a2
41 changed files with 129 additions and 136 deletions
+8 -8
View File
@@ -669,7 +669,7 @@ const (
const i2cTimeout = 1000
// Configure is intended to setup the I2C interface.
func (i2c I2C) Configure(config I2CConfig) error {
func (i2c *I2C) Configure(config I2CConfig) error {
// Default I2C bus speed is 100 kHz.
if config.Frequency == 0 {
config.Frequency = TWI_FREQ_100KHZ
@@ -725,7 +725,7 @@ func (i2c I2C) Configure(config I2CConfig) error {
}
// SetBaudRate sets the communication speed for the I2C.
func (i2c I2C) SetBaudRate(br uint32) {
func (i2c *I2C) SetBaudRate(br uint32) {
// Synchronous arithmetic baudrate, via Arduino SAMD implementation:
// SystemCoreClock / ( 2 * baudrate) - 5 - (((SystemCoreClock / 1000000) * WIRE_RISE_TIME_NANOSECONDS) / (2 * 1000));
baud := CPUFrequency()/(2*br) - 5 - (((CPUFrequency() / 1000000) * riseTimeNanoseconds) / (2 * 1000))
@@ -735,7 +735,7 @@ func (i2c I2C) SetBaudRate(br uint32) {
// Tx does a single I2C transaction at the specified address.
// It clocks out the given address, writes the bytes in w, reads back len(r)
// bytes and stores them in r, and generates a stop condition on the bus.
func (i2c I2C) Tx(addr uint16, w, r []byte) error {
func (i2c *I2C) Tx(addr uint16, w, r []byte) error {
var err error
if len(w) != 0 {
// send start/address for write
@@ -812,7 +812,7 @@ func (i2c I2C) Tx(addr uint16, w, r []byte) error {
}
// WriteByte writes a single byte to the I2C bus.
func (i2c I2C) WriteByte(data byte) error {
func (i2c *I2C) WriteByte(data byte) error {
// Send data byte
i2c.Bus.DATA.Set(data)
@@ -837,7 +837,7 @@ func (i2c I2C) WriteByte(data byte) error {
}
// sendAddress sends the address and start signal
func (i2c I2C) sendAddress(address uint16, write bool) error {
func (i2c *I2C) sendAddress(address uint16, write bool) error {
data := (address << 1)
if !write {
data |= 1 // set read flag
@@ -857,7 +857,7 @@ func (i2c I2C) sendAddress(address uint16, write bool) error {
return nil
}
func (i2c I2C) signalStop() error {
func (i2c *I2C) signalStop() error {
i2c.Bus.CTRLB.SetBits(wireCmdStop << sam.SERCOM_I2CM_CTRLB_CMD_Pos) // Stop command
timeout := i2cTimeout
for i2c.Bus.SYNCBUSY.HasBits(sam.SERCOM_I2CM_SYNCBUSY_SYSOP) {
@@ -869,7 +869,7 @@ func (i2c I2C) signalStop() error {
return nil
}
func (i2c I2C) signalRead() error {
func (i2c *I2C) signalRead() error {
i2c.Bus.CTRLB.SetBits(wireCmdRead << sam.SERCOM_I2CM_CTRLB_CMD_Pos) // Read command
timeout := i2cTimeout
for i2c.Bus.SYNCBUSY.HasBits(sam.SERCOM_I2CM_SYNCBUSY_SYSOP) {
@@ -881,7 +881,7 @@ func (i2c I2C) signalRead() error {
return nil
}
func (i2c I2C) readByte() byte {
func (i2c *I2C) readByte() byte {
for !i2c.Bus.INTFLAG.HasBits(sam.SERCOM_I2CM_INTFLAG_SB) {
}
return byte(i2c.Bus.DATA.Get())