ВходНаше всё Теги codebook 无线电组件 Поиск Опросы Закон Вторник
26 ноября
1042774
POV_ (08.10.2020 11:58, просмотров: 537)
Вот такое вот вроде начало шуршать... 
int i2c_ReadBuffer(I2C_TypeDef* I2Cx, uint8_t slaveAddr, uint8_t* buf, uint16_t len, uint8_t ReadAddr)
{
    int i = 0;

    slaveAddr <<= 1;

    TIMED(10, I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY));

    I2C_GenerateSTART(I2Cx, ENABLE);
    TIMED(10, !I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT));

    I2C_Send7bitAddress(I2Cx, slaveAddr, I2C_Direction_Transmitter);
    TIMED(10, !I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

    //I2C_Cmd(I2Cx, ENABLE);

    I2C_SendData(I2Cx, ReadAddr);
    TIMED(10, !I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

    I2C_GenerateSTART(I2Cx, ENABLE);
    TIMED(10, !I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT));

    I2C_Send7bitAddress(I2Cx, slaveAddr, I2C_Direction_Receiver);
    TIMED(10, !I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));

    for (i = 0; i < len-1; i++)
    {
        I2Cx->CR1 &= ~I2C_CR1_ACK;
        TIMED(10, !I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED));
        buf[i] = I2C_ReceiveData(I2Cx);
    }
    I2Cx->CR1 &= ~I2C_CR1_ACK;
    TIMED(10, !I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED));
    I2C_GenerateSTOP(I2Cx, ENABLE);
    buf[i] = I2C_ReceiveData(I2Cx);

    return SUCCESS;

errReturn:
    I2Cx->CR1 &= ~I2C_CR1_ACK;
    I2C_GenerateSTOP(I2Cx, ENABLE);
    return ERROR;
}