From 717fa46a538c82fe78667b2f12328273f8e310f1 Mon Sep 17 00:00:00 2001 From: Matias Nitsche Date: Fri, 8 May 2020 14:35:51 -0300 Subject: [PATCH] stm32: move nunchuck driver to common board logic --- .../arm/stm32/common/include/stm32_nunchuck.h | 85 ++++++++++++++ boards/arm/stm32/common/src/Make.defs | 4 + boards/arm/stm32/common/src/stm32_nunchuck.c | 111 ++++++++++++++++++ .../stm32f103-minimum/src/stm32_bringup.c | 6 +- .../stm32f103-minimum/src/stm32_nunchuck.c | 99 ---------------- .../arm/stm32/stm32f4discovery/src/Make.defs | 4 - .../stm32f4discovery/src/stm32_bringup.c | 6 +- .../stm32f4discovery/src/stm32_nunchuck.c | 99 ---------------- 8 files changed, 210 insertions(+), 204 deletions(-) create mode 100644 boards/arm/stm32/common/include/stm32_nunchuck.h create mode 100644 boards/arm/stm32/common/src/stm32_nunchuck.c delete mode 100644 boards/arm/stm32/stm32f103-minimum/src/stm32_nunchuck.c delete mode 100644 boards/arm/stm32/stm32f4discovery/src/stm32_nunchuck.c diff --git a/boards/arm/stm32/common/include/stm32_nunchuck.h b/boards/arm/stm32/common/include/stm32_nunchuck.h new file mode 100644 index 0000000000..82405357fc --- /dev/null +++ b/boards/arm/stm32/common/include/stm32_nunchuck.h @@ -0,0 +1,85 @@ +/**************************************************************************** + * boards/arm/stm32/common/include/stm32_nunchuck.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 __STM32_NUNCHUCK_H +#define __STM32_NUNCHUCK_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_nunchuk_initialize + * + * Description: + * Initialize and register the Nunchuck joystick driver driver. + * + * Input Parameters: + * devno - The device number, used to build the device path as + * /dev/nunchuckN + * busno - The I2C bus number + * + * Returned Value: + * Zero (OK) on success; a negated errno value on failure. + * + ****************************************************************************/ + +int board_nunchuck_initialize(int devno, int busno); + +#undef EXTERN +#ifdef __cplusplus +} +#endif + +#endif // __STM32_NUNCHUCK_H diff --git a/boards/arm/stm32/common/src/Make.defs b/boards/arm/stm32/common/src/Make.defs index 77377438c4..58951f5867 100644 --- a/boards/arm/stm32/common/src/Make.defs +++ b/boards/arm/stm32/common/src/Make.defs @@ -34,6 +34,10 @@ ifeq ($(CONFIG_SENSORS_VEML6070),y) CSRCS += stm32_veml6070.c endif +ifeq ($(CONFIG_INPUT_NUNCHUCK),y) +CSRCS += stm32_nunchuck.c +endif + DEPPATH += --dep-path src VPATH += :src CFLAGS += $(shell $(INCDIR) $(INCDIROPT) "$(CC)" $(TOPDIR)$(DELIM)arch$(DELIM)$(CONFIG_ARCH)$(DELIM)src$(DELIM)board$(DELIM)src) diff --git a/boards/arm/stm32/common/src/stm32_nunchuck.c b/boards/arm/stm32/common/src/stm32_nunchuck.c new file mode 100644 index 0000000000..4cfa94ce2b --- /dev/null +++ b/boards/arm/stm32/common/src/stm32_nunchuck.c @@ -0,0 +1,111 @@ +/**************************************************************************** + * boards/arm/stm32/common/src/stm32_nunchuck.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 +#include +#include + +#include "stm32_i2c.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: board_nunchuk_initialize + * + * Description: + * Initialize and register the Nunchuck joystick driver driver. + * + * Input Parameters: + * devno - The device number, used to build the device path as + * /dev/nunchuckN + * busno - The I2C bus number + * + * Returned Value: + * Zero (OK) on success; a negated errno value on failure. + * + ****************************************************************************/ + +int board_nunchuck_initialize(int devno, int busno) +{ + FAR struct i2c_master_s *i2c; + char devpath[14]; + int ret; + + iinfo("Initializing Wii Nunchuck!\n"); + + /* Initialize I2C */ + + i2c = stm32_i2cbus_initialize(busno); + if (i2c == NULL) + { + return -ENODEV; + } + + /* Register the joystick device as /dev/nunchuck0 */ + + iinfo("Initialize joystick driver: %s\n", devname); + + snprintf(devpath, 14, "/dev/nunchuck%d", devno); + ret = nunchuck_register(devname, i2c); + if (ret < 0) + { + ierr("ERROR: nunchuck_register failed: %d\n", ret); + } + + return ret; +} diff --git a/boards/arm/stm32/stm32f103-minimum/src/stm32_bringup.c b/boards/arm/stm32/stm32f103-minimum/src/stm32_bringup.c index 8312d3681a..7ed51ffc18 100644 --- a/boards/arm/stm32/stm32f103-minimum/src/stm32_bringup.c +++ b/boards/arm/stm32/stm32f103-minimum/src/stm32_bringup.c @@ -101,6 +101,10 @@ #include "stm32_veml6070.h" #endif +#ifdef CONFIG_INPUT_NUNCHUCK +#include "stm32_nunchuck.h" +#endif + /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ @@ -360,7 +364,7 @@ int stm32_bringup(void) #ifdef CONFIG_INPUT_NUNCHUCK /* Register the Nunchuck driver */ - ret = nunchuck_initialize("/dev/nunchuck0"); + ret = board_nunchuck_initialize(0, 1); if (ret < 0) { syslog(LOG_ERR, "ERROR: nunchuck_initialize() failed: %d\n", ret); diff --git a/boards/arm/stm32/stm32f103-minimum/src/stm32_nunchuck.c b/boards/arm/stm32/stm32f103-minimum/src/stm32_nunchuck.c deleted file mode 100644 index 68ff188714..0000000000 --- a/boards/arm/stm32/stm32f103-minimum/src/stm32_nunchuck.c +++ /dev/null @@ -1,99 +0,0 @@ -/**************************************************************************** - * boards/arm/stm32/stm32f4discovery/src/stm32_nunchuck.c - * - * Copyright (C) 2017 Gregory Nutt. All rights reserved. - * Author: Alan Carvalho de Assis - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * 3. Neither the name NuttX nor the names of its contributors may be - * used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN - * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - * - ****************************************************************************/ - -/**************************************************************************** - * Included Files - ****************************************************************************/ - -#include - -#include -#include -#include -#include - -#include -#include -#include -#include - -#include "stm32_i2c.h" -#include "stm32f103_minimum.h" - -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - -#define NUNCHUCK_I2C_PORTNO 1 /* On I2C1 */ - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: nunchuck_initialize - * - * Description: - * Initialize and register the Nunchuck joystick driver - * - ****************************************************************************/ - -int nunchuck_initialize(FAR char *devname) -{ - FAR struct i2c_master_s *i2c; - int ret; - - iinfo("Initializing Wii Nunchuck!\n"); - - /* Initialize I2C */ - - i2c = stm32_i2cbus_initialize(NUNCHUCK_I2C_PORTNO); - if (i2c == NULL) - { - return -ENODEV; - } - - /* Register the joystick device as /dev/nunchuck0 */ - - iinfo("Initialize joystick driver: %s\n", devname); - - ret = nunchuck_register(devname, i2c); - if (ret < 0) - { - ierr("ERROR: nunchuck_register failed: %d\n", ret); - } - - return ret; -} diff --git a/boards/arm/stm32/stm32f4discovery/src/Make.defs b/boards/arm/stm32/stm32f4discovery/src/Make.defs index 6c12413bfd..2169b99ec0 100644 --- a/boards/arm/stm32/stm32f4discovery/src/Make.defs +++ b/boards/arm/stm32/stm32f4discovery/src/Make.defs @@ -85,10 +85,6 @@ ifeq ($(CONFIG_LPWAN_SX127X),y) CSRCS += stm32_sx127x.c endif -ifeq ($(CONFIG_INPUT_NUNCHUCK),y) -CSRCS += stm32_nunchuck.c -endif - ifeq ($(CONFIG_SENSORS_MAX31855),y) CSRCS += stm32_max31855.c endif diff --git a/boards/arm/stm32/stm32f4discovery/src/stm32_bringup.c b/boards/arm/stm32/stm32f4discovery/src/stm32_bringup.c index 8e4aebbb5e..094b2799a5 100644 --- a/boards/arm/stm32/stm32f4discovery/src/stm32_bringup.c +++ b/boards/arm/stm32/stm32f4discovery/src/stm32_bringup.c @@ -90,6 +90,10 @@ #include "stm32_max6675.h" #endif +#ifdef CONFIG_INPUT_NUNCHUCK +#include "stm32_nunchuck.h" +#endif + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -318,7 +322,7 @@ int stm32_bringup(void) #ifdef CONFIG_INPUT_NUNCHUCK /* Register the Nunchuck driver */ - ret = nunchuck_initialize("/dev/nunchuck0"); + ret = board_nunchuck_initialize(0, 1); if (ret < 0) { syslog(LOG_ERR, "ERROR: nunchuck_initialize() failed: %d\n", ret); diff --git a/boards/arm/stm32/stm32f4discovery/src/stm32_nunchuck.c b/boards/arm/stm32/stm32f4discovery/src/stm32_nunchuck.c deleted file mode 100644 index d04d698808..0000000000 --- a/boards/arm/stm32/stm32f4discovery/src/stm32_nunchuck.c +++ /dev/null @@ -1,99 +0,0 @@ -/**************************************************************************** - * boards/arm/stm32/stm32f4discovery/src/stm32_nunchuck.c - * - * Copyright (C) 2017 Gregory Nutt. All rights reserved. - * Author: Alan Carvalho de Assis - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * 3. Neither the name NuttX nor the names of its contributors may be - * used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN - * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - * - ****************************************************************************/ - -/**************************************************************************** - * Included Files - ****************************************************************************/ - -#include - -#include -#include -#include -#include - -#include -#include -#include -#include - -#include "stm32_i2c.h" -#include "stm32f4discovery.h" - -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - -#define NUNCHUCK_I2C_PORTNO 1 /* On I2C1 */ - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: nunchuck_initialize - * - * Description: - * Initialize and register the Nunchuck joystick driver - * - ****************************************************************************/ - -int nunchuck_initialize(FAR char *devname) -{ - FAR struct i2c_master_s *i2c; - int ret; - - iinfo("Initializing Wii Nunchuck!\n"); - - /* Initialize I2C */ - - i2c = stm32_i2cbus_initialize(NUNCHUCK_I2C_PORTNO); - if (i2c == NULL) - { - return -ENODEV; - } - - /* Register the joystick device as /dev/nunchuck0 */ - - iinfo("Initialize joystick driver: %s\n", devname); - - ret = nunchuck_register(devname, i2c); - if (ret < 0) - { - ierr("ERROR: nunchuck_register failed: %d\n", ret); - } - - return ret; -}