Hardware Reference
In-Depth Information
Finally, the clock source is set to use the oscillator, and the clock is enabled:
ugclk[PWMCLK_CNTL] = 0x5A000011;
After this, GPIO 18 is configured for ALT function 5, to gain access to the PWM
peripheral:
INP_GPIO(18); /* Set ALT = 0 */
SET_GPIO_ALT(18,5); /* Or in '5' */
The way the
SET_GPIO_ALT()
macro is defined requires that the
INP_GPIO()
macro
be used first. The
INP_GPIO()
macro clears the ALT function bits so that
SET_GPIO_ALT()
can
or
in the new bits (the value 5, in this case).
The remaining steps ready the PWM peripheral:
pwm_ctl->MODE1 = 0; /* PWM mode */
pwm_ctl->RPTL1 = 0;
pwm_ctl->SBIT1 = 0;
pwm_ctl->POLA1 = 0;
pwm_ctl->USEF1 = 0;
pwm_ctl->MSEN1 = 0; /* PWM mode */
pwm_ctl->CLRF1 = 1;
Now, at this point, the PWM peripheral is almost ready to go. It needs the ratio
N
M
and then to be enabled. This is done in the routine
pwm_ratio()
:
static void
pwm_ratio(unsigned n,unsigned m) {
...
This function allows the
N
M
ratio be changed without having to fully reinitialize the
other aspects, including the clock. With our CPU percent-busy function, this ratio will
be changing often.
pwm_ctl->PWEN1 = 0; /* Disable */
*pwm_rng1 = m;
*pwm_dat1 = n;
After initialization, the PWM peripheral is already disabled. But the first step here
disables it, because it may be running when the ratio is being changed. The following pair
of statements put the value of M into the PWM register
RNG1
, while N goes into the
DAT1
register.