summaryrefslogtreecommitdiff
path: root/src/stlink-lib/helper.c
blob: 1e756f1de7d80af5fddecc9cb0711f94ddfc1779 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
 * File: helper.c
 *
 * General helper functions
 */

#ifdef STLINK_HAVE_SYS_TIME_H
#include <sys/time.h>
#else
#include <sys_time.h>
#endif // STLINK_HAVE_SYS_TIME_H

#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>

#include "helper.h"

uint32_t time_ms() {
    struct timeval tv;
    gettimeofday(&tv, NULL);
    return (uint32_t)(tv.tv_sec * 1000 + tv.tv_usec / 1000);
}

int32_t arg_parse_freq(const char *str) {
    char *tail;
    int32_t value = (int32_t)strtol(str, &tail, 10);

    if (tail[0] == 'M' && tail[1] == '\0') {
        value = value*1000;
    } else if ((tail[0] != 'k' || tail[1] != '\0') && tail[0] != '\0') {
        return -1;
    }

    return value;
}