|
8 | 8 | #include "mxc_sys.h" |
9 | 9 | #include "max32690.h" |
10 | 10 | #include "gpio.h" |
| 11 | +#include "gpio_regs.h" |
| 12 | + |
| 13 | +// Structs to represent GPIO ports & valid pins/pads |
| 14 | +#ifdef MAX32690 |
| 15 | +// todo: special constraints are applied to GPIO4 for MAX32690. Tend to these later (low prior) |
| 16 | +static mxc_gpio_regs_t* ports[NUM_GPIO_PORTS] = { MXC_GPIO0, MXC_GPIO1, MXC_GPIO2, MXC_GPIO3}; |
| 17 | +#endif |
| 18 | + |
| 19 | +static uint32_t claimed_pins[NUM_GPIO_PORTS]; |
| 20 | +static uint32_t never_reset_pins[NUM_GPIO_PORTS]; |
| 21 | + |
| 22 | +#define INVALID_PIN 0xFF // id for invalid pin |
11 | 23 |
|
12 | 24 | void reset_all_pins(void) { |
13 | | - // todo: this is not a good method for this long-term |
14 | | - // Pins should be individually reset to account for never_reset pins like SWD |
| 25 | + // reset all pins except for never_reset_pins |
15 | 26 | for (int i = 0; i < NUM_GPIO_PORTS; i++) { |
16 | | - MXC_GPIO_Reset(i); |
| 27 | + for (int j = 0; j < 32; j++) { |
| 28 | + if (!(never_reset_pins[i] & (1 << j))) { |
| 29 | + reset_pin_number(i, j); |
| 30 | + } |
| 31 | + } |
| 32 | + // set claimed pins to never_reset pins |
| 33 | + claimed_pins[i] = never_reset_pins[i]; |
17 | 34 | } |
18 | 35 | } |
19 | 36 |
|
20 | | -// todo: Implement |
21 | | -void reset_pin_number(uint8_t pin) { |
22 | | -} |
| 37 | +void reset_pin_number(uint8_t pin_port, uint8_t pin_pad) { |
| 38 | + if (pin_port == INVALID_PIN || pin_port > NUM_GPIO_PORTS) { |
| 39 | + return; |
| 40 | + } |
23 | 41 |
|
24 | | -// todo: Implement |
25 | | -void claim_pin(const mcu_pin_obj_t *pin) { |
26 | | - return; |
27 | | -} |
| 42 | + uint32_t mask = 1 << (pin_pad); |
28 | 43 |
|
29 | | -// todo: Implement |
30 | | -bool pin_number_is_free(uint8_t pin_number) { |
31 | | - return true; |
32 | | -} |
| 44 | + /** START: RESET LOGIC for GPIOs */ |
| 45 | + // Switch to I/O mode first |
| 46 | + ports[pin_port]->en0_set = mask; |
| 47 | + |
| 48 | + // set GPIO configuration enable bits to I/O |
| 49 | + ports[pin_port]->en0_clr = mask; |
| 50 | + ports[pin_port]->en1_clr = mask; |
| 51 | + ports[pin_port]->en2_clr = mask; |
| 52 | + |
| 53 | + // enable input mode GPIOn_INEN.pin = 1 |
| 54 | + ports[pin_port]->inen |= mask; |
| 55 | + |
| 56 | + // High Impedance mode enable (GPIOn_PADCTRL1 = 0, _PADCTRL0 = 0), pu/pd disable |
| 57 | + ports[pin_port]->padctrl0 &= ~mask; |
| 58 | + ports[pin_port]->padctrl1 &= ~mask; |
33 | 59 |
|
| 60 | + // Output mode disable GPIOn_OUTEN = 0 |
| 61 | + ports[pin_port]->outen |= mask; |
34 | 62 |
|
35 | | -// todo: Implement |
36 | | -void never_reset_pin_number(uint8_t pin_number) { |
37 | | - return; |
| 63 | + // Interrupt disable GPIOn_INTEN = 0 |
| 64 | + ports[pin_port]->inten &= ~mask; |
| 65 | + /** END: RESET LOGIC for GPIOs */ |
38 | 66 | } |
39 | 67 |
|
40 | | -//todo: implement |
41 | 68 | uint8_t common_hal_mcu_pin_number(const mcu_pin_obj_t *pin) { |
42 | | - return 0; |
| 69 | + if (pin == NULL) { |
| 70 | + return INVALID_PIN; |
| 71 | + } |
| 72 | + |
| 73 | + // most max32 gpio ports have 32 pins |
| 74 | + // todo: create a struct to encode # of pins for each port, since some GPIO ports differ |
| 75 | + return pin->port * 32 + pin->pad; |
43 | 76 | } |
44 | 77 |
|
45 | | -// todo: implement |
46 | 78 | bool common_hal_mcu_pin_is_free(const mcu_pin_obj_t *pin) { |
47 | | - return true; |
| 79 | + if (pin == NULL) { |
| 80 | + return true; |
| 81 | + } |
| 82 | + return !(claimed_pins[pin->port] & (pin->pad)); |
48 | 83 | } |
49 | 84 |
|
50 | 85 | void common_hal_never_reset_pin(const mcu_pin_obj_t *pin) { |
| 86 | + if ((pin != NULL) && (pin->pad != INVALID_PIN)) { |
| 87 | + never_reset_pins[pin->port] |= (1 << pin->pad); |
| 88 | + |
| 89 | + // any never reset pin must also be claimed |
| 90 | + claimed_pins[pin->port] |= (1 << pin->pad); |
| 91 | + } |
51 | 92 | } |
52 | 93 |
|
53 | 94 | void common_hal_reset_pin(const mcu_pin_obj_t *pin) { |
54 | | -} |
| 95 | + if (pin == NULL) { |
| 96 | + return; |
| 97 | + } |
55 | 98 |
|
56 | | -void common_hal_mcu_pin_claim(const mcu_pin_obj_t *pin) { |
| 99 | + reset_pin_number(pin->port, pin->pad); |
57 | 100 | } |
58 | 101 |
|
59 | | -void common_hal_mcu_pin_claim_number(uint8_t pin_no) { |
| 102 | +void common_hal_mcu_pin_claim(const mcu_pin_obj_t *pin) { |
| 103 | + if (pin == NULL) { |
| 104 | + return; |
| 105 | + } |
| 106 | + claimed_pins[pin->port] |= (1 << pin->pad); |
60 | 107 | } |
61 | 108 |
|
62 | 109 | void common_hal_mcu_pin_reset_number(uint8_t pin_no) { |
| 110 | + reset_pin_number(pin_no / 32, pin_no & 32); |
63 | 111 | } |
0 commit comments