spi测试代码

/*
* 说明:SPI通讯实现
* 方式一: 同时发送与接收实现函数: spi_transfer()
* 方式二:发送与接收分开来实现
* spi_write() 只发送
* spi_read() 只接收
* 两种方式不同之处:方式一,在发的过程中也在接收,第二种方式,收与发单独进行
*/




#include <stdint.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>




#include "Debug.h"
#define SPI_DEBUG 0




static const char *device[2] = {"/dev/spidev0.0","/dev/spidev1.0"};
static uint8_t mode = 0; /* SPI通信使用全双工,设置CPOL=0,CPHA=0。 */
static uint8_t bits = 8; /* 8bits读写,MSB first。*/
static uint32_t speed = 3 * 1000 * 1000;/* 设置12M传输速度 */
static uint16_t delay = 0;
static int g_spi_fd[2] = {0,0};




static void pabort(const char *s)
{
    perror(s);
    abort();
}






/**
* 功 能:打开设备 并初始化设备
* 入口参数 :
* 出口参数:
* 返回值:0 表示已打开 0XF1 表示SPI已打开 其它出错
*/
int spi_open(int spi_no)
{
    int fd;
    int ret = 0;




    if (g_spi_fd[spi_no] != 0) /* 设备已打开 */
        return 0xF1;




    fd = open(device[spi_no], O_RDWR);
    if (fd < 0)
    {
        pabort("can't open device\n");
    }
    else
    {
        pr_debug("SPI - Open Succeed. Start Init SPI...\n");
    }




    g_spi_fd[spi_no] = fd;
    /*
        * spi mode
        */
    ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
    if (ret == -1)
        pabort("can't set spi mode\n");




    ret = ioctl(fd, SPI_IOC_RD_MODE, &mode);
    if (ret == -1)
        pabort("can't get spi mode\n");




    /*
    * bits per word
    */
    ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
    if (ret == -1)
        pabort("can't set bits per word\n");




    ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
    if (ret == -1)
        pabort("can't get bits per word\n");




    /*
    * max speed hz
    */
    ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
    if (ret == -1)
        pabort("can't set max speed hz\n");




    ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
    if (ret == -1)
        pabort("can't get max speed hz\n");




    pr_debug("spi mode: %d\n", mode);
    pr_debug("bits per word: %d\n", bits);
    pr_debug("max speed: %d KHz (%d MHz)\n", speed / 1000, speed / 1000 / 1000);




    return ret;
}




/**
* 功 能:关闭SPI模块
*/
int spi_close(int spi_no)
{
    int fd = g_spi_fd[spi_no];




    if (fd == 0) /* SPI是否已经打开*/
        return 0;
    
    close(fd);
    g_spi_fd[spi_no] = 0;


    return 0;
}




/**
* 功 能:同步数据传输
* 入口参数 :
* TxBuf -> 发送数据首地址
* len -> 交换数据的长度
* 出口参数:
* RxBuf -> 接收数据缓冲区
* 返回值:0 成功
*/
int spi_transfer(int spi_no, const uint8_t *TxBuf, uint8_t *RxBuf, int len)
{
    int ret;
    int fd = 0;


    struct spi_ioc_transfer tr ={
        .tx_buf = (unsigned long) TxBuf,
        .rx_buf = (unsigned long) RxBuf,
        .len =len,
        .delay_usecs = delay,
        };


    spi_open(spi_no);
    fd = g_spi_fd[spi_no];
    ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
    if (ret < 1)
    {
        spi_close(spi_no);
        pr_err("can't send spi message\n");
    }
    else
    {
#if SPI_DEBUG
        int i;
        pr_debug("\nsend spi message Succeed");
        pr_debug("\nSPI Send [Len:%d]: ", len);
        for (i = 0; i < len; i++)
        {
            if (i % 8 == 0)
            printf("\n\t");
            printf("0x%02X ", TxBuf[i]);
        }
        printf("\n");


        pr_debug("SPI Receive [len:%d]:", len);
        for (i = 0; i < len; i++)
        {
            if (i % 8 == 0)
            printf("\n\t");
            printf("0x%02X ", RxBuf[i]);
        }
        printf("\n");
#endif
    }
    spi_close(spi_no);
    return ret;
}




/**
* 功 能:发送数据
* 入口参数 :
* TxBuf -> 发送数据首地址
*len -> 发送与长度
*返回值:0 成功
*/
int spi_write(int spi_no, uint8_t *TxBuf, int len)
{
    int ret;
    int fd = 0;


    spi_open(spi_no);
    fd = g_spi_fd[spi_no];
    ret = write(fd, TxBuf, len);
    if (ret < 0)
    {
        spi_close(spi_no);
        pr_err("SPI Write error\n");
    }
    else
    {
#if SPI_DEBUG
        int i;
        pr_debug("\nSPI Write [Len:%d]: ", len);
        for (i = 0; i < len; i++)
        {
            if (i % 8 == 0)
            printf("\n\t");
            printf("0x%02X ", TxBuf[i]);
        }
    printf("\n");
#endif
    }
    spi_close(spi_no);
    return ret;
}




/**
* 功 能:接收数据
* 出口参数:
* RxBuf -> 接收数据缓冲区
* rtn -> 接收到的长度
* 返回值:>=0 成功
*/
int spi_read(int spi_no, uint8_t *RxBuf, int len)
{
    int ret;
    int fd = 0;


    spi_open(spi_no);
    fd = g_spi_fd[spi_no];
    ret = read(fd, RxBuf, len);
    if (ret < 0)
    {
        spi_close(spi_no);
        pr_err("SPI Read error\n");
    }
    else
    {
#if SPI_DEBUG
        int i;
        pr_debug("SPI Read [len:%d]:", len);
        for (i = 0; i < len; i++)
        {
            if (i % 8 == 0)
            printf("\n\t");
            printf("0x%02X ", RxBuf[i]);
        }
        printf("\n");
#endif
    }
    spi_close(spi_no);
    return ret;
}






/**
* 功 能:自发自收测试程序
* 接收到的数据与发送的数据如果不一样 ,则失败
* 说明:
* 在硬件上需要把输入与输出引脚短跑
*/
int spi_lookback_test(int spi_no)
{
    int ret, i;
    const int BufSize = 16;
    uint8_t tx[BufSize], rx[BufSize];




    bzero(rx, sizeof(rx));
    for (i = 0; i < BufSize; i++)
    {
        tx[i] = i;
    }




    pr_debug("\nSPI - LookBack Mode Test...\n");
    ret = spi_transfer(spi_no, tx, rx, BufSize);
    if (ret > 1)
    {
        ret = memcmp(tx, rx, BufSize);
        if (ret != 0)
        {
            pr_err("LookBack Mode Test error\n");
            //pabort("error");
        }
        else
            pr_debug("SPI - LookBack Mode OK\n");
    }


    return ret;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值