11package bmi160
22
33import (
4- "machine"
54 "time"
65
76 "tinygo.org/x/drivers"
7+ "tinygo.org/x/drivers/internal/legacy"
88)
99
1010// DeviceSPI is the SPI interface to a BMI160 accelerometer/gyroscope. There is
1111// also an I2C interface, but it is not yet supported.
1212type DeviceSPI struct {
1313 // Chip select pin
14- CSB machine. Pin
14+ CSB drivers. PinOutput
1515
1616 buf [7 ]byte
1717
1818 // SPI bus (requires chip select to be usable).
19- Bus drivers.SPI
19+ Bus drivers.SPI
20+ config func ()
2021}
2122
2223// NewSPI returns a new device driver. The pin and SPI interface are not
2324// touched, provide a fully configured SPI object and call Configure to start
2425// using this device.
25- func NewSPI (csb machine. Pin , spi drivers.SPI ) * DeviceSPI {
26+ func NewSPI (csb legacy. PinOutput , spi drivers.SPI ) * DeviceSPI {
2627 return & DeviceSPI {
27- CSB : csb , // chip select
28+ CSB : csb . Set , // chip select
2829 Bus : spi ,
30+ config : func () {
31+ legacy .ConfigurePinOut (csb )
32+ },
2933 }
3034}
3135
3236// Configure configures the BMI160 for use. It configures the CSB pin and
3337// configures the BMI160, but it does not configure the SPI interface (it is
3438// assumed to be up and running).
3539func (d * DeviceSPI ) Configure () error {
36- d .CSB .Configure (machine.PinConfig {Mode : machine .PinOutput })
37- d .CSB .High ()
40+ d .CSB (true )
3841
3942 // The datasheet recommends doing a register read from address 0x7F to get
4043 // SPI communication going:
@@ -86,9 +89,9 @@ func (d *DeviceSPI) ReadTemperature() (temperature int32, err error) {
8689 data [0 ] = 0x80 | reg_TEMPERATURE_0
8790 data [1 ] = 0
8891 data [2 ] = 0
89- d .CSB . Low ( )
92+ d .CSB ( false )
9093 err = d .Bus .Tx (data , data )
91- d .CSB . High ( )
94+ d .CSB ( true )
9295 if err != nil {
9396 return
9497 }
@@ -123,9 +126,9 @@ func (d *DeviceSPI) ReadAcceleration() (x int32, y int32, z int32, err error) {
123126 for i := 1 ; i < len (data ); i ++ {
124127 data [i ] = 0
125128 }
126- d .CSB . Low ( )
129+ d .CSB ( false )
127130 err = d .Bus .Tx (data , data )
128- d .CSB . High ( )
131+ d .CSB ( true )
129132 if err != nil {
130133 return
131134 }
@@ -153,9 +156,9 @@ func (d *DeviceSPI) ReadRotation() (x int32, y int32, z int32, err error) {
153156 for i := 1 ; i < len (data ); i ++ {
154157 data [i ] = 0
155158 }
156- d .CSB . Low ( )
159+ d .CSB ( false )
157160 err = d .Bus .Tx (data , data )
158- d .CSB . High ( )
161+ d .CSB ( true )
159162 if err != nil {
160163 return
161164 }
@@ -201,9 +204,9 @@ func (d *DeviceSPI) readRegister(address uint8) uint8 {
201204 data := d .buf [:2 ]
202205 data [0 ] = 0x80 | address
203206 data [1 ] = 0
204- d .CSB . Low ( )
207+ d .CSB ( false )
205208 d .Bus .Tx (data , data )
206- d .CSB . High ( )
209+ d .CSB ( true )
207210 return data [1 ]
208211}
209212
@@ -217,7 +220,7 @@ func (d *DeviceSPI) writeRegister(address, data uint8) {
217220 buf [0 ] = address
218221 buf [1 ] = data
219222
220- d .CSB . Low ( )
223+ d .CSB ( false )
221224 d .Bus .Tx (buf , buf )
222- d .CSB . High ( )
225+ d .CSB ( true )
223226}
0 commit comments