zoukankan      html  css  js  c++  java
  • Linux下SPI读写外部寄存器的操作

    SPI写寄存器操作:

      

    staticvoid mcp251x_write_reg(struct spi_device *spi, uint8_t reg, uint8_t val)   
    {   
    struct mcp251x *chip = dev_get_drvdata(&spi->dev);   
    int ret;   

        down(&chip->lock);   

        chip->spi_transfer_buf[0] = INSTRUCTION_WRITE;   
        chip->spi_transfer_buf[1] = reg;   
        chip->spi_transfer_buf[2] = val;   

        ret = spi_write(spi, chip->spi_transfer_buf, 3);   
    if (ret < 0)   
            dev_dbg(&spi->dev, "%s: failed: ret = %d ", __FUNCTION__, ret);   

        up(&chip->lock);   
    }  

      



    staticvoid mcp251x_write_bits(struct spi_device *spi, uint8_t reg, uint8_t mask, uint8_t val)  
    {   
    struct mcp251x *chip = dev_get_drvdata(&spi->dev);   
    int ret;   

        down(&chip->lock);   

        chip->spi_transfer_buf[0] = INSTRUCTION_BIT_MODIFY;   
        chip->spi_transfer_buf[1] = reg;   
        chip->spi_transfer_buf[2] = mask;   
        chip->spi_transfer_buf[3] = val;   

        ret = spi_write(spi, chip->spi_transfer_buf, 4);   
    if (ret < 0)   
            dev_dbg(&spi->dev, "%s: failed: ret = %d ", __FUNCTION__, ret);   

        up(&chip->lock);   
    }  

    SPI读寄存器操作:

      

    static uint8_t mcp251x_read_reg(struct spi_device *spi, uint8_t reg)   
    {   
    struct mcp251x *chip = dev_get_drvdata(&spi->dev);   
        uint8_t *tx_buf, *rx_buf;   
        uint8_t val;   
    int ret;   

        tx_buf = chip->spi_transfer_buf;   
        rx_buf = chip->spi_transfer_buf + 8;   

        down(&chip->lock);   

        tx_buf[0] = INSTRUCTION_READ;   
        tx_buf[1] = reg;   
        ret = spi_write_then_read(spi, tx_buf, 2, rx_buf, 1);   
    if (ret < 0)   
        {   
            dev_dbg(&spi->dev, "%s: failed: ret = %d ", __FUNCTION__, ret);   
            val = 0;   
        }   
    else 
            val = rx_buf[0];   

        up(&chip->lock);   

    return val;   
    }  

      



    static uint8_t mcp251x_read_state(struct spi_device *spi, uint8_t cmd)   
    {   
    struct mcp251x *chip = dev_get_drvdata(&spi->dev);   
        uint8_t *tx_buf, *rx_buf;   
        uint8_t val;   
    int ret;   

        tx_buf = chip->spi_transfer_buf;   
        rx_buf = chip->spi_transfer_buf + 8;   

        down(&chip->lock);   

        tx_buf[0] = cmd;   
        ret = spi_write_then_read(spi, tx_buf, 1, rx_buf, 1);   
    if (ret < 0)   
        {   
            dev_dbg(&spi->dev, "%s: failed: ret = %d ", __FUNCTION__, ret);   
            val = 0;   
        }   
    else 
            val = rx_buf[0];   

        up(&chip->lock);   

    return val;   
    }  

  • 相关阅读:
    There is no Action mapped for namespace [/pages/action/student] and action name [findStudent]
    站点设计至尊 (展示文)
    【翻译自mos文章】DBA_JOBS 和 DBA_JOBS_RUNNING 不同的结果的解释
    Effective C++ 条款45
    NYOJ109 数列转换 【守恒法】
    Windows 上安装 pip
    UnicodeDecodeError: 'ascii' codec can't decode byte 0xe0 in position 0
    解决Eclipse 项目报错:Unbound classpath container: ‘JRE System Library [JavaSE-1.7]
    neuroph Perceptron Sample
    Visio 画图去掉页边距(图形四周的空白区域)的解决办法
  • 原文地址:https://www.cnblogs.com/lidabo/p/6406100.html
Copyright © 2011-2022 走看看