Skip to content

Commit

Permalink
usec timer: add support to reset time
Browse files Browse the repository at this point in the history
* initUsecTimer -> usecTimerInit for consistency
* new (public) function to reset the timer to 0
* new parameter "usec.reset" to trigger a reset

The latter an easy way to synchronize the clocks of multiple UAVs, by
sending a broadcast parameter change.
  • Loading branch information
whoenig committed Mar 24, 2023
1 parent 1346781 commit 15f674a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
9 changes: 7 additions & 2 deletions src/hal/interface/usec_time.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* Crazyflie control firmware
*
* Copyright (C) 2011-2021 Bitcraze AB
* Copyright (C) 2011-2023 Bitcraze AB
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -30,7 +30,12 @@
/**
* Initialize microsecond-resolution timer (TIM1).
*/
void initUsecTimer(void);
void usecTimerInit(void);

/**
* Reset the microsecond-resolution timer to 0.
*/
void usecTimerReset(void);

/**
* Get microsecond-resolution timestamp.
Expand Down
38 changes: 36 additions & 2 deletions src/hal/src/usec_time.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* Crazyflie control firmware
*
* Copyright (C) 2011-2021 Bitcraze AB
* Copyright (C) 2011-2023 Bitcraze AB
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -27,14 +27,16 @@
#include <stdbool.h>
#include "usec_time.h"
#include "cfassert.h"
#include "param.h"

#include "nvicconf.h"
#include "stm32fxxx.h"

static bool isInit = false;
static uint8_t reset = 0;
static uint32_t usecTimerHighCount;

void initUsecTimer(void)
void usecTimerInit(void)
{
if (isInit) {
return;
Expand Down Expand Up @@ -71,6 +73,16 @@ void initUsecTimer(void)
isInit = true;
}

void usecTimerReset(void)
{
IF_DEBUG_ASSERT(isInit);

const uint32_t zero = 0;
__atomic_store(&usecTimerHighCount, &zero, __ATOMIC_SEQ_CST);

TIM7->CNT = 0;
}

uint64_t usecTimestamp(void)
{
IF_DEBUG_ASSERT(isInit);
Expand All @@ -96,3 +108,25 @@ void __attribute__((used)) TIM7_IRQHandler(void)

__sync_fetch_and_add(&usecTimerHighCount, 1);
}

/**
* Parameters for the usec timer
* */
static void resetParamCallback(void)
{
if (reset) {
usecTimerReset();
reset = 0;
}
}

PARAM_GROUP_START(usec)

/**
* @brief Reset the time to zero.
*
* Useful for time synchronization between UAVs, if reset is send as a broadcast.
*/
PARAM_ADD_WITH_CALLBACK(PARAM_UINT8, reset, &reset, &resetParamCallback)

PARAM_GROUP_STOP(usec)

0 comments on commit 15f674a

Please sign in to comment.