Skip to content

Commit 6808287

Browse files
committed
hackrf_transfer: add fixed point support for frequency and sample rate
1 parent 608c92f commit 6808287

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

host/hackrf-tools/src/hackrf_transfer.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,49 @@ int parse_u32(char* s, uint32_t* const value)
255255
}
256256
}
257257

258+
int parse_fp64(char* s, uint8_t Qn, uint64_t* const value)
259+
{
260+
uint64_t m = 0;
261+
uint64_t n = 0;
262+
uint64_t div = 1;
263+
uint64_t div_max = log10(1 << Qn) * 10;
264+
265+
// parse integer component
266+
for (; (*s >= '0') && (*s <= '9'); s++) {
267+
m = (m * 10) + (*s - '0');
268+
}
269+
*value = (m << Qn);
270+
271+
if (*s != '.') {
272+
return HACKRF_SUCCESS;
273+
}
274+
s++;
275+
276+
// parse fractional component
277+
for (; (*s >= '0') && (*s <= '9') && (div < div_max); s++) {
278+
n = (n * 10) + (*s - '0');
279+
div *= 10;
280+
}
281+
282+
if (div == 1) {
283+
return HACKRF_SUCCESS;
284+
}
285+
286+
*value += ((n << Qn) + (div / 2)) / div;
287+
288+
return HACKRF_SUCCESS;
289+
}
290+
291+
int parse_frequency_fp(char* optarg, fp_40_24_t* value)
292+
{
293+
return parse_fp64(optarg, 24, value);
294+
}
295+
296+
int parse_sample_rate_fp(char* optarg, fp_28_36_t* value)
297+
{
298+
return parse_fp64(optarg, 36, value);
299+
}
300+
258301
/* Parse frequencies as doubles to take advantage of notation parsing */
259302
int parse_frequency_i64(char* optarg, char* endptr, int64_t* value)
260303
{

0 commit comments

Comments
 (0)