//uint32_t systick_current, systick_count, systick_istatus; // testing only
-uint32_t micros(void)
+static uint32_t micros_mask(void)
{
uint32_t count, current, istatus;
return count * 1000 + current / (F_CPU / 1000000);
}
+uint32_t micros(void)
+{
+ uint32_t count, current, istatus;
+
+ if (nvic_execution_priority() <= (SCB_SHPR3 >> 24))
+ return micros_mask();
+
+ do {
+ current = SYST_CVR;
+ count = systick_millis_count;
+ istatus = SCB_ICSR; // bit 26 indicates if systick exception pending
+ }
+ while (istatus & SCB_ICSR_PENDSTSET);
+
+ current = ((F_CPU / 1000) - 1) - current;
+ return count * 1000 + current / (F_CPU / 1000000);
+}
+
void delay(uint32_t ms)
{
uint32_t start = micros();