STM32CubeIDE 8 (I2C with DMA)

処理が増えてくると、DMA転送させてすぐの状態判定だと厳しくなってきます。

I2C with DMA

HAL_Delayを追加します。

main.cpp

void
i2c_write( uint16_t addr, uint8_t *tx_buf, uint16_t len )
{
    /// without DMA

    // while (HAL_I2C_Master_Transmit(&hi2c1, addr, tx_buf, len, 10000) != HAL_OK)
    // {
    //     if (HAL_I2C_GetError(&hi2c1) != HAL_I2C_ERROR_AF)
    //     {
    //         error_type = I2C_ERROR;
    //         Error_Handler();
    //     }
    // }

    /// with DMA

    if (HAL_I2C_Master_Transmit_DMA(&hi2c1, addr, tx_buf, len) != HAL_OK)
    {
        sprintf( uart_message, "\r\n HAL_I2C_Master_Transmit_DMA \r\n" );
        p_( uart_message );
        error_type = I2C_ERROR;
        Error_Handler();
    }

    HAL_Delay(1);

    while (HAL_I2C_GetState(&hi2c1) != HAL_I2C_STATE_READY)
    {
        sprintf( uart_message, "\r\n WRITE HAL_I2C_STATE_BUSY \r\n" );
        p_( uart_message );
        HAL_Delay(1);
    }
}

void
i2c_read( uint16_t addr, uint8_t reg, uint8_t *rx_buf, uint16_t len )
{
    /// without DMA

    // while (HAL_I2C_Master_Receive(&hi2c1, addr, rx_buf, len, 10000) != HAL_OK)
    // {
    //     if (HAL_I2C_GetError(&hi2c1) != HAL_I2C_ERROR_AF)
    //     {
    //         error_type = I2C_ERROR;
    //         Error_Handler();
    //     }
    // }

    /// with DMA

    if (HAL_I2C_Master_Receive_DMA(&hi2c1, addr, rx_buf, len) != HAL_OK)
    {
        sprintf( uart_message, "\r\n HAL_I2C_Master_Receive_DMA \r\n" );
        p_( uart_message );
        error_type = I2C_ERROR;
        Error_Handler();
    }

    HAL_Delay(1);

    while (HAL_I2C_GetState(&hi2c1) != HAL_I2C_STATE_READY)
    {
        sprintf( uart_message, "\r\n READ HAL_I2C_STATE_BUSY \r\n" );
        p_( uart_message );
        HAL_Delay(1);
    }

    // if (HAL_I2C_Mem_Read_DMA(&hi2c1, addr, (uint16_t)reg, 1, rx_buf, len) != HAL_OK)
    // {
    //     sprintf( uart_message, "\r\n HAL_I2C_Mem_Read_DMA \r\n" );
    //     p_( uart_message );
    //     error_type = I2C_ERROR;
    //     Error_Handler();
    // }
}

まとめ

いかにも小手先な感じですが、動作は安定します。

広告

IT開発関連書とビジネス書が豊富な翔泳社の通販『SEshop』
さくらのレンタルサーバ
ムームードメイン
Oisix(おいしっくす)
らでぃっしゅぼーや
珈琲きゃろっと
エプソムソルト




«       »