embeded/raspberry pi2015. 10. 16. 09:46

데이터 시트상으로는 PWM 1과 PWM 2 채널 독립적으로

모드를 설정하여 사용이 가능해야 하나...

wiringPi에서는 편의상.. 걍 묶어 버렸다!!!!


wiringPi/wiringPi $ vi wiringPi.c

void pwmSetMode (int mode)

{

  if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))

  {

    if (mode == PWM_MODE_MS)

      *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE | PWM0_MS_MODE | PWM1_MS_MODE ;

    else

      *(pwm + PWM_CONTROL) = PWM0_ENABLE | PWM1_ENABLE ;

  }

}


void pwmSetRange (unsigned int range)

{

  if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))

  {

    *(pwm + PWM0_RANGE) = range ; delayMicroseconds (10) ;

    *(pwm + PWM1_RANGE) = range ; delayMicroseconds (10) ;

  }

}


void pwmSetClock (int divisor)

{

  uint32_t pwm_control ;

  divisor &= 4095 ;


  if ((wiringPiMode == WPI_MODE_PINS) || (wiringPiMode == WPI_MODE_PHYS) || (wiringPiMode == WPI_MODE_GPIO))

  {

    if (wiringPiDebug)

      printf ("Setting to: %d. Current: 0x%08X\n", divisor, *(clk + PWMCLK_DIV)) ;


    pwm_control = *(pwm + PWM_CONTROL) ;                // preserve PWM_CONTROL


// We need to stop PWM prior to stopping PWM clock in MS mode otherwise BUSY

// stays high.


    *(pwm + PWM_CONTROL) = 0 ;                          // Stop PWM


// Stop PWM clock before changing divisor. The delay after this does need to

// this big (95uS occasionally fails, 100uS OK), it's almost as though the BUSY

// flag is not working properly in balanced mode. Without the delay when DIV is

// adjusted the clock sometimes switches to very slow, once slow further DIV

// adjustments do nothing and it's difficult to get out of this mode.


    *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x01 ;        // Stop PWM Clock

      delayMicroseconds (110) ;                 // prevents clock going sloooow


    while ((*(clk + PWMCLK_CNTL) & 0x80) != 0)  // Wait for clock to be !BUSY

      delayMicroseconds (1) ;


    *(clk + PWMCLK_DIV)  = BCM_PASSWORD | (divisor << 12) ;


    *(clk + PWMCLK_CNTL) = BCM_PASSWORD | 0x11 ;        // Start PWM clock

    *(pwm + PWM_CONTROL) = pwm_control ;                // restore PWM_CONTROL


    if (wiringPiDebug)

      printf ("Set     to: %d. Now    : 0x%08X\n", divisor, *(clk + PWMCLK_DIV)) ;

  }

}




'embeded > raspberry pi' 카테고리의 다른 글

라즈베리 파이 CLCD 라이브러리  (0) 2015.11.03
bcm2835 pwm 데이터시트  (0) 2015.10.16
라즈베리 파이 GPIO 유틸 PWM  (0) 2015.10.14
DHT-11 DHT-22 RHT-03  (0) 2015.10.14
dSPIN_raspi / L6470 / sparkfun  (0) 2015.10.12
Posted by 구차니