硬汉嵌入式论坛

 找回密码
 立即注册
查看: 450|回复: 2
收起左侧

[技术讨论] 求助iic通信问题

[复制链接]

4

主题

11

回帖

23

积分

新手上路

积分
23
发表于 2023-7-17 16:02:02 | 显示全部楼层 |阅读模式
         在调试两个模块,从机的iic读写想调用主机的,但是从机读写没有寄存器地址,应该怎么操作



uint8_t hal_gh30x_i2c_write(uint8_t device_id, const uint8_t write_buffer[], uint16_t length)//从机写函数
{               
}

uint8_t hal_gh30x_i2c_read(uint8_t device_id, const uint8_t write_buffer[], uint16_t write_length, uint8_t read_buffer[], uint16_t read_length)//从机读函数
{
}



uint8_t iic_write_bytes(enum iic_channel_t channel, uint8_t slave_addr, uint8_t reg_addr, uint8_t *buffer, uint16_t length)
{
    volatile struct iic_reg_t *iic_reg;

    if(length == 0)
    {
        return true;
    }
    length--;

    if(channel == IIC_CHANNEL_0)
    {
        iic_reg = IIC0_REG_BASE;
    }
    else
    {
        iic_reg = IIC1_REG_BASE;
    }

    iic_reg->data = slave_addr | IIC_TRAN_START;
    while(iic_reg->status.trans_emp != 1);
    co_delay_10us(iic_byte_period[channel]);
    if(iic_reg->status.no_ack == 1)
    {
        return false;
    }

    iic_reg->data = reg_addr & 0xff;

    while(length)
    {
        while(iic_reg->status.trans_ful == 1);
        iic_reg->data = *buffer++;
        length--;
    }

    while(iic_reg->status.trans_ful == 1);
    iic_reg->data = *buffer | IIC_TRAN_STOP;

    while(iic_reg->status.bus_atv == 1);

    return true;
}



uint8_t iic_read_bytes(enum iic_channel_t channel, uint8_t slave_addr, uint8_t reg_addr, uint8_t *buffer, uint16_t length)
{
    volatile struct iic_reg_t *iic_reg;

    if(length == 0)
    {
        return true;
    }

    if(channel == IIC_CHANNEL_0)
    {
        iic_reg = IIC0_REG_BASE;
    }
    else
    {
        iic_reg = IIC1_REG_BASE;
    }

    iic_reg->data = slave_addr | IIC_TRAN_START;
    while(iic_reg->status.trans_emp != 1);
    co_delay_10us(iic_byte_period[channel]);
    if(iic_reg->status.no_ack == 1)
    {
        return false;
    }

    iic_reg->data = reg_addr & 0xff;
    iic_reg->data = slave_addr | 0x01 | IIC_TRAN_START;
       
    while(length > 1)
    {
        iic_reg->data = 0x00;

        while(iic_reg->status.rec_emp != 1)
        {
            *buffer++ = iic_reg->data;
            length--;
        }
        while(iic_reg->status.trans_emp != 1);
    }

    iic_reg->data = IIC_TRAN_STOP;

    while(iic_reg->status.bus_atv == 1);

    while(length)
    {
        *buffer++ = iic_reg->data;
        length--;
    }

    *buffer = iic_reg->data&0xff;

    return true;
}


               

回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106769
QQ
发表于 2023-7-17 16:04:02 | 显示全部楼层
意思是从机主动给主机发消息吗,这个不支持,不知道楼主说的是不是这个意思 。
回复

使用道具 举报

4

主题

11

回帖

23

积分

新手上路

积分
23
 楼主| 发表于 2023-7-17 16:09:54 来自手机 | 显示全部楼层

едiicд涼дЩ
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-5-5 09:54 , Processed in 0.153431 second(s), 26 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表