【T113】【spi 驱动】【大数据DMA传输】
-
1.需求:通过gsip引脚接收来自fpga的数据等待接收中断,然后中断到来后,113的spi驱动为主控读取fpga的spi数据,数据量12800字节,驱动设置了10个缓冲区映射内存,spi内次写入一个缓冲区。中断频率为25Hz。应用程序通过map映射会读取存在的10个里面的ok的缓冲区。
2.现状:驱动代码基于imx6ull linux4.9版本修改,驱动已经编译通过,并且可以使用read、write接口正常的进行spi通讯(1000字节以内)。但是如果是使用GPIS中断函数来完成spi的读取,调用同样的spi读写函数,spi不能正常工作,会提示:“Bufferless transfer has length ”,错误来自spi.c。(注意,gpio中断是fpga的引脚给T113的引脚,代码中通过中断函数触发spi的一次传输)
3.驱动文件是:fsfpgain.c;read、write测试app是:test_spidev.c;gpio中断来读取spi测试app是:test_fsfpgain.c
下面是三个文件的代码:
fsfpgain.c#include <linux/types.h> #include <linux/kernel.h> #include <linux/delay.h> #include <linux/ide.h> #include <linux/init.h> #include <linux/module.h> #include <linux/errno.h> #include <linux/gpio.h> #include <linux/cdev.h> #include <linux/device.h> #include <linux/err.h> #include <linux/list.h> #include <linux/of_gpio.h> #include <linux/semaphore.h> #include <linux/timer.h> #include <linux/spi/spi.h> #include <linux/of.h> #include <linux/of_address.h> #include <linux/of_gpio.h> #include <linux/platform_device.h> #include <linux/atomic.h> #include <linux/irq.h> #include <linux/of_irq.h> #include <asm/mach/map.h> #include <asm/uaccess.h> #include <asm/io.h> #include <linux/spi/spi.h> #include <linux/spi/spidev.h> #include <linux/acpi.h> #include <linux/of_irq.h> #include <asm/mach/map.h> #include <asm/uaccess.h> #include <asm/io.h> /* * This supports access to SPI devices using normal userspace I/O calls. * Note that while traditional UNIX/POSIX I/O semantics are half duplex, * and often mask message boundaries, full SPI support requires full duplex * transfers. There are several kinds of internal message boundaries to * handle chipselect management and other protocol options. * * SPI has a character major number assigned. We allocate minor numbers * dynamically using a bitmask. You must use hotplug tools, such as udev * (or mdev with busybox) to create and destroy the /dev/spidevB.C device * nodes, since there is no fixed association of minor numbers with any * particular SPI bus or device. */ #define SPIDEV_MAJOR 153 /* assigned */ #define N_SPI_MINORS 32 /* ... up to 256 */ static DECLARE_BITMAP(minors, N_SPI_MINORS); /* Bit masks for spi_device.mode management. Note that incorrect * settings for some settings can cause *lots* of trouble for other * devices on a shared bus: * * - CS_HIGH ... this device will be active when it shouldn't be * - 3WIRE ... when active, it won't behave as it should * - NO_CS ... there will be no explicit message boundaries; this * is completely incompatible with the shared bus model * - READY ... transfers may proceed when they shouldn't. * * REVISIT should changing those flags be privileged? */ #define SPI_MODE_MASK (SPI_CPHA | SPI_CPOL | SPI_CS_HIGH \ | SPI_LSB_FIRST | SPI_3WIRE | SPI_LOOP \ | SPI_NO_CS | SPI_READY | SPI_TX_DUAL \ | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD) #define DMAC_CNLL_MAX_NUM 32 #define DMAC_ONE_TR_NUM 1000 //一次spi从fpga读取的大小 #define DMAC_MAX_TRF_SIZE DMAC_CNLL_MAX_NUM*DMAC_ONE_TR_NUM #define DMA_RECV_MAX_NUM 10//环形缓冲器大小 #define VER 0x20000113 #define NEXT_BUF(x) (((x+1) < DMA_RECV_MAX_NUM) ? (x+1) : 0) struct mem_addr{ char* v_adr; dma_addr_t p_adr; };//内存物理地址和虚拟地址 //环形缓冲器,分块保存每个dma头物理地址 struct buffer_cut{ unsigned int recv_num; unsigned int last_buf_len;//最后一块内存的长度 dma_addr_t pr_buf_fpga[DMA_RECV_MAX_NUM][DMAC_CNLL_MAX_NUM]; }; struct spidev_data { dev_t devt; spinlock_t spi_lock; struct spi_device *spi; struct list_head device_entry; /* TX/RX buffers are NULL unless this device is open (users > 0) */ struct mutex buf_lock; unsigned users; u8 *tx_buffer; u8 *rx_buffer; u32 speed_hz; /*connet app param*/ struct spi_transfer t; struct spi_message m; struct buffer_cut *buf_cut; int irq_gpio; int irq_num; //fpga中断号 struct mem_addr rxbuf; int spi_len;//spi dma 单次长度 bool b_workflag;//工作状态 bool b_printk;//调试打印状态 unsigned int send_data_len;//与fpga通讯一次交换数据的长度 struct semaphore spi_sema;//信号量 表示环形缓冲区内的可读数据量 int recv_buf_no;//接收到的数据块号码 int copy_recv_buf_no;//被复制的“接收到的数据块号码”防止被中断修改数据 int read_buf_no;//可读取的数据块号码 }; static LIST_HEAD(device_list); static DEFINE_MUTEX(device_list_lock); static void init_p_buf_cut(struct spidev_data *spidev); static unsigned bufsiz = 40960; module_param(bufsiz, uint, S_IRUGO); MODULE_PARM_DESC(bufsiz, "data bytes in biggest supported SPI message"); /*-------------------------------------------------------------------------*/ /*add shiguojie */ static inline ssize_t spidev_sync_write(struct spidev_data *spidev, size_t len); static ssize_t spidev_sync(struct spidev_data *spidev) { int status; struct spi_device *spi; spin_lock_irq(&spidev->spi_lock); spi = spidev->spi; spin_unlock_irq(&spidev->spi_lock); if (spi == NULL) status = -ESHUTDOWN; else status = spi_sync(spi, &spidev->m); printk("fsfpgain: spidev_sync re-%d\n",status); if (status == 0) status = spidev->m.actual_length; printk("fsfpgain: spidev_sync %d-%d-%d\n",spidev->t.speed_hz,spidev->t.len,status); return status; } static inline ssize_t spidev_sync_read(struct spidev_data *spidev, size_t len) { struct spi_transfer t = { .rx_buf = spidev->rx_buffer, .len = len, .speed_hz = spidev->speed_hz, }; spi_message_init(&spidev->m); spi_message_add_tail(&t, &spidev->m); return spidev_sync(spidev); } static void spi_fpga_recv(struct spidev_data* spidev) { int i = 0; int status = -1; struct buffer_cut *_p = spidev->buf_cut; int _no = spidev->recv_buf_no; //gpio_set_value(fsfpgaindev.cs_gpio, 0); printk("fafpgain:spi_fpga_recv %d-%d",_p->recv_num,spidev->buf_cut->last_buf_len); mutex_lock(&spidev->buf_lock); for (i = 0; i < _p->recv_num; i++) { spidev_sync_write(spidev,DMAC_ONE_TR_NUM); memcpy((void*)spidev->buf_cut->pr_buf_fpga[_no][i],spidev->rx_buffer,DMAC_ONE_TR_NUM); } if(spidev->buf_cut->last_buf_len) { spidev_sync_read(spidev,spidev->buf_cut->last_buf_len); memcpy((void*)spidev->buf_cut->pr_buf_fpga[_no][i],spidev->rx_buffer,spidev->buf_cut->last_buf_len); } mutex_unlock(&spidev->buf_lock); } static irqreturn_t irq_gpio_spi_handler(int irq, void *dev_id) { //printk("irq_gpio_spi_handler run\r\n"); return IRQ_WAKE_THREAD; } static irqreturn_t irq_gpio_spi_thread_func(int irq, void *data) { struct spidev_data *spidev = (struct spidev_data*)data; if(spidev->b_workflag) { printk("fsfpgain:irq_gpio rec buf[%d],add:%x\r\n", spidev->recv_buf_no,spidev->buf_cut->pr_buf_fpga[spidev->recv_buf_no][0]); spi_fpga_recv(spidev);//读取一次 //fsfpgaindev.read_buf_no = fsfpgaindev.recv_buf_no; spidev->recv_buf_no = NEXT_BUF(spidev->recv_buf_no);//取出下个环形缓冲器编号 up(&spidev->spi_sema); spidev->b_workflag = 0;//test } return IRQ_HANDLED; } void setSpidev_data(struct spidev_data *spidev) { int ret = 0; spidev->speed_hz = 10000000; spidev->t.speed_hz = spidev->speed_hz; spidev->b_workflag = 0;//初始化工作状态为不工作 spidev->send_data_len = 0;//初始化spi收发数据长度为0 spidev->recv_buf_no = 0; spidev->read_buf_no = 0; spidev->copy_recv_buf_no = 0; spidev->spi->mode = SPI_MODE_1; ret = spi_setup(spidev->spi); printk("fsfpgain:spi_setup re:%d\n",ret); //set map spidev->buf_cut = kzalloc(sizeof(struct buffer_cut), GFP_KERNEL); printk("fsfpgain:rxvadr value:0x%lx padr:0x%x\r\n",(long)spidev->rxbuf.v_adr,spidev->rxbuf.p_adr); printk("fsfpgain:rxvadr addr:0x%lx padr:0x%x\r\n",(long)&spidev->rxbuf.v_adr,&spidev->rxbuf.p_adr); if((dma_set_coherent_mask(&spidev->spi->dev,DMA_BIT_MASK(32))) != 0) { dev_err(&spidev->spi->dev,"Failed to set 32-bit DMA mask\n"); return -ENODEV; } spidev->rxbuf.v_adr = dma_alloc_coherent(&spidev->spi->dev,DMAC_MAX_TRF_SIZE*DMA_RECV_MAX_NUM,&spidev->rxbuf.p_adr,GFP_KERNEL|GFP_DMA); //set irq struct device_node *nd = of_get_parent(spidev->spi->dev.of_node); struct device_node *child; if(NULL == nd){ printk("fsfpgain:nd get error\r\n"); }else { printk("fsfpgain:Child scan\r\n"); for_each_child_of_node(nd, child) { printk("Child node name: %s\r\n", of_node_full_name(child)); break; } } spidev->irq_gpio = of_get_named_gpio(child, "irq-gpio", 0); if (spidev->irq_gpio < 0) { printk("fsfpgaindev.irq_gpio get error\r\n"); } ret = gpio_request(spidev->irq_gpio, "irq_gpio"); if(0 == ret) printk("fsfpgain:gpio request irq gpio ok\r\n"); else printk("fsfpgain:gpio request ret:%d\r\n",ret); gpio_direction_input(spidev->irq_gpio); spidev->irq_num = gpio_to_irq(spidev->irq_gpio); ret = request_threaded_irq(spidev->irq_num, irq_gpio_spi_handler,irq_gpio_spi_thread_func,IRQF_TRIGGER_RISING, "irq_gpio", spidev); if(0 > ret) { printk("request_irq error\r\n"); } else { printk("fsfpgain irq_num:%d\n",spidev->irq_num); } return; } void resetSpidev_data(struct spidev_data *spidev) { free_irq(spidev->irq_num, spidev); gpio_direction_output(spidev->irq_gpio, 0); gpio_free(spidev->irq_gpio); printk("fsfpgain:gpio_free\r\n"); kfree(spidev->buf_cut); dma_free_coherent(&spidev->spi->dev,DMAC_MAX_TRF_SIZE*DMA_RECV_MAX_NUM,spidev->rxbuf.v_adr, spidev->rxbuf.p_adr); } /*add shiguojie end*/ static inline ssize_t spidev_sync_write(struct spidev_data *spidev, size_t len) { struct spi_transfer t = { .tx_buf = spidev->tx_buffer, .rx_buf = spidev->rx_buffer,//add by shiguojie .len = len, .speed_hz = spidev->speed_hz, }; printk("fsfpgain:transfer len-%d speed-%d\r\n",len,spidev->speed_hz); spi_message_init(&spidev->m); spi_message_add_tail(&t, &spidev->m); return spidev_sync(spidev); } /*-------------------------------------------------------------------------*/ /* Read-only message with current device setup */ static ssize_t spidev_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos) { struct spidev_data *spidev; ssize_t status = 0; /* chipselect only toggles at start or end of operation */ if (count > bufsiz) return -EMSGSIZE; spidev = filp->private_data; mutex_lock(&spidev->buf_lock); status = spidev_sync_read(spidev, count); if (status > 0) { unsigned long missing; missing = copy_to_user(buf, spidev->rx_buffer, status); if (missing == status) status = -EFAULT; else status = status - missing; } mutex_unlock(&spidev->buf_lock); return status; } /* Write-only message with current device setup */ static ssize_t spidev_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_pos) { int x = 0; struct spidev_data *spidev; ssize_t status = 0; unsigned long missing; /* chipselect only toggles at start or end of operation */ if (count > bufsiz) return -EMSGSIZE; spidev = filp->private_data; mutex_lock(&spidev->buf_lock); char ttempBuf[1000]; char rtempBuf[1000]; spidev->tx_buffer = ttempBuf; spidev->rx_buffer = rtempBuf; missing = copy_from_user(spidev->tx_buffer, buf, count); if (missing == 0) { spidev_sync_write(spidev, count); } else status = -EFAULT; mutex_unlock(&spidev->buf_lock); printk("fsfpgain:write read:%s\n",spidev->rx_buffer); return status; } static int spidev_message(struct spidev_data *spidev, struct spi_ioc_transfer *u_xfers, unsigned n_xfers) { struct spi_transfer *k_xfers; struct spi_transfer *k_tmp; struct spi_ioc_transfer *u_tmp; unsigned n, total, tx_total, rx_total; u8 *tx_buf, *rx_buf; int status = -EFAULT; spi_message_init(&spidev->m); k_xfers = kcalloc(n_xfers, sizeof(*k_tmp), GFP_KERNEL); if (k_xfers == NULL) return -ENOMEM; /* Construct spi_message, copying any tx data to bounce buffer. * We walk the array of user-provided transfers, using each one * to initialize a kernel version of the same transfer. */ tx_buf = spidev->tx_buffer; rx_buf = spidev->rx_buffer; total = 0; tx_total = 0; rx_total = 0; for (n = n_xfers, k_tmp = k_xfers, u_tmp = u_xfers; n; n--, k_tmp++, u_tmp++) { /* Ensure that also following allocations from rx_buf/tx_buf will meet * DMA alignment requirements. */ unsigned int len_aligned = ALIGN(u_tmp->len, ARCH_KMALLOC_MINALIGN); k_tmp->len = u_tmp->len; total += k_tmp->len; /* Since the function returns the total length of transfers * on success, restrict the total to positive int values to * avoid the return value looking like an error. Also check * each transfer length to avoid arithmetic overflow. */ if (total > INT_MAX || k_tmp->len > INT_MAX) { status = -EMSGSIZE; goto done; } if (u_tmp->rx_buf) { /* this transfer needs space in RX bounce buffer */ rx_total += len_aligned; if (rx_total > bufsiz) { status = -EMSGSIZE; goto done; } k_tmp->rx_buf = rx_buf; rx_buf += len_aligned; } if (u_tmp->tx_buf) { /* this transfer needs space in TX bounce buffer */ tx_total += len_aligned; if (tx_total > bufsiz) { status = -EMSGSIZE; goto done; } k_tmp->tx_buf = tx_buf; if (copy_from_user(tx_buf, (const u8 __user *) (uintptr_t) u_tmp->tx_buf, u_tmp->len)) goto done; tx_buf += len_aligned; } k_tmp->cs_change = !!u_tmp->cs_change; k_tmp->tx_nbits = u_tmp->tx_nbits; k_tmp->rx_nbits = u_tmp->rx_nbits; k_tmp->bits_per_word = u_tmp->bits_per_word; k_tmp->delay_usecs = u_tmp->delay_usecs; k_tmp->speed_hz = u_tmp->speed_hz; k_tmp->word_delay_usecs = u_tmp->word_delay_usecs; if (!k_tmp->speed_hz) ;//k_tmp->speed_hz = spidev->speed_hz; #ifdef VERBOSE dev_dbg(&spidev->spi->dev, " xfer len %u %s%s%s%dbits %u usec %u usec %uHz\n", u_tmp->len, u_tmp->rx_buf ? "rx " : "", u_tmp->tx_buf ? "tx " : "", u_tmp->cs_change ? "cs " : "", u_tmp->bits_per_word ? : spidev->spi->bits_per_word, u_tmp->delay_usecs, u_tmp->word_delay_usecs, u_tmp->speed_hz ? : spidev->spi->max_speed_hz); #endif spi_message_add_tail(k_tmp, &spidev->m); } status = spidev_sync(spidev); if (status < 0) goto done; /* copy any rx data out of bounce buffer */ for (n = n_xfers, k_tmp = k_xfers, u_tmp = u_xfers; n; n--, k_tmp++, u_tmp++) { if (u_tmp->rx_buf) { if (copy_to_user((u8 __user *) (uintptr_t) u_tmp->rx_buf, k_tmp->rx_buf, u_tmp->len)) { status = -EFAULT; goto done; } } } status = total; done: kfree(k_xfers); return status; } static struct spi_ioc_transfer * spidev_get_ioc_message(unsigned int cmd, struct spi_ioc_transfer __user *u_ioc, unsigned *n_ioc) { u32 tmp; /* Check type, command number and direction */ if (_IOC_TYPE(cmd) != SPI_IOC_MAGIC || _IOC_NR(cmd) != _IOC_NR(SPI_IOC_MESSAGE(0)) || _IOC_DIR(cmd) != _IOC_WRITE) return ERR_PTR(-ENOTTY); tmp = _IOC_SIZE(cmd); if ((tmp % sizeof(struct spi_ioc_transfer)) != 0) return ERR_PTR(-EINVAL); *n_ioc = tmp / sizeof(struct spi_ioc_transfer); if (*n_ioc == 0) return NULL; /* copy into scratch area */ return memdup_user(u_ioc, tmp); } static long spidev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) { struct spidev_data *spidev; struct spi_device *spi; u32 tmp; unsigned n_ioc; struct spi_ioc_transfer *ioc; int ret = 0; int result = 0;//信号量就绪 unsigned long _info = 0;//arg change kernel value printk("fsfpgain:spidev_ioctl is run:0x%x-0x%x\r\n",cmd,arg); /* Check type and command number */ // if (_IOC_TYPE(cmd) != SPI_IOC_MAGIC) // { // printk("spi is SPI_IOC_MAGIC\r\n"); // return -ENOTTY; // } /* guard against device removal before, or while, * we issue this ioctl. */ spidev = filp->private_data; printk("fsfpgain:spidev addr0x%x-%x;spi addr0x%x-%x\r\n",*spidev,spidev,*spidev->spi,spidev->spi); spin_lock_irq(&spidev->spi_lock); spi = spi_dev_get(spidev->spi); spin_unlock_irq(&spidev->spi_lock); if (spi == NULL) { printk("spi is null\r\n"); return -ESHUTDOWN; } /* use the buffer lock here for triple duty: * - prevent I/O (from us) so calling spi_setup() is safe; * - prevent concurrent SPI_IOC_WR_* from morphing * data fields while SPI_IOC_RD_* reads them; * - SPI_IOC_MESSAGE needs the buffer locked "normally". */ mutex_lock(&spidev->buf_lock); switch (cmd) { case 0x100: ret = 0x20000113; break; case 0x309: spidev->b_workflag = 0; break; case 0x307: ret = DMAC_MAX_TRF_SIZE*DMA_RECV_MAX_NUM; break; case 0x306: ret = DMAC_MAX_TRF_SIZE; break; case 0x305: ret = spidev->send_data_len; break; case 0x304: ret = copy_from_user(&_info, &arg, sizeof(unsigned long)); if (_info) spidev->b_printk = 1; else spidev->b_printk = 0; break; case 0x303://开始工作由APP启动工作 spidev->read_buf_no = spidev->recv_buf_no;//重置可读数据块编号为最新接收到的数据块编号? sema_init(&spidev->spi_sema,0);//初始化信号量 spidev->b_workflag = 1;//开始工作标志 printk("fafpgain start work!\r\n"); ret = 1; break; case 0x302://设置spi传输数据大小并返回大小 ret = copy_from_user(&_info, &arg, sizeof(unsigned long)); #ifdef _DEBUG printk("cmd 0x302 set len:%ld-%ld return :%d\r\n",_info,arg,ret); #endif spidev->send_data_len = arg; ret = spidev->send_data_len; if(DMAC_MAX_TRF_SIZE < arg) {//判断设置的一次接收fpga长度是否超出预设内存 ret = -1; printk("iotcl cmd:%d arg:%ld error\r\n",cmd,_info); break; } init_p_buf_cut(spidev); break; case 0x301://应用程序获取一次buf,在此判断是否有buf #ifdef _DEBUG printk("test jiffies:%ld\r\n", jiffies); #endif result = down_timeout(&spidev->spi_sema,jiffies+msecs_to_jiffies(60));//获取信号 #ifdef _DEBUG printk("down_temeout result:%d\r\n", result); #endif spidev->copy_recv_buf_no = spidev->recv_buf_no;//copy一份当前值 #ifdef _DEBUG printk("read_no:%d-copy_recv_no:%d\r\n",spidev->read_buf_no,spidev->copy_recv_buf_no); #endif if((spidev->read_buf_no != spidev->copy_recv_buf_no) && (0 == result)) {//right if ((spidev->read_buf_no + 1) % DMA_RECV_MAX_NUM == spidev->copy_recv_buf_no) { ret = (spidev->read_buf_no | 0x10000);//不明白什么意思 }else { ret = spidev->read_buf_no; } spidev->read_buf_no = NEXT_BUF(spidev->read_buf_no); } else if (0 != result) { ret = -2; } else if (spidev->read_buf_no == spidev->copy_recv_buf_no) { ret = -3; }else { ret = -4; } #ifdef _DEBUG printk("0x301 ret:%d\r\n",ret); #endif break; } mutex_unlock(&spidev->buf_lock); //#ifdef _DEBUGWZG printk("2121wzg cmd 0x%x arg:%d ret:%d DMAC_MAX_TRF_SIZE:%d \r\n",cmd,arg,ret,DMAC_MAX_TRF_SIZE); //#endif spi_dev_put(spi); return ret; } #ifdef CONFIG_COMPAT static long spidev_compat_ioc_message(struct file *filp, unsigned int cmd, unsigned long arg) { struct spi_ioc_transfer __user *u_ioc; int retval = 0; struct spidev_data *spidev; struct spi_device *spi; unsigned n_ioc, n; struct spi_ioc_transfer *ioc; u_ioc = (struct spi_ioc_transfer __user *) compat_ptr(arg); /* guard against device removal before, or while, * we issue this ioctl. */ printk("fsfpgain:spidev_compat_ioc_message\r\n"); spidev = filp->private_data; spin_lock_irq(&spidev->spi_lock); spi = spi_dev_get(spidev->spi); spin_unlock_irq(&spidev->spi_lock); if (spi == NULL) return -ESHUTDOWN; /* SPI_IOC_MESSAGE needs the buffer locked "normally" */ mutex_lock(&spidev->buf_lock); /* Check message and copy into scratch area */ ioc = spidev_get_ioc_message(cmd, u_ioc, &n_ioc); if (IS_ERR(ioc)) { retval = PTR_ERR(ioc); goto done; } if (!ioc) goto done; /* n_ioc is also 0 */ /* Convert buffer pointers */ for (n = 0; n < n_ioc; n++) { ioc[n].rx_buf = (uintptr_t) compat_ptr(ioc[n].rx_buf); ioc[n].tx_buf = (uintptr_t) compat_ptr(ioc[n].tx_buf); } /* translate to spi_message, execute */ retval = spidev_message(spidev, ioc, n_ioc); kfree(ioc); done: mutex_unlock(&spidev->buf_lock); spi_dev_put(spi); return retval; } static long spidev_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) { printk("fsfpgain:spidev_compat_ioctl\r\n"); if (_IOC_TYPE(cmd) == SPI_IOC_MAGIC && _IOC_NR(cmd) == _IOC_NR(SPI_IOC_MESSAGE(0)) && _IOC_DIR(cmd) == _IOC_WRITE) return spidev_compat_ioc_message(filp, cmd, arg); return spidev_ioctl(filp, cmd, (unsigned long)compat_ptr(arg)); } #else #define spidev_compat_ioctl NULL #endif /* CONFIG_COMPAT */ static int spidev_open(struct inode *inode, struct file *filp) { struct spidev_data *spidev; int status = -ENXIO; mutex_lock(&device_list_lock); list_for_each_entry(spidev, &device_list, device_entry) { if (spidev->devt == inode->i_rdev) { status = 0; break; } } if (status) { pr_debug("spidev: nothing for minor %d\n", iminor(inode)); goto err_find_dev; } spidev->users++; filp->private_data = spidev; stream_open(inode, filp); mutex_unlock(&device_list_lock); return 0; err_alloc_rx_buf: kfree(spidev->tx_buffer); spidev->tx_buffer = NULL; err_find_dev: mutex_unlock(&device_list_lock); return status; } static int spidev_release(struct inode *inode, struct file *filp) { struct spidev_data *spidev; int dofree; mutex_lock(&device_list_lock); spidev = filp->private_data; filp->private_data = NULL; spin_lock_irq(&spidev->spi_lock); /* ... after we unbound from the underlying device? */ dofree = (spidev->spi == NULL); spin_unlock_irq(&spidev->spi_lock); /* last close? */ spidev->users--; if (!spidev->users) { //kfree(spidev->tx_buffer); spidev->tx_buffer = NULL; //kfree(spidev->rx_buffer); spidev->rx_buffer = NULL; if (dofree) kfree(spidev); else ;//spidev->speed_hz = spidev->spi->max_speed_hz; } #ifdef CONFIG_SPI_SLAVE if (!dofree) spi_slave_abort(spidev->spi); #endif mutex_unlock(&device_list_lock); return 0; } static void init_p_buf_cut(struct spidev_data *spidev) { int i = 0; int j = 0; spidev->buf_cut->recv_num = spidev->send_data_len / DMAC_ONE_TR_NUM; spidev->buf_cut->last_buf_len = spidev->send_data_len % DMAC_ONE_TR_NUM; printk("buf_cut recv_num:%d last_buf_len:%d\r\n",spidev->buf_cut->recv_num,spidev->buf_cut->last_buf_len); for (i = 0; i < DMA_RECV_MAX_NUM; i++) { for (j = 0; j < DMAC_CNLL_MAX_NUM; j++) { spidev->buf_cut->pr_buf_fpga[i][j] = spidev->rxbuf.p_adr + (i*DMAC_MAX_TRF_SIZE) + (j*DMAC_ONE_TR_NUM); } printk("p_rx buf[%d] dma[%3d] padd:0x%x\r\n", i,j,spidev->buf_cut->pr_buf_fpga[i][0]); } } static int fsfpgain_mmap(struct file *filp, struct vm_area_struct *vma) { int ret = 0; struct spidev_data *spidev; spidev = filp->private_data; printk("fsfpgain:spidev addr0x%x-%x;spi addr0x%x-%x\r\n",*spidev,spidev,*spidev->spi,spidev->spi); #ifdef _DEBUG printk("fsfpgain_mmap run,PAGE_SIZE:%ld\r\n",PAGE_SIZE); printk("vm_start:%lx vm_end:%lx\r\n", vma->vm_start,vma->vm_end); #endif //dma_mmap_attrs(struct device *dev, struct vm_area_struct *vma, void *cpu_addr, // dma_addr_t dma_addr, size_t size, unsigned long attrs) ret = dma_mmap_coherent(&spidev->spi->dev, vma, (void*)spidev->rxbuf.v_adr, spidev->rxbuf.p_adr,DMAC_MAX_TRF_SIZE*DMA_RECV_MAX_NUM); memset(spidev->rxbuf.v_adr, 0,DMAC_MAX_TRF_SIZE*DMA_RECV_MAX_NUM); #ifdef _DEBUG memcpy(spidev->rxbuf.v_adr, "Hello,mmap!", sizeof("Hello,mmap!"));//test mmap printk("fsfpgain_mmap re:%d-%s\r\n",ret,spidev->rxbuf.v_adr); #endif memset(spidev->rxbuf.v_adr, 0,DMAC_MAX_TRF_SIZE*DMA_RECV_MAX_NUM); return ret; } static const struct file_operations spidev_fops = { .owner = THIS_MODULE, /* REVISIT switch to aio primitives, so that userspace * gets more complete API coverage. It'll simplify things * too, except for the locking. */ .write = spidev_write, .read = spidev_read, .unlocked_ioctl = spidev_ioctl, .compat_ioctl = spidev_compat_ioctl, .open = spidev_open, .release = spidev_release, .llseek = no_llseek, .mmap = fsfpgain_mmap, }; /*-------------------------------------------------------------------------*/ /* The main reason to have this class is to make mdev/udev create the * /dev/spidevB.C character device nodes exposing our userspace API. * It also simplifies memory management. */ static struct class *spidev_class; #ifdef CONFIG_OF static const struct of_device_id spidev_dt_ids[] = { { .compatible = "fsml,fsfpgain" }, {}, }; MODULE_DEVICE_TABLE(of, spidev_dt_ids); #endif #ifdef CONFIG_ACPI /* Dummy SPI devices not to be used in production systems */ #define SPIDEV_ACPI_DUMMY 1 static const struct acpi_device_id spidev_acpi_ids[] = { /* * The ACPI SPT000* devices are only meant for development and * testing. Systems used in production should have a proper ACPI * description of the connected peripheral and they should also use * a proper driver instead of poking directly to the SPI bus. */ { "SPT0001", SPIDEV_ACPI_DUMMY }, { "SPT0002", SPIDEV_ACPI_DUMMY }, { "SPT0003", SPIDEV_ACPI_DUMMY }, {}, }; MODULE_DEVICE_TABLE(acpi, spidev_acpi_ids); static void spidev_probe_acpi(struct spi_device *spi) { const struct acpi_device_id *id; if (!has_acpi_companion(&spi->dev)) return; id = acpi_match_device(spidev_acpi_ids, &spi->dev); if (WARN_ON(!id)) return; if (id->driver_data == SPIDEV_ACPI_DUMMY) dev_warn(&spi->dev, "do not use this driver in production systems!\n"); } #else static inline void spidev_probe_acpi(struct spi_device *spi) {} #endif /*-------------------------------------------------------------------------*/ static int spidev_probe(struct spi_device *spi) { struct spidev_data *spidev; int status; unsigned long minor; /* * spidev should never be referenced in DT without a specific * compatible string, it is a Linux implementation thing * rather than a description of the hardware. */ WARN(spi->dev.of_node && of_device_is_compatible(spi->dev.of_node, "fsfpgain"), "%pOF: buggy DT: spidev listed directly in DT\n", spi->dev.of_node); spidev_probe_acpi(spi); /* Allocate driver data */ spidev = kzalloc(sizeof(*spidev), GFP_KERNEL); if (!spidev) return -ENOMEM; /* Initialize the driver data */ spidev->spi = spi; printk("fsfpgain:spidev addr0x%x-%x;spi addr0x%x-%x\r\n",*spidev,spidev,*spi,spi); spin_lock_init(&spidev->spi_lock); mutex_init(&spidev->buf_lock); //sema_init(&spidev->spi_sema,0);//初始化信号量 INIT_LIST_HEAD(&spidev->device_entry); /* If we can allocate a minor number, hook up this device. * Reusing minors is fine so long as udev or mdev is working. */ mutex_lock(&device_list_lock); minor = find_first_zero_bit(minors, N_SPI_MINORS); if (minor < N_SPI_MINORS) { struct device *dev; spidev->devt = MKDEV(SPIDEV_MAJOR, minor); dev = device_create(spidev_class, &spi->dev, spidev->devt, spidev, "fsfpgain"); status = PTR_ERR_OR_ZERO(dev); } else { dev_dbg(&spi->dev, "no minor number available!\n"); status = -ENODEV; } if (status == 0) { set_bit(minor, minors); list_add(&spidev->device_entry, &device_list); } mutex_unlock(&device_list_lock); //spidev->speed_hz = spi->max_speed_hz; setSpidev_data(spidev); if (status == 0) spi_set_drvdata(spi, spidev);//spidev private reg else kfree(spidev); return status; } static int spidev_remove(struct spi_device *spi) { struct spidev_data *spidev = spi_get_drvdata(spi); /* prevent new opens */ mutex_lock(&device_list_lock); /* make sure ops on existing fds can abort cleanly */ spin_lock_irq(&spidev->spi_lock); spidev->spi = NULL; spin_unlock_irq(&spidev->spi_lock); list_del(&spidev->device_entry); device_destroy(spidev_class, spidev->devt); clear_bit(MINOR(spidev->devt), minors); resetSpidev_data(spidev); if (spidev->users == 0) kfree(spidev); mutex_unlock(&device_list_lock); return 0; } static struct spi_driver spidev_spi_driver = { .driver = { .name = "fsfpgain", .of_match_table = of_match_ptr(spidev_dt_ids), .acpi_match_table = ACPI_PTR(spidev_acpi_ids), }, .probe = spidev_probe, .remove = spidev_remove, /* NOTE: suspend/resume methods are not necessary here. * We don't do anything except pass the requests to/from * the underlying controller. The refrigerator handles * most issues; the controller driver handles the rest. */ }; /*-------------------------------------------------------------------------*/ static int __init spidev_init(void) { int status; /* Claim our 256 reserved device numbers. Then register a class * that will key udev/mdev to add/remove /dev nodes. Last, register * the driver which manages those device numbers. */ BUILD_BUG_ON(N_SPI_MINORS > 256); status = register_chrdev(SPIDEV_MAJOR, "spi", &spidev_fops); if (status < 0) return status; spidev_class = class_create(THIS_MODULE, "fsfpgain"); if (IS_ERR(spidev_class)) { unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name); return PTR_ERR(spidev_class); } status = spi_register_driver(&spidev_spi_driver); if (status < 0) { class_destroy(spidev_class); unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name); } return status; } module_init(spidev_init); static void __exit spidev_exit(void) { spi_unregister_driver(&spidev_spi_driver); class_destroy(spidev_class); unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name); } module_exit(spidev_exit); MODULE_AUTHOR("shi guojie"); MODULE_DESCRIPTION("fsml"); MODULE_LICENSE("GPL"); MODULE_ALIAS("v1.0.0.0");
test_spidev.c
#include <stdio.h> #include <ctype.h> #include <sys/ioctl.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #define DEVICE_NAME "/dev/fsfpgain" #define HEAD_LEN 5 #define PKT_MAX_LEN 0x40 #define STATUS_LEN 0x01 #define SUNXI_OP_WRITE 0x01 #define SUNXI_OP_READ 0x03 #define STATUS_WRITABLE 0x02 #define STATUS_READABLE 0x04 #define WRITE_DELAY 200 #define READ_DELAY 100000 void dump_data(unsigned char *buf, unsigned int len) { unsigned int i; unsigned char tmp[len*2], cnt = 0; for (i = 0; i < len; i++) { if (i%0x10== 0) cnt += sprintf(tmp + cnt, "0x%08x: ", i); cnt += sprintf(tmp + cnt, "%02x ", buf[i]); if ( (i%0x10== 0x0f) || (i == (len -1)) ) { printf("%s\n", tmp); cnt = 0; } } } void batch_rand(char *buf, unsigned int length) { unsigned int i; srand(time(0)); for(i = 0; i < length; i++) { *(buf + i) = rand() % 256; } } int main(int argc, const char *argv[]) { unsigned int length = 0, test_len; char wbuf_head[HEAD_LEN] = {SUNXI_OP_WRITE, 0x0b, 0xb0, 0xea, 0x10}; char rbuf_head[HEAD_LEN] = {SUNXI_OP_READ, 0x00, 0x00, 0xc0, 0x00}; char wbuf[PKT_MAX_LEN], rbuf[PKT_MAX_LEN], i, time; int fd, ret; test_len = 10;//send 10 numbers if (test_len > PKT_MAX_LEN) { printf("invalid argument, numbers must less 64B\n"); return -1; } wbuf_head[4] = test_len; rbuf_head[4] = test_len; for (i = 0; i < test_len; i++) wbuf[i] = i; printf("wbuf:\n"); dump_data(wbuf, test_len); fd = open(DEVICE_NAME, O_RDWR); if (fd <= 0) { printf("Fail to to open %s\n", DEVICE_NAME); ret = -1; return ret; } char str[100]; memset(str,0,100); int size = 1000; char* buffer = (char*)malloc(size * sizeof(char)); char ch = 'a'; for (int i = 0; i < size; i++) { buffer[i] = ch; ch++; if (ch > 'z') { ch = 'a'; } } sprintf(str,"abcdefghijklmnabcdefghijklmnabcdefghijklmnabcdefghijklmnabcdefghijklmnabcdefghijklmnabcdefghijklmn"); //for(int x=0;x<10;x++) for(int x=0;x<10;x++) { write(fd, buffer, size); } if(0) {//write if (write(fd, wbuf_head, HEAD_LEN) != HEAD_LEN) { printf("W Fail to write head\n"); ret = -1; goto err; } else printf("W write head successful\n"); usleep(WRITE_DELAY); if (write(fd, wbuf, test_len) != test_len) { printf("W Fail to write data\n"); ret = -1; goto err; } else printf("W write data successful\n"); usleep(READ_DELAY); } err: if (fd > 0) close(fd); return ret; }
test_fsfpgain.c
#include <stdio.h> #include <ctype.h> #include <sys/ioctl.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <sys/mman.h> #include <string.h> #define DMAC_CNLL_MAX_NUM 32 #define DMAC_ONE_TR_NUM 1000 #define DMAC_MAX_TRF_SIZE DMAC_CNLL_MAX_NUM*DMAC_ONE_TR_NUM #define DMA_RECV_MAX_NUM 10//环形缓冲器大小 int main() { int ret,fpdata,fpio,i,shmm_len,one_trf_len; long value; void* shmm_buf = NULL; char* trf_buf[DMAC_CNLL_MAX_NUM]; char rbuf[1]; char cstr[100] = "hello!spi world!"; fpdata = open("/dev/fsfpgain", O_RDWR); if(0 > fpdata) { printf("open error!\r\n"); return -1; } ret = ioctl(fpdata, 0x100, value); printf("100fsfpga version = 0x%x\r\n", ret); sleep(1); value = 10240; ret = ioctl(fpdata, 0x302, value); printf("302fsfpga set send-data-len = %d\r\n", ret); sleep(1); ret = ioctl(fpdata, 0x305, value); printf("0x305fsfpga get send-data-len = %d\r\n", ret); sleep(1); shmm_len = ioctl(fpdata, 0x307, value); printf("0x307shmm len = %d\r\n",shmm_len); sleep(1); one_trf_len = ioctl(fpdata, 0x306, value); printf("0x306one dmaspi len = %d\r\n",one_trf_len); sleep(1); printf("&shmm_buf:0x%p shmm_buf:0x%p shmm_len:%d\r\n",&shmm_buf, shmm_buf,shmm_len); shmm_buf = mmap(NULL, shmm_len, PROT_READ|PROT_WRITE,MAP_SHARED, fpdata,0); if(MAP_FAILED == shmm_buf) { printf("mmap error!\r\n"); return 1; } printf("&shmm_buf:0x%p shmm_buf:0x%p shmm_len:%d\r\n",&shmm_buf, shmm_buf,shmm_len); printf("shmm_buf:%s\r\n",(char*)shmm_buf); sleep(1); ret = ioctl(fpdata, 0x303, value); printf("fsfpga start work = %d\r\n", ret); printf("sleep...\r\n"); sleep(1); for(i = 0; i < DMA_RECV_MAX_NUM; i++) { trf_buf[i] = (char*)shmm_buf + (i * one_trf_len); printf("trf_buf[%d]add:0x%lx\r\n",i,(long)trf_buf[i]); } printf("trf_buf[0]:%s\r\n", trf_buf[0]); printf("set trf_buf ok\r\n"); sleep(1); while (1) { //memset(shmm_buf, 0 ,shmm_len); ret = ioctl(fpdata, 0x301, value); printf("ioctl 0x301 ret:0x%x-v:0x%lx\r\n",ret, value); printf("get data no = %x\r\n",ret); value = 1; break; usleep(500*1000); } printf("munmap return:%d\r\n",munmap(shmm_buf, shmm_len)); close(fpdata); return 0; }
4.运行故障提示
# ./test_fafpgain [ 4610.396357] fsfpgain:spidev_ioctl is run:0x100-0xb6ee3b58 [ 4610.402515] fsfpgain:spidev addr0x9900000-650065;spi addr0xc6594000-bf002018 [ 4610.410667] 2121wzg cmd 0x100 arg:-1225901224 ret:536871187 DMAC_MAX_TRF_SIZE:32000 100fsfpga version = 0x20000113 [ 4611.420340] fsfpgain:spidev_ioctl is run:0x302-0x2800 [ 4611.426075] fsfpgain:spidev addr0x9900000-660066;spi addr0xc6594000-bf002018 [ 4611.434057] buf_cut recv_num:10 last_buf_len:240 [ 4611.439322] p_rx buf[0] dma[ 32] padd:0x47880000 [ 4611.444561] p_rx buf[1] dma[ 32] padd:0x47887d00 [ 4611.449983] p_rx buf[2] dma[ 32] padd:0x4788fa00 [ 4611.455215] p_rx buf[3] dma[ 32] padd:0x47897700 [ 4611.460490] p_rx buf[4] dma[ 32] padd:0x4789f400 [ 4611.465722] p_rx buf[5] dma[ 32] padd:0x478a7100 [ 4611.470986] p_rx buf[6] dma[ 32] padd:0x478aee00 [ 4611.476223] p_rx buf[7] dma[ 32] padd:0x478b6b00 [ 4611.481478] p_rx buf[8] dma[ 32] padd:0x478be800 [ 4611.486730] p_rx buf[9] dma[ 32] padd:0x478c6500 [ 4611.491998] 2121wzg cmd 0x302 arg:10240 ret:10240 DMAC_MAX_TRF_SIZE:32000 302fsfpga set send-data-len = 10240 [ 4612.500269] fsfpgain:spidev_ioctl is run:0x305-0x2800 [ 4612.506000] fsfpgain:spidev addr0x9900000-670067;spi addr0xc6594000-bf002018 [ 4612.513984] 2121wzg cmd 0x305 arg:10240 ret:10240 DMAC_MAX_TRF_SIZE:32000 0x305fsfpga get send-data-len = 10240 [ 4613.522220] fsfpgain:spidev_ioctl is run:0x307-0x2800 [ 4613.528134] fsfpgain:spidev addr0x9900000-680068;spi addr0xc6594000-bf002018 [ 4613.536077] 2121wzg cmd 0x307 arg:10240 ret:320000 DMAC_MAX_TRF_SIZE:32000 0x307shmm len = 320000 [ 4614.544450] fsfpgain:spidev_ioctl is run:0x306-0x2800 [ 4614.550210] fsfpgain:spidev addr0x9900000-690069;spi addr0xc6594000-bf002018 [ 4614.558175] 2121wzg cmd 0x306 arg:10240 ret:32000 DMAC_MAX_TRF_SIZE:32000 0x306one dmaspi len = 32000 [ 4615.566559] fsfpgain:spidev addr0x9900000-6a006a;spi addr0xc6594000-bf002018 &shmm_buf:0x0xbe84bca4 shmm_buf:0x0xb6da9000 shmm_len:320000 shmm_buf: [ 4616.579679] fsfpgain:spidev_ioctl is run:0x303-0x2800 [ 4616.585411] fsfpgain:spidev addr0x9900000-6a006a;spi addr0xc6594000-bf002018 [ 4616.593411] fafpgain start work! [ 4616.597144] 2121wzg cmd 0x303 arg:10240 ret:1 DMAC_MAX_TRF_SIZE:32000 fsfpga start work = 1 sleep... [ 4616.607572] fsfpgain:irq_gpio rec buf[0],add:47880000 [ 4616.613588] fafpgain:spi_fpga_recv 10-240 [ 4616.613593] fsfpgain:transfer len-1000 speed-10000000 [ 4616.623825] fsfpgain spi1.0: Bufferless transfer has length 1000 [ 4616.630553] fsfpgain: spidev_sync re-0 [ 4616.634722] fsfpgain: spidev_sync 10000000-0-1000 //fsfpgain spi1.0: Bufferless transfer has length 1000是spi.c中的报错,我没有理解为什么会跳到这里面来。使用test_spidev.c来测试,就不会跳进来,在驱动层面都是调用的write函数来读写的。
5.问题汇总:
1)通过GPIO中断来调用spi读写函数,spi不能正常工作,只有片选,没有时钟:
2)如何设置缓存,使得spi可以连续读取大数据量,目前片选频繁跳动,效率太低,25Hz的中断没办法完成数据读取。 -
此回复已被删除!
Copyright © 2024 深圳全志在线有限公司 粤ICP备2021084185号 粤公网安备44030502007680号