From 0e8d14618477de053417fcf7b8c2e72df589cb69 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Tue, 21 Aug 2018 20:28:58 -0400 Subject: [PATCH] wip --- ports/nrf/common-hal/microcontroller/Pin.c | 69 ++++++++++++++++++++-- ports/nrf/common-hal/microcontroller/Pin.h | 12 ++++ 2 files changed, 75 insertions(+), 6 deletions(-) diff --git a/ports/nrf/common-hal/microcontroller/Pin.c b/ports/nrf/common-hal/microcontroller/Pin.c index 837e813ac0..1b7ac0d838 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" +void reset_all_pins(void) { + 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; } - -void reset_all_pins(void) { - for (uint32_t pin = 0; pin < NUMBER_OF_PINS; ++pin) - nrf_gpio_cfg_default(pin); -} - diff --git a/ports/nrf/common-hal/microcontroller/Pin.h b/ports/nrf/common-hal/microcontroller/Pin.h index 1694a82493..954d6e51df 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