Skip to content

Commit 7872b6a

Browse files
Kimmo Vaisanentheotherjimmy
authored andcommitted
Use EventQueue for elapsed time
Current implementation uses high resolution timers to calculate elapsed time. This prevents for example deep sleep completely and causes unnecessary timer events. This commit changes implamentation to use EventQueue::tick() to get elapsed time.
1 parent b0889f7 commit 7872b6a

File tree

3 files changed

+17
-38
lines changed

3 files changed

+17
-38
lines changed

features/lorawan/LoRaWANStack.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ lora_mac_status_t LoRaWANStack::initialize_mac_layer(EventQueue *queue)
199199
_compliance_test.app_data_buffer = compliance_test_buffer;
200200
#endif
201201

202-
TimerTimeCounterInit( );
202+
TimerTimeCounterInit(queue);
203203
LoRaMacPrimitives.MacMcpsConfirm = callback(this, &LoRaWANStack::mcps_confirm);
204204
LoRaMacPrimitives.MacMcpsIndication = callback(this, &LoRaWANStack::mcps_indication);
205205
LoRaMacPrimitives.MacMlmeConfirm = callback(this, &LoRaWANStack::mlme_confirm);

features/lorawan/system/LoRaWANTimer.cpp

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,46 +20,22 @@ SPDX-License-Identifier: BSD-3-Clause
2020

2121
#include "lorawan/system/LoRaWANTimer.h"
2222

23-
static mbed::Timer TimeCounter;
24-
static mbed::Ticker LoadTimeCounter;
23+
static events::EventQueue *_queue = NULL;
2524

26-
volatile uint32_t CurrentTime = 0;
27-
28-
void TimerResetTimeCounter( void )
29-
{
30-
CurrentTime = CurrentTime + TimeCounter.read_us( ) / 1000;
31-
TimeCounter.reset( );
32-
TimeCounter.start( );
33-
}
34-
35-
void TimerTimeCounterInit( void )
25+
void TimerTimeCounterInit(events::EventQueue *queue)
3626
{
37-
TimeCounter.start( );
38-
LoadTimeCounter.attach( mbed::callback( &TimerResetTimeCounter ), 10 );
27+
_queue = queue;
3928
}
4029

4130
TimerTime_t TimerGetCurrentTime( void )
4231
{
43-
CurrentTime += TimeCounter.read_us( ) / 1000;
44-
TimeCounter.reset( );
45-
TimeCounter.start( );
46-
return ( ( TimerTime_t )CurrentTime );
32+
const uint32_t current_time = _queue->tick();
33+
return (TimerTime_t)current_time;
4734
}
4835

4936
TimerTime_t TimerGetElapsedTime( TimerTime_t savedTime )
5037
{
51-
CurrentTime += TimeCounter.read_us( ) / 1000;
52-
TimeCounter.reset( );
53-
TimeCounter.start( );
54-
return ( TimerTime_t )( CurrentTime - savedTime );
55-
}
56-
57-
TimerTime_t TimerGetFutureTime( TimerTime_t eventInFuture )
58-
{
59-
CurrentTime += TimeCounter.read_us( ) / 1000;
60-
TimeCounter.reset( );
61-
TimeCounter.start( );
62-
return ( TimerTime_t )( CurrentTime + eventInFuture );
38+
return TimerGetCurrentTime() - savedTime;
6339
}
6440

6541
void TimerInit( TimerEvent_t *obj, void ( *callback )( void ) )

features/lorawan/system/LoRaWANTimer.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ SPDX-License-Identifier: BSD-3-Clause
2323
#include "drivers/Timer.h"
2424
#include "drivers/Ticker.h"
2525
#include "lorawan/system/lorawan_data_structures.h"
26+
#include "events/EventQueue.h"
2627

2728
/*!
2829
* \brief Timer object description
@@ -34,13 +35,6 @@ typedef struct TimerEvent_s
3435
mbed::Ticker Timer;
3536
}TimerEvent_t;
3637

37-
/*!
38-
* \brief Initializes the timer used to get the current time.
39-
*
40-
* \remark The current time corresponds to the time since system startup.
41-
*/
42-
void TimerTimeCounterInit( void );
43-
4438
/*!
4539
* \brief Initializes the timer object.
4640
*
@@ -81,6 +75,15 @@ void TimerReset( TimerEvent_t *obj );
8175
*/
8276
void TimerSetValue( TimerEvent_t *obj, uint32_t value );
8377

78+
/*!
79+
* \brief Initializes the timer used to get the current time.
80+
*
81+
* \remark The current time corresponds to the time since system startup.
82+
*
83+
* \param [in] queue Handle to EventQueue object
84+
*/
85+
void TimerTimeCounterInit(events::EventQueue *queue);
86+
8487
/*!
8588
* \brief Read the current time.
8689
*

0 commit comments

Comments
 (0)