No more FM1216-137, or maybe still...
[RRG-proxmark3.git] / common_arm / ticks.c
blob10c3dcb723727f57c8f94283fa0deda6dc104da9
1 //-----------------------------------------------------------------------------
2 // Copyright (C) Jonathan Westhues, Sept 2005
3 // Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
4 //
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // See LICENSE.txt for the text of the license.
16 //-----------------------------------------------------------------------------
17 // Timers, Clocks functions used in LF or Legic where you would need detailed time.
18 //-----------------------------------------------------------------------------
19 #include "ticks.h"
21 #include "proxmark3_arm.h"
22 #ifndef AS_BOOTROM
23 #include "dbprint.h"
24 #endif
27 #ifndef AS_BOOTROM
29 // timer counts in 666ns increments (32/48MHz), rounding applies
30 // WARNING: timer can't measure more than 43ms (666ns * 0xFFFF)
31 void SpinDelayUsPrecision(int us) {
32 int ticks = ((MCK / 1000000) * us + 16) >> 5;
34 // Borrow a PWM unit for my real-time clock
35 AT91C_BASE_PWMC->PWMC_ENA = PWM_CHANNEL(0);
37 // 48 MHz / 32 gives 1.5 Mhz
38 AT91C_BASE_PWMC_CH0->PWMC_CMR = PWM_CH_MODE_PRESCALER(5); // Channel Mode Register
39 AT91C_BASE_PWMC_CH0->PWMC_CDTYR = 0; // Channel Duty Cycle Register
40 AT91C_BASE_PWMC_CH0->PWMC_CPRDR = 0xFFFF; // Channel Period Register
42 uint16_t end = AT91C_BASE_PWMC_CH0->PWMC_CCNTR + ticks;
43 if (end == 0) // AT91C_BASE_PWMC_CH0->PWMC_CCNTR is never == 0
44 end++; // so we have to end++ to avoid inivity loop
46 for (;;) {
47 uint16_t now = AT91C_BASE_PWMC_CH0->PWMC_CCNTR;
49 if (now == end)
50 return;
52 WDT_HIT();
56 // timer counts in 21.3us increments (1024/48MHz), rounding applies
57 // WARNING: timer can't measure more than 1.39s (21.3us * 0xffff)
58 void SpinDelayUs(int us) {
59 int ticks = ((MCK / 1000000) * us + 512) >> 10;
61 // Borrow a PWM unit for my real-time clock
62 AT91C_BASE_PWMC->PWMC_ENA = PWM_CHANNEL(0);
64 // 48 MHz / 1024 gives 46.875 kHz
65 AT91C_BASE_PWMC_CH0->PWMC_CMR = PWM_CH_MODE_PRESCALER(10); // Channel Mode Register
66 AT91C_BASE_PWMC_CH0->PWMC_CDTYR = 0; // Channel Duty Cycle Register
67 AT91C_BASE_PWMC_CH0->PWMC_CPRDR = 0xffff; // Channel Period Register
69 uint16_t end = AT91C_BASE_PWMC_CH0->PWMC_CCNTR + ticks;
70 if (end == 0) // AT91C_BASE_PWMC_CH0->PWMC_CCNTR is never == 0
71 end++; // so we have to end++ to avoid inivity loop
73 for (;;) {
74 uint16_t now = AT91C_BASE_PWMC_CH0->PWMC_CCNTR;
76 if (now == end)
77 return;
78 WDT_HIT();
82 // WARNING: timer can't measure more than 1.39s (21.3us * 0xffff)
83 void SpinDelay(int ms) {
84 if (ms > 1390) {
85 if (g_dbglevel >= DBG_ERROR) Dbprintf(_RED_("Error, SpinDelay called with %i > 1390"), ms);
86 ms = 1390;
88 // convert to us and call microsecond delay function
89 SpinDelayUs(ms * 1000);
91 // -------------------------------------------------------------------------
92 // timer lib
93 // -------------------------------------------------------------------------
94 // test procedure:
96 // ti = GetTickCount();
97 // SpinDelay(1000);
98 // ti = GetTickCount() - ti;
99 // Dbprintf("timer(1s): %d t=%d", ti, GetTickCount());
100 void StartTickCount(void) {
101 // This timer is based on the slow clock. The slow clock frequency is between 22kHz and 40kHz.
102 // We can determine the actual slow clock frequency by looking at the Main Clock Frequency Register.
103 while ((AT91C_BASE_PMC->PMC_MCFR & AT91C_CKGR_MAINRDY) == 0); // Wait for MAINF value to become available...
104 uint16_t mainf = AT91C_BASE_PMC->PMC_MCFR & AT91C_CKGR_MAINF; // Get # main clocks within 16 slow clocks
105 // set RealTimeCounter divider to count at 1kHz, should be 32 if RC is exactly at 32kHz:
106 AT91C_BASE_RTTC->RTTC_RTMR = AT91C_RTTC_RTTRST | ((((MAINCK / 1000 * 16) + (mainf / 2)) / mainf) & AT91C_RTTC_RTPRES);
107 // note: worst case precision is approx 2.5%
111 * Get the current count.
113 uint32_t RAMFUNC GetTickCount(void) {
114 return AT91C_BASE_RTTC->RTTC_RTVR;
117 uint32_t RAMFUNC GetTickCountDelta(uint32_t start_ticks) {
118 uint32_t stop_ticks = AT91C_BASE_RTTC->RTTC_RTVR;
119 if (stop_ticks >= start_ticks)
120 return stop_ticks - start_ticks;
121 return (UINT32_MAX - start_ticks) + stop_ticks;
124 // -------------------------------------------------------------------------
125 // Timer for iso14443 commands. Uses ssp_clk from FPGA
126 // -------------------------------------------------------------------------
127 void StartCountSspClk(void) {
128 AT91C_BASE_PMC->PMC_PCER |= (1 << AT91C_ID_TC0) | (1 << AT91C_ID_TC1) | (1 << AT91C_ID_TC2); // Enable Clock to all timers
129 AT91C_BASE_TCB->TCB_BMR = AT91C_TCB_TC0XC0S_TIOA1 // XC0 Clock = TIOA1
130 | AT91C_TCB_TC1XC1S_NONE // XC1 Clock = none
131 | AT91C_TCB_TC2XC2S_TIOA0; // XC2 Clock = TIOA0
133 // configure TC1 to create a short pulse on TIOA1 when a rising edge on TIOB1 (= ssp_clk from FPGA) occurs:
134 AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKDIS; // disable TC1
135 AT91C_BASE_TC1->TC_CMR = AT91C_TC_CLKS_TIMER_DIV1_CLOCK // TC1 Clock = MCK(48MHz)/2 = 24MHz
136 | AT91C_TC_CPCSTOP // Stop clock on RC compare
137 | AT91C_TC_EEVTEDG_RISING // Trigger on rising edge of Event
138 | AT91C_TC_EEVT_TIOB // Event-Source: TIOB1 (= ssp_clk from FPGA = 13,56MHz/16)
139 | AT91C_TC_ENETRG // Enable external trigger event
140 | AT91C_TC_WAVESEL_UP // Upmode without automatic trigger on RC compare
141 | AT91C_TC_WAVE // Waveform Mode
142 | AT91C_TC_AEEVT_SET // Set TIOA1 on external event
143 | AT91C_TC_ACPC_CLEAR; // Clear TIOA1 on RC Compare
144 AT91C_BASE_TC1->TC_RC = 0x01; // RC Compare value = 0x01, pulse width to TC0
146 // use TC0 to count TIOA1 pulses
147 AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS; // disable TC0
148 AT91C_BASE_TC0->TC_CMR = AT91C_TC_CLKS_XC0 // TC0 clock = XC0 clock = TIOA1
149 | AT91C_TC_WAVE // Waveform Mode
150 | AT91C_TC_WAVESEL_UP // just count
151 | AT91C_TC_ACPA_CLEAR // Clear TIOA0 on RA Compare
152 | AT91C_TC_ACPC_SET // Set TIOA0 on RC Compare
153 | AT91C_TC_ASWTRG_SET; // Set TIOA0 on software trigger to trigger instant reset of TC2
154 AT91C_BASE_TC0->TC_RA = 1; // RA Compare value = 1; pulse width to TC2
155 AT91C_BASE_TC0->TC_RC = 0; // RC Compare value = 0; increment TC2 on overflow
157 // use TC2 to count TIOA0 pulses (giving us a 32bit counter (TC0/TC2) clocked by ssp_clk)
158 AT91C_BASE_TC2->TC_CCR = AT91C_TC_CLKDIS; // disable TC2
159 AT91C_BASE_TC2->TC_CMR = AT91C_TC_CLKS_XC2 // TC2 clock = XC2 clock = TIOA0
160 | AT91C_TC_WAVE // Waveform Mode
161 | AT91C_TC_WAVESEL_UP; // just count
163 AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG; // enable and reset TC0
164 AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG; // enable and reset TC1
165 AT91C_BASE_TC2->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG; // enable and reset TC2
168 // synchronize the counter with the ssp_frame signal.
169 // Note: FPGA must be in a FPGA mode with SSC transfer, otherwise SSC_FRAME and SSC_CLK signals would not be present
171 while (AT91C_BASE_PIOA->PIO_PDSR & GPIO_SSC_FRAME); // wait for ssp_frame to be low
172 while (!(AT91C_BASE_PIOA->PIO_PDSR & GPIO_SSC_FRAME)); // wait for ssp_frame to go high (start of frame)
173 while (!(AT91C_BASE_PIOA->PIO_PDSR & GPIO_SSC_CLK)); // wait for ssp_clk to go high; 1st ssp_clk after start of frame
174 while (AT91C_BASE_PIOA->PIO_PDSR & GPIO_SSC_CLK); // wait for ssp_clk to go low;
175 while (!(AT91C_BASE_PIOA->PIO_PDSR & GPIO_SSC_CLK)); // wait for ssp_clk to go high; 2nd ssp_clk after start of frame
176 if ((AT91C_BASE_SSC->SSC_RFMR & SSC_FRAME_MODE_BITS_IN_WORD(32)) == SSC_FRAME_MODE_BITS_IN_WORD(16)) { // 16bit frame
177 while (AT91C_BASE_PIOA->PIO_PDSR & GPIO_SSC_CLK); // wait for ssp_clk to go low;
178 while (!(AT91C_BASE_PIOA->PIO_PDSR & GPIO_SSC_CLK)); // wait for ssp_clk to go high; 3rd ssp_clk after start of frame
179 while (AT91C_BASE_PIOA->PIO_PDSR & GPIO_SSC_CLK); // wait for ssp_clk to go low;
180 while (!(AT91C_BASE_PIOA->PIO_PDSR & GPIO_SSC_CLK)); // wait for ssp_clk to go high; 4th ssp_clk after start of frame
181 while (AT91C_BASE_PIOA->PIO_PDSR & GPIO_SSC_CLK); // wait for ssp_clk to go low;
182 while (!(AT91C_BASE_PIOA->PIO_PDSR & GPIO_SSC_CLK)); // wait for ssp_clk to go high; 5th ssp_clk after start of frame
183 while (AT91C_BASE_PIOA->PIO_PDSR & GPIO_SSC_CLK); // wait for ssp_clk to go low;
184 while (!(AT91C_BASE_PIOA->PIO_PDSR & GPIO_SSC_CLK)); // wait for ssp_clk to go high; 6th ssp_clk after start of frame
187 // note: up to now two ssp_clk rising edges have passed since the rising edge of ssp_frame
188 // it is now safe to assert a sync signal. This sets all timers to 0 on next active clock edge
189 AT91C_BASE_TCB->TCB_BCR = 1; // assert Sync (set all timers to 0 on next active clock edge)
190 // at the next (3rd) ssp_clk rising edge, TC1 will be reset (and not generate a clock signal to TC0)
191 // at the next (4th) ssp_clk rising edge, TC0 (the low word of our counter) will be reset. From now on,
192 // whenever the last three bits of our counter go 0, we can be sure to be in the middle of a frame transfer.
193 // (just started with the transfer of the 4th Bit).
195 // The high word of the counter (TC2) will not reset until the low word (TC0) clocks to process the external trigger.
196 // Therefore may need to wait a little bit before we can use the counter.
197 while (AT91C_BASE_TC2->TC_CV > 0);
199 void ResetSspClk(void) {
200 //enable clock of timer and software trigger
201 AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG;
202 AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG;
203 AT91C_BASE_TC2->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG;
204 while (AT91C_BASE_TC2->TC_CV > 0);
207 uint32_t RAMFUNC GetCountSspClk(void) {
208 uint32_t tmp_count = (AT91C_BASE_TC2->TC_CV << 16) | AT91C_BASE_TC0->TC_CV;
210 // small chance that we may have missed an increment in TC2
211 if ((tmp_count & 0x0000ffff) == 0) {
212 return (AT91C_BASE_TC2->TC_CV << 16);
214 return tmp_count;
217 uint32_t RAMFUNC GetCountSspClkDelta(uint32_t start) {
218 uint32_t stop = GetCountSspClk();
219 if (stop >= start) {
220 return stop - start;
222 return (UINT32_MAX - start) + stop;
225 void WaitMS(uint32_t ms) {
226 WaitTicks((ms & 0x1FFFFF) * 1500);
229 #endif // #ifndef AS_BOOTROM
231 // -------------------------------------------------------------------------
232 // microseconds timer
233 // -------------------------------------------------------------------------
234 void StartCountUS(void) {
235 AT91C_BASE_PMC->PMC_PCER |= (1 << AT91C_ID_TC0) | (1 << AT91C_ID_TC1);
236 AT91C_BASE_TCB->TCB_BMR = AT91C_TCB_TC0XC0S_NONE | AT91C_TCB_TC1XC1S_TIOA0 | AT91C_TCB_TC2XC2S_NONE;
238 // fast clock
239 // tick=1.5mks
240 AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS; // timer disable
241 AT91C_BASE_TC0->TC_CMR = AT91C_TC_CLKS_TIMER_DIV3_CLOCK | // MCK(48MHz) / 32
242 AT91C_TC_WAVE | AT91C_TC_WAVESEL_UP_AUTO | AT91C_TC_ACPA_CLEAR |
243 AT91C_TC_ACPC_SET | AT91C_TC_ASWTRG_SET;
244 AT91C_BASE_TC0->TC_RA = 1;
245 AT91C_BASE_TC0->TC_RC = 0xBFFF + 1; // 0xC000
247 AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKDIS; // timer disable
248 AT91C_BASE_TC1->TC_CMR = AT91C_TC_CLKS_XC1; // from timer 0
250 AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG;
251 AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG;
253 // Assert a sync signal. This sets all timers to 0 on next active clock edge
254 AT91C_BASE_TCB->TCB_BCR = 1;
256 while (AT91C_BASE_TC1->TC_CV > 0);
259 uint32_t RAMFUNC GetCountUS(void) {
260 //return (AT91C_BASE_TC1->TC_CV * 0x8000) + ((AT91C_BASE_TC0->TC_CV / 15) * 10);
261 // By suggestion from PwPiwi, http://www.proxmark.org/forum/viewtopic.php?pid=17548#p17548
262 return ((uint32_t)AT91C_BASE_TC1->TC_CV) * 0x8000 + (((uint32_t)AT91C_BASE_TC0->TC_CV) * 2) / 3;
266 // -------------------------------------------------------------------------
267 // Timer for bitbanging, or LF stuff when you need a very precis timer
268 // 1us = 1.5ticks
269 // -------------------------------------------------------------------------
270 void StartTicks(void) {
271 // initialization of the timer
272 AT91C_BASE_PMC->PMC_PCER |= (1 << AT91C_ID_TC0) | (1 << AT91C_ID_TC1);
273 AT91C_BASE_TCB->TCB_BMR = AT91C_TCB_TC0XC0S_NONE | AT91C_TCB_TC1XC1S_TIOA0 | AT91C_TCB_TC2XC2S_NONE;
275 // disable TC0 and TC1 for re-configuration
276 AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS;
277 AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKDIS;
279 // first configure TC1 (higher, 0xFFFF0000) 16 bit counter
280 AT91C_BASE_TC1->TC_CMR = AT91C_TC_CLKS_XC1; // just connect to TIOA0 from TC0
281 AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG; // re-enable timer and wait for TC0
283 // second configure TC0 (lower, 0x0000FFFF) 16 bit counter
284 AT91C_BASE_TC0->TC_CMR = AT91C_TC_CLKS_TIMER_DIV3_CLOCK | // MCK(48MHz) / 32
285 AT91C_TC_WAVE | AT91C_TC_WAVESEL_UP_AUTO |
286 AT91C_TC_ACPA_CLEAR | // RA comperator clears TIOA (carry bit)
287 AT91C_TC_ACPC_SET | // RC comperator sets TIOA (carry bit)
288 AT91C_TC_ASWTRG_SET; // SWTriger sets TIOA (carry bit)
289 AT91C_BASE_TC0->TC_RC = 0; // set TIOA (carry bit) on overflow, return to zero
290 AT91C_BASE_TC0->TC_RA = 1; // clear carry bit on next clock cycle
291 AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG; // reset and re-enable timer
293 // synchronized startup procedure
294 while (AT91C_BASE_TC0->TC_CV > 0); // wait until TC0 returned to zero
295 while (AT91C_BASE_TC0->TC_CV < 2); // and has started (TC_CV > TC_RA, now TC1 is cleared)
297 // return to zero
298 AT91C_BASE_TC1->TC_CCR = AT91C_TC_SWTRG;
299 AT91C_BASE_TC0->TC_CCR = AT91C_TC_SWTRG;
300 while (AT91C_BASE_TC0->TC_CV > 0);
302 uint32_t GetTicks(void) {
303 uint32_t hi, lo;
305 do {
306 hi = AT91C_BASE_TC1->TC_CV;
307 lo = AT91C_BASE_TC0->TC_CV;
308 } while (hi != AT91C_BASE_TC1->TC_CV);
310 return (hi << 16) | lo;
313 uint32_t RAMFUNC GetTicksDelta(uint32_t start) {
314 uint32_t stop = GetTicks();
315 if (stop >= start) {
316 return stop - start;
318 return (UINT32_MAX - start) + stop;
321 // Wait - Spindelay in ticks.
322 // if called with a high number, this will trigger the WDT...
323 void WaitTicks(uint32_t ticks) {
324 if (ticks == 0) return;
325 ticks += GetTicks();
326 while (GetTicks() < ticks);
329 // Wait / Spindelay in us (microseconds)
330 // 1us = 1.5ticks.
331 void WaitUS(uint32_t us) {
332 WaitTicks((us & 0x3FFFFFFF) * 3 / 2);
335 // stop clock
336 void StopTicks(void) {
337 AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS;
338 AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKDIS;