Skip to content

Commit e89409f

Browse files
committed
Optimize systime
1 parent cc56e6f commit e89409f

3 files changed

Lines changed: 10 additions & 12 deletions

File tree

src/fmt_def.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ extern "C" {
2424
#endif
2525

2626
/* Firmament version information */
27-
#define FMT_VERSION "v1.1.2"
27+
#define FMT_VERSION "v1.1.3"
2828

2929
/* Thread Prority */
3030
#define VEHICLE_THREAD_PRIORITY 3

src/module/system/systime.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,17 @@ uint64_t systime_now_us(void)
9595
uint32_t systick_us = 0;
9696
uint64_t time_now_ms;
9797
uint64_t now_us;
98-
static uint64_t monotonic_us = 0;
9998
uint32_t level;
99+
static uint64_t monotonic_us = 0;
100100

101101
if (systick_dev == NULL) {
102102
return 0;
103103
}
104104

105105
level = rt_hw_interrupt_disable();
106-
rt_device_read(systick_dev, SYSTICK_RD_TIME_US, &systick_us, sizeof(uint32_t));
106+
107107
/* atomic read */
108+
rt_device_read(systick_dev, SYSTICK_RD_TIME_US, &systick_us, sizeof(uint32_t));
108109
time_now_ms = __systime.msPeriod;
109110

110111
now_us = time_now_ms * 1000ULL + systick_us;
@@ -115,11 +116,8 @@ uint64_t systime_now_us(void)
115116
}
116117

117118
/* Ensure strict monotonicity */
118-
if (now_us > monotonic_us) {
119-
monotonic_us = now_us;
120-
} else {
121-
now_us = monotonic_us;
122-
}
119+
monotonic_us = now_us;
120+
123121
rt_hw_interrupt_enable(level);
124122

125123
return now_us;
@@ -132,7 +130,7 @@ uint64_t systime_now_us(void)
132130
*/
133131
uint32_t systime_now_ms(void)
134132
{
135-
uint32_t time_now_ms = systime_now_us() / 1e3;
133+
uint32_t time_now_ms = systime_now_us() / 1000ULL;
136134

137135
return time_now_ms;
138136
}
@@ -161,7 +159,7 @@ void systime_udelay(uint32_t time_us)
161159
*/
162160
inline void systime_mdelay(uint32_t time_ms)
163161
{
164-
systime_udelay(time_ms * 1000);
162+
systime_udelay(time_ms * 1000ULL);
165163
}
166164

167165
/**
@@ -203,7 +201,7 @@ fmt_err_t systime_init(void)
203201

204202
__systime.msPeriod = 0;
205203
/* Calculate integer ms without losing precision */
206-
__systime.msPerPeriod = systick_device->ticks_per_isr / (systick_device->ticks_per_us * 1000);
204+
__systime.msPerPeriod = systick_device->ticks_per_isr / (systick_device->ticks_per_us * 1000UL);
207205

208206
systick_device->systick_isr_cb = systick_isr_cb;
209207

0 commit comments

Comments
 (0)