From c1822a1f65facc85349c0ef34f9cc54ff036f600 Mon Sep 17 00:00:00 2001 From: jacobseptember Date: Fri, 4 Jan 2019 11:09:21 -0500 Subject: Changes to interrupt blocking, etc for PR change --- Projects/AVRISP-MKII/Lib/ISP/ISPProtocol.c | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/Projects/AVRISP-MKII/Lib/ISP/ISPProtocol.c b/Projects/AVRISP-MKII/Lib/ISP/ISPProtocol.c index 4dc37a4c1..ffe2c56db 100644 --- a/Projects/AVRISP-MKII/Lib/ISP/ISPProtocol.c +++ b/Projects/AVRISP-MKII/Lib/ISP/ISPProtocol.c @@ -36,6 +36,7 @@ */ #include "ISPProtocol.h" +#include #if defined(ENABLE_ISP_PROTOCOL) || defined(__DOXYGEN__) @@ -395,14 +396,14 @@ volatile uint16_t HalfCyclesRemaining; volatile uint8_t ResponseTogglesRemaining; /** ISR to toggle MOSI pin when TIMER1 overflows */ -ISR(TIMER1_OVF_vect) +ISR(TIMER1_OVF_vect, ISR_BLOCK) { PINB |= (1 << PB2); // toggle PB2 (MOSI) by writing 1 to its bit in PINB HalfCyclesRemaining--; } /** ISR to listen for toggles on MISO pin */ -ISR(PCINT0_vect) +ISR(PCINT0_vect, ISR_BLOCK) { ResponseTogglesRemaining--; } @@ -444,22 +445,23 @@ void ISPProtocol_Calibrate(void) ResponseTogglesRemaining = SUCCESS_TOGGLE_NUM; /* Turn on interrupts */ - uint8_t OldSREG = SREG; // save current global interrupt state PCICR |= (1 << PCIE0); // enable interrupts for PCINT7:0 (don't touch setting for PCINT12:8) TIMSK1 = (1 << TOIE1); // enable T1 OVF interrupt (and no other T1 interrupts) - sei(); // enable global interrupts - - /* Let device do its calibration, wait for reponse on MISO */ - while ( HalfCyclesRemaining && ResponseTogglesRemaining ) + + /* Turn on global interrupts for the following block, restoring current state at end */ + NONATOMIC_BLOCK(NONATOMIC_RESTORESTATE) { - // do nothing... + /* Let device do its calibration, wait for reponse on MISO */ + while ( HalfCyclesRemaining && ResponseTogglesRemaining ) + { + // do nothing... + } + + /* Disable interrupts */ + PCICR &= ~(1 << PCIE0); + TIMSK1 = 0; } - /* Disable interrupts, restore SREG */ - PCICR &= ~(1 << PCIE0); - TIMSK1 = 0; - SREG = OldSREG; - /* Check if device responded with a success message or if we timed out */ if (ResponseTogglesRemaining) { -- cgit v1.2.3