arm/stm32f401rc-rs485: Add support to BMP280 sensor

Signed-off-by: Rodrigo Sim <rcsim10@gmail.com>
This commit is contained in:
Rodrigo Sim 2024-08-10 13:17:06 -03:00 committed by Xiang Xiao
parent d77ef098c6
commit 208b8e084f
9 changed files with 322 additions and 0 deletions

View File

@ -691,3 +691,22 @@ at 13.56 MHz and ISO/IEC 14443 A/MIFARE and NTAG.
.. figure:: mfrc522_image.jpg .. figure:: mfrc522_image.jpg
:align: center :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.

View File

@ -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 <nuttx/config.h>
/****************************************************************************
* 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 */

View File

@ -24,6 +24,10 @@ if(CONFIG_SENSORS_BMP180)
list(APPEND SRCS stm32_bmp180.c) list(APPEND SRCS stm32_bmp180.c)
endif() endif()
if(CONFIG_SENSORS_BMP280)
list(APPEND SRCS stm32_bmp280.c)
endif()
if(CONFIG_LEDS_APA102) if(CONFIG_LEDS_APA102)
list(APPEND SRCS stm32_apa102.c) list(APPEND SRCS stm32_apa102.c)
endif() endif()

View File

@ -24,6 +24,10 @@ ifeq ($(CONFIG_SENSORS_BMP180),y)
CSRCS += stm32_bmp180.c CSRCS += stm32_bmp180.c
endif endif
ifeq ($(CONFIG_SENSORS_BMP280),y)
CSRCS += stm32_bmp280.c
endif
ifeq ($(CONFIG_SENSORS_MS56XX),y) ifeq ($(CONFIG_SENSORS_MS56XX),y)
CSRCS += stm32_ms5611.c CSRCS += stm32_ms5611.c
endif endif

View File

@ -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 <nuttx/config.h>
#include <stdio.h>
#include <debug.h>
#include <nuttx/arch.h>
#include <nuttx/sensors/bmp280.h>
#include <nuttx/i2c/i2c_master.h>
#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;
}

View File

@ -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

View File

@ -75,6 +75,10 @@
#include "stm32_drv8266.h" #include "stm32_drv8266.h"
#endif #endif
#ifdef CONFIG_SENSORS_BMP280
#include "stm32_bmp280.h"
#endif
/**************************************************************************** /****************************************************************************
* Public Functions * Public Functions
****************************************************************************/ ****************************************************************************/
@ -298,5 +302,16 @@ int stm32_bringup(void)
} }
#endif #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; return ret;
} }

View File

@ -379,6 +379,23 @@ config SENSORS_BMP280
if 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 config BMP280_I2C_FREQUENCY
int "BMP280 I2C frequency" int "BMP280 I2C frequency"
default 400000 default 400000

View File

@ -42,7 +42,11 @@
* Pre-processor Definitions * Pre-processor Definitions
****************************************************************************/ ****************************************************************************/
#ifdef CONFIG_BMP280_I2C_ADDR_76
#define BMP280_ADDR 0x76 #define BMP280_ADDR 0x76
#else
#define BMP280_ADDR 0x77
#endif
#define BMP280_FREQ CONFIG_BMP280_I2C_FREQUENCY #define BMP280_FREQ CONFIG_BMP280_I2C_FREQUENCY
#define DEVID 0x58 #define DEVID 0x58