From c5af7c2649126db954acfb37ff59cf7ad98d2033 Mon Sep 17 00:00:00 2001 From: HanLinXing <1328346221@qq.com> Date: Sat, 12 Apr 2025 20:10:03 +0800 Subject: [PATCH 1/6] I2C --- I2C.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ I2C.h | 22 ++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 I2C.c create mode 100644 I2C.h diff --git a/I2C.c b/I2C.c new file mode 100644 index 0000000..f875322 --- /dev/null +++ b/I2C.c @@ -0,0 +1,85 @@ +#include "I2C.h" + +/** + * @brief 通过 I2C 写入数据 + * @param i2cControl: I2C 句柄 + * @param devAddr: 设备地址 + * @param regAddr: 寄存器地址 + * @param pData: 要写入的数据指针 + * @param len: 数据长度 + * @param timeOverCount: 超时时间 + * @retval HAL 状态 + */ +HAL_StatusTypeDef i2cWrite(I2C_HandleTypeDef i2cControl, uint8_t devAddr, uint16_t regAddr, uint8_t *pData, uint16_t len, uint16_t timeOverCount) +{ + return HAL_I2C_Mem_Write(&i2cControl, devAddr, regAddr, I2C_MEMADD_SIZE_16BIT, pData, len, timeOverCount); +} + +/** + * @brief 通过 I2C 读取数据 + * @param i2cControl: I2C 句柄 + * @param devAddr: 设备地址 + * @param regAddr: 寄存器地址 + * @param pData: 存储读取数据的指针 + * @param len: 数据长度 + * @param timeOverCount: 超时时间 + * @retval HAL 状态 + */ +HAL_StatusTypeDef i2cRead(I2C_HandleTypeDef i2cControl, uint8_t devAddr, uint16_t regAddr, uint8_t *pData, uint16_t len, uint16_t timeOverCount) +{ + return HAL_I2C_Mem_Read(&i2cControl, devAddr, regAddr, I2C_MEMADD_SIZE_16BIT, pData, len, timeOverCount); +} + +/** + * @brief 通过 I2C DMA 写入数据 + * @param i2cControl: I2C 句柄 + * @param devAddr: 设备地址 + * @param regAddr: 寄存器地址 + * @param pData: 要写入的数据指针 + * @param len: 数据长度 + * @param timeOverCount: 超时时间 + * @retval HAL 状态 + */ +HAL_StatusTypeDef i2cWriteDma(I2C_HandleTypeDef i2cControl, uint8_t devAddr, uint16_t regAddr, uint8_t *pData, uint16_t len, uint16_t timeOverCount) +{ + HAL_StatusTypeDef status = HAL_I2C_Mem_Write_DMA(&i2cControl, devAddr, regAddr, I2C_MEMADD_SIZE_16BIT, pData, len); + if (status == HAL_OK) + { + uint32_t tickstart = HAL_GetTick(); + while (HAL_I2C_GetState(&i2cControl) != HAL_I2C_STATE_READY) + { + if ((HAL_GetTick() - tickstart) > timeOverCount) + { + return HAL_TIMEOUT; + } + } + } + return status; +} + +/** + * @brief 通过 I2C DMA 读取数据 + * @param i2cControl: I2C 句柄 + * @param devAddr: 设备地址 + * @param regAddr: 寄存器地址 + * @param pData: 存储读取数据的指针 + * @param len: 数据长度 + * @param timeOverCount: 超时时间 + * @retval HAL 状态 + */ +HAL_StatusTypeDef i2cReadDma(I2C_HandleTypeDef i2cControl, uint8_t devAddr, uint16_t regAddr, uint8_t *pData, uint16_t len, uint16_t timeOverCount) +{ + HAL_StatusTypeDef status = HAL_I2C_Mem_Read_DMA(&i2cControl, devAddr, regAddr, I2C_MEMADD_SIZE_16BIT, pData, len); + if (status == HAL_OK) + { + uint32_t tickstart = HAL_GetTick(); + while (HAL_I2C_GetState(&i2cControl) != HAL_I2C_STATE_READY) + { + if ((HAL_GetTick() - tickstart) > timeOverCount) + { + return HAL_TIMEOUT; + } + } + } + return status; +} diff --git a/I2C.h b/I2C.h new file mode 100644 index 0000000..1328644 --- /dev/null +++ b/I2C.h @@ -0,0 +1,22 @@ +#ifndef __I2C_H +#define __I2C_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include "stm32f1xx_hal.h" + +// 基础读写功能 +HAL_StatusTypeDef i2cWrite(I2C_HandleTypeDef i2cControl, uint8_t devAddr, uint16_t regAddr, uint8_t *pData, uint16_t len, uint16_t timeOverCount = 100); +HAL_StatusTypeDef i2cRead(I2C_HandleTypeDef i2cControl, uint8_t devAddr, uint16_t regAddr, uint8_t *pData, uint16_t len, uint16_t timeOverCount = 100); + +// DMA 读写功能 +HAL_StatusTypeDef i2cWriteDma(I2C_HandleTypeDef i2cControl, uint8_t devAddr, uint16_t regAddr, uint8_t *pData, uint16_t len, uint16_t timeOverCount = 100); +HAL_StatusTypeDef i2cReadDma(I2C_HandleTypeDef i2cControl, uint8_t devAddr, uint16_t regAddr, uint8_t *pData, uint16_t len, uint16_t timeOverCount = 100); + +#ifdef __cplusplus +} +#endif + +#endif /* __I2C_H */ -- Gitee From 7f317dddd19167090ffaa0ed58f9ffe7ad7f6f38 Mon Sep 17 00:00:00 2001 From: HanLinXing <15655368+HanLinXing@user.noreply.gitee.com> Date: Wed, 16 Apr 2025 10:09:17 +0000 Subject: [PATCH 2/6] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20I2C.?= =?UTF-8?q?c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- I2C.c | 85 ----------------------------------------------------------- 1 file changed, 85 deletions(-) delete mode 100644 I2C.c diff --git a/I2C.c b/I2C.c deleted file mode 100644 index f875322..0000000 --- a/I2C.c +++ /dev/null @@ -1,85 +0,0 @@ -#include "I2C.h" - -/** - * @brief 通过 I2C 写入数据 - * @param i2cControl: I2C 句柄 - * @param devAddr: 设备地址 - * @param regAddr: 寄存器地址 - * @param pData: 要写入的数据指针 - * @param len: 数据长度 - * @param timeOverCount: 超时时间 - * @retval HAL 状态 - */ -HAL_StatusTypeDef i2cWrite(I2C_HandleTypeDef i2cControl, uint8_t devAddr, uint16_t regAddr, uint8_t *pData, uint16_t len, uint16_t timeOverCount) -{ - return HAL_I2C_Mem_Write(&i2cControl, devAddr, regAddr, I2C_MEMADD_SIZE_16BIT, pData, len, timeOverCount); -} - -/** - * @brief 通过 I2C 读取数据 - * @param i2cControl: I2C 句柄 - * @param devAddr: 设备地址 - * @param regAddr: 寄存器地址 - * @param pData: 存储读取数据的指针 - * @param len: 数据长度 - * @param timeOverCount: 超时时间 - * @retval HAL 状态 - */ -HAL_StatusTypeDef i2cRead(I2C_HandleTypeDef i2cControl, uint8_t devAddr, uint16_t regAddr, uint8_t *pData, uint16_t len, uint16_t timeOverCount) -{ - return HAL_I2C_Mem_Read(&i2cControl, devAddr, regAddr, I2C_MEMADD_SIZE_16BIT, pData, len, timeOverCount); -} - -/** - * @brief 通过 I2C DMA 写入数据 - * @param i2cControl: I2C 句柄 - * @param devAddr: 设备地址 - * @param regAddr: 寄存器地址 - * @param pData: 要写入的数据指针 - * @param len: 数据长度 - * @param timeOverCount: 超时时间 - * @retval HAL 状态 - */ -HAL_StatusTypeDef i2cWriteDma(I2C_HandleTypeDef i2cControl, uint8_t devAddr, uint16_t regAddr, uint8_t *pData, uint16_t len, uint16_t timeOverCount) -{ - HAL_StatusTypeDef status = HAL_I2C_Mem_Write_DMA(&i2cControl, devAddr, regAddr, I2C_MEMADD_SIZE_16BIT, pData, len); - if (status == HAL_OK) - { - uint32_t tickstart = HAL_GetTick(); - while (HAL_I2C_GetState(&i2cControl) != HAL_I2C_STATE_READY) - { - if ((HAL_GetTick() - tickstart) > timeOverCount) - { - return HAL_TIMEOUT; - } - } - } - return status; -} - -/** - * @brief 通过 I2C DMA 读取数据 - * @param i2cControl: I2C 句柄 - * @param devAddr: 设备地址 - * @param regAddr: 寄存器地址 - * @param pData: 存储读取数据的指针 - * @param len: 数据长度 - * @param timeOverCount: 超时时间 - * @retval HAL 状态 - */ -HAL_StatusTypeDef i2cReadDma(I2C_HandleTypeDef i2cControl, uint8_t devAddr, uint16_t regAddr, uint8_t *pData, uint16_t len, uint16_t timeOverCount) -{ - HAL_StatusTypeDef status = HAL_I2C_Mem_Read_DMA(&i2cControl, devAddr, regAddr, I2C_MEMADD_SIZE_16BIT, pData, len); - if (status == HAL_OK) - { - uint32_t tickstart = HAL_GetTick(); - while (HAL_I2C_GetState(&i2cControl) != HAL_I2C_STATE_READY) - { - if ((HAL_GetTick() - tickstart) > timeOverCount) - { - return HAL_TIMEOUT; - } - } - } - return status; -} -- Gitee From 8d017aad58f7e5bfd8bd8e9cfa5efaa49db872e0 Mon Sep 17 00:00:00 2001 From: HanLinXing <15655368+HanLinXing@user.noreply.gitee.com> Date: Wed, 16 Apr 2025 10:09:26 +0000 Subject: [PATCH 3/6] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20I2C.?= =?UTF-8?q?h?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- I2C.h | 22 ---------------------- 1 file changed, 22 deletions(-) delete mode 100644 I2C.h diff --git a/I2C.h b/I2C.h deleted file mode 100644 index 1328644..0000000 --- a/I2C.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef __I2C_H -#define __I2C_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include "stm32f1xx_hal.h" - -// 基础读写功能 -HAL_StatusTypeDef i2cWrite(I2C_HandleTypeDef i2cControl, uint8_t devAddr, uint16_t regAddr, uint8_t *pData, uint16_t len, uint16_t timeOverCount = 100); -HAL_StatusTypeDef i2cRead(I2C_HandleTypeDef i2cControl, uint8_t devAddr, uint16_t regAddr, uint8_t *pData, uint16_t len, uint16_t timeOverCount = 100); - -// DMA 读写功能 -HAL_StatusTypeDef i2cWriteDma(I2C_HandleTypeDef i2cControl, uint8_t devAddr, uint16_t regAddr, uint8_t *pData, uint16_t len, uint16_t timeOverCount = 100); -HAL_StatusTypeDef i2cReadDma(I2C_HandleTypeDef i2cControl, uint8_t devAddr, uint16_t regAddr, uint8_t *pData, uint16_t len, uint16_t timeOverCount = 100); - -#ifdef __cplusplus -} -#endif - -#endif /* __I2C_H */ -- Gitee From 61bc960df864a25a6a8ddede6f145c5a90e3684e Mon Sep 17 00:00:00 2001 From: HanLinXing <15655368+HanLinXing@user.noreply.gitee.com> Date: Wed, 16 Apr 2025 10:55:49 +0000 Subject: [PATCH 4/6] add common/iic/I2c.c. Signed-off-by: HanLinXing <15655368+HanLinXing@user.noreply.gitee.com> --- common/iic/I2c.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 common/iic/I2c.c diff --git a/common/iic/I2c.c b/common/iic/I2c.c new file mode 100644 index 0000000..dd63551 --- /dev/null +++ b/common/iic/I2c.c @@ -0,0 +1,13 @@ +#include "I2C.h" + +void HAL_I2C_WriteCmd(uint8_t* hi2cx,uint16_t adr,uint8_t* TxData,uint8_t len,uint16_t timeout) +{ + HAL_I2C_Master_Transmit(&hi2cx,adr,(uint8_t*)TxData,len,timeout); +} + +void HAL_I2C_ReadCmd(uint8_t* hi2cx,uint16_t adr,uint8_t* RxData,uint8_t len,uint16_t timeout) +{ + HAL_I2C_Master_Receive(&hi2cx,adr,(uint8_t*)RxData,len,timeout); +} + +return; -- Gitee From 574442e272904912ff6762e31af3ab22fa83b852 Mon Sep 17 00:00:00 2001 From: HanLinXing <15655368+HanLinXing@user.noreply.gitee.com> Date: Wed, 16 Apr 2025 10:56:22 +0000 Subject: [PATCH 5/6] add common/iic/iic.h. Signed-off-by: HanLinXing <15655368+HanLinXing@user.noreply.gitee.com> --- common/iic/iic.h | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 common/iic/iic.h diff --git a/common/iic/iic.h b/common/iic/iic.h new file mode 100644 index 0000000..722d8f9 --- /dev/null +++ b/common/iic/iic.h @@ -0,0 +1,36 @@ +#ifndef __I2C_H +#define __I2C_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include "stm32f1xx_hal.h" + +/** + * @brief 通过 I2C 接口写入命令数据 + * @param hi2cx: 指向 I2C 句柄的指针,用于指定使用的 I2C 总线实例 + * @param adr: 目标设备的 I2C 地址,标识要与之通信的从设备 + * @param TxData: 指向待发送数据的指针,包含了要写入 I2C 设备的命令或数据内容 + * @param len: 要发送数据的长度,即 TxData 中数据的字节数 + * @param timeout: 操作超时时间,若在该时间内操作未完成,则操作失败,单位取决于具体实现 + * @retval 无 + */ + void HAL_I2C_WriteCmd(uint8_t* hi2cx,uint16_t adr,uint8_t* TxData,uint8_t len,uint16_t timeout) + + /** + * @brief 通过 I2C 接口读取命令响应数据 + * @param hi2cx: 指向 I2C 句柄的指针,用于指定使用的 I2C 总线实例 + * @param adr: 目标设备的 I2C 地址,标识要与之通信的从设备 + * @param RxData: 指向用于存储接收到数据的缓冲区的指针,读取到的数据会存于此 + * @param len: 要读取数据的长度,即期望从 I2C 设备读取的字节数 + * @param timeout: 操作超时时间,若在该时间内操作未完成,则操作失败,单位取决于具体实现 + * @retval 无 + */ + void HAL_I2C_ReadCmd(uint8_t* hi2cx,uint16_t adr,uint8_t* RxData,uint8_t len,uint16_t timeout) + +#ifdef __cplusplus +} +#endif + +#endif /* __I2C_H */ -- Gitee From 1f2a308b93843f1e61942025c778addd4445d69b Mon Sep 17 00:00:00 2001 From: HanLinXing <15655368+HanLinXing@user.noreply.gitee.com> Date: Wed, 16 Apr 2025 10:56:36 +0000 Subject: [PATCH 6/6] rename common/iic/I2c.c to common/iic/iic.c. Signed-off-by: HanLinXing <15655368+HanLinXing@user.noreply.gitee.com> --- common/iic/{I2c.c => iic.c} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename common/iic/{I2c.c => iic.c} (100%) diff --git a/common/iic/I2c.c b/common/iic/iic.c similarity index 100% rename from common/iic/I2c.c rename to common/iic/iic.c -- Gitee