代码之家  ›  专栏  ›  技术社区  ›  fadedbee

如何在STM32F105上作为从机接收未知数量的I2C数据?

  •  0
  • fadedbee  · 技术社区  · 3 年前

    我在STM32F105上使用ST的HAL作为从机接收I2C数据。

    使用以下代码读取数据:

    // Called when an initial I2C address packet is found.
    void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode)
    {
        // Check I2C communication is addressed to the slave.
        if(AddrMatchCode == I2C_USERSPACE_ADDR || AddrMatchCode == I2C_KERNELSPACE_ADDR) {
            i2c_last_activity = HAL_GetTick();
            switch(TransferDirection) {
                case I2C_DIRECTION_TRANSMIT:
                    // Get all data written to slave.
                    // If it is a write to register command from the Master this will grab all data that comes in
                    // to be later polled when the slave_data_available flag is set.
                    // If it is a read from register command from the Master then this will grab the register being
                    // addressed by the Master.
                    wr_AddrMatchCode = AddrMatchCode;
                    if (HAL_I2C_Slave_Seq_Receive_IT(&hi2c1, rx_buffer, RX_BUFFER_SIZE, I2C_FIRST_FRAME) != HAL_OK) {
                        Error_Handler();
                    }
                break;
    

    这个 Error_Handler() 不调用上面的函数。下面的错误是由I2C生成的信号触发的 STOP .)

    前提是我准确地发送 RX_BUFFER_SIZE 字节,一切都很好。

    如果我发送的邮件少,就会出现错误 HAL_I2C_ERROR_AF (== 4) 在STM32接收到I2C STOP的时刻生成。

    HAL_I2C_ErrorCallback() at stm32f1xx_hal_i2c.c:4,997 0x800447e  
    I2C_ITError() at stm32f1xx_hal_i2c.c:6,278 0x8005510    
    I2C_Slave_STOPF() at stm32f1xx_hal_i2c.c:6,037 0x80052ba    
    HAL_I2C_EV_IRQHandler() at stm32f1xx_hal_i2c.c:4,729 0x800439c  
    I2C1_EV_IRQHandler() at stm32f1xx_it.c:261 0x8001e9a    
    <signal handler called>() at 0xfffffff9 
    HAL_GetTick() at stm32f1xx_hal.c:307 0x8002174  
    HAL_Delay() at stm32f1xx_hal.c:371 0x80021ac    
    main() at main.c:192 0x800162c  
    

    注意:这个堆栈跟踪中没有非HAL函数,除了 main() .

    我如何接收小于的未知量的I2C数据 RX_BUFFER_SIZE ?

    0 回复  |  直到 3 年前