diff --git a/boards/arm/stm32/stm32f411-minimum/Kconfig b/boards/arm/stm32/stm32f411-minimum/Kconfig index 9691e7195f8..da3164b395c 100644 --- a/boards/arm/stm32/stm32f411-minimum/Kconfig +++ b/boards/arm/stm32/stm32f411-minimum/Kconfig @@ -34,4 +34,53 @@ config STM32F411MINIMUM_FLASH_MINOR ---help--- Sets the minor number for the FLASH MTD /dev entry +menuconfig STM32F411MINIMUM_HX711 + bool "Enable hx711 scale sensor" + default n + select ADC_HX711 + +if STM32F411MINIMUM_HX711 + +choice + prompt "Select GPIO port for clock pin" + default STM32F411MINIMUM_HX711_CLK_PORTA + +config STM32F411MINIMUM_HX711_CLK_PORTA + bool "Port A" +config STM32F411MINIMUM_HX711_CLK_PORTB + bool "Port B" + +endchoice # Select GPIO port for clock pin + +config STM32F411MINIMUM_HX711_CLK_PIN + int "Select GPIO pin number for clock pin" + default 1 + range 0 15 + +choice + prompt "Select GPIO port for data pin" + default STM32F411MINIMUM_HX711_DATA_PORTA + +config STM32F411MINIMUM_HX711_DATA_PORTA + bool "Port A" +config STM32F411MINIMUM_HX711_DATA_PORTB + bool "Port B" + +endchoice # Select GPIO port for data pin + +config STM32F411MINIMUM_HX711_DATA_PIN + int "Select GPIO pin number for data pin" + default 2 + range 0 15 + +endif # STM32F411MINIMUM_HX711 + +menuconfig STM32F411MINIMUM_GPIO + select DEV_GPIO + bool "enable gpio subsystem" + +if STM32F411MINIMUM_GPIO +source boards/arm/stm32/stm32f411-minimum/Kconfig.gpio +endif + endif diff --git a/boards/arm/stm32/stm32f411-minimum/src/CMakeLists.txt b/boards/arm/stm32/stm32f411-minimum/src/CMakeLists.txt index c13bf9cf978..d117ba809da 100644 --- a/boards/arm/stm32/stm32f411-minimum/src/CMakeLists.txt +++ b/boards/arm/stm32/stm32f411-minimum/src/CMakeLists.txt @@ -48,6 +48,10 @@ if(CONFIG_USBMSC) list(APPEND SRCS stm32_usbmsc.c) endif() +if(CONFIG_ADC_HX711) + list(APPEND SRCS stm32_hx711.c) +endif() + target_sources(board PRIVATE ${SRCS}) set_property(GLOBAL PROPERTY LD_SCRIPT diff --git a/boards/arm/stm32/stm32f411-minimum/src/Make.defs b/boards/arm/stm32/stm32f411-minimum/src/Make.defs index c3e8906ad61..25d82b27650 100644 --- a/boards/arm/stm32/stm32f411-minimum/src/Make.defs +++ b/boards/arm/stm32/stm32f411-minimum/src/Make.defs @@ -30,6 +30,10 @@ ifeq ($(CONFIG_ARCH_LEDS),y) CSRCS += stm32_autoleds.c endif +ifeq ($(CONFIG_ADC_HX711),y) + CSRCS += stm32_hx711.c +endif + ifeq ($(CONFIG_SPI),y) CSRCS += stm32_spi.c endif diff --git a/boards/arm/stm32/stm32f411-minimum/src/stm32_bringup.c b/boards/arm/stm32/stm32f411-minimum/src/stm32_bringup.c index 320f4c4153c..7a41bffee0f 100644 --- a/boards/arm/stm32/stm32f411-minimum/src/stm32_bringup.c +++ b/boards/arm/stm32/stm32f411-minimum/src/stm32_bringup.c @@ -84,6 +84,14 @@ int stm32_bringup(void) { int ret = OK; +#ifdef CONFIG_ADC_HX711 + ret = stm32_hx711_initialize(); + if (ret != OK) + { + aerr("ERROR: Failed to initialize hx711: %d\n", ret); + } +#endif + #if defined(CONFIG_STM32_OTGFS) && defined(CONFIG_USBHOST) /* Initialize USB host operation. stm32_usbhost_initialize() starts a * thread will monitor for USB connection and disconnection events. diff --git a/boards/arm/stm32/stm32f411-minimum/src/stm32_hx711.c b/boards/arm/stm32/stm32f411-minimum/src/stm32_hx711.c new file mode 100644 index 00000000000..8a1ba2b699d --- /dev/null +++ b/boards/arm/stm32/stm32f411-minimum/src/stm32_hx711.c @@ -0,0 +1,102 @@ +/**************************************************************************** + * boards/arm/stm32/stm32f411-minimum/src/stm32_hx711.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_gpio.h" +#include "stm32f411-minimum.h" + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static int stm32_hx711_clock_set(unsigned char minor, int value); +static int stm32_hx711_data_read(unsigned char minor); +static int stm32_hx711_data_irq(unsigned char minor, + xcpt_t handler, void *arg); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +struct hx711_lower_s g_lower = +{ + .data_read = stm32_hx711_data_read, + .clock_set = stm32_hx711_clock_set, + .data_irq = stm32_hx711_data_irq, + .cleanup = NULL +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static int stm32_hx711_clock_set(unsigned char minor, int value) +{ + UNUSED(minor); + + stm32_gpiowrite(HX711_CLK_PIN, value); + return OK; +} + +static int stm32_hx711_data_read(unsigned char minor) +{ + UNUSED(minor); + + return stm32_gpioread(HX711_DATA_PIN); +} + +static int stm32_hx711_data_irq(unsigned char minor, + xcpt_t handler, void *arg) +{ + UNUSED(minor); + + return stm32_gpiosetevent(HX711_DATA_PIN, false, true, true, handler, arg); +}; + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +int stm32_hx711_initialize(void) +{ + int ret; + + stm32_configgpio(HX711_DATA_PIN); + stm32_configgpio(HX711_CLK_PIN); + + ret = hx711_register(0, &g_lower); + if (ret != 0) + { + aerr("ERROR: Failed to register hx711 device: %d\n", ret); + return -1; + } + + return OK; +} diff --git a/boards/arm/stm32/stm32f411-minimum/src/stm32f411-minimum.h b/boards/arm/stm32/stm32f411-minimum/src/stm32f411-minimum.h index c255d92dcf6..47e5b3a10e2 100644 --- a/boards/arm/stm32/stm32f411-minimum/src/stm32f411-minimum.h +++ b/boards/arm/stm32/stm32f411-minimum/src/stm32f411-minimum.h @@ -76,6 +76,29 @@ # endif #endif +/* HX711 pins */ + +#ifdef CONFIG_ADC_HX711 +# ifdef CONFIG_STM32F411MINIMUM_HX711_CLK_PORTB +# define HX711_CLK_PORT GPIO_PORTB +# else +# define HX711_CLK_PORT GPIO_PORTA +# endif + +# ifdef CONFIG_STM32F411MINIMUM_HX711_DATA_PORTB +# define HX711_DATA_PORT GPIO_PORTB +# else +# define HX711_DATA_PORT GPIO_PORTA +# endif + +#define HX711_CLK_PIN (GPIO_OUTPUT|GPIO_PUSHPULL|GPIO_OUTPUT_SET|\ + GPIO_SPEED_2MHz|GPIO_PULLUP|\ + HX711_CLK_PORT|CONFIG_STM32F411MINIMUM_HX711_CLK_PIN) +#define HX711_DATA_PIN (GPIO_INPUT|GPIO_SPEED_2MHz|GPIO_PULLUP|GPIO_EXTI|\ + HX711_DATA_PORT|CONFIG_STM32F411MINIMUM_HX711_DATA_PIN) + +#endif /* CONFIG_HX711 */ + /**************************************************************************** * Public Data ****************************************************************************/ @@ -99,6 +122,18 @@ extern struct spi_dev_s *g_spi2; void stm32_spidev_initialize(void); +/**************************************************************************** + * Name: stm32_hx711_initialize + * + * Description: + * Initialize hx711 chip + * + ****************************************************************************/ + +#ifdef CONFIG_ADC_HX711 +int stm32_hx711_initialize(void); +#endif + /**************************************************************************** * Name: stm32_mmcsd_initialize *