generated from hulk/gd32e23x_template
iic驱动重写
This commit is contained in:
parent
828721759d
commit
7a3e93f5d8
@ -27,6 +27,7 @@ set(TARGET_C_SRC
|
|||||||
${CMAKE_SOURCE_DIR}/src/peripheral.c
|
${CMAKE_SOURCE_DIR}/src/peripheral.c
|
||||||
${CMAKE_SOURCE_DIR}/src/LDC1612.c
|
${CMAKE_SOURCE_DIR}/src/LDC1612.c
|
||||||
${CMAKE_SOURCE_DIR}/src/RS485.c
|
${CMAKE_SOURCE_DIR}/src/RS485.c
|
||||||
|
${CMAKE_SOURCE_DIR}/src/i2c.c
|
||||||
)
|
)
|
||||||
|
|
||||||
add_executable(xlsw_3dp_LDC1612 ${TARGET_C_SRC})
|
add_executable(xlsw_3dp_LDC1612 ${TARGET_C_SRC})
|
||||||
|
@ -21,7 +21,7 @@
|
|||||||
#define LED_IRQ TIMER16_IRQn
|
#define LED_IRQ TIMER16_IRQn
|
||||||
|
|
||||||
#define I2C_SPEED 100000
|
#define I2C_SPEED 100000
|
||||||
#define IR_I2C I2C0
|
|
||||||
#define RCU_IR_GPIO RCU_GPIOF
|
#define RCU_IR_GPIO RCU_GPIOF
|
||||||
#define RCU_I2C RCU_I2C0
|
#define RCU_I2C RCU_I2C0
|
||||||
#define I2C_SCL_PORT GPIOF
|
#define I2C_SCL_PORT GPIOF
|
||||||
@ -30,13 +30,19 @@
|
|||||||
#define I2C_SDA_PIN GPIO_PIN_0
|
#define I2C_SDA_PIN GPIO_PIN_0
|
||||||
#define I2C_GPIO_AF GPIO_AF_1
|
#define I2C_GPIO_AF GPIO_AF_1
|
||||||
|
|
||||||
#define I2C_TIME_OUT (uint16_t)(5000)
|
#define LDC1612_ADDR (0x2A << 1)
|
||||||
|
|
||||||
#define SLAVE_ADDR (0x5A << 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 led_config(void);
|
||||||
void LDC1612_I2CConfig(void);
|
void LDC1612_I2CConfig(void);
|
||||||
|
void LDC1612_Init(void);
|
||||||
|
|
||||||
#endif //LDC1612_H
|
#endif //LDC1612_H
|
||||||
|
15
inc/i2c.h
Normal file
15
inc/i2c.h
Normal file
@ -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
|
@ -3,6 +3,7 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
#include "LDC1612.h"
|
#include "LDC1612.h"
|
||||||
|
#include "i2c.h"
|
||||||
|
|
||||||
|
|
||||||
void led_config(void)
|
void led_config(void)
|
||||||
@ -33,6 +34,8 @@ void led_config(void)
|
|||||||
nvic_irq_enable(LED_IRQ, 0);
|
nvic_irq_enable(LED_IRQ, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief This function configure the I2C peripheral & GPIO
|
* @brief This function configure the I2C peripheral & GPIO
|
||||||
* @param[in] none
|
* @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_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);
|
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_mode_addr_config(LDC_I2C, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, LDC1612_ADDR);
|
||||||
i2c_enable(IR_I2C);
|
i2c_enable(LDC_I2C);
|
||||||
i2c_ack_config(IR_I2C, I2C_ACK_ENABLE);
|
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);
|
||||||
}
|
}
|
@ -56,4 +56,3 @@ void process_command(char *cmd) {
|
|||||||
printf("Invalid Command!\r\n");
|
printf("Invalid Command!\r\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
78
src/i2c.c
Normal file
78
src/i2c.c
Normal file
@ -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;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user