From 7a3e93f5d8136f6dda348fb0299452a32ca391f3 Mon Sep 17 00:00:00 2001 From: yelvlab Date: Wed, 4 Dec 2024 18:43:16 +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 | 16 +++++++---- inc/i2c.h | 15 ++++++++++ src/LDC1612.c | 30 ++++++++++++++++--- src/RS485.c | 1 - src/i2c.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 131 insertions(+), 10 deletions(-) create mode 100644 inc/i2c.h create mode 100644 src/i2c.c diff --git a/CMakeLists.txt b/CMakeLists.txt index fe82fc0..e42d494 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,6 +27,7 @@ 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 84cde97..a8ccf0e 100644 --- a/inc/LDC1612.h +++ b/inc/LDC1612.h @@ -21,7 +21,7 @@ #define LED_IRQ TIMER16_IRQn #define I2C_SPEED 100000 -#define IR_I2C I2C0 + #define RCU_IR_GPIO RCU_GPIOF #define RCU_I2C RCU_I2C0 #define I2C_SCL_PORT GPIOF @@ -30,13 +30,19 @@ #define I2C_SDA_PIN GPIO_PIN_0 #define I2C_GPIO_AF GPIO_AF_1 -#define I2C_TIME_OUT (uint16_t)(5000) - -#define SLAVE_ADDR (0x5A << 1) +#define LDC1612_ADDR (0x2A << 1) +// LDC1612 寄存器地址 +#define RCOUNT0_ADDR 0x08 +#define SETTLECOUNT0_ADDR 0x10 +#define CLOCK_DIVIDERS0_ADDR 0x14 +#define ERROR_CONFIG_ADDR 0x19 +#define MUX_CONFIG_ADDR 0x1B +#define DRIVE_CURRENT0_ADDR 0x1E +#define CONFIG_ADDR 0x1A void led_config(void); void LDC1612_I2CConfig(void); - +void LDC1612_Init(void); #endif //LDC1612_H diff --git a/inc/i2c.h b/inc/i2c.h new file mode 100644 index 0000000..0af57d3 --- /dev/null +++ b/inc/i2c.h @@ -0,0 +1,15 @@ +// +// 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 d936d8f..e97e900 100644 --- a/src/LDC1612.c +++ b/src/LDC1612.c @@ -3,6 +3,7 @@ // #include "LDC1612.h" +#include "i2c.h" void led_config(void) @@ -33,6 +34,8 @@ void led_config(void) nvic_irq_enable(LED_IRQ, 0); } + + /** * @brief This function configure the I2C peripheral & GPIO * @param[in] none @@ -52,9 +55,28 @@ void LDC1612_I2CConfig(void) { gpio_mode_set(I2C_SDA_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, I2C_SDA_PIN); gpio_output_options_set(I2C_SDA_PORT, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, I2C_SDA_PIN); - i2c_clock_config(IR_I2C, I2C_SPEED, I2C_DTCY_2); + i2c_clock_config(LDC_I2C, I2C_SPEED, I2C_DTCY_2); - i2c_mode_addr_config(IR_I2C, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, SLAVE_ADDR); - i2c_enable(IR_I2C); - i2c_ack_config(IR_I2C, I2C_ACK_ENABLE); + i2c_mode_addr_config(LDC_I2C, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, LDC1612_ADDR); + i2c_enable(LDC_I2C); + 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 + +void LDC1612_Init(void) { + 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 diff --git a/src/RS485.c b/src/RS485.c index a53d3a8..b629599 100644 --- a/src/RS485.c +++ b/src/RS485.c @@ -56,4 +56,3 @@ void process_command(char *cmd) { printf("Invalid Command!\r\n"); } } - diff --git a/src/i2c.c b/src/i2c.c new file mode 100644 index 0000000..f2c7a04 --- /dev/null +++ b/src/i2c.c @@ -0,0 +1,78 @@ +// +// 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