summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Halbert <halbert@halwitz.org>2018-08-21 20:28:58 -0400
committerDan Halbert <halbert@halwitz.org>2018-08-31 18:05:55 -0400
commit0e8d14618477de053417fcf7b8c2e72df589cb69 (patch)
treebec4eaccd3c5d50be0625b437a41f8ba1bc6a2b0
parent9b98ad779468676c3d5f1efdc06b454aaed7c407 (diff)
wip
-rw-r--r--ports/nrf/common-hal/microcontroller/Pin.c67
-rw-r--r--ports/nrf/common-hal/microcontroller/Pin.h12
2 files changed, 74 insertions, 5 deletions
diff --git a/ports/nrf/common-hal/microcontroller/Pin.c b/ports/nrf/common-hal/microcontroller/Pin.c
index 837e813ac..1b7ac0d83 100644
--- a/ports/nrf/common-hal/microcontroller/Pin.c
+++ b/ports/nrf/common-hal/microcontroller/Pin.c
@@ -28,12 +28,69 @@
#include "nrf_gpio.h"
#include "py/mphal.h"
-bool common_hal_mcu_pin_is_free(const mcu_pin_obj_t *pin) {
- return true;
-}
-
void reset_all_pins(void) {
- for (uint32_t pin = 0; pin < NUMBER_OF_PINS; ++pin)
+ for (uint32_t pin = 0; pin < NUMBER_OF_PINS; ++pin) {
nrf_gpio_cfg_default(pin);
+ }
+
+ #ifdef MICROPY_HW_NEOPIXEL
+ neopixel_in_use = false;
+ #endif
+ #ifdef MICROPY_HW_APA102_MOSI
+ apa102_sck_in_use = false;
+ apa102_mosi_in_use = false;
+ #endif
+
+ // After configuring SWD because it may be shared.
+ #ifdef SPEAKER_ENABLE_PIN
+ speaker_enable_in_use = false;
+ // TODO set pin to out and turn off.
+ #endif
+}
+
+void claim_pin(const mcu_pin_obj_t* pin) {
+ #ifdef MICROPY_HW_NEOPIXEL
+ if (pin == MICROPY_HW_NEOPIXEL) {
+ neopixel_in_use = true;
+ }
+ #endif
+ #ifdef MICROPY_HW_APA102_MOSI
+ if (pin == MICROPY_HW_APA102_MOSI) {
+ apa102_mosi_in_use = true;
+ }
+ if (pin == MICROPY_HW_APA102_SCK) {
+ apa102_sck_in_use = true;
+ }
+ #endif
+
+ #ifdef SPEAKER_ENABLE_PIN
+ if (pin == SPEAKER_ENABLE_PIN) {
+ speaker_enable_in_use = true;
+ }
+ #endif
}
+bool common_hal_mcu_pin_is_free(const mcu_pin_obj_t *pin) {
+ #ifdef MICROPY_HW_NEOPIXEL
+ if (pin == MICROPY_HW_NEOPIXEL) {
+ return !neopixel_in_use;
+ }
+ #endif
+ #ifdef MICROPY_HW_APA102_MOSI
+ if (pin == MICROPY_HW_APA102_MOSI) {
+ return !apa102_mosi_in_use;
+ }
+ if (pin == MICROPY_HW_APA102_SCK) {
+ return !apa102_sck_in_use;
+ }
+ #endif
+
+ #ifdef SPEAKER_ENABLE_PIN
+ if (pin == SPEAKER_ENABLE_PIN) {
+ return !speaker_enable_in_use;
+ }
+ #endif
+
+ // TODO check pin enable.
+ return true;
+}
diff --git a/ports/nrf/common-hal/microcontroller/Pin.h b/ports/nrf/common-hal/microcontroller/Pin.h
index 1694a8249..954d6e51d 100644
--- a/ports/nrf/common-hal/microcontroller/Pin.h
+++ b/ports/nrf/common-hal/microcontroller/Pin.h
@@ -30,7 +30,19 @@
#include "nrf_pin.h"
#include "py/mphal.h"
+#ifdef MICROPY_HW_NEOPIXEL
+extern bool neopixel_in_use;
+#endif
+#ifdef MICROPY_HW_APA102_MOSI
+extern bool apa102_sck_in_use;
+extern bool apa102_mosi_in_use;
+#endif
+
#define mcu_pin_obj_t pin_obj_t
void reset_all_pins(void);
+// reset_pin takes the pin number instead of the pointer so that objects don't
+// need to store a full pointer.
+void reset_pin(uint8_t pin);
+void claim_pin(const mcu_pin_obj_t* pin);
#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_MICROCONTROLLER_PIN_H