|
24 | 24 | * THE SOFTWARE. |
25 | 25 | */ |
26 | 26 |
|
27 | | -#include <stdint.h> |
28 | | -#include <string.h> |
29 | | - |
| 27 | +#include "shared-bindings/digitalio/DigitalInOut.h" |
30 | 28 | #include "py/runtime.h" |
31 | | -#include "py/mphal.h" |
32 | 29 |
|
33 | | -#include "hal/hal_gpio.h" |
34 | | - |
35 | | -#include "common-hal/microcontroller/Pin.h" |
36 | | -#include "shared-bindings/digitalio/DigitalInOut.h" |
| 30 | +#include "nrf_gpio.h" |
37 | 31 |
|
38 | 32 | digitalinout_result_t common_hal_digitalio_digitalinout_construct( |
39 | | - digitalio_digitalinout_obj_t* self, const mcu_pin_obj_t* pin) { |
| 33 | + digitalio_digitalinout_obj_t *self, const mcu_pin_obj_t *pin) { |
40 | 34 | self->pin = pin; |
41 | | - hal_gpio_cfg_pin(pin->port, pin->pin, HAL_GPIO_MODE_INPUT, HAL_GPIO_PULL_DISABLED); |
| 35 | + |
| 36 | + nrf_gpio_cfg_input(NRF_GPIO_PIN_MAP(pin->port, pin->pin), NRF_GPIO_PIN_NOPULL); |
| 37 | + |
42 | 38 | return DIGITALINOUT_OK; |
43 | 39 | } |
44 | 40 |
|
45 | | -bool common_hal_digitalio_digitalinout_deinited(digitalio_digitalinout_obj_t* self) { |
| 41 | +bool common_hal_digitalio_digitalinout_deinited(digitalio_digitalinout_obj_t *self) { |
46 | 42 | return self->pin == mp_const_none; |
47 | 43 | } |
48 | 44 |
|
49 | | -void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t* self) { |
| 45 | +void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t *self) { |
50 | 46 | if (common_hal_digitalio_digitalinout_deinited(self)) { |
51 | 47 | return; |
52 | 48 | } |
53 | | - reset_pin(self->pin->pin); |
| 49 | + |
| 50 | + const uint32_t pin = NRF_GPIO_PIN_MAP(self->pin->port, self->pin->pin); |
| 51 | + nrf_gpio_cfg_default(pin); |
| 52 | + |
54 | 53 | self->pin = mp_const_none; |
55 | 54 | } |
56 | 55 |
|
57 | 56 | void common_hal_digitalio_digitalinout_switch_to_input( |
58 | | - digitalio_digitalinout_obj_t* self, digitalio_pull_t pull) { |
59 | | - self->output = false; |
| 57 | + digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) { |
| 58 | + const uint32_t pin = NRF_GPIO_PIN_MAP(self->pin->port, self->pin->pin); |
| 59 | + |
| 60 | + nrf_gpio_cfg_input(pin, NRF_GPIO_PIN_NOPULL); |
60 | 61 |
|
61 | | - hal_gpio_cfg_pin(self->pin->port, self->pin->pin, HAL_GPIO_MODE_INPUT, HAL_GPIO_PULL_DISABLED); |
62 | 62 | common_hal_digitalio_digitalinout_set_pull(self, pull); |
63 | 63 | } |
64 | 64 |
|
65 | 65 | void common_hal_digitalio_digitalinout_switch_to_output( |
66 | | - digitalio_digitalinout_obj_t* self, bool value, |
| 66 | + digitalio_digitalinout_obj_t *self, bool value, |
67 | 67 | digitalio_drive_mode_t drive_mode) { |
68 | | - const uint8_t pin = self->pin->pin; |
| 68 | + const uint32_t pin = NRF_GPIO_PIN_MAP(self->pin->port, self->pin->pin); |
69 | 69 |
|
70 | | - self->output = true; |
71 | 70 | self->open_drain = (drive_mode == DRIVE_MODE_OPEN_DRAIN); |
72 | 71 |
|
73 | | - hal_gpio_cfg_pin(self->pin->port, pin, HAL_GPIO_MODE_OUTPUT, HAL_GPIO_PULL_DISABLED); |
| 72 | + nrf_gpio_cfg_input(pin, NRF_GPIO_PIN_NOPULL); |
| 73 | + |
74 | 74 | common_hal_digitalio_digitalinout_set_value(self, value); |
75 | 75 | } |
76 | 76 |
|
77 | 77 | digitalio_direction_t common_hal_digitalio_digitalinout_get_direction( |
78 | | - digitalio_digitalinout_obj_t* self) { |
79 | | - return self->output? DIRECTION_OUTPUT : DIRECTION_INPUT; |
| 78 | + digitalio_digitalinout_obj_t *self) { |
| 79 | + const uint32_t pin = NRF_GPIO_PIN_MAP(self->pin->port, self->pin->pin); |
| 80 | + |
| 81 | + return (nrf_gpio_pin_dir_get(pin) == NRF_GPIO_PIN_DIR_OUTPUT) ? DIRECTION_OUTPUT : DIRECTION_INPUT; |
80 | 82 | } |
81 | 83 |
|
82 | 84 | void common_hal_digitalio_digitalinout_set_value( |
83 | | - digitalio_digitalinout_obj_t* self, bool value) { |
84 | | - if (value) { |
85 | | - if (self->open_drain) { |
86 | | - hal_gpio_dir_set(self->pin->port, self->pin->pin, HAL_GPIO_MODE_INPUT); |
87 | | - } else { |
88 | | - hal_gpio_pin_set(self->pin->port, self->pin->pin); |
89 | | - hal_gpio_dir_set(self->pin->port, self->pin->pin, HAL_GPIO_MODE_OUTPUT); |
90 | | - } |
| 85 | + digitalio_digitalinout_obj_t *self, bool value) { |
| 86 | + const uint32_t pin = NRF_GPIO_PIN_MAP(self->pin->port, self->pin->pin); |
| 87 | + |
| 88 | + if (value && self->open_drain) { |
| 89 | + nrf_gpio_pin_dir_set(pin, NRF_GPIO_PIN_DIR_INPUT); |
91 | 90 | } else { |
92 | | - hal_gpio_pin_clear(self->pin->port, self->pin->pin); |
93 | | - hal_gpio_dir_set(self->pin->port, self->pin->pin, HAL_GPIO_MODE_OUTPUT); |
| 91 | + nrf_gpio_pin_dir_set(pin, NRF_GPIO_PIN_DIR_OUTPUT); |
| 92 | + nrf_gpio_pin_write(pin, value); |
94 | 93 | } |
95 | 94 | } |
96 | 95 |
|
97 | 96 | bool common_hal_digitalio_digitalinout_get_value( |
98 | | - digitalio_digitalinout_obj_t* self) { |
99 | | - const uint8_t pin = self->pin->pin; |
100 | | - if (!self->output) { |
101 | | - return hal_gpio_pin_read(self->pin); |
102 | | - } else { |
103 | | - if (self->open_drain && hal_gpio_dir_get(self->pin->port, self->pin->pin) == HAL_GPIO_MODE_INPUT) { |
| 97 | + digitalio_digitalinout_obj_t *self) { |
| 98 | + const uint32_t pin = NRF_GPIO_PIN_MAP(self->pin->port, self->pin->pin); |
| 99 | + const nrf_gpio_pin_dir_t dir = nrf_gpio_pin_dir_get(pin); |
| 100 | + |
| 101 | + if (dir == NRF_GPIO_PIN_DIR_INPUT) { |
| 102 | + if (self->open_drain) |
104 | 103 | return true; |
105 | | - } else { |
106 | | - return (GPIO_BASE(self->pin->port)->OUT >> pin) & 1; |
107 | | - } |
| 104 | + |
| 105 | + return nrf_gpio_pin_read(pin); |
108 | 106 | } |
| 107 | + |
| 108 | + return nrf_gpio_pin_out_read(pin); |
109 | 109 | } |
110 | 110 |
|
111 | 111 | void common_hal_digitalio_digitalinout_set_drive_mode( |
112 | | - digitalio_digitalinout_obj_t* self, |
| 112 | + digitalio_digitalinout_obj_t *self, |
113 | 113 | digitalio_drive_mode_t drive_mode) { |
114 | | - bool value = common_hal_digitalio_digitalinout_get_value(self); |
| 114 | + const bool value = common_hal_digitalio_digitalinout_get_value(self); |
115 | 115 | self->open_drain = drive_mode == DRIVE_MODE_OPEN_DRAIN; |
| 116 | + |
116 | 117 | // True is implemented differently between modes so reset the value to make |
117 | 118 | // sure its correct for the new mode. |
118 | | - if (value) { |
| 119 | + if (value) |
119 | 120 | common_hal_digitalio_digitalinout_set_value(self, value); |
120 | | - } |
121 | 121 | } |
122 | 122 |
|
123 | 123 | digitalio_drive_mode_t common_hal_digitalio_digitalinout_get_drive_mode( |
124 | | - digitalio_digitalinout_obj_t* self) { |
125 | | - if (self->open_drain) { |
| 124 | + digitalio_digitalinout_obj_t *self) { |
| 125 | + if (self->open_drain) |
126 | 126 | return DRIVE_MODE_OPEN_DRAIN; |
127 | | - } else { |
128 | | - return DRIVE_MODE_PUSH_PULL; |
129 | | - } |
| 127 | + |
| 128 | + return DRIVE_MODE_PUSH_PULL; |
130 | 129 | } |
131 | 130 |
|
132 | 131 | void common_hal_digitalio_digitalinout_set_pull( |
133 | | - digitalio_digitalinout_obj_t* self, digitalio_pull_t pull) { |
134 | | - hal_gpio_pull_t asf_pull = HAL_GPIO_PULL_DISABLED; |
| 132 | + digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) { |
| 133 | + const uint32_t pin = NRF_GPIO_PIN_MAP(self->pin->port, self->pin->pin); |
| 134 | + nrf_gpio_pin_pull_t hal_pull = NRF_GPIO_PIN_NOPULL; |
| 135 | + |
135 | 136 | switch (pull) { |
136 | 137 | case PULL_UP: |
137 | | - asf_pull = HAL_GPIO_PULL_UP; |
| 138 | + hal_pull = NRF_GPIO_PIN_PULLUP; |
138 | 139 | break; |
139 | 140 | case PULL_DOWN: |
140 | | - asf_pull = HAL_GPIO_PULL_DOWN; |
| 141 | + hal_pull = NRF_GPIO_PIN_PULLDOWN; |
141 | 142 | break; |
142 | 143 | case PULL_NONE: |
143 | 144 | default: |
144 | 145 | break; |
145 | 146 | } |
146 | | - hal_gpio_pull_set(self->pin->port, self->pin->pin, asf_pull); |
| 147 | + |
| 148 | + nrf_gpio_cfg_input(pin, hal_pull); |
147 | 149 | } |
148 | 150 |
|
149 | 151 | digitalio_pull_t common_hal_digitalio_digitalinout_get_pull( |
150 | | - digitalio_digitalinout_obj_t* self) { |
151 | | - uint32_t pin = self->pin->pin; |
152 | | - if (self->output) { |
| 152 | + digitalio_digitalinout_obj_t *self) { |
| 153 | + uint32_t pin = NRF_GPIO_PIN_MAP(self->pin->port, self->pin->pin); |
| 154 | + NRF_GPIO_Type *reg = nrf_gpio_pin_port_decode(&pin); |
| 155 | + |
| 156 | + if (nrf_gpio_pin_dir_get(pin) == NRF_GPIO_PIN_DIR_OUTPUT) { |
153 | 157 | mp_raise_AttributeError("Cannot get pull while in output mode"); |
154 | 158 | return PULL_NONE; |
155 | | - } else { |
156 | | - hal_gpio_pull_t pull = hal_gpio_pull_get(self->pin->port, pin); |
| 159 | + } |
157 | 160 |
|
158 | | - switch(pull) |
159 | | - { |
160 | | - case HAL_GPIO_PULL_UP: |
161 | | - return PULL_UP; |
| 161 | + switch (reg->PIN_CNF[self->pin->pin] & GPIO_PIN_CNF_PULL_Msk) { |
| 162 | + case NRF_GPIO_PIN_PULLUP: |
| 163 | + return PULL_UP; |
162 | 164 |
|
163 | | - case HAL_GPIO_PULL_DOWN: |
164 | | - return PULL_DOWN; |
| 165 | + case NRF_GPIO_PIN_PULLDOWN: |
| 166 | + return PULL_DOWN; |
165 | 167 |
|
166 | | - default: return PULL_NONE; |
167 | | - } |
| 168 | + default: |
| 169 | + return PULL_NONE; |
168 | 170 | } |
169 | 171 | } |
0 commit comments