GPIO control of AM335X in linux
Author: chenzhufly QQ: 36886052 (reprinted please indicate the source)
Along the way, I am familiar with the hardware system, set up the software development environment, compile the Linux system, and so on. Now I should do some things on the hardware. This is my research experience over the past few days and I will share it with you.
1. GPIO char-type driver. Here we will focus on a lamp. Feel the driver design and hardware control driver:
Copy content to clipboardCode: # include <linux/init. h>
# Include <linux/module. h>
# Include <linux/leds. h>
# Include <linux/io. h>
# Include <linux/semaphore. h>
# Include <linux/kernel. h>
# Include <linux/cdev. h>
# Include <linux/types. h>
# Include <linux/fs. h>
# Include <mach/gpio. h>
# Include <plat/mux. h>
# Include <linux/gpio. h>
/*************************************** ****/
# Define NAME \ "leds \"
# Define GPIO_TO_PIN (bank, gpio) (32 * (bank) + (gpio ))
Static int major = 251; // defines the master device number.
/*************************************** ****/
Void led_on (void)
{
Gpio_set_value (GPIO_TO_PIN (1, 22), 1 );
}
Void led_off (void)
{
Gpio_set_value (GPIO_TO_PIN (1, 22), 0 );
}
Void led_init (void)
{
Int result;
/* Allocating GPIOs and setting direction */
Result = gpio_request (GPIO_TO_PIN (1, 22), \ "Leds \"); // usr1
If (result! = 0)
Printk (\ "gpio_request (rule 22) failed! \ N \");
Result = gpio_direction_output (GPIO_TO_PIN (1, 22), 1 );
If (result! = 0)
Printk (\ "gpio_direction (Direction 22) failed! \ N \");
}
Struct light_dev
{
Struct cdev;
Unsigned char value;
};
Struct light_dev * light_devp;
MODULE_AUTHOR (\ "chenzhufly \");
MODULE_LICENSE (\ "Dual BSD/GPL \");
// Open and Close Functions
Int light_open (struct inode * inode, struct file * filp)
{
Struct light_dev * dev;
// Obtain the device struct pointer
Dev = container_of (inode-> I _cdev, struct light_dev, cdev );
// Make the device struct as the private information of the device
Filp-> private_data = dev;
Return 0;
}
Int light_release (struct inode * inode, struct file * filp)
{
Return 0;
}
// Ioctl
Int light_ioctl (struct file * filp, unsigned int cmd,
Unsigned long arg)
{
Struct light_dev * dev = filp-> private_data;
Switch (cmd)
{
Case 0:
Dev-> value = 0;
Led_off ();
Break;
Case 1:
Dev-> value = 1;
Led_on ();
Break;
Default:
Return-ENOTTY;
// Break;
}
Return 0;
}
Struct file_operations light_fops =
{
. Owner = THIS_MODULE,
. Unlocked_ioctl = light_ioctl,
. Open = light_open,
. Release = light_release,
};
// Module Loading Function
Int light_init (void)
{
Int ret;
Led_init ();
Printk (KERN_ALERT \ "led modules is install \ n \");
Ret = register_chrdev (major, NAME, & light_fops );
If (ret <0)
{
Printk (\ "unable to register myled driver! \ N \");
Return ret;
}
Return 0;
}
// Unmount a function
Void light_cleanup (void)
{
Unregister_chrdev (major, NAME );
Printk (\ "Goodbye, cruel world! \ N \");
}
Module_init (light_init );
Module_exit (light_cleanup );
Application:
Copy content to clipboardCode: # include <stdlib. h>
# Include <sys/types. h>
# Include <sys/stat. h>
# Include <fcntl. h>
# Include <linux/input. h>
# Include <unistd. h>
# Include <sys/ioctl. h>
Int main (int argc, char * argv)
{
Int I, n, fd;
Fd = open (\ "/dev/leds \", O_RDWR );
If (fd <0)
{
Printf (\ "can't open/dev/leds! \ N \");
Exit (1 );
}
While (1 ){
Ioctl (fd, 1, 1 );
Sleep (1 );
Ioctl (fd, 0, 1 );
Sleep (1 );
}
Close (fd );
Return 0;
}
Makefile file:
Copy content to clipboardCode: ARCH = arm
CROSS_COMPILE =/home/chenzhufly/beaglebone/linux-devkit/bin/arm-arago-linux-gnueabi-
Obj-m: = leds. o
KDIR: =/home/chenzhufly/beaglebone/board-support/linux-3.1.0-psp04.06.00.03.sdk
PWD: = $ (shell pwd)
Default:
Make-C $ (KDIR) M = $ (PWD) ARCH = $ (ARCH) CROSS_COMPILE = $ (CROSS_COMPILE) modules
App: leds_test.c
$ (CROSS_COMPILE) gcc-o leds_test leds_test.c
Clean:
$ (MAKE)-C $ (KDIR) M = $ (PWD) clean
Leds. sh script
Copy content to clipboardCode: insmod leds. ko
Mknod/dev/leds c 251 0
./Leds_test
2. Use the echo command. I have mentioned this before.
Copy content to clipboardCode: lights up usr1
Root @ beaglebone :~ # Echo 1>
/Sys/class/leds/beaglebone: usr1/brightness
Disable usr1
Root @ beaglebone :~ # Echo 0>/sys/class/leds/beaglebone: usr1/brightness
3. Create an application to implement the fl lamp function.
Copy content to clipboardCode: # include <stdio. h>
# Include <unistd. h>
# Include <sys/types. h>
# Include <sys/ipc. h>
# Include <sys/ioctl. h>
# Include <fcntl. h>
# Define LED1 \ "/sys/class/leds/beaglebone: usr1/brightness \" // usr1 led
# Define LED2 \ "/sys/class/leds/beaglebone: usr2/brightness \" // usr2 led
# Define LED3 \ "/sys/class/leds/beaglebone: usr3/brightness \" // usr3 led
Int main (void)
{
Int f_led1 = open (LED1, O_RDWR );
Int f_led2 = open (LED2, O_RDWR );
Int f_led3 = open (LED3, O_RDWR );
Unsigned char dat1, dat2, dat3;
Unsigned char I = 0;
If (f_led1 <0)
{
Printf (\ "error in open % s \", LED1 );
Return-1;
}
If (f_led2 <0)
{
Printf (\ "error in open % s \", LED2 );
Return-1;
}
If (f_led3 <0)
{
Printf (\ "error in open % s \", LED3 );
Return-1;
}
// Add 10 times
For (I = 1; I <30; I ++)
{
Dat1 = (I % 3) = 1 )? '1': '0 ';
Dat2 = (I % 3) = 2 )? '1': '0 ';
Dat3 = (I % 3) = 0 )? '1': '0 ';
Write (f_led1, & dat1, sizeof (dat1 ));
Write (f_led2, & dat2, sizeof (dat2 ));
Write (f_led3, & dat3, sizeof (dat3 ));
Usleep (300000 );
}
// All the bright
{
Dat1 = '1 ';
Dat2 = '1 ';
Dat3 = '1 ';
Write (f_led1, & dat1, sizeof (dat1 ));
Write (f_led2, & dat2, sizeof (dat2 ));
Write (f_led3, & dat3, sizeof (dat3 ));
}
}