Skip to content

Commit

Permalink
[sub-mac] unify the timer usage (openthread#10520)
Browse files Browse the repository at this point in the history
The `SubMac` uses `MilliTimer` or `MicroTimer` based on the
configuration.  When the `SubMac` uses the timer, it has to
distinguish the type of the timer. This commit optimizes the code to
provide unified time related functions to `SubMac` to use time. This
commit doesn't change the code logic.
  • Loading branch information
zhanglongxia authored Jul 18, 2024
1 parent b73114c commit 602167f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 44 deletions.
51 changes: 29 additions & 22 deletions src/core/mac/sub_mac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,13 +394,14 @@ void SubMac::StartCsmaBackoff(void)

if (ShouldHandleTransmitTargetTime())
{
if (Time(static_cast<uint32_t>(otPlatRadioGetNow(&GetInstance()))) <
Time(mTransmitFrame.mInfo.mTxInfo.mTxDelayBaseTime) + mTransmitFrame.mInfo.mTxInfo.mTxDelay -
kCcaSampleInterval - kCslTransmitTimeAhead - kRadioHeaderShrDuration)
static constexpr uint32_t kAheadTime = kCcaSampleInterval + kCslTransmitTimeAhead + kRadioHeaderShrDuration;
Time txStartTime = Time(mTransmitFrame.mInfo.mTxInfo.mTxDelayBaseTime);

txStartTime += (mTransmitFrame.mInfo.mTxInfo.mTxDelay - kAheadTime);

if (Time(static_cast<uint32_t>(otPlatRadioGetNow(&GetInstance()))) < txStartTime)
{
mTimer.StartAt(Time(mTransmitFrame.mInfo.mTxInfo.mTxDelayBaseTime) - kCcaSampleInterval -
kCslTransmitTimeAhead - kRadioHeaderShrDuration,
mTransmitFrame.mInfo.mTxInfo.mTxDelay);
mTimer.FireAt(txStartTime);
}
else // Transmit without delay
{
Expand Down Expand Up @@ -444,11 +445,7 @@ void SubMac::StartTimerForBackoff(uint8_t aBackoffExponent)
IgnoreError(Get<Radio>().Sleep());
}

#if OPENTHREAD_CONFIG_PLATFORM_USEC_TIMER_ENABLE
mTimer.Start(backoff);
#else
mTimer.Start(backoff / 1000UL);
#endif
StartTimer(backoff);

#if OPENTHREAD_CONFIG_MAC_ADD_DELAY_ON_NO_ACK_ERROR_BEFORE_RETRY
if (mState == kStateDelayBeforeRetx)
Expand Down Expand Up @@ -501,11 +498,7 @@ void SubMac::HandleTransmitStarted(TxFrame &aFrame)
{
if (ShouldHandleAckTimeout() && aFrame.GetAckRequest())
{
#if OPENTHREAD_CONFIG_PLATFORM_USEC_TIMER_ENABLE
mTimer.Start(kAckTimeout * 1000UL);
#else
mTimer.Start(kAckTimeout);
#endif
StartTimer(kAckTimeout);
}
}

Expand Down Expand Up @@ -706,7 +699,7 @@ Error SubMac::EnergyScan(uint8_t aScanChannel, uint16_t aScanDuration)
SetState(kStateEnergyScan);
mEnergyScanMaxRssi = Radio::kInvalidRssi;
mEnergyScanEndTime = TimerMilli::GetNow() + static_cast<uint32_t>(aScanDuration);
mTimer.Start(0);
StartTimer(0);
}
else
{
Expand All @@ -733,11 +726,7 @@ void SubMac::SampleRssi(void)

if (TimerMilli::GetNow() < mEnergyScanEndTime)
{
#if OPENTHREAD_CONFIG_PLATFORM_USEC_TIMER_ENABLE
mTimer.StartAt(mTimer.GetFireTime(), kEnergyScanRssiSampleInterval * 1000UL);
#else
mTimer.StartAt(mTimer.GetFireTime(), kEnergyScanRssiSampleInterval);
#endif
StartTimerAt(mTimer.GetFireTime(), kEnergyScanRssiSampleInterval);
}
else
{
Expand Down Expand Up @@ -978,6 +967,24 @@ void SubMac::SetFrameCounter(uint32_t aFrameCounter, bool aSetIfLarger)
return;
}

void SubMac::StartTimer(uint32_t aDelayUs)
{
#if OPENTHREAD_CONFIG_PLATFORM_USEC_TIMER_ENABLE
mTimer.Start(aDelayUs);
#else
mTimer.Start(aDelayUs / Time::kOneMsecInUsec);
#endif
}

void SubMac::StartTimerAt(Time aStartTime, uint32_t aDelayUs)
{
#if OPENTHREAD_CONFIG_PLATFORM_USEC_TIMER_ENABLE
mTimer.StartAt(aStartTime, aDelayUs);
#else
mTimer.StartAt(aStartTime, aDelayUs / Time::kOneMsecInUsec);
#endif
}

// LCOV_EXCL_START

const char *SubMac::StateToString(State aState)
Expand Down
25 changes: 14 additions & 11 deletions src/core/mac/sub_mac.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -532,26 +532,27 @@ class SubMac : public InstanceLocator, private NonCopyable
static void HandleCslTimer(Timer &aTimer);
void HandleCslTimer(void);
void GetCslWindowEdges(uint32_t &aAhead, uint32_t &aAfter);
uint32_t GetLocalTime(void);
#if OPENTHREAD_CONFIG_MAC_CSL_DEBUG_ENABLE
void LogReceived(RxFrame *aFrame);
#endif
#endif

static constexpr uint8_t kCsmaMinBe = 3; // macMinBE (IEEE 802.15.4-2006).
static constexpr uint8_t kCsmaMaxBe = 5; // macMaxBE (IEEE 802.15.4-2006).
static constexpr uint32_t kUnitBackoffPeriod = 20; // Number of symbols (IEEE 802.15.4-2006).
static constexpr uint32_t kAckTimeout = 16; // Timeout for waiting on an ACK (in msec).
static constexpr uint32_t kCcaSampleInterval = 128; // CCA sample interval, 128 usec.
static constexpr uint8_t kCsmaMinBe = 3; // macMinBE (IEEE 802.15.4-2006).
static constexpr uint8_t kCsmaMaxBe = 5; // macMaxBE (IEEE 802.15.4-2006).
static constexpr uint32_t kUnitBackoffPeriod = 20; // Number of symbols (IEEE 802.15.4-2006).
static constexpr uint32_t kAckTimeout = 16 * Time::kOneMsecInUsec; // Timeout for waiting on an ACK (in usec).
static constexpr uint32_t kCcaSampleInterval = 128; // CCA sample interval, 128 usec.

#if OPENTHREAD_CONFIG_MAC_ADD_DELAY_ON_NO_ACK_ERROR_BEFORE_RETRY
static constexpr uint8_t kRetxDelayMinBackoffExponent = OPENTHREAD_CONFIG_MAC_RETX_DELAY_MIN_BACKOFF_EXPONENT;
static constexpr uint8_t kRetxDelayMaxBackoffExponent = OPENTHREAD_CONFIG_MAC_RETX_DELAY_MAX_BACKOFF_EXPONENT;
#endif

#if OPENTHREAD_CONFIG_PLATFORM_USEC_TIMER_ENABLE
static constexpr uint32_t kEnergyScanRssiSampleInterval = 128; // RSSI sample interval for energy scan, 128 usec
static constexpr uint32_t kEnergyScanRssiSampleInterval = 128; // RSSI sample interval for energy scan, in usec
#else
static constexpr uint32_t kEnergyScanRssiSampleInterval = 1; // RSSI sample interval during energy scan, 1 msec
static constexpr uint32_t kEnergyScanRssiSampleInterval = 1000; // RSSI sample interval for energy scan, in usec
#endif

enum State : uint8_t
Expand Down Expand Up @@ -623,6 +624,8 @@ class SubMac : public InstanceLocator, private NonCopyable
void StartTimerForBackoff(uint8_t aBackoffExponent);
void BeginTransmit(void);
void SampleRssi(void);
void StartTimer(uint32_t aDelayUs);
void StartTimerAt(Time aStartTime, uint32_t aDelayUs);

void HandleReceiveDone(RxFrame *aFrame, Error aError);
void HandleTransmitStarted(TxFrame &aFrame);
Expand Down Expand Up @@ -667,10 +670,10 @@ class SubMac : public InstanceLocator, private NonCopyable
SubMacTimer mTimer;

#if OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE
uint16_t mCslPeriod; // The CSL sample period, in units of 10 symbols (160 microseconds).
uint8_t mCslChannel : 7; // The CSL sample channel.
bool mIsCslSampling : 1; // Indicates that the radio is receiving in CSL state for platforms not supporting delayed
// reception.
uint16_t mCslPeriod; // The CSL sample period, in units of 10 symbols (160 microseconds).
uint8_t mCslChannel : 7; // The CSL sample channel.
bool mIsCslSampling : 1; // Indicates that the radio is receiving in CSL state for platforms not supporting
// delayed reception.
uint16_t mCslPeerShort; // The CSL peer short address.
TimeMicro mCslSampleTime; // The CSL sample time of the current period relative to the local radio clock.
TimeMicro mCslLastSync; // The timestamp of the last successful CSL synchronization.
Expand Down
26 changes: 15 additions & 11 deletions src/core/mac/sub_mac_csl_receiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,7 @@ void SubMac::UpdateCslLastSyncTimestamp(TxFrame &aFrame, RxFrame *aAckFrame)
// Assuming the error here since it is bounded and has very small effect on the final window duration.
if (aAckFrame != nullptr && aFrame.GetHeaderIe(CslIe::kHeaderIeId) != nullptr)
{
#if OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_LOCAL_TIME_SYNC
mCslLastSync = TimerMicro::GetNow();
#else
mCslLastSync = TimeMicro(static_cast<uint32_t>(otPlatRadioGetNow(&GetInstance())));
#endif
mCslLastSync = TimeMicro(GetLocalTime());
}
}

Expand Down Expand Up @@ -237,12 +233,7 @@ void SubMac::GetCslWindowEdges(uint32_t &aAhead, uint32_t &aAfter)
uint32_t semiPeriod = mCslPeriod * kUsPerTenSymbols / 2;
uint32_t curTime, elapsed, semiWindow;

#if OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_LOCAL_TIME_SYNC
curTime = TimerMicro::GetNow().GetValue();
#else
curTime = static_cast<uint32_t>(otPlatRadioGetNow(&GetInstance()));
#endif

curTime = GetLocalTime();
elapsed = curTime - mCslLastSync.GetValue();

semiWindow =
Expand All @@ -254,6 +245,19 @@ void SubMac::GetCslWindowEdges(uint32_t &aAhead, uint32_t &aAfter)
aAfter = Min(semiPeriod, semiWindow + kMinReceiveOnAfter);
}

uint32_t SubMac::GetLocalTime(void)
{
uint32_t now;

#if OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_LOCAL_TIME_SYNC
now = TimerMicro::GetNow().GetValue();
#else
now = static_cast<uint32_t>(otPlatRadioGetNow(&GetInstance()));
#endif

return now;
}

#if OPENTHREAD_CONFIG_MAC_CSL_DEBUG_ENABLE
void SubMac::LogReceived(RxFrame *aFrame)
{
Expand Down

0 comments on commit 602167f

Please sign in to comment.