Stop using usleep().

Use nanosleep() on Unix-like systems, and Sleep() on Windows.
This commit is contained in:
Daniel Beer 2012-08-14 15:51:22 +12:00
parent 4ab905751c
commit 29dfb4da84
8 changed files with 47 additions and 26 deletions

View File

@ -388,7 +388,7 @@ static device_t bsl_open(const struct device_args *args)
if (enter_via_fet(dev) < 0) if (enter_via_fet(dev) < 0)
printc_err("bsl: warning: FET firmware not responding\n"); printc_err("bsl: warning: FET firmware not responding\n");
usleep(500000); delay_ms(500);
/* Show chip info */ /* Show chip info */
if (bsl_xfer(dev, CMD_TX_DATA, 0xff0, NULL, 0x10) < 0) { if (bsl_xfer(dev, CMD_TX_DATA, 0xff0, NULL, 0x10) < 0) {

View File

@ -676,7 +676,7 @@ static device_status_t fet_poll(device_t dev_base)
struct fet_device *dev = (struct fet_device *)dev_base; struct fet_device *dev = (struct fet_device *)dev_base;
ctrlc_reset(); ctrlc_reset();
if ((usleep(50000) < 0) || ctrlc_check()) if ((delay_ms(50) < 0) || ctrlc_check())
return DEVICE_STATUS_INTR; return DEVICE_STATUS_INTR;
if (xfer(dev, C_STATE, NULL, 0, 1, 0) < 0) { if (xfer(dev, C_STATE, NULL, 0, 1, 0) < 0) {
@ -1002,9 +1002,9 @@ int try_open(struct fet_device *dev, const struct device_args *args,
if (dev->flags & FET_PROTO_NOLEAD_SEND) { if (dev->flags & FET_PROTO_NOLEAD_SEND) {
printc("Resetting Olimex command processor...\n"); printc("Resetting Olimex command processor...\n");
transport->send(dev->transport, (const uint8_t *)"\x7e", 1); transport->send(dev->transport, (const uint8_t *)"\x7e", 1);
usleep(5000); delay_ms(5);
transport->send(dev->transport, (const uint8_t *)"\x7e", 1); transport->send(dev->transport, (const uint8_t *)"\x7e", 1);
usleep(5000); delay_ms(5);
} }
printc_dbg("Initializing FET...\n"); printc_dbg("Initializing FET...\n");
@ -1064,7 +1064,7 @@ static device_t fet_open(const struct device_args *args,
dev->flags = flags; dev->flags = flags;
if (try_open(dev, args, flags & FET_PROTO_FORCE_RESET) < 0) { if (try_open(dev, args, flags & FET_PROTO_FORCE_RESET) < 0) {
usleep(500000); delay_ms(500);
printc("Trying again...\n"); printc("Trying again...\n");
if (try_open(dev, args, 1) < 0) if (try_open(dev, args, 1) < 0)
goto fail; goto fail;

View File

@ -226,7 +226,7 @@ static int flash_bsl_recv(struct flash_bsl_device *dev,
debug_hexdump("received message", recv_buf, recv_len); debug_hexdump("received message", recv_buf, recv_len);
#endif #endif
usleep(10000); delay_ms(10);
return recv_len; return recv_len;
} }
@ -550,7 +550,7 @@ static int flash_bsl_writemem(device_t dev_base,
static void entry_delay(void) static void entry_delay(void)
{ {
usleep(1000); delay_ms(1);
} }
static int enter_via_dtr_rts(struct flash_bsl_device *dev) static int enter_via_dtr_rts(struct flash_bsl_device *dev)
@ -657,7 +657,7 @@ static device_t flash_bsl_open(const struct device_args *args)
if (enter_via_dtr_rts(dev) < 0) if (enter_via_dtr_rts(dev) < 0)
goto fail; goto fail;
usleep(500000); delay_ms(500);
/* unlock device (erase then send password) */ /* unlock device (erase then send password) */
if (flash_bsl_unlock(dev) < 0) { if (flash_bsl_unlock(dev) < 0) {

View File

@ -95,7 +95,7 @@ static int reset_sequence(sport_t fd)
return -1; return -1;
} }
usleep(20000); delay_ms(20);
} }
return 0; return 0;
@ -578,7 +578,7 @@ static device_status_t goodfet_poll(device_t dev_base)
(void)dev_base; (void)dev_base;
ctrlc_reset(); ctrlc_reset();
if (usleep(100000) < 0 || ctrlc_check()) if (delay_ms(100) < 0 || ctrlc_check())
return DEVICE_STATUS_INTR; return DEVICE_STATUS_INTR;
return DEVICE_STATUS_RUNNING; return DEVICE_STATUS_RUNNING;

View File

@ -509,7 +509,7 @@ static int do_download(struct usb_device *dev, const struct firmware *f)
offset += r; offset += r;
} }
usleep(100000); delay_ms(100);
if (usb_reset(hnd) < 0) if (usb_reset(hnd) < 0)
pr_error("ti3410: warning: reset failed"); pr_error("ti3410: warning: reset failed");

View File

@ -472,7 +472,7 @@ static device_status_t tilib_poll(device_t dev_base)
struct tilib_device *dev = (struct tilib_device *)dev_base; struct tilib_device *dev = (struct tilib_device *)dev_base;
ctrlc_reset(); ctrlc_reset();
if ((usleep(50000) < 0) || ctrlc_check()) if ((delay_ms(50) < 0) || ctrlc_check())
return DEVICE_STATUS_INTR; return DEVICE_STATUS_INTR;
if (event_fetch(dev) & MID_HALT_ANY) if (event_fetch(dev) & MID_HALT_ANY)

View File

@ -24,6 +24,7 @@
#include <unistd.h> #include <unistd.h>
#include <signal.h> #include <signal.h>
#include <assert.h> #include <assert.h>
#include <time.h>
#ifdef __Windows__ #ifdef __Windows__
#include <windows.h> #include <windows.h>
@ -360,3 +361,34 @@ char *expand_tilde(const char *path)
/* Caller must free()! */ /* Caller must free()! */
return expanded; return expanded;
} }
#ifdef __Windows__
int delay_s(unsigned int s)
{
Sleep(s * 1000);
return 0;
}
int delay_ms(unsigned int s)
{
Sleep(s);
return 0;
}
#else
int delay_s(unsigned int s)
{
return sleep(s);
}
int delay_ms(unsigned int s)
{
struct timespec ts;
ts.tv_sec = s / 1000;
ts.tv_nsec = (s % 1000) * 1000000;
return nanosleep(&ts, NULL);
}
#endif

View File

@ -24,8 +24,6 @@
#ifdef __Windows__ #ifdef __Windows__
#include <windows.h> #include <windows.h>
#else
#include <unistd.h>
#endif #endif
#define ARRAY_LEN(a) (sizeof(a) / sizeof((a)[0])) #define ARRAY_LEN(a) (sizeof(a) / sizeof((a)[0]))
@ -71,17 +69,8 @@ HANDLE ctrlc_win32_event(void);
/* Expand `~' in path names. Caller must free the returned ptr */ /* Expand `~' in path names. Caller must free the returned ptr */
char *expand_tilde(const char *path); char *expand_tilde(const char *path);
/* Sleep for a number of seconds */ /* Sleep for a number of seconds (_s) or milliseconds (_ms) */
#ifdef __Windows__ int delay_s(unsigned int s);
static inline void delay_s(unsigned int s) int delay_ms(unsigned int s);
{
Sleep(s * 1000);
}
#else
static inline void delay_s(unsigned int s)
{
sleep(s);
}
#endif
#endif #endif