Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

usec timer: add support to reset time #1255

Merged
merged 1 commit into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/config/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
#define FREERTOS_MCU_CLOCK_HZ 168000000

#define configGENERATE_RUN_TIME_STATS 1
#define portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() initUsecTimer()
#define portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() usecTimerInit()
#define portGET_RUN_TIME_COUNTER_VALUE() usecTimestamp()


Expand Down
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)
2 changes: 1 addition & 1 deletion src/modules/src/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ void systemTask(void *arg)
uart1Init(CONFIG_DEBUG_PRINT_ON_UART1_BAUDRATE);
#endif

initUsecTimer();
usecTimerInit();
i2cdevInit(I2C3_DEV);
i2cdevInit(I2C1_DEV);
passthroughInit();
Expand Down