linux spi讀寫


用的xubuntu,發現有/dev/spidev0.0,是不是意味着驅動已經裝好。測試代碼,如下:
/*
* Debug.h
* 摘要:用於打印調試信息
*         為了統一控制打印信息是否輸出,而用宏定義的打印函數。同時也可以起到開發版本與發布版本是同一個版本
*  Created on: 2013-5-22
*      Author: lzy
*/

#ifndef DEBUG_H_
#define DEBUG_H_

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

#define     DEBUG_SWITCH    1        /* 打開調試信息打印功能 */
#define     ERR_DEBUG_SWITCH    1    /* 打印錯誤信息打印功能 */

/**
* 簡單打印調試信息
*/
#if    DEBUG_SWITCH
#define pr_debug(fmt,args...) printf(fmt, ##args)
#else
#define pr_debug(fmt,args...) /*do nothing */
#endif

/**
* 錯誤信息打印
* 自動打印發生錯誤時代碼所在的位置
*/
#if    ERR_DEBUG_SWITCH
#define pr_err(fmt,args...) printf("\nError:\nFile:<%s> Fun:[%s] Line:%d\n "fmt, __FILE__, __FUNCTION__, __LINE__, ##args)
#else
#define pr_err(fmt,args...) /*do nothing */
#endif

#endif /* DEBUG_H_ */

/*
* 說明:SPI通訊實現
*           方式一: 同時發送與接收實現函數: SPI_Transfer()
*           方式二:發送與接收分開來實現
*           SPI_Write() 只發送
*           SPI_Read()  只接收
*           兩種方式不同之處:方式一,在發的過程中也在接收,第二種方式,收與發單獨進行
*  Created on: 2013-5-28
*      Author: lzy
*/

#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 = "/dev/spidev0.0";
static uint8_t mode = 0; /* SPI通信使用全雙工,設置CPOL=0,CPHA=0。 */
static uint8_t bits = 8; /* 8bits讀寫,MSB first。*/
static uint32_t speed = 12 * 1000 * 1000;/* 設置12M傳輸速度 */
static uint16_t delay = 0;
static int g_SPI_Fd = 0;

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

/**
*  功 能:同步數據傳輸
* 入口參數 :
*             TxBuf -> 發送數據首地址
*             len -> 交換數據的長度
* 出口參數:
*             RxBuf -> 接收數據緩沖區
* 返回值:0 成功
* 開發人員:Lzy 2013-5-22
*/
int SPI_Transfer(const uint8_t *TxBuf, uint8_t *RxBuf, int len)
{
    int ret;
    int fd = g_SPI_Fd;

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

    ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
    if (ret < 1)
        pr_err("can't send spi message");
    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
    }
    return ret;
}

/**
* 功 能:發送數據
* 入口參數 :
*             TxBuf -> 發送數據首地址
*            len ->  發送與長度
*返回值:0 成功
* 開發人員:Lzy 2013-5-22
*/
int SPI_Write(uint8_t *TxBuf, int len)
{
    int ret;
    int fd = g_SPI_Fd;

    ret = write(fd, TxBuf, len);
    if (ret < 0)
        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
    }

    return ret;
}

/**
* 功 能:接收數據
* 出口參數:
*         RxBuf -> 接收數據緩沖區
*         rtn -> 接收到的長度
* 返回值:>=0 成功
* 開發人員:Lzy 2013-5-22
*/
int SPI_Read(uint8_t *RxBuf, int len)
{
    int ret;
    int fd = g_SPI_Fd;
    ret = read(fd, RxBuf, len);
    if (ret < 0)
        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
    }

    return ret;
}

/**
* 功 能:打開設備  並初始化設備
* 入口參數 :
* 出口參數:
* 返回值:0 表示已打開  0XF1 表示SPI已打開 其它出錯
* 開發人員:Lzy 2013-5-22
*/
int SPI_Open(void)
{
    int fd;
    int ret = 0;

    if (g_SPI_Fd != 0) /* 設備已打開 */
        return 0xF1;

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

    g_SPI_Fd = fd;
    /*
     * spi mode
     */
    ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
    if (ret == -1)
        pabort("can't set spi mode");

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

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

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

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

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

    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(void)
{
    int fd = g_SPI_Fd;

    if (fd == 0) /* SPI是否已經打開*/
        return 0;
    close(fd);
    g_SPI_Fd = 0;

    return 0;
}

/**
* 功 能:自發自收測試程序
*         接收到的數據與發送的數據如果不一樣 ,則失敗
* 說明:
*         在硬件上需要把輸入與輸出引腳短跑
* 開發人員:Lzy 2013-5-22
*/
int SPI_LookBackTest(void)
{
    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(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;
}

int main(int argc, char *argv[])
{
    int ret = 0;

    ret = SPI_Open();
    if (ret)
        return ret;

    SPI_LookBackTest();

//    unsigned char buf[10];
//    SPI_Write(buf, 10);
//    SPI_Read(buf, 10);

    SPI_Close();

return 0;
}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM