Mark3 Realtime Kernel
kerneltimer.cpp
Go to the documentation of this file.
1 /*===========================================================================
2  _____ _____ _____ _____
3  ___| _|__ __|_ |__ __|__ |__ __| __ |__ ______
4 | \ / | || \ || | || |/ / ||___ |
5 | \/ | || \ || \ || \ ||___ |
6 |__/\__/|__|_||__|\__\ __||__|\__\ __||__|\__\ __||______|
7  |_____| |_____| |_____| |_____|
8 
9 --[Mark3 Realtime Platform]--------------------------------------------------
10 
11 Copyright (c) 2012 - 2019 m0slevin, all rights reserved.
12 See license.txt for more information
13 ===========================================================================*/
21 #include "mark3.h"
22 
23 #include <avr/common.h>
24 #include <avr/io.h>
25 #include <avr/interrupt.h>
26 
27 #define TCCR1B_INIT ((1 << WGM12) | (1 << CS12))
28 #define TIMER_IMSK (1 << OCIE1A)
29 #define TIMER_IFR (1 << OCF1A)
30 
31 namespace
32 {
33 using namespace Mark3;
34 //---------------------------------------------------------------------------
35 // Static objects implementing the timer thread and its synchronization objects
36 Thread s_clTimerThread;
37 K_WORD s_clTimerThreadStack[PORT_KERNEL_TIMERS_THREAD_STACK];
38 Semaphore s_clTimerSemaphore;
39 }
40 
41 namespace Mark3
42 {
43 //---------------------------------------------------------------------------
44 static void KernelTimer_Task(void* unused)
45 {
46  (void)unused;
47  while (1) {
48  s_clTimerSemaphore.Pend();
49 #if KERNEL_ROUND_ROBIN
51 #endif // #if KERNEL_ROUND_ROBIN
53 #if KERNEL_ROUND_ROBIN
55 #endif // #if KERNEL_ROUND_ROBIN
56  }
57 }
58 
59 //---------------------------------------------------------------------------
61 {
62  TCCR1B = TCCR1B_INIT;
63  s_clTimerSemaphore.Init(0, 1);
64  s_clTimerThread.Init(s_clTimerThreadStack,
65  sizeof(s_clTimerThreadStack) / sizeof(K_WORD),
68  0);
69 #if KERNEL_ROUND_ROBIN
70  Quantum::SetTimerThread(&s_clTimerThread);
71 #endif // #if KERNEL_ROUND_ROBIN
72  s_clTimerThread.Start();
73 }
74 
75 //---------------------------------------------------------------------------
77 {
78  TCCR1B = ((1 << WGM12) | (1 << CS11) | (1 << CS10));
79  OCR1A = ((PORT_SYSTEM_FREQ / 1000) / 64);
80  TCNT1 = 0;
81  TIFR1 &= ~TIMER_IFR;
82  TIMSK1 |= TIMER_IMSK;
83 }
84 
85 //---------------------------------------------------------------------------
87 {
88  TIFR1 &= ~TIMER_IFR;
89  TIMSK1 &= ~TIMER_IMSK;
90  TCCR1B &= ~(1 << CS12); // Disable count...
91  TCNT1 = 0;
92  OCR1A = 0;
93 }
94 } // namespace Mark3
95 
96 //---------------------------------------------------------------------------
101 //---------------------------------------------------------------------------
102 using namespace Mark3;
103 ISR(TIMER1_COMPA_vect)
104 {
105  Kernel::Tick();
106  s_clTimerSemaphore.Post();
107 }
#define K_WORD
Size of a data word.
Definition: portcfg.h:62
#define PORT_KERNEL_TIMERS_THREAD_STACK
Definition: portcfg.h:101
ISR(INT2_vect) __attribute__((signal
ISR(INT2 _vect) SWI using INT2 - used to trigger a context switch.
Definition: threadport.cpp:137
#define TIMER_IFR
Definition: kerneltimer.cpp:29
static void KernelTimer_Task(void *unused)
Definition: kerneltimer.cpp:44
static void Process()
Process This function must be called on timer expiry (from the timer&#39;s ISR context). This will result in all timers being updated based on the epoch that just elapsed. The next timer epoch is set based on the next Timer object to expire.
the Semaphore class provides Binary & Counting semaphore objects, based on BlockingObject base class...
Definition: ksemaphore.h:36
static void SetTimerThread(Thread *pclTimerThread_)
SetTimerThread Pass the timer thread&#39;s Thread pointer to the Quantum module to track against requests...
Definition: quantum.h:79
Definition: atomic.cpp:23
static void Tick()
Definition: kernel.h:205
static void Start(void)
Start Starts the kernel time (must be configured first)
Definition: kerneltimer.cpp:76
static void SetInTimer()
SetInTimer Set a flag to indicate that the CPU is currently running within the timer-callback routine...
#define TIMER_IMSK
Definition: kerneltimer.cpp:28
Single include file given to users of the Mark3 Kernel API.
The Thread Class. This object providing the fundamental thread control data structures and functions ...
Definition: thread.h:64
static void ClearInTimer()
ClearInTimer Clear the flag once the timer callback function has been completed.
#define KERNEL_TIMERS_THREAD_PRIORITY
Definition: portcfg.h:37
static void Stop(void)
Stop Shut down the kernel timer, used when no timers are scheduled.
Definition: kerneltimer.cpp:86
#define PORT_SYSTEM_FREQ
CPU Frequency in Hz.
Definition: portcfg.h:81
static void Config(void)
Config Initializes the kernel timer before use.
Definition: kerneltimer.cpp:60
#define TCCR1B_INIT
Definition: kerneltimer.cpp:27