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 uint32_t SwapBits(uint32_t value
, int nrbits
) {
17 uint32_t newvalue
= 0;
18 for(i
= 0; i
< nrbits
; i
++) {
19 newvalue
^= ((value
>> i
) & 1) << (nrbits
- 1 - i
);
24 void num_to_bytes(uint64_t n
, size_t len
, uint8_t* dest
)
27 dest
[len
] = (uint8_t) n
;
32 uint64_t bytes_to_num(uint8_t* src
, size_t len
)
37 num
= (num
<< 8) | (*src
);
51 // LEDs: R(C) O(A) G(B) -- R(D) [1, 2, 4 and 8]
52 void LED(int led
, int ms
)
79 // Determine if a button is double clicked, single clicked,
80 // not clicked, or held down (for ms || 1sec)
81 // In general, don't use this function unless you expect a
82 // double click, otherwise it will waste 500ms -- use BUTTON_HELD instead
83 int BUTTON_CLICKED(int ms
)
85 // Up to 500ms in between clicks to mean a double click
86 int ticks
= (48000 * (ms
? ms
: 1000)) >> 10;
88 // If we're not even pressed, forget about it!
90 return BUTTON_NO_CLICK
;
92 // Borrow a PWM unit for my real-time clock
93 AT91C_BASE_PWMC
->PWMC_ENA
= PWM_CHANNEL(0);
94 // 48 MHz / 1024 gives 46.875 kHz
95 AT91C_BASE_PWMC_CH0
->PWMC_CMR
= PWM_CH_MODE_PRESCALER(10);
96 AT91C_BASE_PWMC_CH0
->PWMC_CDTYR
= 0;
97 AT91C_BASE_PWMC_CH0
->PWMC_CPRDR
= 0xffff;
99 uint16_t start
= AT91C_BASE_PWMC_CH0
->PWMC_CCNTR
;
104 uint16_t now
= AT91C_BASE_PWMC_CH0
->PWMC_CCNTR
;
106 // We haven't let off the button yet
109 // We just let it off!
114 // reset our timer for 500ms
115 start
= AT91C_BASE_PWMC_CH0
->PWMC_CCNTR
;
116 ticks
= (48000 * (500)) >> 10;
119 // Still haven't let it off
121 // Have we held down a full second?
122 if (now
== (uint16_t)(start
+ ticks
))
126 // We already let off, did we click again?
128 // Sweet, double click!
130 return BUTTON_DOUBLE_CLICK
;
132 // Have we ran out of time to double click?
134 if (now
== (uint16_t)(start
+ ticks
))
135 // At least we did a single click
136 return BUTTON_SINGLE_CLICK
;
141 // We should never get here
145 // Determine if a button is held down
146 int BUTTON_HELD(int ms
)
148 // If button is held for one second
149 int ticks
= (48000 * (ms
? ms
: 1000)) >> 10;
151 // If we're not even pressed, forget about it!
153 return BUTTON_NO_CLICK
;
155 // Borrow a PWM unit for my real-time clock
156 AT91C_BASE_PWMC
->PWMC_ENA
= PWM_CHANNEL(0);
157 // 48 MHz / 1024 gives 46.875 kHz
158 AT91C_BASE_PWMC_CH0
->PWMC_CMR
= PWM_CH_MODE_PRESCALER(10);
159 AT91C_BASE_PWMC_CH0
->PWMC_CDTYR
= 0;
160 AT91C_BASE_PWMC_CH0
->PWMC_CPRDR
= 0xffff;
162 uint16_t start
= AT91C_BASE_PWMC_CH0
->PWMC_CCNTR
;
166 uint16_t now
= AT91C_BASE_PWMC_CH0
->PWMC_CCNTR
;
168 // As soon as our button let go, we didn't hold long enough
170 return BUTTON_SINGLE_CLICK
;
172 // Have we waited the full second?
174 if (now
== (uint16_t)(start
+ ticks
))
180 // We should never get here
184 // attempt at high resolution microsecond timer
185 // beware: timer counts in 21.3uS increments (1024/48Mhz)
186 void SpinDelayUs(int us
)
188 int ticks
= (48*us
) >> 10;
190 // Borrow a PWM unit for my real-time clock
191 AT91C_BASE_PWMC
->PWMC_ENA
= PWM_CHANNEL(0);
192 // 48 MHz / 1024 gives 46.875 kHz
193 AT91C_BASE_PWMC_CH0
->PWMC_CMR
= PWM_CH_MODE_PRESCALER(10);
194 AT91C_BASE_PWMC_CH0
->PWMC_CDTYR
= 0;
195 AT91C_BASE_PWMC_CH0
->PWMC_CPRDR
= 0xffff;
197 uint16_t start
= AT91C_BASE_PWMC_CH0
->PWMC_CCNTR
;
200 uint16_t now
= AT91C_BASE_PWMC_CH0
->PWMC_CCNTR
;
201 if (now
== (uint16_t)(start
+ ticks
))
208 void SpinDelay(int ms
)
210 // convert to uS and call microsecond delay function
211 SpinDelayUs(ms
*1000);
214 /* Similar to FpgaGatherVersion this formats stored version information
215 * into a string representation. It takes a pointer to the struct version_information,
216 * verifies the magic properties, then stores a formatted string, prefixed by
219 void FormatVersionInformation(char *dst
, int len
, const char *prefix
, void *version_information
)
221 struct version_information
*v
= (struct version_information
*)version_information
;
223 strncat(dst
, prefix
, len
);
224 if(v
->magic
!= VERSION_INFORMATION_MAGIC
) {
225 strncat(dst
, "Missing/Invalid version information", len
);
228 if(v
->versionversion
!= 1) {
229 strncat(dst
, "Version information not understood", len
);
233 strncat(dst
, "Version information not available", len
);
237 strncat(dst
, v
->svnversion
, len
);
239 strncat(dst
, "-unclean", len
);
240 } else if(v
->clean
== 2) {
241 strncat(dst
, "-suspect", len
);
244 strncat(dst
, " ", len
);
245 strncat(dst
, v
->buildtime
, len
);
248 // -------------------------------------------------------------------------
250 // -------------------------------------------------------------------------
253 // ti = GetTickCount();
255 // ti = GetTickCount() - ti;
256 // Dbprintf("timer(1s): %d t=%d", ti, GetTickCount());
258 void StartTickCount()
260 // must be 0x40, but on my cpu - included divider is optimal
264 AT91C_BASE_RTTC
->RTTC_RTMR
= AT91C_RTTC_RTTRST
+ 0x001D; // was 0x003B
268 * Get the current count.
270 uint32_t RAMFUNC
GetTickCount(){
271 return AT91C_BASE_RTTC
->RTTC_RTVR
;// was * 2;
274 // -------------------------------------------------------------------------
275 // microseconds timer
276 // -------------------------------------------------------------------------
279 AT91C_BASE_PMC
->PMC_PCER
|= (0x1 << 12) | (0x1 << 13) | (0x1 << 14);
280 // AT91C_BASE_TCB->TCB_BMR = AT91C_TCB_TC1XC1S_TIOA0;
281 AT91C_BASE_TCB
->TCB_BMR
= AT91C_TCB_TC0XC0S_NONE
| AT91C_TCB_TC1XC1S_TIOA0
| AT91C_TCB_TC2XC2S_NONE
;
284 AT91C_BASE_TC0
->TC_CCR
= AT91C_TC_CLKDIS
; // timer disable
285 AT91C_BASE_TC0
->TC_CMR
= AT91C_TC_CLKS_TIMER_DIV3_CLOCK
| // MCK(48MHz)/32 -- tick=1.5mks
286 AT91C_TC_WAVE
| AT91C_TC_WAVESEL_UP_AUTO
| AT91C_TC_ACPA_CLEAR
|
287 AT91C_TC_ACPC_SET
| AT91C_TC_ASWTRG_SET
;
288 AT91C_BASE_TC0
->TC_RA
= 1;
289 AT91C_BASE_TC0
->TC_RC
= 0xBFFF + 1; // 0xC000
291 AT91C_BASE_TC1
->TC_CCR
= AT91C_TC_CLKDIS
; // timer disable
292 AT91C_BASE_TC1
->TC_CMR
= AT91C_TC_CLKS_XC1
; // from timer 0
294 AT91C_BASE_TC0
->TC_CCR
= AT91C_TC_CLKEN
;
295 AT91C_BASE_TC1
->TC_CCR
= AT91C_TC_CLKEN
;
296 AT91C_BASE_TCB
->TCB_BCR
= 1;
299 uint32_t RAMFUNC
GetCountUS(){
300 return (AT91C_BASE_TC1
->TC_CV
* 0x8000) + ((AT91C_BASE_TC0
->TC_CV
/ 15) * 10);
303 static uint32_t GlobalUsCounter
= 0;
305 uint32_t RAMFUNC
GetDeltaCountUS(){
306 uint32_t g_cnt
= GetCountUS();
307 uint32_t g_res
= g_cnt
- GlobalUsCounter
;
308 GlobalUsCounter
= g_cnt
;