@@ -34,8 +34,6 @@ digitalinout_result_t common_hal_digitalio_digitalinout_construct(
3434 digitalio_digitalinout_obj_t * self , const mcu_pin_obj_t * pin ) {
3535 claim_pin (pin );
3636 self -> pin = pin ;
37- self -> output = false;
38- self -> open_drain = false;
3937
4038 nrf_gpio_cfg_input (pin -> number , NRF_GPIO_PIN_NOPULL );
4139
@@ -58,70 +56,61 @@ void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t *self
5856
5957void common_hal_digitalio_digitalinout_switch_to_input (
6058 digitalio_digitalinout_obj_t * self , digitalio_pull_t pull ) {
61- self -> output = false;
6259 nrf_gpio_cfg_input (self -> pin -> number , NRF_GPIO_PIN_NOPULL );
6360 common_hal_digitalio_digitalinout_set_pull (self , pull );
6461}
6562
6663void common_hal_digitalio_digitalinout_switch_to_output (
6764 digitalio_digitalinout_obj_t * self , bool value ,
6865 digitalio_drive_mode_t drive_mode ) {
69- self -> output = true;
70- self -> open_drain = (drive_mode == DRIVE_MODE_OPEN_DRAIN );
71-
72- nrf_gpio_cfg_input (self -> pin -> number , NRF_GPIO_PIN_NOPULL );
7366
67+ common_hal_digitalio_digitalinout_set_drive_mode (self , drive_mode );
7468 common_hal_digitalio_digitalinout_set_value (self , value );
7569}
7670
7771digitalio_direction_t common_hal_digitalio_digitalinout_get_direction (
7872 digitalio_digitalinout_obj_t * self ) {
79- return self -> output ? DIRECTION_OUTPUT : DIRECTION_INPUT ;
73+
74+ return (nrf_gpio_pin_dir_get (self -> pin -> number ) == NRF_GPIO_PIN_DIR_INPUT )
75+ ? DIRECTION_INPUT : DIRECTION_OUTPUT ;
8076}
8177
8278void common_hal_digitalio_digitalinout_set_value (
8379 digitalio_digitalinout_obj_t * self , bool value ) {
84- if (value && self -> open_drain ) {
85- nrf_gpio_pin_dir_set (self -> pin -> number , NRF_GPIO_PIN_DIR_INPUT );
86- } else {
87- nrf_gpio_pin_dir_set (self -> pin -> number , NRF_GPIO_PIN_DIR_OUTPUT );
88- nrf_gpio_pin_write (self -> pin -> number , value );
89- }
80+ nrf_gpio_pin_write (self -> pin -> number , value );
9081}
9182
9283bool common_hal_digitalio_digitalinout_get_value (
9384 digitalio_digitalinout_obj_t * self ) {
94- if (nrf_gpio_pin_dir_get (self -> pin -> number ) == NRF_GPIO_PIN_DIR_INPUT ) {
95- if (self -> open_drain ) {
96- return true;
97- }
98-
99- return nrf_gpio_pin_read (self -> pin -> number );
100- }
101-
102- return nrf_gpio_pin_out_read (self -> pin -> number );
85+ return (nrf_gpio_pin_dir_get (self -> pin -> number ) == NRF_GPIO_PIN_DIR_INPUT )
86+ ? nrf_gpio_pin_read (self -> pin -> number )
87+ : nrf_gpio_pin_out_read (self -> pin -> number );
10388}
10489
10590void common_hal_digitalio_digitalinout_set_drive_mode (
10691 digitalio_digitalinout_obj_t * self ,
10792 digitalio_drive_mode_t drive_mode ) {
108- const bool value = common_hal_digitalio_digitalinout_get_value (self );
109- self -> open_drain = drive_mode == DRIVE_MODE_OPEN_DRAIN ;
110-
111- // True is implemented differently between modes so reset the value to make
112- // sure its correct for the new mode.
113- if (value ) {
114- common_hal_digitalio_digitalinout_set_value (self , value );
115- }
93+ nrf_gpio_cfg (self -> pin -> number ,
94+ NRF_GPIO_PIN_DIR_OUTPUT ,
95+ NRF_GPIO_PIN_INPUT_DISCONNECT ,
96+ NRF_GPIO_PIN_NOPULL ,
97+ drive_mode == DRIVE_MODE_OPEN_DRAIN ? NRF_GPIO_PIN_H0D1 : NRF_GPIO_PIN_H0H1 ,
98+ NRF_GPIO_PIN_NOSENSE );
11699}
117100
118101digitalio_drive_mode_t common_hal_digitalio_digitalinout_get_drive_mode (
119102 digitalio_digitalinout_obj_t * self ) {
120- if (self -> open_drain ) {
103+ uint32_t pin = self -> pin -> number ;
104+ // Changes pin to be a relative pin number in port.
105+ NRF_GPIO_Type * reg = nrf_gpio_pin_port_decode (& pin );
106+
107+ switch ((reg -> PIN_CNF [pin ] & GPIO_PIN_CNF_DRIVE_Msk ) >> GPIO_PIN_CNF_DRIVE_Pos ) {
108+ case NRF_GPIO_PIN_S0D1 :
109+ case NRF_GPIO_PIN_H0D1 :
121110 return DRIVE_MODE_OPEN_DRAIN ;
111+ default :
112+ return DRIVE_MODE_PUSH_PULL ;
122113 }
123-
124- return DRIVE_MODE_PUSH_PULL ;
125114}
126115
127116void common_hal_digitalio_digitalinout_set_pull (
@@ -151,16 +140,13 @@ digitalio_pull_t common_hal_digitalio_digitalinout_get_pull(
151140
152141 if (nrf_gpio_pin_dir_get (self -> pin -> number ) == NRF_GPIO_PIN_DIR_OUTPUT ) {
153142 mp_raise_AttributeError (translate ("Cannot get pull while in output mode" ));
154- return PULL_NONE ;
155143 }
156144
157- switch (reg -> PIN_CNF [pin ] & GPIO_PIN_CNF_PULL_Msk ) {
145+ switch (( reg -> PIN_CNF [pin ] & GPIO_PIN_CNF_PULL_Msk ) >> GPIO_PIN_CNF_PULL_Pos ) {
158146 case NRF_GPIO_PIN_PULLUP :
159147 return PULL_UP ;
160-
161148 case NRF_GPIO_PIN_PULLDOWN :
162149 return PULL_DOWN ;
163-
164150 default :
165151 return PULL_NONE ;
166152 }
0 commit comments