sam9261s片內(nèi)sram映射
在真實的終端應用中,將采用sram來保存變化頻繁的終端實時數(shù)據(jù),這樣終端偶然的掉電數(shù)據(jù)也不會丟失。sbc9261s無外擴的sram,只能利用其片內(nèi)16k的sram做了,主要思路:做個簡單的內(nèi)核模塊,在內(nèi)核態(tài)完成sram地址的映射,并提供接口完成內(nèi)核態(tài)地址到用戶空間的映射。先來個linux驅(qū)動的簡單測試主要代碼網(wǎng)上搜的,makefile結(jié)合:https://www.ibm.com/developerworks/cn/linux/l-module26/假設驅(qū)動包含多個.c文件:
1.a.h
#ifndef _A_H_
#define _A_H_
void sayHello(void);
#endif
2.a.c
#include"a.h"
#include
#include
void sayHello(void)
{
printk("nhello world!n");
}
3.hello_medule.c
/*hello_module.c*/
#include
#include
#include "a.h"http://包含頭文件
static int __init mini2440_hello_module_init(void)
{ printk("Hello, Mini2440 module is installed !n");
sayHello();//這里調(diào)用a.h中定義的方法
return 0;
}
static void __exit mini2440_hello_module_cleanup(void)
{ printk("Good-bye, Mini2440 module was removed!n");
}
module_init(mini2440_hello_module_init);
module_exit(mini2440_hello_module_cleanup);
MODULE_LICENSE("GPL");
makefile如下:
ifeq ($(KERNELRELEASE),)
KERNELDIR ?= ../linux-2.6.24
PWD := $(shell pwd)
all:
$(MAKE) -C $(KERNELDIR) M=$(PWD)
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions Module.symvers
else
obj-m := mymodule.o
mymodule-objs:= a.o hello_module.o
endif
測試方法:insmod ./mymodule.ko
rmmod mymodule
lsmod
接下來是sram的映射驅(qū)動(以下內(nèi)容基于linux2.6.24內(nèi)核):
//sram_module.h驅(qū)動頭文件:
#define SRAM_STARTADD 0x300000 //arm926ej-s datasheet
#define SRAM_SIZE (0x400<<4) //16kB
#define SRAM_MAJOR 200
#define SRAM_FILENAME "sram_mmap"
int sram_open(struct inode *inode, struct file *file);
int sram_release(struct inode *inode, struct file *file);
int sram_mmap(struct file *file, struct vm_area_struct *vma);
int sram_ioctl(struct inode *inode, struct file *file,unsigned int cmd, unsigned long arg) ;
static struct file_operations sram_fops =
{
owner: THIS_MODULE,
mmap: sram_mmap,
open: sram_open,
ioctl: sram_ioctl,
release: sram_release,
};
接下來是驅(qū)動的實現(xiàn)://sram_module.c
#include
#include
#include
#include
#include
#include
#include
#include "sram_module.h"
#if 0
#define IOMAP_WRITE 1
#define IOMAP_READ 2
volatile unsigned short * reserve_virt_addr16;
#endif
MODULE_LICENSE ("GPL");
struct sram_dev
{
struct cdev cdev;
dev_t devno;
}mydev;
struct class *my_class;
static void reg_setup_cdev (struct sram_dev * _dev)
{
int error =0;
cdev_init (&_dev->cdev, &sram_fops);
_dev->cdev.owner = THIS_MODULE;
error = cdev_add (&_dev->cdev, _dev->devno , 1);
if (error)
printk (KERN_NOTICE "sram:Error %d adding reg_setup_cdev", error);
}
static int __init sram_init_module(void)
{
int result;
mydev.devno = MKDEV (SRAM_MAJOR, 0);
result = register_chrdev_region (mydev.devno, 1, SRAM_FILENAME);
if (result<0)
{
printk (KERN_WARNING "sram: can't get major number %dn", SRAM_MAJOR);
return result;
}
reg_setup_cdev (&mydev);
/* create your own class under /sysfs */
my_class = class_create(THIS_MODULE, "my_sramclass");
if(IS_ERR(my_class))
{
printk("sram->Err: failed in creating class.n");
return -1;
}
/* register your own device in sysfs, and this will cause udev to create corresponding device node */
//device_create( my_class, NULL, mydev.devno, SRAM_FILENAME "%d", 0 );
device_create( my_class, NULL, mydev.devno, SRAM_FILENAME);
printk (KERN_INFO "sram:Registered character drivern");
#if 0
reserve_virt_addr16 = (short *)ioremap( SRAM_STARTADD,SRAM_SIZE);
if ( !reserve_virt_addr16 )
{
printk("ioremap failedn");
return -EINVAL;
}
#endif
return 0;
}
/* remove the module */
static void __exit sram_cleanup_module(void)
{
unregister_chrdev( SRAM_MAJOR, SRAM_FILENAME );
cdev_del (&mydev.cdev);
device_destroy(my_class, mydev.devno); //delete device node under /dev
class_destroy(my_class); //delete class created by us
unregister_chrdev_region (mydev.devno, 1);
printk (KERN_INFO "sram:char driver cleaned upn");
}
int sram_ioctl(struct inode *inode, struct file *file,unsigned int cmd, unsigned long arg)
{
#if 0
static int i=0;
switch(cmd)
{
case IOMAP_WRITE:
for(i=0;i<10;i++)
*(reserve_virt_addr16+i)='A'+i;
break;
case IOMAP_READ:
for ( i=0;i<10;i++)
printk("[%d]:%cn",i,reserve_virt_addr16[i]);
break;
}
#endif
return 0;
}
int sram_open(struct inode *inode, struct file *file)
{
return(0);
}
int sram_release(struct inode *inode, struct file *file)
{
return(0);
}
int sram_mmap(struct file *file, struct vm_area_struct *vma)
{
size_t size = vma->vm_end - vma->vm_start;
unsigned long pfn = virt_to_phys((void *)(SRAM_STARTADD >> PAGE_SHIFT));
if (remap_pfn_range(vma,vma->vm_start,pfn,size,vma->vm_page_prot))
{
printk (KERN_INFO "sram:sram_mmap fail!n");
return -EAGAIN;
}
return 0;
}
module_init(sram_init_module);
module_exit(sram_cleanup_module);
MODULE_LICENSE("GPL");
主要使用mmap映射到用戶空間使用,在mdev的配合下insmod sram_mmap.ko,將自動創(chuàng)建/dev/sram_mmap設備文件。接下來是測試程序,來驗證驅(qū)動是否可用,由于目前開發(fā)板配置的啟動方式是dataflash,第1階段的Bootstrap加載在sram中,基本上把sram快覆蓋完了,我們只用16k最后幾個字節(jié)來做測試。
#include
#include
#include
#include
#define MMAP_SIZE 1024*16
#define DEV_FILE "/dev/sram_mmap"
struct zydz
{
char buff[MMAP_SIZE];
};
void readbuff()
{
#if 1
int fd;
int i;
struct zydz * vadr=NULL;
fd = open(DEV_FILE, O_RDWR);
vadr = (struct zydz *)mmap(NULL, MMAP_SIZE, PROT_READ|PROT_WRITE , MAP_SHARED, fd, 0);
if (vadr == MAP_FAILED)
{
perror("mmap err");
exit(-1);
}
for(i=1;i<10;i++)
{
printf("[%c]",vadr->buff[1024*16-i]);
}
printf("n");
munmap(vadr, MMAP_SIZE);
close(fd);
#else
int fd = open(DEV_FILE, O_RDWR);
if(ioctl(fd,2,0)<0)
{
printf("ioctl failedn");
return ;
}
close(fd);
#endif
}
void writebuff()
{
#if 1
int fd;
int i;
struct zydz * vadr=NULL;
fd = open(DEV_FILE, O_RDWR);
vadr = (struct zydz *)mmap(NULL, MMAP_SIZE, PROT_READ|PROT_WRITE , MAP_SHARED, fd, 0);
if (vadr == MAP_FAILED)
{
perror("mmap err");
exit(-1);
}
for(i=1;i<10;i++)
{
vadr->buff[1024*16-i] = 'A'+ i;
}
printf("n");
munmap(vadr, MMAP_SIZE);
close(fd);
#else
int fd = open(DEV_FILE, O_RDWR);
if(ioctl(fd,1,0)<0)
{
printf("ioctl failedn");
return ;
}
close(fd);
#endif
}