Skip to content

Commit 0e8d146

Browse files
committed
wip
1 parent 9b98ad7 commit 0e8d146

File tree

2 files changed

+74
-5
lines changed

2 files changed

+74
-5
lines changed

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

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,69 @@
2828
#include "nrf_gpio.h"
2929
#include "py/mphal.h"
3030

31-
bool common_hal_mcu_pin_is_free(const mcu_pin_obj_t *pin) {
32-
return true;
33-
}
34-
3531
void reset_all_pins(void) {
36-
for (uint32_t pin = 0; pin < NUMBER_OF_PINS; ++pin)
32+
for (uint32_t pin = 0; pin < NUMBER_OF_PINS; ++pin) {
3733
nrf_gpio_cfg_default(pin);
34+
}
35+
36+
#ifdef MICROPY_HW_NEOPIXEL
37+
neopixel_in_use = false;
38+
#endif
39+
#ifdef MICROPY_HW_APA102_MOSI
40+
apa102_sck_in_use = false;
41+
apa102_mosi_in_use = false;
42+
#endif
43+
44+
// After configuring SWD because it may be shared.
45+
#ifdef SPEAKER_ENABLE_PIN
46+
speaker_enable_in_use = false;
47+
// TODO set pin to out and turn off.
48+
#endif
49+
}
50+
51+
void claim_pin(const mcu_pin_obj_t* pin) {
52+
#ifdef MICROPY_HW_NEOPIXEL
53+
if (pin == MICROPY_HW_NEOPIXEL) {
54+
neopixel_in_use = true;
55+
}
56+
#endif
57+
#ifdef MICROPY_HW_APA102_MOSI
58+
if (pin == MICROPY_HW_APA102_MOSI) {
59+
apa102_mosi_in_use = true;
60+
}
61+
if (pin == MICROPY_HW_APA102_SCK) {
62+
apa102_sck_in_use = true;
63+
}
64+
#endif
65+
66+
#ifdef SPEAKER_ENABLE_PIN
67+
if (pin == SPEAKER_ENABLE_PIN) {
68+
speaker_enable_in_use = true;
69+
}
70+
#endif
3871
}
3972

73+
bool common_hal_mcu_pin_is_free(const mcu_pin_obj_t *pin) {
74+
#ifdef MICROPY_HW_NEOPIXEL
75+
if (pin == MICROPY_HW_NEOPIXEL) {
76+
return !neopixel_in_use;
77+
}
78+
#endif
79+
#ifdef MICROPY_HW_APA102_MOSI
80+
if (pin == MICROPY_HW_APA102_MOSI) {
81+
return !apa102_mosi_in_use;
82+
}
83+
if (pin == MICROPY_HW_APA102_SCK) {
84+
return !apa102_sck_in_use;
85+
}
86+
#endif
87+
88+
#ifdef SPEAKER_ENABLE_PIN
89+
if (pin == SPEAKER_ENABLE_PIN) {
90+
return !speaker_enable_in_use;
91+
}
92+
#endif
93+
94+
// TODO check pin enable.
95+
return true;
96+
}

ports/nrf/common-hal/microcontroller/Pin.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,19 @@
3030
#include "nrf_pin.h"
3131
#include "py/mphal.h"
3232

33+
#ifdef MICROPY_HW_NEOPIXEL
34+
extern bool neopixel_in_use;
35+
#endif
36+
#ifdef MICROPY_HW_APA102_MOSI
37+
extern bool apa102_sck_in_use;
38+
extern bool apa102_mosi_in_use;
39+
#endif
40+
3341
#define mcu_pin_obj_t pin_obj_t
3442
void reset_all_pins(void);
43+
// reset_pin takes the pin number instead of the pointer so that objects don't
44+
// need to store a full pointer.
45+
void reset_pin(uint8_t pin);
46+
void claim_pin(const mcu_pin_obj_t* pin);
3547

3648
#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_MICROCONTROLLER_PIN_H

0 commit comments

Comments
 (0)