dnx RTOS 2.2.0 "Eagle"
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
GPIO Driver

Detailed Description

Description

Driver handles GPIO peripheral.

Supported architectures

Details

Meaning of major and minor numbers

Some manufactures enumerate devices starting from 1 instead of 0 (e.g. ST). In this case major number starts from 0 and is connected to the first device e.g. GPIOA. Major number selects GPIO peripheral.

Numeration restrictions

Number of peripherals determines how big the major number can be. If there is only one GPIO peripheral then the major number is always 0.

Driver initialization

To initialize driver the following code can be used:

driver_init("GPIO", 0, 0, "/dev/GPIOA");
driver_init("GPIO", 1, 0, "/dev/GPIOB");

Driver release

To release driver the following code can be used:

driver_release("GPIO", 0, 0);
driver_release("GPIO", 1, 0);

Driver configuration

Driver configuration can be done by using Configtool or configuration files.

Data write

Data to the GPIO device can be wrote as regular file. The entire wide of port is used to send bytes. If port is 16-bit then entire word will set output pins to selected state.

Data read

Data to the GPIO device can be read as regular file. The entire word of port is read to buffer in read operation.

Pin control

Each bit can be controlled by using ioctl() function. There is possibility to control selected pin on not opened port for example: user opened GPIOA and by using IOCTL_GPIO__SET_PIN_IN_PORT ioctl()'s request can control pin in GPIOB. This is possible only by port index.

#include <stdio.h>
#include <sys/ioctl.h>
FILE *f = fopen("/dev/GPIOA", "r+);
if (f) {
// control pin on port A...
static const u8_t LED = IOCTL_GPIO_PIN_IDX__LED;
ioctl(fileno(f), IOCTL_GPIO__CLEAR_PIN, &LED);
// ... this same can be done by using alternative interface:
static const GPIO_pin_in_port_t LED_PORT = {
.pin_idx = IOCTL_GPIO_PIN_IDX__LED,
.port_idx = IOCTL_GPIO_PORT_IDX__LED
};
ioctl(fileno(f), IOCTL_GPIO__CLEAR_PIN_IN_PORT, &LED_PORT);
// there is possiblity to control pin on other port
static const GPIO_pin_in_port_t OTHER_PIN = {
.pin_idx = IOCTL_GPIO_PIN_IDX__OTHER_PIN,
.port_idx = IOCTL_GPIO_PORT_IDX__OTHER_PIN
};
ioctl(fileno(f), IOCTL_GPIO__SET_PIN_IN_PORT, &OTHER_PIN);
fclose(f);
} else {
perror(NULL);
}

There are restrictions that allow user to control pins by using other port file: the port that is controlled should be earlier initialized. If port is not initialized then pin behavior is undefined (depends on microcontroller).

Data Structures

struct  GPIO_pin_state_t
 
struct  GPIO_pin_in_port_t
 
struct  GPIO_pin_in_port_state_t
 

Macros

#define IOCTL_GPIO_PIN_IDX__NULL   0xFF
 Not existing pin number. More...
 
#define IOCTL_GPIO_PORT_IDX__NULL   0xFF
 Not existing port number. More...
 
#define IOCTL_GPIO__SET_PIN   _IOW(GPIO, 0, const u8_t*)
 Set selected pin (set pin to Hi state). More...
 
#define IOCTL_GPIO__CLEAR_PIN   _IOW(GPIO, 1, const u8_t*)
 Clear selected pin (set pin to Low state). More...
 
#define IOCTL_GPIO__TOGGLE_PIN   _IOW(GPIO, 2, const u8_t*)
 Toggle pin state. More...
 
#define IOCTL_GPIO__SET_PIN_STATE   _IOW(GPIO, 3, const GPIO_pin_state_t*)
 Set pin state. More...
 
#define IOCTL_GPIO__GET_PIN_STATE   _IOR(GPIO, 4, GPIO_pin_state_t*)
 Gets pin state. More...
 
#define IOCTL_GPIO__SET_PIN_IN_PORT   _IOW(GPIO, 5, GPIO_pin_in_port_t*)
 Set selected pin in port (set pin to Hi state). More...
 
#define IOCTL_GPIO__CLEAR_PIN_IN_PORT   _IOW(GPIO, 6, GPIO_pin_in_port_t*)
 Set selected pin in port (set pin to Low state). More...
 
#define IOCTL_GPIO__TOGGLE_PIN_IN_PORT   _IOW(GPIO, 7, GPIO_pin_in_port_t*)
 Toggle selected pin in port. More...
 
#define IOCTL_GPIO__SET_PIN_STATE_IN_PORT   _IOW(GPIO, 8, GPIO_pin_in_port_state_t*)
 Set pin state in selected port. More...
 
#define IOCTL_GPIO__GET_PIN_STATE_IN_PORT   _IOR(GPIO, 9, GPIO_pin_in_port_state_t*)
 Gets pin state in selected port. More...
 

Data Structure Documentation

struct GPIO_pin_state_t

Type used to read pin state

Data Fields
u8_t pin_idx

Pin index.

int state

Pin state.

struct GPIO_pin_in_port_t

Type represent pin on port selected by index.

Data Fields
u8_t pin_idx

Pin index.

u8_t port_idx

Port index.

struct GPIO_pin_in_port_state_t

Type represent pin state on port selected by index.

Data Fields
u8_t pin_idx

Pin index.

u8_t port_idx

Port index.

int state

Pin state.

Macro Definition Documentation

#define IOCTL_GPIO__CLEAR_PIN   _IOW(GPIO, 1, const u8_t*)
Parameters
[WR]u8_t* pin index
Returns
On success 0 is returned, otherwise -1 and errno code is set
#define IOCTL_GPIO__CLEAR_PIN_IN_PORT   _IOW(GPIO, 6, GPIO_pin_in_port_t*)
Parameters
[WR]GPIO_pin_in_port_t* pin and port index
Returns
On success 0 is returned, otherwise -1 and errno code is set
#define IOCTL_GPIO__GET_PIN_STATE   _IOR(GPIO, 4, GPIO_pin_state_t*)
Parameters
[RD]GPIO_pin_state_t* pin index and read state
Returns
On success 0 is returned, otherwise -1 and errno code is set
#define IOCTL_GPIO__GET_PIN_STATE_IN_PORT   _IOR(GPIO, 9, GPIO_pin_in_port_state_t*)
Parameters
[RD]GPIO_pin_in_port_state_t* pin and port index and read state
Returns
On success 0 is returned, otherwise -1 and errno code is set
#define IOCTL_GPIO__SET_PIN   _IOW(GPIO, 0, const u8_t*)
Parameters
[WR]u8_t* pin index
Returns
On success 0 is returned, otherwise -1 and errno code is set
#define IOCTL_GPIO__SET_PIN_IN_PORT   _IOW(GPIO, 5, GPIO_pin_in_port_t*)
Parameters
[WR]GPIO_pin_in_port_t* pin and port index
Returns
On success 0 is returned, otherwise -1 and errno code is set
#define IOCTL_GPIO__SET_PIN_STATE   _IOW(GPIO, 3, const GPIO_pin_state_t*)
Parameters
[WR]GPIO_pin_state_t* pin index and state to set
Returns
On success 0 is returned, otherwise -1 and errno code is set
#define IOCTL_GPIO__SET_PIN_STATE_IN_PORT   _IOW(GPIO, 8, GPIO_pin_in_port_state_t*)
Parameters
[WR]GPIO_pin_in_port_state_t* pin and port index and state to set
Returns
On success 0 is returned, otherwise -1 and errno code is set
#define IOCTL_GPIO__TOGGLE_PIN   _IOW(GPIO, 2, const u8_t*)
Parameters
[WR]u8_t* pin index
Returns
On success 0 is returned, otherwise -1 and errno code is set
#define IOCTL_GPIO__TOGGLE_PIN_IN_PORT   _IOW(GPIO, 7, GPIO_pin_in_port_t*)
Parameters
[WR]GPIO_pin_in_port_t* pin and port index
Returns
On success 0 is returned, otherwise -1 and errno code is set
#define IOCTL_GPIO_PIN_IDX__NULL   0xFF
#define IOCTL_GPIO_PORT_IDX__NULL   0xFF