|
@@ -14,76 +14,28 @@
|
|
* along with this program. If not, see <http:
|
|
* along with this program. If not, see <http:
|
|
*/
|
|
*/
|
|
|
|
|
|
-#ifndef __OPTIMIZE__
|
|
+#include <ch.h>
|
|
-# pragma message "Compiler optimizations disabled; wait_cpuclock() won't work as designed"
|
|
+#include <hal.h>
|
|
-#endif
|
|
|
|
|
|
|
|
-#define CLOCK_DELAY_NOP8 "nop\n\t nop\n\t nop\n\t nop\n\t nop\n\t nop\n\t nop\n\t nop\n\t"
|
|
+#include "_wait.h"
|
|
|
|
|
|
-__attribute__((always_inline)) static inline void wait_cpuclock(unsigned int n) {
|
|
+#ifdef WAIT_US_TIMER
|
|
-
|
|
+void wait_us(uint16_t duration) {
|
|
- * That way, compiler optimization will remove unnecessary code. */
|
|
+ static const GPTConfig gpt_cfg = {1000000, NULL, 0, 0};
|
|
- if (n < 1) {
|
|
+
|
|
- return;
|
|
+ if (duration == 0) {
|
|
- }
|
|
+ duration = 1;
|
|
- if (n > 8) {
|
|
|
|
- unsigned int n8 = n / 8;
|
|
|
|
- n = n - n8 * 8;
|
|
|
|
- switch (n8) {
|
|
|
|
- case 16:
|
|
|
|
- asm volatile(CLOCK_DELAY_NOP8::: "memory");
|
|
|
|
- case 15:
|
|
|
|
- asm volatile(CLOCK_DELAY_NOP8::: "memory");
|
|
|
|
- case 14:
|
|
|
|
- asm volatile(CLOCK_DELAY_NOP8::: "memory");
|
|
|
|
- case 13:
|
|
|
|
- asm volatile(CLOCK_DELAY_NOP8::: "memory");
|
|
|
|
- case 12:
|
|
|
|
- asm volatile(CLOCK_DELAY_NOP8::: "memory");
|
|
|
|
- case 11:
|
|
|
|
- asm volatile(CLOCK_DELAY_NOP8::: "memory");
|
|
|
|
- case 10:
|
|
|
|
- asm volatile(CLOCK_DELAY_NOP8::: "memory");
|
|
|
|
- case 9:
|
|
|
|
- asm volatile(CLOCK_DELAY_NOP8::: "memory");
|
|
|
|
- case 8:
|
|
|
|
- asm volatile(CLOCK_DELAY_NOP8::: "memory");
|
|
|
|
- case 7:
|
|
|
|
- asm volatile(CLOCK_DELAY_NOP8::: "memory");
|
|
|
|
- case 6:
|
|
|
|
- asm volatile(CLOCK_DELAY_NOP8::: "memory");
|
|
|
|
- case 5:
|
|
|
|
- asm volatile(CLOCK_DELAY_NOP8::: "memory");
|
|
|
|
- case 4:
|
|
|
|
- asm volatile(CLOCK_DELAY_NOP8::: "memory");
|
|
|
|
- case 3:
|
|
|
|
- asm volatile(CLOCK_DELAY_NOP8::: "memory");
|
|
|
|
- case 2:
|
|
|
|
- asm volatile(CLOCK_DELAY_NOP8::: "memory");
|
|
|
|
- case 1:
|
|
|
|
- asm volatile(CLOCK_DELAY_NOP8::: "memory");
|
|
|
|
- case 0:
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
}
|
|
}
|
|
- switch (n) {
|
|
+
|
|
- case 8:
|
|
+
|
|
- asm volatile("nop" ::: "memory");
|
|
+ * Only use this timer on the main thread;
|
|
- case 7:
|
|
+ * other threads need to use their own timer.
|
|
- asm volatile("nop" ::: "memory");
|
|
+ */
|
|
- case 6:
|
|
+ if (chThdGetSelfX() == &ch.mainthread && duration < (1ULL << (sizeof(gptcnt_t) * 8))) {
|
|
- asm volatile("nop" ::: "memory");
|
|
+ gptStart(&WAIT_US_TIMER, &gpt_cfg);
|
|
- case 5:
|
|
+ gptPolledDelay(&WAIT_US_TIMER, duration);
|
|
- asm volatile("nop" ::: "memory");
|
|
+ } else {
|
|
- case 4:
|
|
+ chThdSleepMicroseconds(duration);
|
|
- asm volatile("nop" ::: "memory");
|
|
|
|
- case 3:
|
|
|
|
- asm volatile("nop" ::: "memory");
|
|
|
|
- case 2:
|
|
|
|
- asm volatile("nop" ::: "memory");
|
|
|
|
- case 1:
|
|
|
|
- asm volatile("nop" ::: "memory");
|
|
|
|
- case 0:
|
|
|
|
- break;
|
|
|
|
}
|
|
}
|
|
-}
|
|
+}
|
|
|
|
+#endif
|