1 //-----------------------------------------------------------------------------
2 // Utility functions used in many places, not specific to any piece of code.
3 // Jonathan Westhues, Sept 2005
4 //-----------------------------------------------------------------------------
9 void num_to_bytes(uint64_t n
, size_t len
, uint8_t* dest
)
12 dest
[len
] = (uint8_t) n
;
17 uint64_t bytes_to_num(uint8_t* src
, size_t len
)
22 num
= (num
<< 8) | (*src
);
36 // LEDs: R(C) O(A) G(B) -- R(D) [1, 2, 4 and 8]
37 void LED(int led
, int ms
)
64 // Determine if a button is double clicked, single clicked,
65 // not clicked, or held down (for ms || 1sec)
66 // In general, don't use this function unless you expect a
67 // double click, otherwise it will waste 500ms -- use BUTTON_HELD instead
68 int BUTTON_CLICKED(int ms
)
70 // Up to 500ms in between clicks to mean a double click
71 int ticks
= (48000 * (ms
? ms
: 1000)) >> 10;
73 // If we're not even pressed, forget about it!
75 return BUTTON_NO_CLICK
;
77 // Borrow a PWM unit for my real-time clock
78 AT91C_BASE_PWMC
->PWMC_ENA
= PWM_CHANNEL(0);
79 // 48 MHz / 1024 gives 46.875 kHz
80 AT91C_BASE_PWMC_CH0
->PWMC_CMR
= PWM_CH_MODE_PRESCALER(10);
81 AT91C_BASE_PWMC_CH0
->PWMC_CDTYR
= 0;
82 AT91C_BASE_PWMC_CH0
->PWMC_CPRDR
= 0xffff;
84 uint16_t start
= AT91C_BASE_PWMC_CH0
->PWMC_CCNTR
;
89 uint16_t now
= AT91C_BASE_PWMC_CH0
->PWMC_CCNTR
;
91 // We haven't let off the button yet
94 // We just let it off!
99 // reset our timer for 500ms
100 start
= AT91C_BASE_PWMC_CH0
->PWMC_CCNTR
;
101 ticks
= (48000 * (500)) >> 10;
104 // Still haven't let it off
106 // Have we held down a full second?
107 if (now
== (uint16_t)(start
+ ticks
))
111 // We already let off, did we click again?
113 // Sweet, double click!
115 return BUTTON_DOUBLE_CLICK
;
117 // Have we ran out of time to double click?
119 if (now
== (uint16_t)(start
+ ticks
))
120 // At least we did a single click
121 return BUTTON_SINGLE_CLICK
;
126 // We should never get here
130 // Determine if a button is held down
131 int BUTTON_HELD(int ms
)
133 // If button is held for one second
134 int ticks
= (48000 * (ms
? ms
: 1000)) >> 10;
136 // If we're not even pressed, forget about it!
138 return BUTTON_NO_CLICK
;
140 // Borrow a PWM unit for my real-time clock
141 AT91C_BASE_PWMC
->PWMC_ENA
= PWM_CHANNEL(0);
142 // 48 MHz / 1024 gives 46.875 kHz
143 AT91C_BASE_PWMC_CH0
->PWMC_CMR
= PWM_CH_MODE_PRESCALER(10);
144 AT91C_BASE_PWMC_CH0
->PWMC_CDTYR
= 0;
145 AT91C_BASE_PWMC_CH0
->PWMC_CPRDR
= 0xffff;
147 uint16_t start
= AT91C_BASE_PWMC_CH0
->PWMC_CCNTR
;
151 uint16_t now
= AT91C_BASE_PWMC_CH0
->PWMC_CCNTR
;
153 // As soon as our button let go, we didn't hold long enough
155 return BUTTON_SINGLE_CLICK
;
157 // Have we waited the full second?
159 if (now
== (uint16_t)(start
+ ticks
))
165 // We should never get here
169 // attempt at high resolution microsecond timer
170 // beware: timer counts in 21.3uS increments (1024/48Mhz)
171 void SpinDelayUs(int us
)
173 int ticks
= (48*us
) >> 10;
175 // Borrow a PWM unit for my real-time clock
176 AT91C_BASE_PWMC
->PWMC_ENA
= PWM_CHANNEL(0);
177 // 48 MHz / 1024 gives 46.875 kHz
178 AT91C_BASE_PWMC_CH0
->PWMC_CMR
= PWM_CH_MODE_PRESCALER(10);
179 AT91C_BASE_PWMC_CH0
->PWMC_CDTYR
= 0;
180 AT91C_BASE_PWMC_CH0
->PWMC_CPRDR
= 0xffff;
182 uint16_t start
= AT91C_BASE_PWMC_CH0
->PWMC_CCNTR
;
185 uint16_t now
= AT91C_BASE_PWMC_CH0
->PWMC_CCNTR
;
186 if (now
== (uint16_t)(start
+ ticks
))
193 void SpinDelay(int ms
)
195 // convert to uS and call microsecond delay function
196 SpinDelayUs(ms
*1000);
199 /* Similar to FpgaGatherVersion this formats stored version information
200 * into a string representation. It takes a pointer to the struct version_information,
201 * verifies the magic properties, then stores a formatted string, prefixed by
204 void FormatVersionInformation(char *dst
, int len
, const char *prefix
, void *version_information
)
206 struct version_information
*v
= (struct version_information
*)version_information
;
208 strncat(dst
, prefix
, len
);
209 if(v
->magic
!= VERSION_INFORMATION_MAGIC
) {
210 strncat(dst
, "Missing/Invalid version information", len
);
213 if(v
->versionversion
!= 1) {
214 strncat(dst
, "Version information not understood", len
);
218 strncat(dst
, "Version information not available", len
);
222 strncat(dst
, v
->svnversion
, len
);
224 strncat(dst
, "-unclean", len
);
225 } else if(v
->clean
== 2) {
226 strncat(dst
, "-suspect", len
);
229 strncat(dst
, " ", len
);
230 strncat(dst
, v
->buildtime
, len
);