Mostrando postagens com marcador linux sunxi. Mostrar todas as postagens
Mostrando postagens com marcador linux sunxi. Mostrar todas as postagens

domingo, 6 de abril de 2014

Configuring the WIFI chipset rtl8188cu

Building Linux sunxi with WIFI Realtek support



The first step to configure the wifi module is to load a driver support this chipset. There is a legacy driver called 8188cu.ko and other newer called rtl8192cu.ko. This driver support both chiptsets rtl8192cu and rtl8188cu, but it is necessary a firmware to work.

Before doing these steps, first test if there is already WIFI Realtek support in Linux board:
modprobe rtl8192cu (linux board)

If the message is: modprobe: FATAL: Module rtl8192cu not found, do next steps. If no, go to section Configuring the interface.


Warning: this step is to be done on PC! You can enable the driver in kernel config:
Device Drivers --> [*] Network device support -->[*] Wireless LAN --> <M> Realtek RTL8192CU/RTL8188CU USB Wireless Network Adapter


Now compile the kernel with Realtek support enabled. You could read: Compiling linux for sunxi.


You can also download the source code RTL8192CU and build it manually. For the Wifi works it is necessary to have the firmware rtl8192cufw.bin. In the Debian repo there is the package firmware-realtek that contains the firmware. Download here debian-repo and tranfer to uSD or media used and install it:
dpkg -i firmware-realtek
In my case (Debian 6), even installing the package firmware-realtek, rtl8192cufw.bin was not present. Verify the /lib/firmware/rtlwifi and look for it. If this is your case, download here rtl8192cufw.bin and tranfer to uSD or media used. Then copy the firmware to /lib/firmware/rtlwifi of target. This debian-rootfs already have all the necessary packages. Choose your preferred Linux distribution!


In Linux with WIFI Realtek support enabled and the firmware in right place just type:
modprobe rtl8192cu (linux board)
You should see something like:
rtl8192cu: MAC address: 48:02:2a:ea:8c:3e
rtl8192cu: Board Type 0
rtl8192cu: rx_max_size 15360, rx_urb_num 8, in_ep 1
ieee80211 phy0: Selected rate control algorithm 'rtl_rc'
usbcore: registered new interface driver rtl8192cu

Configuring the interface (linux board)



All commands and actions performed in that section is to be done on board. That is, in linux that is running on the board!
In order to configure the wlanN(N can be any interger) interface download from your PC and transfer to uSD the packages wireless-tools and wpasupplicant and install them, if your system does not have them! These packages are to Debian wheezy armhf. Download the appropriate packages for your system!
Thereafter check if your wifi interface is active:
ifconfig
If you see only the lo interface,  we find out the number of interface wlanN(N can be any integer). Type:
iwconfig
lo        no wireless extensions.

tunl0     no wireless extensions.

wlan0     IEEE 802.11bgn  ESSID:off/any  
          Mode:Managed  Access Point: Not-Associated   Tx-Power=0 dBm   
          Retry  long limit:7   RTS thr=2347 B   Fragment thr:off
          Encryption key:off
          Power Management:on
Ok, my N is 0. Edit the file /etc/network/interfaces and change as your network(dhcp or static):
auto wlan0 
iface wlan0 inet dhcp
    wpa-driver wext
    wpa-ssid YOUR_WIFI_SSID (change as your network)
    wpa-key-mgmt WPA-PSK
    wpa-psk YOUR_WIFI_PASSWORD (change as your network)
Now we can type:
ifup wlan0 (replace with the number you found)
From that point everything already works in the current kernel!


Verifying the Wifi functionality (linux board)

Type ifconfig and a similar result should appear:
ifconfig
lo        Link encap:Loopback Local  
          inet end.: 127.0.0.1  Masc:255.0.0.0
          endereço inet6: ::1/128 Escopo:Máquina
          UP LOOPBACKRUNNING  MTU:16436  Métrica:1
          RX packets:16 errors:0 dropped:0 overruns:0 frame:0
          TX packets:16 errors:0 dropped:0 overruns:0 carrier:0
          colisões:0 txqueuelen:0 
          RX bytes:1000 (1000.0 B)  TX bytes:1000 (1000.0 B)

wlan0     Link encap:Ethernet  Endereço de HW 00:0d:f0:a2:63:74  
          inet end.: 192.168.1.3  Bcast:192.168.1.255  Masc:255.255.255.0
          endereço inet6: fe80::20d:f0ff:fea2:6374/64 Escopo:Link
          UP BROADCASTRUNNING MULTICAST  MTU:1500  Métrica:1
          RX packets:604836 errors:0 dropped:0 overruns:0 frame:0
          TX packets:343723 errors:0 dropped:0 overruns:0 carrier:0
          colisões:0 txqueuelen:1000 
          RX bytes:898817015 (857.1 MiB)  TX bytes:31367981 (29.9 MiB)
Make sure you are connected on the wireless network:
iwconfig
lo        no wireless extensions.

wlan0     IEEE 802.11bgn  ESSID:"GVT-9EE1"  
          Mode:Managed  Frequency:2.447 GHz  Access Point: 84:C9:B2:C9:9E:E2   
          Bit Rate=72.2 Mb/s   Tx-Power=20 dBm   
          Retry  long limit:7   RTS thr=2347 B   Fragment thr:off
          Encryption key:off
          Power Management:off
          Link Quality=51/70  Signal level=-59 dBm  
          Rx invalid nwid:0  Rx invalid crypt:0  Rx invalid frag:0
          Tx excessive retries:0  Invalid misc:1   Missed beacon:0
One way to improve the connectivity is downloading the network-manager. Test your internet connection and type:
apt-get update
apt-get install 'any package'
If these two commands to work, the configuration is ok!

sexta-feira, 4 de abril de 2014

Using GPIOs from userspace

GPIO usage for linux sunxi


What I'll state here applies to many boards on the market today. However, it is targeted specifically for boards with Allwinner processors. In this article we will use GPIO Sysfs Interface for Userspace.

In order to you can to use GPIOs signals of your board, you have to determine if there is a GPIO driver for your platform. Besides, you must know if this driver follows the standard GPIO Linux framework. Check under the folder /drivers/gpio/xxxx.c whether there is a GPIO driver. If so, this driver uses the standard GPIO framework. If not, you have to look in other folders.

For the case of processors Allwinner, there was a driver called sun4i-gpio, but was removed in favor of the new gpio-sunxi. This new driver uses GPIO Linux framework. This means that you can manipulate the pins I/O through the entries in the /sys directory. In addition, there are many other benefits to using the standard framework.

Since you have found the driver of the GPIO to board processor, how can we use it in user space? You either can access the GPIO signals through command line or by software. The second option is obviously more interesting for those who make projects. I will present both.

Defining the pin number


First you must determine the number corresponding to the pin you want to use. The number that kernel "see". In Linux there is a standard to define this number. For example, if your CPU have a GPIO with 2 ports, each port containing 32 pins, so GPIO1.5 will be 5, GPIO1.32 will be 32 and GPIO2.1 will be 33. Do the calculation according to the number of pins and ports of your GPIO.

You can also get information about number pin by GPIO controller, referenced in the kernel as gpiochipN. GPIO controllers have paths like /sys/class/gpio/gpiochip42 (for the controller implementing GPIOs starting at #42) and have the following read-only attributes under /sys/class/gpio/gpiochipN: base, label and ngpio.

See the manufacturer's documentation to know the correct number for your platform. On Allwinner, the correct number is in script.fex file. For example, if we want to use pin PG9 on A13-OLinuxino:

[gpio_para]
gpio_used = 1
gpio_num = 6
gpio_pin_1 = port:PB03<1><0>
gpio_pin_2 = port:PB04<1><0>
gpio_pin_3 = port:PB05<1><0>
gpio_pin_4 = port:PB06<1><0>
gpio_pin_5 = port:PB07<1><1>
gpio_pin_6 = port:PG09<1><1>

you will pass the number 6 as parameter. This suits to commands and software. Let's see!

GPIO Example usage via command line


Usage via command is only useful for testing. Perhaps with a script you can do something more dynamic. For more complex tasks, the method for software is the preferred.
GPIO=6
cd /sys/class/gpio
echo $GPIO > export
If export was successful, you should see a new folder. Get in.
cd gpio$GPIO
Set the direction of the pin using the strings "in" or "out". They are self-explanatory. Example of reading:
echo "in" > direction
cat value
Example of writing:
echo "out" > direction
echo 1 > value

GPIO Example usage via software


In case the software is interesting to create a library with functions as such export, set direction, read, write, etc. I did a library based on RidgeRun's code.  I just rearranged the code!
https://www.dropbox.com/sh/gaqxgasjuixmnr3/7rHxvPxdoC
We can set a pin at high level and low level as follows:
gpio = atoi(argv[1]); // take pin as parameter
gpio_export(gpio);
gpio_set_dir(gpio, 1);
gpio_set_value(gpio, 1); // Set high level
sleep(1);
gpio_set_value(gpio, 0); // set low level
Please, always keep the license and credit to the original author!

The code shown above will only work if you apply this patch: gpio-sunxi.patch, created by Emilio Lopes.

Video on Youtube: https://www.youtube.com/watch?v=iN3mEWqaf1s

References


http://www.softwarelivre.blog.br/2014/05/usando-gpio-partir-do-userspace.html
https://github.com/torvalds/linux/blob/master/Documentation/gpio/sysfs.txt
https://developer.ridgerun.com/wiki/index.php/How_to_use_GPIO_signals
http://sergioprado.org/user-space-device-drivers-no-linux-parte-2/

quarta-feira, 2 de abril de 2014

Compiling linux for sunxi A13/A10

Toolchain
First you need to download all the tools and the cross compiler in order to compile a linux kernel for sunxi. These softwares are downloaded in your PC! The cross compiler installed in PC, will generate an image that will run in board. The same compiler also generates software and drivers that can run in board.
If you have not installed toolchain, folow these steps.
Add this link into your /etc/apt/sources.list:
Note: Emdebian's armhf-toolchain is only available for Debian/unstable at the moment.
It is important to use armhf because most software currently is compiled for armhf. If you use armel, you must get software compatible with armel!
Run these commands in order to get cross compiler:
apt-get install emdebian-archive-keyring
apt-get update
apt-get install gcc-4.7-arm-linux-gnueabihf
Choose the same version of the compiler's distro, thus you avoid incompatibilities and broken packages. Type 'gcc -v' and check your version.
Create a symbolic link to arm-linux-gnueabihf-gcc:
ln -s arm-linux-gnueabihf-gcc-4.7 arm-linux-gnueabihf-gcc
You need extra tools to compile linux kernel:
apt-get install build-essential git debootstrap u-boot-tools libncurses5-dev

Clone the repository
git clone -b sunxi-3.4 https://github.com/linux-sunxi/linux-sunxi.git
The kernel linux-sunxi is provided by sunxi community, but it is not included in the official linux kernel. The official linux kernel with support sunxi can be found in these links:
https://github.com/mripard/linux
https://github.com/jwrdegoede/linux-sunxi/tree/sunxi-devel

But still is not usable, for now use linux-sunxi.

Configure
For A10 use:
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- sun4i_defconfig
For A13 use:
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- sun5i_defconfig

Configure additional parameters, such as drivers or debug options:
make ARCH=arm menuconfig
To A13-OLinuXino-Wifi enable Realtek's driver:
Device drivers --> Network device support --> Wireless LAN --> <M> Realtek RTL8192CU/RTL8188CU USB Wireless Network Adapter

Build
make -j4 ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- uImage modules
Choose -jN according to the number of cores of your CPU. Four cores -j4, two cores -j2...

This command will copy all enabled modules in the specified folder by INSTALL_MOD_PATH:
make ARCH=arm INSTALL_MOD_PATH=output modules_install
Install
As a final result we will have kernel and modules:
arch/arm/boot/uImage
output/lib/modules/3.4.XXX/
The uImage file needs to be started by u-boot, and the modules directory needs to be copied to the /lib/modules on the target rootfs. So copy kernel image uImage to SD partition 1 and modules to partition 2, into rootfs.

Videos on Youtube:
Part 1: https://www.youtube.com/watch?v=3xNdH9SquL4
Part 2: https://www.youtube.com/watch?v=ilDhOzmkORY
Part 3: https://www.youtube.com/watch?v=LWJ11nJ_0-k