-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystimerops.h
More file actions
executable file
·115 lines (92 loc) · 3.05 KB
/
systimerops.h
File metadata and controls
executable file
·115 lines (92 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <unistd.h> /* read(), close() */
#include <fcntl.h> /* open() */
#include <sched.h> /* Process priority changing mechanism via scheduler */
#include <string.h> /* memset() */
/*
* Attempt to set a high (realtime) priority schedulling for the running program
* from Gordon Henderson's WiringPi project
*********************************************************************************
*/
void piHiPri (unsigned int priority)
{
struct sched_param scheduler ;
/* Initialisation - clears the memory held by the structure */
memset (&scheduler, 0, sizeof(scheduler)) ;
/* Either set the given priority or clip it at maximum */
if (priority > sched_get_priority_max (SCHED_RR))
scheduler.sched_priority = sched_get_priority_max (SCHED_RR) ;
else
scheduler.sched_priority = priority ;
/* Apply changes */
sched_setscheduler (0, SCHED_RR, &scheduler) ;
}
/*
*
*********************************************************************************
*/
ssize_t read_from_device (const char *file, unsigned int *value)
{
int fd ; /* file descriptor */
char buffer[32] ; /* char driver requires char buffer */
size_t bytes = sizeof (buffer) ; /* size of the buffer */
/* Open the given file and check return code */
if ((fd = open (file, O_RDONLY)) < 0)
{
fprintf(stderr, "open() failed.\n") ;
exit(1) ;
}
/* Read a number of bytes from the file and put the contents in the buffer */
ssize_t read_bytes = read (fd, buffer, bytes) ;
/* Copy the buffer contents to the value to return */
sscanf (buffer, "%u", value) ;
/* DEBUGGING: Check return value of our read */
#ifdef DEBUG_ST
if (read_bytes == 0)
printf ("Nothing more to read. EOF.\n") ;
else
printf ("%s :\nBytes read: %zu\nContents: %s\n", file, read_bytes, buffer) ;
#endif
/* Close the file and check return code */
if (close (fd) < 0)
{
fprintf(stderr, "close() failed.\n") ;
exit(1) ;
}
/* Might be useful.. */
return read_bytes ;
}
/*
*
*********************************************************************************
*/
ssize_t write_to_device (const char *file, unsigned int *value)
{
int fd ; /* file descriptor */
char buffer[32] ; /* char driver requires char buffer */
size_t bytes = sizeof (buffer) ; /* size of the buffer */
/* Open the given file and check return code */
if ((fd = open (file, O_WRONLY)) < 0)
{
fprintf(stderr, "open() failed.\n") ;
exit(1) ;
}
/* Copy the value to be written to the buffer */
snprintf (buffer, bytes, "%u", *value) ;
/* Write the number of bytes from the buffer into the file */
ssize_t wrote_bytes = write (fd, buffer, bytes) ;
/* DEBUGGING: Check the return value of our write */
#ifdef DEBUG_ST
if (wrote_bytes == 0)
printf ("%s :\nCould not write any bytes. EOF.\n", file) ;
else
printf ("%s :\nBytes wrote: %zu\nWrote: %s\n", file, wrote_bytes, buffer) ;
#endif
/* Close the file and check return code */
if (close (fd) < 0)
{
fprintf(stderr, "close() failed.\n") ;
exit(1) ;
}
/* Again, might be useful.. */
return wrote_bytes ;
}