From 76082589d1973a22673e4dd720204f7c901dbcff Mon Sep 17 00:00:00 2001 From: Kyle Wenner Date: Thu, 9 Apr 2020 01:31:14 -0600 Subject: [PATCH 1/3] add check in digital write to switch back to GPIO if not current function --- cores/arduino/ard_sup/gpio/ap3_gpio.cpp | 76 +++++++++++++++++-------- 1 file changed, 51 insertions(+), 25 deletions(-) diff --git a/cores/arduino/ard_sup/gpio/ap3_gpio.cpp b/cores/arduino/ard_sup/gpio/ap3_gpio.cpp index a7991296..50bfccd5 100644 --- a/cores/arduino/ard_sup/gpio/ap3_gpio.cpp +++ b/cores/arduino/ard_sup/gpio/ap3_gpio.cpp @@ -12,11 +12,16 @@ ap3_gpio_isr_entry_t gpio_isr_entries[AP3_GPIO_MAX_PADS] = {NULL}; uint8_t gpio_num_isr = 0; //***************************************************************************** -// Local defines. Copied from am_hal_gpio.c +// Local function declarations +//***************************************************************************** +uint32_t ap3_get_funct_sel(ap3_gpio_pad_t pad); + +//***************************************************************************** +// Local defines. //***************************************************************************** // // Generally define GPIO PADREG and GPIOCFG bitfields -// +// Copied from am_hal_gpio.c #define PADREG_FLD_76_S 6 #define PADREG_FLD_FNSEL_S 3 #define PADREG_FLD_DRVSTR_S 2 @@ -27,6 +32,11 @@ uint8_t gpio_num_isr = 0; #define GPIOCFG_FLD_OUTCFG_S 1 #define GPIOCFG_FLD_INCFG_S 0 +//Additional Defines +#define PADREG_FNSEL_Msk 0x38 +#define GPIO_FUNCTION 3 +# + ap3_gpio_pad_t ap3_gpio_pin2pad(ap3_gpio_pin_t pin) { return ap3_variant_pinmap[pin]; @@ -76,32 +86,34 @@ void padMode(uint8_t pad, am_hal_gpio_pincfg_t mode) } // translate Arduino style pin mode function to apollo3 implementation -void pinMode(uint8_t pin, uint8_t mode) { +void pinMode(uint8_t pin, uint8_t mode) +{ am_hal_gpio_pincfg_t pinmode = AP3_GPIO_PINCFG_NULL; - switch (mode) { - case INPUT: - pinmode = AP3_PINCFG_INPUT; - break; - case OUTPUT: - pinmode = AP3_PINCFG_OUTPUT; - break; - case INPUT_PULLUP: - pinmode = AP3_PINCFG_INPUT_PULLUP; - break; - case INPUT_PULLDOWN: - pinmode = AP3_PINCFG_INPUT_PULLDOWN; - break; - case OPEN_DRAIN: - pinmode = AP3_PINCFG_OPEN_DRAIN; - break; - case TRISTATE: - pinmode = AP3_PINCFG_TRISTATE; - break; - default: - //no match, just do nothing - return; + switch (mode) + { + case INPUT: + pinmode = AP3_PINCFG_INPUT; + break; + case OUTPUT: + pinmode = AP3_PINCFG_OUTPUT; + break; + case INPUT_PULLUP: + pinmode = AP3_PINCFG_INPUT_PULLUP; + break; + case INPUT_PULLDOWN: + pinmode = AP3_PINCFG_INPUT_PULLDOWN; + break; + case OPEN_DRAIN: + pinmode = AP3_PINCFG_OPEN_DRAIN; + break; + case TRISTATE: + pinmode = AP3_PINCFG_TRISTATE; + break; + default: + //no match, just do nothing + return; } pinMode(pin, pinmode); @@ -125,6 +137,10 @@ extern void digitalWrite(uint8_t pin, uint8_t val) { return; } + if (ap3_get_funct_sel(pad) != GPIO_FUNCTION) + { + pinMode(pin, OUTPUT); + } if (val) { am_hal_gpio_output_set(ap3_gpio_pin2pad(pin)); @@ -135,6 +151,16 @@ extern void digitalWrite(uint8_t pin, uint8_t val) } } +uint32_t ap3_get_funct_sel(ap3_gpio_pad_t pad) +{ + uint32_t padregAddr = AM_REGADDR(GPIO, PADREGA) + (pad & ~0x3); + uint32_t padShft = ((pad & 0x3) << 3); + uint32_t functSelShift = PADREG_FLD_FNSEL_S; + uint32_t functSelMask = PADREG_FNSEL_Msk; + + return (((AM_REGVAL(padregAddr) >> padShft) & functSelMask) >> functSelShift); +} + extern int digitalRead(uint8_t pin) { ap3_gpio_pad_t pad = ap3_gpio_pin2pad(pin); From c88d62f3638257e55a5d0f0bae144b3b94e9882d Mon Sep 17 00:00:00 2001 From: Kyle Wenner Date: Thu, 9 Apr 2020 15:05:49 -0600 Subject: [PATCH 2/3] made gpio function selection check inline --- cores/arduino/ard_sup/gpio/ap3_gpio.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cores/arduino/ard_sup/gpio/ap3_gpio.cpp b/cores/arduino/ard_sup/gpio/ap3_gpio.cpp index 50bfccd5..b217a517 100644 --- a/cores/arduino/ard_sup/gpio/ap3_gpio.cpp +++ b/cores/arduino/ard_sup/gpio/ap3_gpio.cpp @@ -14,7 +14,7 @@ uint8_t gpio_num_isr = 0; //***************************************************************************** // Local function declarations //***************************************************************************** -uint32_t ap3_get_funct_sel(ap3_gpio_pad_t pad); +static inline uint32_t ap3_get_funct_sel(ap3_gpio_pad_t pad); //***************************************************************************** // Local defines. @@ -151,7 +151,7 @@ extern void digitalWrite(uint8_t pin, uint8_t val) } } -uint32_t ap3_get_funct_sel(ap3_gpio_pad_t pad) +static inline uint32_t ap3_get_funct_sel(ap3_gpio_pad_t pad) { uint32_t padregAddr = AM_REGADDR(GPIO, PADREGA) + (pad & ~0x3); uint32_t padShft = ((pad & 0x3) << 3); From 11c110eb45b0722f29dd6f3ec8e4ffe00a195860 Mon Sep 17 00:00:00 2001 From: oclyke Date: Wed, 15 Apr 2020 14:47:55 -0600 Subject: [PATCH 3/3] remove automatic pinMode in digitalWrite leaving the ap3_get_funct_sel function for future utility --- cores/arduino/ard_sup/gpio/ap3_gpio.cpp | 5 ----- 1 file changed, 5 deletions(-) diff --git a/cores/arduino/ard_sup/gpio/ap3_gpio.cpp b/cores/arduino/ard_sup/gpio/ap3_gpio.cpp index b217a517..4d56230e 100644 --- a/cores/arduino/ard_sup/gpio/ap3_gpio.cpp +++ b/cores/arduino/ard_sup/gpio/ap3_gpio.cpp @@ -35,7 +35,6 @@ static inline uint32_t ap3_get_funct_sel(ap3_gpio_pad_t pad); //Additional Defines #define PADREG_FNSEL_Msk 0x38 #define GPIO_FUNCTION 3 -# ap3_gpio_pad_t ap3_gpio_pin2pad(ap3_gpio_pin_t pin) { @@ -137,10 +136,6 @@ extern void digitalWrite(uint8_t pin, uint8_t val) { return; } - if (ap3_get_funct_sel(pad) != GPIO_FUNCTION) - { - pinMode(pin, OUTPUT); - } if (val) { am_hal_gpio_output_set(ap3_gpio_pin2pad(pin));