pwm.h File Reference


Detailed Description

PWM driver for AVR32 UC3.

This file defines a useful set of functions for the PWM interface on AVR32 devices.

Author:
Atmel Corporation: http://www.atmel.com
Support and FAQ: http://support.atmel.no/

Definition in file pwm.h.

#include <avr32/io.h>

Go to the source code of this file.

Data Structures

struct  pwm_opt_t
 Input parameters when initializing a PWM channel. More...

Defines

#define PWM_FAILURE   -1
 Value returned by function when it was unable to complete successfully for some unspecified reason.
#define PWM_INVALID_ARGUMENT   1
 Value returned by function when the channel number is invalid.
#define PWM_INVALID_INPUT   1
 Value returned by function when the input paramters are out of range.
#define PWM_MODE_CENTER_ALIGNED   1
 Operate PWM channel in center aligned mode.
#define PWM_MODE_LEFT_ALIGNED   0
 Operate PWM channel in left aligned mode.
#define PWM_POLARITY_HIGH   1
 PWM channel starts output high level.
#define PWM_POLARITY_LOW   0
 PWM channel starts output low level.
#define PWM_SUCCESS   0
 Value returned by function when it completed successfully.
#define PWM_UPDATE_DUTY   0
 PWM channel write in CUPDx updates duty cycle at the next period start event.
#define PWM_UPDATE_PERIOD   1
 PWM channel write in CUPDx updates period at the next period start event.

Functions

int pwm_async_update_channel (unsigned int channel_id, const avr32_pwm_channel_t *pwm_channel)
 Update channel register CPRDx or CDTYx without synchronizing with the PWM period. This function uses the CUPDx register as a double buffer for the period or the duty cycle. Only the first 20 bits of cupd are significant.
int pwm_channel_init (unsigned int channel_id, const avr32_pwm_channel_t *pwm_channel)
 Initialize a specific PWM channel.
int pwm_init (const pwm_opt_t *opt)
 This function initialize the PWM controller (mode register) and disable the interrupt.
int pwm_start_channels (unsigned long channels_bitmask)
 Start PWM channels.
int pwm_stop_channels (unsigned long channels_bitmask)
 Stop PWM channels.
int pwm_sync_update_channel (unsigned int channel_id, const avr32_pwm_channel_t *pwm_channel)
 Update channel register CPRDx or CDTYx by forcing synchronization with the PWM period. This function uses the CUPDx register as a double buffer for the period or the duty cycle. Only the first 20 bits of cupd are significant.


Define Documentation

#define PWM_FAILURE   -1

Value returned by function when it was unable to complete successfully for some unspecified reason.

Definition at line 60 of file pwm.h.

#define PWM_INVALID_ARGUMENT   1

Value returned by function when the channel number is invalid.

Definition at line 66 of file pwm.h.

Referenced by pwm_channel_init().

#define PWM_INVALID_INPUT   1

Value returned by function when the input paramters are out of range.

Definition at line 63 of file pwm.h.

Referenced by pwm_async_update_channel(), pwm_channel_init(), pwm_init(), pwm_start_channels(), pwm_stop_channels(), and pwm_sync_update_channel().

#define PWM_MODE_CENTER_ALIGNED   1

Operate PWM channel in center aligned mode.

Definition at line 72 of file pwm.h.

#define PWM_MODE_LEFT_ALIGNED   0

Operate PWM channel in left aligned mode.

Definition at line 69 of file pwm.h.

Referenced by main().

#define PWM_POLARITY_HIGH   1

PWM channel starts output high level.

Definition at line 78 of file pwm.h.

#define PWM_POLARITY_LOW   0

PWM channel starts output low level.

Definition at line 75 of file pwm.h.

Referenced by main().

#define PWM_SUCCESS   0

Value returned by function when it completed successfully.

Definition at line 56 of file pwm.h.

Referenced by pwm_async_update_channel(), pwm_channel_init(), pwm_init(), pwm_start_channels(), pwm_stop_channels(), and pwm_sync_update_channel().

#define PWM_UPDATE_DUTY   0

PWM channel write in CUPDx updates duty cycle at the next period start event.

Definition at line 81 of file pwm.h.

Referenced by main().

#define PWM_UPDATE_PERIOD   1

PWM channel write in CUPDx updates period at the next period start event.

Definition at line 84 of file pwm.h.


Function Documentation

int pwm_async_update_channel ( unsigned int  channel_id,
const avr32_pwm_channel_t *  pwm_channel 
)

Update channel register CPRDx or CDTYx without synchronizing with the PWM period. This function uses the CUPDx register as a double buffer for the period or the duty cycle. Only the first 20 bits of cupd are significant.

Parameters:
channel_id The channel identifier (0 to max channel-1)
*pwm_channel Pointer to PWM channel struct avr32_pwm_channel_t
Returns:
PWM_SUCCESS or PWM_INVALID_INPUT
Warning:
Calling this function several times in a row may result in some update values never being issued to PWM if some external synchronizing mechanism like an interrupt is not used.
Note:
This update function should be preferred when updating a PWM channel from an interrupt handler.

Definition at line 134 of file pwm.c.

References PWM_INVALID_INPUT, and PWM_SUCCESS.

00135 {
00136   volatile avr32_pwm_t *pwm = &AVR32_PWM;
00137 
00138   if (channel_id > AVR32_PWM_LINES_MSB)
00139      return PWM_INVALID_INPUT;
00140 
00141   pwm->channel[channel_id].cmr= pwm_channel->cmr;   // Channel mode register: update of the period or duty cycle.
00142   pwm->channel[channel_id].cupd= pwm_channel->cupd; // Channel update CPRDx or CDTYx according to CPD value in CMRx.
00143 
00144   return PWM_SUCCESS;
00145 }

int pwm_channel_init ( unsigned int  channel_id,
const avr32_pwm_channel_t *  pwm_channel 
)

Initialize a specific PWM channel.

Parameters:
channel_id The channel identifier mask
*pwm_channel Pointer to PWM channel struct avr32_pwm_channel_t
Returns:
PWM_SUCCESS, PWM_INVALID_INPUT or PWM_INVALID_ARGUMENT

Definition at line 79 of file pwm.c.

References PWM_INVALID_ARGUMENT, PWM_INVALID_INPUT, and PWM_SUCCESS.

Referenced by main().

00080 {
00081   volatile avr32_pwm_t *pwm = &AVR32_PWM;
00082 
00083   if (pwm_channel == 0) // Null pointer.
00084     return PWM_INVALID_ARGUMENT;
00085   if (channel_id > AVR32_PWM_LINES_MSB) // Control input values.
00086     return PWM_INVALID_INPUT;
00087 
00088   pwm->channel[channel_id].cmr= pwm_channel->cmr;   // Channel mode.
00089   pwm->channel[channel_id].cdty= pwm_channel->cdty; // Duty cycle, should be < CPRD.
00090   pwm->channel[channel_id].cprd= pwm_channel->cprd; // Channel period.
00091 
00092   return PWM_SUCCESS;
00093 }

int pwm_init ( const pwm_opt_t opt  ) 

This function initialize the PWM controller (mode register) and disable the interrupt.

Parameters:
opt PWM Channel structure parameter
Returns:
PWM_SUCCESS or PWM_INVALID_INPUT

Definition at line 53 of file pwm.c.

References pwm_opt_t::diva, pwm_opt_t::divb, pwm_opt_t::prea, pwm_opt_t::preb, PWM_INVALID_INPUT, and PWM_SUCCESS.

Referenced by main().

00054 {
00055   volatile avr32_pwm_t *pwm = &AVR32_PWM;
00056   Bool global_interrupt_enabled = Is_global_interrupt_enabled();
00057 
00058   if (opt == 0 ) // Null pointer.
00059     return PWM_INVALID_INPUT;
00060 
00061   // Disable interrupt.
00062   if (global_interrupt_enabled) Disable_global_interrupt();
00063   pwm->idr = ((1 << (AVR32_PWM_LINES_MSB + 1)) - 1) << AVR32_PWM_IDR_CHID0_OFFSET;
00064   pwm->isr;
00065   if (global_interrupt_enabled) Enable_global_interrupt();
00066 
00067   // Set PWM mode register.
00068   pwm->mr =
00069     ((opt->diva)<<AVR32_PWM_DIVA_OFFSET) |
00070     ((opt->divb)<<AVR32_PWM_DIVB_OFFSET) |
00071     ((opt->prea)<<AVR32_PWM_PREA_OFFSET) |
00072     ((opt->preb)<<AVR32_PWM_PREB_OFFSET)
00073     ;
00074 
00075   return PWM_SUCCESS;
00076 }

int pwm_start_channels ( unsigned long  channels_bitmask  ) 

Start PWM channels.

Parameters:
channels_bitmask A bit-mask with set bits indicating channels to start.
Returns:
PWM_SUCCESS or PWM_INVALID_INPUT

Definition at line 96 of file pwm.c.

References PWM_INVALID_INPUT, and PWM_SUCCESS.

Referenced by main().

00097 {
00098   if (channels_bitmask & ~((1 << (AVR32_PWM_LINES_MSB + 1)) - 1))
00099     return PWM_INVALID_INPUT;
00100 
00101   AVR32_PWM.ena = channels_bitmask; // Enable channels.
00102 
00103   return PWM_SUCCESS;
00104 }

int pwm_stop_channels ( unsigned long  channels_bitmask  ) 

Stop PWM channels.

Parameters:
channels_bitmask A bit-mask with set bits indicating channels to stop.
Returns:
PWM_SUCCESS or PWM_INVALID_INPUT

Definition at line 107 of file pwm.c.

References PWM_INVALID_INPUT, and PWM_SUCCESS.

00108 {
00109   if (channels_bitmask & ~((1 << (AVR32_PWM_LINES_MSB + 1)) - 1))
00110     return PWM_INVALID_INPUT;
00111 
00112   AVR32_PWM.dis = channels_bitmask; // Disable channels.
00113 
00114   return PWM_SUCCESS;
00115 }

int pwm_sync_update_channel ( unsigned int  channel_id,
const avr32_pwm_channel_t *  pwm_channel 
)

Update channel register CPRDx or CDTYx by forcing synchronization with the PWM period. This function uses the CUPDx register as a double buffer for the period or the duty cycle. Only the first 20 bits of cupd are significant.

Parameters:
channel_id The channel identifier (0 to max channel-1)
*pwm_channel Pointer to PWM channel struct avr32_pwm_channel_t
Returns:
PWM_SUCCESS or PWM_INVALID_INPUT
Note:
This update function should be preferred when updating a PWM channel by polling.

Definition at line 118 of file pwm.c.

References PWM_INVALID_INPUT, and PWM_SUCCESS.

00119 {
00120   volatile avr32_pwm_t *pwm = &AVR32_PWM;
00121 
00122   if (channel_id > AVR32_PWM_LINES_MSB)
00123      return PWM_INVALID_INPUT;
00124 
00125   AVR32_PWM.isr;                                    // Acknowledgement and clear previous register state.
00126   pwm->channel[channel_id].cmr= pwm_channel->cmr;   // Channel mode register: update of the period or duty cycle.
00127   while (!(AVR32_PWM.isr & (1 << channel_id)));     // Wait until the last write has been taken into account.
00128   pwm->channel[channel_id].cupd= pwm_channel->cupd; // Channel update CPRDx or CDTYx according to CPD value in CMRx.
00129 
00130   return PWM_SUCCESS;
00131 }


Generated on Thu Dec 17 19:59:13 2009 for AVR32 - PWM Driver by  doxygen 1.5.5