fix iic driver

This commit is contained in:
2025-08-14 19:57:57 +08:00
parent 4e0ad6e8eb
commit 88f79f7eb0
8 changed files with 1853 additions and 959 deletions

View File

@@ -31,8 +31,8 @@ set(TARGET_SRC
Src/command.c Src/command.c
Src/i2c.c Src/i2c.c
Src/ldc1612.c Src/ldc1612.c
Src/tmp112.c # Src/tmp112.c
Src/sensor_example.c # Src/sensor_example.c
) )
# 设置输出目录 # 设置输出目录

View File

@@ -13,8 +13,8 @@
/* >>>>>>>>>>>>>>>>>>>>[DEBUG ASSERTIONS DEFINE]<<<<<<<<<<<<<<<<<<<< */ /* >>>>>>>>>>>>>>>>>>>>[DEBUG ASSERTIONS DEFINE]<<<<<<<<<<<<<<<<<<<< */
#define DEBUG_VERBOSE // Debug Assertions Status : Debug Verbose Information // #define DEBUG_VERBOSE // Debug Assertions Status : Debug Verbose Information
// #undef DEBUG_VERBOSE // Debug Assertions Status : No Debug Verbose Information #undef DEBUG_VERBOSE // Debug Assertions Status : No Debug Verbose Information
/******************************************************************************/ /******************************************************************************/

View File

@@ -32,15 +32,15 @@
/******************************************************************************/ /******************************************************************************/
/* I2C status enumeration */ /* I2C result enumeration */
typedef enum { typedef enum {
I2C_STATUS_SUCCESS = 0, /* Operation successful */ I2C_RESULT_SUCCESS = 0, /* Operation successful */
I2C_STATUS_TIMEOUT, /* Timeout occurred */ I2C_RESULT_TIMEOUT, /* Timeout occurred */
I2C_STATUS_NACK, /* No acknowledge received */ I2C_RESULT_NACK, /* No acknowledge received */
I2C_STATUS_BUS_BUSY, /* Bus is busy */ I2C_RESULT_BUS_BUSY, /* Bus is busy */
I2C_STATUS_ERROR, /* General error */ I2C_RESULT_ERROR, /* General error */
I2C_STATUS_INVALID_PARAM /* Invalid parameter */ I2C_RESULT_INVALID_PARAM /* Invalid parameter */
} i2c_status_t; } i2c_result_t;
/* I2C state machine enumeration */ /* I2C state machine enumeration */
typedef enum { typedef enum {
@@ -67,31 +67,26 @@ typedef enum {
/******************************************************************************/ /******************************************************************************/
/* Function declarations */ /* Function declarations */
/*! // TODO I2C Result
\brief configure the GPIO ports for I2C
\param[in] none
\param[out] none
\retval none
*/
void i2c_gpio_config(void);
/*! /*!
\brief configure the I2C interface \brief configure the I2C interface
\param[in] none \param[in] none
\param[out] none \param[out] none
\retval i2c_status_t \retval i2c_result_t
*/ */
i2c_status_t i2c_config(void); void i2c_config(void);
// TODO I2C Result
/*! /*!
\brief reset I2C bus with proper recovery \brief reset I2C bus with proper recovery
\param[in] none \param[in] none
\param[out] none \param[out] none
\retval i2c_status_t \retval i2c_result_t
*/ */
i2c_status_t i2c_bus_reset(void); void i2c_bus_reset(void);
/*! /*!
\brief scan I2C bus for devices \brief scan I2C bus for devices
@@ -101,31 +96,43 @@ i2c_status_t i2c_bus_reset(void);
*/ */
void i2c_scan(void); void i2c_scan(void);
// TODO I2C Result
/*! /*!
\brief write 16-bit data to I2C device \brief write 16-bit data to I2C device
\param[in] slave_addr: 7-bit slave address \param[in] slave_addr: 7-bit slave address
\param[in] reg_addr: register address \param[in] reg_addr: register address
\param[in] data: pointer to 2-byte data array \param[in] data: pointer to 2-byte data array
\param[out] none \param[out] none
\retval i2c_status_t \retval i2c_result_t
*/ */
i2c_status_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, const uint8_t data[2]); uint8_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data[2]);
// TODO I2C Result
/*! /*!
\brief read 16-bit data from I2C device \brief read 16-bit data from I2C device
\param[in] slave_addr: 7-bit slave address \param[in] slave_addr: 7-bit slave address
\param[in] reg_addr: register address \param[in] reg_addr: register address
\param[out] data: pointer to 2-byte data buffer \param[out] data: pointer to 2-byte data buffer
\retval i2c_status_t \retval i2c_result_t
*/ */
i2c_status_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data); uint8_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data);
// TODO I2C Result
/*!
\brief read 16-bit data from I2C device
\param[in] slave_addr: 7-bit slave address
\param[in] reg_addr: register address
\param[out] data: pointer to 2-byte data buffer
\retval i2c_result_t
*/
uint8_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data);
/*! /*!
\brief get status string for debugging \brief get status string for debugging
\param[in] status: i2c_status_t value \param[in] status: i2c_result_t value
\param[out] none \param[out] none
\retval const char* status string \retval const char* status string
*/ */
const char* i2c_get_status_string(i2c_status_t status); const char* i2c_get_status_string(i2c_result_t status);
#endif //I2C_H #endif //I2C_H

View File

@@ -1,6 +1,5 @@
// //
// Created by dell on 24-12-3. // Created by dell on 24-12-3.
// LDC1612 Inductive Sensor Driver Header
// //
#ifndef LDC1612_H #ifndef LDC1612_H
@@ -15,163 +14,88 @@
#include <stdlib.h> #include <stdlib.h>
#include <math.h> #include <math.h>
#include "board_config.h" #include "board_config.h"
#include "soft_i2c.h"
#include "i2c.h" #include "i2c.h"
/******************************************************************************/ /***************************************************************************/
/* LDC1612 I2C Address */
#define LDC1612_ADDR (0x2B) // 7-bit address
/* Register Addresses */ #define LDC1612_ADDR 0x2B
/******************************************************************************/
#define LDC1612_DATA_CH0_MSB 0x00
#define LDC1612_DATA_CH0_LSB 0x01
#define LDC1612_DATA_CH1_MSB 0x02
#define LDC1612_DATA_CH1_LSB 0x03
#define LDC1612_RCOUNT_CH0 0x08
#define LDC1612_RCOUNT_CH1 0x09
#define LDC1612_OFFSET_CH0 0x0C
#define LDC1612_OFFSET_CH1 0x0D
#define LDC1612_SETTLECOUNT_CH0 0x10
#define LDC1612_SETTLECOUNT_CH1 0x11
#define LDC1612_CLOCK_DIVIDERS_CH0 0x14
#define LDC1612_CLOCK_DIVIDERS_CH1 0x15
#define LDC1612_STATUS 0x18
#define LDC1612_ERROR_CONFIG 0x19
#define LDC1612_CONFIG 0x1A
#define LDC1612_MUX_CONFIG 0x1B
#define LDC1612_RESET_DEV 0x1C
#define LDC1612_DRIVE_CURRENT_CH0 0x1E
#define LDC1612_DRIVE_CURRENT_CH1 0x1F
#define LDC1612_MANUFACTURER_ID 0x7E
#define LDC1612_DEVICE_ID 0x7F
/* Channel Definitions */ /*Register Rddr*/
/******************************************************************************/ /***************************************************************************/
#define LDC1612_CHANNEL_0 0
#define LDC1612_CHANNEL_1 1
/* Configuration Values */ #define CONVERTION_RESULT_REG_START 0X00
/******************************************************************************/ #define SET_CONVERSION_TIME_REG_START 0X08
#define LDC1612_CONVERSION_TIME_CH0 0x0546 // 转换时间 #define SET_CONVERSION_OFFSET_REG_START 0X0C
#define LDC1612_DRIVE_CURRENT_DEFAULT 0x9000 // 驱动电流 #define SET_LC_STABILIZE_REG_START 0X10
#define LDC1612_MUX_CONFIG_DEFAULT 0x020C // 无自动扫描滤波器带宽3.3MHz #define SET_FREQ_REG_START 0X14
#define LDC1612_SENSOR_CONFIG_ACTIVE 0x1601 // 激活配置
#define LDC1612_SENSOR_CONFIG_SLEEP 0x2801 // 休眠配置
#define LDC1612_ERROR_CONFIG_DEFAULT 0x0000 // 错误配置
#define LDC1612_SETTLECOUNT_CH0_DEFAULT 0x001E // 稳定时间
#define LDC1612_RESET_VALUE 0x8000 // 复位值
/* Coil Parameters */ #define SENSOR_STATUS_REG 0X18
/******************************************************************************/ #define ERROR_CONFIG_REG 0X19
#define LDC1612_COIL_RP_KOHM 7.2f // 并联电阻 (kΩ) #define SENSOR_CONFIG_REG 0X1A
#define LDC1612_COIL_L_UH 33.0f // 电感值 (μH) #define MUL_CONFIG_REG 0X1B
#define LDC1612_COIL_C_PF 150.0f // 电容值 (pF) #define SENSOR_RESET_REG 0X1C
#define LDC1612_COIL_Q_FACTOR 35.97f // 品质因数 #define SET_DRIVER_CURRENT_REG 0X1E
#define LDC1612_COIL_FREQ_HZ 2262000 // 谐振频率 (Hz)
/* Error Codes */ #define READ_MANUFACTURER_ID 0X7E
/******************************************************************************/ #define READ_DEVICE_ID 0X7F
#define LDC1612_ERROR_NONE 0x00000000
#define LDC1612_ERROR_NO_COIL 0xF0000000
#define LDC1612_ERROR_UNDER_RANGE 0x80000000
#define LDC1612_ERROR_OVER_RANGE 0x40000000
#define LDC1612_ERROR_WATCHDOG 0x20000000
#define LDC1612_ERROR_AMPLITUDE 0x10000000
/* Status Definitions */
/******************************************************************************/
typedef enum {
LDC1612_STATUS_SUCCESS = 0,
LDC1612_STATUS_ERROR,
LDC1612_STATUS_TIMEOUT,
LDC1612_STATUS_INVALID_PARAM,
LDC1612_STATUS_NO_COIL,
LDC1612_STATUS_UNDER_RANGE,
LDC1612_STATUS_OVER_RANGE
} ldc1612_status_t;
typedef struct {
uint32_t raw_data;
uint32_t frequency;
float distance_mm;
bool error_flag;
uint8_t error_code;
} ldc1612_result_t;
/******************************************************************************/ /******************************************************************************/
/* Function Declarations */
/*! #define CHANNEL_0 0
\brief 初始化LDC1612传感器 #define CHANNEL_1 1
\param[in] none
\param[out] none
\retval ldc1612_status_t
*/
ldc1612_status_t ldc1612_init(void);
/*! /******************************************************************************/
\brief 复位LDC1612传感器
\param[in] none
\param[out] none
\retval ldc1612_status_t
*/
ldc1612_status_t ldc1612_reset(void);
/*! #define LDC1612_CONVERSION_TIME_CH0 0x0546 //0536
\brief 配置单通道模式 #define LDC1612_DRIVE_CURRENT 0x9000 //A000
\param[in] channel: 通道号 (0或1) #define LDC1612_MUX_CONFIG 0x020C // no auto scan and filter bandwidth 3.3MHz
\param[out] none #define LDC1612_SENSOR_CONFIG 0x1601
\retval ldc1612_status_t #define LDC1612_SLEEP_MODE 0x2801
*/ #define LDC1612_ERROR_CONFIG 0x0000
ldc1612_status_t ldc1612_config_single_channel(uint8_t channel); #define LC_STABILIZE_TIME_CH0 0x001E //30
#define LDC1612_RESET_DEV 0x8000 //[15:0] 0b1000 0000 0000 0000
/******************************************************************************/
#define COIL_RP_KOM 7.2
#define COIL_L_UH 33
#define COIL_C_PF 150
#define COIL_Q_FACTOR 35.97
#define COIL_FREQ_HZ 2262000
/******************************************************************************/
void ldc1612_set_conversion_time(uint8_t channel, uint16_t result);
void ldc1612_set_conversion_offset(uint8_t channel, uint16_t result);
void ldc1612_set_LC_stabilize_time(uint8_t channel, uint16_t result);
void ldc1612_set_freq_divide(uint8_t channel);
void ldc1612_set_error_config(uint16_t value);
void ldc1612_set_mux_config(uint16_t value);
void ldc1612_reset_sensor(void);
void ldc1612_set_drive_current(uint8_t channel, uint16_t value);
void ldc1612_set_sensor_config(uint16_t value);
void ldc1612_single_ch0_config(void);
void ldc1612_iic_get_sensor_infomation(void);
/*!
\brief 读取制造商ID
\param[in] none
\param[out] none
\retval uint16_t 制造商ID
*/
uint16_t ldc1612_get_manufacturer_id(void); uint16_t ldc1612_get_manufacturer_id(void);
/*! uint16_t ldc1612_get_deveice_id(void);
\brief 读取设备ID
\param[in] none
\param[out] none
\retval uint16_t 设备ID
*/
uint16_t ldc1612_get_device_id(void);
/*! uint32_t ldc1612_get_raw_channel_result(uint8_t channel);
\brief 读取通道原始数据
\param[in] channel: 通道号
\param[out] result: 结果结构体指针
\retval ldc1612_status_t
*/
ldc1612_status_t ldc1612_read_channel(uint8_t channel, ldc1612_result_t *result);
/*! uint32_t ldc1612_parse_raw_result(uint32_t raw_result);
\brief 设置驱动电流
\param[in] channel: 通道号
\param[in] current: 电流值
\param[out] none
\retval ldc1612_status_t
*/
ldc1612_status_t ldc1612_set_drive_current(uint8_t channel, uint16_t current);
/*! void ldc1612_drvie_current_detect(uint8_t channel);
\brief 自动检测驱动电流
\param[in] channel: 通道号
\param[out] none
\retval ldc1612_status_t
*/
ldc1612_status_t ldc1612_auto_detect_drive_current(uint8_t channel);
/*!
\brief 获取状态字符串
\param[in] status: 状态码
\param[out] none
\retval const char* 状态字符串
*/
const char* ldc1612_get_status_string(ldc1612_status_t status);
#endif //LDC1612_H #endif //LDC1612_H

759
Src/i2c.c
View File

@@ -1,13 +1,9 @@
// //
// Created by dell on 24-12-20. // Created by dell on 24-12-20.
// Improved I2C driver with better state machine and error handling
// //
#include "i2c.h" #include "i2c.h"
/* Private variables */
static uint8_t i2c_retry_count = 0;
/*! /*!
\brief configure the GPIO ports \brief configure the GPIO ports
\param[in] none \param[in] none
@@ -33,81 +29,54 @@ void i2c_gpio_config(void) {
\brief configure the I2CX interface \brief configure the I2CX interface
\param[in] none \param[in] none
\param[out] none \param[out] none
\retval i2c_status_t \retval none
*/ */
i2c_status_t i2c_config(void) { void i2c_config(void) {
/* configure I2C GPIO */ /* configure I2C GPIO */
i2c_gpio_config(); i2c_gpio_config();
/* enable I2C clock */ /* enable I2C clock */
rcu_periph_clock_enable(RCU_I2C); rcu_periph_clock_enable(RCU_I2C);
/* configure I2C clock */ /* configure I2C clock */
i2c_clock_config(I2C0, I2C_SPEED, I2C_DTCY_2); i2c_clock_config(I2C0, I2C_SPEED, I2C_DTCY_2);
/* configure I2C address */
/* configure I2C address - use 0x00 as master doesn't need specific address */ i2c_mode_addr_config(I2C0, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, 0xA0);
i2c_mode_addr_config(I2C0, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, I2C_MASTER_ADDRESS);
/* enable I2CX */ /* enable I2CX */
i2c_enable(I2C0); i2c_enable(I2C0);
/* enable acknowledge */ /* enable acknowledge */
i2c_ack_config(I2C0, I2C_ACK_ENABLE); i2c_ack_config(I2C0, I2C_ACK_ENABLE);
/* reset retry counter */
i2c_retry_count = 0;
return I2C_STATUS_SUCCESS;
} }
/*! /*!
\brief reset I2C bus with proper 9-clock recovery \brief reset I2C bus
\param[in] none \param[in] none
\param[out] none \param[out] none
\retval i2c_status_t \retval none
*/ */
i2c_status_t i2c_bus_reset(void) { void i2c_bus_reset(void) {
uint8_t i;
/* disable I2C peripheral */
i2c_disable(I2C0);
i2c_deinit(I2C0); i2c_deinit(I2C0);
/* configure SDA/SCL for GPIO */
/* configure SDA/SCL as GPIO output for manual control */ GPIO_BC(I2C_SCL_PORT) |= I2C_SCL_PIN;
gpio_mode_set(I2C_SCL_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, I2C_SCL_PIN); GPIO_BC(I2C_SDA_PORT) |= I2C_SDA_PIN;
gpio_mode_set(I2C_SDA_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, I2C_SDA_PIN); gpio_output_options_set(I2C_SCL_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, I2C_SCL_PIN);
gpio_output_options_set(I2C_SDA_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, I2C_SDA_PIN);
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
GPIO_BOP(I2C_SCL_PORT) |= I2C_SCL_PIN;
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
GPIO_BOP(I2C_SDA_PORT) |= I2C_SDA_PIN;
/* connect I2C_SCL_PIN to I2C_SCL */
/* connect I2C_SDA_PIN to I2C_SDA */
gpio_output_options_set(I2C_SCL_PORT, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, I2C_SCL_PIN); gpio_output_options_set(I2C_SCL_PORT, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, I2C_SCL_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);
/* configure the I2CX interface */
/* ensure both lines are high initially */ i2c_config();
gpio_bit_set(I2C_SCL_PORT, I2C_SCL_PIN);
gpio_bit_set(I2C_SDA_PORT, I2C_SDA_PIN);
delay_10us(I2C_DELAY_10US);
/* generate 9 clock pulses to release any stuck slave */
for (i = 0; i < I2C_RECOVERY_CLOCKS; i++) {
gpio_bit_reset(I2C_SCL_PORT, I2C_SCL_PIN);
delay_10us(I2C_DELAY_10US);
gpio_bit_set(I2C_SCL_PORT, I2C_SCL_PIN);
delay_10us(I2C_DELAY_10US);
}
/* generate stop condition */
gpio_bit_reset(I2C_SDA_PORT, I2C_SDA_PIN);
delay_10us(I2C_DELAY_10US);
gpio_bit_set(I2C_SCL_PORT, I2C_SCL_PIN);
delay_10us(I2C_DELAY_10US);
gpio_bit_set(I2C_SDA_PORT, I2C_SDA_PIN);
delay_10us(I2C_DELAY_10US);
/* reconfigure as I2C pins */
gpio_af_set(I2C_SCL_PORT, I2C_GPIO_AF, I2C_SCL_PIN);
gpio_af_set(I2C_SDA_PORT, I2C_GPIO_AF, I2C_SDA_PIN);
gpio_mode_set(I2C_SCL_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, I2C_SCL_PIN);
gpio_mode_set(I2C_SDA_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, I2C_SDA_PIN);
/* reconfigure the I2CX interface */
return i2c_config();
} }
/** /**
@@ -122,13 +91,7 @@ void i2c_scan(void) {
uint8_t address; uint8_t address;
int found_devices = 0; int found_devices = 0;
// printf("Scanning I2C bus...\r\n"); printf("Scanning I2C bus...\r\n");
const char* msg1 = "Scanning I2C bus...\r\n";
for (uint8_t i = 0; msg1[i] != '\0'; i++) {
while (usart_flag_get(I2C_DEBUG_UART, USART_FLAG_TBE) == RESET) {}
usart_data_transmit(I2C_DEBUG_UART, msg1[i]);
}
while (usart_flag_get(I2C_DEBUG_UART, USART_FLAG_TC) == RESET) {}
for (address = 1; address < 127; address++) { for (address = 1; address < 127; address++) {
timeout = 0; timeout = 0;
@@ -156,24 +119,7 @@ void i2c_scan(void) {
timeout++; timeout++;
if (timeout < I2C_TIME_OUT) { if (timeout < I2C_TIME_OUT) {
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND); i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
// printf("Found device at 0x%02X\r\n", address); printf("Found device at 0x%02X\r\n", address);
const char* msg2_prefix = "Found device at 0x";
for (uint8_t i = 0; msg2_prefix[i] != '\0'; i++) {
while (usart_flag_get(I2C_DEBUG_UART, USART_FLAG_TBE) == RESET) {}
usart_data_transmit(I2C_DEBUG_UART, msg2_prefix[i]);
}
// 发送地址的十六进制表示
uint8_t hex_chars[] = "0123456789ABCDEF";
while (usart_flag_get(I2C_DEBUG_UART, USART_FLAG_TBE) == RESET) {}
usart_data_transmit(I2C_DEBUG_UART, hex_chars[(address >> 4) & 0x0F]);
while (usart_flag_get(I2C_DEBUG_UART, USART_FLAG_TBE) == RESET) {}
usart_data_transmit(I2C_DEBUG_UART, hex_chars[address & 0x0F]);
const char* msg2_suffix = "\r\n";
for (uint8_t i = 0; msg2_suffix[i] != '\0'; i++) {
while (usart_flag_get(I2C_DEBUG_UART, USART_FLAG_TBE) == RESET) {}
usart_data_transmit(I2C_DEBUG_UART, msg2_suffix[i]);
}
while (usart_flag_get(I2C_DEBUG_UART, USART_FLAG_TC) == RESET) {}
found_devices++; found_devices++;
} }
@@ -182,458 +128,349 @@ void i2c_scan(void) {
timeout = 0; timeout = 0;
while ((I2C_CTL0(I2C0) & I2C_CTL0_STOP) && (timeout < I2C_TIME_OUT)) while (i2c_flag_get(I2C0, I2C_FLAG_STPDET) && (timeout < I2C_TIME_OUT))
timeout++; timeout++;
} }
if (found_devices == 0) { if (found_devices == 0) {
// printf("No I2C devices found.\r\n"); printf("No I2C devices found.\r\n");
const char* msg3 = "No I2C devices found.\r\n";
for (uint8_t i = 0; msg3[i] != '\0'; i++) {
while (usart_flag_get(I2C_DEBUG_UART, USART_FLAG_TBE) == RESET) {}
usart_data_transmit(I2C_DEBUG_UART, msg3[i]);
}
while (usart_flag_get(I2C_DEBUG_UART, USART_FLAG_TC) == RESET) {}
} else { } else {
// printf("Total %d I2C devices found.\r\n", found_devices); printf("Total %d I2C devices found.\r\n", found_devices);
const char* msg4_prefix = "Total ";
for (uint8_t i = 0; msg4_prefix[i] != '\0'; i++) {
while (usart_flag_get(I2C_DEBUG_UART, USART_FLAG_TBE) == RESET) {}
usart_data_transmit(I2C_DEBUG_UART, msg4_prefix[i]);
}
// 发送设备数量
if (found_devices >= 10) {
while (usart_flag_get(I2C_DEBUG_UART, USART_FLAG_TBE) == RESET) {}
usart_data_transmit(I2C_DEBUG_UART, '0' + (found_devices / 10));
}
while (usart_flag_get(I2C_DEBUG_UART, USART_FLAG_TBE) == RESET) {}
usart_data_transmit(I2C_DEBUG_UART, '0' + (found_devices % 10));
const char* msg4_suffix = " I2C devices found.\r\n";
for (uint8_t i = 0; msg4_suffix[i] != '\0'; i++) {
while (usart_flag_get(I2C_DEBUG_UART, USART_FLAG_TBE) == RESET) {}
usart_data_transmit(I2C_DEBUG_UART, msg4_suffix[i]);
}
while (usart_flag_get(I2C_DEBUG_UART, USART_FLAG_TC) == RESET) {}
} }
} }
/*! uint8_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data[2]) {
\brief write 16-bit data to I2C device with improved state machine uint8_t state = I2C_START;
\param[in] slave_addr: 7-bit slave address
\param[in] reg_addr: register address
\param[in] data: pointer to 2-byte data array
\param[out] none
\retval i2c_status_t
*/
i2c_status_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, const uint8_t data[2]) {
i2c_state_t state = I2C_STATE_START;
uint16_t timeout = 0; uint16_t timeout = 0;
uint8_t data_index = 0; uint8_t i2c_timeout_flag = 0;
uint8_t retry_count = 0;
/* Parameter validation */ /* enable acknowledge */
if (data == NULL || slave_addr > 0x7F) {
return I2C_STATUS_INVALID_PARAM;
}
/* Enable acknowledge */
i2c_ack_config(I2C0, I2C_ACK_ENABLE); i2c_ack_config(I2C0, I2C_ACK_ENABLE);
while (!(i2c_timeout_flag)) {
while (retry_count < I2C_MAX_RETRY) {
switch (state) { switch (state) {
case I2C_STATE_START: case I2C_START:
timeout = 0; /* i2c master sends start signal only when the bus is idle */
/* Wait for bus to be idle */
while (i2c_flag_get(I2C0, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT)) { while (i2c_flag_get(I2C0, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT)) {
timeout++; timeout++;
} }
if (timeout >= I2C_TIME_OUT) { if (timeout < I2C_TIME_OUT) {
state = I2C_STATE_ERROR; i2c_start_on_bus(I2C0);
break; timeout = 0;
state = I2C_SEND_ADDRESS;
} else {
timeout = 0;
state = I2C_START;
#ifdef DEBUG_VERBOES
printf("i2c bus is busy in WRITE BYTE!\n");
#endif
} }
/* Send start condition */
i2c_start_on_bus(I2C0);
state = I2C_STATE_SEND_ADDRESS;
timeout = 0;
break; break;
case I2C_SEND_ADDRESS:
case I2C_STATE_SEND_ADDRESS: /* i2c master sends START signal successfully */
/* Wait for start condition to be sent */
while ((!i2c_flag_get(I2C0, I2C_FLAG_SBSEND)) && (timeout < I2C_TIME_OUT)) { while ((!i2c_flag_get(I2C0, I2C_FLAG_SBSEND)) && (timeout < I2C_TIME_OUT)) {
timeout++; timeout++;
} }
if (timeout >= I2C_TIME_OUT) { if (timeout < I2C_TIME_OUT) {
state = I2C_STATE_ERROR; i2c_master_addressing(I2C0, slave_addr << 1, I2C_TRANSMITTER);
break; timeout = 0;
state = I2C_CLEAR_ADDRESS_FLAG;
} else {
timeout = 0;
state = I2C_START;
#ifdef DEBUG_VERBOES
printf("i2c master sends start signal timeout in WRITE BYTE!\n");
#endif
} }
/* Send slave address with write bit */
i2c_master_addressing(I2C0, (slave_addr << 1), I2C_TRANSMITTER);
state = I2C_STATE_CLEAR_ADDRESS;
timeout = 0;
break; break;
case I2C_CLEAR_ADDRESS_FLAG:
case I2C_STATE_CLEAR_ADDRESS: /* address flag set means i2c slave sends ACK */
/* Wait for address to be acknowledged */
while ((!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND)) && (timeout < I2C_TIME_OUT)) { while ((!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND)) && (timeout < I2C_TIME_OUT)) {
timeout++; timeout++;
} }
if (timeout >= I2C_TIME_OUT) { if (timeout < I2C_TIME_OUT) {
state = I2C_STATE_ERROR; i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
break; timeout = 0;
state = I2C_TRANSMIT_DATA;
} else {
timeout = 0;
state = I2C_START;
#ifdef DEBUG_VERBOES
printf("i2c master clears address flag timeout in WRITE BYTE!\n");
#endif
} }
/* Clear address flag */
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
state = I2C_STATE_TRANSMIT_REG;
timeout = 0;
break; break;
case I2C_TRANSMIT_DATA:
case I2C_STATE_TRANSMIT_REG: /* wait until the transmit data buffer is empty */
/* Wait for transmit buffer to be empty */
while ((!i2c_flag_get(I2C0, I2C_FLAG_TBE)) && (timeout < I2C_TIME_OUT)) { while ((!i2c_flag_get(I2C0, I2C_FLAG_TBE)) && (timeout < I2C_TIME_OUT)) {
timeout++; timeout++;
} }
if (timeout >= I2C_TIME_OUT) { if (timeout < I2C_TIME_OUT) {
state = I2C_STATE_ERROR; /* send IIC register address */
break; i2c_data_transmit(I2C0, reg_addr);
timeout = 0;
} else {
timeout = 0;
state = I2C_START;
#ifdef DEBUG_VERBOES
printf("i2c master sends data timeout in WRITE BYTE!\n");
#endif
} }
/* Send register address */ /* wait until BTC bit is set */
i2c_data_transmit(I2C0, reg_addr);
state = I2C_STATE_TRANSMIT_DATA;
timeout = 0;
data_index = 0;
break;
case I2C_STATE_TRANSMIT_DATA:
/* Wait for byte transfer complete */
while ((!i2c_flag_get(I2C0, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) { while ((!i2c_flag_get(I2C0, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) {
timeout++; timeout++;
} }
if (timeout >= I2C_TIME_OUT) { if (timeout < I2C_TIME_OUT) {
state = I2C_STATE_ERROR; /* send register MSB value */
break; i2c_data_transmit(I2C0, data[0]);
timeout = 0;
} else {
timeout = 0;
state = I2C_START;
#ifdef DEBUG_VERBOES
printf("i2c master sends MSB data timeout in WRITE BYTE!\n");
#endif
} }
/* Send data bytes */ /* wait until BTC bit is set */
if (data_index < 2) { while ((!i2c_flag_get(I2C0, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) {
i2c_data_transmit(I2C0, data[data_index]); timeout++;
data_index++; }
if (timeout < I2C_TIME_OUT) {
/* send register LSB value */
i2c_data_transmit(I2C0, data[1]);
timeout = 0; timeout = 0;
/* Stay in this state until all data is sent */ state = I2C_STOP;
} else { } else {
/* All data sent, proceed to stop */
state = I2C_STATE_STOP;
timeout = 0; timeout = 0;
state = I2C_START;
#ifdef DEBUG_VERBOES
printf("i2c master sends LSB data timeout in WRITE BYTE!\n");
#endif
}
/* wait until BTC bit is set */
while ((!i2c_flag_get(I2C0, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) {
timeout++;
}
if (timeout < I2C_TIME_OUT) {
state = I2C_STOP;
timeout = 0;
} else {
timeout = 0;
state = I2C_START;
#ifdef DEBUG_VERBOES
printf("i2c master sends data timeout in WRITE BYTE!\n");
#endif
} }
break; break;
case I2C_STOP:
case I2C_STATE_STOP: /* send a stop condition to I2C bus */
/* Send stop condition */
i2c_stop_on_bus(I2C0); i2c_stop_on_bus(I2C0);
/* i2c master sends STOP signal successfully */
/* Wait for stop condition to complete */
while ((I2C_CTL0(I2C0) & I2C_CTL0_STOP) && (timeout < I2C_TIME_OUT)) { while ((I2C_CTL0(I2C0) & I2C_CTL0_STOP) && (timeout < I2C_TIME_OUT)) {
timeout++; timeout++;
} }
if (timeout >= I2C_TIME_OUT) { if (timeout < I2C_TIME_OUT) {
state = I2C_STATE_ERROR; timeout = 0;
break; state = I2C_END;
} i2c_timeout_flag = I2C_OK;
} else {
/* Success */ timeout = 0;
return I2C_STATUS_SUCCESS; state = I2C_START;
#ifdef DEBUG_VERBOES
case I2C_STATE_ERROR: printf("i2c master sends stop signal timeout in WRITE BYTE!\n");
/* Send stop condition to release bus */
i2c_stop_on_bus(I2C0);
/* Increment retry counter */
retry_count++;
if (retry_count >= I2C_MAX_RETRY) {
#ifdef DEBUG_VERBOSE
// printf("I2C write failed after %d retries\r\n", I2C_MAX_RETRY);
const char* msg5_prefix = "I2C write failed after ";
for (uint8_t i = 0; msg5_prefix[i] != '\0'; i++) {
while (usart_flag_get(I2C_DEBUG_UART, USART_FLAG_TBE) == RESET) {}
usart_data_transmit(I2C_DEBUG_UART, msg5_prefix[i]);
}
while (usart_flag_get(I2C_DEBUG_UART, USART_FLAG_TBE) == RESET) {}
usart_data_transmit(I2C_DEBUG_UART, '0' + I2C_MAX_RETRY);
const char* msg5_suffix = " retries\r\n";
for (uint8_t i = 0; msg5_suffix[i] != '\0'; i++) {
while (usart_flag_get(I2C_DEBUG_UART, USART_FLAG_TBE) == RESET) {}
usart_data_transmit(I2C_DEBUG_UART, msg5_suffix[i]);
}
while (usart_flag_get(I2C_DEBUG_UART, USART_FLAG_TC) == RESET) {}
#endif #endif
return I2C_STATUS_TIMEOUT;
} }
/* Reset state machine for retry */
state = I2C_STATE_START;
timeout = 0;
data_index = 0;
/* Small delay before retry */
delay_10us(10);
break; break;
default: default:
state = I2C_STATE_ERROR; state = I2C_START;
i2c_timeout_flag = I2C_OK;
timeout = 0;
#ifdef DEBUG_VERBOES
printf("i2c master sends start signal in WRITE BYTE.\n");
#endif
break; break;
} }
} }
return I2C_END;
return I2C_STATUS_TIMEOUT;
} }
/*! uint8_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data) {
\brief read 16-bit data from I2C device with improved state machine uint8_t state = I2C_START;
\param[in] slave_addr: 7-bit slave address uint8_t read_cycle = 0;
\param[in] reg_addr: register address
\param[out] data: pointer to 2-byte data buffer
\retval i2c_status_t
*/
i2c_status_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data) {
i2c_state_t state = I2C_STATE_START;
uint16_t timeout = 0; uint16_t timeout = 0;
uint8_t data_index = 0; uint8_t i2c_timeout_flag = 0;
uint8_t retry_count = 0; uint8_t number_of_byte = 2;
bool write_phase = true; /* First phase: write register address */
/* Parameter validation */ /* enable acknowledge */
if (data == NULL || slave_addr > 0x7F) {
return I2C_STATUS_INVALID_PARAM;
}
/* Enable acknowledge */
i2c_ack_config(I2C0, I2C_ACK_ENABLE); i2c_ack_config(I2C0, I2C_ACK_ENABLE);
while (retry_count < I2C_MAX_RETRY) { while (!(i2c_timeout_flag)) {
switch (state) { switch (state) {
case I2C_STATE_START: case I2C_START:
timeout = 0; if (RESET == read_cycle) {
/* Wait for bus to be idle */ /* i2c master sends start signal only when the bus is idle */
while (i2c_flag_get(I2C0, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT)) { while (i2c_flag_get(I2C0, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT)) {
timeout++;
}
if (timeout >= I2C_TIME_OUT) {
state = I2C_STATE_ERROR;
break;
}
/* Configure ACK position for 2-byte read */
if (!write_phase) {
i2c_ackpos_config(I2C0, I2C_ACKPOS_NEXT);
}
/* Send start condition */
i2c_start_on_bus(I2C0);
state = I2C_STATE_SEND_ADDRESS;
timeout = 0;
break;
case I2C_STATE_SEND_ADDRESS:
/* Wait for start condition to be sent */
while ((!i2c_flag_get(I2C0, I2C_FLAG_SBSEND)) && (timeout < I2C_TIME_OUT)) {
timeout++;
}
if (timeout >= I2C_TIME_OUT) {
state = I2C_STATE_ERROR;
break;
}
/* Send slave address */
if (write_phase) {
/* Write phase: send address with write bit */
i2c_master_addressing(I2C0, (slave_addr << 1), I2C_TRANSMITTER);
} else {
/* Read phase: send address with read bit */
i2c_master_addressing(I2C0, (slave_addr << 1) | 0x01, I2C_RECEIVER);
/* Disable ACK for last byte */
i2c_ack_config(I2C0, I2C_ACK_DISABLE);
}
state = I2C_STATE_CLEAR_ADDRESS;
timeout = 0;
break;
case I2C_STATE_CLEAR_ADDRESS:
/* Wait for address to be acknowledged */
while ((!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND)) && (timeout < I2C_TIME_OUT)) {
timeout++;
}
if (timeout >= I2C_TIME_OUT) {
state = I2C_STATE_ERROR;
break;
}
/* Clear address flag */
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
if (write_phase) {
state = I2C_STATE_TRANSMIT_REG;
} else {
/* For single byte read, send stop after clearing address */
if (data_index == 1) {
i2c_stop_on_bus(I2C0);
}
state = I2C_STATE_RECEIVE_DATA;
data_index = 0;
}
timeout = 0;
break;
case I2C_STATE_TRANSMIT_REG:
/* Wait for transmit buffer to be empty */
while ((!i2c_flag_get(I2C0, I2C_FLAG_TBE)) && (timeout < I2C_TIME_OUT)) {
timeout++;
}
if (timeout >= I2C_TIME_OUT) {
state = I2C_STATE_ERROR;
break;
}
/* Send register address */
i2c_data_transmit(I2C0, reg_addr);
state = I2C_STATE_RESTART;
timeout = 0;
break;
case I2C_STATE_RESTART:
/* Wait for byte transfer complete */
while ((!i2c_flag_get(I2C0, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) {
timeout++;
}
if (timeout >= I2C_TIME_OUT) {
state = I2C_STATE_ERROR;
break;
}
/* Switch to read phase */
write_phase = false;
state = I2C_STATE_START;
timeout = 0;
break;
case I2C_STATE_RECEIVE_DATA:
if (data_index < 2) {
if (data_index == 1) {
/* Wait for BTC before sending stop for last byte */
while ((!i2c_flag_get(I2C0, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) {
timeout++;
}
if (timeout >= I2C_TIME_OUT) {
state = I2C_STATE_ERROR;
break;
}
/* Send stop condition before reading last byte */
i2c_stop_on_bus(I2C0);
}
/* Wait for receive buffer not empty */
while ((!i2c_flag_get(I2C0, I2C_FLAG_RBNE)) && (timeout < I2C_TIME_OUT)) {
timeout++; timeout++;
} }
if (timeout >= I2C_TIME_OUT) { if (timeout < I2C_TIME_OUT) {
state = I2C_STATE_ERROR; /* whether to send ACK or not for the next byte */
break; i2c_ackpos_config(I2C0, I2C_ACKPOS_NEXT);
} else {
// i2c_bus_reset();
timeout = 0;
state = I2C_START;
#ifdef DEBUG_VERBOES
printf("i2c bus is busy in READ!\n");
#endif
}
}
/* send the start signal */
i2c_start_on_bus(I2C0);
timeout = 0;
state = I2C_SEND_ADDRESS;
break;
case I2C_SEND_ADDRESS:
/* i2c master sends START signal successfully */
while ((!i2c_flag_get(I2C0, I2C_FLAG_SBSEND)) && (timeout < I2C_TIME_OUT)) {
timeout++;
}
if (timeout < I2C_TIME_OUT) {
if (RESET == read_cycle) {
i2c_master_addressing(I2C0, slave_addr << 1, I2C_TRANSMITTER);
state = I2C_CLEAR_ADDRESS_FLAG;
} else {
i2c_master_addressing(I2C0, slave_addr << 1, I2C_RECEIVER);
i2c_ack_config(I2C0, I2C_ACK_DISABLE);
state = I2C_CLEAR_ADDRESS_FLAG;
} }
/* Read data byte */
data[data_index] = i2c_data_receive(I2C0);
data_index++;
timeout = 0; timeout = 0;
if (data_index >= 2) {
state = I2C_STATE_STOP;
}
} else { } else {
state = I2C_STATE_STOP; timeout = 0;
state = I2C_START;
read_cycle = RESET;
#ifdef DEBUG_VERBOES
printf("i2c master sends start signal timeout in READ!\n");
#endif
} }
break; break;
case I2C_CLEAR_ADDRESS_FLAG:
case I2C_STATE_STOP: /* address flag set means i2c slave sends ACK */
/* Wait for stop condition to complete */ while ((!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND)) && (timeout < I2C_TIME_OUT)) {
timeout++;
}
if (timeout < I2C_TIME_OUT) {
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
if ((SET == read_cycle) && (1 == number_of_byte)) {
/* send a stop condition to I2C bus */
i2c_stop_on_bus(I2C0);
}
timeout = 0;
state = I2C_TRANSMIT_DATA;
} else {
timeout = 0;
state = I2C_START;
read_cycle = RESET;
#ifdef DEBUG_VERBOES
printf("i2c master clears address flag timeout in READ!\n");
#endif
}
break;
case I2C_TRANSMIT_DATA:
if (RESET == read_cycle) {
/* wait until the transmit data buffer is empty */
while ((!i2c_flag_get(I2C0, I2C_FLAG_TBE)) && (timeout < I2C_TIME_OUT)) {
timeout++;
}
if (timeout < I2C_TIME_OUT) {
/* send the EEPROM's internal address to write to : only one byte address */
i2c_data_transmit(I2C0, reg_addr);
timeout = 0;
} else {
timeout = 0;
state = I2C_START;
read_cycle = RESET;
#ifdef DEBUG_VERBOES
printf("i2c master wait data buffer is empty timeout in READ!\n");
#endif
}
/* wait until BTC bit is set */
while ((!i2c_flag_get(I2C0, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) {
timeout++;
}
if (timeout < I2C_TIME_OUT) {
timeout = 0;
state = I2C_START;
read_cycle = SET;
} else {
timeout = 0;
state = I2C_START;
read_cycle = RESET;
#ifdef DEBUG_VERBOES
printf("i2c master sends register address timeout in READ!\n");
#endif
}
} else {
while (number_of_byte) {
timeout++;
if (2 == number_of_byte) {
/* wait until BTC bit is set */
while (!i2c_flag_get(I2C0, I2C_FLAG_BTC));
/* send a stop condition to I2C bus */
i2c_stop_on_bus(I2C0);
}
/* wait until RBNE bit is set */
if (i2c_flag_get(I2C0, I2C_FLAG_RBNE)) {
/* read a byte from the EEPROM */
*data = i2c_data_receive(I2C0);
/* point to the next location where the byte read will be saved */
data++;
/* decrement the read bytes counter */
number_of_byte--;
timeout = 0;
}
if (timeout > I2C_TIME_OUT) {
timeout = 0;
state = I2C_START;
read_cycle = 0;
#ifdef DEBUG_VERBOES
printf("i2c master sends data timeout in READ!\n");
#endif
}
}
timeout = 0;
state = I2C_STOP;
}
break;
case I2C_STOP:
/* i2c master sends STOP signal successfully */
while ((I2C_CTL0(I2C0) & I2C_CTL0_STOP) && (timeout < I2C_TIME_OUT)) { while ((I2C_CTL0(I2C0) & I2C_CTL0_STOP) && (timeout < I2C_TIME_OUT)) {
timeout++; timeout++;
} }
if (timeout >= I2C_TIME_OUT) { if (timeout < I2C_TIME_OUT) {
state = I2C_STATE_ERROR; timeout = 0;
break; state = I2C_END;
} i2c_timeout_flag = I2C_OK;
} else {
/* Success */ timeout = 0;
return I2C_STATUS_SUCCESS; state = I2C_START;
read_cycle = 0;
case I2C_STATE_ERROR: #ifdef DEBUG_VERBOES
/* Send stop condition to release bus */ printf("i2c master sends stop signal timeout in READ!\n");
i2c_stop_on_bus(I2C0);
/* Increment retry counter */
retry_count++;
if (retry_count >= I2C_MAX_RETRY) {
#ifdef DEBUG_VERBOSE
// printf("I2C read failed after %d retries\r\n", I2C_MAX_RETRY);
const char* msg6_prefix = "I2C read failed after ";
for (uint8_t i = 0; msg6_prefix[i] != '\0'; i++) {
while (usart_flag_get(I2C_DEBUG_UART, USART_FLAG_TBE) == RESET) {}
usart_data_transmit(I2C_DEBUG_UART, msg6_prefix[i]);
}
while (usart_flag_get(I2C_DEBUG_UART, USART_FLAG_TBE) == RESET) {}
usart_data_transmit(I2C_DEBUG_UART, '0' + I2C_MAX_RETRY);
const char* msg6_suffix = " retries\r\n";
for (uint8_t i = 0; msg6_suffix[i] != '\0'; i++) {
while (usart_flag_get(I2C_DEBUG_UART, USART_FLAG_TBE) == RESET) {}
usart_data_transmit(I2C_DEBUG_UART, msg6_suffix[i]);
}
while (usart_flag_get(I2C_DEBUG_UART, USART_FLAG_TC) == RESET) {}
#endif #endif
return I2C_STATUS_TIMEOUT;
} }
/* Reset state machine for retry */
state = I2C_STATE_START;
write_phase = true;
timeout = 0;
data_index = 0;
/* Small delay before retry */
delay_10us(10);
break; break;
default: default:
state = I2C_STATE_ERROR; state = I2C_START;
read_cycle = 0;
i2c_timeout_flag = I2C_OK;
timeout = 0;
#ifdef DEBUG_VERBOES
printf("i2c master sends start signal in READ.\n");
#endif
break; break;
} }
} }
return I2C_END;
return I2C_STATUS_TIMEOUT;
}
/*!
\brief get status string for debugging
\param[in] status: i2c_status_t value
\param[out] none
\retval const char* status string
*/
const char* i2c_get_status_string(i2c_status_t status) {
switch (status) {
case I2C_STATUS_SUCCESS:
return "SUCCESS";
case I2C_STATUS_TIMEOUT:
return "TIMEOUT";
case I2C_STATUS_NACK:
return "NACK";
case I2C_STATUS_BUS_BUSY:
return "BUS_BUSY";
case I2C_STATUS_ERROR:
return "ERROR";
case I2C_STATUS_INVALID_PARAM:
return "INVALID_PARAM";
default:
return "UNKNOWN";
}
} }

View File

@@ -1,367 +1,295 @@
// //
// Created by dell on 24-12-3. // Created by dell on 24-12-3.
// LDC1612 Inductive Sensor Driver Implementation
// //
#include "ldc1612.h" #include "ldc1612.h"
/* Private function prototypes */ /** @brief set conversion interval time.
static i2c_status_t ldc1612_write_register(uint8_t reg_addr, uint16_t value); @param channel LDC1612 has total two channels.
static i2c_status_t ldc1612_read_register(uint8_t reg_addr, uint16_t *value); @param result The value to be set.
static uint16_t ldc1612_calculate_clock_dividers(uint8_t channel); * */
static uint32_t ldc1612_parse_raw_result(uint32_t raw_result); void ldc1612_set_conversion_time(uint8_t channel, uint16_t result) {
uint8_t data[2] = {0};
data[0] = (result >> 8) & 0xFF;
data[1] = result & 0xFF;
/*! #ifdef SOFTWARE_IIC
\brief 初始化LDC1612传感器 soft_i2c_write_16bits(LDC1612_ADDR, SET_CONVERSION_TIME_REG_START + channel, data);
\param[in] none #else
\param[out] none i2c_write_16bits(LDC1612_ADDR, SET_CONVERSION_TIME_REG_START + channel, data);
\retval ldc1612_status_t #endif
*/
ldc1612_status_t ldc1612_init(void) {
i2c_status_t i2c_status;
uint16_t device_id, manufacturer_id;
/* 复位传感器 */
i2c_status = ldc1612_reset();
if (i2c_status != I2C_STATUS_SUCCESS) {
return LDC1612_STATUS_ERROR;
}
/* 等待复位完成 */
delay_ms(10);
/* 验证设备ID */
device_id = ldc1612_get_device_id();
manufacturer_id = ldc1612_get_manufacturer_id();
if (device_id != 0x3055 || manufacturer_id != 0x5449) {
return LDC1612_STATUS_ERROR;
}
return LDC1612_STATUS_SUCCESS;
} }
/*! /** @brief set conversion offset.
\brief 复位LDC1612传感器 @param channel LDC1612 has total two channels.
\param[in] none @param result The value to be set.
\param[out] none * */
\retval ldc1612_status_t void ldc1612_set_conversion_offset(uint8_t channel, uint16_t result) {
*/ uint8_t data[2] = {0};
ldc1612_status_t ldc1612_reset(void) { data[0] = (result >> 8) & 0xFF;
i2c_status_t status = ldc1612_write_register(LDC1612_RESET_DEV, LDC1612_RESET_VALUE); data[1] = result & 0xFF;
return (status == I2C_STATUS_SUCCESS) ? LDC1612_STATUS_SUCCESS : LDC1612_STATUS_ERROR; #ifdef SOFTWARE_IIC
soft_i2c_write_16bits(LDC1612_ADDR, SET_CONVERSION_OFFSET_REG_START + channel, data);
#else
i2c_write_16bits(LDC1612_ADDR, SET_CONVERSION_OFFSET_REG_START + channel, data);
#endif
} }
/*! /** @brief Before conversion,wait LC sensor stabilize for a short time.
\brief 配置单通道模式 @param channel LDC1612 has total two channels.
\param[in] channel: 通道号 (0或1) @param result The value to be set.
\param[out] none * */
\retval ldc1612_status_t void ldc1612_set_LC_stabilize_time(uint8_t channel, uint16_t result) {
*/ uint8_t data[2] = {0};
ldc1612_status_t ldc1612_config_single_channel(uint8_t channel) { data[0] = (result >> 8) & 0xFF;
i2c_status_t status; data[1] = result & 0xFF;
uint16_t clock_dividers; #ifdef SOFTWARE_IIC
soft_i2c_write_16bits(LDC1612_ADDR, SET_LC_STABILIZE_REG_START + channel, data);
if (channel > 1) { #else
return LDC1612_STATUS_INVALID_PARAM; i2c_write_16bits(LDC1612_ADDR, SET_LC_STABILIZE_REG_START + channel, data);
} #endif
/* 进入休眠模式进行配置 */
status = ldc1612_write_register(LDC1612_CONFIG, LDC1612_SENSOR_CONFIG_SLEEP);
if (status != I2C_STATUS_SUCCESS) return LDC1612_STATUS_ERROR;
/* 计算并设置时钟分频 */
clock_dividers = ldc1612_calculate_clock_dividers(channel);
status = ldc1612_write_register(LDC1612_CLOCK_DIVIDERS_CH0 + channel, clock_dividers);
if (status != I2C_STATUS_SUCCESS) return LDC1612_STATUS_ERROR;
/* 设置稳定时间 */
status = ldc1612_write_register(LDC1612_SETTLECOUNT_CH0 + channel, LDC1612_SETTLECOUNT_CH0_DEFAULT);
if (status != I2C_STATUS_SUCCESS) return LDC1612_STATUS_ERROR;
/* 设置转换时间 */
status = ldc1612_write_register(LDC1612_RCOUNT_CH0 + channel, LDC1612_CONVERSION_TIME_CH0);
if (status != I2C_STATUS_SUCCESS) return LDC1612_STATUS_ERROR;
/* 设置错误配置 */
status = ldc1612_write_register(LDC1612_ERROR_CONFIG, LDC1612_ERROR_CONFIG_DEFAULT);
if (status != I2C_STATUS_SUCCESS) return LDC1612_STATUS_ERROR;
/* 设置驱动电流 */
status = ldc1612_write_register(LDC1612_DRIVE_CURRENT_CH0 + channel, LDC1612_DRIVE_CURRENT_DEFAULT);
if (status != I2C_STATUS_SUCCESS) return LDC1612_STATUS_ERROR;
/* 设置MUX配置 */
status = ldc1612_write_register(LDC1612_MUX_CONFIG, LDC1612_MUX_CONFIG_DEFAULT);
if (status != I2C_STATUS_SUCCESS) return LDC1612_STATUS_ERROR;
/* 退出休眠模式,开始转换 */
status = ldc1612_write_register(LDC1612_CONFIG, LDC1612_SENSOR_CONFIG_ACTIVE);
if (status != I2C_STATUS_SUCCESS) return LDC1612_STATUS_ERROR;
return LDC1612_STATUS_SUCCESS;
} }
/*! /** @brief set input frequency divide and fref divide.
\brief 读取制造商ID @param channel LDC1612 has total two channels.
\param[in] none @param FIN_DIV FIN input divide
\param[out] none @param FREF_DIV fref,reference frequency of sensor.
\retval uint16_t 制造商ID * */
*/ void ldc1612_set_freq_divide(uint8_t channel) {
uint16_t ldc1612_get_manufacturer_id(void) { uint16_t value;
uint16_t id = 0; uint16_t fin_div, freq_div;
ldc1612_read_register(LDC1612_MANUFACTURER_ID, &id); float sensor_freq;
return id;
}
/*! sensor_freq = 1 / (2 * 3.14 * sqrt(COIL_L_UH * COIL_C_PF * pow(10, -18))) * pow(10, -6);
\brief 读取设备ID
\param[in] none
\param[out] none
\retval uint16_t 设备ID
*/
uint16_t ldc1612_get_device_id(void) {
uint16_t id = 0;
ldc1612_read_register(LDC1612_DEVICE_ID, &id);
return id;
}
/*! fin_div = (uint16_t) (sensor_freq / 8.75 + 1);
\brief 读取通道原始数据
\param[in] channel: 通道号 if (fin_div * 4 < 40) {
\param[out] result: 结果结构体指针 freq_div = 2;
\retval ldc1612_status_t
*/
ldc1612_status_t ldc1612_read_channel(uint8_t channel, ldc1612_result_t *result) {
uint16_t msb, lsb;
uint32_t raw_data;
i2c_status_t status;
if (channel > 1 || result == NULL) {
return LDC1612_STATUS_INVALID_PARAM;
}
/* 读取MSB */
status = ldc1612_read_register(LDC1612_DATA_CH0_MSB + (channel * 2), &msb);
if (status != I2C_STATUS_SUCCESS) return LDC1612_STATUS_ERROR;
/* 读取LSB */
status = ldc1612_read_register(LDC1612_DATA_CH0_LSB + (channel * 2), &lsb);
if (status != I2C_STATUS_SUCCESS) return LDC1612_STATUS_ERROR;
/* 组合32位数据 */
raw_data = ((uint32_t)msb << 16) | lsb;
/* 解析结果 */
result->raw_data = raw_data;
result->frequency = ldc1612_parse_raw_result(raw_data);
/* 检查错误 */
if (result->frequency >= 0x10000000) {
result->error_flag = true;
result->error_code = (result->frequency >> 24) & 0xFF;
return LDC1612_STATUS_ERROR;
} else { } else {
result->error_flag = false; freq_div = 4;
result->error_code = 0;
} }
return LDC1612_STATUS_SUCCESS; value = fin_div << 12;
value |= freq_div;
// printf("\tvalue: 0x%x\r\n", value);
uint8_t data[2] = {0};
data[0] = (value >> 8) & 0xFF;
data[1] = value & 0xFF;
// printf("\tFIN_DIV: %d, FREF_DIV: %d\r\n", fin_div, freq_div);
#ifdef SOFTWARE_IIC
soft_i2c_write_16bits(LDC1612_ADDR, SET_FREQ_REG_START + channel, data);
#else
i2c_write_16bits(LDC1612_ADDR, SET_FREQ_REG_START + channel, data);
#endif
} }
/*! /** @brief Error output config.
\brief 设置驱动电流 @param result The value to be set.
\param[in] channel: 通道号 * */
\param[in] current: 电流值 void ldc1612_set_error_config(uint16_t value) {
\param[out] none uint8_t data[2] = {0};
\retval ldc1612_status_t
*/
ldc1612_status_t ldc1612_set_drive_current(uint8_t channel, uint16_t current) {
if (channel > 1) {
return LDC1612_STATUS_INVALID_PARAM;
}
i2c_status_t status = ldc1612_write_register(LDC1612_DRIVE_CURRENT_CH0 + channel, current);
return (status == I2C_STATUS_SUCCESS) ? LDC1612_STATUS_SUCCESS : LDC1612_STATUS_ERROR;
}
/*!
\brief 自动检测驱动电流
\param[in] channel: 通道号
\param[out] none
\retval ldc1612_status_t
*/
ldc1612_status_t ldc1612_auto_detect_drive_current(uint8_t channel) {
uint16_t config_value, drive_current_reg;
uint16_t init_value, drive_current;
i2c_status_t status;
if (channel > 1) {
return LDC1612_STATUS_INVALID_PARAM;
}
/* 进入休眠模式 */
status = ldc1612_write_register(LDC1612_CONFIG, LDC1612_SENSOR_CONFIG_SLEEP);
if (status != I2C_STATUS_SUCCESS) return LDC1612_STATUS_ERROR;
/* 设置时钟分频 */
uint16_t clock_dividers = ldc1612_calculate_clock_dividers(channel);
status = ldc1612_write_register(LDC1612_CLOCK_DIVIDERS_CH0 + channel, clock_dividers);
if (status != I2C_STATUS_SUCCESS) return LDC1612_STATUS_ERROR;
/* 读取当前配置并禁用Rp覆盖 */
status = ldc1612_read_register(LDC1612_CONFIG, &config_value);
if (status != I2C_STATUS_SUCCESS) return LDC1612_STATUS_ERROR;
config_value &= ~(1 << 12); // 禁用RP_OVERRIDE_EN
status = ldc1612_write_register(LDC1612_CONFIG, config_value);
if (status != I2C_STATUS_SUCCESS) return LDC1612_STATUS_ERROR;
/* 启动测量 */
status = ldc1612_write_register(LDC1612_CONFIG, LDC1612_SENSOR_CONFIG_ACTIVE);
if (status != I2C_STATUS_SUCCESS) return LDC1612_STATUS_ERROR;
/* 等待至少一次转换完成 */
delay_ms(10);
/* 读取初始驱动电流值 */
status = ldc1612_read_register(LDC1612_DRIVE_CURRENT_CH0 + channel, &drive_current_reg);
if (status != I2C_STATUS_SUCCESS) return LDC1612_STATUS_ERROR;
init_value = (drive_current_reg >> 6) & 0x1F;
drive_current = (init_value << 11) | 0x0000;
/* 写入检测到的驱动电流 */
status = ldc1612_write_register(LDC1612_DRIVE_CURRENT_CH0 + channel, drive_current);
if (status != I2C_STATUS_SUCCESS) return LDC1612_STATUS_ERROR;
return LDC1612_STATUS_SUCCESS;
}
/*!
\brief 获取状态字符串
\param[in] status: 状态码
\param[out] none
\retval const char* 状态字符串
*/
const char* ldc1612_get_status_string(ldc1612_status_t status) {
switch (status) {
case LDC1612_STATUS_SUCCESS:
return "SUCCESS";
case LDC1612_STATUS_ERROR:
return "ERROR";
case LDC1612_STATUS_TIMEOUT:
return "TIMEOUT";
case LDC1612_STATUS_INVALID_PARAM:
return "INVALID_PARAM";
case LDC1612_STATUS_NO_COIL:
return "NO_COIL";
case LDC1612_STATUS_UNDER_RANGE:
return "UNDER_RANGE";
case LDC1612_STATUS_OVER_RANGE:
return "OVER_RANGE";
default:
return "UNKNOWN";
}
}
/* Private Functions Implementation */
/*!
\brief 写入寄存器
\param[in] reg_addr: 寄存器地址
\param[in] value: 写入值
\param[out] none
\retval i2c_status_t
*/
static i2c_status_t ldc1612_write_register(uint8_t reg_addr, uint16_t value) {
uint8_t data[2];
data[0] = (value >> 8) & 0xFF; data[0] = (value >> 8) & 0xFF;
data[1] = value & 0xFF; data[1] = value & 0xFF;
return i2c_write_16bits(LDC1612_ADDR, reg_addr, data); #ifdef SOFTWARE_IIC
soft_i2c_write_16bits(LDC1612_ADDR, ERROR_CONFIG_REG, data);
#else
i2c_write_16bits(LDC1612_ADDR, ERROR_CONFIG_REG, data);
#endif
} }
/*! /** @brief mux config.
\brief 读取寄存器 @param result The value to be set.
\param[in] reg_addr: 寄存器地址 * */
\param[out] value: 读取值指针 void ldc1612_set_mux_config(uint16_t value) {
\retval i2c_status_t uint8_t data[2] = {0};
*/ data[0] = (value >> 8) & 0xFF;
static i2c_status_t ldc1612_read_register(uint8_t reg_addr, uint16_t *value) { data[1] = value & 0xFF;
uint8_t data[2];
i2c_status_t status;
if (value == NULL) { #ifdef SOFTWARE_IIC
return I2C_STATUS_INVALID_PARAM; soft_i2c_write_16bits(LDC1612_ADDR, MUL_CONFIG_REG, data);
} #else
i2c_write_16bits(LDC1612_ADDR, MUL_CONFIG_REG, data);
status = i2c_read_16bits(LDC1612_ADDR, reg_addr, data); #endif
if (status == I2C_STATUS_SUCCESS) {
*value = ((uint16_t)data[0] << 8) | data[1];
}
return status;
} }
/*! /** @brief reset sensor.
\brief 计算时钟分频值
\param[in] channel: 通道号
\param[out] none
\retval uint16_t 分频值
*/
static uint16_t ldc1612_calculate_clock_dividers(uint8_t channel) {
uint16_t fin_div, fref_div;
float sensor_freq;
/* 计算传感器频率 (MHz) */ * */
sensor_freq = 1.0f / (2.0f * 3.14159f * sqrtf(LDC1612_COIL_L_UH * LDC1612_COIL_C_PF * 1e-18f)) * 1e-6f; void ldc1612_reset_sensor(void) {
uint8_t data[2] = {0};
data[0] = 0x80;
data[1] = 0x00;
/* 计算FIN分频 */ #ifdef SOFTWARE_IIC
fin_div = (uint16_t)(sensor_freq / 8.75f + 1); soft_i2c_write_16bits(LDC1612_ADDR, SENSOR_RESET_REG, data);
#else
/* 计算FREF分频 */ i2c_write_16bits(LDC1612_ADDR, SENSOR_RESET_REG, data);
if (fin_div * 4 < 40) { #endif
fref_div = 2;
} else {
fref_div = 4;
}
return (fin_div << 12) | fref_div;
} }
/*! /** @brief set drive current of sensor.
\brief 解析原始结果 @param result The value to be set.
\param[in] raw_result: 原始数据 * */
\param[out] none void ldc1612_set_drive_current(uint8_t channel, uint16_t value) {
\retval uint32_t 解析后的数据 uint8_t data[2] = {0};
*/ data[0] = (value >> 8) & 0xFF;
static uint32_t ldc1612_parse_raw_result(uint32_t raw_result) { data[1] = value & 0xFF;
uint32_t calibration_value;
uint8_t error_code; #ifdef SOFTWARE_IIC
soft_i2c_write_16bits(LDC1612_ADDR, SET_DRIVER_CURRENT_REG + channel, data);
#else
i2c_write_16bits(LDC1612_ADDR, SET_DRIVER_CURRENT_REG + channel, data);
#endif
}
/** @brief Main config part of sensor.Contains select channel、start conversion、sleep mode、sensor activation mode、INT pin disable ..
@param result The value to be set.
* */
void ldc1612_set_sensor_config(uint16_t value) {
uint8_t data[2] = {0};
data[0] = (value >> 8) & 0xFF;
data[1] = value & 0xFF;
#ifdef SOFTWARE_IIC
soft_i2c_write_16bits(LDC1612_ADDR, SENSOR_CONFIG_REG, data);
#else
i2c_write_16bits(LDC1612_ADDR, SENSOR_CONFIG_REG, data);
#endif
}
void ldc1612_single_ch0_config(void) {
ldc1612_set_freq_divide(CHANNEL_0); //0x14 --0x1002
ldc1612_set_LC_stabilize_time(CHANNEL_0, LC_STABILIZE_TIME_CH0); //0x10 --0x001E
ldc1612_set_conversion_time(CHANNEL_0, LDC1612_CONVERSION_TIME_CH0); //0x08 --0x0546
ldc1612_set_error_config(LDC1612_ERROR_CONFIG); //0x19 --0x0000)
ldc1612_set_drive_current(CHANNEL_0, LDC1612_DRIVE_CURRENT); //0x1E --0x9000
ldc1612_set_mux_config(LDC1612_MUX_CONFIG); //0x1B --0x020C
ldc1612_set_sensor_config(LDC1612_SENSOR_CONFIG); //0x1A --0x1601
}
void ldc1612_iic_get_sensor_infomation(void) {
uint8_t data[2] = {0};
#ifdef SOFTWARE_IIC
soft_i2c_read_16bits(LDC1612_ADDR, READ_MANUFACTURER_ID, data);
#else
i2c_read_16bits(LDC1612_ADDR, READ_MANUFACTURER_ID, data);
#endif
printf("\tManufacturer: 0x%x", (data[0] << 8) | data[1]);
#ifdef SOFTWARE_IIC
soft_i2c_read_16bits(LDC1612_ADDR, READ_DEVICE_ID, data);
#else
i2c_read_16bits(LDC1612_ADDR, READ_DEVICE_ID, data);
#endif
printf("\tDevice: 0x%x", (data[0] << 8) | data[1]);
}
uint16_t ldc1612_get_manufacturer_id(void) {
uint8_t data[2] = {0};
#ifdef SOFTWARE_IIC
soft_i2c_read_16bits(LDC1612_ADDR, READ_MANUFACTURER_ID, data);
#else
i2c_read_16bits(LDC1612_ADDR, READ_MANUFACTURER_ID, data);
#endif
return (data[0] << 8) | data[1];
}
uint16_t ldc1612_get_deveice_id(void) {
uint8_t data[2] = {0};
#ifdef SOFTWARE_IIC
soft_i2c_read_16bits(LDC1612_ADDR, READ_DEVICE_ID, data);
#else
i2c_read_16bits(LDC1612_ADDR, READ_DEVICE_ID, data);
#endif
return (data[0] << 8) | data[1];
}
/** @brief read the raw channel result from register.
@param channel LDC1612 has total two channels.
@param result raw data
* */
uint32_t ldc1612_get_raw_channel_result(uint8_t channel) {
uint32_t raw_value = 0;
uint8_t value[2] = {0};
#ifdef SOFTWARE_IIC
soft_i2c_read_16bits(LDC1612_ADDR, CONVERTION_RESULT_REG_START + channel, value);
#else
i2c_read_16bits(LDC1612_ADDR, CONVERTION_RESULT_REG_START + channel, value);
#endif
raw_value |= (uint32_t) ((value[0] << 8) | value[1]) << 16;
#ifdef SOFTWARE_IIC
soft_i2c_read_16bits(LDC1612_ADDR, CONVERTION_RESULT_REG_START + channel + 1, value);
#else
i2c_read_16bits(LDC1612_ADDR, CONVERTION_RESULT_REG_START + channel + 1, value);
#endif
raw_value |= (uint32_t) ((value[0] << 8) | value[1]);
return ldc1612_parse_raw_result(raw_value);
}
/** @brief parse the data which read from data register.
@param channel LDC1612 has total two channels.
@param raw_result the raw data which read from data register,it contains error codes and sensor value;
* */
uint32_t ldc1612_parse_raw_result(uint32_t raw_result) {
uint32_t calibration_value = 0;
uint8_t error_code = 0;
calibration_value = raw_result & 0x0FFFFFFF; calibration_value = raw_result & 0x0FFFFFFF;
if (0xFFFFFFF == calibration_value) {
/* 检查无线圈错误 */ return 0xF0000000;
if (calibration_value == 0x0FFFFFFF) { // ERR_NC-No coil detected!!!
return LDC1612_ERROR_NO_COIL;
} }
error_code = (raw_result >> 24) & 0xFF; error_code = raw_result >> 24;
/* 检查各种错误 */
if (error_code & 0x80) { if (error_code & 0x80) {
return LDC1612_ERROR_UNDER_RANGE; return 0x80000000;
// ERR_UR-Under range error!!!
} }
if (error_code & 0x40) { if (error_code & 0x40) {
return LDC1612_ERROR_OVER_RANGE; return 0x40000000;
// ERR_OR-Over range error!!!
} }
if (error_code & 0x20) { if (error_code & 0x20) {
return LDC1612_ERROR_WATCHDOG; return 0x20000000;
// ERR_WD-Watch dog timeout error!!!
} }
if (error_code & 0x10) { if (error_code & 0x10) {
return LDC1612_ERROR_AMPLITUDE; return 0x10000000;
// ERR_AE-error!!!
} }
return calibration_value; return raw_result;
} }
void ldc1612_drvie_current_detect(uint8_t channel) {
uint8_t data[2] = {0};
uint16_t init_value = 0 , drive_current = 0;
ldc1612_set_sensor_config(LDC1612_SLEEP_MODE);
ldc1612_set_freq_divide(channel);
soft_i2c_read_16bits(LDC1612_ADDR, SENSOR_CONFIG_REG, data);
ldc1612_set_sensor_config(LDC1612_SLEEP_MODE);
ldc1612_set_sensor_config(LDC1612_SENSOR_CONFIG); //0x1A --0x1601
delay_ms(10);
soft_i2c_read_16bits(LDC1612_ADDR, SET_DRIVER_CURRENT_REG, data);
init_value = (((data[0] << 8) | data[1]) >> 6) & 0x1F;
drive_current = (init_value << 11) | 0x0000;
printf("init value: 0x%x\tdrive current: 0x%x\n", init_value, drive_current);
}

View File

@@ -40,7 +40,7 @@ OF SUCH DAMAGE.
#include <stdio.h> #include <stdio.h>
#include "i2c.h" #include "i2c.h"
#include "board_config.h" #include "board_config.h"
#include "sensor_example.h" #include "ldc1612.h"
bool g_status_switch = false; bool g_status_switch = false;
@@ -75,17 +75,24 @@ int main(void)
i2c_config(); i2c_config();
i2c_scan();
// i2c_bus_reset(); // i2c_bus_reset();
// uint8_t sensor_data[2] = {0};
// i2c_read_16bits(0x2B, 0x7E, sensor_data);
// printf("Sensor Data: 0x%02X 0x%02X\r\n", sensor_data[0], sensor_data[1]);
// i2c_bus_reset();
ldc1612_iic_get_sensor_infomation();
#ifdef DEBUG_VERBOSE #ifdef DEBUG_VERBOSE
i2c_scan(); i2c_scan();
#endif #endif
sensors_init_example();
sensors_read_example();
while(1){ while(1){
command_process(); command_process();
delay_ms(100); delay_ms(100);

1191
i2c_wait.c Normal file

File diff suppressed because it is too large Load Diff