From 3b49fb0271dcbd2c6f865a085bce182101aa4281 Mon Sep 17 00:00:00 2001 From: skarg Date: Fri, 22 Oct 2010 00:11:25 +0000 Subject: [PATCH] Change global interrupt disable to timer specific interrupt disable for data protection. Added interrupt disabled around ADC API. --- bacnet-stack/ports/bdk-atxx4-mstp/Makefile | 5 +++ bacnet-stack/ports/bdk-atxx4-mstp/adc.c | 6 +++ bacnet-stack/ports/bdk-atxx4-mstp/timer2.c | 46 +++++++++------------- 3 files changed, 30 insertions(+), 27 deletions(-) diff --git a/bacnet-stack/ports/bdk-atxx4-mstp/Makefile b/bacnet-stack/ports/bdk-atxx4-mstp/Makefile index 7bea01c0..1df30eb8 100644 --- a/bacnet-stack/ports/bdk-atxx4-mstp/Makefile +++ b/bacnet-stack/ports/bdk-atxx4-mstp/Makefile @@ -144,6 +144,11 @@ COMMON = -mmcu=$(MCU) # define something from the Makefile or batch file DEFINES = +# various SEEPROM sizes +ifeq (${SEEPROM},128) +DEFINES += -DSEEPROM_PAGE_SIZE=64 +DEFINES += -DSEEPROM_WORD_ADDRESS_16BIT=1 +endif OPTIMIZE_FLAGS = -mcall-prologues OPTIMIZE_FLAGS += -finline-functions-called-once diff --git a/bacnet-stack/ports/bdk-atxx4-mstp/adc.c b/bacnet-stack/ports/bdk-atxx4-mstp/adc.c index 1c4b9f3e..d8ff7e32 100644 --- a/bacnet-stack/ports/bdk-atxx4-mstp/adc.c +++ b/bacnet-stack/ports/bdk-atxx4-mstp/adc.c @@ -86,7 +86,9 @@ void adc_enable( { if (Enabled_Channels) { /* ADC interupt is already started */ + BIT_CLEAR(ADCSRA, ADIE); BIT_SET(Enabled_Channels, index); + BIT_SET(ADCSRA, ADIE); } else { if (index < ADC_CHANNELS_MAX) { /* not running yet */ @@ -108,7 +110,9 @@ uint8_t adc_result_8bit( if (index < ADC_CHANNELS_MAX) { adc_enable(index); + BIT_CLEAR(ADCSRA, ADIE); result = (uint8_t)(Sample_Result[index]>>2); + BIT_SET(ADCSRA, ADIE); } return result; @@ -121,7 +125,9 @@ uint16_t adc_result_10bit( if (index < ADC_CHANNELS_MAX) { adc_enable(index); + BIT_CLEAR(ADCSRA, ADIE); result = Sample_Result[index]; + BIT_SET(ADCSRA, ADIE); } return result; diff --git a/bacnet-stack/ports/bdk-atxx4-mstp/timer2.c b/bacnet-stack/ports/bdk-atxx4-mstp/timer2.c index c6dca7d1..13b2e613 100644 --- a/bacnet-stack/ports/bdk-atxx4-mstp/timer2.c +++ b/bacnet-stack/ports/bdk-atxx4-mstp/timer2.c @@ -30,7 +30,8 @@ #error "F_CPU must be defined for Timer configuration." #endif /* Timer2 Prescaling: 1, 8, 32, 64, 128, 256, or 1024 */ -#define TIMER_TICKS(p) (F_CPU/(p)/1000) +#define TIMER_MICROSECONDS 1000 +#define TIMER_TICKS(p) ((((F_CPU)/(p)/1000)*(TIMER_MICROSECONDS))/1000) #define TIMER_TICKS_MAX 0xff /* adjust the prescaler for the processor clock */ #if (TIMER_TICKS(1) <= TIMER_TICKS_MAX) @@ -60,11 +61,11 @@ static volatile uint32_t Millisecond_Counter; ISR(TIMER2_OVF_vect); /************************************************************************* -* Description: Timer Interrupt Service Routine - Timer Overflowed! -* Returns: none -* Notes: Global interupts must be enabled +* Description: Timer Interrupt Handler +* Returns: nothing +* Notes: none *************************************************************************/ -ISR(TIMER2_OVF_vect) +static inline void timer_interrupt_handler(void) { /* Set the counter for the next interrupt */ TCNT2 = TIMER2_COUNT; @@ -72,40 +73,31 @@ ISR(TIMER2_OVF_vect) } /************************************************************************* -* Description: sets the current time count with a value +* Description: Timer Interrupt Service Routine - Timer Overflowed! * Returns: none -* Notes: none +* Notes: Global interupts must be enabled *************************************************************************/ -uint32_t timer_milliseconds_set( - uint32_t value) +ISR(TIMER2_OVF_vect) { - uint8_t sreg = 0; /* holds interrupts pending */ - uint32_t old_value = 0; /* return value */ - - sreg = SREG; - __disable_interrupt(); - old_value = Millisecond_Counter; - Millisecond_Counter = value; - SREG = sreg; - - return old_value; + timer_interrupt_handler(); } /************************************************************************* * Description: returns the current millisecond count * Returns: none -* Notes: none +* Notes: This method only disables the timer overflow interrupt. *************************************************************************/ uint32_t timer_milliseconds( void) { - uint32_t timer_value = 0; /* return value */ - uint8_t sreg = 0; /* holds interrupts pending */ + uint32_t timer_value; /* return value */ - sreg = SREG; - __disable_interrupt(); + /* Disable the overflow interrupt. + Prevents value corruption that would happen if interrupted */ + BIT_CLEAR(TIMSK2, TOIE2); timer_value = Millisecond_Counter; - SREG = sreg; + /* Enable the overflow interrupt */ + BIT_SET(TIMSK2, TOIE2); return timer_value; } @@ -149,8 +141,8 @@ void timer_init( #else #error Timer2 Prescale: Invalid Value #endif - /* Clear any TOV Flag set when the timer overflowed */ - BIT_CLEAR(TIFR2, TOV2); + /* Clear any TOV Flag set when the timer overflowed - by setting! */ + BIT_SET(TIFR2, TOV2); /* Initial value */ TCNT2 = TIMER2_COUNT; /* Enable the overflow interrupt */