11package bmi160
22
33import (
4- "machine"
54 "time"
65
76 "tinygo.org/x/drivers"
7+ "tinygo.org/x/drivers/internal/legacy"
8+ "tinygo.org/x/drivers/internal/pin"
89)
910
1011// DeviceSPI is the SPI interface to a BMI160 accelerometer/gyroscope. There is
1112// also an I2C interface, but it is not yet supported.
1213type DeviceSPI struct {
1314 // Chip select pin
14- CSB machine. Pin
15+ csb pin. OutputFunc
1516
1617 buf [7 ]byte
1718
1819 // SPI bus (requires chip select to be usable).
19- Bus drivers.SPI
20+ bus drivers.SPI
21+ configurePins func ()
2022}
2123
2224// NewSPI returns a new device driver. The pin and SPI interface are not
2325// touched, provide a fully configured SPI object and call Configure to start
2426// using this device.
25- func NewSPI (csb machine. Pin , spi drivers.SPI ) * DeviceSPI {
27+ func NewSPI (csb pin. Output , spi drivers.SPI ) * DeviceSPI {
2628 return & DeviceSPI {
27- CSB : csb , // chip select
28- Bus : spi ,
29+ csb : csb .Set , // chip select
30+ bus : spi ,
31+ configurePins : func () {
32+ legacy .ConfigurePinOut (csb )
33+ },
2934 }
3035}
3136
3237// Configure configures the BMI160 for use. It configures the CSB pin and
3338// configures the BMI160, but it does not configure the SPI interface (it is
3439// assumed to be up and running).
3540func (d * DeviceSPI ) Configure () error {
36- d .CSB .Configure (machine.PinConfig {Mode : machine .PinOutput })
37- d .CSB .High ()
38-
41+ if d .configurePins == nil {
42+ return legacy .ErrConfigBeforeInstantiated
43+ }
44+ d .configurePins ()
45+ d .csb .High ()
3946 // The datasheet recommends doing a register read from address 0x7F to get
4047 // SPI communication going:
4148 // > If CSB sees a rising edge after power-up, the BMI160 interface switches
@@ -86,9 +93,9 @@ func (d *DeviceSPI) ReadTemperature() (temperature int32, err error) {
8693 data [0 ] = 0x80 | reg_TEMPERATURE_0
8794 data [1 ] = 0
8895 data [2 ] = 0
89- d .CSB .Low ()
90- err = d .Bus .Tx (data , data )
91- d .CSB .High ()
96+ d .csb .Low ()
97+ err = d .bus .Tx (data , data )
98+ d .csb .High ()
9299 if err != nil {
93100 return
94101 }
@@ -123,9 +130,9 @@ func (d *DeviceSPI) ReadAcceleration() (x int32, y int32, z int32, err error) {
123130 for i := 1 ; i < len (data ); i ++ {
124131 data [i ] = 0
125132 }
126- d .CSB .Low ()
127- err = d .Bus .Tx (data , data )
128- d .CSB .High ()
133+ d .csb .Low ()
134+ err = d .bus .Tx (data , data )
135+ d .csb .High ()
129136 if err != nil {
130137 return
131138 }
@@ -153,9 +160,9 @@ func (d *DeviceSPI) ReadRotation() (x int32, y int32, z int32, err error) {
153160 for i := 1 ; i < len (data ); i ++ {
154161 data [i ] = 0
155162 }
156- d .CSB .Low ()
157- err = d .Bus .Tx (data , data )
158- d .CSB .High ()
163+ d .csb .Low ()
164+ err = d .bus .Tx (data , data )
165+ d .csb .High ()
159166 if err != nil {
160167 return
161168 }
@@ -201,9 +208,9 @@ func (d *DeviceSPI) readRegister(address uint8) uint8 {
201208 data := d .buf [:2 ]
202209 data [0 ] = 0x80 | address
203210 data [1 ] = 0
204- d .CSB .Low ()
205- d .Bus .Tx (data , data )
206- d .CSB .High ()
211+ d .csb .Low ()
212+ d .bus .Tx (data , data )
213+ d .csb .High ()
207214 return data [1 ]
208215}
209216
@@ -217,7 +224,7 @@ func (d *DeviceSPI) writeRegister(address, data uint8) {
217224 buf [0 ] = address
218225 buf [1 ] = data
219226
220- d .CSB .Low ()
221- d .Bus .Tx (buf , buf )
222- d .CSB .High ()
227+ d .csb .Low ()
228+ d .bus .Tx (buf , buf )
229+ d .csb .High ()
223230}
0 commit comments