2017-12-21 07:49:14 -05:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016 Scott Shawcroft for Adafruit Industries
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2018-08-30 21:42:25 -04:00
|
|
|
#include "shared-bindings/microcontroller/Pin.h"
|
|
|
|
|
2018-07-08 11:48:32 -04:00
|
|
|
#include "nrf_gpio.h"
|
2017-12-21 07:49:14 -05:00
|
|
|
#include "py/mphal.h"
|
|
|
|
|
2018-08-30 21:42:25 -04:00
|
|
|
#include "nrf/pins.h"
|
|
|
|
#include "supervisor/shared/rgb_led_status.h"
|
|
|
|
|
|
|
|
#ifdef MICROPY_HW_NEOPIXEL
|
|
|
|
bool neopixel_in_use;
|
|
|
|
#endif
|
|
|
|
#ifdef MICROPY_HW_APA102_MOSI
|
|
|
|
bool apa102_sck_in_use;
|
|
|
|
bool apa102_mosi_in_use;
|
|
|
|
#endif
|
|
|
|
#ifdef SPEAKER_ENABLE_PIN
|
|
|
|
bool speaker_enable_in_use;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Bit mask of claimed pins on each of up to two ports. nrf52832 has one port; nrf52840 has two.
|
2018-09-04 16:43:21 -04:00
|
|
|
STATIC uint32_t claimed_pins[GPIO_COUNT];
|
2018-10-19 21:46:22 -04:00
|
|
|
STATIC uint32_t never_reset_pins[GPIO_COUNT];
|
2018-08-30 21:42:25 -04:00
|
|
|
|
2017-12-21 07:49:14 -05:00
|
|
|
void reset_all_pins(void) {
|
2018-09-04 16:43:21 -04:00
|
|
|
for (size_t i = 0; i < GPIO_COUNT; i++) {
|
2018-10-19 21:46:22 -04:00
|
|
|
claimed_pins[i] = never_reset_pins[i];
|
2018-09-04 16:43:21 -04:00
|
|
|
}
|
2018-08-30 21:42:25 -04:00
|
|
|
|
2018-08-21 20:28:58 -04:00
|
|
|
for (uint32_t pin = 0; pin < NUMBER_OF_PINS; ++pin) {
|
2018-10-19 21:46:22 -04:00
|
|
|
if (!(never_reset_pins[nrf_pin_port(pin)] & (1 << nrf_relative_pin_number(pin)))) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-07-08 11:48:32 -04:00
|
|
|
nrf_gpio_cfg_default(pin);
|
2018-08-21 20:28:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#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
|
|
|
|
}
|
|
|
|
|
2018-08-30 21:42:25 -04:00
|
|
|
// Mark pin as free and return it to a quiescent state.
|
2018-08-31 17:46:03 -04:00
|
|
|
void reset_pin_number(uint8_t pin_number) {
|
|
|
|
if (pin_number == NO_PIN) {
|
2018-08-30 21:42:25 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear claimed bit.
|
2018-08-31 17:46:03 -04:00
|
|
|
claimed_pins[nrf_pin_port(pin_number)] &= ~(1 << nrf_relative_pin_number(pin_number));
|
2018-08-30 21:42:25 -04:00
|
|
|
|
|
|
|
#ifdef MICROPY_HW_NEOPIXEL
|
2018-08-31 17:46:03 -04:00
|
|
|
if (pin_number == MICROPY_HW_NEOPIXEL->number) {
|
2018-08-30 21:42:25 -04:00
|
|
|
neopixel_in_use = false;
|
|
|
|
rgb_led_status_init();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef MICROPY_HW_APA102_MOSI
|
|
|
|
if (pin == MICROPY_HW_APA102_MOSI->number ||
|
|
|
|
pin == MICROPY_HW_APA102_SCK->number) {
|
2018-08-31 17:46:03 -04:00
|
|
|
apa102_mosi_in_use = apa102_mosi_in_use && pin_number != MICROPY_HW_APA102_MOSI->number;
|
|
|
|
apa102_sck_in_use = apa102_sck_in_use && pin_number != MICROPY_HW_APA102_SCK->number;
|
2018-08-30 21:42:25 -04:00
|
|
|
if (!apa102_sck_in_use && !apa102_mosi_in_use) {
|
|
|
|
rgb_led_status_init();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef SPEAKER_ENABLE_PIN
|
2018-08-31 17:46:03 -04:00
|
|
|
if (pin_number == SPEAKER_ENABLE_PIN->number) {
|
2018-08-30 21:42:25 -04:00
|
|
|
speaker_enable_in_use = false;
|
|
|
|
common_hal_digitalio_digitalinout_switch_to_output(
|
2018-08-31 17:46:03 -04:00
|
|
|
nrf_gpio_pin_dir_set(pin_number, NRF_GPIO_PIN_DIR_OUTPUT);
|
|
|
|
nrf_gpio_pin_write(pin_number, false);
|
2018-08-30 21:42:25 -04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-10-19 21:46:22 -04:00
|
|
|
|
|
|
|
void never_reset_pin_number(uint8_t pin_number) {
|
|
|
|
never_reset_pins[nrf_pin_port(pin_number)] |= 1 << nrf_relative_pin_number(pin_number);
|
|
|
|
}
|
|
|
|
|
2018-08-21 20:28:58 -04:00
|
|
|
void claim_pin(const mcu_pin_obj_t* pin) {
|
2018-08-30 21:42:25 -04:00
|
|
|
// Set bit in claimed_pins bitmask.
|
|
|
|
claimed_pins[nrf_pin_port(pin->number)] |= 1 << nrf_relative_pin_number(pin->number);
|
|
|
|
|
2018-08-21 20:28:58 -04:00
|
|
|
#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
|
2017-12-21 07:49:14 -05:00
|
|
|
}
|
|
|
|
|
2018-08-21 20:28:58 -04:00
|
|
|
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
|
|
|
|
|
2018-08-31 17:46:03 -04:00
|
|
|
return !(claimed_pins[nrf_pin_port(pin->number)] & (1 << nrf_relative_pin_number(pin->number)));
|
2018-08-21 20:28:58 -04:00
|
|
|
}
|