Hardware Reference
In-Depth Information
Software Based CPU Percent-Busy Display
Without command-line arguments, the softpwm command defaults to being a CPU
percent-busy driver. It drives pin GPIO 22 (GEN3), which when attached to a meter as
shown in Figure 9-1 , will display CPU utilization.
$ ./softpwm
CPU Meter Mode :
6.5%
Press ^C after the fascination of the CPU meter wears off.
1 /*********************************************************************
2 * softpwm.c Software PWM example program
3 *********************************************************************/
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <fcntl.h>
9 #include <sys/mman.h>
10 #include <errno.h>
11 #include <string.h>
12 #include <math.h>
13 #include <pthread.h>
14
15 #include "gpio_io.c"
16
17 typedef struct {
18 int gpio; /* GPIO out put pin */
19 double freq; /* Operating frequency */
20 unsigned n; /* The N in N/M */
21 unsigned m; /* The M in N/M */
22 pthread_t thread; /* Controlling thread */
23 volatile char chgf; /* True when N/M changed */
24 volatile char stopf; /* True when thread to stop */
25 } PWM;
26
27 /*
28 * Timed wait from a float
29 */
30 static void
31 float_wait(double seconds) {
32 fd_set mt ;
33 struct timeval time out;
34 int rc;
35
 
Search WWH ::




Custom Search