]>
Commit | Line | Data |
---|---|---|
e30c654b | 1 | //----------------------------------------------------------------------------- |
e30c654b | 2 | // Jonathan Westhues, Sept 2005 |
bd20f8f4 | 3 | // |
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 | |
6 | // the license. | |
7 | //----------------------------------------------------------------------------- | |
8 | // Utility functions used in many places, not specific to any piece of code. | |
e30c654b | 9 | //----------------------------------------------------------------------------- |
bd20f8f4 | 10 | |
e30c654b | 11 | #include "proxmark3.h" |
f7e3ed82 | 12 | #include "util.h" |
9ab7a6c7 | 13 | #include "string.h" |
e30c654b | 14 | |
195af472 | 15 | size_t nbytes(size_t nbits) { |
16 | return (nbits/8)+((nbits%8)>0); | |
17 | } | |
18 | ||
81cd0474 | 19 | uint32_t SwapBits(uint32_t value, int nrbits) { |
20 | int i; | |
21 | uint32_t newvalue = 0; | |
22 | for(i = 0; i < nrbits; i++) { | |
23 | newvalue ^= ((value >> i) & 1) << (nrbits - 1 - i); | |
24 | } | |
25 | return newvalue; | |
26 | } | |
27 | ||
f7e3ed82 | 28 | void num_to_bytes(uint64_t n, size_t len, uint8_t* dest) |
e30c654b | 29 | { |
30 | while (len--) { | |
f7e3ed82 | 31 | dest[len] = (uint8_t) n; |
e30c654b | 32 | n >>= 8; |
33 | } | |
34 | } | |
35 | ||
f7e3ed82 | 36 | uint64_t bytes_to_num(uint8_t* src, size_t len) |
e30c654b | 37 | { |
38 | uint64_t num = 0; | |
39 | while (len--) | |
40 | { | |
41 | num = (num << 8) | (*src); | |
42 | src++; | |
43 | } | |
44 | return num; | |
45 | } | |
46 | ||
47 | void LEDsoff() | |
48 | { | |
49 | LED_A_OFF(); | |
50 | LED_B_OFF(); | |
51 | LED_C_OFF(); | |
52 | LED_D_OFF(); | |
53 | } | |
54 | ||
55 | // LEDs: R(C) O(A) G(B) -- R(D) [1, 2, 4 and 8] | |
56 | void LED(int led, int ms) | |
57 | { | |
58 | if (led & LED_RED) | |
59 | LED_C_ON(); | |
60 | if (led & LED_ORANGE) | |
61 | LED_A_ON(); | |
62 | if (led & LED_GREEN) | |
63 | LED_B_ON(); | |
64 | if (led & LED_RED2) | |
65 | LED_D_ON(); | |
66 | ||
67 | if (!ms) | |
68 | return; | |
69 | ||
70 | SpinDelay(ms); | |
71 | ||
72 | if (led & LED_RED) | |
73 | LED_C_OFF(); | |
74 | if (led & LED_ORANGE) | |
75 | LED_A_OFF(); | |
76 | if (led & LED_GREEN) | |
77 | LED_B_OFF(); | |
78 | if (led & LED_RED2) | |
79 | LED_D_OFF(); | |
80 | } | |
81 | ||
82 | ||
83 | // Determine if a button is double clicked, single clicked, | |
84 | // not clicked, or held down (for ms || 1sec) | |
85 | // In general, don't use this function unless you expect a | |
86 | // double click, otherwise it will waste 500ms -- use BUTTON_HELD instead | |
87 | int BUTTON_CLICKED(int ms) | |
88 | { | |
89 | // Up to 500ms in between clicks to mean a double click | |
90 | int ticks = (48000 * (ms ? ms : 1000)) >> 10; | |
91 | ||
92 | // If we're not even pressed, forget about it! | |
93 | if (!BUTTON_PRESS()) | |
94 | return BUTTON_NO_CLICK; | |
95 | ||
96 | // Borrow a PWM unit for my real-time clock | |
97 | AT91C_BASE_PWMC->PWMC_ENA = PWM_CHANNEL(0); | |
98 | // 48 MHz / 1024 gives 46.875 kHz | |
99 | AT91C_BASE_PWMC_CH0->PWMC_CMR = PWM_CH_MODE_PRESCALER(10); | |
100 | AT91C_BASE_PWMC_CH0->PWMC_CDTYR = 0; | |
101 | AT91C_BASE_PWMC_CH0->PWMC_CPRDR = 0xffff; | |
102 | ||
f7e3ed82 | 103 | uint16_t start = AT91C_BASE_PWMC_CH0->PWMC_CCNTR; |
e30c654b | 104 | |
105 | int letoff = 0; | |
106 | for(;;) | |
107 | { | |
f7e3ed82 | 108 | uint16_t now = AT91C_BASE_PWMC_CH0->PWMC_CCNTR; |
e30c654b | 109 | |
110 | // We haven't let off the button yet | |
111 | if (!letoff) | |
112 | { | |
113 | // We just let it off! | |
114 | if (!BUTTON_PRESS()) | |
115 | { | |
116 | letoff = 1; | |
117 | ||
118 | // reset our timer for 500ms | |
119 | start = AT91C_BASE_PWMC_CH0->PWMC_CCNTR; | |
120 | ticks = (48000 * (500)) >> 10; | |
121 | } | |
122 | ||
123 | // Still haven't let it off | |
124 | else | |
125 | // Have we held down a full second? | |
f7e3ed82 | 126 | if (now == (uint16_t)(start + ticks)) |
e30c654b | 127 | return BUTTON_HOLD; |
128 | } | |
129 | ||
130 | // We already let off, did we click again? | |
131 | else | |
132 | // Sweet, double click! | |
133 | if (BUTTON_PRESS()) | |
134 | return BUTTON_DOUBLE_CLICK; | |
135 | ||
136 | // Have we ran out of time to double click? | |
137 | else | |
f7e3ed82 | 138 | if (now == (uint16_t)(start + ticks)) |
e30c654b | 139 | // At least we did a single click |
140 | return BUTTON_SINGLE_CLICK; | |
141 | ||
142 | WDT_HIT(); | |
143 | } | |
144 | ||
145 | // We should never get here | |
146 | return BUTTON_ERROR; | |
147 | } | |
148 | ||
149 | // Determine if a button is held down | |
150 | int BUTTON_HELD(int ms) | |
151 | { | |
152 | // If button is held for one second | |
153 | int ticks = (48000 * (ms ? ms : 1000)) >> 10; | |
154 | ||
155 | // If we're not even pressed, forget about it! | |
156 | if (!BUTTON_PRESS()) | |
157 | return BUTTON_NO_CLICK; | |
158 | ||
159 | // Borrow a PWM unit for my real-time clock | |
160 | AT91C_BASE_PWMC->PWMC_ENA = PWM_CHANNEL(0); | |
161 | // 48 MHz / 1024 gives 46.875 kHz | |
162 | AT91C_BASE_PWMC_CH0->PWMC_CMR = PWM_CH_MODE_PRESCALER(10); | |
163 | AT91C_BASE_PWMC_CH0->PWMC_CDTYR = 0; | |
164 | AT91C_BASE_PWMC_CH0->PWMC_CPRDR = 0xffff; | |
165 | ||
f7e3ed82 | 166 | uint16_t start = AT91C_BASE_PWMC_CH0->PWMC_CCNTR; |
e30c654b | 167 | |
168 | for(;;) | |
169 | { | |
f7e3ed82 | 170 | uint16_t now = AT91C_BASE_PWMC_CH0->PWMC_CCNTR; |
e30c654b | 171 | |
172 | // As soon as our button let go, we didn't hold long enough | |
173 | if (!BUTTON_PRESS()) | |
174 | return BUTTON_SINGLE_CLICK; | |
175 | ||
176 | // Have we waited the full second? | |
177 | else | |
f7e3ed82 | 178 | if (now == (uint16_t)(start + ticks)) |
e30c654b | 179 | return BUTTON_HOLD; |
180 | ||
181 | WDT_HIT(); | |
182 | } | |
183 | ||
184 | // We should never get here | |
185 | return BUTTON_ERROR; | |
186 | } | |
187 | ||
188 | // attempt at high resolution microsecond timer | |
189 | // beware: timer counts in 21.3uS increments (1024/48Mhz) | |
190 | void SpinDelayUs(int us) | |
191 | { | |
192 | int ticks = (48*us) >> 10; | |
193 | ||
194 | // Borrow a PWM unit for my real-time clock | |
195 | AT91C_BASE_PWMC->PWMC_ENA = PWM_CHANNEL(0); | |
196 | // 48 MHz / 1024 gives 46.875 kHz | |
197 | AT91C_BASE_PWMC_CH0->PWMC_CMR = PWM_CH_MODE_PRESCALER(10); | |
198 | AT91C_BASE_PWMC_CH0->PWMC_CDTYR = 0; | |
199 | AT91C_BASE_PWMC_CH0->PWMC_CPRDR = 0xffff; | |
200 | ||
f7e3ed82 | 201 | uint16_t start = AT91C_BASE_PWMC_CH0->PWMC_CCNTR; |
e30c654b | 202 | |
203 | for(;;) { | |
f7e3ed82 | 204 | uint16_t now = AT91C_BASE_PWMC_CH0->PWMC_CCNTR; |
205 | if (now == (uint16_t)(start + ticks)) | |
e30c654b | 206 | return; |
207 | ||
208 | WDT_HIT(); | |
209 | } | |
210 | } | |
211 | ||
212 | void SpinDelay(int ms) | |
213 | { | |
214 | // convert to uS and call microsecond delay function | |
215 | SpinDelayUs(ms*1000); | |
216 | } | |
217 | ||
218 | /* Similar to FpgaGatherVersion this formats stored version information | |
219 | * into a string representation. It takes a pointer to the struct version_information, | |
220 | * verifies the magic properties, then stores a formatted string, prefixed by | |
221 | * prefix in dst. | |
222 | */ | |
223 | void FormatVersionInformation(char *dst, int len, const char *prefix, void *version_information) | |
224 | { | |
225 | struct version_information *v = (struct version_information*)version_information; | |
226 | dst[0] = 0; | |
227 | strncat(dst, prefix, len); | |
228 | if(v->magic != VERSION_INFORMATION_MAGIC) { | |
229 | strncat(dst, "Missing/Invalid version information", len); | |
230 | return; | |
231 | } | |
232 | if(v->versionversion != 1) { | |
233 | strncat(dst, "Version information not understood", len); | |
234 | return; | |
235 | } | |
236 | if(!v->present) { | |
237 | strncat(dst, "Version information not available", len); | |
238 | return; | |
239 | } | |
240 | ||
241 | strncat(dst, v->svnversion, len); | |
242 | if(v->clean == 0) { | |
243 | strncat(dst, "-unclean", len); | |
244 | } else if(v->clean == 2) { | |
245 | strncat(dst, "-suspect", len); | |
246 | } | |
247 | ||
248 | strncat(dst, " ", len); | |
249 | strncat(dst, v->buildtime, len); | |
250 | } | |
9ca155ba M |
251 | |
252 | // ------------------------------------------------------------------------- | |
253 | // timer lib | |
254 | // ------------------------------------------------------------------------- | |
255 | // test procedure: | |
256 | // | |
257 | // ti = GetTickCount(); | |
258 | // SpinDelay(1000); | |
259 | // ti = GetTickCount() - ti; | |
260 | // Dbprintf("timer(1s): %d t=%d", ti, GetTickCount()); | |
261 | ||
262 | void StartTickCount() | |
263 | { | |
264 | // must be 0x40, but on my cpu - included divider is optimal | |
265 | // 0x20 - 1 ms / bit | |
266 | // 0x40 - 2 ms / bit | |
267 | ||
0a39986e | 268 | AT91C_BASE_RTTC->RTTC_RTMR = AT91C_RTTC_RTTRST + 0x001D; // was 0x003B |
9ca155ba M |
269 | } |
270 | ||
271 | /* | |
272 | * Get the current count. | |
273 | */ | |
274 | uint32_t RAMFUNC GetTickCount(){ | |
8f51ddb0 | 275 | return AT91C_BASE_RTTC->RTTC_RTVR;// was * 2; |
9ca155ba M |
276 | } |
277 | ||
8f51ddb0 M |
278 | // ------------------------------------------------------------------------- |
279 | // microseconds timer | |
280 | // ------------------------------------------------------------------------- | |
281 | void StartCountUS() | |
282 | { | |
283 | AT91C_BASE_PMC->PMC_PCER |= (0x1 << 12) | (0x1 << 13) | (0x1 << 14); | |
284 | // AT91C_BASE_TCB->TCB_BMR = AT91C_TCB_TC1XC1S_TIOA0; | |
285 | AT91C_BASE_TCB->TCB_BMR = AT91C_TCB_TC0XC0S_NONE | AT91C_TCB_TC1XC1S_TIOA0 | AT91C_TCB_TC2XC2S_NONE; | |
286 | ||
287 | // fast clock | |
288 | AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS; // timer disable | |
289 | AT91C_BASE_TC0->TC_CMR = AT91C_TC_CLKS_TIMER_DIV3_CLOCK | // MCK(48MHz)/32 -- tick=1.5mks | |
290 | AT91C_TC_WAVE | AT91C_TC_WAVESEL_UP_AUTO | AT91C_TC_ACPA_CLEAR | | |
291 | AT91C_TC_ACPC_SET | AT91C_TC_ASWTRG_SET; | |
292 | AT91C_BASE_TC0->TC_RA = 1; | |
293 | AT91C_BASE_TC0->TC_RC = 0xBFFF + 1; // 0xC000 | |
294 | ||
295 | AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKDIS; // timer disable | |
296 | AT91C_BASE_TC1->TC_CMR = AT91C_TC_CLKS_XC1; // from timer 0 | |
297 | ||
298 | AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKEN; | |
299 | AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKEN; | |
300 | AT91C_BASE_TCB->TCB_BCR = 1; | |
301 | } | |
302 | ||
303 | uint32_t RAMFUNC GetCountUS(){ | |
304 | return (AT91C_BASE_TC1->TC_CV * 0x8000) + ((AT91C_BASE_TC0->TC_CV / 15) * 10); | |
305 | } | |
306 | ||
307 | static uint32_t GlobalUsCounter = 0; | |
308 | ||
309 | uint32_t RAMFUNC GetDeltaCountUS(){ | |
310 | uint32_t g_cnt = GetCountUS(); | |
311 | uint32_t g_res = g_cnt - GlobalUsCounter; | |
312 | GlobalUsCounter = g_cnt; | |
313 | return g_res; | |
314 | } | |
315 | ||
316 |