]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - armsrc/util.c
1 //-----------------------------------------------------------------------------
2 // Utility functions used in many places, not specific to any piece of code.
3 // Jonathan Westhues, Sept 2005
4 //-----------------------------------------------------------------------------
8 void *memcpy(void *dest
, const void *src
, int len
)
20 void *memset(void *dest
, int c
, int len
)
30 int memcmp(const void *av
, const void *bv
, int len
)
63 // LEDs: R(C) O(A) G(B) -- R(D) [1, 2, 4 and 8]
64 void LED(int led
, int ms
)
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
)
97 // Up to 500ms in between clicks to mean a double click
98 int ticks
= (48000 * (ms
? ms
: 1000)) >> 10;
100 // If we're not even pressed, forget about it!
102 return BUTTON_NO_CLICK
;
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;
111 WORD start
= (WORD
)PWM_CH_COUNTER(0);
116 WORD now
= (WORD
)PWM_CH_COUNTER(0);
118 // We haven't let off the button yet
121 // We just let it off!
126 // reset our timer for 500ms
127 start
= (WORD
)PWM_CH_COUNTER(0);
128 ticks
= (48000 * (500)) >> 10;
131 // Still haven't let it off
133 // Have we held down a full second?
134 if (now
== (WORD
)(start
+ ticks
))
138 // We already let off, did we click again?
140 // Sweet, double click!
142 return BUTTON_DOUBLE_CLICK
;
144 // Have we ran out of time to double click?
146 if (now
== (WORD
)(start
+ ticks
))
147 // At least we did a single click
148 return BUTTON_SINGLE_CLICK
;
153 // We should never get here
157 // Determine if a button is held down
158 int BUTTON_HELD(int ms
)
160 // If button is held for one second
161 int ticks
= (48000 * (ms
? ms
: 1000)) >> 10;
163 // If we're not even pressed, forget about it!
165 return BUTTON_NO_CLICK
;
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;
174 WORD start
= (WORD
)PWM_CH_COUNTER(0);
178 WORD now
= (WORD
)PWM_CH_COUNTER(0);
180 // As soon as our button let go, we didn't hold long enough
182 return BUTTON_SINGLE_CLICK
;
184 // Have we waited the full second?
186 if (now
== (WORD
)(start
+ ticks
))
192 // We should never get here
196 // attempt at high resolution microsecond timer
197 // beware: timer counts in 21.3uS increments (1024/48Mhz)
198 void SpinDelayUs(int us
)
200 int ticks
= (48*us
) >> 10;
202 // Borrow a PWM unit for my real-time clock
203 PWM_ENABLE
= PWM_CHANNEL(0);
204 // 48 MHz / 1024 gives 46.875 kHz
205 PWM_CH_MODE(0) = PWM_CH_MODE_PRESCALER(10);
206 PWM_CH_DUTY_CYCLE(0) = 0;
207 PWM_CH_PERIOD(0) = 0xffff;
209 WORD start
= (WORD
)PWM_CH_COUNTER(0);
212 WORD now
= (WORD
)PWM_CH_COUNTER(0);
213 if (now
== (WORD
)(start
+ ticks
))
220 void SpinDelay(int ms
)
222 // convert to uS and call microsecond delay function
223 SpinDelayUs(ms
*1000);