1. Bitcraze
  2. Untitled project
  3. Crazyflie firmware
  4. Pull requests

Pull requests

#13 Merged at 9a67fa8

Implement usecTimestamp. Initialize TIM1 at startup for that.

  1. krasin

Hi Tobias, this is the third attempt to make a pull request. Sorry for spam. :)

This CL adds hal/interface/user_time.h with uint64_t usecTimestamp(void) implemented.

It uses TIM1, which is now initialized at startup with 1 microsecond interval. hal/src/freeRTOSdebug.c now uses usecTimestamp, instead of a raw access to the counters.

Also, fixed a race condition, where an TIM1 interrupt could happen in the middle of evaluating the expression in freeRTOSdebug which would result in a wrong timestamp. Use atomic load and write helpers provided by CMSIS library.

This CL is a prerequisite for fixing issue #14.

Please, don't hesitate to point to a code style violations. I would be happy to fix them, if any.

Comments (9)

  1. ledvinap

    Is there any advantage to use timer for this? There is already systick timer running and counted in freertos, so it should be quite easy to compose usec value as


    Only proper handling of timer overflow and scaling to stay in integer land will be necessary.

    There could be race condition in your code if usecTimestamp() is called with interrupts disabled (when TIM1_UP_IRQHandler can't run) - the value may overflow, but interrupt routine is not called. NVIC_GetPendingIRQ in critical section with overflow handler disabled is probably way to solve this; reading counter twice won't be necessary)

  2. krasin author


    thanks for pointing out to the issue with the disabled interrupts. It's real and I don't yet know how handle it.

    As for your formula for calculating time, I didn't get it. Could you please explain, what's uxMissedTicks?

  3. ledvinap

    It's from vTaskIncrementTick in tasks.c. When there is critical section (uxSchedulerSuspended is nonzero after vTaskSuspendAll), the timer ticks are counted into uxMissedTicks and replayed in when xTaskResumeAll is called.

  4. krasin author


    thanks for the pointers.

    From what I can see, your solution is also prone to the disabled interrupts, so it might be ~1 tick off (and the tick is 1 ms in this case). Is that right?

    P.S. it appears that uxMissedTicks has been renamed to uxPendedTicks in FreeRTOS 7.5.2.