Silly Question re: a higher-reolution mills() function

Status
Not open for further replies.

Constantin

Well-known member
Silly Question re: a higher-resolution micros() function

Hi guys,

You may or may not have followed my travails in the TXCO hilarity thread and I'm wondering whether a higher-resolution version of micros() may help resolve remaining errors in the compensation function. My computer hunted down the following in the Teensy core that presumably figures out what micros() is.

Naturally, I managed to first mistakenly look into the original Teensy core, which explains the assembly language I found. Looking in the correct directory (i.e. teensy3), the computer found the following

In teensy3/core_pins.h:
Code:
uint32_t micros(void);

and in teensy3/pins_teensy.c:
Code:
uint32_t micros(void)
{
	uint32_t count, current, istatus;

	__disable_irq();
	current = SYST_CVR;
	count = systick_millis_count;
	istatus = SCB_ICSR;	// bit 26 indicates if systick exception pending
	__enable_irq();
	 //systick_current = current;
	 //systick_count = count;
	 //systick_istatus = istatus & SCB_ICSR_PENDSTSET ? 1 : 0;
	if ((istatus & SCB_ICSR_PENDSTSET) && current > 50) count++;
	current = ((F_CPU / 1000) - 1) - current;
	return count * 1000 + current / (F_CPU / 1000000);
}

where in Teensy3/kinetis.h:
Code:
// System Control Space (SCS), ARMv7 ref manual, B3.2, page 708
#define SCB_ICSR_PENDSTSET		((uint32_t)0x04000000)
#define SCB_ICSR		(*(volatile uint32_t *)0xE000ED04) // Interrupt Control and State
#define SYST_CVR		(*(volatile uint32_t *)0xE000E018) // SysTick Current Value

I wonder if anyone here has the experience to suggest a way to achieve a function that can measure time down to 1/10th of a micro-second. If not, no worries. Time to curl up with the reference manual... Have a great and happy new year everyone!
 
Last edited:
Haven't actually had a teensy in my hands yet, but is micros() not ported over from Arduino to the teensy core?
 
Thank you for the fast reply!

I had a brain cramp and looked for millis() instead of micros(); But I meant micros, will adapt post up there accordingly. :D

I will try to go through each variable using the hardware handbook to try and understand what is going on.
 
Last edited:
1/10th of a microsecond can be observed with the micros() function, but without great precision.
 
Start by trying stuff with only this line:

Code:
	return count * 1000 + current / (F_CPU / 1000000);

Perhaps change it to something like this:

Code:
	return count * (1000 * (F_CPU / 1000000)) + current;

That will give you elapsed time in CPU cycles, instead of microseconds.
 
Status
Not open for further replies.
Back
Top