Difference between revisions of "WRTnode"

From LinkSprite Playgound
Jump to: navigation, search
Line 686: Line 686:
 
         };
 
         };
 
   };
 
   };
 +
 +
 +
== SPI ==
 +
 +
=== WRTnode add spi slave ===
 +
 +
一:Modify spi controller driver to achieve the support of the spi cs1
 +
 +
We assume that $(TOPDIR) for OpenWrt of compiler directory.
 +
open spi-rt2880.c
 +
vim $(TOPDIR)/build_dir/target-mipsel_24kec+dsp_uClibc-0.9.33.2/linux-ramips_mt7620n/linux-3.10.49/drivers/spi/spi-rt2880.c
 +
find struct rt2880_spi_ops then set spi_ops[0].num_cs = 2
 +
static struct rt2880_spi_ops spi_ops[] = {
 +
    {
 +
        .init_hw = rt2880_spi_reset,
 +
        .set_cs = rt2880_spi_set_cs,
 +
        .baudrate_set = rt2880_spi_baudrate_set,
 +
        .write_read = rt2880_spi_write_read,
 +
        .num_cs = 1,  // .num_cs = 2
 +
    }, {
 +
        .init_hw = rt5350_spi_reset,
 +
        .set_cs = rt2880_spi_set_cs,
 +
        .baudrate_set = rt2880_spi_baudrate_set,
 +
        .write_read = rt2880_spi_write_read,
 +
        .num_cs = 2,
 +
    }, {
 +
        .init_hw = mt7621_spi_reset,
 +
        .set_cs = mt7621_spi_set_cs,
 +
        .baudrate_set = mt7621_spi_baudrate_set,
 +
        .write_read = mt7621_spi_write_read,
 +
        .num_cs = 1,
 +
    },
 +
};
 +
二:Complete the registration of spi slave device where the dts
 +
cd $(TOPDIR)/
 +
wget --no-check-certificate https://raw.githubusercontent.com/WRTnode/openwrt-patches/master/R39786/wrtnode_spidev_dts.patch
 +
patch  -p1 < wrtnode_spidev_dts.patch
 +
 +
[[File:37.png]]
 +
 +
三:spi configuration
 +
 +
make menuconfig
 +
Kernel modules  --->    SPI Support  --->    <*> kmod-spi-dev
 +
四:Compile
 +
make
 +
五:flash Firmware
 +
The new firmware re-programmed into the WRTnode, perform the following operations on WRTnode:
 +
 +
ls /dev
 +
 +
 +
[[File:38.png]]
 +
 +
Provide [https://github.com/WRTnode/openwrt-packages/tree/master/wrtnode/tank/ a demo for spidev]

Revision as of 06:09, 17 December 2015

Introduction

1.jpg

WRTnode, which is based on Wi-Fi AP-Soc, is an open source development board hardware. There is a mediatek MT7620n chip in it.

It is mini, cheap and has pretty low power consumption and reasonable capability of computing. Besides, It is born to high speed of Wi-Fi network exchange.

What does WRTnode aim for? Maybe different people have different opinions. Our team considers it as a high speed and digital version of "Arduino". Since it could connect and interact with USB devices like camera and sound card, we can collect images and voices at the same time and stream out multimedia stream in real time. After a layer of A / D converter, It could directly interact with physical world like all kinds of sensors and motors.

OpenWrt, which is a distribution of embedded Linux, is the system of WRTnode. When multimedia information and various of input and output control focus on a Linux system which has small volume, low power consumption and enough computing capacity, after porting and optimizing the existing pattern recognition, artificial intelligence and other applications, we may setup a series of interesting "smart" devices. And they are supplemented by powerful network capabilities. we may get a structure of MATRIX and NODE. This is where the node in WRTnode comes from.

We pronounce WRTnode as what-node.

As an OpenWrt supported device, WRTnode applies everything on wiki.openwrt.org. We highly recommended take wiki.OpenWrt.org as the trusted reference.

Features

  • WRTnode is based on Wi-Fi AP-Soc and it is an open source development board hardware.
  • Opensource hardware for OpenWrt
  • mini Linux + Wi-Fi board
  • easy and completed IDE
  • smart machines' heart
  • low power consuming
  • complete I/Os, high performance
  • 300MBit/s Wi-Fi and low price

Specifications

Hardware parameters

2.jpg

  • 45mm*50mm
  • MTK MT7620N 580MHz MIPS CPU (MIPS24KEc)
  • 512Mbit DDR2 ram
  • 128Mbit SPI Flash rom
  • 300Mbit Wi-Fi 2T2R 802.11n 2.4 GHz
  • 23GPIOs
  • JTAG
  • SPI
  • UART Lite
  • USB2.0 host

Software Environment

  • Based on OpenWrt BARRIER BREAKER (Bleeding Edge, r41508)
  • rt2860v2 Wi-Fi driver hacked by lintel
  • Customized uboot hacked by manfeel
  • WRTnode aplci up-link Wi-Fi configuration (aps/vw/nr/ia)
  • Luci Wi-Fi wpa patch for rt2860v2
  • Local dns add i.wrtno.de & wrtnode.lan besides openwrt.lan to WRTnode which the default ip is 192.168.8.1
  • WRTnode additional feature (all source opened github.com/WRTnode):
  • Opencv 2.4.8
  • Native gcc-mipsel on mt7620 and bin-utils
  • Porting linino (Arduino yun) source to WRTnode
  • Shine: fast fixed-point mp3 encoding
  • And some WRTnode demo apps:
  • opencv application demo
  • mechanical control demo
  • RESTful front-end demo and some other thing

WRTnode firmware, SDK and cross-compiler toolchain using OpenWrt trunk version, designed for WRTnode maintenance and support by OpenWrt.org official. Meanwhile, WRTnode can use the market all the programs using the MT7620 router firmware and associated SDK and cross-compiler toolchain.

Pin map

Click the pic to see full resolution version. Perspective A pin map

3.jpg

4.jpg

Starting guide

What you will get?

5.jpg

You get the WRTnode in a very nice transparent plastic box, inside the box you get WRTnode stickers too. The box is useful to store various small items useful for WRTnode (USB memory stick, I2C port expander module, jumpers and so on).

6.jpg 7.jpg


8.jpg

Besides these, you get:

The 'special' USB cable customized for WRTnode

9.jpg

10.jpg

The micro USB standard plug will be connected to the WRTnode board and to power the board itself. The USB standard type A plug will be connected to any USB wall adapter, any PC USB port, any USB 5Vdc power supply (at least 500mA (0,5A) current). The USB standard type A receptacle can connect any USB device to the WRTnode board, for example can connect a USB memory stick:

11.jpg

Or a USB webcam like this:

12.jpg

Or you can connect many more USB devices at once through a USB hub like this:

13.jpg

Caution: use USB hub with external 5Vdc power supply if the USB hub connects more than one USB device.

The WRTnode

The golden board is WRTnode and can be seen on the above images.

Power-up the WRTnode board

Connect the micro USB plug to the WRTnode board (if you don't need to connect USB devices to WRTnode, a normal USB data cable with micro USB can work). Connect the USB A plug of the special USB cable to a 5Vdc USB power supply (at least 500mA current - any USB port on a PC can provide this current).

14.jpg

WRTnode connected to a USB port using a standard micro USB data cable

After ~5 seconds, the blue LED on WRTnode will be on, that means that OpenWrt operating system is initializing on the WRTnode board. 20 seconds later, the WRTnode WiFi SSID should be visible on the WiFi networks on the PC: 'WRTnodeXXXX' (XXXX from the SSID refers to the last 4 bytes of WRTnode’s MAC address). Connect your PC or smartphone to WRTnode WiFi SSID using WPA2-PSK encryption and password '12345678'.

Let's play with WRTnode

Login to WRTnode using telnet and ssh

Use any PC with Windows, Linux or MacOS operating system, install PuTTY on Windows PC, use any Linux / MacOS terminal software on Linux / MacOS.

First ping the WRTnode to see if connection is available. The default IP of WRTnode is set to 192.168.8.1, but WRTnode is able to resolve as well i.wrtno.de, wrtnode.lan and openwrt.lan to 192.168.8.1:

  ping i.wrtno.de

or

  ping wrtnode.lan

or

  ping openwrt.lan

Connect to WRTnode using telnet or SSH If you connect first time to WRTnode or you didn't set password for root user of OpenWrt, use telnet to connect:

  telnet i.wrtno.de

If you a password for root user was set, use SSH to connect:

  ssh root@i.wrtno.de

Input your root password and a similar screen should appear:

15.png

No internet no happiness

Because WRTnode may need new applications or needs to update installed applications, an Internet connection is necessary. The WRTnode will be connected to Internet through a WiFi router - you home router connected to Internet.

  root@OpenWrt:~# aps

'aps' is a WRTnode customized command which scans the WiFi SSIDs available in your area. You should see as well your home router SSID - let's name it 'OUR_WIFI'. The result should look like this:

  WRTnode AP scaner.
  Begin scaning APs, pls wait...
  Finished.
  APs available are...
  ra0       get_site_survey:
  Ch  SSID                             BSSID               Security               Signal(%)W-Mode  ExtCH  NT WPS DPID
  1   Tenda                            xx:xx:xx:xx:xx:xx   NONE                   70       11b/g/n ABOVE  In YES
  1   OUR_WIFI                         xx:xx:xx:xx:xx:xx   WPA2PSK/AES            96       11b/g/n NONE   In YES
  6   TP-LINK                          xx:xx:xx:xx:xx:xx   NONE                   55       11b/g   NONE   In  NO
  6   BY01                             xx:xx:xx:xx:xx:xx   NONE                   100      11b/g/n NONE   In YES
  6   Tenda_xxxxxx                     xx:xx:xx:xx:xx:xx   WPAPSK/AES             50       11b/g/n ABOVE  In  NO
  11  CU_wpFS                          xx:xx:xx:xx:xx:xx   WPAPSK/TKIPAES         70       11b/g/n NONE   In YES

Then we have to change the settings of aplci on WRTnode, aplci is configuring the up-link part of the router inside of WRTnode (that's it: WRTnode has an internal router)

  root@OpenWrt:~# vw

'vw' is a WRTnode customized command which changes /etc/config/wireless. It is used the file editor named 'vi', a standard editor for Linux, but not very comfortable to use it, therefore here are the tips:

  • delete a character: use arrow keys to move cursor under the character you want to delete, press 'x' to delete the character
  • insert a character: press 'i' (to enter on insert mode), use arrow keys to move cursor under the character, press whatever character(s) you want to insert, the character(s) will be inserted after the character where the cursor is. Finish by pressing ESC key (to quit insert mode)
  • save changes: press ':', then press 'w'
  • save changes and quit vi editor: press ':', then press 'x'
  • quit vi editor without saving changes: press ':', then press 'q'
 config wifi-device 'ra0'
       option type 'ralink'
       option mode '9'
       option channel '1'        #This is change number 1
       option txpower '100'
       option ht '20+40'
       option country 'US'
       option disabled '0'
 config wifi-iface
       option device 'ra0'
       option network 'lan'
       option mode 'ap'
       option encryption 'psk2'
       option key '12345678'
       option ApCliEnable '1'
       option ApCliSsid 'aAP'              #This is change number 2
       option ApCliAuthMode 'WPA2PSK'      #This is change number 3
       option ApCliEncrypType 'AES'        #This is change number 4
       option ApCliPassWord '87654321'     #This is change number 5
       option ssid 'WRTnodeXXXX'
  • Change number 1: set the WiFi channel to match the one from our router 'OUR_WIFI' (specified on column 'Ch' from aps output)
  • Change number 2: set the WiFi SSID to match the one from our router 'OUR_WIFI'
  • Change number 3: set the ApCliAuthMode to match the one from our router 'OUR_WIFI'

Here are the accepted parameters for ApCliAuthMode / ApCliEncrypType, from vw according to aps' security:

 aps:Security           vw:ApCliAuthMode/ApCliEncrypType
 =========================================================
 WPA1PSKWPA2PSK/TKIPAES	WPA2PSK/AES
 WPA2PSK/AES		WPA2PSK/AES
 WPA2PSK/TKIP		WPA2PSK/TKIP
 WPAPSK/TKIPAES		WPAPSK/TKIP
 WPAPSK/AES		WPAPSK/AES
 WPAPSK/TKIP		WPAPSK/TKIP
 WEP			WEP/WEP
 NONE			NONE/NONE
  • Change number 4: set the ApCliEncrypType to match the one from our router 'OUR_WIFI'
  • Change number 5: set the Password to match the one from our router 'OUR_WIFI' (this must be known by the router owner)

Additionally, if 'OUR_ROUTER' router uses ASCII WEP password, you will need to perform the following commands into HEX code

 root@OpenWrt:~# echo -n 'abcde' | hexdump -e '13/1 "%02x" "\n"'   #To convert abcde to 6162636465

Also uci (specific to OpenWRT operating system) can be used to change the above settings, but this is beyond the scope of this Wiki section.

  • Network reset command:
 root@OpenWrt:~# nr

'nr' is a WRTnode customized command which will reset the network side. After 5 seconds it is possible to connect again to WRTnode's SSID. Let's verify if WRTnode got an IP from router 'OUR_WIFI':

 root@OpenWrt:~# ia
 apcli0    Link encap:Ethernet  HWaddr xx:xx:xx:xx:xx:xx
           inet addr:192.168.1.103  Bcast:192.168.1.255  Mask:255.255.255.0
           inet6 addr: xxxx::xxxx:xxxx:xxxx:xxxx/xx Scope:Link
           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
           RX packets:0 errors:0 dropped:0 overruns:0 frame:0
           TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
           collisions:0 txqueuelen:1000
           RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

Sometimes, router 'OUR_WIFI' may lease the IP after tens of seconds. Let's verify if WRTnode is connected to Internet.

 root@OpenWrt:~# ping baidu.com
 PING baidu.com (220.181.111.85): 56 data bytes
 64 bytes from 220.181.111.85: seq=0 ttl=54 time=6.071 ms

In this moment the WRTnode board is connected to the Internet through our home router, we can connect our PC to Internet through the WRTnode and start configure our WRTnode board.

WRTnode software repository

 root@OpenWrt:~# opkg update
 Downloading http://d.wrtnode.com/packages/Packages.gz.
 Updated list of available packages in /var/opkg-lists/barrier_breaker.
 root@OpenWrt:~# opkg install <here put the name of the package>

Restore Factory Settings

 root@OpenWrt:~# firstboot
 This will erase all settings and remove any installed packages. Are you sure? [N/y]
 y
 /dev/mtdblock5 is mounted as /overlay, only erasing files
 root@OpenWrt:~# reboot

WRTnode special network cable

WRTnode uses a special network cable - one end has a RJ45 network connector, the second end has a 2x2 female header connector / Dupont connector (see Figure 1):

16.jpg

Figure 1 Cable pinout First of all, this is a T568B cable. T568B cable is the cable used to connect a computer to a router at home. So get a T568B cable, cut one RJ45 end connector and prepare all the 8 wires from the cable following the pinout from Table 1:

17.png

Use only the green, green and white, orange, orange and white wires from the T568B cable. The rest of 4 wires can be cut, they are not useful for our application. The 4 wires selected above have to be solder to a 2x2 female header connector (Dupont connector) according to pinout from Table 2:

18.png

After the cable is completed, it can be connected to WRTnode board like in Figure 2:

19.png

Figure 2 If a factory cable is used, on the female header connector it is visible a small black arrow which corresponds to Pin 1. This cable can be connected to any of the 4 LAN ports from the WRTnode board taking into consideration that the arrow (Pin 1 on the connector's cable) corresponds to WRTnode pins number 2, 4, 6, 8. Pin 2 from the cable corresponds to WRTnode pins 1, 3, 5, 7. Following figure shows the cable Pin 1 connected to pin 4 on WRTnode (Figure 3). As shown in Figure 2, pins for LAN and WAN ports are defined on the right side of the WRTnode board, Figure 3 below shows the network cable connected to WRTnode.

20.jpg

Figure 3

Let's move on!

UART

Release UART

Since mt7620n only one UART lite interface, native OpenWrt used as the core print and tty console .

And in many cases, require t0 use the serial communication with an external chip on the line, then you need to release the port from OpenWrt.

In fact, this task is only two steps :( we assume that $(TOPDIR) was the compiler directory of OpenWrt )

  • Close serial console

The one comment out $(TOPDIR)/target/linux/ramips/base-files/etc/inittab file below:

 ::askconsole:/bin/ash --login
 $vi $(TOPDIR)/target/linux/ramips/base-files/etc/inittab
 ::sysinit:/etc/init.d/rcS S boot
 ::shutdown:/etc/init.d/rcS K shutdown
 #::askconsole:/bin/ash --login
  • Close kernel Print

In the $(TOPDIR)/package/base-files/files/etc/config/system file, add the following two:

  option 'conloglevel' '1'
  option 'kconloglevel' '1'
  $vi $(TOPDIR)/package/base-files/files/etc/config/system
  config system
        option 'conloglevel' '1'
        option 'kconloglevel' '1'
        option hostname OpenWrt
        option timezone UTC
  config timeserver ntp
        list server     0.openwrt.pool.ntp.org
        list server     1.openwrt.pool.ntp.org
        list server     2.openwrt.pool.ntp.org
        list server     3.openwrt.pool.ntp.org
        option enable_server 0

Through the above two steps, you can turn off all systems of OpenWrt serial interaction, except for the first ten seconds of uboot printing information and Kernel early printing information (residual print information about this part, the proposed development of the next-bit machine when the system has just started made redundant).

The above methods are OpenWrt source inside modify, compile the new firmware, the new firmware release can be burned into WRTnode serial.

Because of OpenWrt writable file system, which also can modify WRTnode existing firmware in the /etc/inittab and /etc/config/system files directly, you can release the port after the restart.


LAN/WAN

LAN/WAN config

In many cases, we need to customize a particular network port for LAN or WAN side on the WRTnode board.

There are many ways to accomplish this task: uboot compile-time setting in OpenWrt compile-time setting in OpenWrt of set in the configuration file.

Here we explain one of the most simple way: in: | set Wikipedia OpenWrt OpenWrt of the configuration file, and then explain how to compile OpenWrt Firmware stage default this setting.

In the OpenWrt after the start, we can find the /etc/config/nework file, edit the file using vi, you can find the following:

 config switch_vlan
       option device 'switch0'
       option vlan '1'
       option ports '1 2 3 4 6t'
 config switch_vlan
       option device 'switch0'
       option vlan '2'
       option ports '0 6t'

The above setup defines 2 vlan, 6t defines the mt7620 built-in switch, 1,2,3,4 defines 4 LAN, 0 defines an Ethernet WAN.

So, we can change the vlan 0, 1,2,3,4 according to our needs in order to define a LAN or WAN port (or even all network ports can be set to LAN or WAN, P-) However, the consequences ~ ~).

When done, simply restart the network interface

  /etc/init.d/network restart 

We can define the network ports and setup on OpenWRT start, we want that firmware compilation phase to complete this by default.

We assume that $(TOPDIR) for OpenWrt of compiler directory.

  vi $(TOPDIR)/target/linux/ramips/base-files/etc/uci-defaults/02_network 

Find the following parameters:

       wrtnode)
               ucidef_set_interfaces_lan_wan "eth0.1" "apcli0"
               ucidef_add_switch "switch0" "1" "1"
               ucidef_add_switch_vlan "switch0" "1" "1 2 3 4 6t"
               ucidef_add_switch_vlan "switch0" "2" "0 6t"
               ;;

Now you should know how to change the network ports according to your needs :)

Keep on rock!

GPIO

Device Tree Source

一:device tree source birth

In the past linux kernel, such as: ARM Linux in, arch/arm/plat-xxx and arch/arm/mach-xxx filled with a lot of garbage code, a considerable majority of the code just in describing the board level detail.

These board-level details for the kernel is concerned, but is garbage, such as platform device board, resource, i2c_board_info, spi_board_info and various hardware platform_data.

Device Tree is a data structure describing the hardware, which originated in OpenFirmware (OF).

  In Linux 2.6 in, ARM architecture plate too much hardware details are hard-coded in the arch /arm/plat-xxx and arch/arm/mach-xxx, after using the Device Tree, many details of the hardware can be passed directly through it to Linux,       without the need for a lot of redundancy in the coding in kernel.
  Device Tree named by a series of nodes (node) and attribute (property) composed of the node itself may contain sub nodes. The so-called property, in fact, occur in pairs name and value. In the Device Tree, the information can be described, including (former mostly hard code this information to the kernel in):

The number and type of CPU

Memory base address and size

Bus and bridge

Peripheral connections

Interrupt controller and interrupt usage

GPIO controller and GPIO usage

Clock controller and Clock Usage

It is basically draw a circuit board CPU, bus, equipment consisting of trees, Bootloader will be passed to the kernel tree, then the kernel can identify this tree, And according to it launched a Linux kernel platform_device, i2c_client, spi_device and other equipment, and these devices use memory, IRQ and other resources, has also been passed to the kernel, the kernel will bind these resources to expand the appropriate device.

二: Device Tree composition and structure

Device Tree involve the whole broad face, that adds new text format used to describe the device hardware information, but also increases the tool to compile the text, but also needs the support Bootloader Device compiled Tree will be passed to the Linux kernel.

DTS (device tree source)

.dts file is an ASCII text description Device Tree format, the text format is very user-friendly, suitable for human reading habits.

Basically, mips Linux, a .dts file corresponds to a mips the machine, usually placed in /target/linux/ramips/dts directory.

Due to an SoC may correspond to multiple machine (an SoC can correspond to multiple products and circuit boards), bound to these .dts file should contain many common parts, Linux kernel in order to simplify,

The common part of the common parts of SoC or more machine usually refined for .dtsi, similar to the C language header files. Other machine corresponding .dts to include this .dtsi. For example, for MT762n terms, mt7620n.dtsi was called WRTNODE.dts, WRTNODE.dts the following line:

/include/ "mt7620n.dtsi"

Of course, and is similar to the C language header files, .dtsi can also include other .dtsi, for example, almost all ARM SoC's .dtsi all references skeleton.dtsi. .dts (or which include a .dtsi) basic elements and attributes of the nodes is described hereinbefore

For example:m25p80的spi flash

 m25p80@0 { 
              #address-cells = <1>; 
              #size-cells = <1>; 
              #compatible = "mx25l6405d"; 
              compatible = "w25q128"; 
              reg = <0 0>; 
              linux,modalias = "m25p80", "w25q128"; 
              spi-max-frequency = <10000000>; 
              partition@0 { 
                  label = "u-boot"; 
                  reg = <0x0 0x30000>; 
                  read-only; 
              }; 
              partition@30000 { 
                  label = "u-boot-env"; 
                  reg = <0x30000 0x10000>; 
                  read-only; 
              }; 
              factory: partition@40000 { 
                  label = "factory"; 
                  reg = <0x40000 0x10000>; 
                  read-only; 
              }; 
              partition@50000 { 
                  label = "firmware"; 
                  reg = <0x50000 0xfb0000>; 
              }; 
          };


m25p80 @ 0 is the junction point, partition @ 0, factory: partition @ 40000 is his child nodes.

Organizational form compatible attribute is <manufacturer>, <model>, in fact, remove the manufacturer alias prefix compatible property before the comma. On this point, you can see drivers /spi/spi.c source code, function spi_match_device () exposes more detail, if an alias appears in the device spi_driver of id_table inside, or alias name field spi_driver same, SPI devices and drive can be matched.

Specific dts presentation can refer [1]

Release the reuse of gpio

-: According mt7620 datasheet in GPIO pin share schemes and WRTnode gpio pins feature can be used to customize the gpio pin marked as shown below:

File:21.PNG

File:22.PNG

Note: The red box is a combination of WRTnode interface parameters obtained can be released gpio multiplexed pins.

二: mt7620n.dtsi in the registration of gpio

We assume that $(TOPDIR) for OpenWrt of compiler directory.

In the $(TOPDIR)/target/linux/ramips/dts/mt7620n.dtsi registered to gpio

Note: The combination of mt7620 datasheet in GPIO pin share schemes and in mt7620n.dtsi where we see the GPIO#0 to GPIO#72 (middle just do GPO or GPI's) are divided into four groups GPIO0-GPIO3;

GPIO0 is starting from the GPIO#0 to GPIO#23, a total of 24;

GPIO1 is starting from the GPIO#24 to GPIO#39, a total of 16;

GPIO2 is starting from the GPIO#40 to GPIO#71, a total of 32;

GPIO3 corresponds corresponding GPIO#72, only one.

32.png

23.png


24.png


25.png


26.png


三: Sign in WRTNODE.dts inside of led, keys, etc.

In the $(TOPDIR)/target/linux/ramips/dts/WRTNODE.dts where you can register through gpio interface control devices, such as led, keys, etc.

If you want to use the GPIO interface, you need to enable the GPIO and release the GPIO pin standby function:

Enable GPIO:

27.png

Release gpio pin alternate function:


28.png


Definition pin multiplexing in $(TOPDIR)/build_dir/target-mipsel_24kec+dsp_uClibc-0.9.33.2/linux-ramips_mt7620n/linux-3.10.28/arch/mips/ralink/mt7620.c

Note: The path is not necessarily for this, according to your own kernel version, linux-3.10.xx

For example, i2c_grp, from gpio#1 start, including gpio#1 and gpio#2, a total of two gpio;

spi refclk, from gpio#37 start, including gpio#37, gpio#38 and gpio#39, a total of three gpio.


29.png


Similarly, other reuse the same pin definitions.

30.png


Note: dts registered in the device is very simple, such as registration gpio-leds is, compatible with Led to drive leds-gpio.c in compatible correspondence, label is the name of the device, the device registration is successful, the file system will be under WRTnode there are /sys/class/leds/wrtnode:blue:wan, we can look commands to control the light off

 $echo 1 > /sys/class/leds/wrtnode\:blue\:wan/brightness
 $echo 0 > /sys/class/leds/wrtnode\:blue\:wan/brightness

gpios = <& gpio2 0 1>, which is designated corresponding gpio pins (GPIO#40)

31.png


The user space gpio calls

User space access gpio, namely through sysfs access gpio interface

control the gpio directory in: / sys/class/gpio

  • /sys/class/gpio/export

export is used to notify system need to export control GPIO pin number

  • /sys/class/gpio/unexport

unexport is used to cancel the export notification to system

  • /sys/class/gpio/gpiochipX

gpiochipX said is a gpio_chip, used to manage and control a group of gpio port controller Directory information stored system GPIO registers,

Base: starting each register control pin number,

label: register a name or logo

ngpio: the total number of pins

  • /sys/class/gpioN

gpioN: specific gpio pin

direction: the direction of the gpio port, read the result is in or out.

value: level of gpio pin, 0 (low level) 1 (high level), if the gpio is configured as output, the value is writable, remember any nonzero value will output a high level

edge: the interrupt is triggered


Export the gpio pins

1 Calculate the pin number

pin number = base of control pin register + offset of control pin register

2 /sys/class/gpio/export write the number

for example gpio0 ,after the success of the command to generate/sys/class/gpio/gpio0 directory, if there are no corresponding directory, illustrate the pin cannot be derived

 echo 0 > /sys/class/gpio/export

3 The direction file in /sys/class/gpio/gpio0

direction: define input and output

defined as the output:

  echo out > direction

defined as the input:

  echo in > direction

4 The value file in /sys/class/gpio/gpio0

Value: level of gpio pin, 0 (low level) 1 (high level), if the gpio is configured as output, the value is writable, remember any nonzero value will output a high level

 echo 1 > value

export gpio0 pin:

33.png

File:34.png

But when we execute unexport will prompt error

  echo 0 > /sys/class/gpio/unexport
  echo 0 > /sys/class/gpio/export
  ash: write error: Invalid argument


The solution:


The ticket for gpio unexport The patch for gpio-ralink.c

How to register a GPIO as Led or Button

"make menuconfig" to enter the configuration interface,select: Led driver:

  LED modules    --->   <*> kmod-leds-gpio 
                     <*> kmod-ledtrig-usbdev

Button driver:

 Other modules  --->   <*> kmod-gpio-button-hotplug.

Open the WRTNODE.dts add Led:

  gpio-leds {
      compatible = "gpio-leds";
      indicator {
          label = "wrtnode:blue:indicator";
          gpios = <&gpio1 14 1>;
      };
  }; 

"compatible" match "compatible" in leds-gpio.c, "label" is the name of the device,<&gpio1 14 1> is equivalent to GPIO#40

35.png


36.png


add Button:

  gpio-keys-polled {
      compatible = "gpio-keys-polled";
      #address-cells = <1>;
      #size-cells = <0>;
      poll-interval = <20>;
      reset {
          label = "reset";
          gpios = <&gpio0 1 1>;
          linux,code = <0x198>;
        };
  };


SPI

WRTnode add spi slave

一:Modify spi controller driver to achieve the support of the spi cs1

We assume that $(TOPDIR) for OpenWrt of compiler directory. open spi-rt2880.c vim $(TOPDIR)/build_dir/target-mipsel_24kec+dsp_uClibc-0.9.33.2/linux-ramips_mt7620n/linux-3.10.49/drivers/spi/spi-rt2880.c find struct rt2880_spi_ops then set spi_ops[0].num_cs = 2 static struct rt2880_spi_ops spi_ops[] = {

   {
       .init_hw = rt2880_spi_reset,
       .set_cs = rt2880_spi_set_cs,
       .baudrate_set = rt2880_spi_baudrate_set,
       .write_read = rt2880_spi_write_read,
       .num_cs = 1,  // .num_cs = 2
   }, {
       .init_hw = rt5350_spi_reset,
       .set_cs = rt2880_spi_set_cs,
       .baudrate_set = rt2880_spi_baudrate_set,
       .write_read = rt2880_spi_write_read,
       .num_cs = 2,
   }, {
       .init_hw = mt7621_spi_reset,
       .set_cs = mt7621_spi_set_cs,
       .baudrate_set = mt7621_spi_baudrate_set,
       .write_read = mt7621_spi_write_read,
       .num_cs = 1,
   },

}; 二:Complete the registration of spi slave device where the dts cd $(TOPDIR)/ wget --no-check-certificate https://raw.githubusercontent.com/WRTnode/openwrt-patches/master/R39786/wrtnode_spidev_dts.patch patch -p1 < wrtnode_spidev_dts.patch

37.png

三:spi configuration

make menuconfig Kernel modules ---> SPI Support ---> <*> kmod-spi-dev 四:Compile make 五:flash Firmware The new firmware re-programmed into the WRTnode, perform the following operations on WRTnode:

ls /dev


38.png

Provide a demo for spidev