]> git.zerfleddert.de Git - proxmark3-svn/blob - armsrc/util.c
fixed compiling under ubuntu with devkitARM_r41
[proxmark3-svn] / armsrc / util.c
1 //-----------------------------------------------------------------------------
2 // Jonathan Westhues, Sept 2005
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.
9 //-----------------------------------------------------------------------------
10
11 #include "proxmark3.h"
12 #include "util.h"
13 #include "string.h"
14
15 uint32_t SwapBits(uint32_t value, int nrbits) {
16 int i;
17 uint32_t newvalue = 0;
18 for(i = 0; i < nrbits; i++) {
19 newvalue ^= ((value >> i) & 1) << (nrbits - 1 - i);
20 }
21 return newvalue;
22 }
23
24 void num_to_bytes(uint64_t n, size_t len, uint8_t* dest)
25 {
26 while (len--) {
27 dest[len] = (uint8_t) n;
28 n >>= 8;
29 }
30 }
31
32 uint64_t bytes_to_num(uint8_t* src, size_t len)
33 {
34 uint64_t num = 0;
35 while (len--)
36 {
37 num = (num << 8) | (*src);
38 src++;
39 }
40 return num;
41 }
42
43 void LEDsoff()
44 {
45 LED_A_OFF();
46 LED_B_OFF();
47 LED_C_OFF();
48 LED_D_OFF();
49 }
50
51 // LEDs: R(C) O(A) G(B) -- R(D) [1, 2, 4 and 8]
52 void LED(int led, int ms)
53 {
54 if (led & LED_RED)
55 LED_C_ON();
56 if (led & LED_ORANGE)
57 LED_A_ON();
58 if (led & LED_GREEN)
59 LED_B_ON();
60 if (led & LED_RED2)
61 LED_D_ON();
62
63 if (!ms)
64 return;
65
66 SpinDelay(ms);
67
68 if (led & LED_RED)
69 LED_C_OFF();
70 if (led & LED_ORANGE)
71 LED_A_OFF();
72 if (led & LED_GREEN)
73 LED_B_OFF();
74 if (led & LED_RED2)
75 LED_D_OFF();
76 }
77
78
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)
84 {
85 // Up to 500ms in between clicks to mean a double click
86 int ticks = (48000 * (ms ? ms : 1000)) >> 10;
87
88 // If we're not even pressed, forget about it!
89 if (!BUTTON_PRESS())
90 return BUTTON_NO_CLICK;
91
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;
98
99 uint16_t start = AT91C_BASE_PWMC_CH0->PWMC_CCNTR;
100
101 int letoff = 0;
102 for(;;)
103 {
104 uint16_t now = AT91C_BASE_PWMC_CH0->PWMC_CCNTR;
105
106 // We haven't let off the button yet
107 if (!letoff)
108 {
109 // We just let it off!
110 if (!BUTTON_PRESS())
111 {
112 letoff = 1;
113
114 // reset our timer for 500ms
115 start = AT91C_BASE_PWMC_CH0->PWMC_CCNTR;
116 ticks = (48000 * (500)) >> 10;
117 }
118
119 // Still haven't let it off
120 else
121 // Have we held down a full second?
122 if (now == (uint16_t)(start + ticks))
123 return BUTTON_HOLD;
124 }
125
126 // We already let off, did we click again?
127 else
128 // Sweet, double click!
129 if (BUTTON_PRESS())
130 return BUTTON_DOUBLE_CLICK;
131
132 // Have we ran out of time to double click?
133 else
134 if (now == (uint16_t)(start + ticks))
135 // At least we did a single click
136 return BUTTON_SINGLE_CLICK;
137
138 WDT_HIT();
139 }
140
141 // We should never get here
142 return BUTTON_ERROR;
143 }
144
145 // Determine if a button is held down
146 int BUTTON_HELD(int ms)
147 {
148 // If button is held for one second
149 int ticks = (48000 * (ms ? ms : 1000)) >> 10;
150
151 // If we're not even pressed, forget about it!
152 if (!BUTTON_PRESS())
153 return BUTTON_NO_CLICK;
154
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;
161
162 uint16_t start = AT91C_BASE_PWMC_CH0->PWMC_CCNTR;
163
164 for(;;)
165 {
166 uint16_t now = AT91C_BASE_PWMC_CH0->PWMC_CCNTR;
167
168 // As soon as our button let go, we didn't hold long enough
169 if (!BUTTON_PRESS())
170 return BUTTON_SINGLE_CLICK;
171
172 // Have we waited the full second?
173 else
174 if (now == (uint16_t)(start + ticks))
175 return BUTTON_HOLD;
176
177 WDT_HIT();
178 }
179
180 // We should never get here
181 return BUTTON_ERROR;
182 }
183
184 // attempt at high resolution microsecond timer
185 // beware: timer counts in 21.3uS increments (1024/48Mhz)
186 void SpinDelayUs(int us)
187 {
188 int ticks = (48*us) >> 10;
189
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;
196
197 uint16_t start = AT91C_BASE_PWMC_CH0->PWMC_CCNTR;
198
199 for(;;) {
200 uint16_t now = AT91C_BASE_PWMC_CH0->PWMC_CCNTR;
201 if (now == (uint16_t)(start + ticks))
202 return;
203
204 WDT_HIT();
205 }
206 }
207
208 void SpinDelay(int ms)
209 {
210 // convert to uS and call microsecond delay function
211 SpinDelayUs(ms*1000);
212 }
213
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
217 * prefix in dst.
218 */
219 void FormatVersionInformation(char *dst, int len, const char *prefix, void *version_information)
220 {
221 struct version_information *v = (struct version_information*)version_information;
222 dst[0] = 0;
223 strncat(dst, prefix, len);
224 if(v->magic != VERSION_INFORMATION_MAGIC) {
225 strncat(dst, "Missing/Invalid version information", len);
226 return;
227 }
228 if(v->versionversion != 1) {
229 strncat(dst, "Version information not understood", len);
230 return;
231 }
232 if(!v->present) {
233 strncat(dst, "Version information not available", len);
234 return;
235 }
236
237 strncat(dst, v->svnversion, len);
238 if(v->clean == 0) {
239 strncat(dst, "-unclean", len);
240 } else if(v->clean == 2) {
241 strncat(dst, "-suspect", len);
242 }
243
244 strncat(dst, " ", len);
245 strncat(dst, v->buildtime, len);
246 }
247
248 // -------------------------------------------------------------------------
249 // timer lib
250 // -------------------------------------------------------------------------
251 // test procedure:
252 //
253 // ti = GetTickCount();
254 // SpinDelay(1000);
255 // ti = GetTickCount() - ti;
256 // Dbprintf("timer(1s): %d t=%d", ti, GetTickCount());
257
258 void StartTickCount()
259 {
260 // must be 0x40, but on my cpu - included divider is optimal
261 // 0x20 - 1 ms / bit
262 // 0x40 - 2 ms / bit
263
264 AT91C_BASE_RTTC->RTTC_RTMR = AT91C_RTTC_RTTRST + 0x001D; // was 0x003B
265 }
266
267 /*
268 * Get the current count.
269 */
270 uint32_t RAMFUNC GetTickCount(){
271 return AT91C_BASE_RTTC->RTTC_RTVR;// was * 2;
272 }
273
274 // -------------------------------------------------------------------------
275 // microseconds timer
276 // -------------------------------------------------------------------------
277 void StartCountUS()
278 {
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;
282
283 // fast clock
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
290
291 AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKDIS; // timer disable
292 AT91C_BASE_TC1->TC_CMR = AT91C_TC_CLKS_XC1; // from timer 0
293
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;
297 }
298
299 uint32_t RAMFUNC GetCountUS(){
300 return (AT91C_BASE_TC1->TC_CV * 0x8000) + ((AT91C_BASE_TC0->TC_CV / 15) * 10);
301 }
302
303 static uint32_t GlobalUsCounter = 0;
304
305 uint32_t RAMFUNC GetDeltaCountUS(){
306 uint32_t g_cnt = GetCountUS();
307 uint32_t g_res = g_cnt - GlobalUsCounter;
308 GlobalUsCounter = g_cnt;
309 return g_res;
310 }
311
312
Impressum, Datenschutz