driver 如下:
/*********************************************************
File Name: my_nand.c
Author: Nightmare@EEFOCUS
Version: v0.1 2014-05-08
Description: Driver of nand_flash peripheral.
*********************************************************/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define DEVICE_NAME "nand_dev"
#define MY_KERNEL_ADDR 0x00060000
#define MY_KERNEL_SIZE 0x00500000
#define MY_ROOTFS_ADDR 0x00560000
#define MY_ROOTFS_SIZE 0x05AA0000
#define MY_DBFILE_ADDR 0x06200000
#define MY_DBFILE_SIZE 0x05AA0000
struct buff {
unsigned int addr;
unsigned char *pData;
};
struct buff *Buffer;
struct mtd_info *m_mtd;
int my_nand_open(struct inode * inode , const char* name)
{
return 0;
}
int my_nand_release(struct inode * inode, struct file *filp)
{
return 0;
}
int my_nand_write(struct file *filp, const char *buffer, size_t length, loff_t * offset)
{
size_t wlen = 0;
struct erase_info m_erase;
Buffer = (struct buff *)buffer; //获得app的write传到驱动中的结构体
switch(Buffer->addr)
{
case MY_KERNEL_ADDR:
{
m_mtd = get_mtd_device_nm("Kernel");//通过分区名获得mtd_info结构体
m_erase.mtd = m_mtd;
m_erase.addr = Buffer->addr;//填充擦除的结构体信息
m_erase.len = length;
m_mtd->erase (m_mtd, &m_erase);//调用mtd_info的erase函数来实现nandflash分区的擦除
//调用mtd_info的write函数来实现nandflash分区的写操作,把应用程序的buf写到mtd中。
m_mtd->write(m_mtd, Buffer->addr, length, &wlen, Buffer->pData);
if(wlen != length)
{
printk("write kernel failed!\n");
return 0;
}
break;
}
case MY_ROOTFS_ADDR:
{
m_mtd = get_mtd_device_nm("root");
m_erase.mtd = m_mtd;
m_erase.addr = Buffer->addr;
m_erase.len = length;
m_mtd->erase (m_mtd, &m_erase);
m_mtd->write(m_mtd, Buffer->addr, length, &wlen, Buffer->pData);
if(wlen != length)
{
return 0;
}
break;
}
case MY_DBFILE_ADDR:
{
m_mtd = get_mtd_device_nm("dbfile");
m_erase.mtd = m_mtd;
m_erase.addr = Buffer->addr;
m_erase.len = length;
m_mtd->erase (m_mtd, &m_erase);
m_mtd->write(m_mtd, Buffer->addr, length, &wlen, Buffer->pData);
if(wlen != length)
{
return 0;
}
break;
}
default:
break;
}
return wlen;
}
int my_nand_read(struct file *filp, char *buffer, size_t length, loff_t * offset)
{
size_t rlen = 0;
Buffer = (struct buff *)buffer;
switch(Buffer->addr)
{
case MY_DBFILE_ADDR:
{
m_mtd = get_mtd_device_nm("dbfile");
m_mtd->read(m_mtd, Buffer->addr, length, &rlen, Buffer->pData); //调用mtd_info的read函数来实现nandflash分区的读操作,把nandflsash中的数据读到应用程序的buf。
break;
}
default:
break;
}
return rlen;
}
int my_nand_ioctl(struct file *filp, unsigned int num, unsigned long data)
{
return 0;
}
struct file_operations my_nand_fops =
{
.owner = THIS_MODULE,
.open = my_nand_open,
.release = my_nand_release,
.read = my_nand_read,
.write = my_nand_write,
.unlocked_ioctl = my_nand_ioctl,
};
struct miscdevice my_nand_dev =
{
.minor = MISC_DYNAMIC_MINOR,
.name = DEVICE_NAME,
.fops = &my_nand_fops,
};
int __init my_nand_init(void)
{
int ret;
ret = misc_register(&my_nand_dev);
if (ret)
{
printk("my_nand:[ERROR] Misc device register failed\n");
return ret;
}
printk("my_nand: Tata! Module init complete!\n");
return 0;
}
void __exit my_nand_exit(void)
{
misc_deregister(&my_nand_dev);
printk("my_nand: Module exit!\n");
}
module_init(my_nand_init);
module_exit(my_nand_exit);
MODULE_AUTHOR("NAND");
MODULE_ALIAS("my_nand");
MODULE_DESCRIPTION("nand flash module");
MODULE_LICENSE("GPL");
app:如下:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MY_KERNEL_ADDR 0x00060000
#define MY_KERNEL_SIZE 0x00500000
#define MY_ROOTFS_ADDR 0x00560000
#define MY_ROOTFS_SIZE 0x05AA0000
#define MY_DBFILE_ADDR 0x06200000
#define MY_DBFILE_SIZE 0x05AA0000
#define PAGE_SIZE512 512
#define PAGE_SIZE2048 2048
#define DEVICE_NAME "/dev/nand_dev"
#define WRITE 1
#define READ 0
#define MODE WRITE
struct buff {
unsigned int addr;
unsigned char *pData;
};
struct buff Buffer;
int main(void)
{
unsigned char wData[512] = "hello word!", rData[32] = {0};
int fd = -1;
Buffer.addr = MY_DBFILE_ADDR;
// Buffer.pData = wData;
fd = open(DEVICE_NAME, O_RDWR);
printf("fd = %d\n", fd);
#if MODE
if(write(fd, (char*)&Buffer,512) <= 0)
{
printf("write failed!\n");
return 0;
}
#endif
memset(rData, 0, 32);
Buffer.pData = rData;
if(read(fd,(char*)&Buffer, 32)<= 0)
{
printf("read failed!\n");
return 0;
}
printf("rData:%s\n", rData);
return 0;
}
程序经过实现已经完成了app对nandflash的读和写。
阅读(751) | 评论(0) | 转发(0) |