MediaTek Linkit 7688 (ii) GPIO basic operation and C language programming __ programming

Source: Internet
Author: User

Linkit Series Blog:

MediaTek Linkit 7688 (i) Start and build OpenWrt Cross compilation environment under Mac, C language compiling Hello,world

MediaTek Linkit 7688 (ii) GPIO basic operation and C language programming

MediaTek Linkit 7688 DUO (iii): Control peripherals and sensors through Arduino

Linkit 7688 DUO (iv): Connect the various Arduino sensors and modules--basic articles

Linkit 7688 DUO (v) Connect various Arduino sensors and modules-extension

Linkit 7688 DUO (vi) joined the MQTT IoT agreement


GPIO (General purpose input output) full name for the general-purpose input, is a CPU of a pin, that can do input, also can do output, often used to switch, key, LED, sensor, etc. to the CPU.

Linkit 7688 has more than 40 gpio pins, numbered respectively: Gpio1, Gpio2, ... Gpioxx


First, GPIO Foundation

The GPIO pin supports up to 4 MA current and is activated at a voltage of 3.3V. The pins have two states: High or low, higher is the High-voltage (3.3V), Lower is the Low-voltage (0 V)

Depending on the connection circuit, there are two ways to activate a device that connects Gpio pins:

ActiveHigh: When the GPIO is high, the device is active Low: When the GPIO is low, the device is activated the following figure shows the LEDs and buttons are active high and Active low two ways to access Gpio circuit diagram
Second, the software control of Gpio
In Linux, GPIO is expressed as a device file, and the operation of GPIO is done by means of file reading and writing.          A gpio device file is stored under the directory/sys/class/gpio/. Check out this directory for a 7688 Gpio

Ls/sys/class/gpio

return Result:

Export gpiochip0 gpiochip127 gpiochip32 gpiochip64 unexport

Among them: Export and Unexport are two files, others are directories.

To manipulate a gpio, first write the Gpio pin number first into the export file to enter the command line: Echo >/sys/class/gpio/export and then view the Gpio directory LS/SYS/CL Ass/gpio results are:

Export gpiochip0 gpiochip32 unexport

gpio44 gpiochip127 Gpiochip64

As you can see, there is an extra directory named gpio44 in this directory. Enter this directory to operate on the GPIO44 port.

Check out the gpio44 directory: ls/sys/class/gpio/gpio44

The result is a few files:

active_low Device Direction edge subsystem uevent value

which

The direction file is the input and output direction of the Gpio, the text "Out" is written to the file, then the Gpio port is placed as the output state. Writes "In" to the file, the Gpio port is placed in the input state.

The value file is the current state of the Gpio, 1 or 0 (that is, high or low). Write the text "1" to the file, then the GPIO is set to High (High-voltage), the text "0" is written to the file, then the GPIO is set to Low (Low-voltage)


With the command line, set the gpio44 direction to "out" and the value set to "0"

echo Out >/sys/class/gpio/gpio44/direction

echo 0 >/sys/class/gpio/gpio44/value

At this point, the WiFi light on the development Board is lit. (The WiFi light from the development Board is connected to the gpio44, because the WiFi light access mode is active low, so write "0" lamp light)

Write "1", the WiFi light on the development Board is out and the command is as follows:

Echo 1 >/sys/class/gpio/gpio44/value

After the operation is complete, you can write the Gpio pin number to the Unexport file

echo >/sys/class/gpio/unexport

The directory/sys/class/gpio/gpio44 will disappear and cannot operate gpio 44 feet.


The above process is a common approach for all Linux versions, not only for openwrt unique.

Three, C language programming operation Gpio

Understand the above process, also understand: C language Programming Operation Gpio is actually read and write files.

For example: The gpio44 is set to high

int FP =open ("/sys/class/gpio/gpio44/value", o_wronly);

Write (FP, "1", 2);

Close (FP);

In general, operations on device files use open (), close () instead of fopen (), fclose (). The former is an operating system function, no buffer, the latter is a standard C function, there is buffering.

For ease of use, I wrote a gpio module containing two files gpio.c, gpio.h, with several Gpio common operation functions. Like what:


/**

* Export specified GPIO pin

* @param pin_number specified the Pin

* @return 1 if success.

* Return negative integer error code if fail.

*/

int gpio_export (int pin_number);


/**

* Write specified GPIO pin

* @param pin_number specified the Pin

* @param value

* @return 1 if success.

* Return negative integer error code if fail.

*/

int gpio_write (int pin_number,int value);


/**

* Set direction of specified GPIO Pin

* @param pin_number specified the Pin

* @param direction could be gpio_in or gpio_out, Gpio_out_high, Gpio_out_low

* @return 1 if success.

* Return negative integer error code if fail.

*/

int gpio_set_direction (int pin_number,int direction);

...


Look at Gpio.h files and example.


Routines:


#include "gpio.h"


int main () {

Gpio_export (44); Export gpio44

Gpio_set_direction (Gpio_output); Set GPIO44 to output state

Gpio_write (Gpio_low); Set the gpio44 value to low and the WiFi light on the 7688 board

}


Gpio modules and routines to share in my downloads: Gpio Library

For the CPU directly leads to the Gpio pin, the above operation can be.


However, in the Linkit 7688 duo Development Board, the GPIO pins on board are provided by a ATMEGA32U4 chip, which provides the Arduino development interface.

Therefore, to operate the Gpio port of the Linkit 7688 Duo Development Board, you need to install the Arduino IDE and Arduino program Atmega32u4 to control the Gpio port.

Linkit 7688 main chip control ATMEGA32U4 via serial port. (see the following posts for details)









Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.