[bsp][stm32] fix hardware i2c driver bug

This commit is contained in:
Dyyt587 2024-04-02 11:21:09 +08:00 committed by GitHub
parent 3a4db99104
commit 34fb8d2f10
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 5 deletions

View File

@ -59,10 +59,18 @@ static rt_err_t stm32_i2c_init(struct stm32_i2c *i2c_drv)
rt_memset(i2c_handle, 0, sizeof(I2C_HandleTypeDef)); rt_memset(i2c_handle, 0, sizeof(I2C_HandleTypeDef));
struct stm32_i2c_config *cfg = i2c_drv->config; struct stm32_i2c_config *cfg = i2c_drv->config;
i2c_handle->Instance = cfg->Instance; i2c_handle->Instance = cfg->Instance;
#if defined(SOC_SERIES_STM32H7)
i2c_handle->Init.Timing = cfg->timing; 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.OwnAddress1 = 0;
i2c_handle->Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; i2c_handle->Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
#if defined(SOC_SERIES_STM32H7)
i2c_handle->Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; i2c_handle->Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
#endif /* defined(SOC_SERIES_STM32H7) */
i2c_handle->Init.OwnAddress2 = 0; i2c_handle->Init.OwnAddress2 = 0;
i2c_handle->Init.OwnAddress2Masks = I2C_OA2_NOMASK; i2c_handle->Init.OwnAddress2Masks = I2C_OA2_NOMASK;
i2c_handle->Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; i2c_handle->Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
@ -76,7 +84,7 @@ static rt_err_t stm32_i2c_init(struct stm32_i2c *i2c_drv)
{ {
return -RT_EFAULT; return -RT_EFAULT;
} }
#if defined(SOC_SERIES_STM32H7)
/* Configure Analogue filter */ /* Configure Analogue filter */
if (HAL_I2CEx_ConfigAnalogFilter(i2c_handle, I2C_ANALOGFILTER_ENABLE) != HAL_OK) if (HAL_I2CEx_ConfigAnalogFilter(i2c_handle, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
{ {
@ -88,7 +96,7 @@ static rt_err_t stm32_i2c_init(struct stm32_i2c *i2c_drv)
{ {
return -RT_EFAULT; return -RT_EFAULT;
} }
#endif /* defined(SOC_SERIES_STM32H7) */
/* I2C2 DMA Init */ /* I2C2 DMA Init */
if (i2c_drv->i2c_dma_flag & I2C_USING_RX_DMA_FLAG) if (i2c_drv->i2c_dma_flag & I2C_USING_RX_DMA_FLAG)
{ {
@ -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) static rt_err_t stm32_i2c_configure(struct rt_i2c_bus_device *bus)
{ {
int ret = -RT_ERROR;
RT_ASSERT(RT_NULL != bus); RT_ASSERT(RT_NULL != bus);
struct stm32_i2c *i2c_drv = rt_container_of(bus, struct stm32_i2c, i2c_bus); struct stm32_i2c *i2c_drv = rt_container_of(bus, struct stm32_i2c, i2c_bus);
@ -307,7 +314,9 @@ out:
{ {
LOG_D("I2C NACK Error now stoped"); LOG_D("I2C NACK Error now stoped");
/* Send stop signal to prevent bus lock-up */ /* Send stop signal to prevent bus lock-up */
#if defined(SOC_SERIES_STM32H7)
handle->Instance->CR1 |= I2C_IT_STOPI; handle->Instance->CR1 |= I2C_IT_STOPI;
#endif /* defined(SOC_SERIES_STM32H7) */
} }
if (handle->ErrorCode == HAL_I2C_ERROR_BERR) if (handle->ErrorCode == HAL_I2C_ERROR_BERR)
{ {
@ -459,6 +468,7 @@ void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c)
} }
void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c) void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c)
{ {
#if defined(SOC_SERIES_STM32H7)
/* Send stop signal to prevent bus lock-up */ /* Send stop signal to prevent bus lock-up */
if (hi2c->ErrorCode == HAL_I2C_ERROR_AF) if (hi2c->ErrorCode == HAL_I2C_ERROR_AF)
{ {
@ -470,6 +480,8 @@ void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c)
LOG_D("I2C BUS Error now stoped"); LOG_D("I2C BUS Error now stoped");
hi2c->Instance->CR1 |= I2C_IT_STOPI; hi2c->Instance->CR1 |= I2C_IT_STOPI;
} }
#endif /* defined(SOC_SERIES_STM32H7) */
} }
#ifdef BSP_USING_HARD_I2C1 #ifdef BSP_USING_HARD_I2C1
/** /**

View File

@ -18,7 +18,7 @@
#include <drv_common.h> #include <drv_common.h>
#include "drv_dma.h" #include "drv_dma.h"
#include <ipc/completion.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 */ /* C binding of definitions if building with C++ compiler */
#ifdef __cplusplus #ifdef __cplusplus
@ -26,7 +26,6 @@ extern "C"
{ {
#endif #endif
struct stm32_i2c_config struct stm32_i2c_config
{ {
const char *name; const char *name;