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
);