Change global interrupt disable to timer specific interrupt disable for data protection. Added interrupt disabled around ADC API.
This commit is contained in:
@@ -144,6 +144,11 @@ COMMON = -mmcu=$(MCU)
|
|||||||
|
|
||||||
# define something from the Makefile or batch file
|
# define something from the Makefile or batch file
|
||||||
DEFINES =
|
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 = -mcall-prologues
|
||||||
OPTIMIZE_FLAGS += -finline-functions-called-once
|
OPTIMIZE_FLAGS += -finline-functions-called-once
|
||||||
|
|||||||
@@ -86,7 +86,9 @@ void adc_enable(
|
|||||||
{
|
{
|
||||||
if (Enabled_Channels) {
|
if (Enabled_Channels) {
|
||||||
/* ADC interupt is already started */
|
/* ADC interupt is already started */
|
||||||
|
BIT_CLEAR(ADCSRA, ADIE);
|
||||||
BIT_SET(Enabled_Channels, index);
|
BIT_SET(Enabled_Channels, index);
|
||||||
|
BIT_SET(ADCSRA, ADIE);
|
||||||
} else {
|
} else {
|
||||||
if (index < ADC_CHANNELS_MAX) {
|
if (index < ADC_CHANNELS_MAX) {
|
||||||
/* not running yet */
|
/* not running yet */
|
||||||
@@ -108,7 +110,9 @@ uint8_t adc_result_8bit(
|
|||||||
|
|
||||||
if (index < ADC_CHANNELS_MAX) {
|
if (index < ADC_CHANNELS_MAX) {
|
||||||
adc_enable(index);
|
adc_enable(index);
|
||||||
|
BIT_CLEAR(ADCSRA, ADIE);
|
||||||
result = (uint8_t)(Sample_Result[index]>>2);
|
result = (uint8_t)(Sample_Result[index]>>2);
|
||||||
|
BIT_SET(ADCSRA, ADIE);
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
@@ -121,7 +125,9 @@ uint16_t adc_result_10bit(
|
|||||||
|
|
||||||
if (index < ADC_CHANNELS_MAX) {
|
if (index < ADC_CHANNELS_MAX) {
|
||||||
adc_enable(index);
|
adc_enable(index);
|
||||||
|
BIT_CLEAR(ADCSRA, ADIE);
|
||||||
result = Sample_Result[index];
|
result = Sample_Result[index];
|
||||||
|
BIT_SET(ADCSRA, ADIE);
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
@@ -30,7 +30,8 @@
|
|||||||
#error "F_CPU must be defined for Timer configuration."
|
#error "F_CPU must be defined for Timer configuration."
|
||||||
#endif
|
#endif
|
||||||
/* Timer2 Prescaling: 1, 8, 32, 64, 128, 256, or 1024 */
|
/* 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
|
#define TIMER_TICKS_MAX 0xff
|
||||||
/* adjust the prescaler for the processor clock */
|
/* adjust the prescaler for the processor clock */
|
||||||
#if (TIMER_TICKS(1) <= TIMER_TICKS_MAX)
|
#if (TIMER_TICKS(1) <= TIMER_TICKS_MAX)
|
||||||
@@ -60,11 +61,11 @@ static volatile uint32_t Millisecond_Counter;
|
|||||||
ISR(TIMER2_OVF_vect);
|
ISR(TIMER2_OVF_vect);
|
||||||
|
|
||||||
/*************************************************************************
|
/*************************************************************************
|
||||||
* Description: Timer Interrupt Service Routine - Timer Overflowed!
|
* Description: Timer Interrupt Handler
|
||||||
* Returns: none
|
* Returns: nothing
|
||||||
* Notes: Global interupts must be enabled
|
* Notes: none
|
||||||
*************************************************************************/
|
*************************************************************************/
|
||||||
ISR(TIMER2_OVF_vect)
|
static inline void timer_interrupt_handler(void)
|
||||||
{
|
{
|
||||||
/* Set the counter for the next interrupt */
|
/* Set the counter for the next interrupt */
|
||||||
TCNT2 = TIMER2_COUNT;
|
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
|
* Returns: none
|
||||||
* Notes: none
|
* Notes: Global interupts must be enabled
|
||||||
*************************************************************************/
|
*************************************************************************/
|
||||||
uint32_t timer_milliseconds_set(
|
ISR(TIMER2_OVF_vect)
|
||||||
uint32_t value)
|
|
||||||
{
|
{
|
||||||
uint8_t sreg = 0; /* holds interrupts pending */
|
timer_interrupt_handler();
|
||||||
uint32_t old_value = 0; /* return value */
|
|
||||||
|
|
||||||
sreg = SREG;
|
|
||||||
__disable_interrupt();
|
|
||||||
old_value = Millisecond_Counter;
|
|
||||||
Millisecond_Counter = value;
|
|
||||||
SREG = sreg;
|
|
||||||
|
|
||||||
return old_value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*************************************************************************
|
/*************************************************************************
|
||||||
* Description: returns the current millisecond count
|
* Description: returns the current millisecond count
|
||||||
* Returns: none
|
* Returns: none
|
||||||
* Notes: none
|
* Notes: This method only disables the timer overflow interrupt.
|
||||||
*************************************************************************/
|
*************************************************************************/
|
||||||
uint32_t timer_milliseconds(
|
uint32_t timer_milliseconds(
|
||||||
void)
|
void)
|
||||||
{
|
{
|
||||||
uint32_t timer_value = 0; /* return value */
|
uint32_t timer_value; /* return value */
|
||||||
uint8_t sreg = 0; /* holds interrupts pending */
|
|
||||||
|
|
||||||
sreg = SREG;
|
/* Disable the overflow interrupt.
|
||||||
__disable_interrupt();
|
Prevents value corruption that would happen if interrupted */
|
||||||
|
BIT_CLEAR(TIMSK2, TOIE2);
|
||||||
timer_value = Millisecond_Counter;
|
timer_value = Millisecond_Counter;
|
||||||
SREG = sreg;
|
/* Enable the overflow interrupt */
|
||||||
|
BIT_SET(TIMSK2, TOIE2);
|
||||||
|
|
||||||
return timer_value;
|
return timer_value;
|
||||||
}
|
}
|
||||||
@@ -149,8 +141,8 @@ void timer_init(
|
|||||||
#else
|
#else
|
||||||
#error Timer2 Prescale: Invalid Value
|
#error Timer2 Prescale: Invalid Value
|
||||||
#endif
|
#endif
|
||||||
/* Clear any TOV Flag set when the timer overflowed */
|
/* Clear any TOV Flag set when the timer overflowed - by setting! */
|
||||||
BIT_CLEAR(TIFR2, TOV2);
|
BIT_SET(TIFR2, TOV2);
|
||||||
/* Initial value */
|
/* Initial value */
|
||||||
TCNT2 = TIMER2_COUNT;
|
TCNT2 = TIMER2_COUNT;
|
||||||
/* Enable the overflow interrupt */
|
/* Enable the overflow interrupt */
|
||||||
|
|||||||
Reference in New Issue
Block a user