新增LED设备--从上层到底层理解安卓架构之内核篇
扫描二维码
随时随地手机看文章
为了更好的理解安卓的层次关系,本文在RK3399的安卓系统上增加LED灯的外设,并使用APP打开关闭LED灯。以这样一个最简单的实例,来演示从上层到底层的调用过程。首先从最底层的kernel层开始。
一、驱动开发
图:led灯原理图
1)设备树文件(kernel/arch/arm64/boot/dts/rockchip/rk3399-nanopi4-common.dtsi)
test-leds{
compatible = "test,leds";
led1-work = <&gpio1 23 GPIO_ACTIVE_LOW>;
led2-work = <&gpio1 24 GPIO_ACTIVE_LOW>;
status = "okay";
};
2) 驱动文件(kernel/drivers/gpio/gpio-testled.c)
MODULE_AUTHOR("embeddedtech");
MODULE_LICENSE("Dual BSD/GPL");
struct led_data {
int led1_pin; //led1引脚
int led2_pin; //led2引脚
};
struct led_data led_info;
/*
* Open the device; in fact, there's nothing to do here.
*/
int testled_open (struct inode *inode, struct file *filp)
{
return 0;
}
ssize_t testled_read(struct file *file, char __user *buff, size_t count, loff_t *offp)
{
return 0;
}
ssize_t testled_write(struct file *file, const char __user *buff, size_t count, loff_t *offp)
{
return 0;
}
static long testled_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
switch (cmd) {
case LED1CTRL_ON_CMD: {
gpio_direction_output(led_info.led1_pin,1);
break;
}
case LED1CTRL_OFF_CMD: {
gpio_direction_output(led_info.led1_pin,0);
break;
}
case LED2CTRL_ON_CMD: {
gpio_direction_output(led_info.led2_pin,1);
break;
}
case LED2CTRL_OFF_CMD: {
gpio_direction_output(led_info.led2_pin,0);
break;
}
default: {
printk("testled compat Default %d\n",cmd);
break;
}
}
return 0;
}
long testled_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
switch (cmd) {
case LED1CTRL_ON_CMD: {
gpio_direction_output(led_info.led1_pin,1);
break;
}
case LED1CTRL_OFF_CMD: {
gpio_direction_output(led_info.led1_pin,0);
break;
}
case LED2CTRL_ON_CMD: {
gpio_direction_output(led_info.led2_pin,1);
break;
}
case LED2CTRL_OFF_CMD: {
gpio_direction_output(led_info.led2_pin,0);
break;
}
default: {
printk("testled Default %d\n",cmd);
break;
}
}
return 0;
}
static int testled_release(struct inode *node, struct file *file)
{
return 0;
}
/*
* Our various sub-devices.
*/
/* Device 0 uses remap_pfn_range */
static struct file_operations testled_remap_ops = {
.owner = THIS_MODULE,
.open = testled_open,
.release = testled_release,
.read = testled_read,
.write = testled_write,
.unlocked_ioctl = testled_ioctl,
.compat_ioctl = testled_compat_ioctl,
};
static struct miscdevice testled_misc = {
.minor = MISC_DYNAMIC_MINOR,
.name = "test-led",
.fops = &testled_remap_ops,
};
/*
* Module housekeeping.
*/
static int testled_probe(struct platform_device *pdev)
{
int ret;
struct device_node *led_node = pdev->dev.of_node;
//enum of_gpio_flags work_flags;
ret = misc_register(&testled_misc);
if(ret < 0){
pr_err("testled_misc_register fails!!!\n");
return ret;
}
led_info.led1_pin = of_get_named_gpio(led_node, "led1-work", 0);
if(!gpio_is_valid(led_info.led1_pin)){
pr_err("led1 pin invaild: %d",led_info.led1_pin);
ret = -ENODEV;
goto probe_fail;
}
ret = gpio_request(led_info.led1_pin, "led1_pin");
if(ret < 0){
pr_err("led1 pin request failed.");
goto probe_fail;
}
led_info.led2_pin = of_get_named_gpio(led_node, "led2-work", 0);
if(!gpio_is_valid(led_info.led2_pin)){
pr_err("led2 pin invaild: %d",led_info.led2_pin);
ret = -ENODEV;
goto probe_fail;
}
ret = gpio_request(led_info.led2_pin, "led2_pin");
if(ret < 0){
pr_err("led2 pin request failed.");
goto probe_fail;
}
printk("led1 pin is: %d,led2 pin is: %d",led_info.led1_pin,led_info.led2_pin);
return 0;
probe_fail:
return ret;
}
static int testled_remove(struct platform_device *pdev)
{
pr_err("testled_misc_remove!!!\n");
misc_deregister(&testled_misc);
return 0;
}
static struct of_device_id testled_table[] = {
{ .compatible = "test,leds",}, //Compatible node must match dts
{ },
};
static struct platform_driver testled_driver = {
.probe = testled_probe,
.remove = testled_remove,
.driver = {
.name = "test_leds",
.owner = THIS_MODULE,
.of_match_table = testled_table,
}
};
static int testled_init(void)
{
int ret;
ret = platform_driver_register(&testled_driver);
if(ret < 0)
pr_err("platform_register_driver fails!!!\n");
pr_err("platform_register_driver succeeds :ret=%d!!!\n",ret);
return ret;
}
static void testled_cleanup(void)
{
platform_driver_unregister(&testled_driver);
}
module_init(testled_init);
module_exit(testled_cleanup);
3)Makefile文件(kernel/drivers/gpio/Makefile)
obj-$(CONFIG_GPIO_TEST_LED) += gpio-testled.o
4) Kconfig文件 (kernel/drivers/gpio/Kconfig)
config GPIO_TEST_LED
bool "select test leds"
default y
5)驱动配置文件(kernel/arch/arm64/configs/nanopi4_nougat_defconfig)
CONFIG_GPIO_TEST_LED=y
6)修改节点权限(device/rockchip/common/ueventd.rockchip.rc)
/dev/test_leds 0666 system system
二、驱动测试
测试代码gpioleds_test.c
int main()
{
int i = 0;
int dev_fd;
dev_fd = open("/dev/test-led",O_RDWR | O_NONBLOCK);
if ( dev_fd == -1 ) {
printf("Cann't open file /dev/test-led\n");
exit(1);
}
printf("Led1 on\n");
ioctl (dev_fd, LED1CTRL_ON_CMD,0);
getchar();
ioctl (dev_fd, LED1CTRL_OFF_CMD,0);
printf("led1 off\n");
printf("Led2 on\n");
ioctl (dev_fd, LED2CTRL_ON_CMD,0);
getchar();
ioctl (dev_fd, LED2CTRL_OFF_CMD,0);
printf("led2 off\n");
close(dev_fd);
return 0;
}
2)Android.mk
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= \
gpioleds_test.c
LOCAL_SHARED_LIBRARIES := \
liblog \
libc \
libutils \
libcrypto \
libhardware \
LOCAL_MODULE := gpioleds_test
LOCAL_MODULE_TAGS :=
LOCAL_MODULE_PATH := $(LOCAL_PATH)
LOCAL_CFLAGS +=-D_FORTIFY_SOURCE=0
LOCAL_CFLAGS += -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=0
include $(BUILD_EXECUTABLE
3)编译成可执行文件
mmm external/gpioleds_test/
完成在目录下生成二进制文件gpioleds_test。拷贝进安卓设备。
4)测试
Chmod 777 /data/user/gpioleds_test
./ data/user/gpioleds_test
扫码关注我们
看更多嵌入式案例
喜欢本篇内容请给我们点个再看
免责声明:本文内容由21ic获得授权后发布,版权归原作者所有,本平台仅提供信息存储服务。文章仅代表作者个人观点,不代表本平台立场,如有问题,请联系我们,谢谢!