Sat, 20 Apr 2024 11:34:00 +0200
Version 0.9.19a1
652
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
1 | /** |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
2 | * @file delay.c |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
3 | * #brief Thread friendly delay functions. |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
4 | * |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
5 | * Copyright (C) 2024 |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
6 | * |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
7 | * Michiel Broek <mbroek at mbse dot eu> |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
8 | * |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
9 | * This file is part of the mbsePi-apps |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
10 | * |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
11 | * This is free software; you can redistribute it and/or modify it |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
12 | * under the terms of the GNU General Public License as published by the |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
13 | * Free Software Foundation; either version 2, or (at your option) any |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
14 | * later version. |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
15 | * |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
16 | * mbsePi-apps is distributed in the hope that it will be useful, but |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
17 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
19 | * General Public License for more details. |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
20 | * |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
21 | * You should have received a copy of the GNU General Public License |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
22 | * along with ThermFerm; see the file COPYING. If not, write to the Free |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
23 | * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
24 | */ |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
25 | |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
26 | #include "delay.h" |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
27 | #include "thermferm.h" |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
28 | |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
29 | |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
30 | void delay (unsigned int howLong) |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
31 | { |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
32 | struct timespec sleeper, dummy ; |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
33 | |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
34 | sleeper.tv_sec = (time_t)(howLong / 1000) ; |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
35 | sleeper.tv_nsec = (long)(howLong % 1000) * 1000000 ; |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
36 | |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
37 | nanosleep (&sleeper, &dummy) ; |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
38 | } |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
39 | |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
40 | |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
41 | |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
42 | |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
43 | /** |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
44 | * @brief mDelay Delay number of milliseconds. |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
45 | * @param howLong The number of milliseconds. |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
46 | */ |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
47 | void mDelay(unsigned int howLong) |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
48 | { |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
49 | struct timespec sleeper ; |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
50 | unsigned int mSecs = howLong % 1000 ; |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
51 | unsigned int wSecs = howLong / 1000 ; |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
52 | |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
53 | /**/ if (howLong == 0) |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
54 | return ; |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
55 | else { |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
56 | sleeper.tv_sec = wSecs ; |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
57 | sleeper.tv_nsec = (long)(mSecs * 1000000L) ; |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
58 | nanosleep (&sleeper, NULL) ; |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
59 | } |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
60 | } |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
61 | |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
62 | |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
63 | /* |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
64 | * Code from wiringPi, Gordon. |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
65 | * |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
66 | * delayMicroseconds: |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
67 | * This is somewhat intersting. It seems that on the Pi, a single call |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
68 | * to nanosleep takes some 80 to 130 microseconds anyway, so while |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
69 | * obeying the standards (may take longer), it's not always what we |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
70 | * want! |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
71 | * |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
72 | * So what I'll do now is if the delay is less than 100uS we'll do it |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
73 | * in a hard loop, watching a built-in counter on the ARM chip. This is |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
74 | * somewhat sub-optimal in that it uses 100% CPU, something not an issue |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
75 | * in a microcontroller, but under a multi-tasking, multi-user OS, it's |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
76 | * wastefull, however we've no real choice )-: |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
77 | * |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
78 | * Plan B: It seems all might not be well with that plan, so changing it |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
79 | * to use gettimeofday () and poll on that instead... |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
80 | ********************************************************************************* |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
81 | */ |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
82 | void delayMicrosecondsHard (unsigned int howLong) |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
83 | { |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
84 | struct timeval tNow, tLong, tEnd ; |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
85 | |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
86 | gettimeofday (&tNow, NULL) ; |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
87 | tLong.tv_sec = howLong / 1000000 ; |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
88 | tLong.tv_usec = howLong % 1000000 ; |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
89 | timeradd (&tNow, &tLong, &tEnd) ; |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
90 | |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
91 | while (timercmp (&tNow, &tEnd, <)) |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
92 | gettimeofday (&tNow, NULL) ; |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
93 | } |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
94 | |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
95 | |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
96 | /** |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
97 | * @brief uDelay Delays a number of microseconds. Also used to replace usleep(). |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
98 | * @param howLong The number is microseconds. |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
99 | */ |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
100 | void uDelay(unsigned int howLong) |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
101 | { |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
102 | struct timespec sleeper ; |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
103 | unsigned int uSecs = howLong % 1000000 ; |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
104 | unsigned int wSecs = howLong / 1000000 ; |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
105 | |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
106 | /**/ if (howLong == 0) |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
107 | return ; |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
108 | else if (howLong < 100) |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
109 | delayMicrosecondsHard(howLong) ; |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
110 | else { |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
111 | sleeper.tv_sec = wSecs ; |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
112 | sleeper.tv_nsec = (long)(uSecs * 1000L) ; |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
113 | nanosleep (&sleeper, NULL) ; |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
114 | } |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
115 | } |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
116 | |
16d3d4b58b5b
Moved all delay functions into a new general file.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
117 |