You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by ma...@apache.org on 2020/06/08 01:53:39 UTC
[incubator-nuttx] 01/02: arch/risc-v/src/k210: Add basic gpiohs
support
This is an automated email from the ASF dual-hosted git repository.
masayuki pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git
commit bcd7ccc0b5392279a159de056779830b57092eb2
Author: Huang Qi <hu...@xiaomi.com>
AuthorDate: Sat Jun 6 10:42:33 2020 +0800
arch/risc-v/src/k210: Add basic gpiohs support
Signed-off-by: Huang Qi <hu...@xiaomi.com>
---
arch/risc-v/src/k210/Make.defs | 4 +-
arch/risc-v/src/k210/hardware/k210_memorymap.h | 3 +-
.../{hardware/k210_memorymap.h => k210_fpioa.c} | 39 +++++----
arch/risc-v/src/k210/k210_fpioa.h | 92 ++++++++++++++++++++++
arch/risc-v/src/k210/k210_gpiohs.c | 83 +++++++++++++++++++
arch/risc-v/src/k210/k210_gpiohs.h | 83 +++++++++++++++++++
6 files changed, 281 insertions(+), 23 deletions(-)
diff --git a/arch/risc-v/src/k210/Make.defs b/arch/risc-v/src/k210/Make.defs
index f6b8f72..c8ad47e 100644
--- a/arch/risc-v/src/k210/Make.defs
+++ b/arch/risc-v/src/k210/Make.defs
@@ -59,8 +59,8 @@ endif
# Specify our C code within this directory to be included
CHIP_CSRCS = k210_allocateheap.c k210_clockconfig.c
CHIP_CSRCS += k210_idle.c k210_irq.c k210_irq_dispatch.c
-CHIP_CSRCS += k210_lowputc.c k210_serial.c
-CHIP_CSRCS += k210_start.c k210_timerisr.c
+CHIP_CSRCS += k210_lowputc.c k210_serial.c k210_fpioa.c
+CHIP_CSRCS += k210_start.c k210_timerisr.c k210_gpiohs.c
ifeq ($(CONFIG_SMP), y)
CHIP_CSRCS += k210_cpuidlestack.c k210_cpuindex.c
diff --git a/arch/risc-v/src/k210/hardware/k210_memorymap.h b/arch/risc-v/src/k210/hardware/k210_memorymap.h
index ac09406..60b6a2a 100644
--- a/arch/risc-v/src/k210/hardware/k210_memorymap.h
+++ b/arch/risc-v/src/k210/hardware/k210_memorymap.h
@@ -39,7 +39,8 @@
#else
#define K210_UART0_BASE 0x38000000
#endif
-#define K210_GPIO_BASE 0x38001000
+#define K210_GPIOHS_BASE 0x38001000
+#define K210_FPIOA_BASE 0x502B0000
#define K210_SYSCTL_BASE 0x50440000
diff --git a/arch/risc-v/src/k210/hardware/k210_memorymap.h b/arch/risc-v/src/k210/k210_fpioa.c
similarity index 62%
copy from arch/risc-v/src/k210/hardware/k210_memorymap.h
copy to arch/risc-v/src/k210/k210_fpioa.c
index ac09406..bb71863 100644
--- a/arch/risc-v/src/k210/hardware/k210_memorymap.h
+++ b/arch/risc-v/src/k210/k210_fpioa.c
@@ -1,9 +1,5 @@
/****************************************************************************
- * arch/risc-v/src/k210/hardware/k210_memorymap.h
- *
- * Derives from software originally provided by Canaan Inc
- *
- * Copyright 2018 Canaan Inc
+ * arch/risc-v/src/k210/k210_fpioa.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
@@ -22,25 +18,28 @@
*
****************************************************************************/
-#ifndef __ARCH_RISCV_SRC_K210_HARDWARE_K210_MEMORYMAP_H
-#define __ARCH_RISCV_SRC_K210_HARDWARE_K210_MEMORYMAP_H
-
/****************************************************************************
- * Pre-processor Definitions
+ * Included Files
****************************************************************************/
-/* Register Base Address ****************************************************/
+#include <nuttx/config.h>
-#define K210_CLINT_BASE 0x02000000
-#define K210_PLIC_BASE 0x0c000000
+#include <assert.h>
+#include <debug.h>
-#ifdef CONFIG_K210_WITH_QEMU
-#define K210_UART0_BASE 0x10010000
-#else
-#define K210_UART0_BASE 0x38000000
-#endif
-#define K210_GPIO_BASE 0x38001000
+#include "riscv_internal.h"
+#include "riscv_arch.h"
-#define K210_SYSCTL_BASE 0x50440000
+#include "k210_memorymap.h"
+#include "k210_fpioa.h"
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
-#endif /* __ARCH_RISCV_SRC_K210_HARDWARE_K210_MEMORYMAP_H */
+void k210_fpioa_config(uint32_t io, uint32_t ioflags)
+{
+ uint32_t *fpioa = (uint32_t *)K210_FPIOA_BASE;
+ DEBUGASSERT(io < K210_IO_NUMBER);
+ putreg32(ioflags, &fpioa[io]);
+}
diff --git a/arch/risc-v/src/k210/k210_fpioa.h b/arch/risc-v/src/k210/k210_fpioa.h
new file mode 100644
index 0000000..5b13884
--- /dev/null
+++ b/arch/risc-v/src/k210/k210_fpioa.h
@@ -0,0 +1,92 @@
+/****************************************************************************
+ * arch/risc-v/src/k210/k210_fpioa.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 __ARCH_RISCV_SRC_K210_K210_FPIOA_H
+#define __ARCH_RISCV_SRC_K210_K210_FPIOA_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdint.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define K210_IO_NUMBER 48
+
+#define K210_IO_FUNC_UARTHS_RX 18 /* UART High speed Receiver */
+#define K210_IO_FUNC_UARTHS_TX 19 /* UART High speed Transmitter */
+#define K210_IO_FUNC_GPIOHS0 24 /* GPIO High speed 0 */
+#define K210_IO_FUNC_GPIOHS1 25 /* GPIO High speed 1 */
+#define K210_IO_FUNC_GPIOHS2 26 /* GPIO High speed 2 */
+#define K210_IO_FUNC_GPIOHS3 27 /* GPIO High speed 3 */
+#define K210_IO_FUNC_GPIOHS4 28 /* GPIO High speed 4 */
+#define K210_IO_FUNC_GPIOHS5 29 /* GPIO High speed 5 */
+#define K210_IO_FUNC_GPIOHS6 30 /* GPIO High speed 6 */
+#define K210_IO_FUNC_GPIOHS7 31 /* GPIO High speed 7 */
+#define K210_IO_FUNC_GPIOHS8 32 /* GPIO High speed 8 */
+#define K210_IO_FUNC_GPIOHS9 33 /* GPIO High speed 9 */
+#define K210_IO_FUNC_GPIOHS10 34 /* GPIO High speed 10 */
+#define K210_IO_FUNC_GPIOHS11 35 /* GPIO High speed 11 */
+#define K210_IO_FUNC_GPIOHS12 36 /* GPIO High speed 12 */
+#define K210_IO_FUNC_GPIOHS13 37 /* GPIO High speed 13 */
+#define K210_IO_FUNC_GPIOHS14 38 /* GPIO High speed 14 */
+#define K210_IO_FUNC_GPIOHS15 39 /* GPIO High speed 15 */
+#define K210_IO_FUNC_GPIOHS16 40 /* GPIO High speed 16 */
+#define K210_IO_FUNC_GPIOHS17 41 /* GPIO High speed 17 */
+#define K210_IO_FUNC_GPIOHS18 42 /* GPIO High speed 18 */
+#define K210_IO_FUNC_GPIOHS19 43 /* GPIO High speed 19 */
+#define K210_IO_FUNC_GPIOHS20 44 /* GPIO High speed 20 */
+#define K210_IO_FUNC_GPIOHS21 45 /* GPIO High speed 21 */
+#define K210_IO_FUNC_GPIOHS22 46 /* GPIO High speed 22 */
+#define K210_IO_FUNC_GPIOHS23 47 /* GPIO High speed 23 */
+#define K210_IO_FUNC_GPIOHS24 48 /* GPIO High speed 24 */
+#define K210_IO_FUNC_GPIOHS25 49 /* GPIO High speed 25 */
+#define K210_IO_FUNC_GPIOHS26 50 /* GPIO High speed 26 */
+#define K210_IO_FUNC_GPIOHS27 51 /* GPIO High speed 27 */
+#define K210_IO_FUNC_GPIOHS28 52 /* GPIO High speed 28 */
+#define K210_IO_FUNC_GPIOHS29 53 /* GPIO High speed 29 */
+#define K210_IO_FUNC_GPIOHS30 54 /* GPIO High speed 30 */
+#define K210_IO_FUNC_GPIOHS31 55 /* GPIO High speed 31 */
+
+#define K210_IO_DS(x) (x << 8) /* Driving Selector */
+
+#define K210_IO_OUTPUT_ENABLE (1 << 12)
+#define K210_IO_OUTPUT_INVERT (1 << 13)
+#define K210_IO_INPUT_ENABLE (1 << 20)
+#define K210_IO_INPUT_INVERT (1 << 21)
+#define K210_IO_PULL_DOWN (1 << 16)
+#define K210_IO_PULL_UP (3 << 16)
+#define K210_IO_PULL_UP_STRONG (7 << 16)
+#define K210_IO_SL (1 << 19)
+#define K210_IO_ST (1 << 23)
+
+#define K210_FLAG_GPIOHS (K210_IO_DS(0xf) | K210_IO_OUTPUT_ENABLE | \
+ K210_IO_INPUT_ENABLE | K210_IO_ST)
+
+/****************************************************************************
+ * Public Functions Prototypes
+ ****************************************************************************/
+
+void k210_fpioa_config(uint32_t io, uint32_t ioflag);
+
+#endif /* __ARCH_RISCV_SRC_K210_K210_FPIOA_H */
diff --git a/arch/risc-v/src/k210/k210_gpiohs.c b/arch/risc-v/src/k210/k210_gpiohs.c
new file mode 100644
index 0000000..6ec32e2
--- /dev/null
+++ b/arch/risc-v/src/k210/k210_gpiohs.c
@@ -0,0 +1,83 @@
+/****************************************************************************
+ * arch/risc-v/src/k210/k210_gpiohs.h
+ *
+ * Derives from software originally provided by Canaan Inc
+ *
+ * Copyright 2018 Canaan Inc
+ *
+ * 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 "riscv_arch.h"
+
+#include "k210_memorymap.h"
+#include "k210_gpiohs.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define GPIOHS_INPUT_VAL_OFFSET 0x00
+#define GPIOHS_INPUT_EN_OFFSET 0x04
+#define GPIOHS_OUTPUT_EN_OFFSET 0x08
+#define GPIOHS_OUTPUT_VAL_OFFSET 0x0c
+#define GPIOHS_PULLUP_EN_OFFSET 0x10
+#define GPIOHS_DRIVE_OFFSET 0x14
+
+#define GPIOHS_INPUT (K210_GPIOHS_BASE + GPIOHS_INPUT_VAL_OFFSET)
+#define GPIOHS_INPUT_EN (K210_GPIOHS_BASE + GPIOHS_INPUT_EN_OFFSET)
+#define GPIOHS_OUTPUT (K210_GPIOHS_BASE + GPIOHS_OUTPUT_VAL_OFFSET)
+#define GPIOHS_OUTPUT_EN (K210_GPIOHS_BASE + GPIOHS_OUTPUT_EN_OFFSET)
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void k210_gpiohs_set_direction(uint32_t io, bool dir)
+{
+ uint32_t outbit = dir << io;
+ uint32_t inbit = (!dir) << io;
+ modifyreg32(GPIOHS_OUTPUT_EN, inbit, outbit);
+ modifyreg32(GPIOHS_INPUT_EN, outbit, inbit);
+}
+
+void k210_gpiohs_set_value(uint32_t io, bool val)
+{
+ uint32_t setbit = val << io;
+ uint32_t clrbit = (!val) << io;
+ modifyreg32(GPIOHS_OUTPUT, clrbit, setbit);
+}
+
+bool k210_gpiohs_get_value(uint32_t io)
+{
+ uint32_t reg = getreg32(GPIOHS_INPUT);
+
+ if (reg & (1 << io))
+ {
+ return true;
+ }
+ else
+ {
+ return false;
+ }
+}
diff --git a/arch/risc-v/src/k210/k210_gpiohs.h b/arch/risc-v/src/k210/k210_gpiohs.h
new file mode 100644
index 0000000..1c00fc1
--- /dev/null
+++ b/arch/risc-v/src/k210/k210_gpiohs.h
@@ -0,0 +1,83 @@
+/****************************************************************************
+ * arch/risc-v/src/k210/k210_gpiohs.h
+ *
+ * Derives from software originally provided by Canaan Inc
+ *
+ * Copyright 2018 Canaan Inc
+ *
+ * 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 __ARCH_RISCV_SRC_K210_K210_GPIOHS_H
+#define __ARCH_RISCV_SRC_K210_K210_GPIOHS_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdint.h>
+#include <stdbool.h>
+
+/****************************************************************************
+ * Public Functions Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: k210_gpiohs_set_direction
+ *
+ * Description:
+ * Set gpiohs direction
+ *
+ * Input Parameters:
+ * io - IO number
+ * dir - true for output, false for input
+ *
+ ****************************************************************************/
+
+void k210_gpiohs_set_direction(uint32_t io, bool dir);
+
+/****************************************************************************
+ * Name: k210_gpiohs_set_value
+ *
+ * Description:
+ * Set gpiohs direction
+ *
+ * Input Parameters:
+ * io - IO number
+ * dir - true for high level, false for low level
+ *
+ ****************************************************************************/
+
+void k210_gpiohs_set_value(uint32_t io, bool val);
+
+/****************************************************************************
+ * Name: k210_gpiohs_get_value
+ *
+ * Description:
+ * Get gpiohs level
+ *
+ * Input Parameters:
+ * io - IO number
+ *
+ * Returned Value:
+ * true for high level, false for low level
+ *
+ ****************************************************************************/
+
+bool k210_gpiohs_get_value(uint32_t io);
+
+#endif /* __ARCH_RISCV_SRC_K210_K210_GPIOHS_H */