Extract CurrentTime and remove Timer_{Get,Set}CurrentTime()

This commit is contained in:
László Monda
2018-10-04 20:38:36 +02:00
parent c5cf738fd0
commit 98f7d512de
6 changed files with 18 additions and 26 deletions

View File

@@ -286,7 +286,7 @@ bool processDelayAction(void)
inDelay = false; inDelay = false;
} }
} else { } else {
Timer_SetCurrentTime(&delayStart); delayStart = CurrentTime;
inDelay = true; inDelay = true;
} }
return inDelay; return inDelay;

View File

@@ -2,12 +2,13 @@
#include "timer.h" #include "timer.h"
#include "peripherals/test_led.h" #include "peripherals/test_led.h"
volatile uint32_t CurrentTime;
static uint32_t timerClockFrequency; static uint32_t timerClockFrequency;
static volatile uint32_t currentTime, delayLength; static volatile uint32_t delayLength;
void PIT_TIMER_HANDLER(void) void PIT_TIMER_HANDLER(void)
{ {
currentTime++; CurrentTime++;
if (delayLength) { if (delayLength) {
--delayLength; --delayLength;
} }
@@ -28,15 +29,11 @@ void Timer_Init(void)
PIT_StartTimer(PIT, PIT_TIMER_CHANNEL); PIT_StartTimer(PIT, PIT_TIMER_CHANNEL);
} }
uint32_t Timer_GetCurrentTime() {
return currentTime;
}
uint32_t Timer_GetCurrentTimeMicros() { uint32_t Timer_GetCurrentTimeMicros() {
uint32_t primask, count, ms; uint32_t primask, count, ms;
primask = DisableGlobalIRQ(); // Make sure the read is atomic primask = DisableGlobalIRQ(); // Make sure the read is atomic
count = PIT_GetCurrentTimerCount(PIT, PIT_TIMER_CHANNEL); // Read the current timer count count = PIT_GetCurrentTimerCount(PIT, PIT_TIMER_CHANNEL); // Read the current timer count
ms = currentTime; // Read the overflow counter ms = CurrentTime; // Read the overflow counter
EnableGlobalIRQ(primask); // Enable interrupts again if they where enabled before - this should make it interrupt safe EnableGlobalIRQ(primask); // Enable interrupts again if they where enabled before - this should make it interrupt safe
// Calculate the counter value in microseconds - note that the PIT timer is counting downward, so we need to subtract the count from the period value // Calculate the counter value in microseconds - note that the PIT timer is counting downward, so we need to subtract the count from the period value
@@ -44,11 +41,6 @@ uint32_t Timer_GetCurrentTimeMicros() {
return ms * 1000U * TIMER_INTERVAL_MSEC + us; return ms * 1000U * TIMER_INTERVAL_MSEC + us;
} }
void Timer_SetCurrentTime(uint32_t *time)
{
*time = Timer_GetCurrentTime();
}
void Timer_SetCurrentTimeMicros(uint32_t *time) void Timer_SetCurrentTimeMicros(uint32_t *time)
{ {
*time = Timer_GetCurrentTimeMicros(); *time = Timer_GetCurrentTimeMicros();
@@ -56,20 +48,18 @@ void Timer_SetCurrentTimeMicros(uint32_t *time)
uint32_t Timer_GetElapsedTime(uint32_t *time) uint32_t Timer_GetElapsedTime(uint32_t *time)
{ {
uint32_t elapsedTime = Timer_GetCurrentTime() - *time; return CurrentTime - *time;
return elapsedTime;
} }
uint32_t Timer_GetElapsedTimeMicros(uint32_t *time) uint32_t Timer_GetElapsedTimeMicros(uint32_t *time)
{ {
uint32_t elapsedTime = Timer_GetCurrentTimeMicros() - *time; return Timer_GetCurrentTimeMicros() - *time;
return elapsedTime;
} }
uint32_t Timer_GetElapsedTimeAndSetCurrent(uint32_t *time) uint32_t Timer_GetElapsedTimeAndSetCurrent(uint32_t *time)
{ {
uint32_t elapsedTime = Timer_GetElapsedTime(time); uint32_t elapsedTime = Timer_GetElapsedTime(time);
*time = Timer_GetCurrentTime(); *time = CurrentTime;
return elapsedTime; return elapsedTime;
} }

View File

@@ -9,12 +9,14 @@
#define TIMER_INTERVAL_MSEC 1 #define TIMER_INTERVAL_MSEC 1
// Variables:
extern volatile uint32_t CurrentTime;
// Functions: // Functions:
void Timer_Init(void); void Timer_Init(void);
uint32_t Timer_GetCurrentTime();
uint32_t Timer_GetCurrentTimeMicros(); uint32_t Timer_GetCurrentTimeMicros();
void Timer_SetCurrentTime(uint32_t *time);
void Timer_SetCurrentTimeMicros(uint32_t *time); void Timer_SetCurrentTimeMicros(uint32_t *time);
uint32_t Timer_GetElapsedTime(uint32_t *time); uint32_t Timer_GetElapsedTime(uint32_t *time);
uint32_t Timer_GetElapsedTimeMicros(uint32_t *time); uint32_t Timer_GetElapsedTimeMicros(uint32_t *time);

View File

@@ -22,7 +22,7 @@ void UsbCommand_GetDebugBuffer(void)
SetDebugBufferUint32(13, I2cWatchdog_RecoveryCounter); SetDebugBufferUint32(13, I2cWatchdog_RecoveryCounter);
SetDebugBufferUint32(17, MatrixScanCounter); SetDebugBufferUint32(17, MatrixScanCounter);
SetDebugBufferUint32(21, UsbReportUpdateCounter); SetDebugBufferUint32(21, UsbReportUpdateCounter);
SetDebugBufferUint32(25, Timer_GetCurrentTime()); SetDebugBufferUint32(25, CurrentTime);
SetDebugBufferUint32(29, UsbGenericHidActionCounter); SetDebugBufferUint32(29, UsbGenericHidActionCounter);
SetDebugBufferUint32(33, UsbBasicKeyboardActionCounter); SetDebugBufferUint32(33, UsbBasicKeyboardActionCounter);
SetDebugBufferUint32(37, UsbMediaKeyboardActionCounter); SetDebugBufferUint32(37, UsbMediaKeyboardActionCounter);

View File

@@ -69,7 +69,7 @@ void UsbCommand_GetDeviceProperty(void)
SetUsbTxBufferUint32(6, I2cMainBusActualBaudRateBps); SetUsbTxBufferUint32(6, I2cMainBusActualBaudRateBps);
break; break;
case DevicePropertyId_Uptime: case DevicePropertyId_Uptime:
SetUsbTxBufferUint32(1, Timer_GetCurrentTime()); SetUsbTxBufferUint32(1, CurrentTime);
break; break;
default: default:
SetUsbTxBufferUint8(0, UsbStatusCode_GetDeviceProperty_InvalidProperty); SetUsbTxBufferUint8(0, UsbStatusCode_GetDeviceProperty_InvalidProperty);

View File

@@ -227,11 +227,11 @@ static void handleSwitchLayerAction(key_state_t *keyState, key_action_t *action)
if (doubleTapSwitchLayerKey && Timer_GetElapsedTimeAndSetCurrent(&doubleTapSwitchLayerStartTime) < DoubleTapSwitchLayerTimeout) { if (doubleTapSwitchLayerKey && Timer_GetElapsedTimeAndSetCurrent(&doubleTapSwitchLayerStartTime) < DoubleTapSwitchLayerTimeout) {
ToggledLayer = action->switchLayer.layer; ToggledLayer = action->switchLayer.layer;
isLayerDoubleTapToggled = true; isLayerDoubleTapToggled = true;
doubleTapSwitchLayerTriggerTime = Timer_GetCurrentTime(); doubleTapSwitchLayerTriggerTime = CurrentTime;
} else { } else {
doubleTapSwitchLayerKey = keyState; doubleTapSwitchLayerKey = keyState;
} }
doubleTapSwitchLayerStartTime = Timer_GetCurrentTime(); doubleTapSwitchLayerStartTime = CurrentTime;
} }
} }
@@ -364,13 +364,13 @@ static void updateActiveUsbReports(void)
key_action_t *action; key_action_t *action;
if (keyState->debouncing) { if (keyState->debouncing) {
if ((uint8_t)(Timer_GetCurrentTime() - keyState->timestamp) > (keyState->previous ? DebounceTimePress : DebounceTimeRelease)) { if ((uint8_t)(CurrentTime - keyState->timestamp) > (keyState->previous ? DebounceTimePress : DebounceTimeRelease)) {
keyState->debouncing = false; keyState->debouncing = false;
} else { } else {
keyState->current = keyState->previous; keyState->current = keyState->previous;
} }
} else if (keyState->previous != keyState->current) { } else if (keyState->previous != keyState->current) {
keyState->timestamp = Timer_GetCurrentTime(); keyState->timestamp = CurrentTime;
keyState->debouncing = true; keyState->debouncing = true;
} }