|
-
-
- #include "IntervalTimer.h"
- #include "debug/printf.h"
-
- static void pit_isr(void);
-
- #define NUM_CHANNELS 4
- static void (*funct_table[4])(void) __attribute((aligned(32))) = {nullptr, nullptr, nullptr, nullptr};
- uint8_t IntervalTimer::nvic_priorites[4] = {255, 255, 255, 255};
-
-
- bool IntervalTimer::beginCycles(void (*funct)(), uint32_t cycles)
- {
- printf("beginCycles %u\n", cycles);
- if (channel) {
- channel->TCTRL = 0;
- channel->TFLG = 1;
- } else {
- CCM_CCGR1 |= CCM_CCGR1_PIT(CCM_CCGR_ON);
-
- PIT_MCR = 1;
- channel = IMXRT_PIT_CHANNELS;
- while (1) {
- if (channel->TCTRL == 0) break;
- if (++channel >= IMXRT_PIT_CHANNELS + NUM_CHANNELS) {
- channel = NULL;
- return false;
- }
- }
- }
- int index = channel - IMXRT_PIT_CHANNELS;
- funct_table[index] = funct;
- channel->LDVAL = cycles;
- channel->TCTRL = 3;
- nvic_priorites[index] = nvic_priority;
- uint8_t top_priority = 255;
- for (int i=0; i < NUM_CHANNELS; i++) {
- if (top_priority > nvic_priorites[i]) top_priority = nvic_priorites[i];
- }
- attachInterruptVector(IRQ_PIT, &pit_isr);
- NVIC_SET_PRIORITY(IRQ_PIT, top_priority);
- NVIC_ENABLE_IRQ(IRQ_PIT);
- return true;
- }
-
-
- void IntervalTimer::end() {
- #if 1
- if (channel) {
- int index = channel - IMXRT_PIT_CHANNELS;
-
- funct_table[index] = nullptr;
- channel->TCTRL = 0;
- nvic_priorites[index] = 255;
- uint8_t top_priority = 255;
- for (int i=0; i < NUM_CHANNELS; i++) {
- if (top_priority > nvic_priorites[i]) top_priority = nvic_priorites[i];
- }
- NVIC_SET_PRIORITY(IRQ_PIT, top_priority);
- channel = 0;
- }
- #endif
- }
-
-
- static void pit_isr()
- {
- #if 0
- for (int i=0; i < NUM_CHANNELS; i++) {
- IMXRT_PIT_CHANNEL_t *channel = IMXRT_PIT_CHANNELS + i;
- if (funct_table[0] && channel->TFLG) {
- channel->TFLG = 1;
- funct_table[i]();
-
- }
- }
- #else
- IMXRT_PIT_CHANNEL_t *channel= IMXRT_PIT_CHANNELS;
- if (funct_table[0] != nullptr && channel->TFLG) {channel->TFLG = 1;funct_table[0]();}
- channel++;
- if (funct_table[1] != nullptr && channel->TFLG) {channel->TFLG = 1;funct_table[1]();}
- channel++;
- if (funct_table[2] != nullptr && channel->TFLG) {channel->TFLG = 1;funct_table[2]();}
- channel++;
- if (funct_table[3] != nullptr && channel->TFLG) {channel->TFLG = 1;funct_table[3]();}
- #endif
- }
|