diff --git a/boards/risc-v/k210/maix-bit/include/board.h b/boards/risc-v/k210/maix-bit/include/board.h index cba6aa49c30..d80c82f95d1 100644 --- a/boards/risc-v/k210/maix-bit/include/board.h +++ b/boards/risc-v/k210/maix-bit/include/board.h @@ -37,10 +37,16 @@ * Pre-processor Definitions ****************************************************************************/ -#define BOARD_LED_PAD 14 /* Connected to red led */ +/* GPIOHS allocation: + * + * GPIOHS0 - System LED (pad 14, red) - k210_leds.c + * GPIOHS1 - User LED 1 (pad 12) - k210_userleds.c + * GPIOHS2 - User LED 2 (pad 13) - k210_userleds.c + */ -/* Map pad 14 to gpiohs io 0 */ +/* System LED (CONFIG_ARCH_LEDS) */ +#define BOARD_LED_PAD 14 #define BOARD_LED_IO_FUNC K210_IO_FUNC_GPIOHS0 #define BOARD_LED_IO 0 @@ -53,10 +59,16 @@ #define LED_ASSERTION 6 /* N/C */ #define LED_PANIC 7 /* blink */ -/* GPIO pins used by the GPIO Subsystem */ +/* User LED definitions */ -#define BOARD_NGPIOOUT 2 /* Amount of GPIO Output pins */ -#define BOARD_NGPIOINT 0 /* Amount of GPIO Input */ +#define BOARD_LEDS 2 + +/* User LED pin mappings */ + +#define BOARD_USERLED1_PAD 12 +#define BOARD_USERLED1_IO 1 +#define BOARD_USERLED2_PAD 13 +#define BOARD_USERLED2_IO 2 /**************************************************************************** * Public Types diff --git a/boards/risc-v/k210/maix-bit/src/CMakeLists.txt b/boards/risc-v/k210/maix-bit/src/CMakeLists.txt index 64e24f27f01..e3da05f111a 100644 --- a/boards/risc-v/k210/maix-bit/src/CMakeLists.txt +++ b/boards/risc-v/k210/maix-bit/src/CMakeLists.txt @@ -30,8 +30,8 @@ if(CONFIG_ARCH_LEDS) list(APPEND SRCS k210_leds.c) endif() -if(CONFIG_DEV_GPIO) - list(APPEND SRCS k210_gpio.c) +if(CONFIG_USERLED_LOWER) + list(APPEND SRCS k210_userleds.c) endif() target_sources(board PRIVATE ${SRCS}) diff --git a/boards/risc-v/k210/maix-bit/src/Makefile b/boards/risc-v/k210/maix-bit/src/Makefile index 7b71046af22..8a66a5a2f16 100644 --- a/boards/risc-v/k210/maix-bit/src/Makefile +++ b/boards/risc-v/k210/maix-bit/src/Makefile @@ -32,8 +32,8 @@ ifeq ($(CONFIG_ARCH_LEDS),y) CSRCS += k210_leds.c endif -ifeq ($(CONFIG_DEV_GPIO),y) -CSRCS += k210_gpio.c +ifeq ($(CONFIG_USERLED_LOWER),y) +CSRCS += k210_userleds.c endif include $(TOPDIR)/boards/Board.mk diff --git a/boards/risc-v/k210/maix-bit/src/k210_bringup.c b/boards/risc-v/k210/maix-bit/src/k210_bringup.c index 7f0441ff2f8..72b2e4c7de8 100644 --- a/boards/risc-v/k210/maix-bit/src/k210_bringup.c +++ b/boards/risc-v/k210/maix-bit/src/k210_bringup.c @@ -34,6 +34,10 @@ #include #include +#ifdef CONFIG_USERLED_LOWER +# include +#endif + #include "k210.h" #include "k210_wdt.h" #include "maix-bit.h" @@ -60,12 +64,11 @@ int k210_bringup(void) } #endif -#ifdef CONFIG_DEV_GPIO - ret = k210_gpio_init(); +#ifdef CONFIG_USERLED_LOWER + ret = userled_lower_initialize("/dev/userleds"); if (ret < 0) { - syslog(LOG_ERR, "Failed to initialize GPIO Driver: %d\n", ret); - return ret; + syslog(LOG_ERR, "ERROR: userled_lower_initialize() failed: %d\n", ret); } #endif diff --git a/boards/risc-v/k210/maix-bit/src/k210_gpio.c b/boards/risc-v/k210/maix-bit/src/k210_gpio.c deleted file mode 100644 index 6fbb41a5144..00000000000 --- a/boards/risc-v/k210/maix-bit/src/k210_gpio.c +++ /dev/null @@ -1,175 +0,0 @@ -/**************************************************************************** - * boards/risc-v/k210/maix-bit/src/k210_gpio.c - * - * SPDX-License-Identifier: Apache-2.0 - * - * 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 - -#include "k210_fpioa.h" -#include "k210_gpiohs.h" - -#if defined(CONFIG_DEV_GPIO) && !defined(CONFIG_GPIO_LOWER_HALF) - -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - -/* Pin 1 and 2 are used for this example as GPIO outputs. */ - -#define GPIO_OUT1 12 -#define GPIO_OUT2 13 - -/**************************************************************************** - * Private Types - ****************************************************************************/ - -struct k210gpio_dev_s -{ - struct gpio_dev_s gpio; - uint8_t id; -}; - -/**************************************************************************** - * Private Function Prototypes - ****************************************************************************/ - -#if BOARD_NGPIOOUT > 0 -static int gpout_read(struct gpio_dev_s *dev, bool *value); -static int gpout_write(struct gpio_dev_s *dev, bool value); -#endif - -/**************************************************************************** - * Private Data - ****************************************************************************/ - -#if BOARD_NGPIOOUT > 0 -static const struct gpio_operations_s gpout_ops = -{ - .go_read = gpout_read, - .go_write = gpout_write, - .go_attach = NULL, - .go_enable = NULL, -}; - -/* This array maps the GPIO pins used as OUTPUT */ - -static const uint32_t g_gpiooutputs[BOARD_NGPIOOUT] = -{ - GPIO_OUT1, GPIO_OUT2 -}; - -static struct k210gpio_dev_s g_gpout[BOARD_NGPIOOUT]; -#endif - -/**************************************************************************** - * Private Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: gpout_read - ****************************************************************************/ - -#if BOARD_NGPIOOUT > 0 -static int gpout_read(struct gpio_dev_s *dev, bool *value) -{ - struct k210gpio_dev_s *k210gpio = - (struct k210gpio_dev_s *)dev; - - DEBUGASSERT(k210gpio != NULL && value != NULL); - DEBUGASSERT(k210gpio->id < BOARD_NGPIOOUT); - gpioinfo("Reading...\n"); - - *value = (int) k210_gpiohs_get_value(k210gpio->id + 1); - return OK; -} - -/**************************************************************************** - * Name: gpout_write - ****************************************************************************/ - -static int gpout_write(struct gpio_dev_s *dev, bool value) -{ - struct k210gpio_dev_s *k210gpio = - (struct k210gpio_dev_s *)dev; - - DEBUGASSERT(k210gpio != NULL); - DEBUGASSERT(k210gpio->id < BOARD_NGPIOOUT); - gpioinfo("Writing %d\n", (int)value); - - k210_gpiohs_set_value(k210gpio->id + 1, !value); - - return OK; -} -#endif - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: k210_gpio_init - ****************************************************************************/ - -int k210_gpio_init(void) -{ - int i; - int pincount = 0; - -#if BOARD_NGPIOOUT > 0 - for (i = 0; i < BOARD_NGPIOOUT; i++) - { - /* Setup and register the GPIO pin */ - - g_gpout[i].gpio.gp_pintype = GPIO_OUTPUT_PIN; - g_gpout[i].gpio.gp_ops = &gpout_ops; - g_gpout[i].id = i; - gpio_pin_register(&g_gpout[i].gpio, pincount); - - /* Configure the pins that will be used as output */ - - k210_fpioa_config(g_gpiooutputs[i], - (K210_IO_FUNC_GPIOHS1 + i) | K210_IOFLAG_GPIOHS); - k210_gpiohs_set_direction(i + 1, true); - k210_gpiohs_set_value(i + 1, true); - - pincount++; - } -#endif - - return OK; -} -#endif /* CONFIG_DEV_GPIO && !CONFIG_GPIO_LOWER_HALF */ diff --git a/boards/risc-v/k210/maix-bit/src/k210_userleds.c b/boards/risc-v/k210/maix-bit/src/k210_userleds.c new file mode 100644 index 00000000000..83c85cd0540 --- /dev/null +++ b/boards/risc-v/k210/maix-bit/src/k210_userleds.c @@ -0,0 +1,190 @@ +/**************************************************************************** + * boards/risc-v/k210/maix-bit/src/k210_userleds.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * 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 "k210_fpioa.h" +#include "k210_gpiohs.h" + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/* LED configuration table */ + +struct k210_led_cfg_s +{ + uint32_t pad; /* FPIOA physical pad number */ + uint32_t func; /* FPIOA function + flags */ + uint32_t io; /* GPIOHS IO number */ +}; + +static const struct k210_led_cfg_s g_led_cfg[BOARD_LEDS] = +{ + { BOARD_USERLED1_PAD, K210_IO_FUNC_GPIOHS1 | K210_IOFLAG_GPIOHS, + BOARD_USERLED1_IO }, + { BOARD_USERLED2_PAD, K210_IO_FUNC_GPIOHS2 | K210_IOFLAG_GPIOHS, + BOARD_USERLED2_IO } +}; + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: board_userled_initialize + * + * Description: + * This function may called from application-specific logic during its + * to perform board-specific initialization of LED resources. This + * includes such things as, for example, configure GPIO pins to drive the + * LEDs and also putting the LEDs in their correct initial state. + * + * If CONFIG_ARCH_LEDS is defined, then NuttX will control the on-board + * LEDs. If CONFIG_ARCH_LEDS is not defined, then this interfaces may be + * available to control the LEDs directly from user board logic or + * indirectly user applications (via the common LED character driver). + * + * Most boards have only a few LEDs and in those cases all LEDs may be + * used by the NuttX LED logic exclusively and may not be available for + * use by user logic if CONFIG_ARCH_LEDS=y. + * + * NOTE: The LED number is returned. + * + * Input Parameters: + * None + * + * Returned Value: + * Number of LEDs on board + * + ****************************************************************************/ + +uint32_t board_userled_initialize(void) +{ + int i; + + /* Configure the LED GPIO for output. + * LEDs are active low (false=on, true=off), so set them + * high (off) initially. + */ + + for (i = 0; i < BOARD_LEDS; i++) + { + /* Configure pin for GPIOHS function */ + + k210_fpioa_config(g_led_cfg[i].pad, g_led_cfg[i].func); + + /* Set direction to output, initial value high (LED off) */ + + k210_gpiohs_set_direction(g_led_cfg[i].io, true); + k210_gpiohs_set_value(g_led_cfg[i].io, true); + } + + return BOARD_LEDS; +} + +/**************************************************************************** + * Name: board_userled + * + * Description: + * This interface may be used by application specific logic to set the + * state of a single LED. Definitions for the led identification are + * provided in the board-specific board.h header file that may be included + * like: + * + * #included + * + * If CONFIG_ARCH_LEDS is defined, then NuttX will control the on-board + * LEDs. If CONFIG_ARCH_LEDS is not defined, then this interfaces may be + * available to control the LEDs directly from user board logic or + * indirectly user applications (via the common LED character driver). + * + * Most boards have only a few LEDs and in those cases all LEDs may be + * used by the NuttX LED logic exclusively and may not be available for + * use by user logic if CONFIG_ARCH_LEDS=y. + * + * Input Parameters: + * led - LED number + * ledon - True if LED should be turned on; False to turn off + * + * Returned Value: + * None + * + ****************************************************************************/ + +void board_userled(int led, bool ledon) +{ + if ((unsigned)led < BOARD_LEDS) + { + /* LEDs are active low: false = on, true = off */ + + k210_gpiohs_set_value(g_led_cfg[led].io, !ledon); + } +} + +/**************************************************************************** + * Name: board_userled_all + * + * Description: + * This interface may be used by application specific logic to set the + * state of all board LED. Definitions for the led set member + * identification is provided in the board-specific board.h header file + * that may be includedlike: + * + * #included + * + * If CONFIG_ARCH_LEDS is defined, then NuttX will control the on-board + * LEDs. If CONFIG_ARCH_LEDS is not defined, then this interfaces may be + * available to control the LEDs directly from user board logic or + * indirectly user applications (via the common LED character driver). + * + * Most boards have only a few LEDs and in those cases all LEDs may be + * used by the NuttX LED logic exclusively and may not be available for + * use by user logic if CONFIG_ARCH_LEDS=y. + * + * Input Parameters: + * ledset - Bitset of LEDs to be turned on and off + * + * Returned Value: + * None + * + ****************************************************************************/ + +void board_userled_all(uint32_t ledset) +{ + int i; + + for (i = 0; i < BOARD_LEDS; i++) + { + board_userled(i, (ledset & (1 << i)) != 0); + } +} diff --git a/boards/risc-v/k210/maix-bit/src/maix-bit.h b/boards/risc-v/k210/maix-bit/src/maix-bit.h index 82fa934db3c..9e117788e06 100644 --- a/boards/risc-v/k210/maix-bit/src/maix-bit.h +++ b/boards/risc-v/k210/maix-bit/src/maix-bit.h @@ -31,8 +31,4 @@ int k210_bringup(void); -#ifdef CONFIG_DEV_GPIO -int k210_gpio_init(void); -#endif - #endif /* __BOARDS_RISCV_K210_MAIX_BIT_SRC_MAIX_BIT_H */