DoubleLi

qq: 517712484 wx: ldbgliet

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  4737 随笔 :: 2 文章 :: 542 评论 :: 1615万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

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\n", __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\n", __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\n", __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\n", __FUNCTION__, ret);   
        val = 0;   
    }   
else 
        val = rx_buf[0];   

    up(&chip->lock);   

return val;   
}  

posted on   DoubleLi  阅读(6881)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示