Skip to content

Commit

Permalink
Extract CurrentTime and remove Timer_{Get,Set}CurrentTime()
Browse files Browse the repository at this point in the history
  • Loading branch information
mondalaci committed Oct 4, 2018
1 parent c5cf738 commit 98f7d51
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 26 deletions.
2 changes: 1 addition & 1 deletion right/src/macros.c
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ bool processDelayAction(void)
inDelay = false;
}
} else {
Timer_SetCurrentTime(&delayStart);
delayStart = CurrentTime;
inDelay = true;
}
return inDelay;
Expand Down
24 changes: 7 additions & 17 deletions right/src/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
#include "timer.h"
#include "peripherals/test_led.h"

volatile uint32_t CurrentTime;
static uint32_t timerClockFrequency;
static volatile uint32_t currentTime, delayLength;
static volatile uint32_t delayLength;

void PIT_TIMER_HANDLER(void)
{
currentTime++;
CurrentTime++;
if (delayLength) {
--delayLength;
}
Expand All @@ -28,48 +29,37 @@ void Timer_Init(void)
PIT_StartTimer(PIT, PIT_TIMER_CHANNEL);
}

uint32_t Timer_GetCurrentTime() {
return currentTime;
}

uint32_t Timer_GetCurrentTimeMicros() {
uint32_t primask, count, ms;
primask = DisableGlobalIRQ(); // Make sure the read is atomic
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

// 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
uint32_t us = 1000U * TIMER_INTERVAL_MSEC - COUNT_TO_USEC(count, timerClockFrequency);
return ms * 1000U * TIMER_INTERVAL_MSEC + us;
}

void Timer_SetCurrentTime(uint32_t *time)
{
*time = Timer_GetCurrentTime();
}

void Timer_SetCurrentTimeMicros(uint32_t *time)
{
*time = Timer_GetCurrentTimeMicros();
}

uint32_t Timer_GetElapsedTime(uint32_t *time)
{
uint32_t elapsedTime = Timer_GetCurrentTime() - *time;
return elapsedTime;
return CurrentTime - *time;
}

uint32_t Timer_GetElapsedTimeMicros(uint32_t *time)
{
uint32_t elapsedTime = Timer_GetCurrentTimeMicros() - *time;
return elapsedTime;
return Timer_GetCurrentTimeMicros() - *time;
}

uint32_t Timer_GetElapsedTimeAndSetCurrent(uint32_t *time)
{
uint32_t elapsedTime = Timer_GetElapsedTime(time);
*time = Timer_GetCurrentTime();
*time = CurrentTime;
return elapsedTime;
}

Expand Down
6 changes: 4 additions & 2 deletions right/src/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@

#define TIMER_INTERVAL_MSEC 1

// Variables:

extern volatile uint32_t CurrentTime;

// Functions:

void Timer_Init(void);
uint32_t Timer_GetCurrentTime();
uint32_t Timer_GetCurrentTimeMicros();
void Timer_SetCurrentTime(uint32_t *time);
void Timer_SetCurrentTimeMicros(uint32_t *time);
uint32_t Timer_GetElapsedTime(uint32_t *time);
uint32_t Timer_GetElapsedTimeMicros(uint32_t *time);
Expand Down
2 changes: 1 addition & 1 deletion right/src/usb_commands/usb_command_get_debug_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void UsbCommand_GetDebugBuffer(void)
SetDebugBufferUint32(13, I2cWatchdog_RecoveryCounter);
SetDebugBufferUint32(17, MatrixScanCounter);
SetDebugBufferUint32(21, UsbReportUpdateCounter);
SetDebugBufferUint32(25, Timer_GetCurrentTime());
SetDebugBufferUint32(25, CurrentTime);
SetDebugBufferUint32(29, UsbGenericHidActionCounter);
SetDebugBufferUint32(33, UsbBasicKeyboardActionCounter);
SetDebugBufferUint32(37, UsbMediaKeyboardActionCounter);
Expand Down
2 changes: 1 addition & 1 deletion right/src/usb_commands/usb_command_get_device_property.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void UsbCommand_GetDeviceProperty(void)
SetUsbTxBufferUint32(6, I2cMainBusActualBaudRateBps);
break;
case DevicePropertyId_Uptime:
SetUsbTxBufferUint32(1, Timer_GetCurrentTime());
SetUsbTxBufferUint32(1, CurrentTime);
break;
default:
SetUsbTxBufferUint8(0, UsbStatusCode_GetDeviceProperty_InvalidProperty);
Expand Down
8 changes: 4 additions & 4 deletions right/src/usb_report_updater.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,11 @@ static void handleSwitchLayerAction(key_state_t *keyState, key_action_t *action)
if (doubleTapSwitchLayerKey && Timer_GetElapsedTimeAndSetCurrent(&doubleTapSwitchLayerStartTime) < DoubleTapSwitchLayerTimeout) {
ToggledLayer = action->switchLayer.layer;
isLayerDoubleTapToggled = true;
doubleTapSwitchLayerTriggerTime = Timer_GetCurrentTime();
doubleTapSwitchLayerTriggerTime = CurrentTime;
} else {
doubleTapSwitchLayerKey = keyState;
}
doubleTapSwitchLayerStartTime = Timer_GetCurrentTime();
doubleTapSwitchLayerStartTime = CurrentTime;
}
}

Expand Down Expand Up @@ -364,13 +364,13 @@ static void updateActiveUsbReports(void)
key_action_t *action;

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;
} else {
keyState->current = keyState->previous;
}
} else if (keyState->previous != keyState->current) {
keyState->timestamp = Timer_GetCurrentTime();
keyState->timestamp = CurrentTime;
keyState->debouncing = true;
}

Expand Down

0 comments on commit 98f7d51

Please sign in to comment.