微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > s5pv210与stm32 spi通信

s5pv210与stm32 spi通信

时间:11-19 来源:互联网 点击:
) 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 shouldnt be* - 3WIRE ... when active, it wont 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 shouldnt.** 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)struct spidev_data {dev_t devt;spinlock_t spi_lock;struct spi_device *spi;struct list_head device_entry;/* buffer is NULL unless this device is open (users > 0) */struct mutex buf_lock;unsigned users;u8 *buffer;int irq;//add by daowait_queue_head_t rqueue;};static flag_poll = 0;static LIST_HEAD(device_list);static DEFINE_MUTEX(device_list_lock);static unsigned bufsiz = 4096;module_param(bufsiz, uint, S_IRUGO);MODULE_PARM_DESC(bufsiz, "data bytes in biggest supported SPI message");/*-------------------------------------------------------------------------*//** We cant use the standard synchronous wrappers for file I/O; we* need to protect against async removal of the underlying spi_device.*/static void spidev_complete(void *arg){complete(arg);}static ssize_tspidev_sync(struct spidev_data *spidev, struct spi_message *message){DECLARE_COMPLETION_ONSTACK(done);int status;message->complete = spidev_complete;message->context = &done;spin_lock_irq(&spidev->spi_lock);if (spidev->spi == NULL)status = -ESHUTDOWN;elsestatus = spi_async(spidev->spi, message);spin_unlock_irq(&spidev->spi_lock);if (status == 0) {wait_for_completion(&done);status = message->status;if (status == 0)status = message->actual_length;}return status;}static inline ssize_tspidev_sync_write(struct spidev_data *spidev, size_t len){struct spi_transfer t = {.tx_buf = spidev->buffer,.len = len,};struct spi_message m;spi_message_init(&m);spi_message_add_tail(&t, &m);return spidev_sync(spidev, &m);}static inline ssize_tspidev_sync_read(struct spidev_data *spidev, size_t len){struct spi_transfer t = {.rx_buf = spidev->buffer,.len = len,};struct spi_message m;spi_message_init(&m);spi_message_add_tail(&t, &m);return spidev_sync(spidev, &m);}/*-------------------------------------------------------------------------*//* Read-only message with current device setup */static ssize_tspidev_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->buffer, status);if (missing == status)status = -EFAULT;elsestatus = status - missing;}mutex_unlock(&spidev->buf_lock);return status;}/* Write-only message with current device setup */static ssize_tspidev_write(struct file *filp, const char __user *buf,size_t count, loff_t *f_pos){struct spidev_data *spidev;ssize_t status = 0;unsigned long missing;/* chipselect only toggles at

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top