Skip to content

Commit 7608258

Browse files
author
Kyle Wenner
committed
add check in digital write to switch back to GPIO if not current function
1 parent f09f385 commit 7608258

File tree

1 file changed

+51
-25
lines changed

1 file changed

+51
-25
lines changed

cores/arduino/ard_sup/gpio/ap3_gpio.cpp

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@ ap3_gpio_isr_entry_t gpio_isr_entries[AP3_GPIO_MAX_PADS] = {NULL};
1212
uint8_t gpio_num_isr = 0;
1313

1414
//*****************************************************************************
15-
// Local defines. Copied from am_hal_gpio.c
15+
// Local function declarations
16+
//*****************************************************************************
17+
uint32_t ap3_get_funct_sel(ap3_gpio_pad_t pad);
18+
19+
//*****************************************************************************
20+
// Local defines.
1621
//*****************************************************************************
1722
//
1823
// Generally define GPIO PADREG and GPIOCFG bitfields
19-
//
24+
// Copied from am_hal_gpio.c
2025
#define PADREG_FLD_76_S 6
2126
#define PADREG_FLD_FNSEL_S 3
2227
#define PADREG_FLD_DRVSTR_S 2
@@ -27,6 +32,11 @@ uint8_t gpio_num_isr = 0;
2732
#define GPIOCFG_FLD_OUTCFG_S 1
2833
#define GPIOCFG_FLD_INCFG_S 0
2934

35+
//Additional Defines
36+
#define PADREG_FNSEL_Msk 0x38
37+
#define GPIO_FUNCTION 3
38+
#
39+
3040
ap3_gpio_pad_t ap3_gpio_pin2pad(ap3_gpio_pin_t pin)
3141
{
3242
return ap3_variant_pinmap[pin];
@@ -76,32 +86,34 @@ void padMode(uint8_t pad, am_hal_gpio_pincfg_t mode)
7686
}
7787

7888
// translate Arduino style pin mode function to apollo3 implementation
79-
void pinMode(uint8_t pin, uint8_t mode) {
89+
void pinMode(uint8_t pin, uint8_t mode)
90+
{
8091

8192
am_hal_gpio_pincfg_t pinmode = AP3_GPIO_PINCFG_NULL;
8293

83-
switch (mode) {
84-
case INPUT:
85-
pinmode = AP3_PINCFG_INPUT;
86-
break;
87-
case OUTPUT:
88-
pinmode = AP3_PINCFG_OUTPUT;
89-
break;
90-
case INPUT_PULLUP:
91-
pinmode = AP3_PINCFG_INPUT_PULLUP;
92-
break;
93-
case INPUT_PULLDOWN:
94-
pinmode = AP3_PINCFG_INPUT_PULLDOWN;
95-
break;
96-
case OPEN_DRAIN:
97-
pinmode = AP3_PINCFG_OPEN_DRAIN;
98-
break;
99-
case TRISTATE:
100-
pinmode = AP3_PINCFG_TRISTATE;
101-
break;
102-
default:
103-
//no match, just do nothing
104-
return;
94+
switch (mode)
95+
{
96+
case INPUT:
97+
pinmode = AP3_PINCFG_INPUT;
98+
break;
99+
case OUTPUT:
100+
pinmode = AP3_PINCFG_OUTPUT;
101+
break;
102+
case INPUT_PULLUP:
103+
pinmode = AP3_PINCFG_INPUT_PULLUP;
104+
break;
105+
case INPUT_PULLDOWN:
106+
pinmode = AP3_PINCFG_INPUT_PULLDOWN;
107+
break;
108+
case OPEN_DRAIN:
109+
pinmode = AP3_PINCFG_OPEN_DRAIN;
110+
break;
111+
case TRISTATE:
112+
pinmode = AP3_PINCFG_TRISTATE;
113+
break;
114+
default:
115+
//no match, just do nothing
116+
return;
105117
}
106118

107119
pinMode(pin, pinmode);
@@ -125,6 +137,10 @@ extern void digitalWrite(uint8_t pin, uint8_t val)
125137
{
126138
return;
127139
}
140+
if (ap3_get_funct_sel(pad) != GPIO_FUNCTION)
141+
{
142+
pinMode(pin, OUTPUT);
143+
}
128144
if (val)
129145
{
130146
am_hal_gpio_output_set(ap3_gpio_pin2pad(pin));
@@ -135,6 +151,16 @@ extern void digitalWrite(uint8_t pin, uint8_t val)
135151
}
136152
}
137153

154+
uint32_t ap3_get_funct_sel(ap3_gpio_pad_t pad)
155+
{
156+
uint32_t padregAddr = AM_REGADDR(GPIO, PADREGA) + (pad & ~0x3);
157+
uint32_t padShft = ((pad & 0x3) << 3);
158+
uint32_t functSelShift = PADREG_FLD_FNSEL_S;
159+
uint32_t functSelMask = PADREG_FNSEL_Msk;
160+
161+
return (((AM_REGVAL(padregAddr) >> padShft) & functSelMask) >> functSelShift);
162+
}
163+
138164
extern int digitalRead(uint8_t pin)
139165
{
140166
ap3_gpio_pad_t pad = ap3_gpio_pin2pad(pin);

0 commit comments

Comments
 (0)