From 208b8e084f0071506491922a944934516e92c8a8 Mon Sep 17 00:00:00 2001 From: Rodrigo Sim Date: Sat, 10 Aug 2024 13:17:06 -0300 Subject: [PATCH] arm/stm32f401rc-rs485: Add support to BMP280 sensor Signed-off-by: Rodrigo Sim --- .../boards/stm32f401rc-rs485/index.rst | 19 ++++ .../arm/stm32/common/include/stm32_bmp280.h | 84 ++++++++++++++ boards/arm/stm32/common/src/CMakeLists.txt | 4 + boards/arm/stm32/common/src/Make.defs | 4 + boards/arm/stm32/common/src/stm32_bmp280.c | 106 ++++++++++++++++++ .../configs/bmp280/defconfig | 69 ++++++++++++ .../stm32f401rc-rs485/src/stm32_bringup.c | 15 +++ drivers/sensors/Kconfig | 17 +++ drivers/sensors/bmp280_uorb.c | 4 + 9 files changed, 322 insertions(+) create mode 100644 boards/arm/stm32/common/include/stm32_bmp280.h create mode 100644 boards/arm/stm32/common/src/stm32_bmp280.c create mode 100644 boards/arm/stm32/stm32f401rc-rs485/configs/bmp280/defconfig diff --git a/Documentation/platforms/arm/stm32f4/boards/stm32f401rc-rs485/index.rst b/Documentation/platforms/arm/stm32f4/boards/stm32f401rc-rs485/index.rst index 339f5064e7c..20d067d01bc 100644 --- a/Documentation/platforms/arm/stm32f4/boards/stm32f401rc-rs485/index.rst +++ b/Documentation/platforms/arm/stm32f4/boards/stm32f401rc-rs485/index.rst @@ -691,3 +691,22 @@ at 13.56 MHz and ISO/IEC 14443 A/MIFARE and NTAG. .. figure:: mfrc522_image.jpg :align: center + +bmp280 +------ + +Configures the NuttShell (nsh) over USB Serial (check usbserial configuration) and enables BMP280 Digital Pressure Sensor. +BMP280 has an I2C address that can be configure by SDO. Connecting SDO to GND results in slave +address 0x76, connection it to VDD results in slave address 0x77. This can be configured by enabling BMP280_I2C_ADDR_76 or BMP280_I2C_ADDR_77. This configuration uses I2C1 and slave address 0x77. + +NSH commands:: + + NuttShell (NSH) NuttX-12.6.0-RC1 + nsh> bmp280 + Absolute pressure [hPa] = 911.400024 + Temperature [C] = 26.110001 + nsh> bmp280 + Absolute pressure [hPa] = 932.650024 + Temperature [C] = 24.490000 + +There is a known issue where every time the sensor is initialized, the first measurement is wrong, please check https://github.com/apache/nuttx/issues/12421 for the latest updates on this issue. \ No newline at end of file diff --git a/boards/arm/stm32/common/include/stm32_bmp280.h b/boards/arm/stm32/common/include/stm32_bmp280.h new file mode 100644 index 00000000000..9c15ff6f610 --- /dev/null +++ b/boards/arm/stm32/common/include/stm32_bmp280.h @@ -0,0 +1,84 @@ +/**************************************************************************** + * boards/arm/stm32/common/include/stm32_bmp280.h + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#ifndef __BOARDS_ARM_STM32_COMMON_INCLUDE_STM32_BMP280_H +#define __BOARDS_ARM_STM32_COMMON_INCLUDE_STM32_BMP280_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Type Definitions + ****************************************************************************/ + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +#ifdef __cplusplus +#define EXTERN extern "C" +extern "C" +{ +#else +#define EXTERN extern +#endif + +/**************************************************************************** + * Inline Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Name: board_bmp280_initialize + * + * Description: + * Initialize and register the BMP280 Pressure Sensor driver. + * + * Input Parameters: + * devno - The device number, used to build the device path as /dev/pressN + * busno - The I2C bus number + * + * Returned Value: + * Zero (OK) on success; a negated errno value on failure. + * + ****************************************************************************/ + +int board_bmp280_initialize(int devno, int busno); + +#undef EXTERN +#ifdef __cplusplus +} +#endif + +#endif /* __BOARDS_ARM_STM32_COMMON_INCLUDE_STM32_BMP280_H */ diff --git a/boards/arm/stm32/common/src/CMakeLists.txt b/boards/arm/stm32/common/src/CMakeLists.txt index 4c6c4df796d..668cb257f66 100644 --- a/boards/arm/stm32/common/src/CMakeLists.txt +++ b/boards/arm/stm32/common/src/CMakeLists.txt @@ -24,6 +24,10 @@ if(CONFIG_SENSORS_BMP180) list(APPEND SRCS stm32_bmp180.c) endif() +if(CONFIG_SENSORS_BMP280) + list(APPEND SRCS stm32_bmp280.c) +endif() + if(CONFIG_LEDS_APA102) list(APPEND SRCS stm32_apa102.c) endif() diff --git a/boards/arm/stm32/common/src/Make.defs b/boards/arm/stm32/common/src/Make.defs index ddb698c13b3..fc75a96098b 100644 --- a/boards/arm/stm32/common/src/Make.defs +++ b/boards/arm/stm32/common/src/Make.defs @@ -24,6 +24,10 @@ ifeq ($(CONFIG_SENSORS_BMP180),y) CSRCS += stm32_bmp180.c endif +ifeq ($(CONFIG_SENSORS_BMP280),y) + CSRCS += stm32_bmp280.c +endif + ifeq ($(CONFIG_SENSORS_MS56XX),y) CSRCS += stm32_ms5611.c endif diff --git a/boards/arm/stm32/common/src/stm32_bmp280.c b/boards/arm/stm32/common/src/stm32_bmp280.c new file mode 100644 index 00000000000..b8b0f70ebfa --- /dev/null +++ b/boards/arm/stm32/common/src/stm32_bmp280.c @@ -0,0 +1,106 @@ +/**************************************************************************** + * boards/arm/stm32/common/src/stm32_bmp280.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include + +#include +#include +#include + +#include "stm32_i2c.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: board_bmp280_initialize + * + * Description: + * Initialize and register the BMP280 Pressure Sensor driver. + * + * Input Parameters: + * devno - The device number, used to build the device path as /dev/pressN + * busno - The I2C bus number + * + * Returned Value: + * Zero (OK) on success; a negated errno value on failure. + * + ****************************************************************************/ + +int board_bmp280_initialize(int devno, int busno) +{ + struct i2c_master_s *i2c; + int ret; + + sninfo("Initializing BMP280!\n"); + + /* Initialize BMP280 */ + + i2c = stm32_i2cbus_initialize(busno); + if (i2c) + { + /* Then try to register the barometer sensor in I2C0 */ + + ret = bmp280_register(devno, i2c); + if (ret < 0) + { + snerr("ERROR: Error registering BMP280 in I2C%d\n", busno); + } + } + else + { + ret = -ENODEV; + } + + return ret; +} + diff --git a/boards/arm/stm32/stm32f401rc-rs485/configs/bmp280/defconfig b/boards/arm/stm32/stm32f401rc-rs485/configs/bmp280/defconfig new file mode 100644 index 00000000000..20d75c29f72 --- /dev/null +++ b/boards/arm/stm32/stm32f401rc-rs485/configs/bmp280/defconfig @@ -0,0 +1,69 @@ +# +# This file is autogenerated: PLEASE DO NOT EDIT IT. +# +# You can use "make menuconfig" to make any modifications to the installed .config file. +# You can then do "make savedefconfig" to generate a new defconfig file that includes your +# modifications. +# +# CONFIG_ARCH_FPU is not set +# CONFIG_NSH_ARGCAT is not set +# CONFIG_NSH_CMDOPT_HEXDUMP is not set +# CONFIG_NSH_DISABLE_IFCONFIG is not set +# CONFIG_NSH_DISABLE_PS is not set +CONFIG_ARCH="arm" +CONFIG_ARCH_BOARD="stm32f401rc-rs485" +CONFIG_ARCH_BOARD_COMMON=y +CONFIG_ARCH_BOARD_STM32F401RC_RS485=y +CONFIG_ARCH_BUTTONS=y +CONFIG_ARCH_CHIP="stm32" +CONFIG_ARCH_CHIP_STM32=y +CONFIG_ARCH_CHIP_STM32F401RC=y +CONFIG_ARCH_INTERRUPTSTACK=2048 +CONFIG_ARCH_IRQBUTTONS=y +CONFIG_ARCH_STACKDUMP=y +CONFIG_BMP280_I2C_ADDR_77=y +CONFIG_BOARDCTL_USBDEVCTRL=y +CONFIG_BOARD_LOOPSPERMSEC=8499 +CONFIG_BUILTIN=y +CONFIG_CDCACM=y +CONFIG_CDCACM_CONSOLE=y +CONFIG_EXAMPLES_BMP280=y +CONFIG_EXAMPLES_BUTTONS=y +CONFIG_EXAMPLES_BUTTONS_NAME0="SW3" +CONFIG_EXAMPLES_BUTTONS_NAME1="SW4" +CONFIG_EXAMPLES_BUTTONS_NAME2="SW5" +CONFIG_EXAMPLES_BUTTONS_NAMES=y +CONFIG_EXAMPLES_BUTTONS_QTD=3 +CONFIG_HAVE_CXX=y +CONFIG_HAVE_CXXINITIALIZE=y +CONFIG_INIT_ENTRYPOINT="nsh_main" +CONFIG_INPUT=y +CONFIG_INPUT_BUTTONS=y +CONFIG_INPUT_BUTTONS_LOWER=y +CONFIG_INTELHEX_BINARY=y +CONFIG_LIBC_FLOATINGPOINT=y +CONFIG_NSH_ARCHINIT=y +CONFIG_NSH_BUILTIN_APPS=y +CONFIG_NSH_FILEIOSIZE=512 +CONFIG_NSH_LINELEN=64 +CONFIG_NSH_READLINE=y +CONFIG_PREALLOC_TIMERS=4 +CONFIG_RAM_SIZE=98304 +CONFIG_RAM_START=0x20000000 +CONFIG_RAW_BINARY=y +CONFIG_RR_INTERVAL=200 +CONFIG_SCHED_WAITPID=y +CONFIG_SENSORS=y +CONFIG_SENSORS_BMP280=y +CONFIG_SPI=y +CONFIG_START_DAY=5 +CONFIG_START_MONTH=5 +CONFIG_START_YEAR=2014 +CONFIG_STM32_I2C1=y +CONFIG_STM32_JTAG_SW_ENABLE=y +CONFIG_STM32_OTGFS=y +CONFIG_STM32_PWR=y +CONFIG_STM32_USART6=y +CONFIG_SYSTEM_NSH=y +CONFIG_TASK_NAME_SIZE=0 +CONFIG_USBDEV=y diff --git a/boards/arm/stm32/stm32f401rc-rs485/src/stm32_bringup.c b/boards/arm/stm32/stm32f401rc-rs485/src/stm32_bringup.c index cd8f236be04..b513580b4f7 100644 --- a/boards/arm/stm32/stm32f401rc-rs485/src/stm32_bringup.c +++ b/boards/arm/stm32/stm32f401rc-rs485/src/stm32_bringup.c @@ -75,6 +75,10 @@ #include "stm32_drv8266.h" #endif +#ifdef CONFIG_SENSORS_BMP280 +#include "stm32_bmp280.h" +#endif + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -298,5 +302,16 @@ int stm32_bringup(void) } #endif +#ifdef CONFIG_SENSORS_BMP280 + /* Initialize the BMP280 pressure sensor. */ + + ret = board_bmp280_initialize(0, 1); + if (ret < 0) + { + syslog(LOG_ERR, "Failed to initialize BMP280, error %d\n", ret); + return ret; + } +#endif + return ret; } diff --git a/drivers/sensors/Kconfig b/drivers/sensors/Kconfig index 6c78c06bc28..6abab708aa0 100644 --- a/drivers/sensors/Kconfig +++ b/drivers/sensors/Kconfig @@ -379,6 +379,23 @@ config SENSORS_BMP280 if SENSORS_BMP280 +choice + prompt "I2C Address" + default BMP280_I2C_ADDR_76 + +config BMP280_I2C_ADDR_76 + bool "0x76" + ---help--- + Default address. + If SDO pin is pulled to GND, use 0x76 + +config BMP280_I2C_ADDR_77 + bool "0x77" + ---help--- + If SDO pin is pulled to VDDIO, use 0x77 + +endchoice # I2C Address + config BMP280_I2C_FREQUENCY int "BMP280 I2C frequency" default 400000 diff --git a/drivers/sensors/bmp280_uorb.c b/drivers/sensors/bmp280_uorb.c index 7acdb664d93..ecf5289d864 100644 --- a/drivers/sensors/bmp280_uorb.c +++ b/drivers/sensors/bmp280_uorb.c @@ -42,7 +42,11 @@ * Pre-processor Definitions ****************************************************************************/ +#ifdef CONFIG_BMP280_I2C_ADDR_76 #define BMP280_ADDR 0x76 +#else +#define BMP280_ADDR 0x77 +#endif #define BMP280_FREQ CONFIG_BMP280_I2C_FREQUENCY #define DEVID 0x58