Peripheral


This chapter describes the common information and instructions of Peripheral on IoT Yocto, such as listing peripheral hardware, operating devices, and so on. Peripheral on different platforms may have some platform-specific instructions or test results. For example, you will use different peripheral interfaces on different platforms. Please refer to the platform-specific section for more details.


Note

All cmd operations presented in this chapter are based on the IoT Yocto v22.1, Genio 350-EVK. You might get different operation result depends on what platform you currently use.

I2C

There are some I2C tools to operate I2C adapters and devices.

i2cdetect will list all available I2C adapters and the I2C devices on a specific I2C bus. You can see the connected I2C device address, e.g. 0x53, and 0x57.

i2cdetect -l
i2c-3   i2c             i2c-mt65xx                              I2C adapter
i2c-1   i2c             i2c-mt65xx                              I2C adapter
i2c-2   i2c             i2c-mt65xx                              I2C adapter
i2c-0   i2c             i2c-mt65xx                              I2C adapter
i2cdetect -y 0
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- 53 -- -- -- 57 -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

Note

Make sure you don’t have more than one device with the same address connected to the same I2C bus. Otherwise, the address will conflict.

i2cdump will list all registers of a specific I2C device.

i2cdump -y 0 0x57
No size specified (using byte-data access)
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f    0123456789abcdef
00: a5 a5 92 00 00 81 63 02 00 0f 00 00 00 00 00 00    ???..?c?.?......
10: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
20: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
...

i2cset and i2cget will write and read the register value.

i2cget -f -y 0 0x57 0x2
0x92
i2cset -f -y 0 0x57 0x2 0x93
i2cget -f -y 0 0x57 0x2
0x93

Please refer to the platform-specific section for more details on each platform.

SPI

First, you need to probe the SPI device manually by the command:

modprobe spidev

After probing, you can find an SPI device under /dev.

ls -l /dev/spidev0.0
crw------- 1 root root 153, 0 Sep 20 12:22 /dev/spidev0.0

To test the SPI device, you can use a pre-installed tool, spidev_test. It verifies that the data sent by Tx should be the same as the data received by Rx.

Note

Before using spidev_test to test, SPI_MOSI and SPI_MISO should be shorted.

dd if=/dev/random of=test.bin bs=96 count=1
1+0 records in
1+0 records out
96 bytes copied, 0.000968692 s, 99.1 kB/s
spidev_test -D /dev/spidev0.0 -s 400000 -i test.bin -v
spi mode: 0x0
bits per word: 8
max speed: 400000 Hz (400 kHz)
TX | A3 62 7A EB CA 8E 69 98 1C A9 A8 E4 2C 9A 6A 0E 94 DA EC 34 84 28 FC AE 7F 27 91 E7 B9 7E D6 32  |.bz...i.....,.j....4.(...'...~.2|
TX | 21 4A 08 0A B5 65 B3 3D D7 63 CC 6B 4C 77 A4 D4 C8 FB 57 93 B7 91 4A 34 F6 16 76 FB 72 9C C9 66  |!J...e.=.c.kLw....W...J4..v.r..f|
TX | 43 FE E0 90 54 62 E4 BC CF BF 36 81 D4 95 BF FB EF 84 66 E1 30 E0 D2 24 E5 DD 98 24 C0 F8 A1 E3  |C...Tb....6.......f.0..$...$....|
RX | A3 62 7A EB CA 8E 69 98 1C A9 A8 E4 2C 9A 6A 0E 94 DA EC 34 84 28 FC AE 7F 27 91 E7 B9 7E D6 32  |.bz...i.....,.j....4.(...'...~.2|
RX | 21 4A 08 0A B5 65 B3 3D D7 63 CC 6B 4C 77 A4 D4 C8 FB 57 93 B7 91 4A 34 F6 16 76 FB 72 9C C9 66  |!J...e.=.c.kLw....W...J4..v.r..f|
RX | 43 FE E0 90 54 62 E4 BC CF BF 36 81 D4 95 BF FB EF 84 66 E1 30 E0 D2 24 E5 DD 98 24 C0 F8 A1 E3  |C...Tb....6.......f.0..$...$....|

You can refer to the source of spidev_test here for how to use the SPI device.

Please refer to the platform-specific section for more details on each platform.

USB

The USB ports on the board may have different usages, such as USB master/slave, image flush port, and the console port. Please refer to the platform-specific section for more details on each platform.

PWM

Please refer to the platform-specific section for more details on each platform.

UART

You can use sysfs to find out the UART devices.

ls -l /sys/class/tty/ttyS*
lrwxrwxrwx 1 root root 0 Sep 20 10:43 /sys/class/tty/ttyS0 -> ../../devices/platform/soc/11002000.uart0/tty/ttyS0
lrwxrwxrwx 1 root root 0 Sep 20 10:43 /sys/class/tty/ttyS1 -> ../../devices/platform/soc/11003000.uart1/tty/ttyS1
lrwxrwxrwx 1 root root 0 Sep 20 10:43 /sys/class/tty/ttyS2 -> ../../devices/platform/soc/11004000.uart2/tty/ttyS2

By default, the console log is transferred to the host through ttyS0.

Please refer to the platform-specific section for more details on each platform.

Note

The UART DMA mode (CONFIG_MTK_UART_APDMA) is enabled by default. If you prefer not to use the DMA mode, you will need to disable it manually.

GPIO

You can use GPIO tools to operate the GPIO pin.

gpiodetect will list all available GPIO chips.

gpiodetect
gpiochip0 [1000b000.pinctrl] (156 lines)

gpioinfo will list all states of GPIO pins.

gpioinfo | head
gpiochip0 - 156 lines:
        line   0:      unnamed       unused   input  active-high
        line   1:      unnamed       unused   input  active-high
        line   2:      unnamed       unused   input  active-high
        line   3:      unnamed       unused   input  active-high
        line   4:      unnamed       unused   input  active-high
        line   5:      unnamed       unused   input  active-high
        line   6:      unnamed       unused   input  active-high
        line   7:      unnamed       unused   input  active-high
        line   8:      unnamed       unused   input  active-high
        ...

gpioset and gpioget will write and read the specific GPIO pin. If you short GPIO pin 32 and GPIO pin 34, you can set one pin and get the value back from another pin.

gpioset gpiochip0 32=0
gpioget gpiochip0 34
0
gpioset gpiochip0 32=1
gpioget gpiochip0 34
1

Please refer to the platform-specific section for more details on each platform.

Pin Header

The boards have the pin header. Some follow Raspberry Pi HAT configuration and some don’t. Please refer to the platform-specific section for more details.

UVC Gadget

This section covers the usage of UVC gadget for Genio 510/700/1200-EVK. It includes instructions on generating the gadget node, setting up and launching the camera, and the interaction between the PC and EVK. This section will use the Genio 700-EVK USB port0 micro-b for demonstrating the UVC gadget example.

Note

The Genio 350-EVK lacks a USB 3.0 port, so its UVC gadget may be limited by the USB 2.0 bandwidth.

Firstly, in accordance with Linux USB gadget , UVC configuration through Configfs is required. Please refer to the uvc.sh script below.

uvc.sh
 #! /bin/sh

 cd  /sys/kernel/config/usb_gadget/g1
 echo "" > UDC
 rm -rf configs/c.1/ffs.adb
 rm -rf configs/c.1/rndis.usb0
 echo "0x046D" > idVendor
 echo "0x087C" > idProduct
 echo "0x0010" > bcdDevice
 echo "0xef" > bDeviceClass
 echo "0x02" > bDeviceSubClass
 echo "0x01" > bDeviceProtocol
 mkdir strings/0x409
 echo "0123456789ABCDEF" > strings/0x409/serialnumber
 echo "Mediatek Inc." > strings/0x409/manufacturer
 echo "uvc gadget" > strings/0x409/product
 mkdir functions/uvc.usb0
 mkdir functions/uvc.usb0/control/header/h
 echo 3072 > functions/uvc.usb0/streaming_maxpacket
 echo 7 > functions/uvc.usb0/streaming_maxburst
 cd functions/uvc.usb0/control/
 ln -s header/h class/fs
 ln -s header/h class/ss
 cd ../../../
 mkdir -p functions/uvc.usb0/streaming/uncompressed/u/360p
 cat <<EOF > functions/uvc.usb0/streaming/uncompressed/u/360p/bmCapabilities
 0
 EOF
 cat <<EOF > functions/uvc.usb0/streaming/uncompressed/u/360p/wWidth
 640
 EOF
 cat <<EOF > functions/uvc.usb0/streaming/uncompressed/u/360p/wHeight
 360
 EOF
 cat <<EOF > functions/uvc.usb0/streaming/uncompressed/u/360p/dwMinBitRate
 55296000
 EOF
 cat <<EOF > functions/uvc.usb0/streaming/uncompressed/u/360p/dwMaxBitRate
 55296000
 EOF
 cat <<EOF > functions/uvc.usb0/streaming/uncompressed/u/360p/dwMaxVideoFrameBufferSize
 460800
 EOF
 cat <<EOF > functions/uvc.usb0/streaming/uncompressed/u/360p/dwDefaultFrameInterval
 333333
 EOF
 cat <<EOF > functions/uvc.usb0/streaming/uncompressed/u/360p/dwFrameInterval
 333333
 EOF
 mkdir -p functions/uvc.usb0/streaming/uncompressed/u/720p
 cat <<EOF > functions/uvc.usb0/streaming/uncompressed/u/720p/bmCapabilities
 0
 EOF
 cat <<EOF > functions/uvc.usb0/streaming/uncompressed/u/720p/wWidth
 1280
 EOF
 cat <<EOF > functions/uvc.usb0/streaming/uncompressed/u/720p/wHeight
 720
 EOF
 cat <<EOF > functions/uvc.usb0/streaming/uncompressed/u/720p/dwMinBitRate
 147456000
 EOF
 cat <<EOF > functions/uvc.usb0/streaming/uncompressed/u/720p/dwMaxBitRate
 147456000
 EOF
 cat <<EOF > functions/uvc.usb0/streaming/uncompressed/u/720p/dwMaxVideoFrameBufferSize
 1843200
 EOF
 cat <<EOF > functions/uvc.usb0/streaming/uncompressed/u/720p/dwDefaultFrameInterval
 333333
 EOF
 cat <<EOF > functions/uvc.usb0/streaming/uncompressed/u/720p/dwFrameInterval
 333333
 EOF
 mkdir -p functions/uvc.usb0/streaming/uncompressed/u/1080p
 cat <<EOF > functions/uvc.usb0/streaming/uncompressed/u/1080p/bmCapabilities
 0
 EOF
 cat <<EOF > functions/uvc.usb0/streaming/uncompressed/u/1080p/wWidth
 1920
 EOF
 cat <<EOF > functions/uvc.usb0/streaming/uncompressed/u/1080p/wHeight
 1080
 EOF
 cat <<EOF > functions/uvc.usb0/streaming/uncompressed/u/1080p/dwMinBitRate
 147456000
 EOF
 cat <<EOF > functions/uvc.usb0/streaming/uncompressed/u/1080p/dwMaxBitRate
 147456000
 EOF
 cat <<EOF > functions/uvc.usb0/streaming/uncompressed/u/1080p/dwMaxVideoFrameBufferSize
 1843200
 EOF
 cat <<EOF > functions/uvc.usb0/streaming/uncompressed/u/1080p/dwDefaultFrameInterval
 333333
 EOF
 cat <<EOF > functions/uvc.usb0/streaming/uncompressed/u/1080p/dwFrameInterval
 333333
 EOF
 mkdir -p functions/uvc.usb0/streaming/mjpeg/m/360p
 cat <<EOF > functions/uvc.usb0/streaming/mjpeg/m/360p/bmCapabilities
 0
 EOF
 cat <<EOF > functions/uvc.usb0/streaming/mjpeg/m/360p/wWidth
 640
 EOF
 cat <<EOF > functions/uvc.usb0/streaming/mjpeg/m/360p/wHeight
 360
 EOF
 cat <<EOF > functions/uvc.usb0/streaming/mjpeg/m/360p/dwMinBitRate
 18432000
 EOF
 cat <<EOF > functions/uvc.usb0/streaming/mjpeg/m/360p/dwMaxBitRate
 55296000
 EOF
 cat <<EOF > functions/uvc.usb0/streaming/mjpeg/m/360p/dwMaxVideoFrameBufferSize
 460800
 EOF
 cat <<EOF > functions/uvc.usb0/streaming/mjpeg/m/360p/dwDefaultFrameInterval
 333333
 EOF
 cat <<EOF > functions/uvc.usb0/streaming/mjpeg/m/360p/dwFrameInterval
 333333
 1000000
 5000000
 EOF
 mkdir -p functions/uvc.usb0/streaming/mjpeg/m/720p
 cat <<EOF > functions/uvc.usb0/streaming/mjpeg/m/720p/bmCapabilities
 0
 EOF
 cat <<EOF > functions/uvc.usb0/streaming/mjpeg/m/720p/wWidth
 1280
 EOF
 cat <<EOF > functions/uvc.usb0/streaming/mjpeg/m/720p/wHeight
 720
 EOF
 cat <<EOF > functions/uvc.usb0/streaming/mjpeg/m/720p/dwMinBitRate
 29491200
 EOF
 cat <<EOF > functions/uvc.usb0/streaming/mjpeg/m/720p/dwMaxBitRate
 29491200
 EOF
 cat <<EOF > functions/uvc.usb0/streaming/mjpeg/m/720p/dwMaxVideoFrameBufferSize
 1843200
 EOF
 cat <<EOF > functions/uvc.usb0/streaming/mjpeg/m/720p/dwDefaultFrameInterval
 333333
 EOF
 cat <<EOF > functions/uvc.usb0/streaming/mjpeg/m/720p/dwFrameInterval
 333333
 EOF
 mkdir -p functions/uvc.usb0/streaming/mjpeg/m/1080p
 cat <<EOF > functions/uvc.usb0/streaming/mjpeg/m/1080p/bmCapabilities
 0
 EOF
 cat <<EOF > functions/uvc.usb0/streaming/mjpeg/m/1080p/wWidth
 1920
 EOF
 cat <<EOF > functions/uvc.usb0/streaming/mjpeg/m/1080p/wHeight
 1080
 EOF
 cat <<EOF > functions/uvc.usb0/streaming/mjpeg/m/1080p/dwMinBitRate
 165888000
 EOF
 cat <<EOF > functions/uvc.usb0/streaming/mjpeg/m/1080p/dwMaxBitRate
 995328000
 EOF
 cat <<EOF > functions/uvc.usb0/streaming/mjpeg/m/1080p/dwMaxVideoFrameBufferSize
 960000
 EOF
 cat <<EOF > functions/uvc.usb0/streaming/mjpeg/m/1080p/dwDefaultFrameInterval
 333333
 EOF
 cat <<EOF > functions/uvc.usb0/streaming/mjpeg/m/1080p/dwFrameInterval
 333333
 EOF
 mkdir -p functions/uvc.usb0/streaming/mjpeg/m/2160p
 cat <<EOF > functions/uvc.usb0/streaming/mjpeg/m/2160p/bmCapabilities
 0
 EOF
 cat <<EOF > functions/uvc.usb0/streaming/mjpeg/m/2160p/wWidth
 3840
 EOF
 cat <<EOF > functions/uvc.usb0/streaming/mjpeg/m/2160p/wHeight
 2160
 EOF
 cat <<EOF > functions/uvc.usb0/streaming/mjpeg/m/2160p/dwMinBitRate
 165888000
 EOF
 cat <<EOF > functions/uvc.usb0/streaming/mjpeg/m/2160p/dwMaxBitRate
 995328000
 EOF
 cat <<EOF > functions/uvc.usb0/streaming/mjpeg/m/2160p/dwMaxVideoFrameBufferSize
 960000
 EOF
 cat <<EOF > functions/uvc.usb0/streaming/mjpeg/m/2160p/dwDefaultFrameInterval
 333333
 EOF
 cat <<EOF > functions/uvc.usb0/streaming/mjpeg/m/2160p/dwFrameInterval
 333333
 EOF
 mkdir functions/uvc.usb0/streaming/header/h
 cd functions/uvc.usb0/streaming/header/h
 ln -s ../../mjpeg/m/
 cd ../../class/fs
 ln -s ../../header/h
 cd ../../class/hs
 ln -s ../../header/h
 cd ../../class/ss
 ln -s ../../header/h
 cd ../../../control
 cd ../../../
 mkdir configs/c.1
 ln -s functions/uvc.usb0/ configs/c.1
 echo $(ls /sys/class/udc | awk 'NR==1') > UDC
 echo "uvc.sh done!"

If using a USB 2.0 port, the frame buffer should be set to 1024 to prevent insufficient bandwidth issues.

echo 1024 > functions/uvc.usb0/streaming_maxpacket
echo 1 > functions/uvc.usb0/streaming_maxburst

Note

The configuration of the UVC gadget configfs should be tailored to specific usage requirements. Here, an executable example is provided on Genio 700-EVK.

Next, adb push this file to the Genio 700-EVK and execute it.

# from PC
adb push uvc.sh /tmp

# from EVK
cd /tmp
chmod 777 uvc.sh
./uvc.sh

After executing uvc.sh and connecting a USB camera, the open-source example program from https://github.com/wlhe/uvc-gadget will be utilized to display the USB camera’s feed to the UVC gadget.

The generation of the gadget node can be confirmed by using ls /sys/class/video4linux/ -l. In the following example, the generated UVC gadget node is /dev/video7, while the USB camera node is /dev/video5.

root@genio-700-evk:/mnt# ls /sys/class/video4linux/ -l
total 0
lrwxrwxrwx 1 root root 0 Apr 28  2022 video0 -> ../../devices/platform/soc/1a030000.jpgenc/video4linux/video0
lrwxrwxrwx 1 root root 0 Apr 28  2022 video1 -> ../../devices/platform/soc/1a040000.jpgdec/video4linux/video1
lrwxrwxrwx 1 root root 0 Dec 13 09:03 video2 -> ../../devices/platform/soc/14001000.mdp3-rdma0/video4linux/video2
lrwxrwxrwx 1 root root 0 Dec 13 09:03 video3 -> ../../devices/platform/soc/18000000.vdec/video4linux/video3
lrwxrwxrwx 1 root root 0 Dec 13 09:03 video4 -> ../../devices/platform/soc/1a020000.venc/video4linux/video4
lrwxrwxrwx 1 root root 0 Dec 13 09:03 video5 -> ../../devices/platform/soc/11201000.usb1/11200000.xhci1/usb3/3-1/3-1.2/3-1.2:1.2/video4linux/video5
lrwxrwxrwx 1 root root 0 Dec 13 09:03 video6 -> ../../devices/platform/soc/11201000.usb1/11200000.xhci1/usb3/3-1/3-1.2/3-1.2:1.2/video4linux/video6
lrwxrwxrwx 1 root root 0 Dec 13 09:03 video7 -> ../../devices/platform/soc/112b1000.usb/gadget/video4linux/video7
root@genio-700-evk:/mnt#

Note

After inserting the USB camera, video5 and video6 will be generated under the USB host xHCI; use the lower number to avoid errors.

After confirming the presence of the UVC gadget node and USB camera node, the UVC-gadget test program can be used to interconnect them. Referring to the uvc-gadget -h command below provides insight into the option functions offered by this tool.

root@genio-700-evk:/mnt# uvc-gadget -h
Usage: uvc-gadget [options]
Available options are
 -b             Use bulk mode
 -d             Do not use any real V4L2 capture device
 -f <format>    Select frame format
        0 = V4L2_PIX_FMT_YUYV
        1 = V4L2_PIX_FMT_MJPEG
 -h             Print this help screen and exit
 -i image       MJPEG image
 -m             Streaming mult for ISOC (b/w 0 and 2)
 -n             Number of Video buffers (b/w 2 and 32)
 -o <IO method> Select UVC IO method:
        0 = MMAP
        1 = USER_PTR
 -r <resolution> Select frame resolution:
        0 = 360p, VGA (640x360)
        1 = 720p, WXGA (1280x720)
 -s <speed>     Select USB bus speed (b/w 0 and 2)
        0 = Full Speed (FS)
        1 = High Speed (HS)
        2 = Super Speed (SS)
 -t             Streaming burst (b/w 0 and 15)
 -u device      UVC Video Output device
 -v device      V4L2 Video Capture device
root@genio-700-evk:/mnt#

Here, it is necessary to specify -u and -v, corresponding to the nodes of the UVC gadget and the USB camera, and to set the resolution to 720p as well as the MJPEG decoding format.

uvc-gadget -u /dev/video7 -v /dev/video5 -r 1 -f 1 -s 1

It’s important to know which UDC port the UVC gadget is bound to, which can be checked using cat /sys/kernel/config/usb_gadget/g1/UDC.

root@genio-700-evk:/mnt# cat /sys/kernel/config/usb_gadget/g1/UDC
112b1000.usb

Here, 112b1000.usb refers to the Genio 700-EVK USB port0 micro-b. By connecting it to the PC using an A-to-B cable, the UVC camera can be viewed on the PC using the built-in Windows Camera Preview app.

../../../../_images/sw_yocto_app-dev_peripheral_peripheral-common_uvcgadget_example.png