|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- #include "core_pins.h"
- #include "arm_math.h"
-
-
-
- volatile uint32_t systick_millis_count = 0;
- volatile uint32_t systick_cycle_count = 0;
- volatile uint32_t scale_cpu_cycles_to_microseconds = 0;
- uint32_t systick_safe_read;
-
-
-
-
- #define SYSTICK_EXT_FREQ 100000
-
- #if 0
-
- void systick_isr(void)
- {
- systick_millis_count++;
-
-
-
-
- }
- #endif
-
- #if 0
- void millis_init(void)
- {
-
- _VectorsRam[15] = systick_isr;
- #ifdef SYSTICK_EXT_FREQ
- SYST_RVR = (SYSTICK_EXT_FREQ / 1000) - 1;
- SYST_CVR = 0;
- SYST_CSR = SYST_CSR_TICKINT | SYST_CSR_ENABLE;
- #else
- SYST_RVR = (F_CPU / 1000) - 1;
- SYST_CVR = 0;
- SYST_CSR = SYST_CSR_CLKSOURCE | SYST_CSR_TICKINT | SYST_CSR_ENABLE;
- #endif
-
-
- }
- #endif
-
-
-
- void delay(uint32_t msec)
- {
- uint32_t start;
-
- if (msec == 0) return;
- start = micros();
- while (1) {
- while ((micros() - start) >= 1000) {
- if (--msec == 0) return;
- start += 1000;
- }
- yield();
- }
-
- }
-
- uint32_t micros(void)
- {
- uint32_t smc, scc;
- do {
- __LDREXW(&systick_safe_read);
- smc = systick_millis_count;
- scc = systick_cycle_count;
- } while ( __STREXW(1, &systick_safe_read));
- uint32_t cyccnt = ARM_DWT_CYCCNT;
- asm volatile("" : : : "memory");
- uint32_t ccdelta = cyccnt - scc;
- uint32_t frac = ((uint64_t)ccdelta * scale_cpu_cycles_to_microseconds) >> 32;
- if (frac > 1000) frac = 1000;
- uint32_t usec = 1000*smc + frac;
- return usec;
- }
-
- #if 0
- uint32_t micros(void)
- {
- uint32_t msec, tick, elapsed, istatus, usec;
-
-
-
-
- static uint32_t prev_usec=0;
- static int doprint=180;
-
- __disable_irq();
- tick = SYST_CVR;
- msec = systick_millis_count;
- istatus = SCB_ICSR;
- #ifndef SYSTICK_EXT_FREQ
- const uint32_t fcpu = F_CPU;
- #endif
- __enable_irq();
- istatus &= SCB_ICSR_PENDSTSET;
- #ifdef SYSTICK_EXT_FREQ
- if ((istatus & SCB_ICSR_PENDSTSET) && (tick == 0 || tick > (SYSTICK_EXT_FREQ / 2000))) {
- #else
- if ((istatus & SCB_ICSR_PENDSTSET) && (tick == 0 || tick > (fcpu / 2000))) {
- #endif
-
-
- msec++;
- }
- #if defined(SYSTICK_EXT_FREQ) && SYSTICK_EXT_FREQ <= 1000000
- elapsed = (SYSTICK_EXT_FREQ / 1000) - tick;
- if (tick == 0) elapsed = 0;
- usec = msec * 1000 + elapsed * (1000000 / SYSTICK_EXT_FREQ);
- #elif defined(SYSTICK_EXT_FREQ) && SYSTICK_EXT_FREQ > 1000000
- elapsed = (SYSTICK_EXT_FREQ / 1000) - tick;
- if (tick == 0) elapsed = 0;
- usec = msec * 1000 + elapsed / (SYSTICK_EXT_FREQ / 1000000);
- #else
- elapsed = (fcpu / 1000) - tick;
- if (tick == 0) elapsed = 0;
- usec = msec * 1000 + elapsed / (fcpu / 1000000);
- #endif
-
- if (usec < prev_usec && doprint) {
-
-
-
-
-
- if (doprint > 0) doprint--;
- }
-
-
-
-
- prev_usec = usec;
- return usec;
- }
- #endif
|