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/9] 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/9] =?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/9] =?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 21aa01bc923c7558785475a9ad6df6fbffb777d2 Mon Sep 17 00:00:00 2001 From: HanLinXing <15655368+HanLinXing@user.noreply.gitee.com> Date: Wed, 16 Apr 2025 11:30:49 +0000 Subject: [PATCH 4/9] add common/iic/MYI2C.c. Signed-off-by: HanLinXing <15655368+HanLinXing@user.noreply.gitee.com> --- common/iic/MYI2C.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 common/iic/MYI2C.c diff --git a/common/iic/MYI2C.c b/common/iic/MYI2C.c new file mode 100644 index 0000000..c50032b --- /dev/null +++ b/common/iic/MYI2C.c @@ -0,0 +1,14 @@ +#include "MYI2C.h" +#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 f744a30d5cf32b4d99ea3482222214fa20c39ff1 Mon Sep 17 00:00:00 2001 From: HanLinXing <15655368+HanLinXing@user.noreply.gitee.com> Date: Wed, 16 Apr 2025 11:31:21 +0000 Subject: [PATCH 5/9] add common/iic/MYI2C.h. Signed-off-by: HanLinXing <15655368+HanLinXing@user.noreply.gitee.com> --- common/iic/MYI2C.h | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 common/iic/MYI2C.h diff --git a/common/iic/MYI2C.h b/common/iic/MYI2C.h new file mode 100644 index 0000000..2211b64 --- /dev/null +++ b/common/iic/MYI2C.h @@ -0,0 +1,36 @@ +#ifndef __MYI2C_H +#define __MYI2C_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 73d926f0a94ba2292419d7c32a2979454f78eab2 Mon Sep 17 00:00:00 2001 From: HanLinXing <15655368+HanLinXing@user.noreply.gitee.com> Date: Wed, 16 Apr 2025 11:57:28 +0000 Subject: [PATCH 6/9] rename common/iic/MYI2C.c to common/iic/Base_I2C.c. Signed-off-by: HanLinXing <15655368+HanLinXing@user.noreply.gitee.com> --- common/iic/{MYI2C.c => Base_I2C.c} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename common/iic/{MYI2C.c => Base_I2C.c} (90%) diff --git a/common/iic/MYI2C.c b/common/iic/Base_I2C.c similarity index 90% rename from common/iic/MYI2C.c rename to common/iic/Base_I2C.c index c50032b..8361d6d 100644 --- a/common/iic/MYI2C.c +++ b/common/iic/Base_I2C.c @@ -1,4 +1,4 @@ -#include "MYI2C.h" +#include "Base_I2C.h" #include "i2c.h" void HAL_I2C_WriteCmd(uint8_t* hi2cx,uint16_t adr,uint8_t* TxData,uint8_t len,uint16_t timeout) -- Gitee From 27ede18919aba9979ff031bb288ae43a5560a6cd Mon Sep 17 00:00:00 2001 From: HanLinXing <15655368+HanLinXing@user.noreply.gitee.com> Date: Wed, 16 Apr 2025 11:58:09 +0000 Subject: [PATCH 7/9] rename common/iic/MYI2C.h to common/iic/Base_I2C.h. Signed-off-by: HanLinXing <15655368+HanLinXing@user.noreply.gitee.com> --- common/iic/{MYI2C.h => Base_I2C.h} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename common/iic/{MYI2C.h => Base_I2C.h} (94%) diff --git a/common/iic/MYI2C.h b/common/iic/Base_I2C.h similarity index 94% rename from common/iic/MYI2C.h rename to common/iic/Base_I2C.h index 2211b64..c39d32c 100644 --- a/common/iic/MYI2C.h +++ b/common/iic/Base_I2C.h @@ -1,5 +1,5 @@ -#ifndef __MYI2C_H -#define __MYI2C_H +#ifndef __Base_I2C_H +#define __Base_I2C_H #ifdef __cplusplus extern "C" { -- Gitee From 6cb20eee4069593c70c02ddad05fcb53b2f4202c Mon Sep 17 00:00:00 2001 From: HanLinXing <15655368+HanLinXing@user.noreply.gitee.com> Date: Thu, 17 Apr 2025 12:00:15 +0000 Subject: [PATCH 8/9] update common/iic/Base_I2C.c. Signed-off-by: HanLinXing <15655368+HanLinXing@user.noreply.gitee.com> --- common/iic/Base_I2C.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/common/iic/Base_I2C.c b/common/iic/Base_I2C.c index 8361d6d..0f63686 100644 --- a/common/iic/Base_I2C.c +++ b/common/iic/Base_I2C.c @@ -1,14 +1,12 @@ #include "Base_I2C.h" #include "i2c.h" -void HAL_I2C_WriteCmd(uint8_t* hi2cx,uint16_t adr,uint8_t* TxData,uint8_t len,uint16_t timeout) +HAL_StatusTypeDef Base_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); + return HAL_I2C_Master_Transmit((I2C_HandleTypeDef*)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_StatusTypeDef Base_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 HAL_I2C_Master_Receive((I2C_HandleTypeDef*)hi2cx, adr, (uint8_t*)RxData, len, timeout); } - -return; -- Gitee From b5087c2a64f19145b012554b88a8697611a0c5ab Mon Sep 17 00:00:00 2001 From: HanLinXing <15655368+HanLinXing@user.noreply.gitee.com> Date: Thu, 17 Apr 2025 12:00:43 +0000 Subject: [PATCH 9/9] update common/iic/Base_I2C.h. Signed-off-by: HanLinXing <15655368+HanLinXing@user.noreply.gitee.com> --- common/iic/Base_I2C.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/common/iic/Base_I2C.h b/common/iic/Base_I2C.h index c39d32c..07c574f 100644 --- a/common/iic/Base_I2C.h +++ b/common/iic/Base_I2C.h @@ -14,23 +14,23 @@ extern "C" { * @param TxData: 指向待发送数据的指针,包含了要写入 I2C 设备的命令或数据内容 * @param len: 要发送数据的长度,即 TxData 中数据的字节数 * @param timeout: 操作超时时间,若在该时间内操作未完成,则操作失败,单位取决于具体实现 - * @retval 无 + * @retval HAL_StatusTypeDef: 操作状态,如 HAL_OK 表示成功,其他值表示失败 */ - void HAL_I2C_WriteCmd(uint8_t* hi2cx,uint16_t adr,uint8_t* TxData,uint8_t len,uint16_t timeout) +HAL_StatusTypeDef Base_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 无 + * @retval HAL_StatusTypeDef: 操作状态,如 HAL_OK 表示成功,其他值表示失败 */ - void HAL_I2C_ReadCmd(uint8_t* hi2cx,uint16_t adr,uint8_t* RxData,uint8_t len,uint16_t timeout) +HAL_StatusTypeDef Base_I2C_ReadCmd(uint8_t* hi2cx, uint16_t adr, uint8_t* RxData, uint8_t len, uint16_t timeout); #ifdef __cplusplus } #endif -#endif /* __I2C_H */ +#endif /* __Base_I2C_H */ -- Gitee