]> git.zerfleddert.de Git - proxmark3-svn/blob - armsrc/util.c
5af09f8868315538e4734e7de35d6f9e218a020d
[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 "apps.h"
7
8 void *memcpy(void *dest, const void *src, int len)
9 {
10 BYTE *d = dest;
11 const BYTE *s = src;
12 while((len--) > 0) {
13 *d = *s;
14 d++;
15 s++;
16 }
17 return dest;
18 }
19
20 void *memset(void *dest, int c, int len)
21 {
22 BYTE *d = dest;
23 while((len--) > 0) {
24 *d = c;
25 d++;
26 }
27 return dest;
28 }
29
30 int memcmp(const void *av, const void *bv, int len)
31 {
32 const BYTE *a = av;
33 const BYTE *b = bv;
34
35 while((len--) > 0) {
36 if(*a != *b) {
37 return *a - *b;
38 }
39 a++;
40 b++;
41 }
42 return 0;
43 }
44
45 int strlen(char *str)
46 {
47 int l = 0;
48 while(*str) {
49 l++;
50 str++;
51 }
52 return l;
53 }
54
55 void LEDsoff()
56 {
57 LED_A_OFF();
58 LED_B_OFF();
59 LED_C_OFF();
60 LED_D_OFF();
61 }
62
63 // LEDs: R(C) O(A) G(B) -- R(D) [1, 2, 4 and 8]
64 void LED(int led, int ms)
65 {
66 if (led & LED_RED)
67 LED_C_ON();
68 if (led & LED_ORANGE)
69 LED_A_ON();
70 if (led & LED_GREEN)
71 LED_B_ON();
72 if (led & LED_RED2)
73 LED_D_ON();
74
75 if (!ms)
76 return;
77
78 SpinDelay(ms);
79
80 if (led & LED_RED)
81 LED_C_OFF();
82 if (led & LED_ORANGE)
83 LED_A_OFF();
84 if (led & LED_GREEN)
85 LED_B_OFF();
86 if (led & LED_RED2)
87 LED_D_OFF();
88 }
89
90
91 // Determine if a button is double clicked, single clicked,
92 // not clicked, or held down (for ms || 1sec)
93 // In general, don't use this function unless you expect a
94 // double click, otherwise it will waste 500ms -- use BUTTON_HELD instead
95 int BUTTON_CLICKED(int ms)
96 {
97 // Up to 500ms in between clicks to mean a double click
98 int ticks = (48000 * (ms ? ms : 1000)) >> 10;
99
100 // If we're not even pressed, forget about it!
101 if (!BUTTON_PRESS())
102 return BUTTON_NO_CLICK;
103
104 // Borrow a PWM unit for my real-time clock
105 PWM_ENABLE = PWM_CHANNEL(0);
106 // 48 MHz / 1024 gives 46.875 kHz
107 PWM_CH_MODE(0) = PWM_CH_MODE_PRESCALER(10);
108 PWM_CH_DUTY_CYCLE(0) = 0;
109 PWM_CH_PERIOD(0) = 0xffff;
110
111 WORD start = (WORD)PWM_CH_COUNTER(0);
112
113 int letoff = 0;
114 for(;;)
115 {
116 WORD now = (WORD)PWM_CH_COUNTER(0);
117
118 // We haven't let off the button yet
119 if (!letoff)
120 {
121 // We just let it off!
122 if (!BUTTON_PRESS())
123 {
124 letoff = 1;
125
126 // reset our timer for 500ms
127 start = (WORD)PWM_CH_COUNTER(0);
128 ticks = (48000 * (500)) >> 10;
129 }
130
131 // Still haven't let it off
132 else
133 // Have we held down a full second?
134 if (now == (WORD)(start + ticks))
135 return BUTTON_HOLD;
136 }
137
138 // We already let off, did we click again?
139 else
140 // Sweet, double click!
141 if (BUTTON_PRESS())
142 return BUTTON_DOUBLE_CLICK;
143
144 // Have we ran out of time to double click?
145 else
146 if (now == (WORD)(start + ticks))
147 // At least we did a single click
148 return BUTTON_SINGLE_CLICK;
149
150 WDT_HIT();
151 }
152
153 // We should never get here
154 return BUTTON_ERROR;
155 }
156
157 // Determine if a button is held down
158 int BUTTON_HELD(int ms)
159 {
160 // If button is held for one second
161 int ticks = (48000 * (ms ? ms : 1000)) >> 10;
162
163 // If we're not even pressed, forget about it!
164 if (!BUTTON_PRESS())
165 return BUTTON_NO_CLICK;
166
167 // Borrow a PWM unit for my real-time clock
168 PWM_ENABLE = PWM_CHANNEL(0);
169 // 48 MHz / 1024 gives 46.875 kHz
170 PWM_CH_MODE(0) = PWM_CH_MODE_PRESCALER(10);
171 PWM_CH_DUTY_CYCLE(0) = 0;
172 PWM_CH_PERIOD(0) = 0xffff;
173
174 WORD start = (WORD)PWM_CH_COUNTER(0);
175
176 for(;;)
177 {
178 WORD now = (WORD)PWM_CH_COUNTER(0);
179
180 // As soon as our button let go, we didn't hold long enough
181 if (!BUTTON_PRESS())
182 return BUTTON_SINGLE_CLICK;
183
184 // Have we waited the full second?
185 else
186 if (now == (WORD)(start + ticks))
187 return BUTTON_HOLD;
188
189 WDT_HIT();
190 }
191
192 // We should never get here
193 return BUTTON_ERROR;
194 }
195
196 void SpinDelayUs(int us)
197 {
198 int ticks = (48*us) >> 10;
199
200 // Borrow a PWM unit for my real-time clock
201 PWM_ENABLE = PWM_CHANNEL(0);
202 // 48 MHz / 1024 gives 46.875 kHz
203 PWM_CH_MODE(0) = PWM_CH_MODE_PRESCALER(10);
204 PWM_CH_DUTY_CYCLE(0) = 0;
205 PWM_CH_PERIOD(0) = 0xffff;
206
207 WORD start = (WORD)PWM_CH_COUNTER(0);
208
209 for(;;) {
210 WORD now = (WORD)PWM_CH_COUNTER(0);
211 if(now == (WORD)(start + ticks)) {
212 return;
213 }
214 WDT_HIT();
215 }
216 }
217
218 void SpinDelay(int ms)
219 {
220 int ticks = (48000*ms) >> 10;
221
222 // Borrow a PWM unit for my real-time clock
223 PWM_ENABLE = PWM_CHANNEL(0);
224 // 48 MHz / 1024 gives 46.875 kHz
225 PWM_CH_MODE(0) = PWM_CH_MODE_PRESCALER(10);
226 PWM_CH_DUTY_CYCLE(0) = 0;
227 PWM_CH_PERIOD(0) = 0xffff;
228
229 WORD start = (WORD)PWM_CH_COUNTER(0);
230
231 for(;;)
232 {
233 WORD now = (WORD)PWM_CH_COUNTER(0);
234 if (now == (WORD)(start + ticks))
235 return;
236
237 WDT_HIT();
238 }
239 }
Impressum, Datenschutz