]> git.zerfleddert.de Git - proxmark3-svn/blob - armsrc/util.c
Add HACKING.txt with coding style guidelines
[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 void num_to_bytes(uint64_t n, size_t len, uint8_t* dest)
16 {
17 while (len--) {
18 dest[len] = (uint8_t) n;
19 n >>= 8;
20 }
21 }
22
23 uint64_t bytes_to_num(uint8_t* src, size_t len)
24 {
25 uint64_t num = 0;
26 while (len--)
27 {
28 num = (num << 8) | (*src);
29 src++;
30 }
31 return num;
32 }
33
34 void 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]
43 void 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
74 int 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
90 uint16_t start = AT91C_BASE_PWMC_CH0->PWMC_CCNTR;
91
92 int letoff = 0;
93 for(;;)
94 {
95 uint16_t now = AT91C_BASE_PWMC_CH0->PWMC_CCNTR;
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?
113 if (now == (uint16_t)(start + ticks))
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
125 if (now == (uint16_t)(start + ticks))
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
137 int 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
153 uint16_t start = AT91C_BASE_PWMC_CH0->PWMC_CCNTR;
154
155 for(;;)
156 {
157 uint16_t now = AT91C_BASE_PWMC_CH0->PWMC_CCNTR;
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
165 if (now == (uint16_t)(start + ticks))
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)
177 void 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
188 uint16_t start = AT91C_BASE_PWMC_CH0->PWMC_CCNTR;
189
190 for(;;) {
191 uint16_t now = AT91C_BASE_PWMC_CH0->PWMC_CCNTR;
192 if (now == (uint16_t)(start + ticks))
193 return;
194
195 WDT_HIT();
196 }
197 }
198
199 void 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 */
210 void 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 }
Impressum, Datenschutz