Skip to content

Commit 7762230

Browse files
committed
Define a cutstom tick_t type which can be set to uint32_t
In many embedded devices, a 32 bit tick is sufficent for any applications. it's also more performant than the 64 bit tick. This patch adds support for switching to 32 bit ticks where it makes sense to do so. Signed-off-by: Siddharth Chandrasekaran <sidcha.dev@gmail.com>
1 parent 781c63a commit 7762230

4 files changed

Lines changed: 31 additions & 14 deletions

File tree

include/utils/utils.h

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,23 @@ extern "C" {
166166
#define PATH_SEPARATOR '/'
167167
#endif
168168

169+
#if defined(__BARE_METAL__) && defined(USE_32BIT_TICK_T)
170+
/*
171+
* Bare-metal timing note:
172+
*
173+
* When __BARE_METAL__ and USE_32BIT_TICK_T are both enabled, tick_t uses
174+
* 32-bit storage. The selected tick source and tick width together define the
175+
* maximum delay interval that can be measured safely in the project.
176+
*
177+
* Example: with a 1 kHz tick source, uint32_t spans about 49.7 days.
178+
* Make sure your largest required delay/timeout remains safe for your chosen
179+
* tick source and counter width.
180+
*/
181+
typedef uint32_t tick_t;
182+
#else
183+
typedef uint64_t tick_t;
184+
#endif
185+
169186
/**
170187
* @brief Return random number between 0 and `limit` both inclusive.
171188
*
@@ -199,18 +216,18 @@ void hexdump(const void *data, size_t len, const char *fmt, ...);
199216
/**
200217
* @brief Get the time in micro seconds.
201218
*/
202-
int64_t usec_now();
219+
tick_t usec_now();
203220

204221
/**
205222
* @brief Get time elapsed in micro seconds since `last`. Used along with
206223
* usec_now().
207224
*/
208-
int64_t usec_since(int64_t last);
225+
tick_t usec_since(tick_t last);
209226

210227
/**
211228
* @brief Get the time in milli seconds.
212229
*/
213-
int64_t millis_now();
230+
tick_t millis_now();
214231

215232
/**
216233
* @brief Get time in seconds and micro_seconds
@@ -228,7 +245,7 @@ int add_iso8601_utc_datetime(char *buf, size_t size);
228245
* @brief Get time elapsed in milli seconds since `last`. Used along with
229246
* millis_now().
230247
*/
231-
int64_t millis_since(int64_t last);
248+
tick_t millis_since(tick_t last);
232249

233250
/**
234251
* @brief Print the stack trace

include/utils/workqueue.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ enum work_status {
3131

3232
typedef struct {
3333
queue_node_t node;
34-
int64_t slice;
34+
tick_t slice;
3535
enum work_status status;
3636
uint32_t requests;
3737

@@ -117,4 +117,4 @@ bool workqueue_work_is_complete(workqueue_t *wq, work_t *work);
117117
}
118118
#endif
119119

120-
#endif /* _UTILS_WORKQUEUE_H_ */
120+
#endif /* _UTILS_WORKQUEUE_H_ */

src/utils.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -218,15 +218,15 @@ int add_iso8601_utc_datetime(char* buf, size_t size) {
218218

219219
#endif
220220

221-
int64_t usec_now()
221+
tick_t usec_now()
222222
{
223-
int64_t usec;
223+
uint64_t usec;
224224
struct timeval tv;
225225

226226
gettimeofday(&tv, NULL);
227227
usec = tv.tv_sec * 1000LL * 1000LL + tv.tv_usec;
228228

229-
return usec;
229+
return (tick_t)usec;
230230
}
231231

232232
void get_time(uint32_t *seconds, uint32_t *micro_seconds)
@@ -238,17 +238,17 @@ void get_time(uint32_t *seconds, uint32_t *micro_seconds)
238238
*micro_seconds = tv.tv_usec;
239239
}
240240

241-
int64_t usec_since(int64_t last)
241+
tick_t usec_since(tick_t last)
242242
{
243243
return usec_now() - last;
244244
}
245245

246-
int64_t millis_now()
246+
tick_t millis_now()
247247
{
248-
return (int64_t)(usec_now() / 1000LL);
248+
return (tick_t)(usec_now() / 1000U);
249249
}
250250

251-
int64_t millis_since(int64_t last)
251+
tick_t millis_since(tick_t last)
252252
{
253253
return millis_now() - last;
254254
}

src/workqueue.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ static void complete_work(work_t *work)
4141
static int do_work(work_t *work)
4242
{
4343
int rc;
44-
int64_t slice;
44+
tick_t slice;
4545

4646
slice = usec_now();
4747
rc = work->work_fn(work->arg);

0 commit comments

Comments
 (0)