Pilot GPIO on LininoIO

GPIO on lininoIO


To follow this guide you need to install and to start LininoIO.

By using MCU IO protocol, LininoIO provides a way to show AVR atmega32u4 GPIOs as they were gpios of the main processor. 
Such “virtualized” GPIOs start from number 100 and follow the mapping in the attached yun-gpios.cfg file. 
For more information look the "GPIO mapping on lininoIO" table below .

GPIO mapping on lininoIO 
In the below table are shown the "virtualized" GPIOs of the Arduino Yun/Yun Mini:

Linino GPIO Yun Pin
104 D8
105 D9
106 D10
107 D11
114 D5
115 D13
116 D3
117 D2
120 D4
122 D12
123 D6


In order to use it as a digital output follow the instructions:

 
 
echo 115 > /sys/class/gpio/export
echo out > /sys/class/gpio/D13/direction
echo 1 > /sys/class/gpio/D13/value
echo 0 > /sys/class/gpio/D13/value
 
 


You can use these GPIOs in the standard linux way, including the possibility of driving them from your preferred user space library. For more information on GPIOS, look at Linux documentation:

https://github.com/linino/kernel_3.3.8/blob/master/Documentation/gpio.txt

Note: 
If, for example, inserting this code:

 
 
echo 115 > /sys/class/gpio/export
 
 

You get this error code:

 
 
ash: write error: Invalid argument
 
 

Then check if you have activated the LininoIO.

 

PWM on lininoIO

PWM on lininoIO use the standard linux pwm framework, with some small improvements. This is a quick reference, for more information refer to kernel documentation, e.g. here: 
https://github.com/linino/kernel_3.3.8/blob/master/Documentation/pwm.txt

1) You need to export the pwm you think you will use. This does not enable pwms, so you can export them all, for convenience.

read number of available pwm here:

 
 
cat/sys/class/pwm/pwmchip0/npwm
 
 


Then export all desired pwm's, chosen from 0 to npwm, with this command:

 
 
echo 0 > /sys/class/pwm/pwmchip0/export
echo 1 > /sys/class/pwm/pwmchip0/export
...
 
 


2) You will find some symbolic link on sysfs, where the name corresponds to the labels on the expansion connector on the board:

 
 
/sys/class/pwm/pwmchip0/D3 
/sys/class/pwm/pwmchip0/D9
/sys/class/pwm/pwmchip0/D10
/sys/class/pwm/pwmchip0/D11
...
 
 


3) For each of them, you have the following attributes (NOTE: all numeric values are nanoseconds):

  • enable: enable pwm output (can be 0 or 1)
  • resolution //(in ns)//: min step for period and duty_cycle changes
  • max//(in ns)//: max value for period and duty_cycle
  • polarity: can be normal or inversed
  • period//(in ns)//: period of output wave
  • duty_cycle //(in ns)//: time output wave is high (if polarity is "normal") or low (in case of "inversed")


Note: 
D9, D10 and D11 share the same timer so, if you modify the period of one of these digital pins this will affect the remaining two as well. 
You can not change period on D3, but duty_cycle only

BOARD PWM
PWM n.OUTPUT
0 D3
1 D9
2 D10
3 D11
4 D5
5 D6

Servo

To control a servo motor you need to set the appropriate values of duty_cycle and period of the pwm. To ensure proper operation of the servo motor the minimum value for the period is 20ms. The values ​​to determine the angle of rotation, depend on the model of the servo motor in use. Typically the values ​​of duty_cycle vary from 0.56ms (0°) to 2.4ms (180°). To initialize the usage of pwm with a servo motor, set the minimum values for period and duty_cycle and then enable the pwm

 
 
echo 1 > /sys/class/pwm/pwmchip0/export
echo 20000000 > /sys/class/pwm/pwmchip0/D9/period
echo 560000 > /sys/class/pwm/pwmchip0/D9/duty_cycle
echo 1 > /sys/class/pwm/pwmchip0/D9/enable
 
 

To rotate of 180°:

 
 
echo 2400000 > /sys/class/pwm/pwmchip0/D9/duty_cycle
 
 

Buzzer

To play tones with a piezo buzzer you need to set the duty_cycle at 50% of the period of the pwm. The period is the inverse of the frequency of the tone that you want to play. For example if you want reproduce a 40Hz tone the period wiil be //1 / 40Hz = 25ms//

 
 
echo 25000000  > /sys/class/pwm/pwmchip0/D9/period
echo 12500000 > / sys/class/pwm/pwmchip0/D9/duty_cycle
echo 1 > /sys/class/pwm/pwmchip0/D9/enable
 
 

Note: you can not play tones lower to 31Hz

A/D Converter on lininoIO

To read the voltage applied on the pin A0,A1,A2,A3,A4,A5 present on the pinheader of Arduino Yun or Linino ONE from Linino OS follow the next steps.

- Once the device is discovered, you should find the symbolic link:

 
 
/sys/bus/iio/devices/iio:device0
 
 

- to enable adc usage, echo 1 to 'enable' file:

 
 
echo 1 > /sys/bus/iio/devices/iio:device0/enable
 
 

- if everything succeeds, you should see those directories in sysfs:

 
 
root@linino:~# ls /sys/bus/iio/devices/iio:device0
dev                 in_voltage_A2_raw   in_voltage_A5_raw
enable               in_voltage_A2_scale in_voltage_A5_scale
in_voltage_A0_raw   in_voltage_A3_raw   name
in_voltage_A0_scale in_voltage_A3_scale subsystem
in_voltage_A1_raw   in_voltage_A4_raw   uevent
in_voltage_A1_scale in_voltage_A4_scale
 
 

- to read the voltage value of the input A0, you can execute the follwoing comamnd:

 
 
root@linino:~# cat /sys/bus/iio/devices/iio:device0/in_voltage_A0_raw
195
 
 

- to get resolution (in microVolts):

 
 
root@linino:~# cat /sys/bus/iio/devices/iio:device0/in_voltage_A0_scale
4882
 
 

you will get 4882uV, that is 5V / 1024

- all analog input values are referenced to AVcc=5V.

I2C on lininoIO

With lininoIO you can also read sensors data from i2c devices, through sysfs. Before starting it would be appropriate to install i2c-tools, which helps to identify the device address:

 
 
opkg update
opkg install i2c-tools
 
 


Below a list of devices ready to use with lininoIO:

SHT20 Humidity - Temperature

get the address of the i2c device through i2cdetect

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


Next register the sht21 driver

 
 
echo sht21 0x40 > /sys/bus/i2c/devices/i2c-0/new_device
 
 


Then list all the fields of the device

 
 
ls /sys/bus/i2c/devices/0-0040/
driver           humidity1_input hwmon           modalias         name             subsystem       temp1_input     uevent
 
 


Finally you can read the sensor data:

 
 
cat /sys/bus/i2c/devices/0-0040/humidity1_input
 
 

MAG3110 Magnetometer

get the address of the i2c device through i2cdetect

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


Next register the mag3110 driver

 
 
echo mag3110 0x0e > /sys/bus/i2c/devices/i2c-0/new_device
 
 


Then list all the fields of the device (the number of devices depends on the number of devices already registered)

 
 
ls /sys/bus/iio/devices/iio\:device0/
buffer                       in_magn_x_calibbias           in_magn_z_calibbias           name                         trigger
dev                           in_magn_x_raw                 in_magn_z_raw                 sampling_frequency_available uevent
in_magn_sampling_frequency   in_magn_y_calibbias           in_temp_raw                   scan_elements
in_magn_scale                 in_magn_y_raw                 in_temp_scale                 subsystem
 
 


Finally you can read the sensor data:

 
 
cat /sys/bus/iio/devices/iio\:device0/in_magn_x_raw
 
 

MPL3115A2 Precision Altimeter

get the address of the i2c device through i2cdetect

 
 
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: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: 60 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
 
 


Next register the mpl3115 driver

 
 
echo mpl3115 0x60 > /sys/bus/i2c/devices/i2c-0/new_device
 
 


Then list all the fields of the device (the number of devices depends on the number of devices already registered)

 
 
ls /sys/bus/iio/devices/iio\:device0/
buffer             in_pressure_raw   in_temp_raw       name               subsystem         uevent
dev               in_pressure_scale in_temp_scale     scan_elements     trigger
 
 


Finally you can read the sensor data:

 
 
cat /sys/bus/iio/devices/iio\:device0/in_pressure_raw
 
 

MMA8491 Accelerometer

get the address of the i2c device through i2cdetect

 
 
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: -- -- -- -- -- 55 -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
 
 


Next register the mma8491 driver

 
 
echo mma8491 0x55 > /sys/bus/i2c/devices/i2c-0/new_device
 
 


Then list all the fields of the device (the number of devices depends on the number of devices already registered)

 
 
ls /sys/bus/iio/devices/iio\:device0/
 
 

TODO list of fields


Finally you can read the sensor data:

 
 
cat /sys/bus/iio/devices/iio\:device0/in_accel_x_calibbias