GD32之I2C读和写底层代码分析


int32_t i2c_buffer_read(uint8_t* p_buffer, uint8_t read_address, uint8_t device_address, uint16_t number_of_byte, uint16_t timeout)
{
    uint32_t delay_time;
    uint32_t i2c_delay_time = timeout * 48;

#ifdef I2C_MUTEX
    xSemaphoreTake(g_i2c_mutex, portMAX_DELAY);
#endif

    /* wait until I2C bus is idle */
    delay_time = i2c_delay_time;
    CHECK_TIMEOUT(delay_time, i2c_flag_get(I2C0, I2C_FLAG_I2CBSY));

    if(2 == number_of_byte){
        i2c_ackpos_config(I2C0,I2C_ACKPOS_NEXT);
    }

    /* send a start condition to I2C bus */
    i2c_start_on_bus(I2C0);

    /* wait until SBSEND bit is set */
    delay_time = i2c_delay_time;
    CHECK_TIMEOUT(delay_time, SET!=i2c_flag_get(I2C0, I2C_FLAG_SBSEND));

    /* send slave address to I2C bus */
    i2c_master_addressing(I2C0, device_address, I2C_TRANSMITTER);

    /* wait until ADDSEND bit is set */
    delay_time = i2c_delay_time;
    CHECK_TIMEOUT(delay_time, SET!=i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));

    /* clear the ADDSEND bit */
    i2c_flag_clear(I2C0,I2C_FLAG_ADDSEND);

    /* wait until the transmit data buffer is empty */
    delay_time = i2c_delay_time;
    CHECK_TIMEOUT(delay_time, SET!=i2c_flag_get(I2C0, I2C_FLAG_TBE));

    /* enable I2C0*/
    i2c_enable(I2C0);

    /* send the EEPROM's internal address to write to */
    i2c_data_transmit(I2C0, read_address);

    /* wait until BTC bit is set */
    delay_time = i2c_delay_time;
    CHECK_TIMEOUT(delay_time, SET!=i2c_flag_get(I2C0, I2C_FLAG_BTC));

    /* send a start condition to I2C bus */
    i2c_start_on_bus(I2C0);

    /* wait until SBSEND bit is set */
    delay_time = i2c_delay_time;
    CHECK_TIMEOUT(delay_time, SET!=i2c_flag_get(I2C0, I2C_FLAG_SBSEND));

    /* send slave address to I2C bus */
    i2c_master_addressing(I2C0, device_address, I2C_RECEIVER);

    if(number_of_byte < 3){
        /* disable acknowledge */
        i2c_ack_config(I2C0,I2C_ACK_DISABLE);
    }

    /* wait until ADDSEND bit is set */
    delay_time = i2c_delay_time;
    CHECK_TIMEOUT(delay_time, SET!=i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));

    /* clear the ADDSEND bit */
    i2c_flag_clear(I2C0,I2C_FLAG_ADDSEND);

    if(1 == number_of_byte){
        /* send a stop condition to I2C bus */
        i2c_stop_on_bus(I2C0);
    }

    /* while there is data to be read */
    delay_time = i2c_delay_time;
    while((--delay_time) && number_of_byte){
        if(3 == number_of_byte){
            /* wait until BTC bit is set */
            while(!i2c_flag_get(I2C0, I2C_FLAG_BTC));

            /* disable acknowledge */
            i2c_ack_config(I2C0,I2C_ACK_DISABLE);
        }
        if(2 == number_of_byte){
            /* wait until BTC bit is set */
            while(!i2c_flag_get(I2C0, I2C_FLAG_BTC));
            /* send a stop condition to I2C bus */
            i2c_stop_on_bus(I2C0);
        }

        /* wait until the RBNE bit is set and clear it */
        if(i2c_flag_get(I2C0, I2C_FLAG_RBNE)){
            /* read a byte from the EEPROM */
            *p_buffer = i2c_data_receive(I2C0);

            /* point to the next location where the byte read will be saved */
            p_buffer++;

            /* decrement the read bytes counter */
            number_of_byte--;
        }
    }

    /* wait until the stop condition is finished */
    delay_time = i2c_delay_time;
    CHECK_TIMEOUT(delay_time, (I2C_CTL0(I2C0)&0x0200));

    /* enable acknowledge */
    i2c_ack_config(I2C0,I2C_ACK_ENABLE);
    i2c_ackpos_config(I2C0,I2C_ACKPOS_CURRENT);

#ifdef I2C_MUTEX
    xSemaphoreGive(g_i2c_mutex);
#endif
    return 0;

error:
#ifdef I2C_MUTEX
    xSemaphoreGive(g_i2c_mutex);
#endif
    i2c_stop_on_bus(I2C0);
    GTK_LOG_ERR(GTK_DBG_DRV_I2C, "i2c error %s:addr=%02x", __func__, device_address);
    return 1;
}

int32_t i2c_byte_write(uint8_t* p_buffer, uint8_t device_address, uint8_t write_address, uint16_t timeout)
{
    uint32_t delay_time;
    uint32_t i2c_delay_time = timeout * 48;

#ifdef I2C_MUTEX
    xSemaphoreTake(g_i2c_mutex, portMAX_DELAY);
#endif

    /* wait until I2C bus is idle */
    delay_time = i2c_delay_time;
    CHECK_TIMEOUT(delay_time, i2c_flag_get(I2C0, I2C_FLAG_I2CBSY));

    /* send a start condition to I2C bus */
    i2c_start_on_bus(I2C0);

    /* wait until SBSEND bit is set */
    delay_time = i2c_delay_time;
    CHECK_TIMEOUT(delay_time, SET!=i2c_flag_get(I2C0, I2C_FLAG_SBSEND));

    /* send slave address to I2C bus */
    delay_time = i2c_delay_time;
    i2c_master_addressing(I2C0, device_address, I2C_TRANSMITTER);

    /* wait until ADDSEND bit is set */
    CHECK_TIMEOUT(delay_time, SET!=i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));

    /* clear the ADDSEND bit */
    i2c_flag_clear(I2C0,I2C_FLAG_ADDSEND);

    /* wait until the transmit data buffer is empty */
    delay_time = i2c_delay_time;
    CHECK_TIMEOUT(delay_time, SET!=i2c_flag_get(I2C0, I2C_FLAG_TBE));

    /* send the EEPROM's internal address to write to : only one byte address */
    i2c_data_transmit(I2C0, write_address);

    /* wait until BTC bit is set */
    delay_time = i2c_delay_time;
    CHECK_TIMEOUT(delay_time, SET!=i2c_flag_get(I2C0, I2C_FLAG_BTC));

    /* send the byte to be written */
    i2c_data_transmit(I2C0, *p_buffer);

    /* wait until BTC bit is set */
    delay_time = i2c_delay_time;
    CHECK_TIMEOUT(delay_time, SET!=i2c_flag_get(I2C0, I2C_FLAG_BTC));

    /* send a stop condition to I2C bus */
    i2c_stop_on_bus(I2C0);

    /* wait until the stop condition is finished */
    delay_time = i2c_delay_time;

    CHECK_TIMEOUT(delay_time, (I2C_CTL0(I2C0)&0x0200));

#ifdef I2C_MUTEX
    xSemaphoreGive(g_i2c_mutex);
#endif
    return 0;

error:
#ifdef I2C_MUTEX
    xSemaphoreGive(g_i2c_mutex);
#endif
    i2c_stop_on_bus(I2C0);
    GTK_LOG_ERR(GTK_DBG_DRV_I2C, "i2c error %s:addr=%02x", __func__, device_address);
    return 1;
}

需要写两次设备地址

int32_t i2c_buffer_read(uint8_t* p_buffer, uint8_t read_address, uint8_t device_address, uint16_t number_of_byte, uint16_t timeout)
{  
    /* wait until I2C bus is idle */
    while(i2c_flag_get(I2C0, I2C_FLAG_I2CBSY));

    if(2 == number_of_byte){
        i2c_ackpos_config(I2C0,I2C_ACKPOS_NEXT);
    }
    
    /* send a start condition to I2C bus */
    i2c_start_on_bus(I2C0);
    
    /* wait until SBSEND bit is set */
    while(!i2c_flag_get(I2C0, I2C_FLAG_SBSEND));
    
    /* send slave address to I2C bus */ // 发送第一次设备地址
    i2c_master_addressing(I2C0, device_address, I2C_TRANSMITTER);
    
    /* wait until ADDSEND bit is set */
    while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
    
    /* clear the ADDSEND bit */
    i2c_flag_clear(I2C0,I2C_FLAG_ADDSEND);
    
    /* wait until the transmit data buffer is empty */
    while(SET != i2c_flag_get( I2C0 , I2C_FLAG_TBE));

    /* enable I2C0*/
    i2c_enable(I2C0);
    
    /* send the EEPROM's internal address to write to */
    i2c_data_transmit(I2C0, read_address);   //发送寄存器地址
    
    /* wait until BTC bit is set */
    while(!i2c_flag_get(I2C0, I2C_FLAG_BTC));
    
    /* send a start condition to I2C bus */
    i2c_start_on_bus(I2C0);
    
    /* wait until SBSEND bit is set */
    while(!i2c_flag_get(I2C0, I2C_FLAG_SBSEND));
    
    /* send slave address to I2C bus */
    i2c_master_addressing(I2C0, device_address, I2C_RECEIVER); //再次发送设备地址

    if(number_of_byte < 3){
        /* disable acknowledge */
        i2c_ack_config(I2C0,I2C_ACK_DISABLE);
    }
    
    /* wait until ADDSEND bit is set */
    while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
    
    /* clear the ADDSEND bit */
    i2c_flag_clear(I2C0,I2C_FLAG_ADDSEND);
    
    if(1 == number_of_byte){
        /* send a stop condition to I2C bus */
        i2c_stop_on_bus(I2C0);
    }
    
    /* while there is data to be read */
    while(number_of_byte){
        if(3 == number_of_byte){
            /* wait until BTC bit is set */
            while(!i2c_flag_get(I2C0, I2C_FLAG_BTC));

            /* disable acknowledge */
            i2c_ack_config(I2C0,I2C_ACK_DISABLE);
        }
        if(2 == number_of_byte){
            /* wait until BTC bit is set */
            while(!i2c_flag_get(I2C0, I2C_FLAG_BTC));
            
            /* send a stop condition to I2C bus */
            i2c_stop_on_bus(I2C0);
        }
        
        /* wait until the RBNE bit is set and clear it */
        if(i2c_flag_get(I2C0, I2C_FLAG_RBNE)){
            /* read a byte from the EEPROM */
            *p_buffer = i2c_data_receive(I2C0); //读取等待的数据
            
            /* point to the next location where the byte read will be saved */
            p_buffer++; 
            
            /* decrement the read bytes counter */
            number_of_byte--;
        } 
    }
    
    /* wait until the stop condition is finished */
    while(I2C_CTL0(I2C0)&0x0200);
    
    /* enable acknowledge */
    i2c_ack_config(I2C0,I2C_ACK_ENABLE);

    i2c_ackpos_config(I2C0,I2C_ACKPOS_CURRENT);
}

 

//需要写一次设备地址
int32_t i2c_byte_write(uint8_t* p_buffer, uint8_t device_address, uint8_t write_address, uint16_t timeout)
{
    /* wait until I2C bus is idle */
    while(i2c_flag_get(I2C0, I2C_FLAG_I2CBSY));

    /* send a start condition to I2C bus */
    i2c_start_on_bus(I2C0);
    
    /* wait until SBSEND bit is set */
    while(!i2c_flag_get(I2C0, I2C_FLAG_SBSEND));
    
    /* send slave address to I2C bus */
    i2c_master_addressing(I2C0, device_address, I2C_TRANSMITTER);
    
    /* wait until ADDSEND bit is set */
    while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
    
    /* clear the ADDSEND bit */
    i2c_flag_clear(I2C0,I2C_FLAG_ADDSEND);
    
    /* wait until the transmit data buffer is empty */
    while(SET != i2c_flag_get(I2C0, I2C_FLAG_TBE));
    
    /* send the EEPROM's internal address to write to : only one byte address */
    i2c_data_transmit(I2C0, write_address);
    
    /* wait until BTC bit is set */
    while(!i2c_flag_get(I2C0, I2C_FLAG_BTC));
    
    /* send the byte to be written */
    i2c_data_transmit(I2C0, *p_buffer); 
    
    /* wait until BTC bit is set */
    while(!i2c_flag_get(I2C0, I2C_FLAG_BTC));

    /* send a stop condition to I2C bus */
    i2c_stop_on_bus(I2C0);
    
    /* wait until the stop condition is finished */
    while(I2C_CTL0(I2C0)&0x0200);
}
 

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值