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
)
55 char* strncat(char *dest
, const char *src
, unsigned int n
)
57 unsigned int dest_len
= strlen(dest
);
60 for (i
= 0 ; i
< n
&& src
[i
] != '\0' ; i
++)
61 dest
[dest_len
+ i
] = src
[i
];
62 dest
[dest_len
+ i
] = '\0';
76 // LEDs: R(C) O(A) G(B) -- R(D) [1, 2, 4 and 8]
77 void LED(int led
, int ms
)
104 // Determine if a button is double clicked, single clicked,
105 // not clicked, or held down (for ms || 1sec)
106 // In general, don't use this function unless you expect a
107 // double click, otherwise it will waste 500ms -- use BUTTON_HELD instead
108 int BUTTON_CLICKED(int ms
)
110 // Up to 500ms in between clicks to mean a double click
111 int ticks
= (48000 * (ms
? ms
: 1000)) >> 10;
113 // If we're not even pressed, forget about it!
115 return BUTTON_NO_CLICK
;
117 // Borrow a PWM unit for my real-time clock
118 PWM_ENABLE
= PWM_CHANNEL(0);
119 // 48 MHz / 1024 gives 46.875 kHz
120 PWM_CH_MODE(0) = PWM_CH_MODE_PRESCALER(10);
121 PWM_CH_DUTY_CYCLE(0) = 0;
122 PWM_CH_PERIOD(0) = 0xffff;
124 WORD start
= (WORD
)PWM_CH_COUNTER(0);
129 WORD now
= (WORD
)PWM_CH_COUNTER(0);
131 // We haven't let off the button yet
134 // We just let it off!
139 // reset our timer for 500ms
140 start
= (WORD
)PWM_CH_COUNTER(0);
141 ticks
= (48000 * (500)) >> 10;
144 // Still haven't let it off
146 // Have we held down a full second?
147 if (now
== (WORD
)(start
+ ticks
))
151 // We already let off, did we click again?
153 // Sweet, double click!
155 return BUTTON_DOUBLE_CLICK
;
157 // Have we ran out of time to double click?
159 if (now
== (WORD
)(start
+ ticks
))
160 // At least we did a single click
161 return BUTTON_SINGLE_CLICK
;
166 // We should never get here
170 // Determine if a button is held down
171 int BUTTON_HELD(int ms
)
173 // If button is held for one second
174 int ticks
= (48000 * (ms
? ms
: 1000)) >> 10;
176 // If we're not even pressed, forget about it!
178 return BUTTON_NO_CLICK
;
180 // Borrow a PWM unit for my real-time clock
181 PWM_ENABLE
= PWM_CHANNEL(0);
182 // 48 MHz / 1024 gives 46.875 kHz
183 PWM_CH_MODE(0) = PWM_CH_MODE_PRESCALER(10);
184 PWM_CH_DUTY_CYCLE(0) = 0;
185 PWM_CH_PERIOD(0) = 0xffff;
187 WORD start
= (WORD
)PWM_CH_COUNTER(0);
191 WORD now
= (WORD
)PWM_CH_COUNTER(0);
193 // As soon as our button let go, we didn't hold long enough
195 return BUTTON_SINGLE_CLICK
;
197 // Have we waited the full second?
199 if (now
== (WORD
)(start
+ ticks
))
205 // We should never get here
209 // attempt at high resolution microsecond timer
210 // beware: timer counts in 21.3uS increments (1024/48Mhz)
211 void SpinDelayUs(int us
)
213 int ticks
= (48*us
) >> 10;
215 // Borrow a PWM unit for my real-time clock
216 PWM_ENABLE
= PWM_CHANNEL(0);
217 // 48 MHz / 1024 gives 46.875 kHz
218 PWM_CH_MODE(0) = PWM_CH_MODE_PRESCALER(10);
219 PWM_CH_DUTY_CYCLE(0) = 0;
220 PWM_CH_PERIOD(0) = 0xffff;
222 WORD start
= (WORD
)PWM_CH_COUNTER(0);
225 WORD now
= (WORD
)PWM_CH_COUNTER(0);
226 if (now
== (WORD
)(start
+ ticks
))
233 void SpinDelay(int ms
)
235 // convert to uS and call microsecond delay function
236 SpinDelayUs(ms
*1000);
239 /* Similar to FpgaGatherVersion this formats stored version information
240 * into a string representation. It takes a pointer to the struct version_information,
241 * verifies the magic properties, then stores a formatted string, prefixed by
244 void FormatVersionInformation(char *dst
, int len
, const char *prefix
, void *version_information
)
246 struct version_information
*v
= (struct version_information
*)version_information
;
248 strncat(dst
, prefix
, len
);
249 if(v
->magic
!= VERSION_INFORMATION_MAGIC
) {
250 strncat(dst
, "Missing/Invalid version information", len
);
253 if(v
->versionversion
!= 1) {
254 strncat(dst
, "Version information not understood", len
);
258 strncat(dst
, "Version information not available", len
);
262 strncat(dst
, v
->svnversion
, len
);
264 strncat(dst
, "-unclean", len
);
265 } else if(v
->clean
== 2) {
266 strncat(dst
, "-suspect", len
);
269 strncat(dst
, " ", len
);
270 strncat(dst
, v
->buildtime
, len
);