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

stm32f401rc-rs485: Add buttons support #11217

Merged
merged 1 commit into from Nov 19, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions boards/arm/stm32/stm32f401rc-rs485/configs/nsh/defconfig
Expand Up @@ -18,13 +18,25 @@ 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_BOARD_LOOPSPERMSEC=8499
CONFIG_BUILTIN=y
CONFIG_EXAMPLES_BUTTONS=y
CONFIG_EXAMPLES_BUTTONS_NAME0="SW3"
CONFIG_EXAMPLES_BUTTONS_NAME1="SW4"
CONFIG_EXAMPLES_BUTTONS_NAME2="SW5"
CONFIG_EXAMPLES_BUTTONS_NAME3="SW6"
CONFIG_EXAMPLES_BUTTONS_NAMES=y
CONFIG_EXAMPLES_BUTTONS_QTD=4
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
Expand Down
18 changes: 11 additions & 7 deletions boards/arm/stm32/stm32f401rc-rs485/include/board.h
Expand Up @@ -387,16 +387,20 @@ extern "C"
#define LED_PANIC 1

/* Buttons
*
* B1 USER:
* the user button is connected to the I/O PC13 (pin 2) of the STM32
* microcontroller.
* The STM32F401RC-RS485 has 4 user buttons: SW3, SW4, SW5 and
* SW6. They are connected to PB13, PB14, PB15 and PC6 respectively.
*/

#define BUTTON_USER 0
#define NUM_BUTTONS 1
#define BUTTON_SW3 0
#define BUTTON_SW4 1
#define BUTTON_SW5 2
#define BUTTON_SW6 3
#define NUM_BUTTONS 4

#define BUTTON_USER_BIT (1 << BUTTON_USER)
#define BUTTON_SW3_BIT (1 << BUTTON_SW3)
#define BUTTON_SW4_BIT (1 << BUTTON_SW4)
#define BUTTON_SW5_BIT (1 << BUTTON_SW5)
#define BUTTON_SW6_BIT (1 << BUTTON_SW6)

#define GPIO_TIM2_CH1IN (GPIO_TIM2_CH1IN_1 | GPIO_PULLUP)
#define GPIO_TIM2_CH2IN (GPIO_TIM2_CH2IN_1 | GPIO_PULLUP)
Expand Down
4 changes: 4 additions & 0 deletions boards/arm/stm32/stm32f401rc-rs485/src/CMakeLists.txt
Expand Up @@ -32,6 +32,10 @@ if(CONFIG_BOARDCTL)
list(APPEND SRCS stm32_appinit.c)
endif()

if(CONFIG_ARCH_BUTTONS)
list(APPEND SRCS stm32_buttons.c)
endif()

target_sources(board PRIVATE ${SRCS})

if(CONFIG_ARCH_CHIP_STM32F401RC)
Expand Down
4 changes: 4 additions & 0 deletions boards/arm/stm32/stm32f401rc-rs485/src/Make.defs
Expand Up @@ -35,6 +35,10 @@ ifeq ($(CONFIG_BOARDCTL),y)
CSRCS += stm32_appinit.c
endif

ifeq ($(CONFIG_ARCH_BUTTONS),y)
CSRCS += stm32_buttons.c
endif

DEPPATH += --dep-path board
VPATH += :board
CFLAGS += ${INCDIR_PREFIX}$(TOPDIR)$(DELIM)arch$(DELIM)$(CONFIG_ARCH)$(DELIM)src$(DELIM)board$(DELIM)board
14 changes: 14 additions & 0 deletions boards/arm/stm32/stm32f401rc-rs485/src/stm32_bringup.c
Expand Up @@ -39,6 +39,10 @@
# include <nuttx/leds/userled.h>
#endif

#ifdef CONFIG_INPUT_BUTTONS
# include <nuttx/input/buttons.h>
#endif

#include "stm32f401rc-rs485.h"

#include <nuttx/board.h>
Expand Down Expand Up @@ -75,5 +79,15 @@ int stm32_bringup(void)
}
#endif

#ifdef CONFIG_INPUT_BUTTONS
/* Register the BUTTON driver */

ret = btn_lower_initialize("/dev/buttons");
if (ret < 0)
{
syslog(LOG_ERR, "ERROR: btn_lower_initialize() failed: %d\n", ret);
}
#endif

return ret;
}
149 changes: 149 additions & 0 deletions boards/arm/stm32/stm32f401rc-rs485/src/stm32_buttons.c
@@ -0,0 +1,149 @@
/****************************************************************************
* boards/arm/stm32/stm32f401rc-rs485/src/stm32_buttons.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 <stdint.h>
#include <errno.h>

#include <nuttx/arch.h>
#include <nuttx/board.h>
#include <arch/board/board.h>

#include "stm32.h"
#include "stm32f401rc-rs485.h"

#ifdef CONFIG_ARCH_BUTTONS

/****************************************************************************
* Private Data
****************************************************************************/

/* Pin configuration for each STM32F401RC RS485 button. This array is indexed
* by the BUTTON_* definitions in board.h
*/

static const uint32_t g_buttons[NUM_BUTTONS] =
{
GPIO_BTN_SW3, GPIO_BTN_SW4, GPIO_BTN_SW5, GPIO_BTN_SW6
};

/****************************************************************************
* Public Functions
****************************************************************************/

/****************************************************************************
* Name: board_button_initialize
*
* Description:
* board_button_initialize() must be called to initialize button resources.
* After that, board_buttons() may be called to collect the current state
* of all buttons or board_button_irq() may be called to register button
* interrupt handlers.
*
****************************************************************************/

uint32_t board_button_initialize(void)
{
int i;

/* Configure the GPIO pins as inputs. NOTE that EXTI interrupts are
* configured for all pins.
*/

for (i = 0; i < NUM_BUTTONS; i++)
{
stm32_configgpio(g_buttons[i]);
}

return NUM_BUTTONS;
}

/****************************************************************************
* Name: board_buttons
****************************************************************************/

uint32_t board_buttons(void)
{
uint32_t ret = 0;
int i;

/* Check that state of each key */

for (i = 0; i < NUM_BUTTONS; i++)
{
/* A LOW value means that the key is pressed. */

bool released = stm32_gpioread(g_buttons[i]);

/* Accumulate the set of depressed (not released) keys */

if (!released)
{
ret |= (1 << i);
}
}

return ret;
}

/****************************************************************************
* Button support.
*
* Description:
* board_button_initialize() must be called to initialize button resources.
* After that, board_buttons() may be called to collect the current state
* of all buttons or board_button_irq() may be called to register button
* interrupt handlers.
*
* After board_button_initialize() has been called, board_buttons() may be
* called to collect the state of all buttons. board_buttons() returns an
* 32-bit bit set with each bit associated with a button. See the
* BUTTON_*_BIT definitions in board.h for the meaning of each bit.
*
* board_button_irq() may be called to register an interrupt handler that
* will be called when a button is depressed or released. The ID value is a
* button enumeration value that uniquely identifies a button resource. See
* the BUTTON_* definitions in board.h for the meaning of enumeration
* value.
*
****************************************************************************/

#ifdef CONFIG_ARCH_IRQBUTTONS
int board_button_irq(int id, xcpt_t irqhandler, void *arg)
{
int ret = -EINVAL;

/* The following should be atomic */

if (id >= MIN_IRQBUTTON && id <= MAX_IRQBUTTON)
{
ret = stm32_gpiosetevent(g_buttons[id], true, true, true,
irqhandler, arg);
}

return ret;
}
#endif
#endif /* CONFIG_ARCH_BUTTONS */
26 changes: 17 additions & 9 deletions boards/arm/stm32/stm32f401rc-rs485/src/stm32f401rc-rs485.h
Expand Up @@ -67,17 +67,25 @@
GPIO_SPEED_50MHz)

/* Buttons
*
* B1 USER: the user button is connected to the I/O PC13 (pin 2) of the STM32
* microcontroller.
* The STm32F401RC-RS485 has 4 user buttons.
* - SW3 is connected to the GPIO PB13.
* - SW4 is connected to the GPIO PB14.
* - SW5 is connected to the GPIO PB15.
* - SW6 is connected to the GPIO PC6.
*/

#define MIN_IRQBUTTON BUTTON_USER
#define MAX_IRQBUTTON BUTTON_USER
#define NUM_IRQBUTTONS 1

#define GPIO_BTN_USER \
(GPIO_INPUT |GPIO_FLOAT |GPIO_EXTI | GPIO_PORTC | GPIO_PIN13)
#define MIN_IRQBUTTON BUTTON_SW3
#define MAX_IRQBUTTON BUTTON_SW6
#define NUM_IRQBUTTONS 4

#define GPIO_BTN_SW3 \
(GPIO_INPUT |GPIO_FLOAT |GPIO_EXTI | GPIO_PORTB | GPIO_PIN13)
#define GPIO_BTN_SW4 \
(GPIO_INPUT |GPIO_FLOAT |GPIO_EXTI | GPIO_PORTB | GPIO_PIN14)
#define GPIO_BTN_SW5 \
(GPIO_INPUT |GPIO_FLOAT |GPIO_EXTI | GPIO_PORTB | GPIO_PIN15)
#define GPIO_BTN_SW6 \
(GPIO_INPUT |GPIO_FLOAT |GPIO_EXTI | GPIO_PORTC | GPIO_PIN6)

/* The shield uses the following pins:
*
Expand Down