CRpi
A library for rpi with intefaces to: gpio, pwm, dma
Functions
pwmByDma.h File Reference

Generates pwm outputs on the gpio pins using the dma engine. More...

Go to the source code of this file.

Functions

int pwmDma_init (unsigned int pulseDuration_us)
 Initialize: call this before any other function in this file. This sets up the pwm to use it as a timer, so don't mess with it after calling this function. More...
 
unsigned int pwmDma_get_pulseDuration_us ()
 Returns the duration in microseconds of a single pulse, you can set this duration in pwmDma_init(...) More...
 
int pwmDma_isChannelInitialized (int channel)
 Tells if a channel is initialized. More...
 
int pwmDma_getChannelPeriodUs (int channel)
 Return the duration in microseconds of a cycle for the specified channel. More...
 
int pwmDma_initChannel_periodInPulses (int channel, unsigned int pulsesPerPeriod)
 Sets up the channel to output pwm, you can use only a channel at a time. More...
 
int pwmDma_initChannel (int channel, unsigned int period_us)
 Sets up the channel to output pwm. More...
 
int pwmDma_freeChannel (int channel)
 Frees a channel initialized with pwmDma_initChannel or pwmDma_initChannel_periodInPulses. More...
 
int pwmDma_startChannel (int channel)
 Starts a stopped channel. More...
 
int pwmDma_stopChannelWaitCycleEnd (int channel)
 Stops channel waiting for the current cycle to finish. More...
 
int pwmDma_stopChannel (int channel)
 Stops channel. More...
 
int pwmDma_addPulse (int channel, int bcmPort, unsigned int start_us, unsigned int length_us)
 Sets a pulse on the specified channel and gpio. More...
 
int pwmDma_printWords (int channel)
 For debugging purposes: prints all the set/clear words. More...
 
int pwmDma_printControlBlocks (int channel)
 For debugging purposes: prints all the control blocks. More...
 
int pwmDma_setMSRatio (int channel, int bcmPort, double ratio)
 Set the port to output a square wave with the specified duty cycle. Will be high for the first ratio*period microseconds and then low. More...
 
int pwmDma_clearPort (int channel, int bcmPort)
 Removes any pulse on this port at the specified channel. More...
 
int pwmDma_addMultiplePulse (int channel, int bcmPort, unsigned int start_us, unsigned int length_us, unsigned int subPeriod_us)
 Adds multiple pulses to the channel at the specified port. The pulse will be repeated every subPeriod_us microseconds. In this way you can simulate a channel initialized with period=subPeriod_us. More...
 

Detailed Description

Generates pwm outputs on the gpio pins using the dma engine.

To generate a pwm output you first have to call pwmDma_init().
Then you can initilize a channel with pwmDma_initChannel().
After this you can add pulses to the channel using pwmDma_addPulse(), pwmDma_addMultiplePulse() or pwmDma_setMSRatio().
You can clear the output on a port using pwmDma_clearPort()
When you finish you HAVE TO call pwmDma_freeChannel() on the channel you initialized.
See pwmByDma.c for more details.

Function Documentation

int pwmDma_addMultiplePulse ( int  channel,
int  bcmPort,
unsigned int  start_us,
unsigned int  length_us,
unsigned int  subPeriod_us 
)

Adds multiple pulses to the channel at the specified port. The pulse will be repeated every subPeriod_us microseconds. In this way you can simulate a channel initialized with period=subPeriod_us.

Parameters
channelThe channel number
bcmPortThe port broadcom number, only ports 0-31 are available
start_usThe start time of the pulse in microseconds, has to be a multiple of the pulse duration, and between 0 and the subPeriod_us
length_usThe length of the pulse in microseconds, has to be a multiple of the pulse duration and not bigger than subPeriod_us
subPeriod_usThe period at which the pulse will be repeated. Has to be a divisor of the period of the channel
int pwmDma_addPulse ( int  channel,
int  bcmPort,
unsigned int  start_us,
unsigned int  length_us 
)

Sets a pulse on the specified channel and gpio.

Parameters
channelThe channel number
bcmPortThe port broadcom number, only ports 0-31 are available
start_usThe start time of the pulse in microseconds, has to be a multiple of the pulse duration, and between 0 and the period duration of this channel
length_usThe length of the pulse in microseconds, has to be a multiple of the pulse duration
Returns
negative in case of error
int pwmDma_clearPort ( int  channel,
int  bcmPort 
)

Removes any pulse on this port at the specified channel.

Parameters
channelThe channel
bcmPortThe port
Returns
negative in case of error
int pwmDma_freeChannel ( int  channel)

Frees a channel initialized with pwmDma_initChannel or pwmDma_initChannel_periodInPulses.

Parameters
channelThe channel number
Returns
negative in case of error
unsigned int pwmDma_get_pulseDuration_us ( )

Returns the duration in microseconds of a single pulse, you can set this duration in pwmDma_init(...)

Returns
The duration, zero if pwmDma_init hasn't been called
int pwmDma_getChannelPeriodUs ( int  channel)

Return the duration in microseconds of a cycle for the specified channel.

Parameters
channelThe channel number
Returns
the duration, 0 in case of error
int pwmDma_init ( unsigned int  pulseDuration_us)

Initialize: call this before any other function in this file. This sets up the pwm to use it as a timer, so don't mess with it after calling this function.

Parameters
pulseDuration_usThe duration of a single pulse in microseconds, in other words: the granularity, the precision. All prime numbers over 4095 and all the numbers over 33546 aren't acceptable.
int pwmDma_initChannel ( int  channel,
unsigned int  period_us 
)

Sets up the channel to output pwm.

Parameters
channelThe channel to use
period_usDuration of a cycle in microseconds
Returns
negative in case of error
int pwmDma_initChannel_periodInPulses ( int  channel,
unsigned int  pulsesPerPeriod 
)

Sets up the channel to output pwm, you can use only a channel at a time.

Parameters
channelThe channel to use
pulsesPerPeriodNumber of pulses in a cycle. The resulting period duration is: period_us = pulsesPerPeriod*pulseDuration_us. Where pulseDuration_us is specified in pwmDma_init(...)
Returns
negative in case of error
int pwmDma_isChannelInitialized ( int  channel)

Tells if a channel is initialized.

Parameters
channelThe channel number
Returns
If the channel has been initialized, 0 in case of error
int pwmDma_printControlBlocks ( int  channel)

For debugging purposes: prints all the control blocks.

Returns
negative in case of error
int pwmDma_printWords ( int  channel)

For debugging purposes: prints all the set/clear words.

Returns
negative in case of error
int pwmDma_setMSRatio ( int  channel,
int  bcmPort,
double  ratio 
)

Set the port to output a square wave with the specified duty cycle. Will be high for the first ratio*period microseconds and then low.

Parameters
channelThe channel to use
bcmPortThe port to use
ratioThe duty cycle ratio
Returns
negative in case of error
int pwmDma_startChannel ( int  channel)

Starts a stopped channel.

Parameters
channelThe channel number
Returns
negative in case of error
int pwmDma_stopChannel ( int  channel)

Stops channel.

Parameters
channelThe channel number
Returns
negative in case of error
int pwmDma_stopChannelWaitCycleEnd ( int  channel)

Stops channel waiting for the current cycle to finish.

Parameters
channelThe channel number
Returns
negative in case of error