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 2021/03/08 08:06:14 UTC

[incubator-nuttx] 01/03: arm/rp2040: Add rp2040_gpio_init/put/get/setdir()

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 a8d269df98fcc4408faa8fd4dc837c65631fe9be
Author: Yuichi Nakamura <y....@gmail.com>
AuthorDate: Fri Mar 5 23:11:38 2021 +0900

    arm/rp2040: Add rp2040_gpio_init/put/get/setdir()
---
 arch/arm/src/rp2040/rp2040_gpio.c | 12 +++++----
 arch/arm/src/rp2040/rp2040_gpio.h | 56 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 63 insertions(+), 5 deletions(-)

diff --git a/arch/arm/src/rp2040/rp2040_gpio.c b/arch/arm/src/rp2040/rp2040_gpio.c
index a2aacbb..5f39cc2 100644
--- a/arch/arm/src/rp2040/rp2040_gpio.c
+++ b/arch/arm/src/rp2040/rp2040_gpio.c
@@ -35,13 +35,8 @@
 #include "arm_arch.h"
 
 #include "chip.h"
-
 #include "rp2040_gpio.h"
 
-#include "hardware/rp2040_pads_bank0.h"
-#include "hardware/rp2040_io_bank0.h"
-#include "hardware/rp2040_sio.h"
-
 /****************************************************************************
  * Private Functions
  ****************************************************************************/
@@ -67,3 +62,10 @@ void rp2040_gpio_set_pulls(uint32_t gpio, int up, int down)
                 RP2040_PADS_BANK0_GPIO_PUE | RP2040_PADS_BANK0_GPIO_PDE,
                 RP2040_PADS_BANK0_GPIO(gpio));
 }
+
+void rp2040_gpio_init(uint32_t gpio)
+{
+  rp2040_gpio_setdir(gpio, false);
+  rp2040_gpio_put(gpio, false);
+  rp2040_gpio_set_function(gpio, RP2040_GPIO_FUNC_SIO);
+}
diff --git a/arch/arm/src/rp2040/rp2040_gpio.h b/arch/arm/src/rp2040/rp2040_gpio.h
index 18dc3de..afcde83 100644
--- a/arch/arm/src/rp2040/rp2040_gpio.h
+++ b/arch/arm/src/rp2040/rp2040_gpio.h
@@ -27,10 +27,26 @@
 
 #include <nuttx/config.h>
 
+#include "hardware/rp2040_sio.h"
+#include "hardware/rp2040_io_bank0.h"
+#include "hardware/rp2040_pads_bank0.h"
+
 /****************************************************************************
  * Pre-processor Definitions
  ****************************************************************************/
 
+#define RP2040_GPIO_FUNC_JTAG       RP2040_IO_BANK0_GPIO_CTRL_FUNCSEL_JTAG
+#define RP2040_GPIO_FUNC_SPI        RP2040_IO_BANK0_GPIO_CTRL_FUNCSEL_SPI
+#define RP2040_GPIO_FUNC_UART       RP2040_IO_BANK0_GPIO_CTRL_FUNCSEL_UART
+#define RP2040_GPIO_FUNC_I2C        RP2040_IO_BANK0_GPIO_CTRL_FUNCSEL_I2C
+#define RP2040_GPIO_FUNC_PWM        RP2040_IO_BANK0_GPIO_CTRL_FUNCSEL_PWM
+#define RP2040_GPIO_FUNC_SIO        RP2040_IO_BANK0_GPIO_CTRL_FUNCSEL_SIO
+#define RP2040_GPIO_FUNC_PIO0       RP2040_IO_BANK0_GPIO_CTRL_FUNCSEL_PIO0
+#define RP2040_GPIO_FUNC_PIO1       RP2040_IO_BANK0_GPIO_CTRL_FUNCSEL_PIO1
+#define RP2040_GPIO_FUNC_CLOCKS     RP2040_IO_BANK0_GPIO_CTRL_FUNCSEL_CLOCKS
+#define RP2040_GPIO_FUNC_USB        RP2040_IO_BANK0_GPIO_CTRL_FUNCSEL_USB
+#define RP2040_GPIO_FUNC_NULL       RP2040_IO_BANK0_GPIO_CTRL_FUNCSEL_NULL
+
 /****************************************************************************
  * Public Types
  ****************************************************************************/
@@ -51,11 +67,51 @@ extern "C"
 #endif
 
 /****************************************************************************
+ * Inline Functions
+ ****************************************************************************/
+
+static inline void rp2040_gpio_put(uint32_t gpio, int set)
+{
+  uint32_t value = 1 << gpio;
+
+  if (set)
+    {
+      putreg32(value, RP2040_SIO_GPIO_OUT_SET);
+    }
+  else
+    {
+      putreg32(value, RP2040_SIO_GPIO_OUT_CLR);
+    }
+}
+
+static inline bool rp2040_gpio_get(uint32_t gpio)
+{
+  uint32_t value = 1 << gpio;
+
+  return (getreg32(RP2040_SIO_GPIO_IN) & value) != 0;
+}
+
+static inline void rp2040_gpio_setdir(uint32_t gpio, int out)
+{
+  uint32_t value = 1 << gpio;
+
+  if (out)
+    {
+      putreg32(value, RP2040_SIO_GPIO_OE_SET);
+    }
+  else
+    {
+      putreg32(value, RP2040_SIO_GPIO_OE_CLR);
+    }
+}
+
+/****************************************************************************
  * Public Function Prototypes
  ****************************************************************************/
 
 void rp2040_gpio_set_function(uint32_t gpio, uint32_t func);
 void rp2040_gpio_set_pulls(uint32_t gpio, int up, int down);
+void rp2040_gpio_init(uint32_t gpio);
 
 #undef EXTERN
 #if defined(__cplusplus)