Micros() resolution on RAK4631 / RAK4630

I’m using a RAK4630 with Arduino BSP and I need a reference to elapsed time. I’m trying to use micros() but the resolution seems to be about 1ms. I created a loop that waits for micros to update and then prints the time, and here is the result: 976 1952 2929 3906 4882 5859 6835 7812. As you can see, the resolution is about 976uS. I need something at least an order of magnitude more precise, ideally down to about 4 or 8 uS resolution (which is typical on an AVR-based Arduino.

How can I get an equivalent to micros() that is actually usable?

As an alternative, what timer functionality is available without interfering with BLE, LORA, or other systems (USB, UART, I2C, etc.)?

I don’t specifically need the value to be represented in uS units, I would be fine with a different unit, as long as it’s precise to a similar level as described above. For example, if the system clock is 32MHz, I would be okay with a clock source that is divided by 64 (each unit would be 2uS) or 256 (125kHz = 8uS) or for this application even 1024 (31,250Hz = 32uS).

I need precise timing because I’m interfacing with hardware that requires it.

I look forward to any advice and suggestions offered.

I was able to get it working by adding dwt_enable(); before using micros()

It looks like micros() checks “if dwt is enabled” and then determines if it will give 1uS resolution or ~976uS resolution.

What is “dwt”?

What are the implications of “enabling” dwt? If nothing bad, then why is it not enabled by default?

Not an expert for the processor, what I found is

DWT ==> Data watchpoint and trace functionality of the ARM Cortex M4 processor.

So I guess it should be only enabled when the device is in debug mode but not as default.

micros() seems to return either the DWT cycle count (not sure what this is) or the TaskTickCount. The second one is from FreeRTOS which ticks (as far as I know) are 1ms. That would match with your finding.

If enabling dwt is not showing any bad behaviour for your application I guess it is ok to do it.