你好!这里是风筝的博客,
欢迎和我一起交流。
前面写了NanoPi NEO的启动过程:NanoPi NEO小试牛刀(一)
但是我发现NanoPi NEO启动Linux内核时加载的东西太多了,Linux内核是没有经过剪裁的,我们可以试着剪裁优化下:
make menuconfig ARCH=arm CROSS_COMPILE=arm-linux-
Networking support —>Networking options—> Network packet filtering framework (Netfilter) 我不打算使用防火墙,要用到时再编译进去
Networking support—> < >CAN bus subsystem support 我不打算使用CAN总线
Networking support —> < > RF switch subsystem support 我没有RF 切换设备
Device Drivers —>< >Serial ATA and Parallel ATA drivers (libata) SATA或PATA接口的硬盘或光驱等设备
Device Drivers —>[ ] Multiple devices driver support (RAID and LVM) 暂时没有要使用Raid (磁盘阵列)和LVM (逻辑卷管理器,添加,删除逻辑分区)的需求
Device Drivers —> <> Sound card support 暂时没有无声卡
Device Drivers —> Graphics support –>< > DRM Support for Allwinner A10 Display Engine 没有视频播放
[ ] Virtualization 不需要
把这些都取消掉,然后选择支持nfs,这个好用:
File systems —> Network File Systems 支持nfs
最后保存退出,编译出来的zImage文件就小好多了
现在可以试着写一个简单的驱动试一下效果:
led.c:
/*
Port A(PA): 22 input/output port ?
Port C(PC): 19 input/output port ?
Port D(PD): 18 input/output port ?
Port E(PE) : 16 input/output port ?
Port F(PF) : 7 input/output port ?
Port G(PG) : 14 input/output port ?
Port L(PL) : 12 input/output port
PIO 0x01C20800
Pn_CFG0 n*0x24+0x00 Port n Configure Register 0 (n from 0 to 6)
Pn_CFG1 n*0x24+0x04 Port n Configure Register 1 (n from 0 to 6)
Pn_CFG2 n*0x24+0x08 Port n Configure Register 2 (n from 0 to 6)
Pn_CFG3 n*0x24+0x0C Port n Configure Register 3 (n from 0 to 6)
Pn_DAT n*0x24+0x10 Port n Data Register (n from 0 to 6)
Pn_DRV0 n*0x24+0x14 Port n Multi-Driving Register 0 (n from 0 to 6)
Pn_DRV1 n*0x24+0x18 Port n Multi-Driving Register 1 (n from 0 to 6)
Pn_PUL0 n*0x24+0x1C Port n Pull Register 0 (n from 0 to 6)
Pn_PUL1 n*0x24+0x20 Port n Pull Register 1 (n from 0 to 6)
*/
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/device.h>
//#include <linux/of_address.h>
//#include <linux/of_platform.h>
static volatile unsigned long *gpacon1;
static volatile unsigned long *gpadat;//a10
/*CFG0_REG Default Value: 0x7777777
* [30:28] Pn7_SELECT
* [26:24] Pn6_SELECT
* [22:20] Pn5_SELECT
* [18:16] Pn4_SELECT
* [14:12] Pn3_SELECT
* [10: 8] Pn2_SELECT
* [ 6: 4] Pn1_SELECT
* [ 2: 0] Pn0_SELECT
*000:Input 001:Output 111:disable
*/
/*CFG1_REG Default Value: 0x7777777
* [30:28] Pn15_SELECT
* [26:24] Pn14_SELECT
* [22:20] Pn13_SELECT
* [18:16] Pn12_SELECT
* [14:12] Pn11_SELECT
* [10: 8] Pn10_SELECT
* [ 6: 4] Pn9_SELECT
* [ 2: 0] Pn8_SELECT
*000:Input 001:Output
*/
/*CFG2_REG Default Value: 0x7777777
* [22:20] Pn21_SELECT
* [18:16] Pn20_SELECT
* [14:12] Pn19_SELECT
* [10: 8] Pn18_SELECT
* [ 6: 4] Pn17_SELECT
* [ 2: 0] Pn16_SELECT
*000:Input 001:Output
*/
/*Pn_DAT Default Value: 0x0
* [21:0] if input :read pin state; if outport : pin state is same bit
*/
static int led_open(struct inode *inode, struct file *file)
{
printk("this is nanopi neo led \n");
*gpacon1 |= (0x01<<(8));//PA10=Output
return 0;
}
static ssize_t led_read(struct file * file, char __user *buf, size_t count, loff_t *off)
{
printk("read nanopi neo led \n");
//*gpadat &= ~(0x01<<(10));//PA10=0
*gpadat |= (0x01<<(10));//PA10=1
return 0;
}
static ssize_t led_write(struct file *file, const char __user *buf,size_t count, loff_t *ppos)
{
//*gpadat
return 0;
}
static struct file_operations led10_fops = {
.owner = THIS_MODULE,
.open = led_open,
.read = led_read,
.write = led_write,
};
/* 1. 确定主设备号 */
static int major;
static struct cdev led10_cdev;
static struct class *cls;
static int led_drv_init(void)
{
int res;
struct device * led_res;
dev_t devid;
/* 3. 告诉内核 */
#if 0
major = register_chrdev(0, "hello", &hello_fops); /* (major, 0), (major, 1), ..., (major, 255)都对应hello_fops */
#else /*仅仅是注册设备号*/
if (major) {
devid = MKDEV(major, 0);
register_chrdev_region(devid, 1, "led_blue"); /* (major,0) 对应 pwm_fops, (major, 1~255)都不对应pwm_fops */
} else {
alloc_chrdev_region(&devid, 0, 1, "led_blue"); /* (major,0) 对应 pwm_fops, (major, 1~255)都不对应pwm_fops */
major = MAJOR(devid);
}
cdev_init(&led10_cdev, &led10_fops);
res=cdev_add(&led10_cdev, devid, 1);
if(res)
{
printk("cdev_add failed\n");
unregister_chrdev_region(MKDEV(major, 0), 1);
return 0;
}
#endif
cls = class_create(THIS_MODULE, "led_blue");
led_res = device_create(cls, (struct device *)NULL, MKDEV(major, 0), (void *)NULL, (const char *)"led_blue"); /* /dev/xxx */
if (IS_ERR((const void *)res))
{
printk("device_create failed\n");
return 0;
}
gpacon1 = ioremap(0x01C20800+0*0x24+0x04 , 0x4*4);
gpadat = gpacon1+3;
if (!gpacon1)
{
printk("ioremap failed\n");
return -EIO;
}
return 0;
}
static void led_drv_exit(void)
{
unregister_chrdev(major, "led_blue"); // 卸载
device_destroy(cls,MKDEV(major, 0));
class_destroy(cls);
iounmap(gpacon1);
}
module_init(led_drv_init);
module_exit(led_drv_exit);
MODULE_LICENSE("GPL");
这就是一个简单的控制PA10管脚的程序,测试了下,可以使用。