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