|
1 // Copyright (c) 2014 Adafruit Industries |
|
2 // Author: Tony DiCola |
|
3 |
|
4 // Permission is hereby granted, free of charge, to any person obtaining a copy |
|
5 // of this software and associated documentation files (the "Software"), to deal |
|
6 // in the Software without restriction, including without limitation the rights |
|
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
8 // copies of the Software, and to permit persons to whom the Software is |
|
9 // furnished to do so, subject to the following conditions: |
|
10 |
|
11 // The above copyright notice and this permission notice shall be included in all |
|
12 // copies or substantial portions of the Software. |
|
13 |
|
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
20 // SOFTWARE. |
|
21 #include <errno.h> |
|
22 #include <sched.h> |
|
23 #include <string.h> |
|
24 #include <sys/time.h> |
|
25 #include <time.h> |
|
26 |
|
27 #include "common_dht_read.h" |
|
28 |
|
29 void busy_wait_milliseconds(uint32_t millis) { |
|
30 // Set delay time period. |
|
31 struct timeval deltatime; |
|
32 deltatime.tv_sec = millis / 1000; |
|
33 deltatime.tv_usec = (millis % 1000) * 1000; |
|
34 struct timeval walltime; |
|
35 // Get current time and add delay to find end time. |
|
36 gettimeofday(&walltime, NULL); |
|
37 struct timeval endtime; |
|
38 timeradd(&walltime, &deltatime, &endtime); |
|
39 // Tight loop to waste time (and CPU) until enough time as elapsed. |
|
40 while (timercmp(&walltime, &endtime, <)) { |
|
41 gettimeofday(&walltime, NULL); |
|
42 } |
|
43 } |
|
44 |
|
45 void sleep_milliseconds(uint32_t millis) { |
|
46 struct timespec sleep; |
|
47 sleep.tv_sec = millis / 1000; |
|
48 sleep.tv_nsec = (millis % 1000) * 1000000L; |
|
49 // while (clock_nanosleep(CLOCK_MONOTONIC, 0, &sleep, &sleep) && errno == EINTR); |
|
50 while (nanosleep(&sleep, &sleep) && errno == EINTR); |
|
51 } |
|
52 |
|
53 void set_max_priority(void) { |
|
54 struct sched_param sched; |
|
55 memset(&sched, 0, sizeof(sched)); |
|
56 // Use FIFO scheduler with highest priority for the lowest chance of the kernel context switching. |
|
57 sched.sched_priority = sched_get_priority_max(SCHED_FIFO); |
|
58 sched_setscheduler(0, SCHED_FIFO, &sched); |
|
59 } |
|
60 |
|
61 void set_default_priority(void) { |
|
62 struct sched_param sched; |
|
63 memset(&sched, 0, sizeof(sched)); |
|
64 // Go back to default scheduler with default 0 priority. |
|
65 sched.sched_priority = 0; |
|
66 sched_setscheduler(0, SCHED_OTHER, &sched); |
|
67 } |