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