1 //-----------------------------------------------------------------------------
2 // Jonathan Westhues, Sept 2005
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
8 // Utility functions used in many places, not specific to any piece of code.
9 //-----------------------------------------------------------------------------
11 #include "proxmark3.h"
15 void num_to_bytes(uint64_t n
, size_t len
, uint8_t* dest
)
18 dest
[len
] = (uint8_t) n
;
23 uint64_t bytes_to_num(uint8_t* src
, size_t len
)
28 num
= (num
<< 8) | (*src
);
42 // LEDs: R(C) O(A) G(B) -- R(D) [1, 2, 4 and 8]
43 void LED(int led
, int ms
)
70 // Determine if a button is double clicked, single clicked,
71 // not clicked, or held down (for ms || 1sec)
72 // In general, don't use this function unless you expect a
73 // double click, otherwise it will waste 500ms -- use BUTTON_HELD instead
74 int BUTTON_CLICKED(int ms
)
76 // Up to 500ms in between clicks to mean a double click
77 int ticks
= (48000 * (ms
? ms
: 1000)) >> 10;
79 // If we're not even pressed, forget about it!
81 return BUTTON_NO_CLICK
;
83 // Borrow a PWM unit for my real-time clock
84 AT91C_BASE_PWMC
->PWMC_ENA
= PWM_CHANNEL(0);
85 // 48 MHz / 1024 gives 46.875 kHz
86 AT91C_BASE_PWMC_CH0
->PWMC_CMR
= PWM_CH_MODE_PRESCALER(10);
87 AT91C_BASE_PWMC_CH0
->PWMC_CDTYR
= 0;
88 AT91C_BASE_PWMC_CH0
->PWMC_CPRDR
= 0xffff;
90 uint16_t start
= AT91C_BASE_PWMC_CH0
->PWMC_CCNTR
;
95 uint16_t now
= AT91C_BASE_PWMC_CH0
->PWMC_CCNTR
;
97 // We haven't let off the button yet
100 // We just let it off!
105 // reset our timer for 500ms
106 start
= AT91C_BASE_PWMC_CH0
->PWMC_CCNTR
;
107 ticks
= (48000 * (500)) >> 10;
110 // Still haven't let it off
112 // Have we held down a full second?
113 if (now
== (uint16_t)(start
+ ticks
))
117 // We already let off, did we click again?
119 // Sweet, double click!
121 return BUTTON_DOUBLE_CLICK
;
123 // Have we ran out of time to double click?
125 if (now
== (uint16_t)(start
+ ticks
))
126 // At least we did a single click
127 return BUTTON_SINGLE_CLICK
;
132 // We should never get here
136 // Determine if a button is held down
137 int BUTTON_HELD(int ms
)
139 // If button is held for one second
140 int ticks
= (48000 * (ms
? ms
: 1000)) >> 10;
142 // If we're not even pressed, forget about it!
144 return BUTTON_NO_CLICK
;
146 // Borrow a PWM unit for my real-time clock
147 AT91C_BASE_PWMC
->PWMC_ENA
= PWM_CHANNEL(0);
148 // 48 MHz / 1024 gives 46.875 kHz
149 AT91C_BASE_PWMC_CH0
->PWMC_CMR
= PWM_CH_MODE_PRESCALER(10);
150 AT91C_BASE_PWMC_CH0
->PWMC_CDTYR
= 0;
151 AT91C_BASE_PWMC_CH0
->PWMC_CPRDR
= 0xffff;
153 uint16_t start
= AT91C_BASE_PWMC_CH0
->PWMC_CCNTR
;
157 uint16_t now
= AT91C_BASE_PWMC_CH0
->PWMC_CCNTR
;
159 // As soon as our button let go, we didn't hold long enough
161 return BUTTON_SINGLE_CLICK
;
163 // Have we waited the full second?
165 if (now
== (uint16_t)(start
+ ticks
))
171 // We should never get here
175 // attempt at high resolution microsecond timer
176 // beware: timer counts in 21.3uS increments (1024/48Mhz)
177 void SpinDelayUs(int us
)
179 int ticks
= (48*us
) >> 10;
181 // Borrow a PWM unit for my real-time clock
182 AT91C_BASE_PWMC
->PWMC_ENA
= PWM_CHANNEL(0);
183 // 48 MHz / 1024 gives 46.875 kHz
184 AT91C_BASE_PWMC_CH0
->PWMC_CMR
= PWM_CH_MODE_PRESCALER(10);
185 AT91C_BASE_PWMC_CH0
->PWMC_CDTYR
= 0;
186 AT91C_BASE_PWMC_CH0
->PWMC_CPRDR
= 0xffff;
188 uint16_t start
= AT91C_BASE_PWMC_CH0
->PWMC_CCNTR
;
191 uint16_t now
= AT91C_BASE_PWMC_CH0
->PWMC_CCNTR
;
192 if (now
== (uint16_t)(start
+ ticks
))
199 void SpinDelay(int ms
)
201 // convert to uS and call microsecond delay function
202 SpinDelayUs(ms
*1000);
205 /* Similar to FpgaGatherVersion this formats stored version information
206 * into a string representation. It takes a pointer to the struct version_information,
207 * verifies the magic properties, then stores a formatted string, prefixed by
210 void FormatVersionInformation(char *dst
, int len
, const char *prefix
, void *version_information
)
212 struct version_information
*v
= (struct version_information
*)version_information
;
214 strncat(dst
, prefix
, len
);
215 if(v
->magic
!= VERSION_INFORMATION_MAGIC
) {
216 strncat(dst
, "Missing/Invalid version information", len
);
219 if(v
->versionversion
!= 1) {
220 strncat(dst
, "Version information not understood", len
);
224 strncat(dst
, "Version information not available", len
);
228 strncat(dst
, v
->svnversion
, len
);
230 strncat(dst
, "-unclean", len
);
231 } else if(v
->clean
== 2) {
232 strncat(dst
, "-suspect", len
);
235 strncat(dst
, " ", len
);
236 strncat(dst
, v
->buildtime
, len
);
239 // -------------------------------------------------------------------------
241 // -------------------------------------------------------------------------
244 // ti = GetTickCount();
246 // ti = GetTickCount() - ti;
247 // Dbprintf("timer(1s): %d t=%d", ti, GetTickCount());
249 void StartTickCount()
251 // must be 0x40, but on my cpu - included divider is optimal
255 AT91C_BASE_RTTC
->RTTC_RTMR
= AT91C_RTTC_RTTRST
+ 0x001D; // was 0x003B
259 * Get the current count.
261 uint32_t RAMFUNC
GetTickCount(){
262 return AT91C_BASE_RTTC
->RTTC_RTVR
;// was * 2;
265 // -------------------------------------------------------------------------
266 // microseconds timer
267 // -------------------------------------------------------------------------
270 AT91C_BASE_PMC
->PMC_PCER
|= (0x1 << 12) | (0x1 << 13) | (0x1 << 14);
271 // AT91C_BASE_TCB->TCB_BMR = AT91C_TCB_TC1XC1S_TIOA0;
272 AT91C_BASE_TCB
->TCB_BMR
= AT91C_TCB_TC0XC0S_NONE
| AT91C_TCB_TC1XC1S_TIOA0
| AT91C_TCB_TC2XC2S_NONE
;
275 AT91C_BASE_TC0
->TC_CCR
= AT91C_TC_CLKDIS
; // timer disable
276 AT91C_BASE_TC0
->TC_CMR
= AT91C_TC_CLKS_TIMER_DIV3_CLOCK
| // MCK(48MHz)/32 -- tick=1.5mks
277 AT91C_TC_WAVE
| AT91C_TC_WAVESEL_UP_AUTO
| AT91C_TC_ACPA_CLEAR
|
278 AT91C_TC_ACPC_SET
| AT91C_TC_ASWTRG_SET
;
279 AT91C_BASE_TC0
->TC_RA
= 1;
280 AT91C_BASE_TC0
->TC_RC
= 0xBFFF + 1; // 0xC000
282 AT91C_BASE_TC1
->TC_CCR
= AT91C_TC_CLKDIS
; // timer disable
283 AT91C_BASE_TC1
->TC_CMR
= AT91C_TC_CLKS_XC1
; // from timer 0
285 AT91C_BASE_TC0
->TC_CCR
= AT91C_TC_CLKEN
;
286 AT91C_BASE_TC1
->TC_CCR
= AT91C_TC_CLKEN
;
287 AT91C_BASE_TCB
->TCB_BCR
= 1;
290 uint32_t RAMFUNC
GetCountUS(){
291 return (AT91C_BASE_TC1
->TC_CV
* 0x8000) + ((AT91C_BASE_TC0
->TC_CV
/ 15) * 10);
294 static uint32_t GlobalUsCounter
= 0;
296 uint32_t RAMFUNC
GetDeltaCountUS(){
297 uint32_t g_cnt
= GetCountUS();
298 uint32_t g_res
= g_cnt
- GlobalUsCounter
;
299 GlobalUsCounter
= g_cnt
;