From 98574564b8a5e7c3c8bc592b5137e773c6fe3d61 Mon Sep 17 00:00:00 2001 From: Luke Wren Date: Mon, 22 Mar 2021 17:54:33 +0000 Subject: [PATCH] =?UTF-8?q?Add=20gpio=5Fget=5Fout=5Flevel()=20accessor,=20?= =?UTF-8?q?and=20correct=20SIO=20GPIO=5FOUT=20struct=20ty=E2=80=A6=20(#247?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add gpio_get_out_level() accessor, and correct SIO GPIO_OUT struct type from WO to RW * Clean up ambiguous-looking parentheses --- .../include/hardware/structs/sio.h | 4 ++-- .../hardware_gpio/include/hardware/gpio.h | 20 +++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/rp2040/hardware_structs/include/hardware/structs/sio.h b/src/rp2040/hardware_structs/include/hardware/structs/sio.h index 400083f..bc277af 100644 --- a/src/rp2040/hardware_structs/include/hardware/structs/sio.h +++ b/src/rp2040/hardware_structs/include/hardware/structs/sio.h @@ -17,7 +17,7 @@ typedef struct { io_ro_32 gpio_hi_in; uint32_t _pad; - io_wo_32 gpio_out; + io_rw_32 gpio_out; io_wo_32 gpio_set; io_wo_32 gpio_clr; io_wo_32 gpio_togl; @@ -27,7 +27,7 @@ typedef struct { io_wo_32 gpio_oe_clr; io_wo_32 gpio_oe_togl; - io_wo_32 gpio_hi_out; + io_rw_32 gpio_hi_out; io_wo_32 gpio_hi_set; io_wo_32 gpio_hi_clr; io_wo_32 gpio_hi_togl; diff --git a/src/rp2_common/hardware_gpio/include/hardware/gpio.h b/src/rp2_common/hardware_gpio/include/hardware/gpio.h index 1a7314c..019ec3d 100644 --- a/src/rp2_common/hardware_gpio/include/hardware/gpio.h +++ b/src/rp2_common/hardware_gpio/include/hardware/gpio.h @@ -415,6 +415,26 @@ static inline void gpio_put(uint gpio, bool value) { gpio_clr_mask(mask); } +/*! \brief Determine whether a GPIO is currently driven high or low + * \ingroup hardware_gpio + * + * This function returns the high/low output level most recently assigned to a + * GPIO via gpio_put() or similar. This is the value that is presented outward + * to the IO muxing, *not* the input level back from the pad (which can be + * read using gpio_get()). + * + * To avoid races, this function must not be used for read-modify-write + * sequences when driving GPIOs -- instead functions like gpio_put() should be + * used to atomically update GPIOs. This accessor is intended for debug use + * only. + * + * \param gpio GPIO number + * \return true if the GPIO output level is high, false if low. + */ +static inline bool gpio_get_out_level(uint gpio) { + return !!(sio_hw->gpio_out & (1u << gpio)); +} + // ---------------------------------------------------------------------------- // Direction // ----------------------------------------------------------------------------