From 5c15fe6ec104798cd656ea498abd0593d1c83f78 Mon Sep 17 00:00:00 2001 From: Matheus Castello Date: Sun, 17 Oct 2021 00:36:41 -0300 Subject: [PATCH] boards: k210: Add initial gpio user space support Signed-off-by: Matheus Castello --- boards/risc-v/k210/maix-bit/include/board.h | 5 + boards/risc-v/k210/maix-bit/src/Makefile | 4 + .../risc-v/k210/maix-bit/src/k210_bringup.c | 10 + boards/risc-v/k210/maix-bit/src/k210_gpio.c | 173 ++++++++++++++++++ boards/risc-v/k210/maix-bit/src/maix-bit.h | 4 + 5 files changed, 196 insertions(+) create mode 100644 boards/risc-v/k210/maix-bit/src/k210_gpio.c diff --git a/boards/risc-v/k210/maix-bit/include/board.h b/boards/risc-v/k210/maix-bit/include/board.h index 796c9e617d..bf69b830c2 100644 --- a/boards/risc-v/k210/maix-bit/include/board.h +++ b/boards/risc-v/k210/maix-bit/include/board.h @@ -55,6 +55,11 @@ #define LED_ASSERTION 6 /* N/C */ #define LED_PANIC 7 /* blink */ +/* GPIO pins used by the GPIO Subsystem */ + +#define BOARD_NGPIOOUT 2 /* Amount of GPIO Output pins */ +#define BOARD_NGPIOINT 0 /* Amount of GPIO Input */ + /**************************************************************************** * Public Types ****************************************************************************/ diff --git a/boards/risc-v/k210/maix-bit/src/Makefile b/boards/risc-v/k210/maix-bit/src/Makefile index e70a4d1374..8c2db777a1 100644 --- a/boards/risc-v/k210/maix-bit/src/Makefile +++ b/boards/risc-v/k210/maix-bit/src/Makefile @@ -30,4 +30,8 @@ ifeq ($(CONFIG_ARCH_LEDS),y) CSRCS += k210_leds.c endif +ifeq ($(CONFIG_DEV_GPIO),y) +CSRCS += k210_gpio.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 2dc3d09c7b..c722275e1c 100644 --- a/boards/risc-v/k210/maix-bit/src/k210_bringup.c +++ b/boards/risc-v/k210/maix-bit/src/k210_bringup.c @@ -33,6 +33,7 @@ #include #include "k210.h" +#include "maix-bit.h" /**************************************************************************** * Public Functions @@ -56,5 +57,14 @@ int k210_bringup(void) } #endif +#ifdef CONFIG_DEV_GPIO + ret = k210_gpio_init(); + if (ret < 0) + { + syslog(LOG_ERR, "Failed to initialize GPIO Driver: %d\n", ret); + return ret; + } +#endif + return ret; } diff --git a/boards/risc-v/k210/maix-bit/src/k210_gpio.c b/boards/risc-v/k210/maix-bit/src/k210_gpio.c new file mode 100644 index 0000000000..0e631dc491 --- /dev/null +++ b/boards/risc-v/k210/maix-bit/src/k210_gpio.c @@ -0,0 +1,173 @@ +/**************************************************************************** + * boards/risc-v/k210/maix-bit/src/k210_gpio.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 + +#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(FAR struct gpio_dev_s *dev, FAR bool *value); +static int gpout_write(FAR 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(FAR struct gpio_dev_s *dev, FAR bool *value) +{ + FAR struct k210gpio_dev_s *k210gpio = + (FAR 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(FAR struct gpio_dev_s *dev, bool value) +{ + FAR struct k210gpio_dev_s *k210gpio = + (FAR 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/maix-bit.h b/boards/risc-v/k210/maix-bit/src/maix-bit.h index 9aec864bd5..a0cb76148a 100644 --- a/boards/risc-v/k210/maix-bit/src/maix-bit.h +++ b/boards/risc-v/k210/maix-bit/src/maix-bit.h @@ -29,4 +29,8 @@ int k210_bringup(void); +#ifdef CONFIG_DEV_GPIO +int k210_gpio_init(void); +#endif + #endif /* __BOARDS_RISCV_K210_MAIX_BIT_SRC_MAIX_BIT_H */