Created
February 19, 2022 06:04
-
-
Save cms-codes/8443fab3e81269d9e16fa2eb3802b7f1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "systick.h" | |
volatile uint32_t systickCount = 0; | |
// ******* Systick_init ******* | |
// Initializes the SysTick interrupt timer. | |
// Inputs: none | |
// Outputs: none | |
void Systick_init(void) | |
{ | |
// 48MHz int osc | |
systick_set_clocksource(STK_CSR_CLKSOURCE_AHB); | |
// 48000000/100000 = 480 overflows per second - every 10 microseconds equals | |
// one interrupt. | |
// SysTick interrupt every N clock pulses: set reload to N-1 | |
systick_set_reload(47900); | |
systick_interrupt_enable(); | |
// Start counting | |
systick_counter_enable(); | |
} | |
// ******* Systick_timeGetCount ******* | |
// Returns the elapsed program time based on the count of SysTick interrupts. | |
// Inputs: none | |
// Outputs: A count of elapsed cycles. | |
uint32_t Systick_timeGetCount(void) | |
{ | |
return systickCount; | |
} | |
// ******* Systick_timeDelta ******* | |
// Provides the difference between two program times, as provided by | |
// Systick_get_time(). | |
// Inputs: A start time and an end time. | |
// Outputs: The delta between two points in execution time. | |
uint32_t Systick_timeDelta( uint32_t start, uint32_t end ) | |
{ | |
uint32_t diff; | |
// Detect if roll-over has occurred and calculate accordingly. | |
if ( end > start ) | |
{ | |
diff = end - start; | |
} | |
else | |
{ | |
diff = UINT32_MAX - ( start - end ) + 1; | |
} | |
return diff; | |
} | |
// ******* Systick_delayTicks ******* | |
// Delays execution for x ticks based on time reporting from | |
// Systick_get_time(). Scheduler event interrupt continues to fire. | |
// Inputs: Number of ticks to delay execution. | |
// Outputs: none | |
void Systick_delayTicks( uint32_t wait_ticks ) | |
{ | |
volatile uint32_t start, now, diff; | |
start = Systick_timeGetCount(); | |
do | |
{ | |
__asm__ __volatile__ ("nop"); | |
now = Systick_timeGetCount(); | |
diff = Systick_timeDelta( start, now ); | |
} | |
while ( diff < wait_ticks ); | |
} | |
// ******* sys_tick_handler ******* | |
// Predefined SysTick ISR function with libopencm3 prototype. Runs the event | |
// manager and increments the counter. | |
// Inputs: none | |
// Outputs: none | |
void sys_tick_handler(void) | |
{ | |
//Sched_runEventManager(); | |
// Roll over to 0 at UINT32_MAX | |
if ( systickCount < UINT32_MAX ) | |
{ | |
systickCount++; | |
} | |
else | |
{ | |
systickCount = 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment