]> git.zerfleddert.de Git - proxmark3-svn/blob - armsrc/util.c
7a2895cfc28a4c944fb16f81ab21df95459a724a
[proxmark3-svn] / armsrc / util.c
1 //-----------------------------------------------------------------------------
2 // Utility functions used in many places, not specific to any piece of code.
3 // Jonathan Westhues, Sept 2005
4 //-----------------------------------------------------------------------------
5 #include "proxmark3.h"
6 #include "util.h"
7 #include "string.h"
8
9 void num_to_bytes(uint64_t n, size_t len, uint8_t* dest)
10 {
11 while (len--) {
12 dest[len] = (uint8_t) n;
13 n >>= 8;
14 }
15 }
16
17 uint64_t bytes_to_num(uint8_t* src, size_t len)
18 {
19 uint64_t num = 0;
20 while (len--)
21 {
22 num = (num << 8) | (*src);
23 src++;
24 }
25 return num;
26 }
27
28 void LEDsoff()
29 {
30 LED_A_OFF();
31 LED_B_OFF();
32 LED_C_OFF();
33 LED_D_OFF();
34 }
35
36 // LEDs: R(C) O(A) G(B) -- R(D) [1, 2, 4 and 8]
37 void LED(int led, int ms)
38 {
39 if (led & LED_RED)
40 LED_C_ON();
41 if (led & LED_ORANGE)
42 LED_A_ON();
43 if (led & LED_GREEN)
44 LED_B_ON();
45 if (led & LED_RED2)
46 LED_D_ON();
47
48 if (!ms)
49 return;
50
51 SpinDelay(ms);
52
53 if (led & LED_RED)
54 LED_C_OFF();
55 if (led & LED_ORANGE)
56 LED_A_OFF();
57 if (led & LED_GREEN)
58 LED_B_OFF();
59 if (led & LED_RED2)
60 LED_D_OFF();
61 }
62
63
64 // Determine if a button is double clicked, single clicked,
65 // not clicked, or held down (for ms || 1sec)
66 // In general, don't use this function unless you expect a
67 // double click, otherwise it will waste 500ms -- use BUTTON_HELD instead
68 int BUTTON_CLICKED(int ms)
69 {
70 // Up to 500ms in between clicks to mean a double click
71 int ticks = (48000 * (ms ? ms : 1000)) >> 10;
72
73 // If we're not even pressed, forget about it!
74 if (!BUTTON_PRESS())
75 return BUTTON_NO_CLICK;
76
77 // Borrow a PWM unit for my real-time clock
78 AT91C_BASE_PWMC->PWMC_ENA = PWM_CHANNEL(0);
79 // 48 MHz / 1024 gives 46.875 kHz
80 AT91C_BASE_PWMC_CH0->PWMC_CMR = PWM_CH_MODE_PRESCALER(10);
81 AT91C_BASE_PWMC_CH0->PWMC_CDTYR = 0;
82 AT91C_BASE_PWMC_CH0->PWMC_CPRDR = 0xffff;
83
84 uint16_t start = AT91C_BASE_PWMC_CH0->PWMC_CCNTR;
85
86 int letoff = 0;
87 for(;;)
88 {
89 uint16_t now = AT91C_BASE_PWMC_CH0->PWMC_CCNTR;
90
91 // We haven't let off the button yet
92 if (!letoff)
93 {
94 // We just let it off!
95 if (!BUTTON_PRESS())
96 {
97 letoff = 1;
98
99 // reset our timer for 500ms
100 start = AT91C_BASE_PWMC_CH0->PWMC_CCNTR;
101 ticks = (48000 * (500)) >> 10;
102 }
103
104 // Still haven't let it off
105 else
106 // Have we held down a full second?
107 if (now == (uint16_t)(start + ticks))
108 return BUTTON_HOLD;
109 }
110
111 // We already let off, did we click again?
112 else
113 // Sweet, double click!
114 if (BUTTON_PRESS())
115 return BUTTON_DOUBLE_CLICK;
116
117 // Have we ran out of time to double click?
118 else
119 if (now == (uint16_t)(start + ticks))
120 // At least we did a single click
121 return BUTTON_SINGLE_CLICK;
122
123 WDT_HIT();
124 }
125
126 // We should never get here
127 return BUTTON_ERROR;
128 }
129
130 // Determine if a button is held down
131 int BUTTON_HELD(int ms)
132 {
133 // If button is held for one second
134 int ticks = (48000 * (ms ? ms : 1000)) >> 10;
135
136 // If we're not even pressed, forget about it!
137 if (!BUTTON_PRESS())
138 return BUTTON_NO_CLICK;
139
140 // Borrow a PWM unit for my real-time clock
141 AT91C_BASE_PWMC->PWMC_ENA = PWM_CHANNEL(0);
142 // 48 MHz / 1024 gives 46.875 kHz
143 AT91C_BASE_PWMC_CH0->PWMC_CMR = PWM_CH_MODE_PRESCALER(10);
144 AT91C_BASE_PWMC_CH0->PWMC_CDTYR = 0;
145 AT91C_BASE_PWMC_CH0->PWMC_CPRDR = 0xffff;
146
147 uint16_t start = AT91C_BASE_PWMC_CH0->PWMC_CCNTR;
148
149 for(;;)
150 {
151 uint16_t now = AT91C_BASE_PWMC_CH0->PWMC_CCNTR;
152
153 // As soon as our button let go, we didn't hold long enough
154 if (!BUTTON_PRESS())
155 return BUTTON_SINGLE_CLICK;
156
157 // Have we waited the full second?
158 else
159 if (now == (uint16_t)(start + ticks))
160 return BUTTON_HOLD;
161
162 WDT_HIT();
163 }
164
165 // We should never get here
166 return BUTTON_ERROR;
167 }
168
169 // attempt at high resolution microsecond timer
170 // beware: timer counts in 21.3uS increments (1024/48Mhz)
171 void SpinDelayUs(int us)
172 {
173 int ticks = (48*us) >> 10;
174
175 // Borrow a PWM unit for my real-time clock
176 AT91C_BASE_PWMC->PWMC_ENA = PWM_CHANNEL(0);
177 // 48 MHz / 1024 gives 46.875 kHz
178 AT91C_BASE_PWMC_CH0->PWMC_CMR = PWM_CH_MODE_PRESCALER(10);
179 AT91C_BASE_PWMC_CH0->PWMC_CDTYR = 0;
180 AT91C_BASE_PWMC_CH0->PWMC_CPRDR = 0xffff;
181
182 uint16_t start = AT91C_BASE_PWMC_CH0->PWMC_CCNTR;
183
184 for(;;) {
185 uint16_t now = AT91C_BASE_PWMC_CH0->PWMC_CCNTR;
186 if (now == (uint16_t)(start + ticks))
187 return;
188
189 WDT_HIT();
190 }
191 }
192
193 void SpinDelay(int ms)
194 {
195 // convert to uS and call microsecond delay function
196 SpinDelayUs(ms*1000);
197 }
198
199 /* Similar to FpgaGatherVersion this formats stored version information
200 * into a string representation. It takes a pointer to the struct version_information,
201 * verifies the magic properties, then stores a formatted string, prefixed by
202 * prefix in dst.
203 */
204 void FormatVersionInformation(char *dst, int len, const char *prefix, void *version_information)
205 {
206 struct version_information *v = (struct version_information*)version_information;
207 dst[0] = 0;
208 strncat(dst, prefix, len);
209 if(v->magic != VERSION_INFORMATION_MAGIC) {
210 strncat(dst, "Missing/Invalid version information", len);
211 return;
212 }
213 if(v->versionversion != 1) {
214 strncat(dst, "Version information not understood", len);
215 return;
216 }
217 if(!v->present) {
218 strncat(dst, "Version information not available", len);
219 return;
220 }
221
222 strncat(dst, v->svnversion, len);
223 if(v->clean == 0) {
224 strncat(dst, "-unclean", len);
225 } else if(v->clean == 2) {
226 strncat(dst, "-suspect", len);
227 }
228
229 strncat(dst, " ", len);
230 strncat(dst, v->buildtime, len);
231 }
Impressum, Datenschutz