Chinaunix首页 | 论坛 | 博客
  • 博客访问: 74448
  • 博文数量: 11
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 94
  • 用 户 组: 普通用户
  • 注册时间: 2015-12-11 15:27
个人简介

给自己一个城堡

文章分类
文章存档

2017年(3)

2016年(8)

我的朋友

分类: 嵌入式

2017-01-12 19:15:30

下面是基于linux2.6.30实现的spi驱动的应用层测试程序:

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <string.h>
  4. #include <fcntl.h>
  5. #include <sys/ioctl.h>
  6. #include <linux/types.h>
  7. #include <linux/spi/spidev.h>

  8. #define SPI_DEVICE "/dev/spidev1.0"

  9. //fm25L256 cmd
  10. #define FM_OPT_WREN     0x06
  11. #define FM_OPT_WRDI     0x04
  12. #define FM_OPT_RDSR 0x05
  13. #define FM_OPT_WRSR 0x01
  14. #define FM_OPT_ERAD 0x03
  15. #define FM_OPT_WRIT 0x02

  16. //fm25l256 para
  17. #define FM_ADDR_MAX 0x7fff
  18. #define FM_MODE SPI_MODE_0
  19. #define FM_BITS            8
  20. #define FM_FREQ_MAX 50000000
  21. #define FM_OPT_ADDR 0x0

  22. static uint8_t gMode = FM_MODE;
  23. static uint8_t gBits = FM_BITS;
  24. static uint32_t gSpeed = FM_FREQ_MAX;
  25. static uint16_t gOptAddr = FM_OPT_ADDR;
  26. uint8_t gLsb;
  27. static int spi_write_read(int fd, const uint8_t *wbuf, int wlen, uint8_t *rbuf, int rlen){
  28.     int ret = 0;
  29.     struct spi_ioc_transfer tr[2] = {
  30.         {
  31.             .tx_buf = (unsigned long)wbuf,
  32.             .rx_buf = 0,
  33.             .len = wlen,
  34.             .speed_hz = 5000000,
  35.         },
  36.         {
  37.             .tx_buf = 0,
  38.             .rx_buf = (unsigned long)rbuf,
  39.             .len = rlen,
  40.             .speed_hz = 5000000,
  41.         },
  42.     };

  43.     ret = ioctl(fd, SPI_IOC_MESSAGE(2), tr);
  44.     return ret;
  45. }

  46. static int spi_write_only(int fd, const uint8_t *wbuf, int wlen, uint8_t *rbuf, int rlen){
  47.     int ret = 0;
  48.     struct spi_ioc_transfer tr[1] = {
  49.         {
  50.             .tx_buf = (unsigned long)wbuf,
  51.             .rx_buf = 0,
  52.             .len = wlen,
  53.             .speed_hz = 5000000,
  54.         },
  55.     };

  56.     ret = ioctl(fd, SPI_IOC_MESSAGE(1), tr);
  57.     return ret;
  58. }
  59. int main(int argc, char *argv[]){
  60.     int fd;
  61.     int index;
  62.     int optSize;
  63.     int ret;
  64.     int i;
  65.     int tmpSize;
  66.     uint8_t rdBuf[128];
  67.     uint8_t wrBuf[128];
  68.     uint8_t cmd;
  69.     
  70.     if(argc != 2){
  71.         printf("argc error.\n");
  72.         return -1;
  73.     }

  74.     tmpSize = atoi(argv[1]);

  75.     fd = open(SPI_DEVICE, O_RDWR);
  76.     if(fd < 0){
  77.         printf("open spi file error.\n");
  78.         return -1;
  79.     }
  80.     
  81.     //mode
  82.     ret = ioctl(fd, SPI_IOC_WR_MODE, &gMode);
  83.     if(ret < 0){
  84.         printf("set wr mode error.\n");
  85.         close(fd);
  86.         return -1;
  87.     }
  88.     
  89.     //bits
  90.     ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &gBits);
  91.     if(ret < 0){
  92.         printf("set bits error.\n");
  93.         close(fd);
  94.         return -1;
  95.     }
  96.     
  97.     //speed
  98.     ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &gSpeed);
  99.     if(ret < 0){
  100.         printf("set speed error.\n");
  101.         close(fd);
  102.         return -1;
  103.     }
  104.     
  105.     //get mode
  106.     ret = ioctl(fd, SPI_IOC_RD_MODE, &gMode);
  107.     printf("FM Mode: %d.\n",gMode);

  108.     //get bits
  109.     ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &gBits);
  110.     printf("FM bits:%d.\n", gBits);
  111.     
  112.     //get speed
  113.     ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &gSpeed);
  114.     printf("FM speed:%d.\n", gSpeed);

  115.     //get lsb
  116.     ret = ioctl(fd, SPI_IOC_RD_LSB_FIRST, &gLsb);
  117.     printf("FM lsb:%d.\n", gLsb);
  118.     
  119. #if 0 //---------------------- method 1 ----------------------------
  120.     cmd = FM_OPT_WREN;
  121.     ret = write(fd, &cmd, sizeof(cmd));
  122.     if(ret < 0 )
  123.         printf("write wren error.err = %d\n", ret);
  124.     else
  125.         printf("write wren ok.\n");

  126.     
  127.     index = 0;
  128.     wrBuf[index++] = FM_OPT_WRIT;
  129.     wrBuf[index++] = (gOptAddr>>8)&0xff;
  130.     wrBuf[index++] = (gOptAddr>>0)&0xff;

  131.     srand((unsigned)time(NULL));
  132.     printf("write data = \n");

  133.         
  134.     if(tmpSize){
  135.         for(i = 0; i < tmpSize; i++){
  136.             rdBuf[i] = 1 + rand()%100+1;
  137.             printf("%02x ", rdBuf[i]);
  138.         }
  139.     }
  140.     else{
  141.         tmpSize = 128;
  142.         for(i = 0; i < tmpSize; i++){
  143.             rdBuf[i] = 0;
  144.             printf("%02x ", rdBuf[i]);
  145.         }
  146.     }
  147.     printf("\n");

  148.     memcpy(&wrBuf[index], rdBuf,tmpSize);
  149.     index += tmpSize;
  150.     optSize = index;
  151.     //ret = spi_write_read(fd, wrBuf, optSize, 0, 0);
  152.     ret = spi_write_only(fd, wrBuf,optSize, 0, 0);
  153.     if(ret < 0)
  154.         printf("write error,err = %d\n", ret);
  155.     else
  156.         printf("write ok.ret = %d\n", ret);
  157.     
  158.     memset(rdBuf, 0, sizeof(rdBuf));
  159.     cmd = FM_OPT_ERAD;
  160.     index = 0;
  161.     wrBuf[index++] = cmd;
  162.     wrBuf[index++] = (gOptAddr>>8)&0xff;
  163.     wrBuf[index++] = (gOptAddr>>0)&0xff;
  164.     ret = spi_write_read(fd, wrBuf, index, rdBuf, sizeof(rdBuf));
  165.     if(ret < 0){
  166.         printf("read error, err = %d\n", ret);
  167.     }
  168.     else{
  169.         printf("read ok.ret = %d, data = \n", ret);
  170.         for(index = 0; index < sizeof(rdBuf); index++){
  171.             printf("%02x ", rdBuf[index]);    
  172.         }
  173.         printf("\n");
  174.     }
  175. #endif //endof method 1 ------------------------------

  176. #if 1
  177. //**************** method 2 *******************
  178.     cmd = FM_OPT_WREN;
  179.     ret = write(fd, &cmd, sizeof(cmd));
  180.     if(ret < 0 )
  181.         printf("write wren error.err = %d\n", ret);
  182.     else
  183.         printf("write wren ok.\n");


  184.     index = 0;
  185.     //wrBuf[index++] = FM_OPT_WREN; //fail
  186.     wrBuf[index++] = FM_OPT_WRIT;
  187.     wrBuf[index++] = (gOptAddr>>8)&0xff;
  188.     wrBuf[index++] = (gOptAddr>>0)&0xff;

  189.     srand((unsigned)time(NULL));
  190.     printf("write data = \n");

  191.         
  192.     if(tmpSize){
  193.         for(i = 0; i < tmpSize; i++){
  194.             rdBuf[i] = 1 + rand()%100+1;
  195.             printf("%02x ", rdBuf[i]);
  196.         }
  197.     }
  198.     else{
  199.         tmpSize = 128;
  200.         for(i = 0; i < tmpSize; i++){
  201.             rdBuf[i] = 0;
  202.             printf("%02x ", rdBuf[i]);
  203.         }
  204.     }
  205.     printf("\n");

  206.     memcpy(&wrBuf[index], rdBuf,tmpSize);
  207.     index += tmpSize;

  208.     ret = write(fd, wrBuf, index);
  209.     if(ret < 0 )
  210.         printf("write error.err = %d\n", ret);
  211.     else
  212.         printf("write ok.ret = %d\n", ret);
  213.     
  214.     memset(rdBuf, 0, sizeof(rdBuf));
  215.     index = 0;
  216.     wrBuf[index++] = FM_OPT_ERAD;
  217.     wrBuf[index++] = (gOptAddr>>8)&0xff;
  218.     wrBuf[index++] = (gOptAddr>>0)&0xff;

  219.     ret = spi_write_read(fd, wrBuf, index, rdBuf, sizeof(rdBuf));
  220.     if(ret < 0 )
  221.         printf("read error.err = %d\n", ret);
  222.     else
  223.         printf("read ok.ret = %d\n", ret);
  224. #if 0 //-- fail
  225.     ret = write(fd, wrBuf, index);
  226.     ret = read(fd, rdBuf, 100);
  227.     if(ret < 0 )
  228.         printf("read error.err = %d\n", ret);
  229.     else
  230.         printf("read wren ok.ret = %d\n", ret);
  231. #endif


  232.     printf("data = \n");
  233.     for(index = 0; index < sizeof(rdBuf); index++){
  234.         printf("%02x ", rdBuf[index]);
  235.     }
  236.     printf("\n");
  237. #endif
  238.  //********************************************************************************/

  239.     close(fd);
  240.     return 0;
  241. }

阅读(2540) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~