-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for LPS22HB MEMS nano pressure sensor (#297)
lps22hb: add support for lps22hb
- Loading branch information
1 parent
76ff632
commit 82e9e4a
Showing
6 changed files
with
171 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package main | ||
|
||
import ( | ||
"machine" | ||
"time" | ||
|
||
"tinygo.org/x/drivers/lps22hb" | ||
) | ||
|
||
func main() { | ||
|
||
machine.I2C1.Configure(machine.I2CConfig{ | ||
SCL: machine.P0_15, // SCL1 on Nano 33 BLE Sense | ||
SDA: machine.P0_14, // SDA1 on Nano 33 BLE Sense | ||
Frequency: machine.TWI_FREQ_400KHZ, | ||
}) | ||
|
||
sensor := lps22hb.New(machine.I2C1) | ||
|
||
if !sensor.Connected() { | ||
println("LPS22HB not connected!") | ||
return | ||
} | ||
|
||
sensor.Configure() | ||
|
||
for { | ||
|
||
p, _ := sensor.ReadPressure() | ||
t, _ := sensor.ReadTemperature() | ||
println("p =", float32(p)/1000.0, "hPa / t =", float32(t)/1000.0, "*C") | ||
time.Sleep(time.Second) | ||
// note: the device would power down itself after each query | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Package lps22hb implements a driver for LPS22HB, a MEMS nano pressure sensor. | ||
// | ||
// Datasheet: https://www.st.com/resource/en/datasheet/dm00140895.pdf | ||
// | ||
package lps22hb | ||
|
||
import ( | ||
"tinygo.org/x/drivers" | ||
) | ||
|
||
// Device wraps an I2C connection to a HTS221 device. | ||
type Device struct { | ||
bus drivers.I2C | ||
Address uint8 | ||
} | ||
|
||
// Connected returns whether LPS22HB has been found. | ||
// It does a "who am I" request and checks the response. | ||
func (d *Device) Connected() bool { | ||
data := []byte{0} | ||
d.bus.ReadRegister(d.Address, LPS22HB_WHO_AM_I_REG, data) | ||
return data[0] == 0xB1 | ||
} | ||
|
||
// Configure sets up the LPS22HB device for communication. | ||
func (d *Device) Configure() { | ||
// set to block update mode | ||
d.bus.WriteRegister(d.Address, LPS22HB_CTRL1_REG, []byte{0x02}) | ||
} | ||
|
||
// ReadPressure returns the pressure in milli pascals (mPa). | ||
func (d *Device) ReadPressure() (pressure int32, err error) { | ||
d.waitForOneShot() | ||
|
||
// read data | ||
data := []byte{0, 0, 0} | ||
d.bus.ReadRegister(d.Address, LPS22HB_PRESS_OUT_REG, data[:1]) | ||
d.bus.ReadRegister(d.Address, LPS22HB_PRESS_OUT_REG+1, data[1:2]) | ||
d.bus.ReadRegister(d.Address, LPS22HB_PRESS_OUT_REG+2, data[2:]) | ||
pValue := float32(uint32(data[2])<<16|uint32(data[1])<<8|uint32(data[0])) / 4096.0 | ||
|
||
return int32(pValue * 1000), nil | ||
} | ||
|
||
// ReadTemperature returns the temperature in celsius milli degrees (°C/1000). | ||
func (d *Device) ReadTemperature() (temperature int32, err error) { | ||
d.waitForOneShot() | ||
|
||
// read data | ||
data := []byte{0, 0} | ||
d.bus.ReadRegister(d.Address, LPS22HB_TEMP_OUT_REG, data[:1]) | ||
d.bus.ReadRegister(d.Address, LPS22HB_TEMP_OUT_REG+1, data[1:]) | ||
tValue := float32(int16(uint16(data[1])<<8|uint16(data[0]))) / 100.0 | ||
|
||
return int32(tValue * 1000), nil | ||
} | ||
|
||
// private functions | ||
|
||
// wait and trigger one shot in block update | ||
func (d *Device) waitForOneShot() { | ||
// trigger one shot | ||
d.bus.WriteRegister(d.Address, LPS22HB_CTRL2_REG, []byte{0x01}) | ||
|
||
// wait until one shot is cleared | ||
data := []byte{1} | ||
for { | ||
d.bus.ReadRegister(d.Address, LPS22HB_CTRL2_REG, data) | ||
if data[0]&0x01 == 0 { | ||
break | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
//go:build !nano_33_ble | ||
// +build !nano_33_ble | ||
|
||
package lps22hb | ||
|
||
import "tinygo.org/x/drivers" | ||
|
||
// New creates a new LPS22HB connection. The I2C bus must already be | ||
// configured. | ||
// | ||
// This function only creates the Device object, it does not touch the device. | ||
func New(bus drivers.I2C) Device { | ||
return Device{bus: bus, Address: LPS22HB_ADDRESS} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
//go:build nano_33_ble | ||
// +build nano_33_ble | ||
|
||
package lps22hb | ||
|
||
import ( | ||
"machine" | ||
"time" | ||
|
||
"tinygo.org/x/drivers" | ||
) | ||
|
||
// New creates a new LPS22HB connection. The I2C bus must already be | ||
// configured. | ||
// | ||
// This function only creates the Device object, it does not touch the device. | ||
func New(bus drivers.I2C) Device { | ||
// turn on internal power pin (machine.P0_22) and I2C1 pullups power pin (machine.P1_00) | ||
// and wait a moment. | ||
ENV := machine.P0_22 | ||
ENV.Configure(machine.PinConfig{Mode: machine.PinOutput}) | ||
ENV.High() | ||
R := machine.P1_00 | ||
R.Configure(machine.PinConfig{Mode: machine.PinOutput}) | ||
R.High() | ||
time.Sleep(time.Millisecond * 10) | ||
|
||
return Device{bus: bus, Address: LPS22HB_ADDRESS} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package lps22hb | ||
|
||
const ( | ||
|
||
// I2C address | ||
LPS22HB_ADDRESS = 0x5C | ||
|
||
// control/status registers | ||
LPS22HB_WHO_AM_I_REG = 0x0F | ||
LPS22HB_CTRL1_REG = 0x10 | ||
LPS22HB_CTRL2_REG = 0x11 | ||
LPS22HB_STATUS_REG = 0x27 | ||
LPS22HB_PRESS_OUT_REG = 0x28 | ||
LPS22HB_TEMP_OUT_REG = 0x2B | ||
) |