Skip to content

Commit

Permalink
[bsp][stm32] format code of i2c hardware drivers
Browse files Browse the repository at this point in the history
  • Loading branch information
mysterywolf authored and Rbb666 committed Mar 8, 2024
1 parent 4e16267 commit b85c4ca
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 47 deletions.
Expand Up @@ -23,7 +23,7 @@ extern "C" {
.Instance = I2C1, \
.timing=0x10707DBC, \
.timeout=0x1000, \
.name = "i2c1", \
.name = "hwi2c1", \
.evirq_type = I2C1_EV_IRQn, \
.erirq_type = I2C1_ER_IRQn, \
}
Expand Down Expand Up @@ -61,7 +61,7 @@ extern "C" {
.Instance = I2C2, \
.timing=0x10707DBC, \
.timeout=0x1000, \
.name = "i2c2", \
.name = "hwi2c2", \
.evirq_type = I2C2_EV_IRQn, \
.erirq_type = I2C2_ER_IRQn, \
}
Expand Down Expand Up @@ -99,7 +99,7 @@ extern "C" {
.Instance = I2C3, \
.timing=0x10707DBC, \
.timeout=0x1000, \
.name = "i2c3", \
.name = "hwi2c3", \
.evirq_type = I2C3_EV_IRQn, \
.erirq_type = I2C3_ER_IRQn, \
}
Expand Down
1 change: 1 addition & 0 deletions bsp/stm32/libraries/HAL_Drivers/drivers/drv_config.h
Expand Up @@ -60,6 +60,7 @@ extern "C" {
#include "f4/tim_config.h"
#include "f4/sdio_config.h"
#include "f4/pwm_config.h"
#include "f4/i2c_hard_config.h"
#include "f4/pulse_encoder_config.h"
#elif defined(SOC_SERIES_STM32F7)
#include "f7/dma_config.h"
Expand Down
73 changes: 37 additions & 36 deletions bsp/stm32/libraries/HAL_Drivers/drivers/drv_hard_i2c.c
Expand Up @@ -11,18 +11,15 @@
#include <rtthread.h>
#include <rthw.h>
#include <board.h>
#include <rtconfig.h>
#include "drv_hard_i2c.h"
#include "drv_config.h"
#include "i2c_hard_config.h"
#include <string.h>

#ifdef (RT_USING_I2C && BSP_USING_I2C)
/* not fully support for I2C4 */
#if defined(BSP_USING_HARD_I2C1) || defined(BSP_USING_HARD_I2C2) || defined(BSP_USING_HARD_I2C3)

//#define DRV_DEBUG
#define LOG_TAG "drv.i2c"
#define LOG_TAG "drv.i2c.hw"
#include <drv_log.h>

enum
Expand All @@ -36,11 +33,10 @@ enum
#ifdef BSP_USING_HARD_I2C3
I2C3_INDEX,
#endif /* BSP_USING_HARD_I2C3 */

};

static struct stm32_i2c_config i2c_config[] =
{
{
#ifdef BSP_USING_HARD_I2C1
I2C1_BUS_CONFIG,
#endif /* BSP_USING_HARD_I2C1 */
Expand All @@ -50,7 +46,6 @@ static struct stm32_i2c_config i2c_config[] =
#ifdef BSP_USING_HARD_I2C3
I2C3_BUS_CONFIG,
#endif /* BSP_USING_HARD_I2C3 */

};

static struct stm32_i2c i2c_objs[sizeof(i2c_config) / sizeof(i2c_config[0])] = {0};
Expand Down Expand Up @@ -136,7 +131,7 @@ static rt_err_t stm32_i2c_configure(struct rt_i2c_bus_device *bus)
}
/**
* @brief Hardware I2C driver transfer
*
*
* @param bus Device bus
* @param msgs Data to be transferred
* @param num Number of data
Expand All @@ -157,6 +152,7 @@ static rt_ssize_t stm32_i2c_master_xfer(struct rt_i2c_bus_device *bus,
uint32_t mode = 0;
uint8_t next_flag = 0;
struct rt_completion *completion;
rt_uint32_t timeout;
if (num == 0)
{
return 0;
Expand All @@ -165,17 +161,16 @@ static rt_ssize_t stm32_i2c_master_xfer(struct rt_i2c_bus_device *bus,
i2c_obj = rt_container_of(bus, struct stm32_i2c, i2c_bus);
completion = &i2c_obj->completion;
I2C_HandleTypeDef *handle = &i2c_obj->handle;
rt_uint32_t timeout;

LOG_D("xfer start %d mags", num);
for (i = 0; i < (num - 1); i++)
{
mode = 0;

msg = &msgs[i];
LOG_D("xfer msgs[%d] addr=0x%2x buf=0x%x len= 0x%x flags= 0x%x", i, msg->addr, msg->buf, msg->len, msg->flags);
next_msg = &msgs[i + 1];
next_flag = next_msg->flags;
timeout = msg->len/TRANS_TIMEOUT_PERSEC+1;
timeout = msg->len/TRANS_TIMEOUT_PERSEC + 1;
if (next_flag & RT_I2C_NO_START)
{
if ((next_flag & RT_I2C_RD) == (msg->flags & RT_I2C_RD))
Expand Down Expand Up @@ -215,7 +210,7 @@ static rt_ssize_t stm32_i2c_master_xfer(struct rt_i2c_bus_device *bus,
if (rt_completion_wait(completion, timeout) != RT_EOK)
{
LOG_D("receive time out");
goto out;
goto out;

}
}
Expand All @@ -240,15 +235,15 @@ static rt_ssize_t stm32_i2c_master_xfer(struct rt_i2c_bus_device *bus,
if (rt_completion_wait(completion, timeout) != RT_EOK)
{
LOG_D("transmit time out");
goto out;
goto out;

}
}
LOG_D("xfer next msgs[%d] addr=0x%2x buf= 0x%x len= 0x%x flags = 0x%x\r\n", i + 1, next_msg->addr, next_msg->buf, next_msg->len, next_msg->flags);
}
/* last msg */
msg = &msgs[i];
timeout = msg->len/TRANS_TIMEOUT_PERSEC+1;
timeout = msg->len/TRANS_TIMEOUT_PERSEC+1;
if (msg->flags & RT_I2C_NO_STOP)
mode = I2C_LAST_FRAME_NO_STOP;
else
Expand All @@ -275,8 +270,7 @@ static rt_ssize_t stm32_i2c_master_xfer(struct rt_i2c_bus_device *bus,
if (rt_completion_wait(completion, timeout) != RT_EOK)
{
LOG_D("receive time out");
goto out;

goto out;
}
}
else
Expand All @@ -300,34 +294,35 @@ static rt_ssize_t stm32_i2c_master_xfer(struct rt_i2c_bus_device *bus,
if (rt_completion_wait(completion, timeout) != RT_EOK)
{
LOG_D("transmit time out");
goto out;
goto out;

}
}
ret = num;
LOG_D("xfer end %d mags\r\n", num);
return ret;
out:

out:
if (handle->ErrorCode == HAL_I2C_ERROR_AF)
{
LOG_D("I2C NACK Error now stoped");
/* Send stop signal to prevent bus lock-up */
handle->Instance->CR1 |= I2C_IT_STOPI;
handle->Instance->CR1 |= I2C_IT_STOPI;
}
if (handle->ErrorCode == HAL_I2C_ERROR_BERR)
{
LOG_D("I2C BUS Error now stoped");
handle->Instance->CR1 |= I2C_IT_STOPI;
ret=i-1;
handle->Instance->CR1 |= I2C_IT_STOPI;
ret=i-1;
return ret;
}

static const struct rt_i2c_bus_device_ops stm32_i2c_ops =
{
.master_xfer = stm32_i2c_master_xfer,
RT_NULL,
RT_NULL};
{
.master_xfer = stm32_i2c_master_xfer,
RT_NULL,
RT_NULL
};

int RT_hw_i2c_bus_init(void)
{
Expand Down Expand Up @@ -468,12 +463,12 @@ void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c)
if (hi2c->ErrorCode == HAL_I2C_ERROR_AF)
{
LOG_D("I2C NACK Error now stoped");
hi2c->Instance->CR1 |= I2C_IT_STOPI;
hi2c->Instance->CR1 |= I2C_IT_STOPI;
}
if (hi2c->ErrorCode == HAL_I2C_ERROR_BERR)
{
LOG_D("I2C BUS Error now stoped");
hi2c->Instance->CR1 |= I2C_IT_STOPI;
hi2c->Instance->CR1 |= I2C_IT_STOPI;
}
}
#ifdef BSP_USING_HARD_I2C1
Expand Down Expand Up @@ -594,7 +589,8 @@ void I2C1_DMA_RX_IRQHandler(void)
/* leave interrupt */
rt_interrupt_leave();
}
#endif /* BSP_USING_HARD_I2C1 && BSP_I2C1_RX_USING_DMA */
#endif /* defined(BSP_USING_HARD_I2C1) && defined(BSP_I2C1_RX_USING_DMA) */

#if defined(BSP_USING_HARD_I2C1) && defined(BSP_I2C1_TX_USING_DMA)
/**
* @brief This function handles DMA Rx interrupt request.
Expand All @@ -611,7 +607,8 @@ void I2C1_DMA_TX_IRQHandler(void)
/* leave interrupt */
rt_interrupt_leave();
}
#endif /* BSP_USING_HARD_I2C1 && BSP_I2C1_TX_USING_DMA */
#endif /* defined(BSP_USING_HARD_I2C1) && defined(BSP_I2C1_TX_USING_DMA) */

#if defined(BSP_USING_HARD_I2C2) && defined(BSP_I2C2_RX_USING_DMA)
/**
* @brief This function handles DMA Rx interrupt request.
Expand All @@ -628,7 +625,8 @@ void I2C2_DMA_RX_IRQHandler(void)
/* leave interrupt */
rt_interrupt_leave();
}
#endif /* BSP_USING_HARD_I2C2 && BSP_I2C2_RX_USING_DMA */
#endif /* defined(BSP_USING_HARD_I2C2) && defined(BSP_I2C2_RX_USING_DMA) */

#if defined(BSP_USING_HARD_I2C2) && defined(BSP_I2C2_TX_USING_DMA)
/**
* @brief This function handles DMA Rx interrupt request.
Expand All @@ -645,7 +643,8 @@ void I2C2_DMA_TX_IRQHandler(void)
/* leave interrupt */
rt_interrupt_leave();
}
#endif /* BSP_USING_HARD_I2C2 && BSP_I2C2_TX_USING_DMA */
#endif /* defined(BSP_USING_HARD_I2C2) && defined(BSP_I2C2_TX_USING_DMA) */

#if defined(BSP_USING_HARD_I2C3) && defined(BSP_I2C3_RX_USING_DMA)
/**
* @brief This function handles DMA Rx interrupt request.
Expand All @@ -662,7 +661,8 @@ void I2C3_DMA_RX_IRQHandler(void)
/* leave interrupt */
rt_interrupt_leave();
}
#endif /* BSP_USING_HARD_I2C3 && BSP_I2C3_RX_USING_DMA */
#endif /* defined(BSP_USING_HARD_I2C3) && defined(BSP_I2C3_RX_USING_DMA) */

#if defined(BSP_USING_HARD_I2C3) && defined(BSP_I2C3_TX_USING_DMA)
/**
* @brief This function handles DMA Rx interrupt request.
Expand All @@ -679,7 +679,8 @@ void I2C3_DMA_TX_IRQHandler(void)
/* leave interrupt */
rt_interrupt_leave();
}
#endif /* BSP_USING_HARD_I2C3 && BSP_I2C3_TX_USING_DMA */
#endif /* defined(BSP_USING_HARD_I2C3) && defined(BSP_I2C3_TX_USING_DMA) */

#if defined(BSP_USING_I2C4) && defined(BSP_I2C4_RX_USING_DMA)
/**
* @brief This function handles DMA Rx interrupt request.
Expand All @@ -696,7 +697,8 @@ void I2C4_DMA_RX_IRQHandler(void)
/* leave interrupt */
rt_interrupt_leave();
}
#endif /* BSP_USING_I2C4 && BSP_I2C4_RX_USING_DMA */
#endif /* defined(BSP_USING_I2C4) && defined(BSP_I2C4_RX_USING_DMA) */

#if defined(BSP_USING_I2C4) && defined(BSP_I2C4_TX_USING_DMA)
/**
* @brief This function handles DMA Rx interrupt request.
Expand All @@ -722,5 +724,4 @@ int rt_hw_hw_i2c_init(void)
}
INIT_CORE_EXPORT(rt_hw_hw_i2c_init);

#endif
#endif /* RT_USING_I2C */
#endif /* defined(BSP_USING_HARD_I2C1) || defined(BSP_USING_HARD_I2C2) || defined(BSP_USING_HARD_I2C3) */
11 changes: 3 additions & 8 deletions bsp/stm32/libraries/HAL_Drivers/drivers/drv_soft_i2c.c
Expand Up @@ -12,17 +12,12 @@
#include "drv_soft_i2c.h"
#include "drv_config.h"

#ifdef RT_USING_I2C
#if defined(BSP_USING_I2C1) || defined(BSP_USING_I2C2) || defined(BSP_USING_I2C3) || defined(BSP_USING_I2C4)

//#define DRV_DEBUG
#define LOG_TAG "drv.i2c"
#define LOG_TAG "drv.i2c.sw"
#include <drv_log.h>

#if !defined(BSP_USING_I2C1) && !defined(BSP_USING_I2C2) && !defined(BSP_USING_I2C3) && !defined(BSP_USING_I2C4)
#error "Please define at least one BSP_USING_I2Cx"
/* this driver can be disabled at menuconfig -> RT-Thread Components -> Device Drivers */
#endif

static const struct stm32_soft_i2c_config soft_i2c_config[] =
{
#ifdef BSP_USING_I2C1
Expand Down Expand Up @@ -183,4 +178,4 @@ int rt_hw_i2c_init(void)
}
INIT_BOARD_EXPORT(rt_hw_i2c_init);

#endif /* RT_USING_I2C */
#endif /* defined(BSP_USING_I2C1) || defined(BSP_USING_I2C2) || defined(BSP_USING_I2C3) || defined(BSP_USING_I2C4) */

0 comments on commit b85c4ca

Please sign in to comment.