1 | #include "delay.h"
|
---|
2 | #include "Arduino.h"
|
---|
3 |
|
---|
4 | #ifdef __cplusplus
|
---|
5 | extern "C" {
|
---|
6 | #endif
|
---|
7 |
|
---|
8 | /** Tick Counter united by ms */
|
---|
9 | static volatile uint32_t _ulTickCount=0 ;
|
---|
10 |
|
---|
11 | uint32_t millis( void )
|
---|
12 | {
|
---|
13 | // todo: ensure no interrupts
|
---|
14 | return _ulTickCount ;
|
---|
15 | }
|
---|
16 |
|
---|
17 | // Interrupt-compatible version of micros
|
---|
18 | // Theory: repeatedly take readings of SysTick counter, millis counter and SysTick interrupt pending flag.
|
---|
19 | // When it appears that millis counter and pending is stable and SysTick hasn't rolled over, use these
|
---|
20 | // values to calculate micros. If there is a pending SysTick, add one to the millis counter in the calculation.
|
---|
21 | uint32_t micros( void )
|
---|
22 | {
|
---|
23 | uint32_t ticks, ticks2;
|
---|
24 | uint32_t pend, pend2;
|
---|
25 | uint32_t count, count2;
|
---|
26 |
|
---|
27 | ticks2 = SysTick->VAL;
|
---|
28 | pend2 = !!(SCB->ICSR & SCB_ICSR_PENDSTSET_Msk) ;
|
---|
29 | count2 = _ulTickCount ;
|
---|
30 |
|
---|
31 | do
|
---|
32 | {
|
---|
33 | ticks=ticks2;
|
---|
34 | pend=pend2;
|
---|
35 | count=count2;
|
---|
36 | ticks2 = SysTick->VAL;
|
---|
37 | pend2 = !!(SCB->ICSR & SCB_ICSR_PENDSTSET_Msk) ;
|
---|
38 | count2 = _ulTickCount ;
|
---|
39 | } while ((pend != pend2) || (count != count2) || (ticks < ticks2));
|
---|
40 |
|
---|
41 | return ((count+pend) * 1000) + (((SysTick->LOAD - ticks)*(1048576/(VARIANT_MCK/1000000)))>>20) ;
|
---|
42 | // this is an optimization to turn a runtime division into two compile-time divisions and
|
---|
43 | // a runtime multiplication and shift, saving a few cycles
|
---|
44 | }
|
---|
45 |
|
---|
46 | void delay( uint32_t ms )
|
---|
47 | {
|
---|
48 | if ( ms == 0 )
|
---|
49 | {
|
---|
50 | return ;
|
---|
51 | }
|
---|
52 |
|
---|
53 | uint32_t start = _ulTickCount ;
|
---|
54 |
|
---|
55 | do
|
---|
56 | {
|
---|
57 | yield() ;
|
---|
58 | } while ( _ulTickCount - start <= (ms-1) ) ;
|
---|
59 | }
|
---|
60 |
|
---|
61 | void SysTick_Handler( void )
|
---|
62 | {
|
---|
63 | // Increment tick count each ms
|
---|
64 | _ulTickCount++ ;
|
---|
65 | }
|
---|
66 |
|
---|
67 | void delayMicroseconds(uint32_t usec)
|
---|
68 | {
|
---|
69 | unsigned int presc=0, ref=0, sot=1;
|
---|
70 | double ref_val=0.0, div_val=0.0, usec_val=0.0;
|
---|
71 | unsigned long int i=0,limit=0;
|
---|
72 |
|
---|
73 | if (usec <=0) return;
|
---|
74 | usec_val=usec * 1.0;
|
---|
75 |
|
---|
76 | if(usec <= 20)
|
---|
77 | {
|
---|
78 |
|
---|
79 |
|
---|
80 | if(usec == 1) limit = usec * 2;
|
---|
81 | else if(usec == 2) limit = usec * 6;
|
---|
82 | else if(usec == 3) limit = usec * 8;
|
---|
83 | else if(usec == 4) limit = usec * 9;
|
---|
84 | else if(usec == 5) limit = usec * 9;
|
---|
85 | else limit = usec * 11;
|
---|
86 |
|
---|
87 | for(i=0; i <= limit; i++);
|
---|
88 | {
|
---|
89 | asm("NOP");
|
---|
90 | }
|
---|
91 | return;
|
---|
92 | }
|
---|
93 | else if(usec <=1363)
|
---|
94 | {
|
---|
95 | presc=TC_CTRLA_PRESCALER_DIV1;
|
---|
96 | div_val=1.0;
|
---|
97 | ref = (uint16_t)(usec_val * 48.0 / div_val);
|
---|
98 | if((usec > 20) & (usec <=60)) ref= ref - 905;
|
---|
99 | if(usec > 60) ref= ref - 921;
|
---|
100 | }
|
---|
101 |
|
---|
102 | else if((usec > 1363) & (usec <= 5461))
|
---|
103 | {
|
---|
104 | presc=TC_CTRLA_PRESCALER_DIV4;
|
---|
105 | div_val=4.0;
|
---|
106 | ref = (uint16_t)(usec_val * 48.0 / div_val);
|
---|
107 |
|
---|
108 | }
|
---|
109 | else if((usec > 5461) & (usec <= 10922))
|
---|
110 | {
|
---|
111 | presc=TC_CTRLA_PRESCALER_DIV8;
|
---|
112 | div_val=8.0;
|
---|
113 | ref = (uint16_t)(usec_val * 48.0 / div_val);
|
---|
114 | }
|
---|
115 | else if((usec > 10922) & (usec <= 21845))
|
---|
116 | {
|
---|
117 | presc=TC_CTRLA_PRESCALER_DIV16;
|
---|
118 | div_val=16.0;
|
---|
119 | ref = (uint16_t)(usec_val * 48.0 / div_val);
|
---|
120 | }
|
---|
121 | else if(usec > 21845)
|
---|
122 | {
|
---|
123 | presc=TC_CTRLA_PRESCALER_DIV64;
|
---|
124 | div_val=64.0;
|
---|
125 | ref = (uint16_t)(usec_val * 48.0 / div_val);
|
---|
126 | }
|
---|
127 | else;
|
---|
128 |
|
---|
129 |
|
---|
130 | GCLK->CLKCTRL.reg = (uint16_t) (GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK0 | GCLK_CLKCTRL_ID( GCM_TC4_TC5 ));
|
---|
131 |
|
---|
132 | TC4->COUNT16.CTRLA.reg &=~(TC_CTRLA_ENABLE);
|
---|
133 | TC4->COUNT16.CTRLA.reg |= TC_CTRLA_MODE_COUNT16 | presc;
|
---|
134 | TC4->COUNT16.READREQ.reg = 0x4002;
|
---|
135 | TC4->COUNT16.CTRLA.reg |= TC_CTRLA_ENABLE;
|
---|
136 |
|
---|
137 |
|
---|
138 | while(TC4->COUNT16.COUNT.reg < ref);
|
---|
139 |
|
---|
140 | return;
|
---|
141 | }
|
---|
142 |
|
---|
143 | #ifdef __cplusplus
|
---|
144 | }
|
---|
145 | #endif
|
---|