CRpi
A library for rpi with intefaces to: gpio, pwm, dma
Data Structures | Typedefs | Functions
pwmByDma.cpp File Reference

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

#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <memoryManagement/cacheCoherentMemoryProvider.h>
#include <peripherals/dma.h>
#include <peripherals/pwm.h>
#include <peripherals/clockManager.h>
#include <peripherals/gpio.h>
#include <CCRCodingUtils/include/errorManagement.h>
#include <memoryManagement/addressing.h>
#include <CCRCodingUtils/include/utils.h>
#include <utils/pwmByDma.h>
#include "../peripherals/gpio_internal.h"
#include "../peripherals/pwm_internal.h"

Data Structures

struct  channelSetup_struct
 Describes the setup of a channel. More...
 

Typedefs

typedef struct channelSetup_struct ChannelSetup
 Describes the setup of a channel.
 

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...
 
int pwmDma_isInit ()
 Tells if pwmByDma has been initialized successfully.
 
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_clearPort (int channel, int bcmPort)
 Removes any pulse on this port at the specified channel. 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_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.

It works by writing periodically to the gpio set and clear registers. Precise timing is achieved using the DREQ signal from the pwm: the dma on each cycle writes to the pwm fifo an so is blocked until the pwm consumes the data. So on each cycle:

To do so we have to allocate:

If you haven't already, see pwmByDma.h

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