From dde90dc1629dadb4f2aca7e104bffd7d3db7dc01 Mon Sep 17 00:00:00 2001 From: yelvlab Date: Thu, 5 Dec 2024 19:54:31 +0800 Subject: [PATCH] =?UTF-8?q?iic=E9=A9=B1=E5=8A=A8=E9=87=8D=E5=86=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 1 - inc/LDC1612.h | 4 + inc/i2c.h | 15 ---- src/LDC1612.c | 215 +++++++++++++++++++++++++++++++++++++++++++++---- src/i2c.c | 78 ------------------ src/main.c | 12 ++- 6 files changed, 215 insertions(+), 110 deletions(-) delete mode 100644 inc/i2c.h delete mode 100644 src/i2c.c diff --git a/CMakeLists.txt b/CMakeLists.txt index e42d494..fe82fc0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,7 +27,6 @@ set(TARGET_C_SRC ${CMAKE_SOURCE_DIR}/src/peripheral.c ${CMAKE_SOURCE_DIR}/src/LDC1612.c ${CMAKE_SOURCE_DIR}/src/RS485.c - ${CMAKE_SOURCE_DIR}/src/i2c.c ) add_executable(xlsw_3dp_LDC1612 ${TARGET_C_SRC}) diff --git a/inc/LDC1612.h b/inc/LDC1612.h index a8ccf0e..e75a21f 100644 --- a/inc/LDC1612.h +++ b/inc/LDC1612.h @@ -20,6 +20,8 @@ #define LED_TIMER TIMER16 #define LED_IRQ TIMER16_IRQn +#define I2C_TIME_OUT (uint16_t)(10000) +#define LDC_I2C I2C0 #define I2C_SPEED 100000 #define RCU_IR_GPIO RCU_GPIOF @@ -44,5 +46,7 @@ void led_config(void); void LDC1612_I2CConfig(void); void LDC1612_Init(void); +int LDC1612_read_reg(uint8_t device_address, uint8_t reg_address, uint8_t *data, uint16_t length); +int i2c_write_reg(uint8_t device_address, uint8_t *data); #endif //LDC1612_H diff --git a/inc/i2c.h b/inc/i2c.h deleted file mode 100644 index 0af57d3..0000000 --- a/inc/i2c.h +++ /dev/null @@ -1,15 +0,0 @@ -// -// Created by dell on 24-12-4. -// - -#ifndef I2C_H -#define I2C_H - -#include "gd32e23x_i2c.h" -#include "LDC1612.h" - -#define I2C_TIME_OUT (uint16_t)(5000) -#define LDC_I2C I2C0 - -int i2c_write_reg(uint8_t device_address, uint8_t *data); -#endif //I2C_H diff --git a/src/LDC1612.c b/src/LDC1612.c index e97e900..135d653 100644 --- a/src/LDC1612.c +++ b/src/LDC1612.c @@ -62,21 +62,206 @@ void LDC1612_I2CConfig(void) { i2c_ack_config(LDC_I2C, I2C_ACK_ENABLE); } -uint8_t RCOUNT0_ALL[3]={RCOUNT0_ADDR,0x05,0x36};//csdn -uint8_t SETTLECOUNT0_ALL[3]={SETTLECOUNT0_ADDR,0x00,0x0a}; -uint8_t CLOCK_DIVIDERS0_ALL[3]={CLOCK_DIVIDERS0_ADDR,0x10,0x02}; -uint8_t ERROR_CONFIG_ALL[3]={ERROR_CONFIG_ADDR,0x00,0x00}; -uint8_t MUX_CONFIG_ALL[3]={MUX_CONFIG_ADDR,0x82,0x0c}; -uint8_t DRIVE_CURRENT0_ALL[3]={DRIVE_CURRENT0_ADDR,0x90,0x00}; -uint8_t CONFIG_ALL[3]={CONFIG_ADDR,0x14,0x01};//csdn +/** + * @brief 向I2C设备寄存器写入数据 + * @param device_address: 设备地址 + * @param data: 要写入的数据 + * @retval int: 0表示成功,-1表示超时 + */ +int i2c_write_reg(uint8_t device_address, uint8_t *data) +{ + uint32_t timeout = 0; + printf("debug tag- 0\r\n"); + // 生成起始条件 + while (i2c_flag_get(LDC_I2C, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT)) + timeout++; + if (timeout >= I2C_TIME_OUT) { + return -1; // 超时返回错误 + } + i2c_start_on_bus(LDC_I2C); + timeout = 0; + printf("debug tag- 1\r\n"); + + // 等待起始条件发送完成 + while (!i2c_flag_get(LDC_I2C, I2C_FLAG_SBSEND) && (timeout < I2C_TIME_OUT)) + timeout++; + if (timeout >= I2C_TIME_OUT) { + return -1; // 超时返回错误 + } + i2c_master_addressing(LDC_I2C, device_address, I2C_TRANSMITTER); + timeout = 0; + printf("debug tag- 2\r\n"); + + // 等待地址发送完成 + while (!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND) && (timeout < I2C_TIME_OUT)) + timeout++; + if (timeout >= I2C_TIME_OUT) { + printf("debug tag- 3-\r\n"); + return -1; // 超时返回错误 + } + i2c_flag_clear(LDC_I2C, I2C_FLAG_ADDSEND); + timeout = 0; + printf("debug tag- 3\r\n"); + + // 发送寄存器地址 + while (!i2c_flag_get(LDC_I2C, I2C_FLAG_TBE) && (timeout < I2C_TIME_OUT)) + timeout++; + if (timeout >= I2C_TIME_OUT) { + return -1; // 超时返回错误 + } + // 发送寄存器地址和数据 + for (int i = 0; i < 3; i++) { + while (!i2c_flag_get(LDC_I2C, I2C_FLAG_TBE) && (timeout < I2C_TIME_OUT)) + timeout++; + if (timeout >= I2C_TIME_OUT) { + return -1; // 超时返回错误 + } + i2c_data_transmit(LDC_I2C, data[i]); + printf("iic_value:%x\r\n", data[i]); + timeout = 0; + } + timeout = 0; + + // 生成停止条件 + while (!i2c_flag_get(LDC_I2C, I2C_FLAG_BTC) && (timeout < I2C_TIME_OUT)) + timeout++; + if (timeout >= I2C_TIME_OUT) { + return -1; // 超时返回错误 + } + i2c_stop_on_bus(LDC_I2C); + timeout = 0; + + while (i2c_flag_get(LDC_I2C, I2C_FLAG_STPDET) && (timeout < I2C_TIME_OUT)) + timeout++; + if (timeout >= I2C_TIME_OUT) { + return -1; // 超时返回错误 + } + + printf("test info"); + return 0; +} void LDC1612_Init(void) { + uint8_t RCOUNT0_ALL[3]={RCOUNT0_ADDR,0x05,0x36};//csdn + uint8_t SETTLECOUNT0_ALL[3]={SETTLECOUNT0_ADDR,0x00,0x0a}; + uint8_t CLOCK_DIVIDERS0_ALL[3]={CLOCK_DIVIDERS0_ADDR,0x10,0x02}; + uint8_t ERROR_CONFIG_ALL[3]={ERROR_CONFIG_ADDR,0x00,0x00}; + uint8_t MUX_CONFIG_ALL[3]={MUX_CONFIG_ADDR,0x82,0x0c}; + uint8_t DRIVE_CURRENT0_ALL[3]={DRIVE_CURRENT0_ADDR,0x90,0x00}; + uint8_t CONFIG_ALL[3]={CONFIG_ADDR,0x14,0x01};//csdn + LDC1612_I2CConfig(); - i2c_write_reg(LDC1612_ADDR, RCOUNT0_ALL); - i2c_write_reg(LDC1612_ADDR, SETTLECOUNT0_ALL); - i2c_write_reg(LDC1612_ADDR, CLOCK_DIVIDERS0_ALL); - i2c_write_reg(LDC1612_ADDR, ERROR_CONFIG_ALL); - i2c_write_reg(LDC1612_ADDR, MUX_CONFIG_ALL); - i2c_write_reg(LDC1612_ADDR, DRIVE_CURRENT0_ALL); - i2c_write_reg(LDC1612_ADDR, CONFIG_ALL); -} \ No newline at end of file + + int ret = i2c_write_reg(LDC1612_ADDR, RCOUNT0_ALL); + + printf("ret:%d\r\n",ret); + // i2c_write_reg(LDC1612_ADDR, RCOUNT0_ALL); +// i2c_write_reg(LDC1612_ADDR, SETTLECOUNT0_ALL); +// i2c_write_reg(LDC1612_ADDR, CLOCK_DIVIDERS0_ALL); +// i2c_write_reg(LDC1612_ADDR, ERROR_CONFIG_ALL); +// i2c_write_reg(LDC1612_ADDR, MUX_CONFIG_ALL); +// i2c_write_reg(LDC1612_ADDR, DRIVE_CURRENT0_ALL); +// i2c_write_reg(LDC1612_ADDR, CONFIG_ALL); +} + +/** + * @brief 从I2C设备寄存器读取数据 + * @param device_address: 设备地址 + * @param reg_address: 寄存器地址 + * @param data: 存储读取数据的缓冲区 + * @param length: 要读取的数据长度 + * @retval int: 0表示成功,-1表示超时 + */ +int LDC1612_read_reg(uint8_t device_address, uint8_t reg_address, uint8_t *data, uint16_t length) +{ + uint32_t timeout = 0; + + i2c_ack_config(LDC_I2C, I2C_ACK_ENABLE); + + // 判断总线是否忙 + while (i2c_flag_get(LDC_I2C, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT)) + timeout++; + if (timeout >= I2C_TIME_OUT) { + return -1; // 超时返回错误 + } + i2c_start_on_bus(LDC_I2C); //发送开始信号 + timeout = 0; + + // 等待起始条件发送完成 + while (!i2c_flag_get(LDC_I2C, I2C_FLAG_SBSEND) && (timeout < I2C_TIME_OUT)) + timeout++; + if (timeout >= I2C_TIME_OUT) { + return -1; // 超时返回错误 + } + i2c_master_addressing(LDC_I2C, device_address, I2C_TRANSMITTER); //设置为发送模式 + timeout = 0; + + // 等待地址发送完成 + while (!i2c_flag_get(LDC_I2C, I2C_FLAG_ADDSEND) && (timeout < I2C_TIME_OUT)) + timeout++; + if (timeout >= I2C_TIME_OUT) { + return -1; // 超时返回错误 + } + i2c_flag_clear(LDC_I2C, I2C_FLAG_ADDSEND); + timeout = 0; + + // 开始接收数据 + while (!i2c_flag_get(LDC_I2C, I2C_FLAG_TBE) && (timeout < I2C_TIME_OUT)) + timeout++; + if (timeout >= I2C_TIME_OUT) { + return -1; // 超时返回错误 + } + i2c_data_transmit(LDC_I2C, reg_address); //发送寄存器地址 + timeout = 0; + + // 生成重复起始条件 + while (!i2c_flag_get(LDC_I2C, I2C_FLAG_BTC) && (timeout < I2C_TIME_OUT)) + timeout++; + if (timeout >= I2C_TIME_OUT) { + return -1; // 超时返回错误 + } + i2c_start_on_bus(LDC_I2C); + timeout = 0; + + // 等待重复起始条件发送完成 + while (!i2c_flag_get(LDC_I2C, I2C_FLAG_SBSEND) && (timeout < I2C_TIME_OUT)) + timeout++; + if (timeout >= I2C_TIME_OUT) { + return -1; // 超时返回错误 + } + i2c_master_addressing(LDC_I2C, device_address, I2C_RECEIVER); + timeout = 0; + + // 等待地址发送完成 + while (!i2c_flag_get(LDC_I2C, I2C_FLAG_ADDSEND) && (timeout < I2C_TIME_OUT)) + timeout++; + if (timeout >= I2C_TIME_OUT) { + return -1; // 超时返回错误 + } + i2c_flag_clear(LDC_I2C, I2C_FLAG_ADDSEND); + timeout = 0; + +// 读取数据 +for (uint16_t i = 0; i < length; i++) { + while (!i2c_flag_get(LDC_I2C, I2C_FLAG_RBNE) && (timeout < I2C_TIME_OUT)) + timeout++; + if (timeout >= I2C_TIME_OUT) { + return -1; // 超时返回错误 + } + if (i == length - 1) { + i2c_ack_config(LDC_I2C, I2C_ACK_DISABLE); // 禁用ACK + i2c_stop_on_bus(LDC_I2C); // 生成停止条件 + } + data[i] = i2c_data_receive(LDC_I2C); + timeout = 0; +} +i2c_ack_config(LDC_I2C, I2C_ACK_ENABLE); // 重新启用ACK + + // 生成停止条件 + i2c_stop_on_bus(LDC_I2C); + timeout = 0; + + return 0; +} + + diff --git a/src/i2c.c b/src/i2c.c deleted file mode 100644 index f2c7a04..0000000 --- a/src/i2c.c +++ /dev/null @@ -1,78 +0,0 @@ -// -// Created by dell on 24-12-4. -// - -#include "i2c.h" - -/** - * @brief 向I2C设备寄存器写入数据 - * @param device_address: 设备地址 - * @param data: 要写入的数据 - * @retval int: 0表示成功,-1表示超时 - */ -int i2c_write_reg(uint8_t device_address, uint8_t *data) -{ - uint32_t timeout = 0; - - // 生成起始条件 - while (i2c_flag_get(LDC_I2C, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT)) - timeout++; - if (timeout >= I2C_TIME_OUT) { - return -1; // 超时返回错误 - } - i2c_start_on_bus(LDC_I2C); - timeout = 0; - - // 等待起始条件发送完成 - while (!i2c_flag_get(LDC_I2C, I2C_FLAG_SBSEND) && (timeout < I2C_TIME_OUT)) - timeout++; - if (timeout >= I2C_TIME_OUT) { - return -1; // 超时返回错误 - } - i2c_master_addressing(LDC_I2C, device_address, I2C_TRANSMITTER); - timeout = 0; - - // 等待地址发送完成 - while (!i2c_flag_get(LDC_I2C, I2C_FLAG_ADDSEND) && (timeout < I2C_TIME_OUT)) - timeout++; - if (timeout >= I2C_TIME_OUT) { - return -1; // 超时返回错误 - } - i2c_flag_clear(LDC_I2C, I2C_FLAG_ADDSEND); - timeout = 0; - - // 发送寄存器地址 - while (!i2c_flag_get(LDC_I2C, I2C_FLAG_TBE) && (timeout < I2C_TIME_OUT)) - timeout++; - if (timeout >= I2C_TIME_OUT) { - return -1; // 超时返回错误 - } - // 发送寄存器地址和数据 - for (int i = 0; i < 3; i++) { - while (!i2c_flag_get(LDC_I2C, I2C_FLAG_TBE) && (timeout < I2C_TIME_OUT)) - timeout++; - if (timeout >= I2C_TIME_OUT) { - return -1; // 超时返回错误 - } - i2c_data_transmit(LDC_I2C, data[i]); - timeout = 0; - } - timeout = 0; - - // 生成停止条件 - while (!i2c_flag_get(LDC_I2C, I2C_FLAG_BTC) && (timeout < I2C_TIME_OUT)) - timeout++; - if (timeout >= I2C_TIME_OUT) { - return -1; // 超时返回错误 - } - i2c_stop_on_bus(LDC_I2C); - timeout = 0; - - while (i2c_flag_get(LDC_I2C, I2C_FLAG_STPDET) && (timeout < I2C_TIME_OUT)) - timeout++; - if (timeout >= I2C_TIME_OUT) { - return -1; // 超时返回错误 - } - - return 0; -} \ No newline at end of file diff --git a/src/main.c b/src/main.c index 015696c..06eb0b5 100644 --- a/src/main.c +++ b/src/main.c @@ -33,8 +33,18 @@ int main(void) printf("XLSW-3DP-LDC1612! V0.0.1\r\n"); printf("\r\n"); + LDC1612_Init(); + + + + uint8_t data[8] = {0}; + uint32_t value = 0; + while(1){ - printf("hello world!\r\n"); + + // LDC1612_read_reg(LDC1612_ADDR, 0x00, data, 2); + // printf("msb:%x %x\r\n",data[0],data[1]); + // printf("hello world!\r\n"); delay_ms(5000); } }