DLN series interface adapters contain a large number of pins that you can use as general-purpose inputs and outputs. Some of these pins are shared between GPIO (general-purpose input/output) and other modules (I2C, SPI). If you do not the corresponding pins to other modules, you can configure them as digital inputs or digital outputs and control them.
If you configure pins as inputs, your application reads the digital value set by the external device. If you define pins as outputs, your application can control the digital value on these pins.
To use a pin as a digital input or a digital output, the pin must be assigned to the GPIO module by using the DlnGpioPinEnable() function. You can use the DlnGpioPinIsEnabled() function, to check whether a pin is already assigned to the GPIO module.
You cannot assign a pin to the GPIO module if another module uses it. Call the DlnGetPinCfg() function to check which module uses a pin.
Most of the DLN-series adapters provide additional functionality for GPIO pins. You can configure the following features for any input and/or output pin individually:
Event-driven interface. You can configure a digital input pin to raise events when the value on this pin changes. There are several configuration options - what kind of events and when should be generated. For more details, read Digital Input Events.
Debounce filter. If you do not want to treat casual pulses as value changes, you can enable the debounce filter, which rejects pulses less that the predefined debounce period. For details, read Debounce Filter.
Open drain mode. If you interconnect several outputs on a single I/O line, they may settle different values simultaneously and cause hardware damage. The open drain mode helps you to avoid this. For details, read Open Drain Mode.
Pull-up and pull-down resistors. When no external hardware is connected to an input line, the logic level on this line is undefined. Use pull-up or pull-down resistors to ensure that inputs to I/O lines are settled at expected levels. For details, read Pull-up/Pull-down Resistors.
You can configure most of the DLN-series adapter pins as general-purpose digital outputs. Call the DlnGpioPinSetDirection() function and pass 1 for the direction
parameter to configure the GPIO pin as a digital output. You can call this function either before or after the pin is assigned to the GPIO module by using the DlnGpioPinEnable() function.
To set the value on the pin, use the DlnGpioPinSetOutVal() function. Call the DlnGpioPinGetOutVal()function to retrieve the output value configured for this pin. The actual value of the pin may differ from the configured one (for example, when the pin is in the open drain mode or it is not assigned to the GPIO module). The DlnGpioPinGetVal()function retrieves the actual value present on the pin.
For digital outputs, you can use the Open Drain Mode.
Use the following GPIO Interface functions to control and monitor output pins:
Configures a pin as a digital output. Pass 1 for the direction
parameter.
Sets the output value for the pin.
Retrieves the output value configured for the pin.
Retrieves the actual value on the I/O line.
You can configure most of the DLN-series adapter pins as general-purpose digital inputs. Call the DlnGpioPinSetDirection() function and pass 0 for the direction parameter to configure the GPIO pin as a digital input. You can call this function either before or after the pin is assigned to the GPIO module by using the DlnGpioPinEnable() function.
You can check a pin direction by using the DlnGpioPinGetDirection() function.
The default pin direction is set to input (value 0).
To check the value on the pin, use the DlnGpioPinGetVal() function.
For digital inputs, you can use the following features:
Use the following GPIO Interface functions to control and monitor input pins:
Configures a pin as a digital input. Pass 0 for the direction
parameter.
Retrieves the value on the I/O line.
There are two ways of monitoring changes on the digital input line:
To poll the input pin periodically. In this case, you can potentially miss an input if your application reads the value at the wrong time.
To receive events when the input value changes. You can configure event generation for specific changes, for example, when the level on a pin rises. You can also configure the adapter to generate events periodically.
To configure events, use the DlnGpioPinSetEventCfg() function. You need to specify the eventType
and the eventPeriod
parameters. The DlnGpioPinGetSupportedEventTypes() function returns the list of event types available for a pin.
The eventType
parameter can have one of the following values:
Event Type | Description |
DLN_GPIO_EVENT_NONE | A DLN adapter does not generate any events. |
DLN_GPIO_EVENT_CHANGE | A DLN adapter generates events when the level on the digital input line changes. For details, read DLN_GPIO_EVENT_CHANGE Events |
DLN_GPIO_EVENT_LEVEL_HIGH | A DLN adapter generates events when the high level is present on the digital input line or after transition from low to high level. For details, read DLN_GPIO_EVENT_LEVEL_HIGH Events |
DLN_GPIO_EVENT_LEVEL_LOW | A DLN adapter generates events when the low level is present on the digital input line or after transition from high to low level. For details, read DLN_GPIO_EVENT_LEVEL_LOW Events |
DLN_GPIO_EVENT_ALWAYS | A DLN adapter generates events continuously, regardless of the level. For details, read DLN_GPIO_EVENT_ALWAYS Events |
The eventPeriod
parameter defines the interval between repeated events in milliseconds (ms). This parameter must be non-zero for the DLN_GPIO_EVENT_ALWAYS
events. For other events, this parameter is optional.
If the Debounce Filter in enabled on a pin, the level change event is generated after the debounce filter accepts a new value.
You can configure events for each input pin individually. If the pin is configured as a digital input, the new settings are applied immediately. If you configure events when a pin is defined as an output or is not assigned to the GPIO module at all, this configuration is saved in the internal memory and is applied when the pin becomes a GPIO input.
When an event occurs, your application is notified (see the Notifications section). Call the DlnGetMessage() function to obtain the event details. The DLN_GPIO_CONDITION_MET_EV structure describes this information.
By default, event generation is disabled for all pins (the eventType
parameter is set to DLN_GPIO_EVENT_NONE).
The following GPIO Interface functions can be used to control and monitor events:
Configures event generation for a pin.
Retrieves event generation configuration for a pin.
Returns the list of the event types available for a pin.
A DLN adapter generates the DLN_GPIO_EVENT_CHANGE
events each time the level on the input pin changes.
Use the DlnGpioPinSetEventCfg()function to configure events. Pass DLN_GPIO_EVENT_CHANGE
for the eventType
parameter.
If the eventPeriod
parameter is zero, the DLN adapter generates single events when the level on the input pin changes.
If the eventPeriod
is non-zero, the DLN adapter resents the events periodically with the eventPeriod
interval while the level on the pin remains unchanged (a series of events).
A series of e vents begins when the level on the GPIO line changes. At this moment, the DLN adapter sends the first event in the series. The eventCount
field of the first event is set to zero and the value
field contains the actual value on the GPIO line. Then, while the level on the GPIO line remains unchanged, the DLN adapter repeatedly sends events with the same value in the value
field. The interval between recurring events is equal to eventPeriod
milliseconds. The eventCount
field increments for each event in the series. You can use this field to calculate the time passed from the last level change (time = eventCount * eventPeriod).When the level on the GPIO line changes, a new series of events begins. The eventCount
field is reset to zero and the value
field is set to the actual value present on the line after the change.
A DLN adapter generates the DLN_GPIO_EVENT_LEVEL_HIGH
events each time the level on the input pin rises.
Use the DlnGpioPinSetEventCfg()function to configure events. Pass DLN_GPIO_EVENT_LEVEL_HIGH
for the eventType
parameter.
If the eventPeriod
is zero, the DLN adapter generates single events when the level on the input pin rises (after transition from low to high level).
If the eventPeriod
is non-zero , the DLN adapter resents events periodically with the eventPeriod
interval while the level is high:
A series of events begins when the level on the GPIO line rises. At this moment, the DLN adapter sends the first event in the series. The eventCount
field of the first event is set to zero and the value
field is set to 1 to reflect the actual value on the GPIO line. Then, while the level on the GPIO line remains high, the DLN adapter repeatedly sends events. The same value in the value
field stays the same. The eventCount
field increments for every new event. The interval between recurring events is equal to eventPeriod
milliseconds. You can calculate the time passed from the last level rise (time = eventCount * eventPeriod
).
When the level on the GPIO line drops, the series of events ends. When the level on the line rises again, a new series of events begins. The eventCount
field is reset to zero.
A DLN adapter generates the DLN_GPIO_EVENT_LEVEL_LOW
events each time the level on the input pin drops.
Use the DlnGpioPinSetEventCfg()function to configure events. Pass DLN_GPIO_EVENT_LEVEL_LOW
for the eventType
parameter.
If the eventPeriod
is zero, the DLN adapter generates single events when the level on the input pin drops (after transition from high to low level).
If the eventPeriod
is non-zero, the DLN adapter resents events periodically with the eventPeriod
interval while the level is low.
A series of events begins when the level on the GPIO line drops. At this moment, the DLN adapter sends the first event in the series. The eventCount
field of the first event is set to zero and the value
field contains 0 to reflect the actual value on the GPIO line. Then, while the level on the GPIO line remains low, the DLN adapter repeatedly sends events. The value in the value
field stays the same. The eventCount
field increments for every new event. The interval between recurring events is equal to eventPeriod
milliseconds. You can calculate the time passed from the last level lowering (time = eventCount * eventPeriod
).
When the level on the GPIO line rises, the series of events ends. When the level on the line drops again, a new series of events begins. The eventCount
field is reset to zero.
A DLN adapter generates the DLN_GPIO_EVENT_ALWAYS
events periodically with the eventPeriod
interval regardless of the level on the pin and its changes. This event notifies the current signal level on a pin.
Call the DlnGpioPinSetEventCfg()function to configure events. Specify DLN_GPIO_EVENT_ALWAYS
as the eventType
parameter.
For the DLN_GPIO_EVENT_ALWAYS
events, the eventPeriod
parameter is required to be non-zero.
Immediately after you configure the DLN_GPIO_EVENT_ALWAYS
event, the DLN adapter sends the first event. The eventCount
field is set to zero and the value
field contains the actual value on the GPIO line. Then, the DLN adapter repeatedly sends events with the actual value on the line in the value
field. The interval between recurring events is equal to eventPeriod
milliseconds. The eventCount
field increments for each event in the series.
The changes of the level on the pin do not affect the eventCount
field. You can only reset it if you change the event configuration.
Contact bounce may cause faulty level changes and sending numerous events. To avoid it, the DLN-series adapters support a debounce filter. The debounce filter accepts a new signal level only if it is stable for a predefined period of time (Debounce interval).
A pulse shorter than the specified debounce interval is automatically rejected:
A pulse longer than the debounce interval is accepted:
If events are configured for the corresponding input pin, they will only be generated after the debounce filter accepts the level change.
The Debounce interval is the same for all pins.You can change it by using the DlnGpioSetDebounce() function. The Debounce interval is specified in microseconds (µs). If a DLN-series adapter does not support the specified value, it rounds it up to the nearest supported value.
You can switch the debounce filter on/off for each pin individually. To switch it on, use the DlnGpioPinDebounceEnable() function. To switch it off, use the DlnGpioPinDebounceDisable() function. If a pin is not assigned to the GPIO module, your settings are saved in the internal memory and will be applied when you assign the pin to the GPIO module.
By default, the debounce filters are disabled on all pins.
Use the following GPIO Interface functions to control and monitor the debounce filter:
Enables the Debounce Filter on a pin.
Disables the Debounce Filter on a pin
Determines whether the Debounce Filter is enabled on a pin.
Configures the debounce interval (the minimum duration of pulses to be accepted).
Retrieves the debounce interval value.
Your application may require connecting several outputs together on a single I/O line. In this case, you should use the Open Drain feature to avoid the situation when different outputs set different signal levels on the line. To avoid hardware damage all interconnected outputs should be in open drain mode.
In open drain mode, pin cannot output high level (logical 1) on the line. It can either output low level (logical 0) or be in high-impedance state (logically disconnected). In high-impedance, pin does not affect the signal level on the line. A pull-up resistor pulls the line to high voltage level when all outputs are in high-impedance.
The Open Drain Mode can be enabled/disabled on each output pin individually. To activate this feature, use the DlnGpioPinOpendrainEnable() function. To disable it, use the DlnGpioPinOpendrainDisable() function. If a pin is not assigned to the GPIO module, your settings are saved in the internal memory and will be applied when you assign the pin to the GPIO module.
By default, the Open Drain Mode is disabled on all pins.
Use the following GPIO Interface functions to control and monitor the Open Drain Mode:
Enables the Open Drain mode on a pin.
Disables the Open Drain mode on a pin.
Determines whether the Open Drain mode in enabled on a pin.
Most I/O lines are equipped with an embedded pull-up resistor. A pull-up resistor has one end connected to the positive voltage and the other end connected to an input. The pull-up resistor ensures that the level on the input is high (logic 1) when no external device is connected. If an external device is connected and outputs 0, the input detects logic 0.
A pull-down resistor works the similar way, but it is connected to ground and the logic 0 is present on the line when no external device is connected.
Pull-up and pull-down resistors can be enabled/disabled on each input pin individually.
Call the DlnGpioPinPullupEnable() function to enable the pull-up resistor. It can be disabled by using the DlnGpioPinPullupDisable() function.
To enable a pull-down resistor, use the DlnGpioPinPulldownEnable() function. To disable it, use the DlnGpioPinPulldownDisable() function.
You cannot simultaneously enable pull-up and pull-down resistors for the same pin.
If a pin is not assigned to the GPIO module, your settings are saved in the internal memory and will be applied when you assign the pin to the GPIO module.
By default, pull-up resistors are enabled for all pins. The pull-down resisters are disabled by default.
Use the following GPIO Interface functions to control and monitor pull-up resistors:
Enables the pull-up resistor on a pin.
Disables the pull-up resistor on a pin.
Determines whether the pull-up resistor is enabled on a pin.
Use the following GPIO Interface functions to control and monitor pull-down resistors:
Enables the pull-down resistor on a pin.
Disables the pull-down resistor on a pin.
Determines whether the pull-down resistor is enabled on a pin.
When you assign a pin to the GPIO module, it has the following default configuration:
The pin is configured as an input.
Events are disabled for the pin.
Debounce filtering is turned off.
An Open Drain mode is not active.
An embedded pull-up resistor is active.
An embedded pull-down resistor is not active.
You can change the configuration before or after you assign a pin to the GPIO module. If a pin is not currently assigned to the GPIO module, your settings are saved in the internal memory and will be applied when you assign the pin to the GPIO module.
The following example shows how to enable/disable and set GPIO pin parameters, such as direction, output value and read GPIO pin value, when its direction set to input. You can find the complete example in the “..\Program Files\Diolan\DLN\examples\c_cpp\examples\simple
” folder after DLN setup package installation.
#include "..\..\..\common\dln_generic.h" #include "..\..\..\common\dln_gpio.h" #pragma comment(lib, "..\\..\\..\\bin\\dln.lib") int _tmain(int argc, _TCHAR* argv[]) { // Open device HDLN device; DlnOpenUsbDevice(&device); // Configure pin 0 to input with pullup DlnGpioPinPullupEnable(device, 0); DlnGpioPinSetDirection(device, 0, 0); DlnGpioPinEnable(device, 0); // Configure pin 1 to output DlnGpioPinSetDirection(device, 1, 1); DlnGpioPinEnable(device, 1); // Read pin 0 and set its iverted value on pin 1 uint8_t value; DlnGpioPinGetVal(device, 0, &value); DlnGpioPinSetOutVal(device, 1, value ^ 1); // Disable buttons DlnGpioPinDisable(device, 0); DlnGpioPinDisable(device, 0); // Close device DlnCloseHandle(device); return 0; }
Line 1:#include "..\..\..\common\dln_generic.h"
The dln_generic..h
header file declares functions and data structures for the generic interface
Line 2:#include "..\..\..\common\dln_gpio.h"
The dln_gpio.h header file declares functions and data structures for GPIO interface.
Line 3:#pragma comment(lib, "..\\..\\..\\bin\\dln.lib")
Use dln.lib
library while project linking.
Line 10:DlnOpenUsbDevice(&device);
The function establishes the connection with the DLN adapter. This application uses the USB connectivity of the adapter. For additional options, refer to the Device Opening & Identification section.
Line 13:DlnGpioPinPullupEnable(device, 0);
Enable pullup for GPIO pin 0. You can read more about pullups at Pull-up/Pull-down Resistors section.
Line 14:DlnGpioPinSetDirection(device, 0, 0);
Set GPIO pin 0 to input. For more information read Digital Inputs section.
Line 15:DlnGpioPinEnable(device, 0);
Enable GPIO pin 0.
Line 18:DlnGpioPinSetDirection(device, 1, 1);
Set GPIO pin 1 direction to output. For more information read Digital Outputs section.
Line 19:DlnGpioPinEnable(device, 1);
Enable GPIO pin 1.
Line 23:DlnGpioPinGetVal(device, 0, &value);
Get value on GPIO pin 0.
Line 24:DlnGpioPinSetOutVal(device, 1, value ^ 1);
Set output value on the GPIO pin 1, which is opposite to the value variable.
Line 27:DlnGpioPinDisable(device, 0);
Disable GPIO pin 0.
Line 28:DlnGpioPinDisable(device, 1);
Disable GPIO pin 1.
Line 30:DlnCloseHandle(device);
The application closes the handle to the connected DLN-series adapter.
Use the GPIO Interface functions to control and monitor the GPIO module of a DLN-series adapter.
General pin information:
Retrieves the number of pins that can be assigned to the GPIO module.
Assigns a pin to the GPIO module.
Unassigns a pin from the GPIO module.
Retrieves whether a pin is connected to the GPIO module.
Configures a pin as a digital input or output.
Retrieves whether a pin is configured as a digital input or output.
Defines a value on the output pin.
Retrieves the output value configured to the pin.
Retrieves the actual value on the I/O line.
Event functions:
Configures event generation for a pin.
Retrieves event generation configuration for a pin.
Returns the list of the event types available for a pin.
Debounce filter functions:
Enables the Debounce Filter on a pin.
Disables the Debounce Filter on a pin
Determines whether the Debounce Filter is enabled on a pin.
Configures the debounce interval (the minimum duration of pulses to be registered).
Retrieves the debounce interval value.
Open Drain mode functions:
Enables the Open Drain mode on a pin.
Disables the Open Drain mode on a pin.
Determines whether the Open Drain mode in enabled on a pin.
Pull-up/Pull-down resistors functions:
Enables the pull-up resistor on a pin.
Enables the pull-down resistor on a pin.
Disables the pull-up resistor on a pin.
Disables the pull-down resistor on a pin.
Determines whether the pull-up resistor is enabled on a pin.
Determines whether the pull-down resistor is enabled on a pin.
The dln_gpio.h
file declares the GPIO Interface functions.
The DlnGpioGetPinCount()
function retrieves the total number of GPIO pins available in the DLN-series adapter.
The DlnGpioPinEnable()
function connects a pin to the GPIO module.
The DlnGpioPinDisable()
function disconnects a pin from the GPIO module. Then, another module can use the pin.
The DlnGpioPinIsEnabled()
function informs whether the GPIO module currently uses the pin.
The DlnGpioPinSetDirection()
function configures a pin as an input or as an output.
The DlnGpioPinGetDirection()
function retrieves current direction of a GPIO pin.
The DlnGpioPinGetVal()
function retrieves the current value on the specified GPIO pin.
The DlnGpioPinSetOutVal()
function sets the output value for the specified GPIO pin.
The DlnGpioPinGetOutVal()
function retrieves the pin output value.
The DlnGpioPinSetEventCfg()
function configures when and which events should be generated for the specified pin. For more information, read Digital Input Events.
The DlnGpioPinGetEventCfg()
function retrieves the current event configuration for the specified pin. For more information, read Digital Input Events.
The DlnGpioPinGetSupportedEventTypes()
function returns all event types supported for the specified pin.
The DlnGpioPinDebounceEnable()
function enables Debounce Filter for the specified pin.
The DlnGpioPinDebounceDisable()
disables Debounce Filter for the specified pin.
The DlnGpioPinDebounceIsEnabled()
function informs whether the Debounce Filter is currently enabled for the specified pin.
The DlnGpioSetDebounce()
function specifies the debounce interval (the minimum duration of pulses to be registered). See Debounce Filter for details.
The DlnGpioGetDebounce()
function retrieves the current value of the debounce interval (the minimum duration of the pulse to be registered). See Debounce Filter for details.
The DlnGpioPinOpendrainEnable()
function enables Open Drain Mode for the specified pin.
The DlnGpioPinOpendrainDisable()
disables Open Drain Mode for the specified pin.
The DlnGpioPinOpendrainIsEnabled()
function informs whether the pin output is currently configured as push-pull or Open Drain.
The DlnGpioPinPullupEnable()
function activates an embedded pull-up resistor for the specified pin. For more information, read Pull-up/Pull-down Resistors.
The DlnGpioPinPullupDisable()
deactivates an embedded pull-up resistor for the specified pin. For more information, read Pull-up/Pull-down Resistors.
The DlnGpioPinPullupIsEnabled()
function informs whether an embedded pull-up resistor is currently enabled for the specified pin.
The DlnGpioPinPulldownEnable()
function activates an embedded pull-down resistor for the specified pin. For more information, read Pull-up/Pull-down Resistors.
The DlnGpioPinPulldownDisable()
function deactivates an embedded pull-down resistor for the specified pin.
The DlnGpioPinPulldownIsEnabled()
function informs whether an embedded pull-down resistor is active for the specified pin.
The DlnGpioPinSetCfg()
function changes the configuration of a single GPIO pin. It allows customizing the following:
Define the pin direction (input or output).
Enable or disable the open drain, pull up resistor and/or debounce filter.
Define the output value.
Define input event parameters.
With this function, you can either reconfigure the pin entirely or change only some of its parameters.
The DlnGpioPinGetCfg()
function retrieves the current configuration of the specified GPIO pin.
This section describes the structure used for GPIO events. This structure is declared in the dln_gpio.h
file.
The DLN_GPIO_CONDITION_MET_EV
structure contains information about the current GPIO event on a pin.
This section describes the structures used for the GPIO module. These structures are declared in the dln_gpio.h
file.
The DLN_GPIO_PIN_EVENT_TYPES
structure contains information about the event types supported for a pin.
The DLN_GPIO_PIN_CONFIG
structure contains information about the pin configuration.