From 8fb458b7fee2bb5e697da95d3c923a75845bf276 Mon Sep 17 00:00:00 2001 From: Rodrigo Sim Date: Sat, 18 May 2024 12:20:46 -0300 Subject: [PATCH] board/stm32f401rc-rs485: Add support to ultrasonic sensor HC-SR04 Signed-off-by: Rodrigo Sim rcsim10@gmail.com --- .../boards/stm32f401rc-rs485/index.rst | 16 +++++ .../configs/hcsr04/defconfig | 67 +++++++++++++++++++ .../stm32/stm32f401rc-rs485/include/board.h | 11 +++ .../stm32f401rc-rs485/src/stm32_bringup.c | 15 +++++ 4 files changed, 109 insertions(+) create mode 100644 boards/arm/stm32/stm32f401rc-rs485/configs/hcsr04/defconfig diff --git a/Documentation/platforms/arm/stm32f4/boards/stm32f401rc-rs485/index.rst b/Documentation/platforms/arm/stm32f4/boards/stm32f401rc-rs485/index.rst index 6778f0b5bd..63ff9e77ea 100644 --- a/Documentation/platforms/arm/stm32f4/boards/stm32f401rc-rs485/index.rst +++ b/Documentation/platforms/arm/stm32f4/boards/stm32f401rc-rs485/index.rst @@ -539,3 +539,19 @@ NSH commands:: mcsonn_main: Connected nsh> msdis + +hcs04 +----- + +Configures the NuttShell (nsh) over USB Serial (check usbserial configuration) and enables ultrasonic sensor HC-SR04:: + + nsh> cat /dev/dist0 + 6241 --> value + 6227 + 6241 + 6255 + +You can convert the value using following:: + + Convert to cm: value/58 + Converto to inches: value/148 diff --git a/boards/arm/stm32/stm32f401rc-rs485/configs/hcsr04/defconfig b/boards/arm/stm32/stm32f401rc-rs485/configs/hcsr04/defconfig new file mode 100644 index 0000000000..decf112494 --- /dev/null +++ b/boards/arm/stm32/stm32f401rc-rs485/configs/hcsr04/defconfig @@ -0,0 +1,67 @@ +# +# 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_BOARDCTL_USBDEVCTRL=y +CONFIG_BOARD_LOOPSPERMSEC=8499 +CONFIG_BUILTIN=y +CONFIG_CDCACM=y +CONFIG_CDCACM_CONSOLE=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_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_HCSR04=y +CONFIG_SPI=y +CONFIG_START_DAY=5 +CONFIG_START_MONTH=5 +CONFIG_START_YEAR=2014 +CONFIG_STM32_FREERUN=y +CONFIG_STM32_JTAG_SW_ENABLE=y +CONFIG_STM32_OTGFS=y +CONFIG_STM32_PWR=y +CONFIG_STM32_TIM1=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/include/board.h b/boards/arm/stm32/stm32f401rc-rs485/include/board.h index 76fd32ec8c..6ade75f5cb 100644 --- a/boards/arm/stm32/stm32f401rc-rs485/include/board.h +++ b/boards/arm/stm32/stm32f401rc-rs485/include/board.h @@ -298,6 +298,17 @@ extern "C" #define GPIO_TIM3_CH1IN GPIO_TIM3_CH1IN_2 #define GPIO_TIM3_CH2IN GPIO_TIM3_CH2IN_1 +/* HCSR04 driver */ + +/* Pins config to use with HC-SR04 sensor */ + +#define GPIO_HCSR04_INT (GPIO_INPUT |GPIO_FLOAT |GPIO_EXTI | GPIO_PORTB | GPIO_PIN1) +#define GPIO_HCSR04_TRIG (GPIO_OUTPUT_CLEAR | GPIO_OUTPUT | GPIO_SPEED_50MHz | GPIO_PORTB | GPIO_PIN0) + +#define BOARD_HCSR04_GPIO_INT GPIO_HCSR04_INT +#define BOARD_HCSR04_GPIO_TRIG GPIO_HCSR04_TRIG +#define BOARD_HCSR04_FRTIMER 1 /* TIM1 as free running timer */ + /* I2C * * The optional _GPIO configurations allow the I2C driver to manually diff --git a/boards/arm/stm32/stm32f401rc-rs485/src/stm32_bringup.c b/boards/arm/stm32/stm32f401rc-rs485/src/stm32_bringup.c index eb7b42072f..036408d9df 100644 --- a/boards/arm/stm32/stm32f401rc-rs485/src/stm32_bringup.c +++ b/boards/arm/stm32/stm32f401rc-rs485/src/stm32_bringup.c @@ -59,6 +59,11 @@ # include #endif +#ifdef CONFIG_SENSORS_HCSR04 +#include "stm32_hcsr04.h" +#endif + + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -233,5 +238,15 @@ int stm32_bringup(void) usbdev_rndis_initialize(mac); #endif +#ifdef CONFIG_SENSORS_HCSR04 + /* Configure and initialize the HC-SR04 distance sensor */ + + ret = board_hcsr04_initialize(0); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: board_hcsr04_initialize() failed: %d\n", ret); + } +#endif + return ret; }