Skip to content

Commit

Permalink
Prevent recursive disconnet loops in NRF52Pin and accidental erasure …
Browse files Browse the repository at this point in the history
…of obj state

  - Utilise isDisconnecting() status to prevent recursive disconection of pins, often caused by reallocation of pins to
    a peripheral that already uses one or more of thos pins.

  - Prevent accidental erasure of the binding between a pin and its associated peripheral when the pin is peripheral-locked.
  • Loading branch information
finneyj committed Mar 7, 2023
1 parent 14bcffe commit 5fa1d16
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions source/NRF52Pin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,17 @@ void NRF52Pin::connect(PinPeripheral &p, bool deleteOnRelease)
*/
void NRF52Pin::disconnect()
{
// Avoid any potential recursive loops caused by pin swaps within a single peripheral
if (isDisconnecting())
return;

// Detach any on chip peripherals attached to this pin.
if (obj)
if (obj && !obj->isPinLocked())
{
if (!obj->isPinLocked())
obj->releasePin(*this);
// Indicate that this pin is in the process of being disconnected.
status |= IO_STATUS_DISCONNECTING;

obj->releasePin(*this);

// If we have previously allocated a PWM channel to this pin through setAnalogValue(), mark that PWM channel as free for future allocation.
if (obj == pwm)
Expand All @@ -202,13 +208,14 @@ void NRF52Pin::disconnect()
if (pwmChannelMap[i] == name)
pwmChannelMap[i] = -1;
}

obj = NULL;
}

// Disable any interrupts that may be attached to the pin GPIO state.
PORT->PIN_CNF[PIN] &= ~(GPIO_PIN_CNF_SENSE_Msk);

// Reset status flags to zero, but retain preferred TouchSense, Polarity and wake modes.
obj = NULL;
status &= IO_STATUS_MODES;
}

Expand Down

0 comments on commit 5fa1d16

Please sign in to comment.