Hardware Reference
In-Depth Information
258 return 0 ;
259 }
260
261 /* End pwm.c */
Software PWM Program
The program softpwm works from the command line very similarly to the hardware
PWM program pwm . One difference, however, is that the software PWM requires that the
program continue to run to maintain the signal. The hardware program can exit and leave
the PWM peripheral running.
The design of the program differs in that a thread is used for each PWM signal being
maintained. With a little bit of work, the softpwm.c module could be formed into a PWM
software library. The data type PWM is created with the same idea as the stdio FILE type:
typedef struct {
int gpio; /* GPIO output pin */
double freq; /* Operating frequency */
unsigned n; /* The N in N/M */
unsigned m; /* The M in N/M */
pthread_t thread; /* Controlling thread */
volatile char chgf; /* True when N/M changed */
volatile char stopf; /* True when thread to stop */
} PWM;
The comments identify the purpose of the structure object members. The last two
members are flags that are used to control the thread.
The function pwm_open() , establishes the GPIO line and the PWM frequency, and
returns the PWM control block. Note that no thread is started just yet:
PWM *
pwm_open(int gpio,double freq) {
PWM *pwm = malloc(sizeof *pwm);
pwm->gpio = gpio;
pwm->freq = freq;
pwm->thread = 0;
pwm->n = pwm->m = 0;
pwm->chgf = 0;
pwm->stopf = 0;
INP_GPIO(pwm->gpio);
OUT_GPIO(pwm->gpio);
return pwm;
}
 
Search WWH ::




Custom Search