s5pv210与stm32 spi通信
时间:11-19
来源:互联网
点击:
register_chrdev(SPIDEV_MAJOR, "spi", &spidev_fops);if (status < 0)return status;spidev_class = class_create(THIS_MODULE, "spidev");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("Andrea Paterniani, ");MODULE_DESCRIPTION("User mode SPI device interface");MODULE_LICENSE("GPL");MODULE_ALIAS("spi:spidev");文件名:spidev_gzsd.c
这两部分源码下载地址为:点击打开链接
位置: drivers/spi
将drivers/spi/Makefile里的spidev.c修改为spidev_gzsd.c
210 spi测试程序如下:
/** SPI testing utility (using spidev driver)** Copyright (c) 2007 MontaVista Software, Inc.* Copyright (c) 2007 Anton Vorontsov ** This program is free software; you can redistribute it and/or modify* it under the terms of the GNU General Public License as published by* the Free Software Foundation; either version 2 of the License.** Cross-compile with cross-gcc -I/path/to/cross-kernel/include*/#include#include #include #include #include #include #include #include #include "spidev.h"//#include #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))#define BUF_SIZE 38#define READ_SIZE (BUF_SIZE + 1)static const char *device = "/dev/spidev0.0";static uint8_t mode;static uint8_t bits = 8;static uint32_t speed = 140000;//140000;static uint16_t delay;static void transfer(int fd){int ret;uint8_t tx[] = {0x01, 0xFD, 0xFC, 0xFB, 0xFA, 0xF9,0xF8, 0xF7, 0xF6, 0xF5, 0xF4, 0xF3,0xF2, 0xF1, 0xF0, 0xEF, 0xEE, 0xED,0xEC, 0xEB, 0xEA, 0xE9, 0xE8, 0xE7,0xE6, 0xE5, 0xE4, 0xE3, 0xE2, 0xE1,0xE0, 0xDF, 0xDE, 0xDD, 0xDC, 0xDB,0xDA, 0xD9,};uint8_t rx[ARRAY_SIZE(tx)] = {0, };struct spi_ioc_transfer tr = {.tx_buf = (unsigned long)tx,.rx_buf = (unsigned long)rx,.len = ARRAY_SIZE(tx),.delay_usecs = delay,.speed_hz = speed,.bits_per_word = bits,};ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);#if 1if(rx[0] != 0xff){for (ret = 0; ret < ARRAY_SIZE(tx); ret++) {if (!(ret % 6))puts("");printf("%.2d ", rx[ret]);}puts("");}#endif}static void do_read(int fd, int len){uint8_t buf[len], bp[len];int status;/* read at least 2 bytes, no more than 32 */if (len < 2)len = 2;else if (len > sizeof(buf))len = sizeof(buf);memset(buf, 0, sizeof buf);status = read(fd, buf, len);if (status < 0) {printf("read error \n");return;}if (status != len) {fprintf(stderr, "short read\n");return;}#if 0printf("read(%2d, %2d): %02x %02x,", len, status,buf[0], buf[1]);status -= 2;bp = buf + 2;while (status-- > 0)printf(" %02x", *bp++);printf("\n");#elseint i;#if 1printf("do read data is :\n");for(i = 0;i < len;i++)printf(" %d",buf[i]);printf("\n");#elsefor(i = 0;i < (len - 1);i++){bp[i] = buf[i+1];}bp[len-1] = buf[0];printf("data is :\n");for(i = 0;i < len;i++)printf(" %d",bp[i]);printf("\n");#endif#endif}static void do_write(int fd,int cmd){struct spi_ioc_transfer xfer;//unsigned char buf[BUF_SIZE], *bp;uint8_t buf[BUF_SIZE], *bp;int status;int len;uint8_t tx[] = {cmd, (cmd +1), (cmd+2), 0xFB, 0xFA, 0xF9,0xF8, 0xF7, 0xF6, 0xF5, 0xF4, 0xF3,0xF2, 0xF1, 0xF0, 0xEF, 0xEE, 0xED,0xEC, 0xEB, 0xEA, 0xE9, 0xE8, 0xE7,0xE6, 0xE5, 0xE4, 0xE3, 0xE2, 0xE1,0xE0, 0xDF, 0xDE, 0xDD, 0xDC, 0xDB,0xDA, 0xD9,};//memset(xfer, 0, sizeof xfer);memset(buf, 1, sizeof buf);len = sizeof buf;//buf[0] = cmd;#if 0buf[0] = cmd;buf[1] = cmd;xfer.tx_buf = (unsigned long)buf;xfer.len = len;status = ioctl(fd, SPI_IOC_MESSAGE(1), xfer);if (status < 0) {printf("SPI_IOC_MESSAGE\n");return;}//printf("do write data is %d\n",buf[0]);printf("response(%2d, %2d)\n", len, status);#endifstatus = write(fd,tx,BUF_SIZE);if(status < 0)printf("do write failed\n");#if 0for (bp = buf; len; len--)printf(" %02x", *bp++);printf("\n");#endif}int main(int argc, char *argv[]){int ret = 0;int fd;int data;fd_set rds; struct timeval tv;#if 0if(argc < 2) {printf("please enter one parameter at least\n");return 0;}data = atoi(argv[1]);#elsedata = 0;#endiffd = open(device, O_RDWR);if (fd < 0)printf("cant open device");mode = SPI_MODE_1;/** spi mode*/ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);if (ret == -1)printf("cant set spi mode");ret = ioctl(fd, SPI_IOC_RD_MODE, &mode);if (ret == -1)printf("cant get spi mode");/** bits per word*/ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);if (ret == -1)printf("cant set bits per word");ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);if (ret == -1)printf("cant get bits per word");/** max speed hz*/ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);if (ret == -1)printf("cant set max speed hz");ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);if (ret == -1)printf("cant get max speed hz");printf("spi mode: %d\n", mode);printf("bits per word: %d\n", bits);printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000);//FD_ZERO(&rds); //FD_SET(fd, &rds);//tv.tv_sec = 10;//tv.tv_usec = 0;int i;while(1){FD_ZERO(&rds); FD_SET(fd, &rds);tv.tv_sec = 1;tv.tv_usec = 0;printf("send daia is -----------------------%d\n",data);do_write(fd,data);sleep(1); ret = select(fd+1,&rds,NULL,NULL,&tv);if(ret < 0) {printf("select error!\n");//break;}printf("ret is +++++++++++++++++++%d\n",ret);if (FD_ISSET(fd, &rds))do_read(fd,BUF_SIZE);data++;if(data > 8)data = 0;sleep(1);}close(fd);return ret;}
这两部分源码下载地址为:点击打开链接
s5pv210stm32spi通 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)