diff --git a/AppKit/stopwatch.c b/AppKit/stopwatch.c new file mode 100644 index 0000000..137f10e --- /dev/null +++ b/AppKit/stopwatch.c @@ -0,0 +1 @@ +#include diff --git a/AppKit/stopwatch.h b/AppKit/stopwatch.h new file mode 100644 index 0000000..122836d --- /dev/null +++ b/AppKit/stopwatch.h @@ -0,0 +1,38 @@ +#ifndef INCLUDED_STOPWATCH_H +#define INCLUDED_STOPWATCH_H + +#ifndef INCLUDED_OS_SYSTICK_H +#include +#endif /*INCLUDED_OS_SYSTICK_H*/ + + +/* ------------------------------------------------------------------------------------------------------------------ */ +/* */ + +typedef struct { + volatile os_tick_t start_tick; + volatile os_tick_t end_tick; +}stopwatch_t; + +OS_STATIC_FORCE_INLINE +void stopwatch_init(stopwatch_t* self) { + self->start_tick = os_systick_safe_get(); +} + +OS_STATIC_FORCE_INLINE +void stopwatch_start(stopwatch_t* self) { + self->start_tick = os_systick_safe_get(); +} + +OS_STATIC_FORCE_INLINE +void stopwatch_stop(stopwatch_t* self) { + self->end_tick = os_systick_safe_get(); +} + +OS_STATIC_FORCE_INLINE +os_tick_t stopwatch_elapsed_ticks(stopwatch_t* self) { + return self->end_tick - self->start_tick; +} + + +#endif /*INCLUDED_STOPWATCH_H*/ diff --git a/Kernel/SingleCore/os_systick.h b/Kernel/SingleCore/os_systick.h index 60e6af3..f8f4a4b 100644 --- a/Kernel/SingleCore/os_systick.h +++ b/Kernel/SingleCore/os_systick.h @@ -69,15 +69,23 @@ void os_systick_tick(void){ OS_STATIC_FORCE_INLINE os_tick_t os_systick_get(void){ -#if 0 - os_critical_enter(); - os_tick_t ticks = g_os_systick_ticks; - os_critical_leave(); - return ticks; -#endif return g_os_systick_ticks; } +OS_STATIC_FORCE_INLINE +os_tick_t os_systick_safe_get(void) { + os_critical_enter(); + const os_tick_t ticks = g_os_systick_ticks; + os_critical_leave(); + return ticks; +} +OS_STATIC_FORCE_INLINE +os_tick_t os_systick_safe_get_in_isr(void) { + os_critical_enter_in_isr(); + const os_tick_t ticks = g_os_systick_ticks; + os_critical_leave_in_isr(); + return ticks; +} #endif /*INCLUDED_OS_SYSTICK_H*/