udoo neo pwm functions

Discussion in 'UDOO NEO' started by qbot, May 26, 2016.

  1. qbot

    qbot New Member

    Joined:
    May 20, 2016
    Messages:
    2
    Likes Received:
    0
    Hi,

    Does anyone know how to use the functions below for pwm on the udoo neo?

    The functions are: (those functions are in pwm.c and pwm_ifc, I think it is given when you install the udoo neo board, it is in UDOO >hardware >solox >1.6.5-00032-g71841f7> variants>udooneo> pwm.c or pwm_ifc)

    Code:
    /*
    //! @brief Clock sources for the PWM.
    kPwmClockSourceNone = 0,
    kPwmClockSourceIpg = 1,
    kPwmClockSourceCkih = 2,
    kPwmClockSourceCkil = 3
    */
    void pwm_set_clock(uint16_t instance, uint16_t clkSrc)
    {
        MY_HW_PWM_PWMCR(instance).B.CLKSRC = clkSrc;
    }
    
    // Set period from 0 to 0xFFFE
    // when this function is called, must be called pwm_set_tesolution
    void pwm_set_period(uint16_t instance, uint16_t period)
    {
        MY_HW_PWM_PWMPR(instance).B.PERIOD = period;
        pwmSetup[instance].period = period;
    }
    
    void pwm_enable(uint16_t instance)
    {
        MY_HW_PWM_PWMCR(instance).B.EN = 1;
        pwmSetup[instance].enabled = 1;
    }
    
    void pwm_disable(uint16_t instance)
    {
        // Disable PWM interrupt
        //fefrpwm_free_interrupt(instance);
        MY_HW_PWM_PWMCR(instance).B.EN = 0;
        pwmSetup[instance].enabled = 0;
    }
    
    // Set prescale after checking its range.
    // 0 - divide by 1, 1 - divide by 2, 4095 divide by 4096
    void pwm_set_prescaler(uint16_t instance, uint16_t prescale)
    {
        if (prescale > PWM_MAX_PRESCALER) {
            printf("Invalid prescaler value.\n");
            return;
        }
    
        MY_HW_PWM_PWMCR(instance).B.PRESCALER = prescale;
    }
    
    
    What the function pwm_set_clock is for?
    What the function pwm_set_period is for?
    How can I define a duty cycle of 50%?

    Where should I put the enable and disable functions? in setup or loop or in an other function?
    Should I put set_prescaler, set_period and set_clock in the setup function?

    I would like to define a pwm signal with those functions. Does someone know how to do it?
     
    Last edited by a moderator: May 31, 2016
  2. ektor5

    ektor5 Administrator Staff Member

    Joined:
    Jul 1, 2013
    Messages:
    97
    Likes Received:
    48
    Hi there,
    They simply set the CR (Control Register) of the PWM module. Check the Reference Manual of the IMX6SX for further information about it.

    I suggest you to get a look to the analogWrite function in wiring_analog.c and do something similar.

    Here an example I tried: I managed to vary the frequency of the PWM using pwm_set_period, but I don't know if it's the better way to go.

    Code:
    #include "Arduino.h"
    #include "pwm_ifc.h"
    
    int ledPin = 9;
    
    void setup() {
      // set duty cycle at 50%
      analogWrite(ledPin, 128);
    }
    
    void loop() {
      // do a conversion
      uint16_t pwmChn = ardPinToPwmChn[ledPin];
      
      // fade  the period from min to max in increments of 5 points:
      for (int fadeValue = 0 ; fadeValue <= 100000; fadeValue += 1000) {
       
         pwm_set_period(pwmChn, fadeValue);
    
         delay(30);
      }
    
      // fade out the period from max to min in increments of 5 points:
      for (int fadeValue = 100000 ; fadeValue >= 0; fadeValue -= 1000) {
         // sets the value (range from 0 to 255):
         pwm_set_period(pwmChn, fadeValue);
         delay(30);
      }
    }
    
    ek5
     

Share This Page