sam9261s片内sram映射
扫描二维码
随时随地手机看文章
在真实的终端应用中,将采用sram来保存变化频繁的终端实时数据,这样终端偶然的掉电数据也不会丢失。sbc9261s无外扩的sram,只能利用其片内16k的sram做了,主要思路:做个简单的内核模块,在内核态完成sram地址的映射,并提供接口完成内核态地址到用户空间的映射。先来个linux驱动的简单测试主要代码网上搜的,makefile结合:https://www.ibm.com/developerworks/cn/linux/l-module26/假设驱动包含多个.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"//包含头文件
static int __init mini2440_hello_module_init(void)
{ printk("Hello, Mini2440 module is installed !n");
sayHello();//这里调用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的映射驱动(以下内容基于linux2.6.24内核):
//sram_module.h驱动头文件:
#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,
};
接下来是驱动的实现://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,将自动创建/dev/sram_mmap设备文件。接下来是测试程序,来验证驱动是否可用,由于目前开发板配置的启动方式是dataflash,第1阶段的Bootstrap加载在sram中,基本上把sram快覆盖完了,我们只用16k最后几个字节来做测试。
#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
}