Skip to content

Commit

Permalink
[bsp][stm32] fix hardware i2c driver bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Dyyt587 committed Apr 2, 2024
1 parent 3a4db99 commit 34fb8d2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
18 changes: 15 additions & 3 deletions bsp/stm32/libraries/HAL_Drivers/drivers/drv_hard_i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,18 @@ static rt_err_t stm32_i2c_init(struct stm32_i2c *i2c_drv)
rt_memset(i2c_handle, 0, sizeof(I2C_HandleTypeDef));
struct stm32_i2c_config *cfg = i2c_drv->config;
i2c_handle->Instance = cfg->Instance;
#if defined(SOC_SERIES_STM32H7)
i2c_handle->Init.Timing = cfg->timing;
#endif /* defined(SOC_SERIES_STM32H7) */
#if defined(SOC_SERIES_STM32F4)
hi2c1.Init.ClockSpeed = 100000;
hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
#endif /* defined(SOC_SERIES_STM32F4) */
i2c_handle->Init.OwnAddress1 = 0;
i2c_handle->Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
#if defined(SOC_SERIES_STM32H7)
i2c_handle->Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
#endif /* defined(SOC_SERIES_STM32H7) */
i2c_handle->Init.OwnAddress2 = 0;
i2c_handle->Init.OwnAddress2Masks = I2C_OA2_NOMASK;
i2c_handle->Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
Expand All @@ -76,7 +84,7 @@ static rt_err_t stm32_i2c_init(struct stm32_i2c *i2c_drv)
{
return -RT_EFAULT;
}

#if defined(SOC_SERIES_STM32H7)
/* Configure Analogue filter */
if (HAL_I2CEx_ConfigAnalogFilter(i2c_handle, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
{
Expand All @@ -88,7 +96,7 @@ static rt_err_t stm32_i2c_init(struct stm32_i2c *i2c_drv)
{
return -RT_EFAULT;
}

#endif /* defined(SOC_SERIES_STM32H7) */
/* I2C2 DMA Init */
if (i2c_drv->i2c_dma_flag & I2C_USING_RX_DMA_FLAG)
{
Expand Down Expand Up @@ -123,7 +131,6 @@ static rt_err_t stm32_i2c_init(struct stm32_i2c *i2c_drv)

static rt_err_t stm32_i2c_configure(struct rt_i2c_bus_device *bus)
{
int ret = -RT_ERROR;
RT_ASSERT(RT_NULL != bus);
struct stm32_i2c *i2c_drv = rt_container_of(bus, struct stm32_i2c, i2c_bus);

Expand Down Expand Up @@ -307,7 +314,9 @@ static rt_ssize_t stm32_i2c_master_xfer(struct rt_i2c_bus_device *bus,
{
LOG_D("I2C NACK Error now stoped");
/* Send stop signal to prevent bus lock-up */
#if defined(SOC_SERIES_STM32H7)
handle->Instance->CR1 |= I2C_IT_STOPI;
#endif /* defined(SOC_SERIES_STM32H7) */
}
if (handle->ErrorCode == HAL_I2C_ERROR_BERR)
{
Expand Down Expand Up @@ -459,6 +468,7 @@ void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c)
}
void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c)
{
#if defined(SOC_SERIES_STM32H7)
/* Send stop signal to prevent bus lock-up */
if (hi2c->ErrorCode == HAL_I2C_ERROR_AF)
{
Expand All @@ -470,6 +480,8 @@ void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c)
LOG_D("I2C BUS Error now stoped");
hi2c->Instance->CR1 |= I2C_IT_STOPI;
}
#endif /* defined(SOC_SERIES_STM32H7) */

}
#ifdef BSP_USING_HARD_I2C1
/**
Expand Down
3 changes: 1 addition & 2 deletions bsp/stm32/libraries/HAL_Drivers/drivers/drv_hard_i2c.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@
#include <drv_common.h>
#include "drv_dma.h"
#include <ipc/completion.h>
#ifdef (RT_USING_I2C && BSP_USING_I2C)
#if defined(RT_USING_I2C) && defined(BSP_USING_I2C)

/* C binding of definitions if building with C++ compiler */
#ifdef __cplusplus
extern "C"
{
#endif


struct stm32_i2c_config
{
const char *name;
Expand Down

0 comments on commit 34fb8d2

Please sign in to comment.