This is an automated email from the ASF dual-hosted git repository. GUIDINGLI pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 027078466dbff86b8efe777da518e1af0ff3de22 Author: Saravanan Sekar <[email protected]> AuthorDate: Mon Nov 23 13:36:59 2026 +0530 arch/tricore: Add a GPIO interface for the AURIX ports. Only the UART driver sets up port pins, with code of its own. Add tricore_gpio_config, tricore_gpio_write and tricore_gpio_read in the common AURIX directory, with a pin set that describes the port, pin, direction, alternate function or pull device, open drain, initial level and pad of a pin, and their TC3xx and TC4xx implementations. On TC4xx the pad configuration is written outside the RUN state of the port protection, which is restored afterwards. The chip and board code include the interface as aurix/tricore_gpio.h through the common directory, which is in their include paths already. Signed-off-by: Saravanan Sekar <[email protected]> --- arch/tricore/src/common/aurix/tricore_gpio.h | 136 +++++++++++++++++++++++++ arch/tricore/src/tc3x/CMakeLists.txt | 2 +- arch/tricore/src/tc3x/Make.defs | 1 + arch/tricore/src/tc3x/tc3x_gpio.c | 145 ++++++++++++++++++++++++++ arch/tricore/src/tc4x/CMakeLists.txt | 2 +- arch/tricore/src/tc4x/Make.defs | 1 + arch/tricore/src/tc4x/tc4x_gpio.c | 147 +++++++++++++++++++++++++++ 7 files changed, 432 insertions(+), 2 deletions(-) diff --git a/arch/tricore/src/common/aurix/tricore_gpio.h b/arch/tricore/src/common/aurix/tricore_gpio.h new file mode 100644 index 00000000000..52ffec19f0e --- /dev/null +++ b/arch/tricore/src/common/aurix/tricore_gpio.h @@ -0,0 +1,136 @@ +/**************************************************************************** + * arch/tricore/src/common/aurix/tricore_gpio.h + * + * 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. + * + ****************************************************************************/ + +#ifndef __ARCH_TRICORE_SRC_COMMON_AURIX_TRICORE_GPIO_H +#define __ARCH_TRICORE_SRC_COMMON_AURIX_TRICORE_GPIO_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#ifndef __ASSEMBLY__ +# include <stdbool.h> +# include <stdint.h> +#endif + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* A pin set describes one port pin and its configuration: + * + * bits 3:0 pin of the port + * bits 9:4 port number + * bit 10 output, else input + * bits 14:11 output: alternate function, 0 is the GPIO (TC3x: 0 to 7) + * input: pull device + * bit 15 open drain output + * bit 16 initial level of a GPIO output + * bits 19:17 pad driver mode (TC3x: 0 to 3) + * bits 22:20 pad level selection (TC3x: 0 to 3) + */ + +#define GPIO_PIN_SHIFT 0 +#define GPIO_PIN_MASK (0xf << GPIO_PIN_SHIFT) +#define GPIO_PIN(n) ((n) << GPIO_PIN_SHIFT) + +#define GPIO_PORT_SHIFT 4 +#define GPIO_PORT_MASK (0x3f << GPIO_PORT_SHIFT) +#define GPIO_PORT(n) ((n) << GPIO_PORT_SHIFT) + +#define GPIO_INPUT (0 << 10) +#define GPIO_OUTPUT (1 << 10) + +#define GPIO_ALT_SHIFT 11 +#define GPIO_ALT_MASK (0xf << GPIO_ALT_SHIFT) +#define GPIO_ALT(n) ((n) << GPIO_ALT_SHIFT) + +#define GPIO_FLOAT (0 << GPIO_ALT_SHIFT) +#define GPIO_PULLDOWN (1 << GPIO_ALT_SHIFT) +#define GPIO_PULLUP (2 << GPIO_ALT_SHIFT) + +#define GPIO_OPENDRAIN (1 << 15) +#define GPIO_OUTPUT_SET (1 << 16) + +#define GPIO_PADDRV_SHIFT 17 +#define GPIO_PADDRV_MASK (0x7 << GPIO_PADDRV_SHIFT) +#define GPIO_PADDRV(n) ((n) << GPIO_PADDRV_SHIFT) + +#define GPIO_PADLEVEL_SHIFT 20 +#define GPIO_PADLEVEL_MASK (0x7 << GPIO_PADLEVEL_SHIFT) +#define GPIO_PADLEVEL(n) ((n) << GPIO_PADLEVEL_SHIFT) + +/* Fields of a pin set */ + +#define GPIO_PIN_OF(p) (((p) & GPIO_PIN_MASK) >> GPIO_PIN_SHIFT) +#define GPIO_PORT_OF(p) (((p) & GPIO_PORT_MASK) >> GPIO_PORT_SHIFT) +#define GPIO_ALT_OF(p) (((p) & GPIO_ALT_MASK) >> GPIO_ALT_SHIFT) +#define GPIO_PADDRV_OF(p) (((p) & GPIO_PADDRV_MASK) >> \ + GPIO_PADDRV_SHIFT) +#define GPIO_PADLEVEL_OF(p) (((p) & GPIO_PADLEVEL_MASK) >> \ + GPIO_PADLEVEL_SHIFT) +#define GPIO_IS_OUTPUT(p) (((p) & GPIO_OUTPUT) != 0) + +#ifndef __ASSEMBLY__ + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Name: tricore_gpio_config + * + * Description: + * Configure a port pin as described by a pin set. A GPIO output first + * gets its initial level, then the pad, then the direction. + * + * Returned Value: + * OK, or -EINVAL if the pin set is not valid for the chip. + * + ****************************************************************************/ + +int tricore_gpio_config(uint32_t pinset); + +/**************************************************************************** + * Name: tricore_gpio_write + * + * Description: + * Set the output level of the pin of a pin set. + * + ****************************************************************************/ + +void tricore_gpio_write(uint32_t pinset, bool value); + +/**************************************************************************** + * Name: tricore_gpio_read + * + * Description: + * Read the input level of the pin of a pin set. + * + ****************************************************************************/ + +bool tricore_gpio_read(uint32_t pinset); + +#endif /* __ASSEMBLY__ */ +#endif /* __ARCH_TRICORE_SRC_COMMON_AURIX_TRICORE_GPIO_H */ diff --git a/arch/tricore/src/tc3x/CMakeLists.txt b/arch/tricore/src/tc3x/CMakeLists.txt index 105dcf348d0..758573fed4c 100644 --- a/arch/tricore/src/tc3x/CMakeLists.txt +++ b/arch/tricore/src/tc3x/CMakeLists.txt @@ -20,7 +20,7 @@ # # ############################################################################## -set(SRCS tc3x_timerisr.c tc3x_wdt.c tc3x_clockconfig.c) +set(SRCS tc3x_timerisr.c tc3x_wdt.c tc3x_clockconfig.c tc3x_gpio.c) target_sources(arch PRIVATE ${SRCS}) diff --git a/arch/tricore/src/tc3x/Make.defs b/arch/tricore/src/tc3x/Make.defs index a9820890995..34920a91a96 100644 --- a/arch/tricore/src/tc3x/Make.defs +++ b/arch/tricore/src/tc3x/Make.defs @@ -23,6 +23,7 @@ CHIP_CSRCS += tc3x_timerisr.c CHIP_CSRCS += tc3x_wdt.c CHIP_CSRCS += tc3x_clockconfig.c +CHIP_CSRCS += tc3x_gpio.c include common/aurix/Make.defs diff --git a/arch/tricore/src/tc3x/tc3x_gpio.c b/arch/tricore/src/tc3x/tc3x_gpio.c new file mode 100644 index 00000000000..bc926ae7adb --- /dev/null +++ b/arch/tricore/src/tc3x/tc3x_gpio.c @@ -0,0 +1,145 @@ +/**************************************************************************** + * arch/tricore/src/tc3x/tc3x_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 <nuttx/config.h> + +#include <stdbool.h> +#include <stdint.h> +#include <errno.h> + +#include <nuttx/irq.h> +#include <nuttx/spinlock.h> + +#include "tricore_internal.h" +#include "aurix/tricore_gpio.h" +#include "tc3x_wdt.h" +#include "hardware/tc3x_port.h" + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static spinlock_t g_tc3x_gpio_lock = SP_UNLOCKED; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static void tc3x_gpio_modify(uintptr_t reg, uint32_t clear, uint32_t set) +{ + putreg32((getreg32(reg) & ~clear) | set, reg); +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: tricore_gpio_config + * + * Description: + * Configure a port pin as described by a pin set. The pad driver is + * protected by the CPU ENDINIT. + * + ****************************************************************************/ + +int tricore_gpio_config(uint32_t pinset) +{ + uintptr_t port = TC3X_PORT_BASE(GPIO_PORT_OF(pinset)); + int pin = GPIO_PIN_OF(pinset); + irqstate_t flags; + uint32_t pc; + uint32_t pd; + int shift; + + if (GPIO_IS_OUTPUT(pinset)) + { + if (GPIO_ALT_OF(pinset) > TC3X_PORT_PC_ALT_MAX) + { + return -EINVAL; + } + + pc = TC3X_PORT_PC_OUTPUT | GPIO_ALT_OF(pinset); + if ((pinset & GPIO_OPENDRAIN) != 0) + { + pc |= TC3X_PORT_PC_OPENDRAIN; + } + } + else + { + pc = GPIO_ALT_OF(pinset) & TC3X_PORT_PC_PULL_MASK; + } + + pd = (GPIO_PADLEVEL_OF(pinset) << TC3X_PORT_PDR_PL_SHIFT) | + GPIO_PADDRV_OF(pinset); + if (pd > TC3X_PORT_PDR_MASK) + { + return -EINVAL; + } + + flags = spin_lock_irqsave(&g_tc3x_gpio_lock); + + if (GPIO_IS_OUTPUT(pinset) && GPIO_ALT_OF(pinset) == 0) + { + tricore_gpio_write(pinset, (pinset & GPIO_OUTPUT_SET) != 0); + } + + shift = TC3X_PORT_PDR_SHIFT(pin); + tc3x_wdt_cpu_endinit(false); + tc3x_gpio_modify(TC3X_PORT_PDR(port, pin), TC3X_PORT_PDR_MASK << shift, + pd << shift); + tc3x_wdt_cpu_endinit(true); + + shift = TC3X_PORT_IOCR_SHIFT(pin); + tc3x_gpio_modify(TC3X_PORT_IOCR(port, pin), TC3X_PORT_IOCR_MASK << shift, + (pc << TC3X_PORT_PC_SHIFT) << shift); + + spin_unlock_irqrestore(&g_tc3x_gpio_lock, flags); + return OK; +} + +/**************************************************************************** + * Name: tricore_gpio_write + ****************************************************************************/ + +void tricore_gpio_write(uint32_t pinset, bool value) +{ + int pin = GPIO_PIN_OF(pinset); + + putreg32(value ? TC3X_PORT_OMR_SET(pin) : TC3X_PORT_OMR_CLR(pin), + TC3X_PORT_BASE(GPIO_PORT_OF(pinset)) + TC3X_PORT_OMR_OFFSET); +} + +/**************************************************************************** + * Name: tricore_gpio_read + ****************************************************************************/ + +bool tricore_gpio_read(uint32_t pinset) +{ + uintptr_t in = TC3X_PORT_BASE(GPIO_PORT_OF(pinset)) + TC3X_PORT_IN_OFFSET; + + return (getreg32(in) & (1 << GPIO_PIN_OF(pinset))) != 0; +} diff --git a/arch/tricore/src/tc4x/CMakeLists.txt b/arch/tricore/src/tc4x/CMakeLists.txt index e7473bd247d..11410189f7f 100644 --- a/arch/tricore/src/tc4x/CMakeLists.txt +++ b/arch/tricore/src/tc4x/CMakeLists.txt @@ -20,7 +20,7 @@ # # ############################################################################## -set(SRCS tc4x_timerisr.c tc4x_wdt.c tc4x_clockconfig.c tc4x_ram.c) +set(SRCS tc4x_timerisr.c tc4x_wdt.c tc4x_clockconfig.c tc4x_ram.c tc4x_gpio.c) target_sources(arch PRIVATE ${SRCS}) diff --git a/arch/tricore/src/tc4x/Make.defs b/arch/tricore/src/tc4x/Make.defs index c7afa7a37d3..1235fb7732e 100644 --- a/arch/tricore/src/tc4x/Make.defs +++ b/arch/tricore/src/tc4x/Make.defs @@ -23,6 +23,7 @@ CHIP_CSRCS += tc4x_timerisr.c CHIP_CSRCS += tc4x_wdt.c CHIP_CSRCS += tc4x_clockconfig.c +CHIP_CSRCS += tc4x_gpio.c CHIP_CSRCS += tc4x_ram.c include common/aurix/Make.defs diff --git a/arch/tricore/src/tc4x/tc4x_gpio.c b/arch/tricore/src/tc4x/tc4x_gpio.c new file mode 100644 index 00000000000..af548da3882 --- /dev/null +++ b/arch/tricore/src/tc4x/tc4x_gpio.c @@ -0,0 +1,147 @@ +/**************************************************************************** + * arch/tricore/src/tc4x/tc4x_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 <nuttx/config.h> + +#include <stdbool.h> +#include <stdint.h> +#include <errno.h> + +#include <nuttx/irq.h> +#include <nuttx/spinlock.h> + +#include "tricore_internal.h" +#include "aurix/tricore_gpio.h" +#include "hardware/tc4x_port.h" + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static spinlock_t g_tc4x_gpio_lock = SP_UNLOCKED; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: tc4x_gpio_unprotect + * + * Description: + * The pad configuration of a port can only be written while the + * protection of its access group is not in the RUN state. Leave RUN for + * CONFIG if needed and return true if it has to be restored. + * + ****************************************************************************/ + +static bool tc4x_gpio_unprotect(uintptr_t prote) +{ + if ((getreg32(prote) & TC4X_PORT_PROT_STATE_MASK) != TC4X_PORT_PROT_RUN) + { + return false; + } + + putreg32(TC4X_PORT_PROT_CONFIG | TC4X_PORT_PROT_SWEN, prote); + return true; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: tricore_gpio_config + * + * Description: + * Configure a port pin as described by a pin set. Direction, mode, pad + * driver and level are all in the DRVCFG register of the pin. + * + ****************************************************************************/ + +int tricore_gpio_config(uint32_t pinset) +{ + uintptr_t port = TC4X_PORT_BASE(GPIO_PORT_OF(pinset)); + uintptr_t drvcfg = TC4X_PORT_DRVCFG(port, GPIO_PIN_OF(pinset)); + irqstate_t flags; + uint32_t regval; + bool restore; + + regval = (GPIO_ALT_OF(pinset) << TC4X_PORT_DRVCFG_MODE_SHIFT) | + (GPIO_PADDRV_OF(pinset) << TC4X_PORT_DRVCFG_PD_SHIFT) | + (GPIO_PADLEVEL_OF(pinset) << TC4X_PORT_DRVCFG_PL_SHIFT); + + if (GPIO_IS_OUTPUT(pinset)) + { + regval |= TC4X_PORT_DRVCFG_DIR; + if ((pinset & GPIO_OPENDRAIN) != 0) + { + regval |= TC4X_PORT_DRVCFG_OD; + } + } + + flags = spin_lock_irqsave(&g_tc4x_gpio_lock); + + if (GPIO_IS_OUTPUT(pinset) && GPIO_ALT_OF(pinset) == 0) + { + tricore_gpio_write(pinset, (pinset & GPIO_OUTPUT_SET) != 0); + } + + restore = tc4x_gpio_unprotect(TC4X_PORT_PROTE(port)); + putreg32((getreg32(drvcfg) & ~(TC4X_PORT_DRVCFG_MODE_MASK | + TC4X_PORT_DRVCFG_PAD_MASK)) | regval, + drvcfg); + if (restore) + { + putreg32(TC4X_PORT_PROT_RUN | TC4X_PORT_PROT_SWEN, + TC4X_PORT_PROTE(port)); + } + + spin_unlock_irqrestore(&g_tc4x_gpio_lock, flags); + return OK; +} + +/**************************************************************************** + * Name: tricore_gpio_write + ****************************************************************************/ + +void tricore_gpio_write(uint32_t pinset, bool value) +{ + int pin = GPIO_PIN_OF(pinset); + + putreg32(value ? TC4X_PORT_OMR_SET(pin) : TC4X_PORT_OMR_CLR(pin), + TC4X_PORT_BASE(GPIO_PORT_OF(pinset)) + TC4X_PORT_OMR_OFFSET); +} + +/**************************************************************************** + * Name: tricore_gpio_read + ****************************************************************************/ + +bool tricore_gpio_read(uint32_t pinset) +{ + uintptr_t in = TC4X_PORT_BASE(GPIO_PORT_OF(pinset)) + TC4X_PORT_IN_OFFSET; + + return (getreg32(in) & (1 << GPIO_PIN_OF(pinset))) != 0; +}
