Skip to content

Commit 5e94915

Browse files
committed
ports: analog: Improve common-hal portability for digitalio & microcontroller
- *microcontroller/Pin.c*: Use mxc_gpio_reva_regs_t for standardizing different GPIO register structures. - *microcontroller/Processor.c*: - Guard MAX32690 GPIO4 specific operations with preproc macros. - Use mxc_gpio_reva_regs_t for direct register operations to improve portability. Signed-off-by: Brandon-Hurst <brandon.hurst97@gmail.com>
1 parent 751191c commit 5e94915

3 files changed

Lines changed: 47 additions & 35 deletions

File tree

ports/analog/common-hal/digitalio/DigitalInOut.c

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "max32_port.h"
1212
#include "gpio_reva.h"
13+
#include "gpio_reva_regs.h"
1314
#include "mxc_errors.h"
1415

1516
extern mxc_gpio_regs_t *gpio_ports[NUM_GPIO_PORTS];
@@ -60,13 +61,7 @@ digitalinout_result_t common_hal_digitalio_digitalinout_switch_to_input(
6061

6162
int err = E_NO_ERROR;
6263

63-
if (self->pin->port == 4) {
64-
// Set GPIO(s) to input mode
65-
MXC_MCR->gpio4_ctrl &= ~GPIO4_OUTEN_MASK(mask);
66-
MXC_MCR->outen &= ~GPIO4_AFEN_MASK(mask);
67-
} else {
68-
err = MXC_GPIO_RevA_SetAF((mxc_gpio_reva_regs_t *)port, MXC_GPIO_FUNC_IN, mask);
69-
}
64+
err = MXC_GPIO_RevA_SetAF((mxc_gpio_reva_regs_t *)port, MXC_GPIO_FUNC_IN, mask);
7065
if (err != E_NO_ERROR) {
7166
return DIGITALINOUT_PIN_BUSY;
7267
}
@@ -82,12 +77,7 @@ digitalinout_result_t common_hal_digitalio_digitalinout_switch_to_output(
8277
self->open_drain = (drive_mode == DRIVE_MODE_OPEN_DRAIN);
8378

8479
// Set GPIO(s) to output mode
85-
if (self->pin->port == 4) {
86-
MXC_MCR->gpio4_ctrl |= GPIO4_OUTEN_MASK(mask);
87-
MXC_MCR->outen &= ~GPIO4_AFEN_MASK(mask);
88-
} else {
89-
MXC_GPIO_RevA_SetAF((mxc_gpio_reva_regs_t *)port, MXC_GPIO_FUNC_OUT, mask);
90-
}
80+
MXC_GPIO_RevA_SetAF((mxc_gpio_reva_regs_t *)port, MXC_GPIO_FUNC_OUT, mask);
9181

9282
common_hal_digitalio_digitalinout_set_value(self, value);
9383

@@ -97,15 +87,24 @@ digitalinout_result_t common_hal_digitalio_digitalinout_switch_to_output(
9787
digitalio_direction_t common_hal_digitalio_digitalinout_get_direction(
9888
digitalio_digitalinout_obj_t *self) {
9989

100-
mxc_gpio_regs_t *port = gpio_ports[self->pin->port];
90+
mxc_gpio_reva_regs_t *port = (mxc_gpio_reva_regs_t *)gpio_ports[self->pin->port];
10191
uint32_t mask = self->pin->mask;
10292

10393
// Open drain must be considered output for CircuitPython API to work properly
10494
if (self->open_drain) {
10595
return DIRECTION_OUTPUT;
10696
}
10797

108-
if (self->pin->port < 4) {
98+
#ifdef MAX32690
99+
if (self->pin->port == 4) {
100+
if (MXC_MCR->gpio4_ctrl & GPIO4_OUTEN_MASK(mask)) {
101+
return DIRECTION_OUTPUT;
102+
} else {
103+
return DIRECTION_INPUT;
104+
}
105+
} else
106+
#endif
107+
{
109108
// Check that I/O mode is enabled and we don't have in AND out on at the same time
110109
MP_STATIC_ASSERT_NONCONSTEXPR(!((port->en0 & mask) && (port->inen & mask) && (port->outen & mask)));
111110

@@ -117,12 +116,6 @@ digitalio_direction_t common_hal_digitalio_digitalinout_get_direction(
117116
} else {
118117
return DIRECTION_INPUT;
119118
}
120-
} else {
121-
if (MXC_MCR->gpio4_ctrl & GPIO4_OUTEN_MASK(mask)) {
122-
return DIRECTION_OUTPUT;
123-
} else {
124-
return DIRECTION_INPUT;
125-
}
126119
}
127120
}
128121

@@ -145,10 +138,13 @@ void common_hal_digitalio_digitalinout_set_value(
145138
} else {
146139
// can't use common_hal_switch_to_output b/c it calls this function
147140
// set the GPIO to output, low
141+
#ifdef MAX32690
148142
if (self->pin->port == 4) {
149143
MXC_MCR->gpio4_ctrl |= GPIO4_OUTEN_MASK(mask);
150144
MXC_MCR->outen &= ~GPIO4_AFEN_MASK(mask);
151-
} else {
145+
} else
146+
#endif
147+
{
152148
MXC_GPIO_RevA_SetAF((mxc_gpio_reva_regs_t *)port, MXC_GPIO_FUNC_OUT, mask);
153149
}
154150
MXC_GPIO_OutClr(port, mask);
@@ -174,9 +170,11 @@ bool common_hal_digitalio_digitalinout_get_value(digitalio_digitalinout_obj_t *s
174170
}
175171

176172
if (dir == DIRECTION_INPUT) {
173+
#ifdef MAX32690
177174
if (self->pin->port == 4) {
178175
return (bool)(MXC_MCR->gpio4_ctrl & GPIO4_DATAIN_MASK(mask));
179176
}
177+
#endif
180178
return MXC_GPIO_InGet(port, mask) && mask;
181179
} else {
182180
return MXC_GPIO_OutGet(port, mask) && mask;
@@ -210,9 +208,10 @@ digitalio_drive_mode_t common_hal_digitalio_digitalinout_get_drive_mode(
210208
digitalinout_result_t common_hal_digitalio_digitalinout_set_pull(
211209
digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) {
212210

213-
mxc_gpio_regs_t *port = gpio_ports[self->pin->port];
211+
mxc_gpio_reva_regs_t *port = (mxc_gpio_reva_regs_t *)gpio_ports[self->pin->port];
214212
uint32_t mask = self->pin->mask;
215213

214+
#ifdef MAX32690
216215
// GPIO4 handling
217216
if (self->pin->port == 4) {
218217
switch (pull) {
@@ -236,7 +235,9 @@ digitalinout_result_t common_hal_digitalio_digitalinout_set_pull(
236235
break;
237236
}
238237
return DIGITALINOUT_OK;
239-
} else {
238+
} else
239+
#endif
240+
{
240241
// padctrl registers only work in input mode
241242
if ((mask & port->en0) & (mask & ~(port->outen))) {
242243
// PULL_NONE, PULL_UP, or PULL_DOWN
@@ -268,12 +269,13 @@ digitalinout_result_t common_hal_digitalio_digitalinout_set_pull(
268269
digitalio_pull_t common_hal_digitalio_digitalinout_get_pull(
269270
digitalio_digitalinout_obj_t *self) {
270271

271-
mxc_gpio_regs_t *port = gpio_ports[self->pin->port];
272+
mxc_gpio_reva_regs_t *port = (mxc_gpio_reva_regs_t *)gpio_ports[self->pin->port];
272273
uint32_t mask = self->pin->mask;
273274

274275
bool pin_padctrl0 = (port->padctrl0) & (mask);
275276
bool pin_padctrl1 = (port->padctrl1) & (mask);
276277

278+
#ifdef MAX32690
277279
if (self->pin->port == 4) {
278280
if (MXC_MCR->gpio4_ctrl & GPIO4_PULLDIS_MASK(mask)) {
279281
return PULL_NONE;
@@ -284,7 +286,9 @@ digitalio_pull_t common_hal_digitalio_digitalinout_get_pull(
284286
return PULL_DOWN;
285287
}
286288
}
287-
} else {
289+
} else
290+
#endif
291+
{
288292
if ((pin_padctrl0) && !(pin_padctrl1)) {
289293
return PULL_UP;
290294
} else if (!(pin_padctrl0) && pin_padctrl1) {

ports/analog/common-hal/microcontroller/Pin.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "max32_port.h"
1414

1515
#include "common-hal/microcontroller/Pin.h"
16+
#include "gpio_reva_regs.h"
1617

1718
static uint32_t claimed_pins[NUM_GPIO_PORTS];
1819

@@ -41,29 +42,32 @@ void reset_pin_number(uint8_t pin_port, uint8_t pin_pad) {
4142
return;
4243
}
4344

45+
// Cast to REVA Regs for portability
46+
mxc_gpio_reva_regs_t *gpio_regs = (mxc_gpio_reva_regs_t *)gpio_ports[pin_port];
47+
4448
uint32_t mask = 1 << (pin_pad);
4549

4650
/** START: RESET LOGIC for GPIOs */
4751
// Switch to I/O mode first
48-
gpio_ports[pin_port]->en0_set = mask;
52+
gpio_regs->en0_set = mask;
4953

5054
// set GPIO configuration enable bits to I/O
51-
gpio_ports[pin_port]->en0_clr = mask;
52-
gpio_ports[pin_port]->en1_clr = mask;
53-
gpio_ports[pin_port]->en2_clr = mask;
55+
gpio_regs->en0_clr = mask;
56+
gpio_regs->en1_clr = mask;
57+
gpio_regs->en2_clr = mask;
5458

5559
// enable input mode GPIOn_INEN.pin = 1
56-
gpio_ports[pin_port]->inen |= mask;
60+
gpio_regs->inen |= mask;
5761

5862
// High Impedance mode enable (GPIOn_PADCTRL1 = 0, _PADCTRL0 = 0), pu/pd disable
59-
gpio_ports[pin_port]->padctrl0 &= ~mask;
60-
gpio_ports[pin_port]->padctrl1 &= ~mask;
63+
gpio_regs->padctrl0 &= ~mask;
64+
gpio_regs->padctrl1 &= ~mask;
6165

6266
// Output mode disable GPIOn_OUTEN = 0
63-
gpio_ports[pin_port]->outen |= mask;
67+
gpio_regs->outen |= mask;
6468

6569
// Interrupt disable GPIOn_INTEN = 0
66-
gpio_ports[pin_port]->inten &= ~mask;
70+
gpio_regs->inten &= ~mask;
6771
/** END: RESET LOGIC for GPIOs */
6872
}
6973

ports/analog/common-hal/microcontroller/Processor.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ uint32_t common_hal_mcu_processor_get_frequency(void) {
3434
// NOTE: COMMON_HAL_MCU_PROCESSOR_UID_LENGTH is defined in mpconfigboard.h
3535
// Use this per device to make sure raw_id is an appropriate minimum number of bytes
3636
void common_hal_mcu_processor_get_uid(uint8_t raw_id[]) {
37+
#if defined(MAX32690) || defined(MAX32665)
3738
MXC_SYS_GetUSN(raw_id, NULL); // NULL checksum will not be verified by AES
39+
#elif defined(MAX32650)
40+
MXC_SYS_GetUSN(raw_id, 13);
41+
#endif
3842
return;
3943
}
4044

0 commit comments

Comments
 (0)