generated from hulk/gd32e23x_template_cmake_vscode
Compare commits
14 Commits
cda1a2284d
...
main
Author | SHA1 | Date | |
---|---|---|---|
749cc2d3e8 | |||
399b81005a | |||
633b2583b2 | |||
b24c0853c9 | |||
77a6525168 | |||
6cc7b2dae2 | |||
8adabcd08d | |||
dd7549d62b | |||
90486c6609 | |||
88f79f7eb0 | |||
4e0ad6e8eb | |||
54bf206ec3 | |||
93294b0e3c | |||
d7029d91e7 |
4
.vscode/settings.json
vendored
4
.vscode/settings.json
vendored
@@ -4,6 +4,10 @@
|
|||||||
"Git Bash": {
|
"Git Bash": {
|
||||||
"path": "C:\\Program Files\\Git\\bin\\bash.exe",
|
"path": "C:\\Program Files\\Git\\bin\\bash.exe",
|
||||||
"icon": "terminal-bash"
|
"icon": "terminal-bash"
|
||||||
|
},
|
||||||
|
"Git-Bash": {
|
||||||
|
"path": "D:\\Git\\bin\\bash.exe",
|
||||||
|
"icon": "terminal-bash"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"terminal.integrated.defaultProfile.windows": "Git-Bash",
|
"terminal.integrated.defaultProfile.windows": "Git-Bash",
|
||||||
|
22
.vscode/tasks.json
vendored
22
.vscode/tasks.json
vendored
@@ -11,7 +11,11 @@
|
|||||||
"Build",
|
"Build",
|
||||||
"Flash MCU"
|
"Flash MCU"
|
||||||
],
|
],
|
||||||
"dependsOrder": "sequence"
|
"dependsOrder": "sequence",
|
||||||
|
"icon": {
|
||||||
|
"id": "insert",
|
||||||
|
"tooltip": "Build and Flash"
|
||||||
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"label": "Flash MCU",
|
"label": "Flash MCU",
|
||||||
@@ -31,6 +35,10 @@
|
|||||||
},
|
},
|
||||||
"presentation": {
|
"presentation": {
|
||||||
"clear": true
|
"clear": true
|
||||||
|
},
|
||||||
|
"icon": {
|
||||||
|
"id": "gather",
|
||||||
|
"tooltip": "Flash MCU"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -51,6 +59,10 @@
|
|||||||
},
|
},
|
||||||
"presentation": {
|
"presentation": {
|
||||||
"clear": true
|
"clear": true
|
||||||
|
},
|
||||||
|
"icon": {
|
||||||
|
"id": "discard",
|
||||||
|
"tooltip": "Reset MCU"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -71,6 +83,10 @@
|
|||||||
},
|
},
|
||||||
"presentation": {
|
"presentation": {
|
||||||
"clear": true
|
"clear": true
|
||||||
|
},
|
||||||
|
"icon": {
|
||||||
|
"id": "clear-all",
|
||||||
|
"tooltip": "Erase MCU"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -119,6 +135,10 @@
|
|||||||
},
|
},
|
||||||
"presentation": {
|
"presentation": {
|
||||||
"clear": true
|
"clear": true
|
||||||
|
},
|
||||||
|
"icon": {
|
||||||
|
"id": "code",
|
||||||
|
"tooltip": "Build"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
@@ -29,6 +29,7 @@ set(TARGET_SRC
|
|||||||
Src/led.c
|
Src/led.c
|
||||||
Src/uart_ring_buffer.c
|
Src/uart_ring_buffer.c
|
||||||
Src/command.c
|
Src/command.c
|
||||||
|
Src/i2c.c
|
||||||
)
|
)
|
||||||
|
|
||||||
# 设置输出目录
|
# 设置输出目录
|
||||||
|
@@ -26,6 +26,8 @@
|
|||||||
#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_DEBUG_UART USART0
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
#define LED_PORT GPIOA
|
#define LED_PORT GPIOA
|
||||||
|
@@ -16,6 +16,9 @@
|
|||||||
* @{
|
* @{
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/** @brief 传感器周期上报使能标志 */
|
||||||
|
extern volatile bool g_sensor_report_enabled;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @section Command_Protocol 协议格式
|
* @section Command_Protocol 协议格式
|
||||||
* 接收命令帧格式:
|
* 接收命令帧格式:
|
||||||
|
102
Inc/i2c.h
102
Inc/i2c.h
@@ -8,7 +8,6 @@
|
|||||||
#include "gd32e23x_it.h"
|
#include "gd32e23x_it.h"
|
||||||
#include "gd32e23x.h"
|
#include "gd32e23x.h"
|
||||||
#include "systick.h"
|
#include "systick.h"
|
||||||
#include "main.h"
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@@ -19,35 +18,110 @@
|
|||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
#define I2C_SPEED 100*(1000)
|
#define I2C_SPEED 100000U /* 100kHz */
|
||||||
|
#define I2C_TIME_OUT 5000U /* 5000 loops timeout */
|
||||||
|
#define I2C_MAX_RETRY 3U /* Maximum retry attempts */
|
||||||
|
#define I2C_DELAY_10US 10U /* Delay in microseconds for bus reset */
|
||||||
|
#define I2C_RECOVERY_CLOCKS 9U /* Clock pulses for bus recovery */
|
||||||
|
#define I2C_MASTER_ADDRESS 0x00U /* Master address (not used) */
|
||||||
|
|
||||||
#define I2C_TIME_OUT (uint16_t)(5000)
|
/* Legacy compatibility */
|
||||||
#define I2C_OK 1
|
#define I2C_OK 1
|
||||||
#define I2C_FAIL 0
|
#define I2C_FAIL 0
|
||||||
#define I2C_END 1
|
#define I2C_END 1
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
|
/* I2C result enumeration */
|
||||||
typedef enum {
|
typedef enum {
|
||||||
I2C_START = 0,
|
I2C_RESULT_SUCCESS = 0, /* Operation successful */
|
||||||
I2C_SEND_ADDRESS,
|
I2C_RESULT_TIMEOUT, /* Timeout occurred */
|
||||||
I2C_CLEAR_ADDRESS_FLAG,
|
I2C_RESULT_NACK, /* No acknowledge received */
|
||||||
I2C_TRANSMIT_DATA,
|
I2C_RESULT_BUS_BUSY, /* Bus is busy */
|
||||||
I2C_STOP
|
I2C_RESULT_ERROR, /* General error */
|
||||||
} i2c_process_enum;
|
I2C_RESULT_INVALID_PARAM, /* Invalid parameter */
|
||||||
|
I2C_RECOVERY_OK,
|
||||||
|
I2C_RECOVERY_SDA_STUCK_LOW,
|
||||||
|
I2C_RECOVERY_SCL_STUCK_LOW
|
||||||
|
} i2c_result_t;
|
||||||
|
|
||||||
|
/* I2C state machine enumeration */
|
||||||
|
typedef enum {
|
||||||
|
I2C_STATE_IDLE = 0, /* Idle state */
|
||||||
|
I2C_STATE_START, /* Generate start condition */
|
||||||
|
I2C_STATE_SEND_ADDRESS, /* Send slave address */
|
||||||
|
I2C_STATE_CLEAR_ADDRESS, /* Clear address flag */
|
||||||
|
I2C_STATE_TRANSMIT_REG, /* Transmit register address */
|
||||||
|
I2C_STATE_TRANSMIT_DATA, /* Transmit data */
|
||||||
|
I2C_STATE_RESTART, /* Generate restart condition */
|
||||||
|
I2C_STATE_RECEIVE_DATA, /* Receive data */
|
||||||
|
I2C_STATE_STOP, /* Generate stop condition */
|
||||||
|
I2C_STATE_ERROR, /* Error state */
|
||||||
|
I2C_STATE_END
|
||||||
|
} i2c_state_t;
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
void i2c_gpio_config(void);
|
|
||||||
|
|
||||||
void i2c_config(void);
|
/* Function declarations */
|
||||||
|
/*!
|
||||||
|
\brief configure the I2C interface
|
||||||
|
\param[in] none
|
||||||
|
\param[out] none
|
||||||
|
\retval i2c_result_t
|
||||||
|
*/
|
||||||
|
i2c_result_t i2c_config(void);
|
||||||
|
|
||||||
void i2c_bus_reset(void);
|
/*!
|
||||||
|
\brief reset I2C bus with proper recovery
|
||||||
|
\param[in] none
|
||||||
|
\param[out] none
|
||||||
|
\retval i2c_result_t
|
||||||
|
*/
|
||||||
|
i2c_result_t i2c_bus_reset(void);
|
||||||
|
|
||||||
|
/*!
|
||||||
|
\brief scan I2C bus for devices
|
||||||
|
\param[in] none
|
||||||
|
\param[out] none
|
||||||
|
\retval none
|
||||||
|
*/
|
||||||
void i2c_scan(void);
|
void i2c_scan(void);
|
||||||
|
|
||||||
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
|
||||||
|
\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_result_t
|
||||||
|
*/
|
||||||
|
i2c_result_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data[2]);
|
||||||
|
|
||||||
uint8_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data);
|
/*!
|
||||||
|
\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
|
||||||
|
*/
|
||||||
|
i2c_result_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data);
|
||||||
|
|
||||||
|
/*!
|
||||||
|
\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
|
||||||
|
*/
|
||||||
|
i2c_result_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data);
|
||||||
|
|
||||||
|
/*!
|
||||||
|
\brief get status string for debugging
|
||||||
|
\param[in] status: i2c_result_t value
|
||||||
|
\param[out] none
|
||||||
|
\retval const char* status string
|
||||||
|
*/
|
||||||
|
const char* i2c_get_status_string(i2c_result_t status);
|
||||||
|
|
||||||
#endif //I2C_H
|
#endif //I2C_H
|
||||||
|
28
Inc/sensor_example.h
Normal file
28
Inc/sensor_example.h
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
//
|
||||||
|
// Sensor Usage Example Header
|
||||||
|
// 传感器使用示例头文件
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef SENSOR_EXAMPLE_H
|
||||||
|
#define SENSOR_EXAMPLE_H
|
||||||
|
|
||||||
|
#include "gd32e23x.h"
|
||||||
|
#include "board_config.h"
|
||||||
|
|
||||||
|
/*!
|
||||||
|
\brief 传感器初始化示例
|
||||||
|
\param[in] none
|
||||||
|
\param[out] none
|
||||||
|
\retval none
|
||||||
|
*/
|
||||||
|
void sensors_init_example(void);
|
||||||
|
|
||||||
|
/*!
|
||||||
|
\brief 传感器读取示例
|
||||||
|
\param[in] none
|
||||||
|
\param[out] none
|
||||||
|
\retval none
|
||||||
|
*/
|
||||||
|
void sensors_read_example(void);
|
||||||
|
|
||||||
|
#endif // SENSOR_EXAMPLE_H
|
@@ -8,7 +8,6 @@
|
|||||||
#include "gd32e23x_it.h"
|
#include "gd32e23x_it.h"
|
||||||
#include "gd32e23x.h"
|
#include "gd32e23x.h"
|
||||||
#include "systick.h"
|
#include "systick.h"
|
||||||
#include "main.h"
|
|
||||||
|
|
||||||
#include "board_config.h"
|
#include "board_config.h"
|
||||||
|
|
||||||
|
437
Src/command.c
437
Src/command.c
@@ -77,7 +77,7 @@
|
|||||||
* ============================================================================ */
|
* ============================================================================ */
|
||||||
|
|
||||||
/** @brief 传感器周期上报使能标志 */
|
/** @brief 传感器周期上报使能标志 */
|
||||||
static volatile bool s_sensor_report_enabled = false;
|
volatile bool g_sensor_report_enabled = false;
|
||||||
|
|
||||||
/** @name 预设响应数据
|
/** @name 预设响应数据
|
||||||
* @{ */
|
* @{ */
|
||||||
@@ -96,7 +96,7 @@ static const uint8_t s_report_status_err[] = { 'e','r','r' }; /**< 错误响应
|
|||||||
*/
|
*/
|
||||||
bool get_sensor_report_enabled(void)
|
bool get_sensor_report_enabled(void)
|
||||||
{
|
{
|
||||||
return s_sensor_report_enabled;
|
return g_sensor_report_enabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -108,7 +108,7 @@ bool get_sensor_report_enabled(void)
|
|||||||
*/
|
*/
|
||||||
void set_sensor_report_status(bool status)
|
void set_sensor_report_status(bool status)
|
||||||
{
|
{
|
||||||
s_sensor_report_enabled = status;
|
g_sensor_report_enabled = status;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -234,8 +234,8 @@ static uint8_t parse_uint_dec(const uint8_t *s, uint8_t n, uint32_t *out)
|
|||||||
* - 带参数命令:M<数字>S<参数>(如 M100S123,参数为十进制)
|
* - 带参数命令:M<数字>S<参数>(如 M100S123,参数为十进制)
|
||||||
*
|
*
|
||||||
* 支持的命令:
|
* 支持的命令:
|
||||||
* - M1: 开启LED,启用传感器上报
|
* - M1: 启用传感器上报
|
||||||
* - M2: 关闭LED,禁用传感器上报
|
* - M2: 禁用传感器上报
|
||||||
* - M100S<value>: 设置PWM值(示例)
|
* - M100S<value>: 设置PWM值(示例)
|
||||||
*
|
*
|
||||||
* @param frame 指向完整命令帧的缓冲区(从包头0xD5开始)。
|
* @param frame 指向完整命令帧的缓冲区(从包头0xD5开始)。
|
||||||
@@ -275,28 +275,425 @@ void handle_command(const uint8_t *frame, uint8_t len) {
|
|||||||
if (cmd_index == cmd_len) {
|
if (cmd_index == cmd_len) {
|
||||||
// 仅基础命令,如 M1, M2, M3
|
// 仅基础命令,如 M1, M2, M3
|
||||||
switch (base_cmd) {
|
switch (base_cmd) {
|
||||||
case 1u: // M1命令
|
case 1u: // M1: enable sensor report
|
||||||
set_sensor_report_status(true);
|
set_sensor_report_status(true);
|
||||||
// led_on();
|
send_response(RESP_TYPE_OK, s_report_status_ok, sizeof(s_report_status_ok));
|
||||||
// send_response(RESP_TYPE_OK, s_report_status_ok, sizeof(s_report_status_ok));
|
|
||||||
uint8_t test_response1[] = { 0xAA, 0xBB, 0xCC, 0xDD };
|
|
||||||
send_response(RESP_TYPE_OK, test_response1, sizeof(test_response1));
|
|
||||||
return;
|
return;
|
||||||
case 2u: // M2命令
|
case 2u: // M2: disable sensor report
|
||||||
set_sensor_report_status(false);
|
set_sensor_report_status(false);
|
||||||
// led_off();
|
send_response(RESP_TYPE_OK, s_report_status_ok, sizeof(s_report_status_ok));
|
||||||
// send_response(RESP_TYPE_OK, s_report_status_ok, sizeof(s_report_status_ok));
|
|
||||||
uint8_t test_response2[] = { 0xDD, 0xCC, 0xBB, 0xAA };
|
|
||||||
send_response(RESP_TYPE_OK, test_response2, sizeof(test_response2));
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// 示例:M3、M10、M201、M100 等(按需添加)
|
// 示例:M3、M10、M201、M100 等(按需添加)
|
||||||
case 3u: // M3命令
|
// case 3u: // M3命令 - 高电流驱动测试
|
||||||
send_response(RESP_TYPE_OK, s_report_status_ok, sizeof(s_report_status_ok));
|
// /**
|
||||||
return;
|
// * M3命令:使用更高驱动电流测试线圈响应
|
||||||
// case 10u: // M10命令
|
// * 响应格式:6字节状态信息
|
||||||
// send_response(RESP_TYPE_OK, s_report_status_ok, sizeof(s_report_status_ok));
|
// *
|
||||||
|
// * 响应数据解析:
|
||||||
|
// * [0-1]: 传感器状态寄存器(大端序)
|
||||||
|
// * bit[15-8]: 预留
|
||||||
|
// * bit[7]: DRDY_1 - 通道1数据就绪
|
||||||
|
// * bit[6]: DRDY_0 - 通道0数据就绪
|
||||||
|
// * bit[5]: UNREAD_CONV - 未读转换结果
|
||||||
|
// * bit[4]: ERR_ZC - 零计数错误
|
||||||
|
// * bit[3]: ERR_AE - 幅度错误(重点关注)
|
||||||
|
// * bit[2]: ERR_WD - 看门狗超时
|
||||||
|
// * bit[1]: ERR_OR - 过量程错误
|
||||||
|
// * bit[0]: ERR_UR - 欠量程错误
|
||||||
|
// * [2]: 数据就绪标志 (0x01=就绪, 0x00=未就绪)
|
||||||
|
// * [3]: 0xA0 - 高电流测试标记
|
||||||
|
// * [4]: 幅度错误专用标志 (0xAE=有幅度错误, 0x00=无)
|
||||||
|
// * [5]: 0x33 - M3命令标记
|
||||||
|
// *
|
||||||
|
// * 分析要点:
|
||||||
|
// * - 如果[0-1]从0x0008变为其他值,说明高电流有效果
|
||||||
|
// * - 如果[2]变为0x01,说明数据开始就绪
|
||||||
|
// * - 如果[4]变为0x00,说明幅度错误消失
|
||||||
|
// */
|
||||||
|
// // 重置传感器
|
||||||
|
// ldc1612_reset_sensor();
|
||||||
|
// delay_ms(50);
|
||||||
|
|
||||||
|
// // 使用更高的驱动电流重新配置
|
||||||
|
// // ldc1612_write_register(SET_DRIVER_CURRENT_REG, 0xA000);
|
||||||
|
// delay_ms(10);
|
||||||
|
|
||||||
|
// // 重新配置其他参数
|
||||||
|
// ldc1612_config_single_channel(CHANNEL_0);
|
||||||
|
// delay_ms(200); // 更长稳定时间
|
||||||
|
|
||||||
|
// // 检查结果
|
||||||
|
// uint16_t status_m3 = ldc1612_get_sensor_status();
|
||||||
|
// bool ready_m3 = ldc1612_is_data_ready(CHANNEL_0);
|
||||||
|
|
||||||
|
// uint8_t m3_info[6];
|
||||||
|
// m3_info[0] = (uint8_t)(status_m3 >> 8);
|
||||||
|
// m3_info[1] = (uint8_t)(status_m3 & 0xFF);
|
||||||
|
// m3_info[2] = ready_m3 ? 0x01 : 0x00;
|
||||||
|
// m3_info[3] = 0xA0; // 高电流标记
|
||||||
|
// m3_info[4] = (status_m3 & 0x0008) ? 0xAE : 0x00; // 幅度错误标志
|
||||||
|
// m3_info[5] = 0x33; // M3命令标记
|
||||||
|
|
||||||
|
// send_response(RESP_TYPE_OK, m3_info, sizeof(m3_info));
|
||||||
// return;
|
// return;
|
||||||
|
// case 4u: // M4命令 - 寄存器诊断
|
||||||
|
// /**
|
||||||
|
// * M4命令:读取关键寄存器进行配置诊断
|
||||||
|
// * 响应格式:8字节寄存器信息
|
||||||
|
// *
|
||||||
|
// * 响应数据解析:
|
||||||
|
// * [0-1]: 状态寄存器 (0x18) - 当前传感器状态
|
||||||
|
// * [2-3]: 传感器配置寄存器 (0x1A) - 传感器工作模式
|
||||||
|
// * 期望值: 0x1601 (活动模式,单通道)
|
||||||
|
// * [4-5]: 驱动电流寄存器 (0x1E) - 当前驱动电流设置
|
||||||
|
// * 常见值: 0x9000(默认), 0xA000(高), 0xF800(最高)
|
||||||
|
// * [6]: I2C读取状态 (0x4F='O'=成功, 0xEE=失败)
|
||||||
|
// * [7]: 0x44 - M4命令标记
|
||||||
|
// *
|
||||||
|
// * 分析要点:
|
||||||
|
// * - [2-3]应该是0x1601,如果不是说明配置异常
|
||||||
|
// * - [4-5]显示实际的驱动电流设置
|
||||||
|
// * - [6]必须是0x4F,否则I2C通信有问题
|
||||||
|
// */
|
||||||
|
// // 简化版本,只读取最关键的寄存器,避免I2C超时
|
||||||
|
// uint16_t status_reg = ldc1612_get_sensor_status(); // 0x18
|
||||||
|
|
||||||
|
// // 逐一安全读取关键寄存器
|
||||||
|
// uint8_t data_buf[2] = {0};
|
||||||
|
// uint16_t sensor_config = 0;
|
||||||
|
// uint16_t drive_current = 0;
|
||||||
|
|
||||||
|
// // 尝试读取传感器配置寄存器
|
||||||
|
// bool result1_ok = (LDC1612_IIC_READ_16BITS(LDC1612_ADDR, SENSOR_CONFIG_REG, data_buf) == I2C_RESULT_SUCCESS);
|
||||||
|
// if (result1_ok) {
|
||||||
|
// sensor_config = (data_buf[0] << 8) | data_buf[1];
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // 尝试读取驱动电流寄存器
|
||||||
|
// bool result2_ok = (LDC1612_IIC_READ_16BITS(LDC1612_ADDR, SET_DRIVER_CURRENT_REG, data_buf) == I2C_RESULT_SUCCESS);
|
||||||
|
// if (result2_ok) {
|
||||||
|
// drive_current = (data_buf[0] << 8) | data_buf[1];
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // 构造8字节简化诊断信息
|
||||||
|
// uint8_t diag_info[8];
|
||||||
|
// diag_info[0] = (uint8_t)(status_reg >> 8); // 状态寄存器高位
|
||||||
|
// diag_info[1] = (uint8_t)(status_reg & 0xFF); // 状态寄存器低位
|
||||||
|
// diag_info[2] = (uint8_t)(sensor_config >> 8); // 传感器配置寄存器高位
|
||||||
|
// diag_info[3] = (uint8_t)(sensor_config & 0xFF); // 传感器配置寄存器低位
|
||||||
|
// diag_info[4] = (uint8_t)(drive_current >> 8); // 驱动电流寄存器高位
|
||||||
|
// diag_info[5] = (uint8_t)(drive_current & 0xFF); // 驱动电流寄存器低位
|
||||||
|
// diag_info[6] = (result1_ok && result2_ok) ? 0x4F : 0xEE; // I2C状态
|
||||||
|
// diag_info[7] = 0x44; // M4命令标记
|
||||||
|
|
||||||
|
// send_response(RESP_TYPE_OK, diag_info, sizeof(diag_info));
|
||||||
|
// return;
|
||||||
|
// case 5u: // M5命令 - 最高电流启动测试
|
||||||
|
// // 命令: D5 03 02 4D 35 87
|
||||||
|
// // 响应: B5 F0 08 [状态2字节][就绪标志][电流设置2字节][幅度错误标志][M5标记][最高电流标记] CRC
|
||||||
|
// // 响应格式:
|
||||||
|
// // [0-1]: 传感器状态寄存器(启动后状态)
|
||||||
|
// // [2]: 数据就绪标志 (0x01=就绪, 0x00=未就绪)
|
||||||
|
// // [3-4]: 实际驱动电流设置值(应该是0xF800)
|
||||||
|
// // [5]: 幅度错误专用标志 (0xAE=仍有错误, 0x00=错误消失)
|
||||||
|
// // [6]: 0x55 - M5命令标记
|
||||||
|
// // [7]: 0xF8 - 最高电流标记
|
||||||
|
// // 重置传感器
|
||||||
|
// ldc1612_reset_sensor();
|
||||||
|
// delay_ms(100);
|
||||||
|
|
||||||
|
// // 使用最高驱动电流并固定配置
|
||||||
|
// // ldc1612_write_register(SET_DRIVER_CURRENT_REG, 0xF800);
|
||||||
|
// delay_ms(10);
|
||||||
|
|
||||||
|
// // 手动配置其他必要寄存器,避免被覆盖
|
||||||
|
// // 配置频率分频器为较低频率 (更容易起振)
|
||||||
|
// uint8_t freq_data[2] = {0x10, 0x00}; // 较低分频
|
||||||
|
// LDC1612_IIC_WRITE_16BITS(LDC1612_ADDR, SET_FREQ_REG_START + CHANNEL_0, freq_data);
|
||||||
|
// delay_ms(10);
|
||||||
|
|
||||||
|
// // 设置较长的LC稳定时间
|
||||||
|
// uint8_t lc_data[2] = {0x04, 0x00}; // 更长稳定时间
|
||||||
|
// LDC1612_IIC_WRITE_16BITS(LDC1612_ADDR, SET_LC_STABILIZE_REG_START + CHANNEL_0, lc_data);
|
||||||
|
// delay_ms(10);
|
||||||
|
|
||||||
|
// // 配置MUX为单通道模式
|
||||||
|
// // ldc1612_configure_mux_register(0, CHANNEL_0, LDC1612_MUX_RR_SEQUENCE_1, LDC1612_MUX_FILTER_1MHz);
|
||||||
|
// delay_ms(10);
|
||||||
|
|
||||||
|
// // 启动传感器
|
||||||
|
// uint8_t sensor_cfg_data[2] = {0x16, 0x01}; // 活动模式,单通道
|
||||||
|
// LDC1612_IIC_WRITE_16BITS(LDC1612_ADDR, SENSOR_CONFIG_REG, sensor_cfg_data);
|
||||||
|
// delay_ms(200); // 更长稳定时间
|
||||||
|
|
||||||
|
// // 读取结果
|
||||||
|
// uint16_t status_m5 = ldc1612_get_sensor_status();
|
||||||
|
// bool ready_m5 = ldc1612_is_data_ready(CHANNEL_0);
|
||||||
|
|
||||||
|
// // 再次确认驱动电流设置
|
||||||
|
// uint8_t curr_data[2];
|
||||||
|
// LDC1612_IIC_READ_16BITS(LDC1612_ADDR, SET_DRIVER_CURRENT_REG, curr_data);
|
||||||
|
// uint16_t actual_current = (curr_data[0] << 8) | curr_data[1];
|
||||||
|
|
||||||
|
// uint8_t m5_info[8];
|
||||||
|
// m5_info[0] = (uint8_t)(status_m5 >> 8);
|
||||||
|
// m5_info[1] = (uint8_t)(status_m5 & 0xFF);
|
||||||
|
// m5_info[2] = ready_m5 ? 0x01 : 0x00;
|
||||||
|
// m5_info[3] = (uint8_t)(actual_current >> 8); // 实际电流设置高位
|
||||||
|
// m5_info[4] = (uint8_t)(actual_current & 0xFF); // 实际电流设置低位
|
||||||
|
// m5_info[5] = (status_m5 & 0x0008) ? 0xAE : 0x00; // 幅度错误标志
|
||||||
|
// m5_info[6] = 0x55; // M5命令标记
|
||||||
|
// m5_info[7] = 0xF8; // 最高电流标记
|
||||||
|
|
||||||
|
// send_response(RESP_TYPE_OK, m5_info, sizeof(m5_info));
|
||||||
|
// return;
|
||||||
|
// case 6u: // M6命令 - 芯片功能验证
|
||||||
|
// // 命令: D5 03 02 4D 36 88
|
||||||
|
// // 响应: B5 F0 0C [写入值2字节][读取值2字节][制造商ID2字节][设备ID2字节][状态2字节][ID读取状态][M6标记] CRC
|
||||||
|
// // 响应格式:
|
||||||
|
// // [0-1]: 写入测试值 (0x9000)
|
||||||
|
// // [2-3]: 读取回的值
|
||||||
|
// // [4-5]: 制造商ID (应该是0x5449="TI")
|
||||||
|
// // [6-7]: 设备ID (应该是0x3055)
|
||||||
|
// // [8-9]: 当前状态寄存器
|
||||||
|
// // [10]: ID读取状态 (0x4F=成功, 0xEE=失败)
|
||||||
|
// // [11]: 0x66 - M6命令标记
|
||||||
|
// // 测试1: 写入和读取特定值到驱动电流寄存器
|
||||||
|
// uint8_t test_current_data[2] = {0x90, 0x00}; // 写入0x9000
|
||||||
|
// LDC1612_IIC_WRITE_16BITS(LDC1612_ADDR, SET_DRIVER_CURRENT_REG, test_current_data);
|
||||||
|
// delay_ms(10);
|
||||||
|
|
||||||
|
// // 读取验证
|
||||||
|
// uint8_t read_current_data[2];
|
||||||
|
// LDC1612_IIC_READ_16BITS(LDC1612_ADDR, SET_DRIVER_CURRENT_REG, read_current_data);
|
||||||
|
// uint16_t read_current = (read_current_data[0] << 8) | read_current_data[1];
|
||||||
|
|
||||||
|
// // 测试2: 读取制造商ID和设备ID
|
||||||
|
// uint8_t manufacturer_data[2];
|
||||||
|
// uint8_t device_data[2];
|
||||||
|
// bool id_read_ok = true;
|
||||||
|
|
||||||
|
// if (LDC1612_IIC_READ_16BITS(LDC1612_ADDR, 0x7E, manufacturer_data) != I2C_RESULT_SUCCESS) {
|
||||||
|
// id_read_ok = false;
|
||||||
|
// }
|
||||||
|
// if (LDC1612_IIC_READ_16BITS(LDC1612_ADDR, 0x7F, device_data) != I2C_RESULT_SUCCESS) {
|
||||||
|
// id_read_ok = false;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// uint16_t manufacturer_id = id_read_ok ? ((manufacturer_data[0] << 8) | manufacturer_data[1]) : 0x0000;
|
||||||
|
// uint16_t device_id = id_read_ok ? ((device_data[0] << 8) | device_data[1]) : 0x0000;
|
||||||
|
|
||||||
|
// // 测试3: 检查当前状态
|
||||||
|
// uint16_t current_status = ldc1612_get_sensor_status();
|
||||||
|
|
||||||
|
// // 构造12字节测试结果
|
||||||
|
// uint8_t test_info[12];
|
||||||
|
// test_info[0] = 0x90; // 写入的值高位
|
||||||
|
// test_info[1] = 0x00; // 写入的值低位
|
||||||
|
// test_info[2] = (uint8_t)(read_current >> 8); // 读取的值高位
|
||||||
|
// test_info[3] = (uint8_t)(read_current & 0xFF); // 读取的值低位
|
||||||
|
// test_info[4] = (uint8_t)(manufacturer_id >> 8);
|
||||||
|
// test_info[5] = (uint8_t)(manufacturer_id & 0xFF);
|
||||||
|
// test_info[6] = (uint8_t)(device_id >> 8);
|
||||||
|
// test_info[7] = (uint8_t)(device_id & 0xFF);
|
||||||
|
// test_info[8] = (uint8_t)(current_status >> 8);
|
||||||
|
// test_info[9] = (uint8_t)(current_status & 0xFF);
|
||||||
|
// test_info[10] = id_read_ok ? 0x4F : 0xEE; // ID读取状态
|
||||||
|
// test_info[11] = 0x66; // M6命令标记
|
||||||
|
|
||||||
|
// send_response(RESP_TYPE_OK, test_info, sizeof(test_info));
|
||||||
|
// return;
|
||||||
|
// case 7u: // M7命令 - 保守参数测试
|
||||||
|
// // 命令: D5 03 02 4D 37 89
|
||||||
|
// // 响应: B5 F0 0A [状态2字节][就绪标志][频率设置2字节][幅度错误标志][欠量程错误标志][过量程错误标志][M7标记][低频标记] CRC
|
||||||
|
// // 响应格式:
|
||||||
|
// // [0-1]: 状态寄存器
|
||||||
|
// // [2]: 数据就绪标志
|
||||||
|
// // [3-4]: 实际频率分频器设置 (0x2000=较低频率)
|
||||||
|
// // [5]: 幅度错误标志 (0xAE=有错误, 0x00=无)
|
||||||
|
// // [6]: 欠量程错误标志 (0x01=有, 0x00=无)
|
||||||
|
// // [7]: 过量程错误标志 (0x02=有, 0x00=无)
|
||||||
|
// // [8]: 0x77 - M7命令标记
|
||||||
|
// // [9]: 0x20 - 低频标记
|
||||||
|
// // 重置传感器
|
||||||
|
// ldc1612_reset_sensor();
|
||||||
|
// delay_ms(100);
|
||||||
|
|
||||||
|
// // 使用保守的配置尝试启动线圈
|
||||||
|
// // 1. 设置最高驱动电流
|
||||||
|
// uint8_t drive_data[2] = {0xF8, 0x00}; // 最高电流
|
||||||
|
// LDC1612_IIC_WRITE_16BITS(LDC1612_ADDR, SET_DRIVER_CURRENT_REG, drive_data);
|
||||||
|
// delay_ms(10);
|
||||||
|
|
||||||
|
// // 2. 设置较低的频率分频器(适合更大电感值)
|
||||||
|
// uint8_t freq_low_data[2] = {0x20, 0x00}; // 更低频率
|
||||||
|
// LDC1612_IIC_WRITE_16BITS(LDC1612_ADDR, SET_FREQ_REG_START + CHANNEL_0, freq_low_data);
|
||||||
|
// delay_ms(10);
|
||||||
|
|
||||||
|
// // 3. 设置更长的LC稳定时间
|
||||||
|
// uint8_t lc_stable_data[2] = {0x08, 0x00}; // 更长稳定时间
|
||||||
|
// LDC1612_IIC_WRITE_16BITS(LDC1612_ADDR, SET_LC_STABILIZE_REG_START + CHANNEL_0, lc_stable_data);
|
||||||
|
// delay_ms(10);
|
||||||
|
|
||||||
|
// // 4. 设置更长的转换时间
|
||||||
|
// uint8_t conv_time_data[2] = {0x04, 0x00}; // 更长转换时间
|
||||||
|
// LDC1612_IIC_WRITE_16BITS(LDC1612_ADDR, SET_CONVERSION_TIME_REG_START + CHANNEL_0, conv_time_data);
|
||||||
|
// delay_ms(10);
|
||||||
|
|
||||||
|
// // 5. 设置转换偏移
|
||||||
|
// uint8_t conv_offset_data[2] = {0x00, 0x00};
|
||||||
|
// LDC1612_IIC_WRITE_16BITS(LDC1612_ADDR, SET_CONVERSION_OFFSET_REG_START + CHANNEL_0, conv_offset_data);
|
||||||
|
// delay_ms(10);
|
||||||
|
|
||||||
|
// // 6. 配置错误寄存器 - 降低错误敏感度
|
||||||
|
// uint8_t error_config_data[2] = {0x00, 0x00}; // 允许所有错误
|
||||||
|
// LDC1612_IIC_WRITE_16BITS(LDC1612_ADDR, ERROR_CONFIG_REG, error_config_data);
|
||||||
|
// delay_ms(10);
|
||||||
|
|
||||||
|
// // 7. 配置MUX寄存器
|
||||||
|
// // ldc1612_configure_mux_register(0, CHANNEL_0, LDC1612_MUX_RR_SEQUENCE_1, LDC1612_MUX_FILTER_1MHz);
|
||||||
|
// delay_ms(10);
|
||||||
|
|
||||||
|
// // 8. 启动传感器
|
||||||
|
// uint8_t sensor_start_data[2] = {0x16, 0x01}; // 活动模式
|
||||||
|
// LDC1612_IIC_WRITE_16BITS(LDC1612_ADDR, SENSOR_CONFIG_REG, sensor_start_data);
|
||||||
|
// delay_ms(500); // 给予充分时间稳定
|
||||||
|
|
||||||
|
// // 检查结果
|
||||||
|
// uint16_t status_m7 = ldc1612_get_sensor_status();
|
||||||
|
// bool ready_m7 = ldc1612_is_data_ready(CHANNEL_0);
|
||||||
|
|
||||||
|
// // 读取实际配置的频率分频器确认
|
||||||
|
// uint8_t freq_readback[2];
|
||||||
|
// LDC1612_IIC_READ_16BITS(LDC1612_ADDR, SET_FREQ_REG_START + CHANNEL_0, freq_readback);
|
||||||
|
// uint16_t freq_actual = (freq_readback[0] << 8) | freq_readback[1];
|
||||||
|
|
||||||
|
// uint8_t m7_info[10];
|
||||||
|
// m7_info[0] = (uint8_t)(status_m7 >> 8);
|
||||||
|
// m7_info[1] = (uint8_t)(status_m7 & 0xFF);
|
||||||
|
// m7_info[2] = ready_m7 ? 0x01 : 0x00;
|
||||||
|
// m7_info[3] = (uint8_t)(freq_actual >> 8); // 实际频率分频器
|
||||||
|
// m7_info[4] = (uint8_t)(freq_actual & 0xFF);
|
||||||
|
// m7_info[5] = (status_m7 & 0x0008) ? 0xAE : 0x00; // 幅度错误
|
||||||
|
// m7_info[6] = (status_m7 & 0x0001) ? 0x01 : 0x00; // 欠量程错误
|
||||||
|
// m7_info[7] = (status_m7 & 0x0002) ? 0x02 : 0x00; // 过量程错误
|
||||||
|
// m7_info[8] = 0x77; // M7命令标记
|
||||||
|
// m7_info[9] = 0x20; // 低频标记
|
||||||
|
|
||||||
|
// send_response(RESP_TYPE_OK, m7_info, sizeof(m7_info));
|
||||||
|
// return;
|
||||||
|
// case 8u: // M8命令 - 极端参数测试
|
||||||
|
// // 命令: D5 03 02 4D 38 8A
|
||||||
|
// // 响应: B5 F0 06 [状态2字节][就绪标志][幅度错误标志][M8标记][极端测试标记] CRC
|
||||||
|
// // 响应格式:
|
||||||
|
// // [0-1]: 传感器状态寄存器
|
||||||
|
// // [2]: 数据就绪标志 (0x01=就绪, 0x00=未就绪)
|
||||||
|
// // [3]: 幅度错误标志 (0xAE=仍有错误, 0x00=错误消失)
|
||||||
|
// // [4]: 0x88 - M8命令标记
|
||||||
|
// // [5]: 0xEE - 极端测试标记
|
||||||
|
// {
|
||||||
|
// // 重置传感器
|
||||||
|
// ldc1612_reset_sensor();
|
||||||
|
// delay_ms(100);
|
||||||
|
|
||||||
|
// // 极端配置1: 极低频率
|
||||||
|
// uint8_t extreme_freq[2] = {0x40, 0x00};
|
||||||
|
// LDC1612_IIC_WRITE_16BITS(LDC1612_ADDR, SET_FREQ_REG_START + CHANNEL_0, extreme_freq);
|
||||||
|
// delay_ms(10);
|
||||||
|
|
||||||
|
// // 极端配置2: 最大驱动电流
|
||||||
|
// uint8_t max_drive[2] = {0xFF, 0x00};
|
||||||
|
// LDC1612_IIC_WRITE_16BITS(LDC1612_ADDR, SET_DRIVER_CURRENT_REG, max_drive);
|
||||||
|
// delay_ms(10);
|
||||||
|
|
||||||
|
// // 极端配置3: 禁用错误检测
|
||||||
|
// uint8_t no_errors[2] = {0x00, 0x00};
|
||||||
|
// LDC1612_IIC_WRITE_16BITS(LDC1612_ADDR, ERROR_CONFIG_REG, no_errors);
|
||||||
|
// delay_ms(10);
|
||||||
|
|
||||||
|
// // 启动传感器
|
||||||
|
// uint8_t start_data[2] = {0x16, 0x01};
|
||||||
|
// LDC1612_IIC_WRITE_16BITS(LDC1612_ADDR, SENSOR_CONFIG_REG, start_data);
|
||||||
|
// delay_ms(1000); // 等待1秒
|
||||||
|
|
||||||
|
// // 读取状态
|
||||||
|
// uint16_t status_8 = ldc1612_get_sensor_status();
|
||||||
|
// bool ready_8 = ldc1612_is_data_ready(CHANNEL_0);
|
||||||
|
|
||||||
|
// uint8_t m8_result[6];
|
||||||
|
// m8_result[0] = (uint8_t)(status_8 >> 8);
|
||||||
|
// m8_result[1] = (uint8_t)(status_8 & 0xFF);
|
||||||
|
// m8_result[2] = ready_8 ? 0x01 : 0x00;
|
||||||
|
// m8_result[3] = (status_8 & 0x0008) ? 0xAE : 0x00; // 幅度错误
|
||||||
|
// m8_result[4] = 0x88; // M8标记
|
||||||
|
// m8_result[5] = 0xEE; // 极端测试标记
|
||||||
|
|
||||||
|
// send_response(RESP_TYPE_OK, m8_result, sizeof(m8_result));
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
// case 9u: // M9命令 - 多频率特性测试
|
||||||
|
// // 命令: D5 03 02 4D 39 8B
|
||||||
|
// // 响应: B5 F0 08 [高频状态2字节][高频就绪标志][低频状态2字节][低频就绪标志][M9标记][多频测试标记] CRC
|
||||||
|
// // 响应格式:
|
||||||
|
// // [0-1]: 高频测试状态
|
||||||
|
// // [2]: 高频就绪标志 (0x01=就绪, 0x00=未就绪)
|
||||||
|
// // [3-4]: 低频测试状态
|
||||||
|
// // [5]: 低频就绪标志 (0x01=就绪, 0x00=未就绪)
|
||||||
|
// // [6]: 0x99 - M9命令标记
|
||||||
|
// // [7]: 0xAA - 多频测试标记
|
||||||
|
// {
|
||||||
|
// // 测试1: 高频配置
|
||||||
|
// ldc1612_reset_sensor();
|
||||||
|
// delay_ms(50);
|
||||||
|
|
||||||
|
// uint8_t high_freq[2] = {0x04, 0x00}; // 高频
|
||||||
|
// uint8_t low_drive[2] = {0x80, 0x00}; // 低电流
|
||||||
|
|
||||||
|
// LDC1612_IIC_WRITE_16BITS(LDC1612_ADDR, SET_FREQ_REG_START + CHANNEL_0, high_freq);
|
||||||
|
// LDC1612_IIC_WRITE_16BITS(LDC1612_ADDR, SET_DRIVER_CURRENT_REG, low_drive);
|
||||||
|
// delay_ms(10);
|
||||||
|
|
||||||
|
// // 启动高频测试
|
||||||
|
// uint8_t start_hf[2] = {0x16, 0x01};
|
||||||
|
// LDC1612_IIC_WRITE_16BITS(LDC1612_ADDR, SENSOR_CONFIG_REG, start_hf);
|
||||||
|
// delay_ms(200);
|
||||||
|
|
||||||
|
// uint16_t hf_status = ldc1612_get_sensor_status();
|
||||||
|
// bool hf_ready = ldc1612_is_data_ready(CHANNEL_0);
|
||||||
|
|
||||||
|
// // 测试2: 低频配置
|
||||||
|
// uint8_t sleep_mode[2] = {0x20, 0x01};
|
||||||
|
// LDC1612_IIC_WRITE_16BITS(LDC1612_ADDR, SENSOR_CONFIG_REG, sleep_mode);
|
||||||
|
// delay_ms(50);
|
||||||
|
|
||||||
|
// uint8_t low_freq[2] = {0x20, 0x00}; // 低频
|
||||||
|
// uint8_t high_drive[2] = {0xC0, 0x00}; // 高电流
|
||||||
|
|
||||||
|
// LDC1612_IIC_WRITE_16BITS(LDC1612_ADDR, SET_FREQ_REG_START + CHANNEL_0, low_freq);
|
||||||
|
// LDC1612_IIC_WRITE_16BITS(LDC1612_ADDR, SET_DRIVER_CURRENT_REG, high_drive);
|
||||||
|
// delay_ms(10);
|
||||||
|
|
||||||
|
// // 启动低频测试
|
||||||
|
// LDC1612_IIC_WRITE_16BITS(LDC1612_ADDR, SENSOR_CONFIG_REG, start_hf);
|
||||||
|
// delay_ms(200);
|
||||||
|
|
||||||
|
// uint16_t lf_status = ldc1612_get_sensor_status();
|
||||||
|
// bool lf_ready = ldc1612_is_data_ready(CHANNEL_0);
|
||||||
|
|
||||||
|
// uint8_t m9_result[8];
|
||||||
|
// m9_result[0] = (uint8_t)(hf_status >> 8); // 高频状态
|
||||||
|
// m9_result[1] = (uint8_t)(hf_status & 0xFF);
|
||||||
|
// m9_result[2] = hf_ready ? 0x01 : 0x00; // 高频就绪
|
||||||
|
// m9_result[3] = (uint8_t)(lf_status >> 8); // 低频状态
|
||||||
|
// m9_result[4] = (uint8_t)(lf_status & 0xFF);
|
||||||
|
// m9_result[5] = lf_ready ? 0x01 : 0x00; // 低频就绪
|
||||||
|
// m9_result[6] = 0x99; // M9标记
|
||||||
|
// m9_result[7] = 0xAA; // 多频测试标记
|
||||||
|
|
||||||
|
// send_response(RESP_TYPE_OK, m9_result, sizeof(m9_result));
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
// case 201u: // M201命令
|
// case 201u: // M201命令
|
||||||
// send_response(RESP_TYPE_OK, s_report_status_ok, sizeof(s_report_status_ok));
|
// send_response(RESP_TYPE_OK, s_report_status_ok, sizeof(s_report_status_ok));
|
||||||
// return;
|
// return;
|
||||||
|
823
Src/i2c.c
823
Src/i2c.c
@@ -31,7 +31,7 @@ void i2c_gpio_config(void) {
|
|||||||
\param[out] none
|
\param[out] none
|
||||||
\retval none
|
\retval none
|
||||||
*/
|
*/
|
||||||
void i2c_config(void) {
|
i2c_result_t i2c_config(void) {
|
||||||
/* configure I2C GPIO */
|
/* configure I2C GPIO */
|
||||||
i2c_gpio_config();
|
i2c_gpio_config();
|
||||||
/* enable I2C clock */
|
/* enable I2C clock */
|
||||||
@@ -44,6 +44,27 @@ void i2c_config(void) {
|
|||||||
i2c_enable(I2C0);
|
i2c_enable(I2C0);
|
||||||
/* enable acknowledge */
|
/* enable acknowledge */
|
||||||
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
||||||
|
|
||||||
|
return I2C_RESULT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* wait for SCL to go high, return true if successful, false if timeout */
|
||||||
|
static bool i2c_wait_scl_high(uint16_t max_wait_time) {
|
||||||
|
while (max_wait_time--) {
|
||||||
|
if (gpio_input_bit_get(I2C_SCL_PORT, I2C_SCL_PIN)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
delay_10us(1);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* generate one manual SCL pulse; return true if SCL observed high (no stuck/overstretch) */
|
||||||
|
static bool i2c_generate_scl_pulse(void) {
|
||||||
|
GPIO_BC(I2C_SCL_PORT) = I2C_SCL_PIN; /* drive SCL low */
|
||||||
|
delay_10us(1);
|
||||||
|
GPIO_BOP(I2C_SCL_PORT) = I2C_SCL_PIN; /* release SCL (open-drain -> high via pull-up) */
|
||||||
|
return i2c_wait_scl_high(200); /* wait up to ~2ms for clock stretching release */
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@@ -52,31 +73,87 @@ void i2c_config(void) {
|
|||||||
\param[out] none
|
\param[out] none
|
||||||
\retval none
|
\retval none
|
||||||
*/
|
*/
|
||||||
void i2c_bus_reset(void) {
|
i2c_result_t i2c_bus_reset(void) {
|
||||||
|
/* 1. Disable & deinit peripheral so pins can be fully controlled */
|
||||||
|
i2c_disable(I2C0);
|
||||||
i2c_deinit(I2C0);
|
i2c_deinit(I2C0);
|
||||||
/* configure SDA/SCL for GPIO */
|
|
||||||
GPIO_BC(I2C_SCL_PORT) |= I2C_SCL_PIN;
|
#ifdef DEBUG_VERBOSE
|
||||||
GPIO_BC(I2C_SDA_PORT) |= I2C_SDA_PIN;
|
printf("I2C bus reset\r\n");
|
||||||
gpio_output_options_set(I2C_SCL_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, I2C_SCL_PIN);
|
#endif
|
||||||
gpio_output_options_set(I2C_SDA_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, I2C_SDA_PIN);
|
|
||||||
__NOP();
|
/* 2. Configure SCL/SDA as GPIO open-drain outputs with pull-up and release them */
|
||||||
__NOP();
|
gpio_mode_set(I2C_SCL_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, I2C_SCL_PIN);
|
||||||
__NOP();
|
gpio_mode_set(I2C_SDA_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, I2C_SDA_PIN);
|
||||||
__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 */
|
gpio_bit_set(I2C_SCL_PORT, I2C_SCL_PIN); /* release SCL */
|
||||||
|
gpio_bit_set(I2C_SDA_PORT, I2C_SDA_PIN); /* release SDA */
|
||||||
|
|
||||||
|
#ifdef DEBUG_VERBOSE
|
||||||
|
printf("I2C bus reset: SCL = %d, SDA = %d\r\n", gpio_input_bit_get(I2C_SCL_PORT, I2C_SCL_PIN), gpio_input_bit_get(I2C_SDA_PORT, I2C_SDA_PIN));
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* 3. Double sample to confirm bus state */
|
||||||
|
delay_10us(1);
|
||||||
|
bool scl_value1 = gpio_input_bit_get(I2C_SCL_PORT, I2C_SCL_PIN);
|
||||||
|
bool sda_value1 = gpio_input_bit_get(I2C_SDA_PORT, I2C_SDA_PIN);
|
||||||
|
delay_10us(1);
|
||||||
|
bool scl_value2 = gpio_input_bit_get(I2C_SCL_PORT, I2C_SCL_PIN);
|
||||||
|
bool sda_value2 = gpio_input_bit_get(I2C_SDA_PORT, I2C_SDA_PIN);
|
||||||
|
|
||||||
|
/* 4. If SCL low -> stuck (cannot proceed) */
|
||||||
|
if (!scl_value2) {
|
||||||
|
#ifdef DEBUG_VERBOSE
|
||||||
|
printf("I2C bus reset: SCL stuck low\r\n");
|
||||||
|
#endif
|
||||||
|
return I2C_RECOVERY_SCL_STUCK_LOW;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 5. Fast path: bus idle */
|
||||||
|
if (scl_value1 && sda_value1 && scl_value2 && sda_value2) {
|
||||||
i2c_config();
|
i2c_config();
|
||||||
|
#ifdef DEBUG_VERBOSE
|
||||||
|
printf("I2C bus reset: bus idle\r\n");
|
||||||
|
#endif
|
||||||
|
return I2C_RECOVERY_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 6. SDA low: attempt to free by generating up to I2C_RECOVERY_CLOCKS pulses */
|
||||||
|
if (scl_value2 && !sda_value2) {
|
||||||
|
bool sda_released = false;
|
||||||
|
#ifdef DEBUG_VERBOSE
|
||||||
|
printf("I2C bus reset: SCL will try to free SDA\r\n");
|
||||||
|
#endif
|
||||||
|
for (uint8_t i = 0; i < I2C_RECOVERY_CLOCKS && !sda_released; i++) {
|
||||||
|
if (!i2c_generate_scl_pulse()) {
|
||||||
|
return I2C_RECOVERY_SCL_STUCK_LOW; /* SCL failed to go high */
|
||||||
|
}
|
||||||
|
if (gpio_input_bit_get(I2C_SDA_PORT, I2C_SDA_PIN)) {
|
||||||
|
sda_released = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!sda_released) {
|
||||||
|
return I2C_RECOVERY_SDA_STUCK_LOW;
|
||||||
|
}
|
||||||
|
/* 7. Generate a STOP condition to leave bus in idle state */
|
||||||
|
#ifdef DEBUG_VERBOSE
|
||||||
|
printf("I2C bus reset: generating STOP condition\r\n");
|
||||||
|
#endif
|
||||||
|
gpio_bit_reset(I2C_SDA_PORT, I2C_SDA_PIN); /* SDA low */
|
||||||
|
delay_10us(1);
|
||||||
|
gpio_bit_set(I2C_SCL_PORT, I2C_SCL_PIN); /* ensure SCL high */
|
||||||
|
delay_10us(1);
|
||||||
|
gpio_bit_set(I2C_SDA_PORT, I2C_SDA_PIN); /* SDA rising while SCL high -> STOP */
|
||||||
|
delay_10us(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef DEBUG_VERBOSE
|
||||||
|
printf("I2C bus reset: bus recovered\r\n");
|
||||||
|
#endif
|
||||||
|
/* 8. Reconfigure & enable peripheral */
|
||||||
|
i2c_config();
|
||||||
|
return I2C_RECOVERY_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -91,7 +168,13 @@ 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;
|
||||||
@@ -119,7 +202,24 @@ 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++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -128,349 +228,450 @@ 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]) {
|
i2c_result_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data[2]) {
|
||||||
uint8_t state = I2C_START;
|
i2c_state_t state = I2C_STATE_START;
|
||||||
uint16_t timeout = 0;
|
uint16_t timeout = 0;
|
||||||
uint8_t i2c_timeout_flag = 0;
|
uint8_t retry_count = 0;
|
||||||
|
|
||||||
/* enable acknowledge */
|
/* parameter validation */
|
||||||
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
if (data == NULL || slave_addr > 0x7F) {
|
||||||
while (!(i2c_timeout_flag)) {
|
return I2C_RESULT_INVALID_PARAM;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (retry_count < I2C_MAX_RETRY) {
|
||||||
switch (state) {
|
switch (state) {
|
||||||
case I2C_START:
|
case I2C_STATE_START:
|
||||||
/* i2c master sends start signal only when the bus is idle */
|
timeout = 0;
|
||||||
|
|
||||||
|
/* 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;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
i2c_start_on_bus(I2C0);
|
i2c_start_on_bus(I2C0);
|
||||||
timeout = 0;
|
timeout = 0;
|
||||||
state = I2C_SEND_ADDRESS;
|
state = I2C_STATE_SEND_ADDRESS;
|
||||||
} else {
|
|
||||||
timeout = 0;
|
|
||||||
state = I2C_START;
|
|
||||||
#ifdef DEBUG_VERBOES
|
|
||||||
printf("i2c bus is busy in WRITE BYTE!\n");
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case I2C_SEND_ADDRESS:
|
|
||||||
/* i2c master sends START signal successfully */
|
case I2C_STATE_SEND_ADDRESS:
|
||||||
|
/* wait for start condition to be sent. SBSEND flag */
|
||||||
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) {
|
||||||
i2c_master_addressing(I2C0, slave_addr, I2C_TRANSMITTER);
|
state = I2C_STATE_ERROR;
|
||||||
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
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case I2C_CLEAR_ADDRESS_FLAG:
|
}
|
||||||
/* address flag set means i2c slave sends ACK */
|
|
||||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND)) && (timeout < I2C_TIME_OUT)) {
|
/* send slave address */
|
||||||
|
i2c_master_addressing(I2C0, slave_addr << 1, I2C_TRANSMITTER);
|
||||||
|
timeout = 0;
|
||||||
|
state = I2C_STATE_CLEAR_ADDRESS;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case I2C_STATE_CLEAR_ADDRESS:
|
||||||
|
/* wait for address to be acknowledged.ADDSEND set means i2c slave sends ACK */
|
||||||
|
while ((!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND)) && (!i2c_flag_get(I2C0, I2C_FLAG_AERR)) && (timeout < I2C_TIME_OUT)) {
|
||||||
timeout++;
|
timeout++;
|
||||||
}
|
}
|
||||||
if (timeout < I2C_TIME_OUT) {
|
if (timeout >= I2C_TIME_OUT) {
|
||||||
|
state = I2C_STATE_ERROR;
|
||||||
|
break;
|
||||||
|
} else if (i2c_flag_get(I2C0, I2C_FLAG_ADDSEND))
|
||||||
|
{
|
||||||
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
||||||
timeout =0;
|
timeout =0;
|
||||||
state = I2C_TRANSMIT_DATA;
|
state = I2C_STATE_TRANSMIT_REG;
|
||||||
} else {
|
|
||||||
timeout = 0;
|
|
||||||
state = I2C_START;
|
|
||||||
#ifdef DEBUG_VERBOES
|
|
||||||
printf("i2c master clears address flag timeout in WRITE BYTE!\n");
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case I2C_TRANSMIT_DATA:
|
} else {
|
||||||
|
i2c_flag_clear(I2C0, I2C_FLAG_AERR);
|
||||||
|
timeout =0;
|
||||||
|
#ifdef DEBUG_VERBOES
|
||||||
|
printf("IIC write failed for Error Slave Address. \n");
|
||||||
|
#endif
|
||||||
|
return I2C_RESULT_NACK;
|
||||||
|
}
|
||||||
|
|
||||||
|
case I2C_STATE_TRANSMIT_REG:
|
||||||
/* wait until the transmit data buffer is empty */
|
/* wait until the transmit data buffer is 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) {
|
||||||
/* send IIC register address */
|
state = I2C_STATE_ERROR;
|
||||||
i2c_data_transmit(I2C0, reg_addr);
|
break;
|
||||||
timeout = 0;
|
|
||||||
} else {
|
|
||||||
timeout = 0;
|
|
||||||
state = I2C_START;
|
|
||||||
#ifdef DEBUG_VERBOES
|
|
||||||
printf("i2c master sends data timeout in WRITE BYTE!\n");
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* wait until BTC bit is set */
|
/* send register address */
|
||||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) {
|
i2c_data_transmit(I2C0, reg_addr);
|
||||||
|
timeout = 0;
|
||||||
|
state = I2C_STATE_TRANSMIT_DATA;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case I2C_STATE_TRANSMIT_DATA:
|
||||||
|
/* wait until the transmit data buffer is empty */
|
||||||
|
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;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
/* send register MSB value */
|
/* send register MSB value */
|
||||||
i2c_data_transmit(I2C0, data[0]);
|
i2c_data_transmit(I2C0, data[0]);
|
||||||
timeout = 0;
|
timeout = 0;
|
||||||
} else {
|
|
||||||
timeout = 0;
|
|
||||||
state = I2C_START;
|
|
||||||
#ifdef DEBUG_VERBOES
|
|
||||||
printf("i2c master sends MSB 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) {
|
|
||||||
/* send register LSB value */
|
|
||||||
i2c_data_transmit(I2C0, data[1]);
|
|
||||||
timeout = 0;
|
|
||||||
state = I2C_STOP;
|
|
||||||
} else {
|
|
||||||
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;
|
|
||||||
case I2C_STOP:
|
|
||||||
/* send a stop condition to I2C bus */
|
|
||||||
i2c_stop_on_bus(I2C0);
|
|
||||||
/* i2c master sends STOP signal successfully */
|
|
||||||
while ((I2C_CTL0(I2C0) & I2C_CTL0_STOP) && (timeout < I2C_TIME_OUT)) {
|
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if (timeout < I2C_TIME_OUT) {
|
|
||||||
timeout = 0;
|
|
||||||
state = I2C_END;
|
|
||||||
i2c_timeout_flag = I2C_OK;
|
|
||||||
} else {
|
|
||||||
timeout = 0;
|
|
||||||
state = I2C_START;
|
|
||||||
#ifdef DEBUG_VERBOES
|
|
||||||
printf("i2c master sends stop signal timeout in WRITE BYTE!\n");
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return I2C_END;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data) {
|
|
||||||
uint8_t state = I2C_START;
|
|
||||||
uint8_t read_cycle = 0;
|
|
||||||
uint16_t timeout = 0;
|
|
||||||
uint8_t i2c_timeout_flag = 0;
|
|
||||||
uint8_t number_of_byte = 2;
|
|
||||||
|
|
||||||
/* enable acknowledge */
|
|
||||||
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
|
||||||
|
|
||||||
while (!(i2c_timeout_flag)) {
|
|
||||||
switch (state) {
|
|
||||||
case I2C_START:
|
|
||||||
if (RESET == read_cycle) {
|
|
||||||
/* i2c master sends start signal only when the bus is idle */
|
|
||||||
while (i2c_flag_get(I2C0, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT)) {
|
|
||||||
timeout++;
|
|
||||||
}
|
|
||||||
if (timeout < I2C_TIME_OUT) {
|
|
||||||
/* whether to send ACK or not for the next byte */
|
|
||||||
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, I2C_TRANSMITTER);
|
|
||||||
state = I2C_CLEAR_ADDRESS_FLAG;
|
|
||||||
} else {
|
|
||||||
i2c_master_addressing(I2C0, slave_addr, I2C_RECEIVER);
|
|
||||||
i2c_ack_config(I2C0, I2C_ACK_DISABLE);
|
|
||||||
state = I2C_CLEAR_ADDRESS_FLAG;
|
|
||||||
}
|
|
||||||
timeout = 0;
|
|
||||||
} else {
|
|
||||||
timeout = 0;
|
|
||||||
state = I2C_START;
|
|
||||||
read_cycle = RESET;
|
|
||||||
#ifdef DEBUG_VERBOES
|
|
||||||
printf("i2c master sends start signal timeout in READ!\n");
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case I2C_CLEAR_ADDRESS_FLAG:
|
|
||||||
/* address flag set means i2c slave sends ACK */
|
|
||||||
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 */
|
/* wait until the transmit data buffer is 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) {
|
||||||
/* send the EEPROM's internal address to write to : only one byte address */
|
state = I2C_STATE_ERROR;
|
||||||
i2c_data_transmit(I2C0, reg_addr);
|
break;
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (i2c_flag_get(I2C0, I2C_FLAG_AERR)) {
|
||||||
|
i2c_stop_on_bus(I2C0);
|
||||||
|
return I2C_RESULT_NACK;
|
||||||
|
} else if (i2c_flag_get(I2C0, I2C_FLAG_BERR) || i2c_flag_get(I2C0, I2C_FLAG_LOSTARB)) {
|
||||||
|
// 可按需清标志
|
||||||
|
i2c_stop_on_bus(I2C0);
|
||||||
|
return I2C_RESULT_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* send register LSB value */
|
||||||
|
i2c_data_transmit(I2C0, data[1]);
|
||||||
|
timeout = 0;
|
||||||
|
|
||||||
/* wait until BTC bit is set */
|
/* wait until BTC bit is set */
|
||||||
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) {
|
||||||
timeout = 0;
|
state = I2C_STATE_ERROR;
|
||||||
state = I2C_START;
|
break;
|
||||||
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) {
|
state = I2C_STATE_STOP;
|
||||||
timeout++;
|
break;
|
||||||
if (2 == number_of_byte) {
|
|
||||||
/* wait until BTC bit is set */
|
case I2C_STATE_STOP:
|
||||||
while (!i2c_flag_get(I2C0, I2C_FLAG_BTC));
|
|
||||||
/* send a stop condition to I2C bus */
|
/* send a stop condition to I2C bus */
|
||||||
i2c_stop_on_bus(I2C0);
|
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;
|
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) {
|
||||||
timeout = 0;
|
state = I2C_STATE_ERROR;
|
||||||
state = I2C_END;
|
|
||||||
i2c_timeout_flag = I2C_OK;
|
|
||||||
} else {
|
|
||||||
timeout = 0;
|
|
||||||
state = I2C_START;
|
|
||||||
read_cycle = 0;
|
|
||||||
#ifdef DEBUG_VERBOES
|
|
||||||
printf("i2c master sends stop signal timeout in READ!\n");
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* i2c master sends STOP signal successfully */
|
||||||
|
/* success */
|
||||||
|
return I2C_RESULT_SUCCESS;
|
||||||
|
|
||||||
|
case I2C_STATE_ERROR:
|
||||||
|
/* send a stop condition to I2C bus */
|
||||||
|
i2c_stop_on_bus(I2C0);
|
||||||
|
|
||||||
|
timeout = 0;
|
||||||
|
while ((I2C_CTL0(I2C0) & I2C_CTL0_STOP) && (timeout < I2C_TIME_OUT)) {
|
||||||
|
timeout++;
|
||||||
|
}
|
||||||
|
if (timeout >= I2C_TIME_OUT) {
|
||||||
|
return I2C_RESULT_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
i2c_flag_clear(I2C0, I2C_FLAG_AERR);
|
||||||
|
i2c_flag_clear(I2C0, I2C_FLAG_BERR);
|
||||||
|
i2c_flag_clear(I2C0, I2C_FLAG_LOSTARB);
|
||||||
|
|
||||||
|
retry_count ++;
|
||||||
|
if (retry_count >= I2C_MAX_RETRY)
|
||||||
|
{
|
||||||
|
#ifdef DEBUG_VERBOES
|
||||||
|
printf("IIC write failed after %d retries\n", I2C_MAX_RETRY);
|
||||||
|
#endif
|
||||||
|
return I2C_RESULT_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* reset state machine for retry */
|
||||||
|
state = I2C_STATE_START;
|
||||||
|
timeout = 0;
|
||||||
|
|
||||||
|
/* small delay before retry */
|
||||||
|
delay_10us(10);
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
state = I2C_START;
|
state = I2C_STATE_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_RESULT_TIMEOUT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
i2c_result_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;
|
||||||
|
uint8_t retry_count = 0;
|
||||||
|
bool write_phase = true;
|
||||||
|
|
||||||
|
// 参数检查:防止空指针和非法地址
|
||||||
|
if (data == NULL || slave_addr > 0x7F) {
|
||||||
|
return I2C_RESULT_INVALID_PARAM;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* enable acknowledge */
|
||||||
|
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
||||||
|
|
||||||
|
while (retry_count < (uint8_t)I2C_MAX_RETRY) {
|
||||||
|
switch (state) {
|
||||||
|
case I2C_STATE_START:
|
||||||
|
timeout = 0;
|
||||||
|
|
||||||
|
// wait for bus to be idle
|
||||||
|
while (i2c_flag_get(I2C0, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT)) {
|
||||||
|
timeout++;
|
||||||
|
}
|
||||||
|
if (timeout >= I2C_TIME_OUT) {
|
||||||
|
state = I2C_STATE_ERROR;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (write_phase) {
|
||||||
|
/* clear address flag (write phase) */
|
||||||
|
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
||||||
|
state = I2C_STATE_TRANSMIT_DATA;
|
||||||
|
} else {
|
||||||
|
/* READ phase for 2 bytes: set POS=NEXT and disable ACK BEFORE clearing ADDR */
|
||||||
|
i2c_ackpos_config(I2C0, I2C_ACKPOS_NEXT);
|
||||||
|
i2c_ack_config(I2C0, I2C_ACK_DISABLE);
|
||||||
|
|
||||||
|
/* now clear address flag to release SCL and enter data phase */
|
||||||
|
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
||||||
|
|
||||||
|
state = I2C_STATE_RECEIVE_DATA;
|
||||||
|
}
|
||||||
|
|
||||||
|
timeout = 0;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case I2C_STATE_TRANSMIT_DATA:
|
||||||
|
/* 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 BTC: Bit 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* generate repeated start condition */
|
||||||
|
i2c_start_on_bus(I2C0);
|
||||||
|
|
||||||
|
/* wait for repeated start condition to be sent */
|
||||||
|
timeout = 0;
|
||||||
|
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 with read bit (R/W bit is set by library) */
|
||||||
|
i2c_master_addressing(I2C0, (slave_addr << 1), I2C_RECEIVER);
|
||||||
|
|
||||||
|
/* switch to read phase */
|
||||||
|
write_phase = false;
|
||||||
|
state = I2C_STATE_CLEAR_ADDRESS;
|
||||||
|
timeout = 0;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case I2C_STATE_RECEIVE_DATA:
|
||||||
|
/* Wait for BTC (both bytes received) */
|
||||||
|
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 before reading the last two bytes */
|
||||||
|
i2c_stop_on_bus(I2C0);
|
||||||
|
|
||||||
|
/* Read the two bytes back-to-back */
|
||||||
|
data[0] = i2c_data_receive(I2C0);
|
||||||
|
data[1] = i2c_data_receive(I2C0);
|
||||||
|
|
||||||
|
state = I2C_STATE_STOP;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case I2C_STATE_STOP:
|
||||||
|
/* wait for stop condition to complete */
|
||||||
|
while ((I2C_CTL0(I2C0) & I2C_CTL0_STOP) && (timeout < I2C_TIME_OUT)) {
|
||||||
|
timeout++;
|
||||||
|
}
|
||||||
|
if (timeout >= I2C_TIME_OUT) {
|
||||||
|
state = I2C_STATE_ERROR;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* i2c master sends STOP signal successfully */
|
||||||
|
/* success */
|
||||||
|
return I2C_RESULT_SUCCESS;
|
||||||
|
|
||||||
|
case I2C_STATE_ERROR:
|
||||||
|
/* send stop condition to release bus */
|
||||||
|
i2c_stop_on_bus(I2C0);
|
||||||
|
|
||||||
|
retry_count++;
|
||||||
|
if (retry_count >= I2C_MAX_RETRY) {
|
||||||
|
#ifdef DEBUG_VERBOES
|
||||||
|
printf("IIC read failed after %d retries\n", I2C_RETRY_MAX);
|
||||||
|
#endif
|
||||||
|
return I2C_RESULT_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* reset state machine for retry */
|
||||||
|
state = I2C_STATE_START;
|
||||||
|
write_phase = true;
|
||||||
|
timeout = 0;
|
||||||
|
|
||||||
|
/* small delay before retry */
|
||||||
|
delay_10us(10);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
state = I2C_STATE_START;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return I2C_RESULT_TIMEOUT;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef DEBUG_VERBOSE
|
||||||
|
/*!
|
||||||
|
\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_result_t status) {
|
||||||
|
switch (status) {
|
||||||
|
case I2C_RESULT_SUCCESS:
|
||||||
|
return "SUCCESS";
|
||||||
|
case I2C_RESULT_TIMEOUT:
|
||||||
|
return "TIMEOUT";
|
||||||
|
case I2C_RESULT_NACK:
|
||||||
|
return "NACK";
|
||||||
|
case I2C_RESULT_BUS_BUSY:
|
||||||
|
return "BUS_BUSY";
|
||||||
|
case I2C_RESULT_ERROR:
|
||||||
|
return "ERROR";
|
||||||
|
case I2C_RESULT_INVALID_PARAM:
|
||||||
|
return "INVALID_PARAM";
|
||||||
|
default:
|
||||||
|
return "UNKNOWN";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
21
Src/main.c
21
Src/main.c
@@ -38,8 +38,8 @@ OF SUCH DAMAGE.
|
|||||||
#include "led.h"
|
#include "led.h"
|
||||||
#include "command.h"
|
#include "command.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include "i2c.h"
|
||||||
bool g_status_switch = false;
|
#include "board_config.h"
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\brief main function
|
\brief main function
|
||||||
@@ -56,7 +56,8 @@ int main(void)
|
|||||||
|
|
||||||
led_init();
|
led_init();
|
||||||
|
|
||||||
char hello_world[] = {"Hello World!"};
|
#ifdef DEBUG_VERBOSE
|
||||||
|
char hello_world[] = {"Hello World!\r\n"};
|
||||||
|
|
||||||
for (uint8_t i = 0; i < sizeof(hello_world); i++)
|
for (uint8_t i = 0; i < sizeof(hello_world); i++)
|
||||||
{
|
{
|
||||||
@@ -66,8 +67,20 @@ int main(void)
|
|||||||
|
|
||||||
while (usart_flag_get(RS485_PHY, USART_FLAG_TC) == RESET) {}
|
while (usart_flag_get(RS485_PHY, USART_FLAG_TC) == RESET) {}
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
i2c_config();
|
||||||
|
|
||||||
|
#ifdef DEBUG_VERBOSE
|
||||||
|
i2c_scan();
|
||||||
|
|
||||||
|
i2c_bus_reset();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
while(1){
|
while(1){
|
||||||
command_process();
|
command_process();
|
||||||
delay_ms(100);
|
delay_ms(10);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -11,7 +11,7 @@
|
|||||||
\retval none
|
\retval none
|
||||||
*/
|
*/
|
||||||
void soft_i2c_delay(void) {
|
void soft_i2c_delay(void) {
|
||||||
delay_us(20); // Adjust delay as needed
|
delay_10us(2); // Adjust delay as needed
|
||||||
/* delay to freq
|
/* delay to freq
|
||||||
* 15KHz: delay_us(20);
|
* 15KHz: delay_us(20);
|
||||||
* 65KHz: delay_us(1);
|
* 65KHz: delay_us(1);
|
||||||
@@ -59,13 +59,13 @@ void soft_i2c_start(void) {
|
|||||||
\retval none
|
\retval none
|
||||||
*/
|
*/
|
||||||
void soft_i2c_stop(void) {
|
void soft_i2c_stop(void) {
|
||||||
// sda_out();
|
I2C_SCL_LOW(); // 确保时钟为低
|
||||||
I2C_SCL_LOW();
|
I2C_SDA_LOW(); // 拉低数据线
|
||||||
I2C_SDA_LOW();
|
|
||||||
soft_i2c_delay();
|
soft_i2c_delay();
|
||||||
I2C_SCL_HIGH();
|
I2C_SCL_HIGH(); // 拉高时钟
|
||||||
soft_i2c_delay();
|
soft_i2c_delay();
|
||||||
I2C_SDA_HIGH();
|
I2C_SDA_HIGH(); // 在时钟高电平时拉高数据线产生停止条件
|
||||||
|
soft_i2c_delay(); // 添加缺失的延时
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@@ -108,12 +108,13 @@ void soft_i2c_send_nack(void) {
|
|||||||
\retval 0: ACK received, 1: ACK not received
|
\retval 0: ACK received, 1: ACK not received
|
||||||
*/
|
*/
|
||||||
uint8_t soft_i2c_wait_ack(void) {
|
uint8_t soft_i2c_wait_ack(void) {
|
||||||
I2C_SDA_HIGH();
|
I2C_SDA_HIGH(); // 释放SDA线,让从设备控制
|
||||||
soft_i2c_delay();
|
soft_i2c_delay();
|
||||||
I2C_SCL_HIGH();
|
I2C_SCL_HIGH(); // 拉高时钟
|
||||||
soft_i2c_delay();
|
soft_i2c_delay();
|
||||||
uint8_t ack = !I2C_SDA_READ();
|
uint8_t ack = !I2C_SDA_READ(); // 读取ACK信号(低电平为ACK)
|
||||||
I2C_SCL_LOW();
|
I2C_SCL_LOW(); // 拉低时钟
|
||||||
|
soft_i2c_delay(); // 添加缺失的延时
|
||||||
return ack;
|
return ack;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -168,8 +169,13 @@ uint8_t soft_i2c_receive_byte(uint8_t ack) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
uint8_t soft_i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data[2]) {
|
uint8_t soft_i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data[2]) {
|
||||||
|
/* 参数验证 */
|
||||||
|
if (data == NULL || slave_addr > 0x7F) {
|
||||||
|
return SOFT_I2C_FAIL;
|
||||||
|
}
|
||||||
|
|
||||||
soft_i2c_start();
|
soft_i2c_start();
|
||||||
soft_i2c_send_byte(slave_addr);
|
soft_i2c_send_byte(slave_addr << 1); // 修复:左移1位,添加写位
|
||||||
if (!soft_i2c_wait_ack()) {
|
if (!soft_i2c_wait_ack()) {
|
||||||
soft_i2c_stop();
|
soft_i2c_stop();
|
||||||
return SOFT_I2C_FAIL;
|
return SOFT_I2C_FAIL;
|
||||||
@@ -185,15 +191,24 @@ uint8_t soft_i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data
|
|||||||
return SOFT_I2C_FAIL;
|
return SOFT_I2C_FAIL;
|
||||||
}
|
}
|
||||||
soft_i2c_send_byte(data[1]);
|
soft_i2c_send_byte(data[1]);
|
||||||
if (soft_i2c_wait_ack()){}
|
if (!soft_i2c_wait_ack()) { // 修复:添加错误处理
|
||||||
|
soft_i2c_stop();
|
||||||
|
return SOFT_I2C_FAIL;
|
||||||
|
}
|
||||||
soft_i2c_stop();
|
soft_i2c_stop();
|
||||||
return SOFT_I2C_OK;
|
return SOFT_I2C_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t soft_i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data)
|
uint8_t soft_i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data)
|
||||||
{
|
{
|
||||||
|
/* 参数验证 */
|
||||||
|
if (data == NULL || slave_addr > 0x7F) {
|
||||||
|
return SOFT_I2C_FAIL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 写阶段:发送寄存器地址 */
|
||||||
soft_i2c_start();
|
soft_i2c_start();
|
||||||
soft_i2c_send_byte(slave_addr);
|
soft_i2c_send_byte(slave_addr << 1); // 修复:左移1位,写操作
|
||||||
if (!soft_i2c_wait_ack()) {
|
if (!soft_i2c_wait_ack()) {
|
||||||
soft_i2c_stop();
|
soft_i2c_stop();
|
||||||
return SOFT_I2C_FAIL;
|
return SOFT_I2C_FAIL;
|
||||||
@@ -203,15 +218,17 @@ uint8_t soft_i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data
|
|||||||
soft_i2c_stop();
|
soft_i2c_stop();
|
||||||
return SOFT_I2C_FAIL;
|
return SOFT_I2C_FAIL;
|
||||||
}
|
}
|
||||||
soft_i2c_start();
|
|
||||||
soft_i2c_send_byte(slave_addr | 0x01);
|
/* 读阶段:重新开始并读取数据 */
|
||||||
|
soft_i2c_start(); // 重新开始
|
||||||
|
soft_i2c_send_byte((slave_addr << 1) | 0x01); // 修复:正确的读地址
|
||||||
if (!soft_i2c_wait_ack()) {
|
if (!soft_i2c_wait_ack()) {
|
||||||
soft_i2c_stop();
|
soft_i2c_stop();
|
||||||
return SOFT_I2C_FAIL;
|
return SOFT_I2C_FAIL;
|
||||||
}
|
}
|
||||||
soft_i2c_delay();
|
soft_i2c_delay();
|
||||||
data[0] = soft_i2c_receive_byte(1);
|
data[0] = soft_i2c_receive_byte(1); // 第一个字节发送ACK
|
||||||
data[1] = soft_i2c_receive_byte(0);
|
data[1] = soft_i2c_receive_byte(0); // 最后一个字节发送NACK
|
||||||
soft_i2c_stop();
|
soft_i2c_stop();
|
||||||
return SOFT_I2C_OK;
|
return SOFT_I2C_OK;
|
||||||
}
|
}
|
@@ -7,8 +7,8 @@ set(VERSION "V${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}")
|
|||||||
string(TIMESTAMP BUILD_DATE "%Y-%m-%d")
|
string(TIMESTAMP BUILD_DATE "%Y-%m-%d")
|
||||||
|
|
||||||
# 编译条件(如IIC类型等)
|
# 编译条件(如IIC类型等)
|
||||||
set(IIC_TYPE "AutoDetectDriveCurrent")
|
# set(IIC_TYPE "AutoDetectDriveCurrent")
|
||||||
# set(IIC_TYPE "HW-IIC")
|
set(IIC_TYPE "HW-IIC")
|
||||||
|
|
||||||
# 其它自定义宏
|
# 其它自定义宏
|
||||||
add_definitions(-DIIC_TYPE=${IIC_TYPE})
|
add_definitions(-DIIC_TYPE=${IIC_TYPE})
|
||||||
|
Reference in New Issue
Block a user