[bsp][stm32] format code of i2c hardware drivers

This commit is contained in:
Meco Man 2024-03-07 23:14:41 -05:00 committed by Rbb666
parent 4e1626703f
commit b85c4cade2
4 changed files with 44 additions and 47 deletions

View File

@ -23,7 +23,7 @@ extern "C" {
.Instance = I2C1, \ .Instance = I2C1, \
.timing=0x10707DBC, \ .timing=0x10707DBC, \
.timeout=0x1000, \ .timeout=0x1000, \
.name = "i2c1", \ .name = "hwi2c1", \
.evirq_type = I2C1_EV_IRQn, \ .evirq_type = I2C1_EV_IRQn, \
.erirq_type = I2C1_ER_IRQn, \ .erirq_type = I2C1_ER_IRQn, \
} }
@ -61,7 +61,7 @@ extern "C" {
.Instance = I2C2, \ .Instance = I2C2, \
.timing=0x10707DBC, \ .timing=0x10707DBC, \
.timeout=0x1000, \ .timeout=0x1000, \
.name = "i2c2", \ .name = "hwi2c2", \
.evirq_type = I2C2_EV_IRQn, \ .evirq_type = I2C2_EV_IRQn, \
.erirq_type = I2C2_ER_IRQn, \ .erirq_type = I2C2_ER_IRQn, \
} }
@ -99,7 +99,7 @@ extern "C" {
.Instance = I2C3, \ .Instance = I2C3, \
.timing=0x10707DBC, \ .timing=0x10707DBC, \
.timeout=0x1000, \ .timeout=0x1000, \
.name = "i2c3", \ .name = "hwi2c3", \
.evirq_type = I2C3_EV_IRQn, \ .evirq_type = I2C3_EV_IRQn, \
.erirq_type = I2C3_ER_IRQn, \ .erirq_type = I2C3_ER_IRQn, \
} }

View File

@ -60,6 +60,7 @@ extern "C" {
#include "f4/tim_config.h" #include "f4/tim_config.h"
#include "f4/sdio_config.h" #include "f4/sdio_config.h"
#include "f4/pwm_config.h" #include "f4/pwm_config.h"
#include "f4/i2c_hard_config.h"
#include "f4/pulse_encoder_config.h" #include "f4/pulse_encoder_config.h"
#elif defined(SOC_SERIES_STM32F7) #elif defined(SOC_SERIES_STM32F7)
#include "f7/dma_config.h" #include "f7/dma_config.h"

View File

@ -11,18 +11,15 @@
#include <rtthread.h> #include <rtthread.h>
#include <rthw.h> #include <rthw.h>
#include <board.h> #include <board.h>
#include <rtconfig.h>
#include "drv_hard_i2c.h" #include "drv_hard_i2c.h"
#include "drv_config.h" #include "drv_config.h"
#include "i2c_hard_config.h"
#include <string.h> #include <string.h>
#ifdef (RT_USING_I2C && BSP_USING_I2C)
/* not fully support for I2C4 */ /* not fully support for I2C4 */
#if defined(BSP_USING_HARD_I2C1) || defined(BSP_USING_HARD_I2C2) || defined(BSP_USING_HARD_I2C3) #if defined(BSP_USING_HARD_I2C1) || defined(BSP_USING_HARD_I2C2) || defined(BSP_USING_HARD_I2C3)
//#define DRV_DEBUG //#define DRV_DEBUG
#define LOG_TAG "drv.i2c" #define LOG_TAG "drv.i2c.hw"
#include <drv_log.h> #include <drv_log.h>
enum enum
@ -36,11 +33,10 @@ enum
#ifdef BSP_USING_HARD_I2C3 #ifdef BSP_USING_HARD_I2C3
I2C3_INDEX, I2C3_INDEX,
#endif /* BSP_USING_HARD_I2C3 */ #endif /* BSP_USING_HARD_I2C3 */
}; };
static struct stm32_i2c_config i2c_config[] = static struct stm32_i2c_config i2c_config[] =
{ {
#ifdef BSP_USING_HARD_I2C1 #ifdef BSP_USING_HARD_I2C1
I2C1_BUS_CONFIG, I2C1_BUS_CONFIG,
#endif /* BSP_USING_HARD_I2C1 */ #endif /* BSP_USING_HARD_I2C1 */
@ -50,7 +46,6 @@ static struct stm32_i2c_config i2c_config[] =
#ifdef BSP_USING_HARD_I2C3 #ifdef BSP_USING_HARD_I2C3
I2C3_BUS_CONFIG, I2C3_BUS_CONFIG,
#endif /* BSP_USING_HARD_I2C3 */ #endif /* BSP_USING_HARD_I2C3 */
}; };
static struct stm32_i2c i2c_objs[sizeof(i2c_config) / sizeof(i2c_config[0])] = {0}; static struct stm32_i2c i2c_objs[sizeof(i2c_config) / sizeof(i2c_config[0])] = {0};
@ -136,7 +131,7 @@ static rt_err_t stm32_i2c_configure(struct rt_i2c_bus_device *bus)
} }
/** /**
* @brief Hardware I2C driver transfer * @brief Hardware I2C driver transfer
* *
* @param bus Device bus * @param bus Device bus
* @param msgs Data to be transferred * @param msgs Data to be transferred
* @param num Number of data * @param num Number of data
@ -157,6 +152,7 @@ static rt_ssize_t stm32_i2c_master_xfer(struct rt_i2c_bus_device *bus,
uint32_t mode = 0; uint32_t mode = 0;
uint8_t next_flag = 0; uint8_t next_flag = 0;
struct rt_completion *completion; struct rt_completion *completion;
rt_uint32_t timeout;
if (num == 0) if (num == 0)
{ {
return 0; return 0;
@ -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); i2c_obj = rt_container_of(bus, struct stm32_i2c, i2c_bus);
completion = &i2c_obj->completion; completion = &i2c_obj->completion;
I2C_HandleTypeDef *handle = &i2c_obj->handle; I2C_HandleTypeDef *handle = &i2c_obj->handle;
rt_uint32_t timeout;
LOG_D("xfer start %d mags", num); LOG_D("xfer start %d mags", num);
for (i = 0; i < (num - 1); i++) for (i = 0; i < (num - 1); i++)
{ {
mode = 0; mode = 0;
msg = &msgs[i]; 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); 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_msg = &msgs[i + 1];
next_flag = next_msg->flags; 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_NO_START)
{ {
if ((next_flag & RT_I2C_RD) == (msg->flags & RT_I2C_RD)) if ((next_flag & RT_I2C_RD) == (msg->flags & RT_I2C_RD))
@ -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) if (rt_completion_wait(completion, timeout) != RT_EOK)
{ {
LOG_D("receive time out"); LOG_D("receive time out");
goto out; goto out;
} }
} }
@ -240,7 +235,7 @@ static rt_ssize_t stm32_i2c_master_xfer(struct rt_i2c_bus_device *bus,
if (rt_completion_wait(completion, timeout) != RT_EOK) if (rt_completion_wait(completion, timeout) != RT_EOK)
{ {
LOG_D("transmit time out"); LOG_D("transmit time out");
goto out; goto out;
} }
} }
@ -248,7 +243,7 @@ static rt_ssize_t stm32_i2c_master_xfer(struct rt_i2c_bus_device *bus,
} }
/* last msg */ /* last msg */
msg = &msgs[i]; msg = &msgs[i];
timeout = msg->len/TRANS_TIMEOUT_PERSEC+1; timeout = msg->len/TRANS_TIMEOUT_PERSEC+1;
if (msg->flags & RT_I2C_NO_STOP) if (msg->flags & RT_I2C_NO_STOP)
mode = I2C_LAST_FRAME_NO_STOP; mode = I2C_LAST_FRAME_NO_STOP;
else else
@ -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) if (rt_completion_wait(completion, timeout) != RT_EOK)
{ {
LOG_D("receive time out"); LOG_D("receive time out");
goto out; goto out;
} }
} }
else else
@ -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) if (rt_completion_wait(completion, timeout) != RT_EOK)
{ {
LOG_D("transmit time out"); LOG_D("transmit time out");
goto out; goto out;
} }
} }
ret = num; ret = num;
LOG_D("xfer end %d mags\r\n", num); LOG_D("xfer end %d mags\r\n", num);
return ret; return ret;
out:
out:
if (handle->ErrorCode == HAL_I2C_ERROR_AF) if (handle->ErrorCode == HAL_I2C_ERROR_AF)
{ {
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 */
handle->Instance->CR1 |= I2C_IT_STOPI; handle->Instance->CR1 |= I2C_IT_STOPI;
} }
if (handle->ErrorCode == HAL_I2C_ERROR_BERR) if (handle->ErrorCode == HAL_I2C_ERROR_BERR)
{ {
LOG_D("I2C BUS Error now stoped"); LOG_D("I2C BUS Error now stoped");
handle->Instance->CR1 |= I2C_IT_STOPI; handle->Instance->CR1 |= I2C_IT_STOPI;
ret=i-1; ret=i-1;
return ret; return ret;
} }
static const struct rt_i2c_bus_device_ops stm32_i2c_ops = static const struct rt_i2c_bus_device_ops stm32_i2c_ops =
{ {
.master_xfer = stm32_i2c_master_xfer, .master_xfer = stm32_i2c_master_xfer,
RT_NULL, RT_NULL,
RT_NULL}; RT_NULL
};
int RT_hw_i2c_bus_init(void) int RT_hw_i2c_bus_init(void)
{ {
@ -468,12 +463,12 @@ void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c)
if (hi2c->ErrorCode == HAL_I2C_ERROR_AF) if (hi2c->ErrorCode == HAL_I2C_ERROR_AF)
{ {
LOG_D("I2C NACK Error now stoped"); 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) if (hi2c->ErrorCode == HAL_I2C_ERROR_BERR)
{ {
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;
} }
} }
#ifdef BSP_USING_HARD_I2C1 #ifdef BSP_USING_HARD_I2C1
@ -594,7 +589,8 @@ void I2C1_DMA_RX_IRQHandler(void)
/* leave interrupt */ /* leave interrupt */
rt_interrupt_leave(); 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) #if defined(BSP_USING_HARD_I2C1) && defined(BSP_I2C1_TX_USING_DMA)
/** /**
* @brief This function handles DMA Rx interrupt request. * @brief This function handles DMA Rx interrupt request.
@ -611,7 +607,8 @@ void I2C1_DMA_TX_IRQHandler(void)
/* leave interrupt */ /* leave interrupt */
rt_interrupt_leave(); 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) #if defined(BSP_USING_HARD_I2C2) && defined(BSP_I2C2_RX_USING_DMA)
/** /**
* @brief This function handles DMA Rx interrupt request. * @brief This function handles DMA Rx interrupt request.
@ -628,7 +625,8 @@ void I2C2_DMA_RX_IRQHandler(void)
/* leave interrupt */ /* leave interrupt */
rt_interrupt_leave(); 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) #if defined(BSP_USING_HARD_I2C2) && defined(BSP_I2C2_TX_USING_DMA)
/** /**
* @brief This function handles DMA Rx interrupt request. * @brief This function handles DMA Rx interrupt request.
@ -645,7 +643,8 @@ void I2C2_DMA_TX_IRQHandler(void)
/* leave interrupt */ /* leave interrupt */
rt_interrupt_leave(); 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) #if defined(BSP_USING_HARD_I2C3) && defined(BSP_I2C3_RX_USING_DMA)
/** /**
* @brief This function handles DMA Rx interrupt request. * @brief This function handles DMA Rx interrupt request.
@ -662,7 +661,8 @@ void I2C3_DMA_RX_IRQHandler(void)
/* leave interrupt */ /* leave interrupt */
rt_interrupt_leave(); 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) #if defined(BSP_USING_HARD_I2C3) && defined(BSP_I2C3_TX_USING_DMA)
/** /**
* @brief This function handles DMA Rx interrupt request. * @brief This function handles DMA Rx interrupt request.
@ -679,7 +679,8 @@ void I2C3_DMA_TX_IRQHandler(void)
/* leave interrupt */ /* leave interrupt */
rt_interrupt_leave(); 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) #if defined(BSP_USING_I2C4) && defined(BSP_I2C4_RX_USING_DMA)
/** /**
* @brief This function handles DMA Rx interrupt request. * @brief This function handles DMA Rx interrupt request.
@ -696,7 +697,8 @@ void I2C4_DMA_RX_IRQHandler(void)
/* leave interrupt */ /* leave interrupt */
rt_interrupt_leave(); 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) #if defined(BSP_USING_I2C4) && defined(BSP_I2C4_TX_USING_DMA)
/** /**
* @brief This function handles DMA Rx interrupt request. * @brief This function handles DMA Rx interrupt request.
@ -722,5 +724,4 @@ int rt_hw_hw_i2c_init(void)
} }
INIT_CORE_EXPORT(rt_hw_hw_i2c_init); INIT_CORE_EXPORT(rt_hw_hw_i2c_init);
#endif #endif /* defined(BSP_USING_HARD_I2C1) || defined(BSP_USING_HARD_I2C2) || defined(BSP_USING_HARD_I2C3) */
#endif /* RT_USING_I2C */

View File

@ -12,17 +12,12 @@
#include "drv_soft_i2c.h" #include "drv_soft_i2c.h"
#include "drv_config.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 DRV_DEBUG
#define LOG_TAG "drv.i2c" #define LOG_TAG "drv.i2c.sw"
#include <drv_log.h> #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[] = static const struct stm32_soft_i2c_config soft_i2c_config[] =
{ {
#ifdef BSP_USING_I2C1 #ifdef BSP_USING_I2C1
@ -183,4 +178,4 @@ int rt_hw_i2c_init(void)
} }
INIT_BOARD_EXPORT(rt_hw_i2c_init); 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) */