|
下面是我写的应用程序与对应的SPI驱动,在NFS下运行没有问题,但是现在直接用U盘,或其他方式下载到开发板上运行,就出现Segmentation fault ,请问这种问题要怎么解决!急!!
应用程序:把一个107565字节的数读取,然后写到下面的驱动,最后通过SPI输出
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/select.h>
#include <sys/time.h>
#include <errno.h>
int main(void)
{int fp;
int fw;
FILE *fxdata;
int fdata;
size_t length;
unsigned char *bufdat;
fxdata=fopen("/home/fpgadata.txt","r");
fseek(fxdata,0,SEEK_END);
length=ftell(fxdata);
printf("data size = %d",length);
printf("Byte,ttf format \n");
fclose(fxdata);
fdata=open("/home/fpgadata.txt",O_RDONLY);
if(fdata<0)
printf("open config data fault!\n");
else printf("open config data success!\n");
bufdat=calloc(length-1,sizeof(char));
read(fdata,bufdat,length);
close(fdata);
fp=open("/dev/SPI_FPGA1",O_RDWR,0666);
if(fp<0)
{ printf("open fault!\n");
free(bufdat);
printf("try open agin please!\n");
return -1;
}
else
{
printf("open ps fpga success\n");
printf("sending data.....\n ");
fw= write(fp,bufdat,length-1);
if(fw==0)
printf("write success\n");
else
printf("try write fpga agin!\n");
free(bufdat);
return 0;
}
驱动::#include<linux/configfs.h>
#include<linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/init.h>
//#include <linux/devfs_fs_kernel.h>
#include <linux/miscdevice.h>
#include <linux/delay.h>
#include <asm/irq.h>
#include <mach/map.h>
#include <mach/regs-gpio.h>
//#include <asm-arm/arch-s3c2410/regs-gpio.h>
#include <mach/hardware.h>
//#include <asm/arch/map.h>
#include <asm/io.h>
#include <asm/uaccess.h>
#include <asm/system.h>
#include <linux/cdev.h>
#include <linux/sched.h>
#include <linux/errno.h>
#include <linux/poll.h>
#include <linux/device.h>
#include <linux/mm.h>
#define SPI_DEV 252 //device number
#define DEVICE_NAM "SPI_FPGA1"
#define version "V-0908"
static unsigned long *SPIGPDCON;
static unsigned long *SPIGPECON;
static unsigned long *SPIGPFCON;
static unsigned long *SPIGPGCON;
static unsigned long *SPISPCON_1;
static unsigned long *SPISPSTA_1;
static unsigned long *SPISPPIN_1;
static unsigned long *SPISPPRE_1;
static unsigned long *SPISPTDAT_1;
static unsigned long *SPISPRDAT_1;
static unsigned long *SPICLKCON_1;
//#define spi_gpdcon (*(volatile unsigned long *)0x56000030);
//#define spi_gpecon (*(volatile unsigned long *)0x56000040);
//#define spi_gpfcon (*(volatile unsigned long *)0x56000050);
//#define spi_gpgcon (*(volatile unsigned long *)0x56000060);
//#define spi_spcon1 (*(volatile unsigned long *)0x59000000);
//#define spi_spsta1 (*(volatile unsigned long *)0x59000004);
//#define spi_sppin1 (*(volatile unsigned long *)0x59000008);
//#define spi_sppre1 (*(volatile unsigned long *)0x5900000c);
//#define spi_sptdat1 (*(volatile unsigned long *)0x59000010);
//#define spi_sprdat1 (*(volatile unsigned long *)0x59000014);
#define spi_gpdcon (*(volatile unsigned long *)SPIGPDCON)
#define spi_gpecon (*(volatile unsigned long *)SPIGPECON)
#define spi_gpfcon (*(volatile unsigned long *)SPIGPFCON)
#define spi_gpgcon (*(volatile unsigned long *)SPIGPGCON)
#define spi_spcon1 (*(volatile unsigned long *)SPISPCON_1)
#define spi_spsta1 (*(volatile unsigned long *)SPISPSTA_1)
#define spi_sppin1 (*(volatile unsigned long *)SPISPPIN_1)
#define spi_sppre1 (*(volatile unsigned long *)SPISPPRE_1)
#define spi_sptdat1 (*(volatile unsigned long *)SPISPTDAT_1)
#define spi_sprdat1 (*(volatile unsigned long *)SPISPRDAT_1)
#define spi_clkcon (*(volatile unsigned long *)SPICLKCON_1)
#define status S3C2410_GPG2 //cofig start (nss_spi)
#define status_in S3C2410_GPG2_INP //set gpg2 output;
#define nconfig S3C2410_GPG13 // read the fpga output for fpga status(gpg13)
#define nconfig_out S3C2410_GPG13_OUTP//set gpg13 output
static int ps_ioport_define(void)
{
printk("************************\n");
printk("ps config now!\n");
printk("begin spi port init\n");
spi_clkcon |=(1<<18);
spi_sppre1 =0x02;
spi_spcon1 =0x18;
spi_sptdat1=0xff;
spi_sptdat1=0xff;
spi_sptdat1=0xff;
spi_sptdat1=0xff;
spi_sptdat1=0xff;
spi_sptdat1=0xff;
spi_sptdat1=0xff;
spi_sptdat1=0xff;
spi_sptdat1=0xff;
spi_sppin1 =0x03;
spi_gpecon &=(~0x05400000); // set spi port (port)
spi_gpecon |= 0x0a800000;
s3c2410_gpio_pullup(S3C2410_GPE12,0);
s3c2410_gpio_pullup(S3C2410_GPE11,0);
s3c2410_gpio_pullup(S3C2410_GPE13,1);
s3c2410_gpio_cfgpin(S3C2410_GPE13,S3C2410_GPE13_SPICLK0);
s3c2410_gpio_cfgpin(S3C2410_GPE12,S3C2410_GPE12_SPIMOSI0);
s3c2410_gpio_cfgpin(S3C2410_GPE11,S3C2410_GPE11_SPIMISO0);
printk("spi init end!\n");
printk("io init begin!\n");
s3c2410_gpio_cfgpin(nconfig,nconfig_out);
s3c2410_gpio_pullup(nconfig,0);
s3c2410_gpio_cfgpin(status,status_in);
s3c2410_gpio_pullup(status,0);
printk("spi/io driver port init end!\n");
return 0;
}
static int ps_fpga1_open(struct inode *node,struct file *file)
{int status_value=1;
// int confg_value=1;
ps_ioport_define();
s3c2410_gpio_setpin(nconfig,0);//set low 40us;
udelay(50);
s3c2410_gpio_setpin(nconfig,1);
status_value=s3c2410_gpio_getpin(status);
if((status_value)<=0)
{ printk("init fpga sucess!\n"); }
// if(spi_spsta1&0x01) //read fpga ncofg_done ready?
// confg_value=spi_sprdat1;
udelay(45);
// if(spi_spsta1&0x01);
// confg_value=spi_sprdat1;
// while(!s3c2410_gpio_getpin(status));
// confg_value=s3c2410_gpio_getpin(status);
if(status_value<=0)
{
printk("ps config ready!\n");
return 1;
}
else
{
printk("ps not ready!\n");
return 0;
}
}
static ssize_t ps_fpga1_write(struct file *filp,const char *buffer,size_t count,loff_t *f_pos) //write to the fpga
{ int i;
unsigned char temp;
unsigned char *configdata;
// printk("ok\n");
configdata=kmalloc(count,GFP_KERNEL);
copy_from_user(configdata,buffer,count);
for(i=0;i<=count;i++)
{temp=configdata;
temp=((temp&0x01)<<7)|((temp&0x02)<<5)|((temp&0x04)<<3)|((temp&0x08)<<1)|
((temp&0x10)>>1)|((temp&0x20)>>3)|((temp&0x40)>>5)|((temp&0x80)>>7);
configdata=temp;
while((spi_spsta1&0x01)==0);
spi_sptdat1=configdata;
}
// printk("send data...!\n");
kfree(configdata);
return 0;
}
static ssize_t ps_fpga1_read(struct file *filp,char *buff,size_t count,loff_t *f_pos) //read from the fpga
{ int ncofgdone_vlaue;
while((spi_spsta1&0x01)==0);
ncofgdone_vlaue=(*(volatile unsigned long *)spi_sprdat1);
if(ncofgdone_vlaue>0)
printk("config fpga sucess!\n");
return 0;
}
static struct file_operations ps_fpga1_fops={ //register the devic file
.owner=THIS_MODULE,
.open =ps_fpga1_open,
.write=ps_fpga1_write,
.read =ps_fpga1_read,
};
static struct miscdevice misc = {
.minor = SPI_DEV,
.name = DEVICE_NAM,
.fops =&ps_fpga1_fops,
};
static int __init ps_fpga1_init(void)
{int ret;
printk("register driver for ps!\n");
// ret=register_chrdev(SPI_DEV,DEVICE_NAM,&ps_fpga1_fops);
ret=misc_register(&misc);
if(ret<0)
printk("register fault\n");
else printk("register success!\n");
SPIGPDCON = ioremap(0x56000030,4);
SPIGPECON = ioremap(0x56000040,4);
SPIGPFCON = ioremap(0x56000050,4);
SPIGPGCON = ioremap(0x56000060,4);
SPISPCON_1= ioremap(0x59000000,4);
SPISPSTA_1= ioremap(0x59000004,4);
SPISPPIN_1= ioremap(0x59000008,4);
SPISPPRE_1= ioremap(0x5900000c,4);
SPISPTDAT_1=ioremap(0x59000010,4);
SPISPRDAT_1=ioremap(0x59000014,4);
SPICLKCON_1=ioremap(0x4c00000c,4);
printk("I/O remap end !\n");
return 0;
}
static void __exit ps_fpga1_exit(void)
{// devfs_remove(DEVICE_NAM);
misc_deregister(&misc);
printk("###########################\n");
printk("remove driver success\n");
printk("###########################\n");
}
module_init(ps_fpga1_init);
module_exit(ps_fpga1_exit);
MODULE_AUTHOR("lin zhi tang");
MODULE_DESCRIPTION("PS FPGA DRIVER!");
MODULE_LICENSE("GPL"); |
|