|
2 | 2 | #include <register.h>
|
3 | 3 | #include <zpuino.h>
|
4 | 4 |
|
5 |
| -void TimerClass::begin() |
6 |
| -{ |
7 |
| - TMR1CTL = BIT(TCTLENA)|BIT(TCTLCCM)|BIT(TCTLDIR); |
8 |
| -} |
9 |
| -/* |
10 |
| -void TimerClass::setPWMFrequency(long int hz) |
11 |
| -{ |
12 |
| - const int dividers[8] = { |
13 |
| - 1,2,4,8,16,64,256,1024 |
14 |
| - }; |
15 |
| -
|
16 |
| - int i; |
17 |
| -
|
18 |
| - for (i=0;i<8;i++) { |
19 |
| - long count = CLK_FREQ/dividers[i]; |
20 |
| - count /= hz; |
21 |
| - if (count<65535) { |
22 |
| - TMR1CNT=0; |
23 |
| - TMR1CMP=count; |
24 |
| - TMR1OCR=count>>1; |
25 |
| - TMR1CTL &= ~(BIT(TCTLCP0)|BIT(TCTLCP1)|BIT(TCTLCP2)); |
26 |
| - TMR1CTL |= (i<<TCTLCP0); |
27 |
| - return; |
28 |
| - } |
29 |
| - } |
30 |
| -} |
31 |
| -*/ |
32 |
| - |
33 |
| -void TimerClass::setPWMDuty(uint8_t val) |
34 |
| -{ |
35 |
| - long cmp = count; |
36 |
| - cmp *= val; |
37 |
| - cmp >>= 8; |
38 |
| -// TMR1OCR=cmp; |
39 |
| -} |
40 |
| - |
41 |
| -void TimerClass::setPWMFrequency(long int hz) |
42 |
| -{ |
43 |
| - count = (CLK_FREQ / hz) -1 ; |
44 |
| - TMR1CNT=0; |
45 |
| - TMR1CMP=count; |
46 |
| - // TMR1OCR=count>>1; |
47 |
| - TMR1CTL &= ~(BIT(TCTLCP0)|BIT(TCTLCP1)|BIT(TCTLCP2)); |
48 |
| - //TMR1CTL |= (i<<TCTLCP0); |
49 |
| -} |
50 |
| - |
51 |
| -void TimerClass::setPWMOutputPin(int pin) |
52 |
| -{ |
53 |
| - pinMode(pin,OUTPUT); |
54 |
| - pinModePPS(pin,HIGH); |
55 |
| - outputPinForFunction( pin, IOPIN_TIMER1_OC); |
56 |
| -} |
| 5 | +#define VENDOR_ZPUINO 0x08 |
| 6 | +#define PRODUCT_ZPUINO_TIMERS 0x13 |
| 7 | + |
| 8 | +namespace ZPUino { |
| 9 | + |
| 10 | + Timers_class::Timers_class(): m_pwmTimerPtr(0), m_intrTimerPtr(0) |
| 11 | + { |
| 12 | + } |
| 13 | + |
| 14 | + void Timers_class::begin() |
| 15 | + { |
| 16 | + if (deviceBegin(VENDOR_ZPUINO, PRODUCT_ZPUINO_TIMERS)==0) { |
| 17 | + m_timer0.begin( getBaseRegister(), getSlot() ); |
| 18 | + m_timer1.begin( getBaseRegister() + 64, getSlot()+1); |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + int TimerInstance_class::setPeriodMilliseconds(unsigned ms) { |
| 23 | + unsigned maxvalue = (1<<getSize())-1; |
| 24 | + unsigned cmp=0; |
| 25 | + |
| 26 | + if (hasPrescaler()) { |
| 27 | + int i; |
| 28 | + for (i=0;i<8;i++) { |
| 29 | + unsigned long count = CLK_FREQ/timerPrescalerDividers[i]; /* This is one second */ |
| 30 | + cmp = count / ms; |
| 31 | + if (cmp<maxvalue) |
| 32 | + break; |
| 33 | + } |
| 34 | + } else { |
| 35 | + unsigned long count = CLK_FREQ; /* This is one second */ |
| 36 | + cmp = count/ms; |
| 37 | + if (cmp>maxvalue) |
| 38 | + cmp=0; |
| 39 | + } |
| 40 | + if (cmp==0) |
| 41 | + return -1; |
| 42 | + |
| 43 | + /* Apply */ |
| 44 | + setComparator(cmp); |
| 45 | + setCounter(0); |
| 46 | + return 0; |
| 47 | + } |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | + /* |
| 53 | + void TimerClass::setPWMFrequency(long int hz) |
| 54 | + { |
| 55 | + const int dividers[8] = { |
| 56 | + 1,2,4,8,16,64,256,1024 |
| 57 | + }; |
| 58 | +
|
| 59 | + int i; |
| 60 | +
|
| 61 | + for (i=0;i<8;i++) { |
| 62 | + long count = CLK_FREQ/dividers[i]; |
| 63 | + count /= hz; |
| 64 | + if (count<65535) { |
| 65 | + TMR1CNT=0; |
| 66 | + TMR1CMP=count; |
| 67 | + TMR1OCR=count>>1; |
| 68 | + TMR1CTL &= ~(BIT(TCTLCP0)|BIT(TCTLCP1)|BIT(TCTLCP2)); |
| 69 | + TMR1CTL |= (i<<TCTLCP0); |
| 70 | + return; |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + */ |
| 75 | +#if 0 |
| 76 | + void TimerClass::setPWMDuty(uint8_t val) |
| 77 | + { |
| 78 | + long cmp = count; |
| 79 | + cmp *= val; |
| 80 | + cmp >>= 8; |
| 81 | + // TMR1OCR=cmp; |
| 82 | + } |
| 83 | + |
| 84 | + void TimerClass::setPWMFrequency(long int hz) |
| 85 | + { |
| 86 | + count = (CLK_FREQ / hz) -1 ; |
| 87 | + TMR1CNT=0; |
| 88 | + TMR1CMP=count; |
| 89 | + // TMR1OCR=count>>1; |
| 90 | + TMR1CTL &= ~(BIT(TCTLCP0)|BIT(TCTLCP1)|BIT(TCTLCP2)); |
| 91 | + //TMR1CTL |= (i<<TCTLCP0); |
| 92 | + } |
| 93 | + |
| 94 | + void TimerClass::setPWMOutputPin(int pin) |
| 95 | + { |
| 96 | + pinMode(pin,OUTPUT); |
| 97 | + pinModePPS(pin,HIGH); |
| 98 | + outputPinForFunction( pin, IOPIN_TIMER1_OC); |
| 99 | + } |
| 100 | +#endif |
| 101 | + |
| 102 | + int Timers_class::singleShot( int msec, void (*function)(void*), void *arg ) |
| 103 | + { |
| 104 | + /* get a free timer */ |
| 105 | + TimerInstance_class *tmr = getFreeTimer(); |
| 106 | + |
| 107 | + if (0==tmr) |
| 108 | + return -1; |
| 109 | + |
| 110 | + m_intrTimerPtr = tmr; |
| 111 | + |
| 112 | + return attachInterrupt(tmr->getInterruptLine(), &timerInterruptHandlerSS, (void*)this); |
| 113 | + } |
| 114 | + |
| 115 | + int Timers_class::singleShot( int msec, void (*function)(void)) |
| 116 | + { |
| 117 | + return singleShot(msec, (void(*)(void*))function, 0); |
| 118 | + } |
| 119 | + |
| 120 | + int Timers_class::periodic( int msec, bool (*function)(void*), void *arg ) |
| 121 | + { |
| 122 | + TimerInstance_class *tmr = getFreeTimer(); |
| 123 | + |
| 124 | + if (0==tmr) |
| 125 | + return -1; |
| 126 | + |
| 127 | + m_intrTimerPtr = tmr; |
| 128 | + |
| 129 | + return attachInterrupt(tmr->getInterruptLine(), &timerInterruptHandler, (void*)this); |
| 130 | + } |
| 131 | + |
| 132 | + int Timers_class::periodic( int msec, bool (*function)(void) ) |
| 133 | + { |
| 134 | + return periodic(msec, (bool(*)(void*))function, 0); |
| 135 | + } |
| 136 | + |
| 137 | + void Timers_class::timerInterruptHandler(void *arg) |
| 138 | + { |
| 139 | + Timers_class *hclass = static_cast<Timers_class*>(arg); |
| 140 | + if (!hclass->handleInterrupt()) { |
| 141 | + hclass->m_intrTimerPtr->stop(); |
| 142 | + detachInterrupt(hclass->m_intrTimerPtr->getInterruptLine()); |
| 143 | + hclass->m_intrTimerPtr=0; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + void Timers_class::timerInterruptHandlerSS(void *arg) |
| 148 | + { |
| 149 | + Timers_class *hclass = static_cast<Timers_class*>(arg); |
| 150 | + hclass->handleInterrupt(); |
| 151 | + hclass->m_intrTimerPtr->stop(); |
| 152 | + detachInterrupt(hclass->m_intrTimerPtr->getInterruptLine()); |
| 153 | + hclass->m_intrTimerPtr=0; |
| 154 | + } |
| 155 | + |
| 156 | +}; |
0 commit comments