Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

9 add support for bmp390 #10

Merged
merged 2 commits into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,27 @@ show different communication features for the
[BMI323 shuttle](https://www.bosch-sensortec.com/media/boschsensortec/downloads/shuttle_board_flyer/bst-bmi323-sf000.pdf)
board:

* [`bmi088/bmi323_i2c_read_write.py`](./bmi323/bmi323_i2c_read_write.py)
* [`bmi088/bmi323_i2c_polling_streaming.py`](./bmi323/bmi323_i2c_polling_streaming.py)
* [`bmi088/bmi323_i2c_interrupt_streaming.py`](./bmi323/bmi323_i2c_interrupt_streaming.py)
* [`bmi088/bmi323_spi_read_write.py`](./bmi323/bmi323_spi_read_write.py)
* [`bmi088/bmi323_spi_polling_streaming.py`](./bmi323/bmi323_spi_polling_streaming.py)
* [`bmi088/bmi323_spi_interrupt_streaming.py`](./bmi323/bmi323_spi_interrupt_streaming.py)
* [`bmi323/bmi323_i2c_read_write.py`](./bmi323/bmi323_i2c_read_write.py)
* [`bmi323/bmi323_i2c_polling_streaming.py`](./bmi323/bmi323_i2c_polling_streaming.py)
* [`bmi323/bmi323_i2c_interrupt_streaming.py`](./bmi323/bmi323_i2c_interrupt_streaming.py)
* [`bmi323/bmi323_spi_read_write.py`](./bmi323/bmi323_spi_read_write.py)
* [`bmi323/bmi323_spi_polling_streaming.py`](./bmi323/bmi323_spi_polling_streaming.py)
* [`bmi323/bmi323_spi_interrupt_streaming.py`](./bmi323/bmi323_spi_interrupt_streaming.py)

## [`bmp390`](https://www.bosch-sensortec.com/products/environmental-sensors/pressure-sensors/bmp390/)

The examples in the [`bmp390`](./bmp390) folder
show different communication features for the
[BMP390 shuttle](https://www.bosch-sensortec.com/media/boschsensortec/downloads/shuttle_board_flyer/application_board_3_1/bst-bmp390-sf000.pdf)
board:

* [`bmp390/bmp390_i2c_read_write.py`](./bmp390/bmp390_i2c_read_write.py)
* [`bmp390/bmp390_i2c_polling_streaming.py`](./bmp390/bmp390_i2c_polling_streaming.py)
* [`bmp390/bmp390_i2c_interrupt_streaming.py`](./bmp390/bmp390_i2c_interrupt_streaming.py)
* [`bmp390/bmp390_spi_read_write.py`](./bmp390/bmp390_spi_read_write.py)
* [`bmp390/bmp390_spi_polling_streaming.py`](./bmp390/bmp390_spi_polling_streaming.py)
* [`bmp390/bmp390_spi_interrupt_streaming.py`](./bmp390/bmp390_spi_interrupt_streaming.py)



## Need a specific example or do not know how to read data from your sensor?
Expand Down
2 changes: 1 addition & 1 deletion examples/bmi323/bmi323_i2c_read_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def setup_logging(level: int = logging.DEBUG) -> logging.Logger:

logger.info(f"chip_id=0x{shuttle.sensor.chip_id:04X}")
logger.info(f"err_reg=0x{shuttle.sensor.err_reg:04X}")
logger.info(f"err_reg=0x{shuttle.sensor.status:04X}")
logger.info(f"status=0x{shuttle.sensor.status:04X}")

a_x, a_y, a_z = shuttle.sensor.acc_data
logger.info(f"acceleration=({a_x=:04X}, {a_y=:04X}, {a_z=:04X})")
Expand Down
Empty file added examples/bmp390/__init__.py
Empty file.
56 changes: 56 additions & 0 deletions examples/bmp390/bmp390_i2c_interrupt_streaming.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import logging
import struct
import sys
import time
from pathlib import Path

from umrx_app_v3.shuttle_board.bmp390.bmp390_shuttle import BMP390Shuttle


def setup_logging(level: int = logging.DEBUG) -> logging.Logger:
logger = logging.getLogger()
logger.setLevel(level)
stdout_handler = logging.StreamHandler(sys.stdout)
log_format = "(%(asctime)s) [%(levelname)-8s] %(filename)s:%(lineno)d: %(message)s"
log_formatter = logging.Formatter(log_format)
stdout_handler.setFormatter(log_formatter)
file_handler = logging.FileHandler(f"{Path(__file__).parent / Path(__file__).stem}.log", mode="w")
file_handler.setFormatter(log_formatter)
logger.addHandler(stdout_handler)
logger.addHandler(file_handler)
return logger


if __name__ == "__main__":
logger = setup_logging()
# This example is for Application Board 3.1 hardware
shuttle = BMP390Shuttle.on_hardware_v3_rev1()
shuttle.initialize()
shuttle.check_connected_hw()

shuttle.configure_i2c()
logger.info(f"chip_id=0x{shuttle.sensor.chip_id:02X}")
assert shuttle.sensor.chip_id == 0x60
_ = shuttle.sensor.compensate_temperature(0x0000) # cache temperature NVM registers
_ = shuttle.sensor.compensate_pressure(0x00, 24.0) # cache pressure NVM registers
shuttle.configure_interrupt_streaming()
shuttle.start_streaming()
time.sleep(0.1)
for idx in range(1000):
for streaming in shuttle.board.receive_interrupt_streaming_multiple(includes_mcu_timestamp=False):
sensor_id, packet, time_stamp, payload = streaming
pressure_b0, pressure_b1, pressure_b2, temp_b0, temp_b1, temp_b2, time_b0, time_b1, time_b2 = struct.unpack(
"<BBBBBBxxBBB", payload
)
raw_pressure = (pressure_b2 << 16) | (pressure_b1 << 8) | pressure_b0
raw_temperature = (temp_b2 << 16) | (temp_b1 << 8) | temp_b0
sensor_time = (time_b2 << 16) | (time_b1 << 8) | time_b0
compensated_temperature = shuttle.sensor.compensate_temperature(raw_temperature)
compensated_pressure = shuttle.sensor.compensate_pressure(raw_pressure, compensated_temperature)
logger.info(
f"[{idx=:03d}], pressure(Pa)={compensated_pressure:7.3f}, "
f"temperature(C)={compensated_temperature:3.3f}, "
f"time_stamp(s)={sensor_time / 25908.533}"
)
time.sleep(0.05)
shuttle.board.stop_interrupt_streaming()
56 changes: 56 additions & 0 deletions examples/bmp390/bmp390_i2c_polling_streaming.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import logging
import struct
import sys
import time
from pathlib import Path

from umrx_app_v3.shuttle_board.bmp390.bmp390_shuttle import BMP390Shuttle


def setup_logging(level: int = logging.DEBUG) -> logging.Logger:
logger = logging.getLogger()
logger.setLevel(level)
stdout_handler = logging.StreamHandler(sys.stdout)
log_format = "(%(asctime)s) [%(levelname)-8s] %(filename)s:%(lineno)d: %(message)s"
log_formatter = logging.Formatter(log_format)
stdout_handler.setFormatter(log_formatter)
file_handler = logging.FileHandler(f"{Path(__file__).parent / Path(__file__).stem}.log", mode="w")
file_handler.setFormatter(log_formatter)
logger.addHandler(stdout_handler)
logger.addHandler(file_handler)
return logger


if __name__ == "__main__":
logger = setup_logging()
# This example is for Application Board 3.1 hardware
shuttle = BMP390Shuttle.on_hardware_v3_rev1()
shuttle.initialize()
shuttle.check_connected_hw()

shuttle.configure_i2c()
logger.info(f"chip_id=0x{shuttle.sensor.chip_id:04X}")
assert shuttle.sensor.chip_id == 0x60
_ = shuttle.sensor.compensate_temperature(0x0000) # caching NVM registers
_ = shuttle.sensor.compensate_pressure(0x00, 24.0) # caching NVM registers
shuttle.configure_polling_streaming()
shuttle.start_streaming()
time.sleep(0.1)
for idx in range(1000):
for streaming in shuttle.board.receive_polling_streaming_multiple():
sensor_id, payload = streaming
(pressure_b0, pressure_b1, pressure_b2, temp_b0, temp_b1, temp_b2, time_b0, time_b1, time_b2) = (
struct.unpack("<BBBBBBxxBBB", payload)
)
raw_pressure = (pressure_b2 << 16) | (pressure_b1 << 8) | pressure_b0
raw_temperature = (temp_b2 << 16) | (temp_b1 << 8) | temp_b0
sensor_time = (time_b2 << 16) | (time_b1 << 8) | time_b0
compensated_temperature = shuttle.sensor.compensate_temperature(raw_temperature)
compensated_pressure = shuttle.sensor.compensate_pressure(raw_pressure, compensated_temperature)
logger.info(
f"[{idx=:03d}], pressure(Pa)={compensated_pressure:7.3f}, "
f"temperature(C)={compensated_temperature:3.3f}, "
f"time_stamp(s)={sensor_time / 25908.533}"
)
time.sleep(0.05)
shuttle.board.stop_polling_streaming()
49 changes: 49 additions & 0 deletions examples/bmp390/bmp390_i2c_read_write.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import logging
import sys
import time
from pathlib import Path

from umrx_app_v3.shuttle_board.bmp390.bmp390_shuttle import BMP390Shuttle


def setup_logging(level: int = logging.DEBUG) -> logging.Logger:
logger = logging.getLogger()
logger.setLevel(level)
stdout_handler = logging.StreamHandler(sys.stdout)
log_format = "(%(asctime)s) [%(levelname)-8s] %(filename)s:%(lineno)d: %(message)s"
log_formatter = logging.Formatter(log_format)
stdout_handler.setFormatter(log_formatter)
file_handler = logging.FileHandler(f"{Path(__file__).parent / Path(__file__).stem}.log", mode="w")
file_handler.setFormatter(log_formatter)
logger.addHandler(stdout_handler)
logger.addHandler(file_handler)
return logger


if __name__ == "__main__":
logger = setup_logging()
shuttle = BMP390Shuttle.on_hardware_v3_rev1()
shuttle.initialize()
shuttle.check_connected_hw()

shuttle.configure_i2c()

logger.info(f"chip_id=0x{shuttle.sensor.chip_id:02X}")
logger.info(f"rev_id=0x{shuttle.sensor.rev_id:02X}")
logger.info(f"err_reg=0x{shuttle.sensor.err_reg:02X}")
logger.info(f"status=0x{shuttle.sensor.status:02X}")
logger.info(f"pwr_ctrl=0b{shuttle.sensor.pwr_ctrl:08b}")
logger.info(f"odr=0b{shuttle.sensor.odr:08b}")
logger.info(f"osr=0b{shuttle.sensor.osr:08b}")

shuttle.sensor.pwr_ctrl = (1 << 0) | (1 << 1) | (0b11 << 4)
time.sleep(0.1)
logger.info(f"pwr_ctrl=0b{shuttle.sensor.pwr_ctrl:08b}")

raw_temperature = shuttle.sensor.temperature
compensated_temperature = shuttle.sensor.compensate_temperature(raw_temperature)
logger.info(f"temperature(raw)=0x{raw_temperature:06X}, temperature(C)={compensated_temperature}")
raw_pressure = shuttle.sensor.pressure
compensated_pressure = shuttle.sensor.compensate_pressure(raw_pressure, compensated_temperature)
logger.info(f"pressure(raw)=0x{raw_pressure:06X}, pressure(Pa)={compensated_pressure}")
logger.info(f"sensor_time=0x{shuttle.sensor.sensor_time:06X}")
57 changes: 57 additions & 0 deletions examples/bmp390/bmp390_spi_interrupt_streaming.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import logging
import struct
import sys
import time
from pathlib import Path

from umrx_app_v3.shuttle_board.bmp390.bmp390_shuttle import BMP390Shuttle


def setup_logging(level: int = logging.DEBUG) -> logging.Logger:
logger = logging.getLogger()
logger.setLevel(level)
stdout_handler = logging.StreamHandler(sys.stdout)
log_format = "(%(asctime)s) [%(levelname)-8s] %(filename)s:%(lineno)d: %(message)s"
log_formatter = logging.Formatter(log_format)
stdout_handler.setFormatter(log_formatter)
file_handler = logging.FileHandler(f"{Path(__file__).parent / Path(__file__).stem}.log", mode="w")
file_handler.setFormatter(log_formatter)
logger.addHandler(stdout_handler)
logger.addHandler(file_handler)
return logger


if __name__ == "__main__":
logger = setup_logging()
shuttle = BMP390Shuttle.on_hardware_v3_rev1()
shuttle.initialize()
shuttle.check_connected_hw()

shuttle.configure_spi()
_ = shuttle.board.read_spi(shuttle.CS, 0, 1) # dummy read is required, do not delete

logger.info(f"chip_id=0x{shuttle.sensor.chip_id:02X}")
assert shuttle.sensor.chip_id == 0x60
_ = shuttle.sensor.compensate_temperature(0x0000) # cache temperature NVM registers
_ = shuttle.sensor.compensate_pressure(0x00, 24.0) # cache pressure NVM registers
shuttle.configure_interrupt_streaming()
shuttle.start_streaming()
time.sleep(0.1)
for idx in range(1000):
for streaming in shuttle.board.receive_interrupt_streaming_multiple(includes_mcu_timestamp=False):
sensor_id, packet, time_stamp, payload = streaming
pressure_b0, pressure_b1, pressure_b2, temp_b0, temp_b1, temp_b2, time_b0, time_b1, time_b2 = struct.unpack(
"<xBBBBBBxxBBB", payload
)
raw_pressure = (pressure_b2 << 16) | (pressure_b1 << 8) | pressure_b0
raw_temperature = (temp_b2 << 16) | (temp_b1 << 8) | temp_b0
sensor_time = (time_b2 << 16) | (time_b1 << 8) | time_b0
compensated_temperature = shuttle.sensor.compensate_temperature(raw_temperature)
compensated_pressure = shuttle.sensor.compensate_pressure(raw_pressure, compensated_temperature)
logger.info(
f"[{idx=:03d}], pressure(Pa)={compensated_pressure:7.3f}, "
f"temperature(C)={compensated_temperature:3.3f}, "
f"time_stamp(s)={sensor_time / 25908.533:.3f}"
)
time.sleep(0.05)
shuttle.board.stop_interrupt_streaming()
58 changes: 58 additions & 0 deletions examples/bmp390/bmp390_spi_polling_streaming.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import logging
import struct
import sys
import time
from pathlib import Path

from umrx_app_v3.shuttle_board.bmp390.bmp390_shuttle import BMP390Shuttle


def setup_logging(level: int = logging.DEBUG) -> logging.Logger:
logger = logging.getLogger()
logger.setLevel(level)
stdout_handler = logging.StreamHandler(sys.stdout)
log_format = "(%(asctime)s) [%(levelname)-8s] %(filename)s:%(lineno)d: %(message)s"
log_formatter = logging.Formatter(log_format)
stdout_handler.setFormatter(log_formatter)
file_handler = logging.FileHandler(f"{Path(__file__).parent / Path(__file__).stem}.log", mode="w")
file_handler.setFormatter(log_formatter)
logger.addHandler(stdout_handler)
logger.addHandler(file_handler)
return logger


if __name__ == "__main__":
logger = setup_logging()
# This example is for Application Board 3.1 hardware
shuttle = BMP390Shuttle.on_hardware_v3_rev1()
shuttle.initialize()
shuttle.check_connected_hw()

shuttle.configure_spi()
_ = shuttle.board.read_spi(shuttle.CS, 0, 1) # dummy read is required, do not delete

logger.info(f"chip_id=0x{shuttle.sensor.chip_id:02X}")
assert shuttle.sensor.chip_id == 0x60
_ = shuttle.sensor.compensate_temperature(0x0000) # caching NVM registers
_ = shuttle.sensor.compensate_pressure(0x00, 24.0) # caching NVM registers
shuttle.configure_polling_streaming()
shuttle.start_streaming()
time.sleep(0.1)
for idx in range(1000):
for streaming in shuttle.board.receive_polling_streaming_multiple():
sensor_id, payload = streaming
(pressure_b0, pressure_b1, pressure_b2, temp_b0, temp_b1, temp_b2, time_b0, time_b1, time_b2) = (
struct.unpack("<xBBBBBBxxBBB", payload)
)
raw_pressure = (pressure_b2 << 16) | (pressure_b1 << 8) | pressure_b0
raw_temperature = (temp_b2 << 16) | (temp_b1 << 8) | temp_b0
sensor_time = (time_b2 << 16) | (time_b1 << 8) | time_b0
compensated_temperature = shuttle.sensor.compensate_temperature(raw_temperature)
compensated_pressure = shuttle.sensor.compensate_pressure(raw_pressure, compensated_temperature)
logger.info(
f"[{idx=:03d}], pressure(Pa)={compensated_pressure:7.3f}, "
f"temperature(C)={compensated_temperature:3.3f}, "
f"time_stamp(s)={sensor_time / 25908.533:.3f}"
)
time.sleep(0.05)
shuttle.board.stop_polling_streaming()
50 changes: 50 additions & 0 deletions examples/bmp390/bmp390_spi_read_write.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import logging
import sys
import time
from pathlib import Path

from umrx_app_v3.shuttle_board.bmp390.bmp390_shuttle import BMP390Shuttle


def setup_logging(level: int = logging.DEBUG) -> logging.Logger:
logger = logging.getLogger()
logger.setLevel(level)
stdout_handler = logging.StreamHandler(sys.stdout)
log_format = "(%(asctime)s) [%(levelname)-8s] %(filename)s:%(lineno)d: %(message)s"
log_formatter = logging.Formatter(log_format)
stdout_handler.setFormatter(log_formatter)
file_handler = logging.FileHandler(f"{Path(__file__).parent / Path(__file__).stem}.log", mode="w")
file_handler.setFormatter(log_formatter)
logger.addHandler(stdout_handler)
logger.addHandler(file_handler)
return logger


if __name__ == "__main__":
logger = setup_logging()
shuttle = BMP390Shuttle.on_hardware_v3_rev1()
shuttle.initialize()
shuttle.check_connected_hw()

shuttle.configure_spi()
_ = shuttle.board.read_spi(shuttle.CS, 0, 1) # dummy read is required, do not delete

logger.info(f"chip_id=0x{shuttle.sensor.chip_id:02X}")
logger.info(f"rev_id=0x{shuttle.sensor.rev_id:02X}")
logger.info(f"err_reg=0x{shuttle.sensor.err_reg:02X}")
logger.info(f"status=0x{shuttle.sensor.status:02X}")
logger.info(f"pwr_ctrl=0b{shuttle.sensor.pwr_ctrl:08b}")
logger.info(f"odr=0b{shuttle.sensor.odr:08b}")
logger.info(f"osr=0b{shuttle.sensor.osr:08b}")

shuttle.sensor.pwr_ctrl = (1 << 0) | (1 << 1) | (0b11 << 4)
time.sleep(0.1)
logger.info(f"pwr_ctrl=0b{shuttle.sensor.pwr_ctrl:08b}")

raw_temperature = shuttle.sensor.temperature
compensated_temperature = shuttle.sensor.compensate_temperature(raw_temperature)
logger.info(f"temperature(raw)=0x{raw_temperature:06X}, temperature(C)={compensated_temperature}")
raw_pressure = shuttle.sensor.pressure
compensated_pressure = shuttle.sensor.compensate_pressure(raw_pressure, compensated_temperature)
logger.info(f"pressure(raw)=0x{raw_pressure:06X}, pressure(Pa)={compensated_pressure}")
logger.info(f"sensor_time=0x{shuttle.sensor.sensor_time:06X}")
Loading
Loading