Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 07d1abf

Browse files
committedJan 19, 2015
Merge pull request #2556 from cmaglie/ide-1.5.x-pulsein-regression
Temporary fix for pulseIn() regression
2 parents 6d7751c + 8ddc519 commit 07d1abf

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed
 

‎hardware/arduino/avr/cores/arduino/wiring_pulse.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,25 @@ unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout)
6161
width++;
6262
}
6363

64-
// convert the reading to microseconds. The loop has been determined
65-
// to be 20 clock cycles long and have about 16 clocks between the edge
66-
// and the start of the loop. There will be some error introduced by
64+
// convert the reading to microseconds. There will be some error introduced by
6765
// the interrupt handlers.
68-
return clockCyclesToMicroseconds(width * 21 + 16);
66+
67+
// Conversion constants are compiler-dependent, different compiler versions
68+
// have different levels of optimization.
69+
#if __GNUC__==4 && __GNUC_MINOR__==3 && __GNUC_PATCHLEVEL__==2
70+
// avr-gcc 4.3.2
71+
return clockCyclesToMicroseconds(width * 21 + 16);
72+
#elif __GNUC__==4 && __GNUC_MINOR__==8 && __GNUC_PATCHLEVEL__==1
73+
// avr-gcc 4.8.1
74+
return clockCyclesToMicroseconds(width * 24 + 16);
75+
#elif __GNUC__<=4 && __GNUC_MINOR__<=3
76+
// avr-gcc <=4.3.x
77+
#warning "pulseIn() results may not be accurate"
78+
return clockCyclesToMicroseconds(width * 21 + 16);
79+
#else
80+
// avr-gcc >4.3.x
81+
#warning "pulseIn() results may not be accurate"
82+
return clockCyclesToMicroseconds(width * 24 + 16);
83+
#endif
84+
6985
}

0 commit comments

Comments
 (0)
Please sign in to comment.