mod.rs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. use core::{fmt, ops};
  2. use self::timekeep::ktime_get_real_ns;
  3. pub mod clocksource;
  4. pub mod jiffies;
  5. pub mod sleep;
  6. pub mod syscall;
  7. pub mod timeconv;
  8. pub mod timekeep;
  9. pub mod timekeeping;
  10. pub mod timer;
  11. /* Time structures. (Partitially taken from smoltcp)
  12. The `time` module contains structures used to represent both
  13. absolute and relative time.
  14. - [Instant] is used to represent absolute time.
  15. - [Duration] is used to represent relative time.
  16. [Instant]: struct.Instant.html
  17. [Duration]: struct.Duration.html
  18. */
  19. #[allow(dead_code)]
  20. pub const MSEC_PER_SEC: u32 = 1000;
  21. #[allow(dead_code)]
  22. pub const USEC_PER_MSEC: u32 = 1000;
  23. #[allow(dead_code)]
  24. pub const NSEC_PER_USEC: u32 = 1000;
  25. #[allow(dead_code)]
  26. pub const NSEC_PER_MSEC: u32 = 1000000;
  27. #[allow(dead_code)]
  28. pub const USEC_PER_SEC: u32 = 1000000;
  29. #[allow(dead_code)]
  30. pub const NSEC_PER_SEC: u32 = 1000000000;
  31. #[allow(dead_code)]
  32. pub const FSEC_PER_SEC: u64 = 1000000000000000;
  33. /// 表示时间的结构体,符合POSIX标准。
  34. #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
  35. #[repr(C)]
  36. pub struct TimeSpec {
  37. pub tv_sec: i64,
  38. pub tv_nsec: i64,
  39. }
  40. impl TimeSpec {
  41. #[allow(dead_code)]
  42. pub fn new(sec: i64, nsec: i64) -> TimeSpec {
  43. return TimeSpec {
  44. tv_sec: sec,
  45. tv_nsec: nsec,
  46. };
  47. }
  48. }
  49. /// A representation of an absolute time value.
  50. ///
  51. /// The `Instant` type is a wrapper around a `i64` value that
  52. /// represents a number of microseconds, monotonically increasing
  53. /// since an arbitrary moment in time, such as system startup.
  54. ///
  55. /// * A value of `0` is inherently arbitrary.
  56. /// * A value less than `0` indicates a time before the starting
  57. /// point.
  58. #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
  59. #[cfg_attr(feature = "defmt", derive(defmt::Format))]
  60. pub struct Instant {
  61. micros: i64,
  62. }
  63. #[allow(dead_code)]
  64. impl Instant {
  65. pub const ZERO: Instant = Instant::from_micros_const(0);
  66. /// Create a new `Instant` from a number of microseconds.
  67. pub fn from_micros<T: Into<i64>>(micros: T) -> Instant {
  68. Instant {
  69. micros: micros.into(),
  70. }
  71. }
  72. pub const fn from_micros_const(micros: i64) -> Instant {
  73. Instant { micros }
  74. }
  75. /// Create a new `Instant` from a number of milliseconds.
  76. pub fn from_millis<T: Into<i64>>(millis: T) -> Instant {
  77. Instant {
  78. micros: millis.into() * 1000,
  79. }
  80. }
  81. /// Create a new `Instant` from a number of milliseconds.
  82. pub const fn from_millis_const(millis: i64) -> Instant {
  83. Instant {
  84. micros: millis * 1000,
  85. }
  86. }
  87. /// Create a new `Instant` from a number of seconds.
  88. pub fn from_secs<T: Into<i64>>(secs: T) -> Instant {
  89. Instant {
  90. micros: secs.into() * 1000000,
  91. }
  92. }
  93. /// Create a new `Instant` from the current time
  94. pub fn now() -> Instant {
  95. Self::from_micros(ktime_get_real_ns() / 1000)
  96. }
  97. /// The fractional number of milliseconds that have passed
  98. /// since the beginning of time.
  99. pub const fn millis(&self) -> i64 {
  100. self.micros % 1000000 / 1000
  101. }
  102. /// The fractional number of microseconds that have passed
  103. /// since the beginning of time.
  104. pub const fn micros(&self) -> i64 {
  105. self.micros % 1000000
  106. }
  107. /// The number of whole seconds that have passed since the
  108. /// beginning of time.
  109. pub const fn secs(&self) -> i64 {
  110. self.micros / 1000000
  111. }
  112. /// The total number of milliseconds that have passed since
  113. /// the beginning of time.
  114. pub const fn total_millis(&self) -> i64 {
  115. self.micros / 1000
  116. }
  117. /// The total number of milliseconds that have passed since
  118. /// the beginning of time.
  119. pub const fn total_micros(&self) -> i64 {
  120. self.micros
  121. }
  122. }
  123. impl fmt::Display for Instant {
  124. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  125. write!(f, "{}.{:0>3}s", self.secs(), self.millis())
  126. }
  127. }
  128. impl ops::Add<Duration> for Instant {
  129. type Output = Instant;
  130. fn add(self, rhs: Duration) -> Instant {
  131. Instant::from_micros(self.micros + rhs.total_micros() as i64)
  132. }
  133. }
  134. impl ops::AddAssign<Duration> for Instant {
  135. fn add_assign(&mut self, rhs: Duration) {
  136. self.micros += rhs.total_micros() as i64;
  137. }
  138. }
  139. impl ops::Sub<Duration> for Instant {
  140. type Output = Instant;
  141. fn sub(self, rhs: Duration) -> Instant {
  142. Instant::from_micros(self.micros - rhs.total_micros() as i64)
  143. }
  144. }
  145. impl ops::SubAssign<Duration> for Instant {
  146. fn sub_assign(&mut self, rhs: Duration) {
  147. self.micros -= rhs.total_micros() as i64;
  148. }
  149. }
  150. impl ops::Sub<Instant> for Instant {
  151. type Output = Duration;
  152. fn sub(self, rhs: Instant) -> Duration {
  153. Duration::from_micros((self.micros - rhs.micros).unsigned_abs())
  154. }
  155. }
  156. /// A relative amount of time.
  157. #[derive(Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
  158. #[cfg_attr(feature = "defmt", derive(defmt::Format))]
  159. pub struct Duration {
  160. micros: u64,
  161. }
  162. impl Duration {
  163. pub const ZERO: Duration = Duration::from_micros(0);
  164. /// Create a new `Duration` from a number of microseconds.
  165. pub const fn from_micros(micros: u64) -> Duration {
  166. Duration { micros }
  167. }
  168. /// Create a new `Duration` from a number of milliseconds.
  169. pub const fn from_millis(millis: u64) -> Duration {
  170. Duration {
  171. micros: millis * 1000,
  172. }
  173. }
  174. /// Create a new `Instant` from a number of seconds.
  175. pub const fn from_secs(secs: u64) -> Duration {
  176. Duration {
  177. micros: secs * 1000000,
  178. }
  179. }
  180. /// The fractional number of milliseconds in this `Duration`.
  181. pub const fn millis(&self) -> u64 {
  182. self.micros / 1000 % 1000
  183. }
  184. /// The fractional number of milliseconds in this `Duration`.
  185. pub const fn micros(&self) -> u64 {
  186. self.micros % 1000000
  187. }
  188. /// The number of whole seconds in this `Duration`.
  189. pub const fn secs(&self) -> u64 {
  190. self.micros / 1000000
  191. }
  192. /// The total number of milliseconds in this `Duration`.
  193. pub const fn total_millis(&self) -> u64 {
  194. self.micros / 1000
  195. }
  196. /// The total number of microseconds in this `Duration`.
  197. pub const fn total_micros(&self) -> u64 {
  198. self.micros
  199. }
  200. }
  201. impl fmt::Display for Duration {
  202. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  203. write!(f, "{}.{:03}s", self.secs(), self.millis())
  204. }
  205. }
  206. impl ops::Add<Duration> for Duration {
  207. type Output = Duration;
  208. fn add(self, rhs: Duration) -> Duration {
  209. Duration::from_micros(self.micros + rhs.total_micros())
  210. }
  211. }
  212. impl ops::AddAssign<Duration> for Duration {
  213. fn add_assign(&mut self, rhs: Duration) {
  214. self.micros += rhs.total_micros();
  215. }
  216. }
  217. impl ops::Sub<Duration> for Duration {
  218. type Output = Duration;
  219. fn sub(self, rhs: Duration) -> Duration {
  220. Duration::from_micros(
  221. self.micros
  222. .checked_sub(rhs.total_micros())
  223. .expect("overflow when subtracting durations"),
  224. )
  225. }
  226. }
  227. impl ops::SubAssign<Duration> for Duration {
  228. fn sub_assign(&mut self, rhs: Duration) {
  229. self.micros = self
  230. .micros
  231. .checked_sub(rhs.total_micros())
  232. .expect("overflow when subtracting durations");
  233. }
  234. }
  235. impl ops::Mul<u32> for Duration {
  236. type Output = Duration;
  237. fn mul(self, rhs: u32) -> Duration {
  238. Duration::from_micros(self.micros * rhs as u64)
  239. }
  240. }
  241. impl ops::MulAssign<u32> for Duration {
  242. fn mul_assign(&mut self, rhs: u32) {
  243. self.micros *= rhs as u64;
  244. }
  245. }
  246. impl ops::Div<u32> for Duration {
  247. type Output = Duration;
  248. fn div(self, rhs: u32) -> Duration {
  249. Duration::from_micros(self.micros / rhs as u64)
  250. }
  251. }
  252. impl ops::DivAssign<u32> for Duration {
  253. fn div_assign(&mut self, rhs: u32) {
  254. self.micros /= rhs as u64;
  255. }
  256. }
  257. impl ops::Shl<u32> for Duration {
  258. type Output = Duration;
  259. fn shl(self, rhs: u32) -> Duration {
  260. Duration::from_micros(self.micros << rhs)
  261. }
  262. }
  263. impl ops::ShlAssign<u32> for Duration {
  264. fn shl_assign(&mut self, rhs: u32) {
  265. self.micros <<= rhs;
  266. }
  267. }
  268. impl ops::Shr<u32> for Duration {
  269. type Output = Duration;
  270. fn shr(self, rhs: u32) -> Duration {
  271. Duration::from_micros(self.micros >> rhs)
  272. }
  273. }
  274. impl ops::ShrAssign<u32> for Duration {
  275. fn shr_assign(&mut self, rhs: u32) {
  276. self.micros >>= rhs;
  277. }
  278. }
  279. impl From<::core::time::Duration> for Duration {
  280. fn from(other: ::core::time::Duration) -> Duration {
  281. Duration::from_micros(other.as_secs() * 1000000 + other.subsec_micros() as u64)
  282. }
  283. }
  284. impl From<Duration> for ::core::time::Duration {
  285. fn from(val: Duration) -> Self {
  286. ::core::time::Duration::from_micros(val.total_micros())
  287. }
  288. }
  289. /// 支持与smoltcp的时间转换
  290. impl From<smoltcp::time::Instant> for Instant {
  291. fn from(val: smoltcp::time::Instant) -> Self {
  292. Instant::from_micros(val.micros())
  293. }
  294. }
  295. impl Into<smoltcp::time::Instant> for Instant {
  296. fn into(self) -> smoltcp::time::Instant {
  297. smoltcp::time::Instant::from_millis(self.millis())
  298. }
  299. }
  300. /// 支持与smoltcp的时间转换
  301. impl From<smoltcp::time::Duration> for Duration {
  302. fn from(val: smoltcp::time::Duration) -> Self {
  303. Duration::from_micros(val.micros())
  304. }
  305. }
  306. impl Into<smoltcp::time::Duration> for Duration {
  307. fn into(self) -> smoltcp::time::Duration {
  308. smoltcp::time::Duration::from_millis(self.millis())
  309. }
  310. }
  311. pub trait TimeArch {
  312. /// Get CPU cycles (Read from register)
  313. fn get_cycles() -> usize;
  314. }