]> git.sur5r.net Git - openocd/commitdiff
Use timeval helpers
authorChristopher Head <chead@zaber.com>
Wed, 24 Jan 2018 22:35:40 +0000 (14:35 -0800)
committerFreddie Chopin <freddie.chopin@gmail.com>
Thu, 25 Jan 2018 16:43:49 +0000 (16:43 +0000)
Some of these changes actually fix broken comparisons which could
occasionally fail. Others just clean up the code and make it more clear.

Change-Id: I6c398bdc45fa0d2716f48a74822457d1351f81a5
Signed-off-by: Christopher Head <chead@zaber.com>
Reviewed-on: http://openocd.zylin.com/4380
Tested-by: jenkins
Reviewed-by: Tomas Vanek <vanekt@fbl.cz>
src/jtag/drivers/presto.c
src/target/cortex_m.c
src/target/embeddedice.c
src/target/openrisc/or1k.c
src/target/target.c
src/target/xscale.c

index 49caa679f9022036ff7e357aa7b105b63529f709..29bc811116d5f54fe3d59fac773e0e48cdd3feae 100644 (file)
@@ -117,8 +117,7 @@ static int presto_read(uint8_t *buf, uint32_t size)
                ftbytes += presto->retval;
 
                gettimeofday(&now, NULL);
-               if ((now.tv_sec > timeout.tv_sec) ||
-                               ((now.tv_sec == timeout.tv_sec) && (now.tv_usec > timeout.tv_usec)))
+               if (timeval_compare(&now, &timeout) > 0)
                        break;
        }
 
index 24063a7e6af06dc49bbf083cb0c72a6f05226dcc..79af632ac7faca703292f02055072b1fb033738a 100644 (file)
@@ -1787,8 +1787,7 @@ int cortex_m_profiling(struct target *target, uint32_t *samples,
 
 
                gettimeofday(&now, NULL);
-               if (sample_count >= max_num_samples ||
-                       (now.tv_sec >= timeout.tv_sec && now.tv_usec >= timeout.tv_usec)) {
+               if (sample_count >= max_num_samples || timeval_compare(&now, &timeout) > 0) {
                        LOG_INFO("Profiling completed. %" PRIu32 " samples.", sample_count);
                        break;
                }
index 09d6fc8a158366896c13cfcac4729de28e0c278b..7232ef1e4685ab9b0e07d143576e9d397208f27e 100644 (file)
@@ -28,6 +28,7 @@
 
 #include "embeddedice.h"
 #include "register.h"
+#include <helper/time_support.h>
 
 /**
  * @file
@@ -576,8 +577,8 @@ int embeddedice_handshake(struct arm_jtag *jtag_info, int hsbit, uint32_t timeou
        uint8_t field2_out[1];
        int retval;
        uint32_t hsact;
-       struct timeval lap;
        struct timeval now;
+       struct timeval timeout_end;
 
        if (hsbit == EICE_COMM_CTRL_WBIT)
                hsact = 1;
@@ -610,7 +611,8 @@ int embeddedice_handshake(struct arm_jtag *jtag_info, int hsbit, uint32_t timeou
        fields[2].in_value = NULL;
 
        jtag_add_dr_scan(jtag_info->tap, 3, fields, TAP_IDLE);
-       gettimeofday(&lap, NULL);
+       gettimeofday(&timeout_end, NULL);
+       timeval_add_time(&timeout_end, 0, timeout * 1000);
        do {
                jtag_add_dr_scan(jtag_info->tap, 3, fields, TAP_IDLE);
                retval = jtag_execute_queue();
@@ -621,8 +623,7 @@ int embeddedice_handshake(struct arm_jtag *jtag_info, int hsbit, uint32_t timeou
                        return ERROR_OK;
 
                gettimeofday(&now, NULL);
-       } while ((uint32_t)((now.tv_sec - lap.tv_sec) * 1000
-                       + (now.tv_usec - lap.tv_usec) / 1000) <= timeout);
+       } while (timeval_compare(&now, &timeout_end) <= 0);
 
        LOG_ERROR("embeddedice handshake timeout");
        return ERROR_TARGET_TIMEOUT;
index 3895ddfaf518100ef48ea5e8cfd302574a947305..bcb648c2788620e43b075c9929294a19b20e9b7e 100644 (file)
@@ -1248,8 +1248,7 @@ static int or1k_profiling(struct target *target, uint32_t *samples,
                samples[sample_count++] = reg_value;
 
                gettimeofday(&now, NULL);
-               if ((sample_count >= max_num_samples) ||
-                       ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec))) {
+               if ((sample_count >= max_num_samples) || timeval_compare(&now, &timeout) > 0) {
                        LOG_INFO("Profiling completed. %" PRIu32 " samples.", sample_count);
                        break;
                }
index 52307dbf443700d75b2b6017b441a7fd2c74b593..58d6d82682aed90f8c2218b0701fb4088f05a5c2 100644 (file)
@@ -1397,7 +1397,6 @@ int target_register_trace_callback(int (*callback)(struct target *target,
 int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
 {
        struct target_timer_callback **callbacks_p = &target_timer_callbacks;
-       struct timeval now;
 
        if (callback == NULL)
                return ERROR_COMMAND_SYNTAX_ERROR;
@@ -1414,14 +1413,8 @@ int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int
        (*callbacks_p)->time_ms = time_ms;
        (*callbacks_p)->removed = false;
 
-       gettimeofday(&now, NULL);
-       (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
-       time_ms -= (time_ms % 1000);
-       (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
-       if ((*callbacks_p)->when.tv_usec > 1000000) {
-               (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
-               (*callbacks_p)->when.tv_sec += 1;
-       }
+       gettimeofday(&(*callbacks_p)->when, NULL);
+       timeval_add_time(&(*callbacks_p)->when, 0, time_ms * 1000);
 
        (*callbacks_p)->priv = priv;
        (*callbacks_p)->next = NULL;
@@ -1556,14 +1549,8 @@ int target_call_trace_callbacks(struct target *target, size_t len, uint8_t *data
 static int target_timer_callback_periodic_restart(
                struct target_timer_callback *cb, struct timeval *now)
 {
-       int time_ms = cb->time_ms;
-       cb->when.tv_usec = now->tv_usec + (time_ms % 1000) * 1000;
-       time_ms -= (time_ms % 1000);
-       cb->when.tv_sec = now->tv_sec + time_ms / 1000;
-       if (cb->when.tv_usec > 1000000) {
-               cb->when.tv_usec = cb->when.tv_usec - 1000000;
-               cb->when.tv_sec += 1;
-       }
+       cb->when = *now;
+       timeval_add_time(&cb->when, 0, cb->time_ms * 1000L);
        return ERROR_OK;
 }
 
@@ -1607,9 +1594,7 @@ static int target_call_timer_callbacks_check_time(int checktime)
 
                bool call_it = (*callback)->callback &&
                        ((!checktime && (*callback)->periodic) ||
-                        now.tv_sec > (*callback)->when.tv_sec ||
-                        (now.tv_sec == (*callback)->when.tv_sec &&
-                         now.tv_usec >= (*callback)->when.tv_usec));
+                        timeval_compare(&now, &(*callback)->when) >= 0);
 
                if (call_it)
                        target_call_timer_callback(*callback, &now);
@@ -2028,8 +2013,7 @@ static int target_profiling_default(struct target *target, uint32_t *samples,
                        break;
 
                gettimeofday(&now, NULL);
-               if ((sample_count >= max_num_samples) ||
-                       ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec))) {
+               if ((sample_count >= max_num_samples) || timeval_compare(&now, &timeout) >= 0) {
                        LOG_INFO("Profiling completed. %" PRIu32 " samples.", sample_count);
                        break;
                }
index 8fe8a2cb9939f1052fe63d5bdcc1c5d61042cb9f..87a3d0f78d69e9bde71337b58c733a8928329f23 100644 (file)
@@ -404,8 +404,7 @@ static int xscale_read_tx(struct target *target, int consume)
                }
 
                gettimeofday(&now, NULL);
-               if ((now.tv_sec > timeout.tv_sec) ||
-                       ((now.tv_sec == timeout.tv_sec) && (now.tv_usec > timeout.tv_usec))) {
+               if (timeval_compare(&now, &timeout) > 0) {
                        LOG_ERROR("time out reading TX register");
                        return ERROR_TARGET_TIMEOUT;
                }