the following code
Code:
#if defined(__IMXRT1062__)
extern "C" uint32_t set_arm_clock(uint32_t frequency);
#endif
extern volatile uint32_t systick_cycle_count;
uint32_t m_micros(void)
{
uint32_t ccdelta, usec, smc, scc;
// do {
// __LDREXW(&systick_safe_read);
smc = systick_millis_count;
scc = systick_cycle_count;
// } while ( __STREXW(1, &systick_safe_read));
ccdelta = ARM_DWT_CYCCNT - scc;
usec = 1000*smc + (ccdelta/(F_CPU_ACTUAL/1000000));
return usec;
}
void setup() {
// put your setup code here, to run once:
#if defined(__IMXRT1062__)
set_arm_clock(24000000); // comment here to get full speed
#endif
}
void loop() {
// put your main code here, to run repeatedly:
static uint32_t t0;
if(millis()>(t0+1000))
{
t0=millis();
uint32_t t2=millis();
uint32_t t1=m_micros();
delay(50);
Serial.printf("%d %d\n", millis()-t2, m_micros()-t1);
}
}
gives me
Code:
49 50004
49 50003
1 1048
1 1048
49 50004
1 1048
49 50004
1 1048
49 50003
1 1048
49 50004
1 1047
49 50003
1 1048
1 1048
1 1045
49 50004
1 1048
49 50004
1 1048
1 1048
1 1048
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
49 50004
1 1048
1 1047
1 1048
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
49 50004
1 1048
49 50004
1 1048
1 1047
1 1047
1 1044
1 1047
1 1047
1 1048
1 1048
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
49 50004
1 1048
1 1048
1 1048
1 1047
1 1047
1 1047
1 1047
49 50004
1 1048
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
1 1047
that is, result is frequently correct
but uncommenting the ARM code
Code:
#if defined(__IMXRT1062__)
extern "C" uint32_t set_arm_clock(uint32_t frequency);
#endif
#include "arm_math.h" // micros() synchronization
extern volatile uint32_t systick_millis_count;
extern volatile uint32_t systick_cycle_count;
extern uint32_t systick_safe_read; // micros() synchronization
uint32_t m_micros(void)
{
uint32_t ccdelta, usec, smc, scc;
do {
__LDREXW(&systick_safe_read);
smc = systick_millis_count;
scc = systick_cycle_count;
} while ( __STREXW(1, &systick_safe_read));
ccdelta = ARM_DWT_CYCCNT - scc;
usec = 1000*smc + (ccdelta/(F_CPU_ACTUAL/1000000));
return usec;
}
void setup() {
// put your setup code here, to run once:
#if defined(__IMXRT1062__)
set_arm_clock(24000000); // comment here to get full speed
#endif
}
void loop() {
// put your main code here, to run repeatedly:
static uint32_t t0;
if(millis()>(t0+1000))
{
t0=millis();
uint32_t t2=millis();
uint32_t t1=m_micros();
delay(50);
Serial.printf("%d %d\n", millis()-t2, m_micros()-t1);
}
}
gives consistently
Code:
1 1045
1 1047
1 1045
1 1048
1 1048
1 1045
1 1048
1 1048
1 1048
1 1048
1 1048
1 1048
1 1048
1 1048
1 1048
1 1048
1 1048
1 1048
1 1048
1 1048
1 1048
1 1048
1 1048
1 1048
1 1048
1 1048
1 1048
1 1048
1 1048
1 1048
1 1048
1 1048
1 1048
1 1048
1 1048
1 1048
1 1048
1 1048
1 1048
1 1048
1 1048
1 1048
1 1048
1 1048
1 1048
1 1048