PWM: PWM.c File Reference

Modbus RTU XMC47

PWM.c File Reference

Detailed Description

Date:
2016-07-28

NOTE: This file is generated by DAVE. Any manual modification done to this file will be lost when the code is regenerated.

Definition in file PWM.c.

Functions

DAVE_APP_VERSION_t PWM_GetAppVersion (void)
 Get PWM APP version.
PWM_STATUS_t PWM_Init (PWM_t *const handle_ptr)
 Initializes the PWM APP.
void PWM_Start (PWM_t *const handle_ptr)
 Starts the PWM generation.
void PWM_Stop (PWM_t *const handle_ptr)
 Stops the PWM generation.
PWM_STATUS_t PWM_SetDutyCycle (PWM_t *const handle_ptr, uint32_t duty_cycle)
 Configure the PWM duty cycle.
PWM_STATUS_t PWM_SetFreq (PWM_t *const handle_ptr, uint32_t pwm_freq_hz)
 Configures the PWM Frequency.
PWM_STATUS_t PWM_SetFreqAndDutyCycle (PWM_t *const handle_ptr, uint32_t pwm_freq_hz, uint32_t duty_cycle)
 Configures the PWM Frequency and duty cycle.
bool PWM_GetTimerStatus (PWM_t *const handle_ptr)
 Gets the corresponding timer status.
bool PWM_GetInterruptStatus (PWM_t *const handle_ptr, PWM_INTERRUPT_t pwm_interrupt)
 Gets the corresponding interrupt status.
void PWM_ClearEvent (PWM_t *const handle_ptr, PWM_INTERRUPT_t pwm_interrupt)
 Clears the PWM related interrupt.
void PWM_SetPassiveLevel (PWM_t *const handle_ptr, PWM_OUTPUT_PASSIVE_LEVEL_t pwm_output_passive_level)
 Configure the passive level of the PWM output waveform.
PWM_STATUS_t PWM_SetPeriodMatchValue (PWM_t *const handle_ptr, uint32_t period_match_value)
 Loads the required period match value.

Function Documentation

void PWM_ClearEvent ( PWM_t *const  handle_ptr,
PWM_INTERRUPT_t  pwm_interrupt 
)

Clears the PWM related interrupt.

Parameters:
handle_ptrConstant pointer to the handle structure PWM_t
pwm_interruptInterrupt to acknowledge.
Returns:
void
Description:
Clears the CCUx related interrupt. When an interrupt occurs it must be acknowledged by clearing the respective flag in the hardware. Clears the interrupt type PWM_INTERRUPT_t.
   // Drag 2 INTERRUPT APPs into the project. In the PWM APPs UI editor enable compare match event and period
   // match event. Goto the Signal connectivity window and connect the event_compare_match and event_period_match to
   // the 2 different INTERRUPT APPs. Give the handler for the compare match interrupt as PWM_compare_match_interrupt
   // and the handler for the period match interrupt as PWM_period_match_interrupt
   #include <DAVE.h>
   uint32_t period_count;

   void PWM_period_match_interrupt(void)
   {
     static uint32_t frequency = 1000; //start frequency 1Khz
     PWM_ClearEvent(&PWM_0, PWM_INTERRUPT_PERIODMATCH);
     period_count++;

     if(period_count == 35000) // wait to get 40K pwm cycles then increment the frequency by 2kHz
     {
       frequency += 2000;
       PWM_SetFreq(&PWM_0,frequency);
       period_count = 0;
       if(frequency > 30000000)
         frequency = 1000;
     }
   }

   void PWM_compare_match_interrupt(void)
   {
     PWM_ClearEvent(&PWM_0, PWM_INTERRUPT_COMPAREMATCH);
   }

   int main(void)
   {

     DAVE_Init();

     PWM_Start(&PWM_0);
     while(1);
     return 0;
   }

Definition at line 943 of file PWM.c.

References PWM_HandleType::ccu4_slice_ptr, PWM_HandleType::ccu8_slice_ptr, and PWM_HandleType::timer_type.

bool PWM_GetInterruptStatus ( PWM_t *const  handle_ptr,
PWM_INTERRUPT_t  pwm_interrupt 
)

Gets the corresponding interrupt status.

Parameters:
handle_ptrConstant pointer to the handle structure PWM_t
pwm_interruptInterrupt to get status.
Returns:
bool returns true if the pwm_interrupt has occured else returns false.
Description:
Returns the status of the corresponding interrupt. Reads the appropriate flag and would return true if the event was asserted.
   // Drag 1 INTERRUPT APP into the project. In the PWM APPs UI editor enable compare match event and period
   // match event. Goto the Signal connectivity window and connect the event_compare_match and event_period_match to
   // the same INTERRUPT APP and give the handler as PWM_compare_period_match_interrupt.
   #include <DAVE.h>
   uint32_t period_count;

   void PWM_compare_period_match_interrupt(void)
   {
     if(PWM_GetInterruptStatus(&PWM_0, PWM_INTERRUPT_PERIODMATCH))
     {
         PWM_Stop(&PWM_0); // A single shot PWM generated
     }
     PWM_ClearEvent(&PWM_0, PWM_INTERRUPT_COMPAREMATCH);
   }

   int main(void)
   {

     DAVE_Init();

     PWM_Start(&PWM_0);
     while(1);
     return 0;
   }

Definition at line 917 of file PWM.c.

References PWM_HandleType::ccu4_slice_ptr, PWM_HandleType::ccu8_slice_ptr, and PWM_HandleType::timer_type.

bool PWM_GetTimerStatus ( PWM_t *const  handle_ptr)

Gets the corresponding timer status.

Parameters:
handle_ptrConstant pointer to the handle structure PWM_t
Returns:
bool
returns true if timer is running , false if the timer is idle.
Description:
Returns the state of the timer. Would return a false if the timer is not running. A call to this API results in invalid outputs if invoked before PWM_Init().
   #include <DAVE.h>

  int main(void)
  {
   DAVE_Init();

   if(PWM_GetTimerStatus(&PWM_0))
   {
     PWM_Stop(&PWM_0);
   }
   while(1);
   return 0;
  }

Definition at line 891 of file PWM.c.

References PWM_HandleType::ccu4_slice_ptr, PWM_HandleType::ccu8_slice_ptr, and PWM_HandleType::timer_type.

PWM_STATUS_t PWM_Init ( PWM_t *const  handle_ptr)

Initializes the PWM APP.

Parameters:
handle_ptrConstant pointer to the handle structure PWM_t
Returns:
PWM_STATUS_t status of the initialization.
Description:
Initializes the PWM APP. This initializes the CCUx slice to compare mode of operation. Configures required events, GPIO pin as output. It will configure CCU4 or CCU8 slice registers with the selected PWM parameters. If PWM generation is set to start after initialization then after the CCUx related initialization is completed the PWM output will start.
  #include <DAVE.h>

  int main(void)
  {
   DAVE_Init(); //PWM_Init() is called by DAVE_Init().
    while(1);
    return 0;
  }

Definition at line 747 of file PWM.c.

References PWM_STATUS_FAILURE, and PWM_HandleType::timer_type.

PWM_STATUS_t PWM_SetDutyCycle ( PWM_t *const  handle_ptr,
uint32_t  duty_cycle 
)

Configure the PWM duty cycle.

Parameters:
handle_ptrConstant pointer to the handle structure PWM_t
duty_cycleDuty cycle in percentage.
The values are scaled by a factor of 100.
Range: [0(d) to 10000(d)] Where 1(d) represents 0.01% duty cycle
10000(d) represents 100% duty cycle
Returns:
Returns PWM_STATUS_FAILURE if the duty_cycle exceeds the valid range.
Description:
Configure the PWM duty cycle by changing the compare match values. The API would configure the duty cycle for the given frequency. The duty_cycle is a scaled parameter where a single value change would result in a 0.01% change.
Note:
This is a fixed point implementation. It is expected that there are resolution losses due to scaling in the API. At higher frequencies it may not be possible to achieve the required duty cycle due to the hardware limitation.
 #include <DAVE.h>

 int main(void)
 {

    uint32_t Counter;

    DAVE_Init();

    for(Counter = 0;Counter <=0xFFFF;Counter++);

    // Change duty cycle value to 60.00%
    if(PWM_SetDutyCycle(&PWM_0,6000))
    {
      while(1);
    }

    // Wait to observe the waveform
    for(Counter = 0;Counter <=0xFFFF;Counter++);

    // Change duty cycle value to 54.26%
    if(PWM_SetDutyCycle(&PWM_0,5426))
    {
      while(1);
    }

    // Wait to observe the waveform
    for(Counter = 0;Counter <=0xFFFF;Counter++);

    // Change duty cycle value to an invalid duty 100.26%
    if(PWM_SetDutyCycle(&PWM_0,10026))
    {
      while(1);
    }
    while(1);
    return 0;
}

Definition at line 816 of file PWM.c.

References PWM_STATUS_FAILURE, and PWM_HandleType::timer_type.

PWM_STATUS_t PWM_SetFreq ( PWM_t *const  handle_ptr,
uint32_t  pwm_freq_hz 
)

Configures the PWM Frequency.

Parameters:
handle_ptrConstant pointer to the handle structure PWM_t
PwmFreqHzFrequency value in Hz.
Range: [1(d) to 60000000(d)]
Returns:
PWM_STATUS_t Would return PWM_STATUS_FAILURE if the PwmFreqHz exceeded the maximum frequency achievable.
Description:
Configures the frequency of the PWM signal. The PWM duty cycle remains unchanged. Calculates the least possible resolution(Prescaler) of the CCUx Timer. Using this Prescaler it would calculate the value for the period register and the compare register. Adjusts the compare match value according to the frequency and the existing duty cycle.
  #include <DAVE.h>

  int main(void)
  {
     PWM_STATUS_t pwm_setfreq_status;
     uint32_t Counter;

     DAVE_Init();

     // Change duty cycle value to 20%.
         PWM_SetDutyCycle(&PWM_0, 2000);

    // Change PWM frequency to 100kHz
    pwm_setfreq_status = PWM_SetFreq(&PWM_0,100000);
    if(PWM_STATUS_FAILURE == pwm_setfreq_status)
    {
        // frequency couldn't be set
        while(1);
    }

    // Wait to observe the waveform
    for(Counter = 0;Counter <=0xFFFF;Counter++);

        // Change duty cycle value to 60%.
    PWM_SetDutyCycle(&PWM_0, 6000);


    while(1);
    return 0;
  }

Definition at line 841 of file PWM.c.

References PWM_STATUS_FAILURE, and PWM_HandleType::timer_type.

PWM_STATUS_t PWM_SetFreqAndDutyCycle ( PWM_t *const  handle_ptr,
uint32_t  pwm_freq_hz,
uint32_t  duty_cycle 
)

Configures the PWM Frequency and duty cycle.

Parameters:
handle_ptrConstant pointer to the handle structure PWM_t
PwmFreqHzFrequency value in Hz.
Range: [1(d) to 60000000(d)]
duty_cycleDuty cycle in percentage.
The values are scaled by a factor of 100.
Range: [0(d) to 10000(d)] Where 1(d) represents 0.01% duty cycle
10000(d) represents 100% duty cycle
Returns:
Return PWM_STATUS_FAILURE if the PwmFreqHz exceeded the maximum frequency achievable.
Description:
Configures the frequency and duty cycle together. Calculates the least possible resolution(Prescaler) of the CCUx Timer. Using this Prescaler it would calculate the value for the period register. Using the duty_cycle the compare register values would be calculated.
Note:
This is a fixed point implementation for duty cycle. It is expected that there are resolution losses due to scaling of the duty cycle in the API. At higher frequencies it may not be possible to achieve the required duty cycle due to the hardware limitation. If the input frequency to the API is very high (> 30MHz) due to integer divisions, the desired frequency might not be set.
  #include <DAVE.h>

  int main(void)
  {
     PWM_STATUS_t pwm_setfreq_status;
     uint32_t Counter;

     DAVE_Init();

        // Change duty cycle value to 90.90%.
    PWM_SetDutyCycle(&PWM_0, 9090);

        // Wait to observe the waveform
    for(Counter = 0;Counter <=0xFFFF;Counter++);

    // Change PWM frequency to 100kHz and duty to 20%
    pwm_setfreq_status = PWM_SetFreqAndDutyCycle(&PWM_0,100000,2000);
    if(PWM_STATUS_FAILURE == pwm_setfreq_status)
    {
        // frequency couldn't be set
        while(1);
    }

    while(1);
    return 0;
}

Definition at line 866 of file PWM.c.

References PWM_STATUS_FAILURE, and PWM_HandleType::timer_type.

void PWM_SetPassiveLevel ( PWM_t *const  handle_ptr,
PWM_OUTPUT_PASSIVE_LEVEL_t  pwm_output_passive_level 
)

Configure the passive level of the PWM output waveform.

Parameters:
handle_ptrConstant pointer to the handle structure PWM_t
pwm_output_passive_levelPassive level LOW or HIGH.
Returns:
void
Description:
Configure the passive level for the PWM signal. If ::PWM_OutputPassiveLevelType::PWM_OUTPUT_PASSIVE_LEVEL_HIGH is selected the output of the PWM will be high before the compare match occurs after that it would be set to low. If ::PWM_OutputPassiveLevelType::PWM_OUTPUT_PASSIVE_LEVEL_LOW is selected the output of the PWM will be low before the compare match occurs after that it would be set to high.
   #include <DAVE.h>

   int main(void)
   {

    uint32_t Counter;

    DAVE_Init();

    for(Counter = 0;Counter <=0xFFFF;Counter++);

    PWM_SetPassiveLevel(&PWM_0, PWM_OUTPUT_PASSIVE_LEVEL_HIGH);
    for(Counter = 0;Counter <=0xFFFF;Counter++);
    PWM_SetPassiveLevel(&PWM_0, PWM_OUTPUT_PASSIVE_LEVEL_LOW);
    for(Counter = 0;Counter <=0xFFFF;Counter++);

    while(1);
    return 0;
   }

Definition at line 964 of file PWM.c.

References PWM_HandleType::ccu4_kernel_ptr, PWM_HandleType::ccu4_slice_ptr, PWM_HandleType::ccu8_kernel_ptr, PWM_HandleType::ccu8_slice_ptr, PWM_HandleType::shadow_mask, and PWM_HandleType::timer_type.

PWM_STATUS_t PWM_SetPeriodMatchValue ( PWM_t *const  handle_ptr,
uint32_t  period_match_value 
)

Loads the required period match value.

Parameters:
handle_ptrConstant pointer to the handle structure PWM_t
period_match_valuevalue which needs to be loaded into the period register. Range: [0x0 to 0xFFFF]
Returns:
void
Description:
Configures or loads the required period value into the period register. This API would also calculate the compare register value (w.r.t period_match_value ) to maintain the duty cycle.
   #include <DAVE.h>

   int main(void)
   {

    uint32_t Counter;

    DAVE_Init();

    PWM_SetFreq(&PWM_1,1U);// Set the required frequency as 1Hz

    for(Counter = 0;Counter <=0xFFFF;Counter++);

    PWM_SetPeriodMatchValue(&PWM_0,PWM_0.period_value/2); // 2Hz
    for(Counter = 0;Counter <=0xFFFF;Counter++);
    PWM_SetPeriodMatchValue(&PWM_0,PWM_0.period_value*2); //1Hz
    for(Counter = 0;Counter <=0xFFFF;Counter++);

    while(1);
    return 0;
   }

Definition at line 995 of file PWM.c.

References PWM_HandleType::ccu4_kernel_ptr, PWM_HandleType::ccu4_slice_ptr, PWM_HandleType::ccu8_kernel_ptr, PWM_HandleType::ccu8_slice_ptr, PWM_HandleType::compare_value, PWM_HandleType::duty_cycle, PWM_HandleType::period_value, PWM_STATUS_FAILURE, PWM_STATUS_SUCCESS, PWM_HandleType::shadow_mask, PWM_HandleType::state, and PWM_HandleType::timer_type.

void PWM_Start ( PWM_t *const  handle_ptr)

Starts the PWM generation.

Parameters:
handle_ptrConstant pointer to the handle structure PWM_t
Returns:
void
Description:
Start the selected PWM generation. It is needed to be called if "Start after initialization" is unchecked in the UI. Enables the clock for the CCUx slice and also starts the timer operation. Invoke this API only after initialization and when the timer is not running. If invoked outside these conditions, the API will have no effect.
  #include <DAVE.h>

  int main(void)
  {
   DAVE_Init();
   //This needs to be called if "Start after initialization" is unchecked
   if(!PWM_GetTimerStatus(&PWM_0))
   {
     PWM_Start(&PWM_0);
   }
   while(1);
   return 0;
  }

Definition at line 773 of file PWM.c.

References PWM_HandleType::timer_type.

void PWM_Stop ( PWM_t *const  handle_ptr)

Stops the PWM generation.

Parameters:
handle_ptrConstant pointer to the handle structure PWM_t
Returns:
void
Description:
Stop the selected PWM generation. Disables the clock for the CCUx slice and also stops the timer operation. Invoke this API only after initialization and when the timer is running. If invoked outside these conditions, the API will have no effect.
   #include <DAVE.h>

  int main(void)
  {
   DAVE_Init();

   if(PWM_GetTimerStatus(&PWM_0))
   {
     PWM_Stop(&PWM_0);
   }
   while(1);
   return 0;
  }

Definition at line 794 of file PWM.c.

References PWM_HandleType::timer_type.

Go to the source code of this file.