This file defines a useful set of functions for the PWM interface on AVR32 devices.
Definition in file pwm.c.
#include "compiler.h"
#include "pwm.h"
Go to the source code of this file.
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. |
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.
channel_id | The channel identifier (0 to max channel-1) | |
*pwm_channel | Pointer to PWM channel struct avr32_pwm_channel_t |
Definition at line 131 of file pwm.c.
References PWM_INVALID_INPUT, and PWM_SUCCESS.
00132 { 00133 volatile avr32_pwm_t *pwm = &AVR32_PWM; 00134 00135 if (channel_id > AVR32_PWM_LINES_MSB) 00136 return PWM_INVALID_INPUT; 00137 00138 pwm->channel[channel_id].cmr= pwm_channel->cmr; // Channel mode register: update of the period or duty cycle. 00139 pwm->channel[channel_id].cupd= pwm_channel->cupd; // Channel update CPRDx or CDTYx according to CPD value in CMRx. 00140 00141 return PWM_SUCCESS; 00142 }
int pwm_channel_init | ( | unsigned int | channel_id, | |
const avr32_pwm_channel_t * | pwm_channel | |||
) |
Initialize a specific PWM channel.
channel_id | The channel identifier mask | |
*pwm_channel | Pointer to PWM channel struct avr32_pwm_channel_t |
Definition at line 76 of file pwm.c.
References PWM_INVALID_ARGUMENT, PWM_INVALID_INPUT, and PWM_SUCCESS.
Referenced by main().
00077 { 00078 volatile avr32_pwm_t *pwm = &AVR32_PWM; 00079 00080 if (pwm_channel == 0) // Null pointer. 00081 return PWM_INVALID_ARGUMENT; 00082 if (channel_id > AVR32_PWM_LINES_MSB) // Control input values. 00083 return PWM_INVALID_INPUT; 00084 00085 pwm->channel[channel_id].cmr= pwm_channel->cmr; // Channel mode. 00086 pwm->channel[channel_id].cdty= pwm_channel->cdty; // Duty cycle, should be < CPRD. 00087 pwm->channel[channel_id].cprd= pwm_channel->cprd; // Channel period. 00088 00089 return PWM_SUCCESS; 00090 }
int pwm_init | ( | const pwm_opt_t * | opt | ) |
This function initialize the PWM controller (mode register) and disable the interrupt.
opt | PWM Channel structure parameter |
Definition at line 50 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().
00051 { 00052 volatile avr32_pwm_t *pwm = &AVR32_PWM; 00053 Bool global_interrupt_enabled = Is_global_interrupt_enabled(); 00054 00055 if (opt == 0 ) // Null pointer. 00056 return PWM_INVALID_INPUT; 00057 00058 // Disable interrupt. 00059 if (global_interrupt_enabled) Disable_global_interrupt(); 00060 pwm->idr = ((1 << (AVR32_PWM_LINES_MSB + 1)) - 1) << AVR32_PWM_IDR_CHID0_OFFSET; 00061 pwm->isr; 00062 if (global_interrupt_enabled) Enable_global_interrupt(); 00063 00064 // Set PWM mode register. 00065 pwm->mr = 00066 ((opt->diva)<<AVR32_PWM_DIVA_OFFSET) | 00067 ((opt->divb)<<AVR32_PWM_DIVB_OFFSET) | 00068 ((opt->prea)<<AVR32_PWM_PREA_OFFSET) | 00069 ((opt->preb)<<AVR32_PWM_PREB_OFFSET) 00070 ; 00071 00072 return PWM_SUCCESS; 00073 }
int pwm_start_channels | ( | unsigned long | channels_bitmask | ) |
Start PWM channels.
channels_bitmask | A bit-mask with set bits indicating channels to start. |
Definition at line 93 of file pwm.c.
References PWM_INVALID_INPUT, and PWM_SUCCESS.
Referenced by main().
00094 { 00095 if (channels_bitmask & ~((1 << (AVR32_PWM_LINES_MSB + 1)) - 1)) 00096 return PWM_INVALID_INPUT; 00097 00098 AVR32_PWM.ena = channels_bitmask; // Enable channels. 00099 00100 return PWM_SUCCESS; 00101 }
int pwm_stop_channels | ( | unsigned long | channels_bitmask | ) |
Stop PWM channels.
channels_bitmask | A bit-mask with set bits indicating channels to stop. |
Definition at line 104 of file pwm.c.
References PWM_INVALID_INPUT, and PWM_SUCCESS.
Referenced by main().
00105 { 00106 if (channels_bitmask & ~((1 << (AVR32_PWM_LINES_MSB + 1)) - 1)) 00107 return PWM_INVALID_INPUT; 00108 00109 AVR32_PWM.dis = channels_bitmask; // Disable channels. 00110 00111 return PWM_SUCCESS; 00112 }
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.
channel_id | The channel identifier (0 to max channel-1) | |
*pwm_channel | Pointer to PWM channel struct avr32_pwm_channel_t |
Definition at line 115 of file pwm.c.
References PWM_INVALID_INPUT, and PWM_SUCCESS.
00116 { 00117 volatile avr32_pwm_t *pwm = &AVR32_PWM; 00118 00119 if (channel_id > AVR32_PWM_LINES_MSB) 00120 return PWM_INVALID_INPUT; 00121 00122 AVR32_PWM.isr; // Acknowledgement and clear previous register state. 00123 pwm->channel[channel_id].cmr= pwm_channel->cmr; // Channel mode register: update of the period or duty cycle. 00124 while (!(AVR32_PWM.isr & (1 << channel_id))); // Wait until the last write has been taken into account. 00125 pwm->channel[channel_id].cupd= pwm_channel->cupd; // Channel update CPRDx or CDTYx according to CPD value in CMRx. 00126 00127 return PWM_SUCCESS; 00128 }