Merge pull request #4171 from mysterywolf/master

[emergency][posix] 实现usleep函数
This commit is contained in:
Bernard Xiong 2020-12-17 11:39:51 +08:00 committed by GitHub
commit 932bbc29c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 34 additions and 17 deletions

View File

@ -5,20 +5,22 @@
*
* Change Logs:
* Date Author Notes
* 2020-09-05 Meco Man fix bugs
* 2020-09-05 Meco Man fix bugs
* 2020-12-16 Meco Man add useconds_t
*/
#ifndef __TYPES_H__
#define __TYPES_H__
#include <stdint.h>
typedef int32_t clockid_t;
typedef int32_t key_t; /* Used for interprocess communication. */
typedef int32_t pid_t; /* Used for process IDs and process group IDs. */
typedef int32_t clockid_t;
typedef int32_t key_t; /* Used for interprocess communication. */
typedef int32_t pid_t; /* Used for process IDs and process group IDs. */
#ifndef ARCH_CPU_64BIT
typedef signed int ssize_t; /* Used for a count of bytes or an error indication. */
typedef signed int ssize_t; /* Used for a count of bytes or an error indication. */
#else
typedef long signed int ssize_t; /* Used for a count of bytes or an error indication. */
typedef long signed int ssize_t; /* Used for a count of bytes or an error indication. */
#endif
typedef unsigned long useconds_t; /* microseconds (unsigned) */
#endif

View File

@ -5,11 +5,13 @@
*
* Change Logs:
* Date Author Notes
* 2020-12-16 Meco Man add usleep
*/
#ifndef _SYS_UNISTD_H
#define _SYS_UNISTD_H
#include <rtthread.h>
#include <rtconfig.h>
#include "types.h"
#ifdef RT_USING_DFS
@ -69,5 +71,6 @@ int isatty (int fd);
char * ttyname (int desc);
unsigned int sleep(unsigned int seconds);
int usleep(useconds_t usec);
#endif /* _SYS_UNISTD_H */

View File

@ -36,8 +36,7 @@ struct timeval {
};
#endif /* _TIMEVAL_DEFINED */
#ifndef _TIMESPEC
#define _TIMESPEC
#if !defined __GNUC__ && !defined __ICCARM__
struct timespec {
time_t tv_sec; /* seconds */
long tv_nsec; /* and nanoseconds */

View File

@ -5,20 +5,21 @@
*
* Change Logs:
* Date Author Notes
* 2020-12-16 Meco Man add useconds_t
*/
#ifndef __TYPES_H__
#define __TYPES_H__
#include <stdint.h>
#include <rtthread.h>
typedef rt_int32_t clockid_t;
typedef rt_int32_t key_t; /* Used for interprocess communication. */
typedef rt_int32_t pid_t; /* Used for process IDs and process group IDs. */
typedef int32_t clockid_t;
typedef int32_t key_t; /* Used for interprocess communication. */
typedef int32_t pid_t; /* Used for process IDs and process group IDs. */
#ifndef ARCH_CPU_64BIT
typedef signed int ssize_t; /* Used for a count of bytes or an error indication. */
typedef signed int ssize_t; /* Used for a count of bytes or an error indication. */
#else
typedef long signed int ssize_t; /* Used for a count of bytes or an error indication. */
typedef long signed int ssize_t; /* Used for a count of bytes or an error indication. */
#endif
typedef unsigned long useconds_t; /* microseconds (unsigned) */
#endif

View File

@ -5,10 +5,14 @@
*
* Change Logs:
* Date Author Notes
* 2020-12-16 Meco Man add usleep
*/
#ifndef _SYS_UNISTD_H
#define _SYS_UNISTD_H
#include <rtconfig.h>
#include "types.h"
#ifdef RT_USING_DFS
#define STDIN_FILENO 0 /* standard input file descriptor */
@ -41,5 +45,6 @@ int isatty (int fd);
char * ttyname (int desc);
unsigned int sleep(unsigned int seconds);
int usleep(useconds_t usec);
#endif /* _SYS_UNISTD_H */

View File

@ -5,10 +5,10 @@
*
* Change Logs:
* Date Author Notes
* 2020-12-16 Meco Man add usleep
*/
#include <stdlib.h>
#include <rtthread.h>
#include <rthw.h>
#include <unistd.h>
unsigned int sleep(unsigned int seconds)
@ -21,3 +21,10 @@ unsigned int sleep(unsigned int seconds)
return seconds - delta_tick/RT_TICK_PER_SECOND;
}
int usleep(useconds_t usec)
{
rt_thread_mdelay(usec / 1000u);
rt_hw_us_delay(usec % 1000u);
return 0;
}