Difference between revisions of "Unit GPIO"

From Ultibo.org
Jump to: navigation, search
Line 18: Line 18:
 
----
 
----
  
''To be documented''
+
 
 +
<div class="toccolours mw-collapsible mw-collapsed" style="border: 1; font-family: arial; padding-top: 20px; padding-bottom: 15px;">
 +
<div style="font-size: 14px; padding-left: 12px;">'''GPIO specific constants''' <code> GPIO_* </code></div>
 +
<div class="mw-collapsible-content" style="text-align: left; padding-left: 5px;">
 +
{| class="wikitable" style="font-size: 14px; background: white;"
 +
|-
 +
| <code>GPIO_NAME_PREFIX = 'GPIO';</code>
 +
| Name prefix for GPIO Devices
 +
|-
 +
|}
 +
</div></div>
 +
<br />
 +
<div class="toccolours mw-collapsible mw-collapsed" style="border: 1; font-family: arial; padding-top: 20px; padding-bottom: 15px;">
 +
<div style="font-size: 14px; padding-left: 12px;">'''GPIO device type constants''' <code> GPIO_TYPE_* </code></div>
 +
<div class="mw-collapsible-content" style="text-align: left; padding-left: 5px;">
 +
{| class="wikitable" style="font-size: 14px; background: white;"
 +
|-
 +
| <code>GPIO_TYPE_NONE = 0;</code>
 +
| style="width: 50%;"|&nbsp;
 +
|-
 +
|}
 +
</div></div>
 +
<br />
 +
<div class="toccolours mw-collapsible mw-collapsed" style="border: 1; font-family: arial; padding-top: 20px; padding-bottom: 15px;">
 +
<div style="font-size: 14px; padding-left: 12px;">'''GPIO device state constants''' <code> GPIO_STATE_* </code></div>
 +
<div class="mw-collapsible-content" style="text-align: left; padding-left: 5px;">
 +
{| class="wikitable" style="font-size: 14px; background: white;"
 +
|-
 +
| <code>GPIO_STATE_DISABLED = 0;</code>
 +
| style="width: 50%;"|&nbsp;
 +
|-
 +
| <code>GPIO_STATE_ENABLED = 1;</code>
 +
| &nbsp;
 +
|-
 +
|}
 +
</div></div>
 +
<br />
 +
<div class="toccolours mw-collapsible mw-collapsed" style="border: 1; font-family: arial; padding-top: 20px; padding-bottom: 15px;">
 +
<div style="font-size: 14px; padding-left: 12px;">'''GPIO device flag constants''' <code> GPIO_FLAG_* </code></div>
 +
<div class="mw-collapsible-content" style="text-align: left; padding-left: 5px;">
 +
{| class="wikitable" style="font-size: 14px; background: white;"
 +
|-
 +
| <code>GPIO_FLAG_NONE = $00000000;</code>
 +
| &nbsp;
 +
|-
 +
| <code>GPIO_FLAG_PULL_UP = $00000001;</code>
 +
| Device supports Pull Up on a pin
 +
|-
 +
| <code>GPIO_FLAG_PULL_DOWN = $00000002;</code>
 +
| Device supports Pull Down on a pin
 +
|-
 +
| <code>GPIO_FLAG_TRIGGER_LOW = $00000004;</code>
 +
| Device supports Trigger on Low level on a pin
 +
|-
 +
| <code>GPIO_FLAG_TRIGGER_HIGH = $00000008;</code>
 +
| Device supports Trigger on High level on a pin
 +
|-
 +
| <code>GPIO_FLAG_TRIGGER_RISING = $00000010;</code>
 +
| Device supports Trigger on Rising edge on a pin
 +
|-
 +
| <code>GPIO_FLAG_TRIGGER_FALLING = $00000020;</code>
 +
| Device supports Trigger on Falling edge on a pin
 +
|-
 +
| <code>GPIO_FLAG_TRIGGER_EDGE = $00000040;</code>
 +
| Device supports Trigger on any edge (Rising or Falling) on a pin
 +
|-
 +
| <code>GPIO_FLAG_TRIGGER_ASYNC = $00000080;</code>
 +
| Device supports Trigger on Asynchronous Rising/Falling edge on a pin
 +
|-
 +
|}
 +
</div></div>
 +
<br />
 +
<div class="toccolours mw-collapsible mw-collapsed" style="border: 1; font-family: arial; padding-top: 20px; padding-bottom: 15px;">
 +
<div style="font-size: 14px; padding-left: 12px;">'''GPIO event flag constants''' <code> GPIO_EVENT_FLAG_* </code></div>
 +
<div class="mw-collapsible-content" style="text-align: left; padding-left: 5px;">
 +
{| class="wikitable" style="font-size: 14px; background: white;"
 +
|-
 +
| <code>GPIO_EVENT_FLAG_NONE = $00000000;</code>
 +
| &nbsp;
 +
|-
 +
| <code>GPIO_EVENT_FLAG_REPEAT = $00000001;</code>
 +
| Event will be repeated until cancelled
 +
|-
 +
| <code>GPIO_EVENT_FLAG_INTERRUPT = $00000002;</code>
 +
| Event will be dispatched by interrupt handler (If applicable)
 +
<br />Caution: Events called by the interrupt handler must obey interrupt rules with regard to locks, memory allocation and latency
 +
|-
 +
|}
 +
</div></div>
 +
<br />
 +
<div class="toccolours mw-collapsible mw-collapsed" style="border: 1; font-family: arial; padding-top: 20px; padding-bottom: 15px;">
 +
<div style="font-size: 14px; padding-left: 12px;">'''GPIO logging constants''' <code> GPIO_LOG_* </code></div>
 +
<div class="mw-collapsible-content" style="text-align: left; padding-left: 5px;">
 +
{| class="wikitable" style="font-size: 14px; background: white;"
 +
|-
 +
| <code>GPIO_LOG_LEVEL_DEBUG = LOG_LEVEL_DEBUG;</code>
 +
| GPIO debugging messages
 +
|-
 +
| <code>GPIO_LOG_LEVEL_INFO = LOG_LEVEL_INFO;</code>
 +
| GPIO informational messages, such as a device being attached or detached
 +
|-
 +
| <code>GPIO_LOG_LEVEL_ERROR = LOG_LEVEL_ERROR;</code>
 +
| GPIO error messages
 +
|-
 +
| <code>GPIO_LOG_LEVEL_NONE = LOG_LEVEL_NONE;</code>
 +
| No GPIO messages
 +
|-
 +
|}
 +
</div></div>
 +
<br />
  
 
=== Type definitions ===
 
=== Type definitions ===
Line 33: Line 142:
 
----
 
----
  
<br />
+
 
 +
 
 
'''Initialization functions'''
 
'''Initialization functions'''
  

Revision as of 00:08, 28 November 2016

Return to Unit Reference


Description


GPIO devices represent the external or internal pins available on most system on chip (SoC) devices to provide control and interfacing capabilities for both hardware and software.

This unit maintains pin numbering exactly as per the SoC documentation but abstracts other features such as alternate function selects to avoid exposing chip specific values via the API.

Not all GPIO devices support the same feature set so the GPIODeviceProperties function returns a structure which describes the number of pins as well as minimum and maximum pin numbers along with a set of flags that indicate what functionality is supported by the device.

Multiple GPIO devices can be accomodated, each one is registered with this unit when the driver for the device is loaded and initialized. This unit includes functions for enumerating the devices that are available and each function takes a GPIODevice parameter to allow specifying the exact device to control.

Simplified versions of many of the functions in this unit are provided in the Platform unit to allow control of the default GPIO device and in cases where there is only one device registered these functions will provide most of the capability required.

Constants



GPIO specific constants GPIO_*
GPIO_NAME_PREFIX = 'GPIO'; Name prefix for GPIO Devices


GPIO device type constants GPIO_TYPE_*
GPIO_TYPE_NONE = 0;  


GPIO device state constants GPIO_STATE_*
GPIO_STATE_DISABLED = 0;  
GPIO_STATE_ENABLED = 1;  


GPIO device flag constants GPIO_FLAG_*
GPIO_FLAG_NONE = $00000000;  
GPIO_FLAG_PULL_UP = $00000001; Device supports Pull Up on a pin
GPIO_FLAG_PULL_DOWN = $00000002; Device supports Pull Down on a pin
GPIO_FLAG_TRIGGER_LOW = $00000004; Device supports Trigger on Low level on a pin
GPIO_FLAG_TRIGGER_HIGH = $00000008; Device supports Trigger on High level on a pin
GPIO_FLAG_TRIGGER_RISING = $00000010; Device supports Trigger on Rising edge on a pin
GPIO_FLAG_TRIGGER_FALLING = $00000020; Device supports Trigger on Falling edge on a pin
GPIO_FLAG_TRIGGER_EDGE = $00000040; Device supports Trigger on any edge (Rising or Falling) on a pin
GPIO_FLAG_TRIGGER_ASYNC = $00000080; Device supports Trigger on Asynchronous Rising/Falling edge on a pin


GPIO event flag constants GPIO_EVENT_FLAG_*
GPIO_EVENT_FLAG_NONE = $00000000;  
GPIO_EVENT_FLAG_REPEAT = $00000001; Event will be repeated until cancelled
GPIO_EVENT_FLAG_INTERRUPT = $00000002; Event will be dispatched by interrupt handler (If applicable)


Caution: Events called by the interrupt handler must obey interrupt rules with regard to locks, memory allocation and latency


GPIO logging constants GPIO_LOG_*
GPIO_LOG_LEVEL_DEBUG = LOG_LEVEL_DEBUG; GPIO debugging messages
GPIO_LOG_LEVEL_INFO = LOG_LEVEL_INFO; GPIO informational messages, such as a device being attached or detached
GPIO_LOG_LEVEL_ERROR = LOG_LEVEL_ERROR; GPIO error messages
GPIO_LOG_LEVEL_NONE = LOG_LEVEL_NONE; No GPIO messages


Type definitions


To be documented

Public variables


To be documented

Function declarations



Initialization functions

procedure GPIOInit;
Description: To be documented
Note None documented


GPIO functions

function GPIODeviceStart(GPIO:PGPIODevice):LongWord;
Description: Start the specified GPIO device and enable access
GPIO The GPIO device to start
Return ERROR_SUCCESS if completed or another error code on failure


function GPIODeviceStop(GPIO:PGPIODevice):LongWord;
Description: Stop the specified GPIO device and disable access
GPIO The GPIO device to stop
Return ERROR_SUCCESS if completed or another error code on failure


function GPIODeviceRead(GPIO:PGPIODevice; Reg:LongWord):LongWord;
Description: To be documented
Return The value of the memory register


procedure GPIODeviceWrite(GPIO:PGPIODevice; Reg,Value:LongWord);
Description: Perform a direct write to a register of the specified GPIO device
GPIO The GPIO device to write to
Reg The memory register to write to
Return The value to write to the register


function GPIODeviceInputGet(GPIO:PGPIODevice; Pin:LongWord):LongWord;
Description: Get the current state of an input pin on the specified GPIO device
Pin The pin to get the state for (eg GPIO_PIN_1)
Return The current state (eg GPIO_LEVEL_HIGH) or GPIO_LEVEL_UNKNOWN on failure


function GPIODeviceInputWait(GPIO:PGPIODevice; Pin,Trigger,Timeout:LongWord):LongWord;
Description: Wait for the state of a input pin to change on the specified GPIO device
GPIO The GPIO device to wait for
Pin The pin to wait for the state to change (eg GPIO_PIN_1)
Trigger The trigger event to wait for (eg GPIO_TRIGGER_HIGH)
Timeout Number of milliseconds to wait for the change (INFINITE to wait forever)
Return The state after the change (eg GPIO_LEVEL_HIGH) or GPIO_LEVEL_UNKNOWN on failure or timeout


function GPIODeviceInputEvent(GPIO:PGPIODevice; Pin,Trigger,Flags,Timeout:LongWord; Callback:TGPIOCallback; Data:Pointer):LongWord;
Description: Schedule a function to be called when the state of a input pin changes on the specified GPIO device
GPIO The GPIO device to schedule the callback for
Pin The pin to schedule the state change for (eg GPIO_PIN_1)
Trigger The trigger event which will cause the function to be called (eg GPIO_TRIGGER_HIGH)
Timeout The number of milliseconds before the scheduled trigger expires (INFINITE to never expire)
Callback The function to be called when the trigger occurs
Data A pointer to be pass to the function when the trigger occurs (Optional)
Return ERROR_SUCCESS if the trigger was scheduled successfully or another error code on failure
Note The pin and trigger that caused the event will be passed to the callback function


function GPIODeviceInputCancel(GPIO:PGPIODevice; Pin:LongWord):LongWord;
Description: Cancel a previously scheduled event callback function for an input pin on the specified GPIO device
GPIO The GPIO device to cancel the callback for
Pin The pin to cancel the state change for (eg GPIO_PIN_1)
Return ERROR_SUCCESS if the callback was cancelled successfully or another error code on failure


function GPIODeviceOutputSet(GPIO:PGPIODevice; Pin,Level:LongWord):LongWord;
Description: Set the state of a output pin on the specified GPIO device
GPIO The GPIO device to set for
Pin The pin to set the state for (eg GPIO_PIN_1)
Level The state to set the pin to (eg GPIO_LEVEL_HIGH)
Return ERROR_SUCCESS if completed successfully or another error code on failure


function GPIODevicePullGet(GPIO:PGPIODevice;Pin:LongWord):LongWord;
Description: Get the current pull state of a pin on the specified GPIO device
Pin The pin to get the pull state for (eg GPIO_PIN_1)
Return The current pull state of the pin (eg GPIO_PULL_UP) or GPIO_PULL_UNKNOWN on failure


function GPIODevicePullSelect(GPIO:PGPIODevice; Pin,Mode:LongWord):LongWord;
Description: Change the pull state of a pin on the specified GPIO device
GPIO The GPIO device to set for
Pin The pin to change the pull state for (eg GPIO_PIN_1)
Mode The pull state to set for the pin (eg GPIO_PULL_UP)
Return ERROR_SUCCESS if completed successfully or another error code on failure


function GPIODeviceFunctionGet(GPIO:PGPIODevice; Pin:LongWord):LongWord;
Description: Get the current function of a pin on the specified GPIO device


function GPIODeviceFunctionSelect(GPIO:PGPIODevice; Pin,Mode:LongWord):LongWord;
Description: Change the function of a pin on the specified GPIO device
Pin The pin to get the function for (eg GPIO_PIN_1)
Return The current function of the pin (eg GPIO_FUNCTION_IN) or GPIO_FUNCTION_UNKNOWN on failure
GPIO The GPIO device to set for
Pin The pin to change the function for (eg GPIO_PIN_1)
Mode The function to set for the pin (eg GPIO_FUNCTION_OUT)
Return ERROR_SUCCESS if completed successfully or another error code on failure


function GPIODeviceProperties(GPIO:PGPIODevice; Properties:PGPIOProperties):LongWord;
Description: Get the properties for the specified GPIO device
Properties Pointer to a TGPIOProperties structure to fill in
Return ERROR_SUCCESS if completed or another error code on failure


function GPIODeviceCreate:PGPIODevice;
Description: Create a new GPIO entry
Return Pointer to new GPIO entry or nil if GPIO could not be created


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


function GPIODeviceDestroy(GPIO:PGPIODevice):LongWord;
Description: Destroy an existing GPIO entry
GPIO The GPIO device to destroy
Return ERROR_SUCCESS if completed or another error code on failure


function GPIODeviceRegister(GPIO:PGPIODevice):LongWord;
Description: Register a new GPIO in the GPIO table
GPIO The GPIO device to register
Return ERROR_SUCCESS if completed or another error code on failure


function GPIODeviceDeregister(GPIO:PGPIODevice):LongWord;
Description: Deregister a GPIO in the GPIO table
Return ERROR_SUCCESS if completed or another error code on failure


function GPIODeviceFind(GPIOId:LongWord):PGPIODevice;
Description: Find a GPIO device by ID in the GPIO table
GPIOId The ID number of the GPIO device to find
Return Pointer to GPIO device entry or nil if not found


function GPIODeviceFindByName(const Name:String):PGPIODevice; inline;
Description: Find a GPIO device by name in the GPIO table
Name The name of the GPIO to find (eg GPIO0)
Return Pointer to GPIO device entry or nil if not found


function GPIODeviceFindByDescription(const Description:String):PGPIODevice; inline;
Description: Find a GPIO device by description in the GPIO table
Description The description of the GPIO to find (eg BCM2836 GPIO)
Return Pointer to GPIO device entry or nil if not found


function GPIODeviceEnumerate(Callback:TGPIOEnumerate; Data:Pointer):LongWord;
Description: Enumerate all GPIO devices in the GPIO table
Callback The callback function to call for each GPIO in the table
Data A private data pointer to pass to callback for each GPIO in the table
Note ERROR_SUCCESS if completed or another error code on failure


function GPIODeviceNotification(GPIO:PGPIODevice; Callback:TGPIONotification; Data:Pointer; Notification,Flags:LongWord):LongWord;
Description: Register a notification for GPIO device changes
GPIO The GPIO device to notify changes for (Optional, pass nil for all GPIO devices)
Callback The function to call when a notification event occurs
Data A private data pointer to pass to callback when a notification event occurs
Notification The events to register for notification of (eg DEVICE_NOTIFICATION_REGISTER)
Flags The flags to control the notification (eg NOTIFIER_FLAG_WORKER)


RTL GPIO functions

function SysGPIOAvailable:Boolean;
Description: Check if a GPIO device is available
Note None documented


function SysGPIOInputGet(Pin:LongWord):LongWord;
Description: Get the current state of a GPIO input pin
Pin The pin to get the state for (eg GPIO_PIN_1)
Return The current state (eg GPIO_LEVEL_HIGH) or GPIO_LEVEL_UNKNOWN on failure


function SysGPIOInputWait(Pin,Trigger,Timeout:LongWord):LongWord;
Description: Wait for the state of a GPIO input pin to change
Pin The pin to wait for the state to change (eg GPIO_PIN_1)
Trigger The trigger event to wait for (eg GPIO_TRIGGER_HIGH)
Timeout Number of milliseconds to wait for the change (INFINITE to wait forever)
Return The state after the change (eg GPIO_LEVEL_HIGH) or GPIO_LEVEL_UNKNOWN on failure or timeout


function SysGPIOInputEvent(Pin,Trigger,Timeout:LongWord; Callback:TGPIOCallback; Data:Pointer):LongWord;
Description: Schedule a function to be called when the state of a GPIO input pin changes
Pin The pin to schedule the state change for (eg GPIO_PIN_1)
Trigger The trigger event which will cause the function to be called (eg GPIO_TRIGGER_HIGH)
Timeout The number of milliseconds before the scheduled trigger expires (INFINITE to never expire)
Callback The function to be called when the trigger occurs
Data A pointer to be pass to the function when the trigger occurs (Optional)
Return ERROR_SUCCESS if the trigger was scheduled successfully or another error code on failure
Note The pin and trigger that caused the event will be passed to the callback function


function SysGPIOOutputSet(Pin,Level:LongWord):LongWord; 
Description: Set the state of a GPIO output pin
Pin The pin to set the state for (eg GPIO_PIN_1)
Level The state to set the pin to (eg GPIO_LEVEL_HIGH)
Return ERROR_SUCCESS if completed successfully or another error code on failure


function SysGPIOPullGet(Pin:LongWord):LongWord;
Description: Get the current pull state of a GPIO pin
Pin The pin to get the pull state for (eg GPIO_PIN_1)
Return The current pull state of the pin (eg GPIO_PULL_UP) or GPIO_PULL_UNKNOWN on failure


function SysGPIOPullSelect(Pin,Mode:LongWord):LongWord;
Description: Change the pull state of a GPIO pin
Pin The pin to change the pull state for (eg GPIO_PIN_1)
Mode The pull state to set for the pin (eg GPIO_PULL_UP)
Return ERROR_SUCCESS if completed successfully or another error code on failure


function SysGPIOFunctionGet(Pin:LongWord):LongWord;
Description: Get the current function of a GPIO pin
Pin The pin to get the function for (eg GPIO_PIN_1)
Return The current function of the pin (eg GPIO_FUNCTION_IN) or GPIO_FUNCTION_UNKNOWN on failure


function SysGPIOFunctionSelect(Pin,Mode:LongWord):LongWord;
Description: Change the function of a GPIO pin
Pin The pin to change the function for (eg GPIO_PIN_1)
Mode The function to set for the pin (eg GPIO_FUNCTION_OUT)
Return ERROR_SUCCESS if completed successfully or another error code on failure


GPIO helper functions

function GPIOGetCount:LongWord; inline;
Description: Get the current GPIO count
Note None documented


function GPIODeviceGetDefault:PGPIODevice; inline;
Description: Get the current default GPIO device
Note None documented


function GPIODeviceSetDefault(GPIO:PGPIODevice):LongWord;
Description: Set the current default GPIO device
Note None documented


function GPIODeviceCheck(GPIO:PGPIODevice):PGPIODevice;
Description: Check if the supplied GPIO is in the GPIO table
Note None documented


function GPIODeviceCreateEvent(GPIO:PGPIODevice; Pin:PGPIOPin; Callback:TGPIOCallback; Data:Pointer; Timeout:LongWord):PGPIOEvent;
Description: Create a new event using the supplied parameters
Note Event must be registered by calling GPIODeviceRegisterEvent. Caller must hold the GPIO device lock.


function GPIODeviceDestroyEvent(GPIO:PGPIODevice; Event:PGPIOEvent):LongWord;
Description: Destroy an existing event
Note Event must be deregistered first by calling GPIODeviceDeregisterEvent. Caller must hold the GPIO device lock.


function GPIODeviceRegisterEvent(GPIO:PGPIODevice; Pin:PGPIOPin; Event:PGPIOEvent):LongWord;
Description: Register an event in the event list of the supplied Pin
Note Event must be created by calling GPIODeviceCreateEvent. Caller must hold the GPIO device lock.


function GPIODeviceDeregisterEvent(GPIO:PGPIODevice;Pin:PGPIOPin;Event:PGPIOEvent):LongWord;
Description: Deregister an event in the event list of the supplied Pin
Note Event must be destroyed by calling GPIODeviceDestroyEvent. Caller must hold the GPIO device lock.


procedure GPIOLog(Level:LongWord; GPIO:PGPIODevice; const AText:String);
Description: To be documented
Note None documented


procedure GPIOLogInfo(GPIO:PGPIODevice; const AText:String);
Description: To be documented
Note None documented


procedure GPIOLogError(GPIO:PGPIODevice; const AText:String);
Description: To be documented
Note None documented


procedure GPIOLogDebug(GPIO:PGPIODevice; const AText:String);
Description: To be documented
Note None documented


function GPIOPinToString(Pin:LongWord):String;
Description: To be documented
Note None documented


function GPIOLevelToString(Level:LongWord):String;
Description: To be documented
Note None documented


function GPIOTriggerToString(Trigger:LongWord):String;
Description: To be documented
Note None documented


function GPIOPullToString(Value:LongWord):String;
Description: To be documented
Note None documented


function GPIOFunctionToString(Value:LongWord):String;
Description: To be documented
Note None documented


Return to Unit Reference