CRpi
A library for rpi with intefaces to: gpio, pwm, dma
|
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... | |
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
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.
channel | The channel number |
bcmPort | The port broadcom number, only ports 0-31 are available |
start_us | The start time of the pulse in microseconds, has to be a multiple of the pulse duration, and between 0 and the subPeriod_us |
length_us | The length of the pulse in microseconds, has to be a multiple of the pulse duration and not bigger than subPeriod_us |
subPeriod_us | The 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.
channel | The channel number |
bcmPort | The port broadcom number, only ports 0-31 are available |
start_us | The 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_us | The length of the pulse in microseconds, has to be a multiple of the pulse duration |
int pwmDma_clearPort | ( | int | channel, |
int | bcmPort | ||
) |
Removes any pulse on this port at the specified channel.
channel | The channel |
bcmPort | The port |
int pwmDma_freeChannel | ( | int | channel | ) |
Frees a channel initialized with pwmDma_initChannel or pwmDma_initChannel_periodInPulses.
channel | The channel number |
unsigned int pwmDma_get_pulseDuration_us | ( | ) |
Returns the duration in microseconds of a single pulse, you can set this duration in pwmDma_init(...)
int pwmDma_getChannelPeriodUs | ( | int | channel | ) |
Return the duration in microseconds of a cycle for the specified channel.
channel | The channel number |
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.
pulseDuration_us | The 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.
channel | The channel to use |
period_us | Duration of a cycle in microseconds |
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.
channel | The channel to use |
pulsesPerPeriod | Number of pulses in a cycle. The resulting period duration is: period_us = pulsesPerPeriod*pulseDuration_us. Where pulseDuration_us is specified in pwmDma_init(...) |
int pwmDma_isChannelInitialized | ( | int | channel | ) |
Tells if a channel is initialized.
channel | The channel number |
int pwmDma_printControlBlocks | ( | int | channel | ) |
For debugging purposes: prints all the control blocks.
int pwmDma_printWords | ( | int | channel | ) |
For debugging purposes: prints all the set/clear words.
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.
channel | The channel to use |
bcmPort | The port to use |
ratio | The duty cycle ratio |
int pwmDma_startChannel | ( | int | channel | ) |
Starts a stopped channel.
channel | The channel number |
int pwmDma_stopChannel | ( | int | channel | ) |
Stops channel.
channel | The channel number |
int pwmDma_stopChannelWaitCycleEnd | ( | int | channel | ) |
Stops channel waiting for the current cycle to finish.
channel | The channel number |