Skip to content

Commit 9ace50a

Browse files
authored
Merge pull request #1177 from dhalbert/gpio-fixs
use open-drain capabilities on GPIO; clean up board init; set correct GPIO voltage
2 parents d9f62a4 + e335c74 commit 9ace50a

File tree

16 files changed

+271
-101
lines changed

16 files changed

+271
-101
lines changed

ports/nrf/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,10 @@ SRC_C += \
118118
lib/utils/sys_stdio_mphal.c \
119119
nrfx/hal/nrf_nvmc.c \
120120
nrfx/mdk/system_$(MCU_SUB_VARIANT).c \
121+
peripherals/nrf/cache.c \
122+
peripherals/nrf/clocks.c \
121123
peripherals/nrf/$(MCU_CHIP)/pins.c \
124+
peripherals/nrf/$(MCU_CHIP)/power.c \
122125
supervisor/shared/memory.c
123126

124127
DRIVERS_SRC_C += $(addprefix modules/,\

ports/nrf/boards/feather_nrf52832/board.c

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -31,41 +31,12 @@
3131

3232
#include "boards/board.h"
3333

34-
// Must match temp register in bootloader
35-
#define BOOTLOADER_VERSION_REGISTER NRF_TIMER2->CC[0]
36-
uint32_t bootloaderVersion = 0;
37-
3834
void board_init(void) {
39-
// Retrieve bootloader version
40-
bootloaderVersion = BOOTLOADER_VERSION_REGISTER;
4135
}
4236

43-
// Check the status of the two buttons on CircuitPlayground Express. If both are
44-
// pressed, then boot into user safe mode.
4537
bool board_requests_safe_mode(void) {
46-
// gpio_set_pin_function(PIN_PA14, GPIO_PIN_FUNCTION_OFF);
47-
// gpio_set_pin_direction(PIN_PA14, GPIO_DIRECTION_IN);
48-
// gpio_set_pin_pull_mode(PIN_PA14, GPIO_PULL_DOWN);
49-
//
50-
// gpio_set_pin_function(PIN_PA28, GPIO_PIN_FUNCTION_OFF);
51-
// gpio_set_pin_direction(PIN_PA28, GPIO_DIRECTION_IN);
52-
// gpio_set_pin_pull_mode(PIN_PA28, GPIO_PULL_DOWN);
53-
// bool safe_mode = gpio_get_pin_level(PIN_PA14) &&
54-
// gpio_get_pin_level(PIN_PA28);
55-
// reset_pin_number(PIN_PA14);
56-
// reset_pin_number(PIN_PA28);
57-
// return safe_mode;
58-
5938
return false;
6039
}
6140

6241
void reset_board(void) {
63-
// uint8_t empty[30];
64-
// memset(empty, 0, 30);
65-
// digitalio_digitalinout_obj_t neopixel_pin;
66-
// common_hal_digitalio_digitalinout_construct(&neopixel_pin, &pin_PB23);
67-
// common_hal_digitalio_digitalinout_switch_to_output(&neopixel_pin, false,
68-
// DRIVE_MODE_PUSH_PULL);
69-
// common_hal_neopixel_write(&neopixel_pin, empty, 30);
70-
// common_hal_digitalio_digitalinout_deinit(&neopixel_pin);
7142
}

ports/nrf/boards/feather_nrf52840_express/board.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,11 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#include <string.h>
28-
#include <stdbool.h>
29-
30-
#include "nrf.h"
31-
3227
#include "boards/board.h"
28+
#include "usb.h"
3329

3430
void board_init(void) {
35-
31+
usb_init();
3632
}
3733

3834
bool board_requests_safe_mode(void) {

ports/nrf/boards/pca10040/board.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
#include "boards/board.h"
3333

3434
void board_init(void) {
35-
3635
}
3736

3837
bool board_requests_safe_mode(void) {

ports/nrf/boards/pca10056/board.c

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,10 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#include <string.h>
28-
#include <stdbool.h>
2927
#include "boards/board.h"
30-
#include "nrfx.h"
3128
#include "usb.h"
3229

3330
void board_init(void) {
34-
35-
// Clock
36-
NRF_CLOCK->LFCLKSRC = (uint32_t)((CLOCK_LFCLKSRC_SRC_Xtal << CLOCK_LFCLKSRC_SRC_Pos) & CLOCK_LFCLKSRC_SRC_Msk);
37-
NRF_CLOCK->TASKS_LFCLKSTART = 1UL;
38-
3931
usb_init();
4032
}
4133

@@ -46,8 +38,3 @@ bool board_requests_safe_mode(void) {
4638
void reset_board(void) {
4739

4840
}
49-
50-
51-
52-
53-

ports/nrf/boards/pca10059/board.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@
3131
#include "usb.h"
3232

3333
void board_init(void) {
34-
35-
// Clock
36-
NRF_CLOCK->LFCLKSRC = (uint32_t)((CLOCK_LFCLKSRC_SRC_Xtal << CLOCK_LFCLKSRC_SRC_Pos) & CLOCK_LFCLKSRC_SRC_Msk);
37-
NRF_CLOCK->TASKS_LFCLKSTART = 1UL;
38-
3934
usb_init();
4035
}
4136

@@ -46,8 +41,3 @@ bool board_requests_safe_mode(void) {
4641
void reset_board(void) {
4742

4843
}
49-
50-
51-
52-
53-

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

Lines changed: 24 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -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

5957
void 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

6663
void 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

7771
digitalio_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

8278
void 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

9283
bool 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

10590
void 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

118101
digitalio_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

127116
void 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
}

ports/nrf/common-hal/digitalio/DigitalInOut.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@
3232
typedef struct {
3333
mp_obj_base_t base;
3434
const mcu_pin_obj_t *pin;
35-
bool output;
36-
bool open_drain;
3735
} digitalio_digitalinout_obj_t;
3836

3937
#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_DIGITALIO_DIGITALINOUT_H

ports/nrf/peripherals/nrf/cache.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018 Dan Halbert for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "nrfx.h"
28+
29+
// Turn off cache and invalidate all data in it.
30+
void nrf_peripherals_disable_and_clear_cache(void) {
31+
// Disabling cache also invalidates all cache entries.
32+
NRF_NVMC->ICACHECNF &= ~(1 << NVMC_ICACHECNF_CACHEEN_Pos);
33+
}
34+
35+
// Enable cache
36+
void nrf_peripherals_enable_cache(void) {
37+
NRF_NVMC->ICACHECNF |= 1 << NVMC_ICACHECNF_CACHEEN_Pos;
38+
}

ports/nrf/peripherals/nrf/cache.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018 Dan Halbert for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
void nrf_peripherals_disable_and_clear_cache(void);
28+
void nrf_peripherals_enable_cache(void);

ports/nrf/peripherals/nrf/clocks.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
/*
3+
* This file is part of the Micro Python project, http://micropython.org/
4+
*
5+
* The MIT License (MIT)
6+
*
7+
* Copyright (c) 2018 Dan Halbert for Adafruit Industries
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy
10+
* of this software and associated documentation files (the "Software"), to deal
11+
* in the Software without restriction, including without limitation the rights
12+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
* copies of the Software, and to permit persons to whom the Software is
14+
* furnished to do so, subject to the following conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be included in
17+
* all copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#include "nrfx.h"
29+
30+
void nrf_peripherals_clocks_init(void) {
31+
// Set low-frequency clock source to be crystal. If there's a crystalless board, this will need to be
32+
// generalized.
33+
NRF_CLOCK->LFCLKSRC = (uint32_t)((CLOCK_LFCLKSRC_SRC_Xtal << CLOCK_LFCLKSRC_SRC_Pos) & CLOCK_LFCLKSRC_SRC_Msk);
34+
NRF_CLOCK->TASKS_LFCLKSTART = 1UL;
35+
}

ports/nrf/peripherals/nrf/clocks.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018 Dan Halbert for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
void nrf_peripherals_clocks_init(void);

0 commit comments

Comments
 (0)