首页 > 编程知识 正文

spi总线协议,spi传输协议

时间:2023-05-06 00:04:45 阅读:51367 作者:2062

1、linux上的spi协议框架

一般spi-core以下的spi_master已由芯片制造商开发完成。 因此,完成基于SPI协议二次开发即可,例如,基于SPI协议的网络数据的传送、SPI外围装置的控制、或者blalalala

2、码帧

2.1、生成dts兴奋保温杯、spi_master

/*兴奋的保温杯GPIO端口模式为SPI模式xsdl/SPI XXX _ pins 3360 SPI XXX _ pins { any ka,pins=15 16 25 26; anyka,function=2 2 3 3; anyka,pull=0x00000010; (; spi1 { pinctrl-names='default '; pinctrl-0=spixxx_pins; cs-gpios=gpio 10 1; status='okay '; spidev : my_XXX @0{ compatible=' test,my _ XXX '; spi-max-frequency=12000000; reg=0; (; (; 2.2、码帧

创建字符设备驱动程序,然后在字符设备驱动程序中调用spi接口以完成数据读写

# include Linux/init.h # include Linux/module.h # include Linux/ioctl.h # include Linux/fs.h # include Linux/dev iiv include Linux/slab.h # include Linux/compat.h # include Linux/of SPI/SPI.h # include Linux/SPI/spidev.hexternster staticstructmy _ XXX _ dev * g _ my _ XXX _ dev=null; intmy _ XXX _ open (结构索引* node,结构文件* filp (//结构my _ XXX _ dev * my _ XXX _ dev=container _ ) } intmy _ XXX _ release (结构索引* node,结构文件* filp )/struct my _ XXX _ dev * my _ XXX _ dev=contain ning } static ssize _ tmy _ XXX _ read (结构文件* filp,const char __user *buf,size_t count,loff_t * ppos ) { cos struct spi_message m; structmy_XXX_dev * my _ XXX _ dev=container _ of (filp-private _ data、struct my _ XXX _ dev、misc_dev ) mutex_lock(my_XXX_dev-buf_lock ); t=kzalloc(sizeof ) structSPI_transfer ),GFP_KERNEL ); if(t==null ) printk (kzallocspi _ transfer error! rn '; 返回- 1; }t-rx_buf=data; 叔t-len=10; SPI_message_init(m; SPI_message_add_tail(t,m ); ret=SPI_sync(SPI,m ); if(ret0) { printk。

spi_sync failed!rn"); kfree(t); return -1; }if (copy_to_user(buf, (void *)data, status) {printk("error: copy from usern"); return -1; }kfree(t);mutex_unlock(&my_xxx_dev->buf_lock);return 0;}static ssize_t my_xxx_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos){ int ret = 0;struct spi_transfer *t; struct spi_message m;struct my_xxx_dev *my_xxx_dev = container_of(filp->private_data, struct my_xxx_dev, misc_dev); struct spi_device *spi = (struct spi_device *)my_xxx_dev->private_data;mutex_lock(&my_xxx_dev->buf_lock); if (copy_from_user(my_xxx_dev->data, (void *)buf, count)) {printk("error: copy from usern"); return -1; } t = kzalloc(sizeof(struct spi_transfer), GFP_KERNEL); if(t == NULL) { printk("kzalloc spi_transfer error!rn"); return -1; } t->tx_buf = my_xxx_dev->data; t->len = count;t->tx_nbits= my_xxx_dev->tx_nbits;spi->mode= ((my_xxx_dev->tx_nbits) == 0x02) ? SPI_TX_DUAL : (((my_xxx_dev->tx_nbits) == 0x04) ? SPI_TX_QUAD : SPI_MODE_0); spi_message_init(&m); spi_message_add_tail(t, &m); ret = spi_sync(spi, &m); if(ret < 0) { printk("spi_sync failed!rn"); kfree(t); return -1; } kfree(t);mutex_unlock(&my_xxx_dev->buf_lock); return 0;}int my_xxx_ioctl(struct file *filp, unsigned int cmd, unsigned long int arg){unsigned long int speed_hz = 0;struct my_xxx_dev *my_xxx_dev = container_of(filp->private_data, struct my_xxx_dev, misc_dev);struct spi_device *spi = (struct spi_device *)my_xxx_dev->private_data;if(copy_from_user(&speed_hz, (void *)arg, sizeof(speed_hz))) {printk("copy mparam data errorn");return -1;}mutex_lock(&my_xxx_dev->buf_lock); switch(cmd) {case 0:spi->max_speed_hz = speed_hz;spi_setup(spi);break;default:break;}mutex_unlock(&my_xxx_dev->buf_lock); return 0;}struct file_operations my_xxx_fops = { .owner = THIS_MODULE, .open = my_xxx_open, .release = my_xxx_release,.read= my_xxx_read, .write = my_xxx_write, .unlocked_ioctl = my_xxx_ioctl,};static int my_xxx_probe(struct spi_device *spi){int ret = 0;printk("spi driver and device was matched!n");g_my_xxx_dev = kzalloc(sizeof(struct my_xxx_dev), GFP_KERNEL);if (!g_my_xxx_dev) { printk("kmalloc stepmotor dev fail.n"); return -1; }memcpy(g_my_xxx_dev, &g_my_xxx_init, sizeof(struct my_xxx_dev));g_my_xxx_dev->misc_dev.minor = MISC_DYNAMIC_MINOR; g_my_xxx_dev->misc_dev.name = my_xxx_DEV_NAME; g_my_xxx_dev->misc_dev.fops = &my_xxx_fops; ret = misc_register(&g_my_xxx_dev->misc_dev); if(unlikely(ret < 0)) { printk("register misc failn"); }mutex_init(&g_my_xxx_dev->buf_lock);spi->mode = SPI_MODE_0;spi_setup(spi);g_my_xxx_dev->private_data = spi; return ret;}static int my_xxx_remove(struct spi_device *spi){ misc_deregister(&g_my_xxx_dev->misc_dev);kfree(g_my_xxx_dev);g_my_xxx_dev = NULL; return 0;}static const struct spi_device_id my_xxx_id_table虚心的乌冬面 = {{"test,my_xxx", 0},{},};static const struct of_device_id my_xxx_of_match虚心的乌冬面 = { {.compatible = "test,my_xxx"},{},};static struct spi_driver my_xxx_driver = {.probe = my_xxx_probe,.remove = my_xxx_remove,.driver = {.name = "my_xxx",.owner = THIS_MODULE,.of_match_table = my_xxx_of_match,},.id_table = my_xxx_id_table,};int my_xxx_init(void){spi_register_driver(&my_xxx_driver); printk("%s module is installedn", __func__); return 0;}void my_xxx_exit(void){spi_unregister_driver(&my_xxx_driver); printk("%s module is removed.n", __func__); return;}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。