Unit PWM

From Ultibo.org
Revision as of 03:33, 5 January 2017 by Ultibo (Talk | contribs)

Jump to: navigation, search

Return to Unit Reference


Description


Ultibo PWM interface unit

Pulse Width Modulation (PWM) is a technique of encoding a pulsed signal so as to control the ratio of on to off by switching the signal between on and off at a very high rate.

This on and off switching allows control of the average power supplied to the load and therefore gives fine grained control of things such as motor speeds, lamp dimming and heating elements.

PWM is also used in many low power applications as well such as controlling servo motors and can even by used for audio applications such as class D amplifiers.

This unit deals with the usage of PWM for control applications (for audio applications see the Audio unit) and provides methods to control the state, frequency, duty cycle, range and mode of PWM host controllers. Not all devices support all of these concepts so this API includes a properties function to allow obtaining information about a PWM device and its capabilities.

Constants



PWM specific constants PWM_*
PWM_NAME_PREFIX = 'PWM'; Name prefix for PWM Devices


PWM device type constants PWM_TYPE_*
PWM_TYPE_NONE = 0;  


PWM device state constants PWM_STATE_*
PWM_STATE_DISABLED = 0;  
PWM_STATE_ENABLED = 1;  


PWM device flag constants PWM_FLAG_*
PWM_FLAG_NONE = $00000000;  
PWM_FLAG_GPIO = $00000001; Device supports Get/Set GPIO
PWM_FLAG_MODE = $00000002; Device supports Get/Set Mode
PWM_FLAG_RANGE = $00000004; Device supports Get/Set Range
PWM_FLAG_FREQUENCY = $00000008; Device supports Get/Set Frequency
PWM_FLAG_POLARITY = $00000010; Device supports Get/Set Polarity


PWM mode value constants PWM_MODE_*
PWM_MODE_MARKSPACE = 0; Standard PWM Mark / Space mode
PWM_MODE_BALANCED = 1; Balanced mode (Device specific)
PWM_MODE_SERIALIZED = 2; Serialized mode (Device specific)


PWM polarity value constants PWM_POLARITY_*
PWM_POLARITY_NORMAL = 0;  
PWM_POLARITY_INVERSE = 1;  


PWM logging constants PWM_LOG_*
PWM_LOG_LEVEL_DEBUG = LOG_LEVEL_DEBUG; PWM debugging messages
PWM_LOG_LEVEL_INFO = LOG_LEVEL_INFO; PWM informational messages, such as a device being attached or detached
PWM_LOG_LEVEL_ERROR = LOG_LEVEL_ERROR; PWM error messages
PWM_LOG_LEVEL_NONE = LOG_LEVEL_NONE; No PWM messages


Type definitions


To be documented

Public variables



PWM logging

PWM_DEFAULT_LOG_LEVEL:LongWord = PWM_LOG_LEVEL_DEBUG; Minimum level for PWM messages. Only messages with level greater than or equal to this will be printed.
PWM_LOG_ENABLED:Boolean;


Function declarations



Initialization functions

procedure PWMInit;
Description: Initialize the PWM unit and PWM device table
Note Called only during system startup


PWM functions

function PWMDeviceStart(PWM:PPWMDevice):LongWord;
Description: Start the specified PWM device
PWM The PWM device to start
Return ERROR_SUCCESS if completed or another error code on failure


function PWMDeviceStop(PWM:PPWMDevice):LongWord;
Description: Stop the specified PWM device
PWM The PWM device to stop
Return ERROR_SUCCESS if completed or another error code on failure


function PWMDeviceWrite(PWM:PPWMDevice; Value:LongWord):LongWord; 
Description: Write a value to the specified PWM device
PWM The PWM device to write to
Value The value to write
Return ERROR_SUCCESS if completed or another error code on failure
Note The exact meaning of value may depend on the device and other configured options, in many cases the value will represent the "on" time of each pulse with regard to the duty cycle of the waveform output by the device


function PWMDeviceGetGPIO(PWM:PPWMDevice):LongWord;
Description: Get the GPIO pin used by the specified PWM device
PWM The PWM device to get the GPIO pin from
Return The current GPIO pin or GPIO_PIN_UNKNOWN on failure


function PWMDeviceSetGPIO(PWM:PPWMDevice; GPIO:LongWord):LongWord;
Description: Set the GPIO pin used by the specified PWM device
PWM The PWM device to set the GPIO pin for
GPIO The GPIO pin to set (eg GPIO_PIN_12)
Return ERROR_SUCCESS if completed or another error code on failure


function PWMDeviceGetMode(PWM:PPWMDevice):LongWord;
Description: Get the current mode of the specified PWM device
PWM The PWM device to get the mode from
Return The current mode or a default value of PWM_MODE_MARKSPACE


function PWMDeviceSetMode(PWM:PPWMDevice; Mode:LongWord):LongWord;
Description: Set the current mode for the specified PWM device
PWM The PWM device to set the mode for
Mode The mode value to set (eg PWM_MODE_MARKSPACE)
Return ERROR_SUCCESS if completed or another error code on failure


function PWMDeviceGetRange(PWM:PPWMDevice):LongWord;
Description: Get the current range of the specified PWM device
PWM The PWM device to get the range from
Return The current range or 0 on failure


function PWMDeviceSetRange(PWM:PPWMDevice; Range:LongWord):LongWord;
Description: Set the current range for the specified PWM device
PWM The PWM device to set the clock rate for
Range The range value to set
Return ERROR_SUCCESS if completed or another error code on failure
Note The exact meaning of range may depend on the device and other configured options, in many cases the range will represent the period of one full cycle of the waveform output by the device


function PWMDeviceGetFrequency(PWM:PPWMDevice):LongWord;
Description: Get the clock frequency of the specified PWM device
PWM The PWM device to get the clock frequency from
Return The clock frequency in Hz or 0 on failure


function PWMDeviceSetFrequency(PWM:PPWMDevice; Frequency:LongWord):LongWord;
Description: Set the clock frequency for the specified PWM device
PWM The PWM device to set the clock frequency for
Frequency The clock frequency to set in Hz
Return ERROR_SUCCESS if completed or another error code on failure


function PWMDeviceGetPolarity(PWM:PPWMDevice):LongWord;
Description: Get the current polarity of the specified PWM device
PWM The PWM device to get the polarity from
Return The current polarity or a default value of PWM_POLARITY_NORMAL


function PWMDeviceSetPolarity(PWM:PPWMDevice; Polarity:LongWord):LongWord;
Description: Set the current polarity for the specified PWM device
PWM The PWM device to set the polarity for
Polarity The polarity value to set (eg PWM_POLARITY_NORMAL)
Return ERROR_SUCCESS if completed or another error code on failure


function PWMDeviceConfigure(PWM:PPWMDevice; DutyNS,PeriodNS:LongWord):LongWord;
Description: Set the configuration of the specified PWM device
PWM The PWM device to set the configuration for
DutyNS The "on" time part of the cycle (Nanoseconds)
PeriodNS The duration of one full cycle (Nanoseconds)
Return ERROR_SUCCESS if completed or another error code on failure


function PWMDeviceProperties(PWM:PPWMDevice; Properties:PPWMProperties):LongWord;
Description: Get the properties for the specified PWM device
PWM The PWM device to get properties from
Properties Pointer to a TPWMProperties structure to fill in
Return ERROR_SUCCESS if completed or another error code on failure


function PWMDeviceCreate:PPWMDevice;
Description: Create a new PWM entry
Return Pointer to new PWM entry or nil if PWM could not be created


function PWMDeviceCreateEx(Size:LongWord):PPWMDevice;
Description: Create a new PWM entry
Size Size in bytes to allocate for new PWM (Including the PWM entry)
Return Pointer to new PWM entry or nil if PWM could not be created


function PWMDeviceDestroy(PWM:PPWMDevice):LongWord;
Description: Destroy an existing PWM entry
Note None documented


function PWMDeviceRegister(PWM:PPWMDevice):LongWord;
Description: Register a new PWM in the PWM table
Note None documented


function PWMDeviceDeregister(PWM:PPWMDevice):LongWord;
Description: Deregister a PWM from the PWM table
Note None documented


function PWMDeviceFind(PWMId:LongWord):PPWMDevice;
Description: To be documented
Note None documented


function PWMDeviceFindByName(const Name:String):PPWMDevice; inline;
Description: To be documented
Note None documented


function PWMDeviceFindByDescription(const Description:String):PPWMDevice; inline;
Description: To be documented
Note None documented


function PWMDeviceEnumerate(Callback:TPWMEnumerate; Data:Pointer):LongWord;
Description: To be documented
Note None documented


function PWMDeviceNotification(PWM:PPWMDevice; Callback:TPWMNotification; Data:Pointer; Notification,Flags:LongWord):LongWord;
Description: To be documented
Note None documented


RTL PWM functions

function SysPWMAvailable:Boolean; 
Description: Check if a PWM device is available
Note None documented


function SysPWMStart:LongWord;
Description: Start the default PWM device
Return ERROR_SUCCESS if completed or another error code on failure


function SysPWMStop:LongWord;
Description: Stop the default PWM device
Return ERROR_SUCCESS if completed or another error code on failure


function SysPWMWrite(Value:LongWord):LongWord;
Description: Write a value to the default PWM device
Value The value to write
Return ERROR_SUCCESS if completed or another error code on failure
Note The exact meaning of value may depend on the device and other configured options, in many cases the value will represent the "on" time of each pulse with regard to the duty cycle of the waveform output by the device


function SysPWMSetMode(Mode:LongWord):LongWord;
Description: Set the mode for the default PWM device
Mode The mode value to set (eg PWM_MODE_MARKSPACE)
Return ERROR_SUCCESS if completed or another error code on failure


function SysPWMSetRange(Range:LongWord):LongWord;
Description: Set the range for the default PWM device
Range The range value to set
Return ERROR_SUCCESS if completed or another error code on failure
Note The exact meaning of range may depend on the device and other configured options, in many cases the range will represent the period of one full cycle of the waveform output by the device


function SysPWMSetFrequency(Frequency:LongWord):LongWord;
Description: Set the clock frequency for the default PWM device
Frequency The frequency to set in Hz
Return ERROR_SUCCESS if completed or another error code on failure


function SysPWMConfigure(DutyNS,PeriodNS:LongWord):LongWord;
Description: Set the configuration of the default PWM device
DutyNS The "on" time part of the cycle (Nanoseconds)
PeriodNS The duration of one full cycle (Nanoseconds)
Return ERROR_SUCCESS if completed or another error code on failure


PWM helper functions

function PWMGetCount:LongWord; inline;
Description: Get the current PWM count
Note None documented


function PWMDeviceGetDefault:PPWMDevice; inline;
Description: Get the current default PWM device
Note None documented


function PWMDeviceSetDefault(PWM:PPWMDevice):LongWord;
Description: Set the current default PWM device
Note None documented


function PWMDeviceCheck(PWM:PPWMDevice):PPWMDevice;
Description: Check if the supplied PWM is in the PWM table
Note None documented


procedure PWMLog(Level:LongWord; PWM:PPWMDevice; const AText:String);
Description: To be documented
Note None documented


procedure PWMLogInfo(PWM:PPWMDevice; const AText:String); inline;
Description: To be documented
Note None documented


procedure PWMLogError(PWM:PPWMDevice; const AText:String); inline;
Description: To be documented
Note None documented


procedure PWMLogDebug(PWM:PPWMDevice; const AText:String); inline;
Description: To be documented
Note None documented


Return to Unit Reference