Breadcrumbs

Developing IO Access in Applications

This page only gives some hints about io access.

RS232/RS485

In typical Linux style, serial ports are represented by files within the operating system. These files usually pop-up in /dev/, and begin with the name tty*.

Common names are:


COM1

COM2

COM3

CP 3x

/dev/ttymxc3 (RS232)

/dev/ttymxc2 (RS232)

/dev/ttymxc4 (RS485)

WP 4x

/dev/ttymxc3 (RS232)

-

-

SOM imx6ul EvalBoard

/dev/ttymxc0 (RS232)

/dev/ttymxc1 (RS485)

-

SOM imx8mm EvalBoard

/dev/ttymxc0 (RS232)

/dev/ttymxc1 (RS485)

-

Example:

C++
int serial_port = open("/dev/ttymxc3 ", O_RDWR);

// Check for errors
if (serial_port < 0) {
    printf("Error %i from open: %s\n", errno, strerror(errno));
}

unsigned char msg[] = { 'H', 'e', 'l', 'l', 'o', '\r' };
write(serial_port, msg, sizeof(msg));

close(serial_port);


RS485:

In the standard devices a rs485 interface can be used in the same way as a rs232.

No read/write gpio setting is needed, because this is already predefined in the linux device tree.

CAN

The Linux C/C API allows you to control a SocketCAN interface via a C/C application.

SocketCAN supports standard frame format (SFF), extended frame format (EFF) and CAN FD frames.

Example:

C++
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <net/if.h>
#include <sys/ioctl.h>
#include <sys/socket.h>

#include <linux/can.h>
#include <linux/can/raw.h>

int main(int argc, char **argv)
{
	int s; 
	struct sockaddr_can addr;
	struct ifreq ifr;
	struct can_frame frame;

	printf("CAN Sockets Demo\r\n");

	if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
		perror("Socket");
		return 1;
	}

	strcpy(ifr.ifr_name, "can0" );
	ioctl(s, SIOCGIFINDEX, &ifr);

	memset(&addr, 0, sizeof(addr));
	addr.can_family = AF_CAN;
	addr.can_ifindex = ifr.ifr_ifindex;

	if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
		perror("Bind");
		return 1;
	}

	frame.can_id = 0x555;
	frame.can_dlc = 5;
	sprintf(frame.data, "Hello");

	if (write(s, &frame, sizeof(struct can_frame)) != sizeof(struct can_frame)) {
		perror("Write");
		return 1;
	}

	if (close(s) < 0) {
		perror("Close");
		return 1;
	}

	return 0;
}

Ethernet

For ethernet commmunication the Linux networking socket layer user interface is used.

The BSD compatible sockets are the uniform interface between the user process and the network protocol stacks in the kernel.

The protocol modules are grouped into protocol families such as AF_INET, AF_IPX, and AF_PACKET, and socket types such as SOCK_STREAM or SOCK_DGRAM.

The usually interface names begin with the name eth*.

GPIO

The Linux kernel includes a user space API based on character devices for managing and controlling GPIOs ( General-Purpose Input/Output). 

Common gpio's are:

CP 3x

Name

GPIO Device

GPIO Offset

Type

GPIO3_IO00 (X4)

/dev/gpiochip2

0

Input/Output

GPIO2_IO24 (X4)

/dev/gpiochip1

24

Input/Output

GPIO3_IO02 (X4)

/dev/gpiochip2

2

Input/Output

GPIO2_IO26 (X4)

/dev/gpiochip1

26

Input/Output

GPIO2_IO27 (X4)

/dev/gpiochip1

27

Input/Output

GPIO2_IO28 (X4)

/dev/gpiochip2

28

Input/Output

GPIO2_IO29 (X4)

/dev/gpiochip1

29

Input/Output

GPIO2_IO30 (X4)

/dev/gpiochip1

30

Input/Output

GPIO2_IO31 (X4)

/dev/gpiochip1

31

Input/Output

WP 4x

Name

GPIO Device

GPIO Offset

Type

EXT_GPIO1 (X20)

/dev/gpiochip3

21

Input/Output

EXT_GPIO2 (X20)

/dev/gpiochip3

22

Input/Output

EXT_GPIO3 (X20)

/dev/gpiochip3

23

Input/Output

EXT_GPIO4 (X20)

/dev/gpiochip3

24

Input/Output

EXT_GPIO5 (X20)

/dev/gpiochip3

25

Input/Output

EXT_GPIO6 (X20)

/dev/gpiochip3

26

Input/Output

SOM imx6ul EvalBoard

Name

GPIO Device

GPIO Offset

Type

DI1 (X19)

/dev/gpiochip4

4

Input

DI2 (X19)

/dev/gpiochip4

0

Input

DO1 (X19)

/dev/gpiochip4

5

Output

DO2 (X19)

/dev/gpiochip4

1

Output

SOM imx8mm EvalBoard

Name

GPIO Device

GPIO Offset

Type

DIO1 (X13 - Pin 1)

/dev/gpiochip0

3

Output

DIO1 (X13 - Pin 1)

/dev/gpiochip0

6

Input

DIO2 (X13 - Pin 3)

/dev/gpiochip0

7

Output

DIO2 (X13 - Pin 3)

/dev/gpiochip0

8

Input

DIO3 (X13 - Pin 5)

/dev/gpiochip0

9

Output

DIO3 (X13 - Pin 5)

/dev/gpiochip0

10

Input

DIO4 (X13 - Pin 7)

/dev/gpiochip0

11

Output

DIO4 (X13 - Pin 7)

/dev/gpiochip4

2

Input

Example:

C++
#include <linux/gpio.h>
 
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <stdint.h>
#include <stdlib.h>
 
#define DEV_NAME "/dev/gpiochip0"
 
int fd, ret;
fd = open(DEV_NAME, O_RDONLY);
if (fd < 0)
{
    printf("Unabled to open %s: %s", dev_name, strerror(errno));
    return;
}
/*
    control GPIO here, such as:
    - configure
    - read
    - write
    - polling
*/
(void)close(fd)


Additional Information:

https://blog.lxsang.me/post/id/33


ADC/AIN

Analog input, also known as Analog to Digital Converter (ADC), is a hardware peripheral that can read an analog voltage value and convert it to a digital value that can be used by the processor.

For Linux the ADC subsystem is abstracted through the sysfs interface.

SOM imx6ul EvalBoard

HW Port

CODESYS Device Name

ADC Resolution

Input Voltage Range

AIN1  (X19)

'/sys/bus/iio/devices/iio\:device0/in_voltage3_raw'

12bits

0..3.3V

AIN2  (X19)

'/sys/bus/iio/devices/iio\:device0/in_voltage8_raw'

12bits

0..3.3V

I2C

Todo

SPI

Tod