vcobvjk 发表于 2023-7-17 16:02:02

求助iic通信问题

         在调试两个模块,从机的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);
    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);
    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;
}


               

eric2013 发表于 2023-7-17 16:04:02

意思是从机主动给主机发消息吗,这个不支持,不知道楼主说的是不是这个意思 。

vcobvjk 发表于 2023-7-17 16:09:54

eric20132023-7-17 16:04


едiicд涼дЩ
页: [1]
查看完整版本: 求助iic通信问题