|
-
-
-
- #include "IntervalTimer.h"
-
-
-
-
-
- bool IntervalTimer::PIT_enabled;
- bool IntervalTimer::PIT_used[];
- IntervalTimer::ISR IntervalTimer::PIT_ISR[];
-
-
-
-
-
-
-
-
-
- #if defined(KINETISK)
- void pit0_isr() { PIT_TFLG0 = 1; IntervalTimer::PIT_ISR[0](); }
- void pit1_isr() { PIT_TFLG1 = 1; IntervalTimer::PIT_ISR[1](); }
- void pit2_isr() { PIT_TFLG2 = 1; IntervalTimer::PIT_ISR[2](); }
- void pit3_isr() { PIT_TFLG3 = 1; IntervalTimer::PIT_ISR[3](); }
-
- #elif defined(KINETISL)
- void pit_isr() {
- if (PIT_TFLG0) { PIT_TFLG0 = 1; IntervalTimer::PIT_ISR[0](); }
- if (!IntervalTimer::PIT_enabled) return;
- if (PIT_TFLG1) { PIT_TFLG1 = 1; IntervalTimer::PIT_ISR[1](); }
- }
- #endif
-
-
-
-
-
-
-
-
-
-
-
-
- bool IntervalTimer::beginCycles(ISR newISR, uint32_t newValue) {
-
-
- if (status == TIMER_PIT) {
- stop_PIT();
- status = TIMER_OFF;
- }
-
- myISR = newISR;
-
-
- if (allocate_PIT(newValue)) status = TIMER_PIT;
- else status = TIMER_OFF;
-
-
- if (status != TIMER_OFF) return true;
- return false;
-
- }
-
-
-
-
-
-
- void IntervalTimer::end() {
- if (status == TIMER_PIT) stop_PIT();
- status = TIMER_OFF;
- }
-
-
-
-
-
-
- void IntervalTimer::enable_PIT() {
- SIM_SCGC6 |= SIM_SCGC6_PIT;
- PIT_MCR = 0;
- PIT_enabled = true;
- }
-
-
-
-
-
-
- void IntervalTimer::disable_PIT() {
- PIT_MCR = 1;
- SIM_SCGC6 &= ~SIM_SCGC6_PIT;
- PIT_enabled = false;
- }
-
-
-
-
-
-
-
-
-
- bool IntervalTimer::allocate_PIT(uint32_t newValue) {
-
-
- if (!PIT_enabled) enable_PIT();
-
-
- for (uint8_t id = 0; id < NUM_PIT; id++) {
- if (!PIT_used[id]) {
- PIT_id = id;
- start_PIT(newValue);
- PIT_used[id] = true;
- return true;
- }
- }
-
-
- return false;
-
- }
-
-
-
-
-
-
-
- void IntervalTimer::start_PIT(uint32_t newValue) {
-
-
- PIT_LDVAL = &PIT_LDVAL0 + PIT_id * 4;
- PIT_TCTRL = &PIT_TCTRL0 + PIT_id * 4;
-
-
- PIT_ISR[PIT_id] = myISR;
-
-
- *PIT_TCTRL = 0;
- *PIT_LDVAL = newValue;
- *PIT_TCTRL = 3;
- #if defined(KINETISK)
- IRQ_PIT_CH = IRQ_PIT_CH0 + PIT_id;
- NVIC_SET_PRIORITY(IRQ_PIT_CH, nvic_priority);
- NVIC_ENABLE_IRQ(IRQ_PIT_CH);
- #elif defined(KINETISL)
- NVIC_SET_PRIORITY(IRQ_PIT, nvic_priority);
- NVIC_ENABLE_IRQ(IRQ_PIT);
- #endif
-
- }
-
-
-
-
-
-
-
-
- void IntervalTimer::stop_PIT() {
-
-
- *PIT_TCTRL = 0;
- #if defined(KINETISK)
- NVIC_DISABLE_IRQ(IRQ_PIT_CH);
- #elif defined(KINETISL)
- NVIC_DISABLE_IRQ(IRQ_PIT);
- #endif
-
-
- PIT_used[PIT_id] = false;
-
-
- for (uint8_t id = 0; id < NUM_PIT; id++) {
- if (PIT_used[id]) return;
- }
-
-
- disable_PIT();
-
- }
-
|