优化整定,DHCP有问题待优化
This commit is contained in:
@ -14,6 +14,10 @@
|
||||
#include "transaction.h"
|
||||
#include "state_manager.h"
|
||||
#include "error_handler.h"
|
||||
#include "sht40.h"
|
||||
#include "lwip/netif.h"
|
||||
#include "lwip/dhcp.h"
|
||||
#include "drv_eth.h"
|
||||
|
||||
/**
|
||||
* @brief 处理网络错误
|
||||
@ -31,7 +35,33 @@ static int handle_network_error(error_code_t error, void *user_data)
|
||||
/* 尝试恢复网络连接 */
|
||||
osal_log_i("Attempting to recover network connection...");
|
||||
|
||||
/* 这里可以添加具体的网络恢复逻辑,比如重新连接、重启网络接口等 */
|
||||
/* 具体的网络恢复逻辑 */
|
||||
if (user_data != NULL)
|
||||
{
|
||||
struct netif *netif = (struct netif *)user_data;
|
||||
|
||||
/* 检查网络连接状态 */
|
||||
ethernet_link_check_state(netif);
|
||||
|
||||
/* 如果网络连接已断开,尝试重新启动DHCP */
|
||||
if (!netif_is_link_up(netif))
|
||||
{
|
||||
osal_log_i("Network link down, waiting for link up...");
|
||||
int wait_count = 0;
|
||||
while (!netif_is_link_up(netif) && wait_count < 10)
|
||||
{
|
||||
osal_thread_mdelay(1000);
|
||||
ethernet_link_check_state(netif);
|
||||
wait_count++;
|
||||
}
|
||||
}
|
||||
|
||||
/* 重新启动DHCP */
|
||||
osal_log_i("Restarting DHCP...");
|
||||
dhcp_stop(netif);
|
||||
osal_thread_mdelay(100);
|
||||
dhcp_start(netif);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -52,9 +82,23 @@ static int handle_sensor_error(error_code_t error, void *user_data)
|
||||
/* 尝试恢复传感器 */
|
||||
osal_log_i("Attempting to recover sensor...");
|
||||
|
||||
/* 这里可以添加具体的传感器恢复逻辑,比如重新初始化传感器等 */
|
||||
/* 具体的传感器恢复逻辑 */
|
||||
int retry_count = 0;
|
||||
while (retry_count < 3)
|
||||
{
|
||||
osal_log_i("Attempt %d: Reinitializing SHT40 sensor...", retry_count + 1);
|
||||
if (sht40_init() == 0)
|
||||
{
|
||||
osal_log_i("SHT40 sensor recovered successfully");
|
||||
state_manager_set_sensor_state(SENSOR_STATE_READY);
|
||||
return 0;
|
||||
}
|
||||
osal_thread_mdelay(1000);
|
||||
retry_count++;
|
||||
}
|
||||
|
||||
return 0;
|
||||
osal_log_e("Failed to recover SHT40 sensor after %d attempts", retry_count);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -73,7 +117,8 @@ static int handle_tcp_error(error_code_t error, void *user_data)
|
||||
/* 尝试恢复TCP连接 */
|
||||
osal_log_i("Attempting to recover TCP connection...");
|
||||
|
||||
/* 这里可以添加具体的TCP恢复逻辑,比如重新建立连接等 */
|
||||
/* 具体的TCP恢复逻辑 */
|
||||
/* 这里可以添加重新建立TCP连接的代码 */
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
267
app/main.c
267
app/main.c
@ -1,5 +1,5 @@
|
||||
#include <rtthread.h>
|
||||
#include "osal.h"
|
||||
#include "hal.h"
|
||||
#include "lwip/init.h"
|
||||
#include "lwip/netif.h"
|
||||
#include "lwip/tcpip.h"
|
||||
@ -101,129 +101,47 @@ static void ethernet_input_entry(void *parameter)
|
||||
#define PING_DATA_SIZE 32
|
||||
#define PING_RCV_TIMEO 5000 // 5 seconds
|
||||
|
||||
/**
|
||||
* @brief 准备Ping回显包
|
||||
* @param iecho 指向icmp_echo_hdr结构体的指针,用于存储回显包
|
||||
* @param len 回显包的总长度(包括icmp_echo_hdr头和数据)
|
||||
* @param seq Ping包的序列号
|
||||
* @note 该函数填充icmp_echo_hdr结构体,设置类型为ICMP_ECHO,ID为PING_ID,序列号为seq,
|
||||
* 并填充数据部分为0到(data_len-1)的连续字节
|
||||
*/
|
||||
static void ping_prepare_echo(struct icmp_echo_hdr *iecho, u16_t len, u16_t seq)
|
||||
{
|
||||
size_t i;
|
||||
size_t data_len = len - sizeof(struct icmp_echo_hdr);
|
||||
|
||||
ICMPH_TYPE_SET(iecho, ICMP_ECHO);
|
||||
ICMPH_CODE_SET(iecho, 0);
|
||||
iecho->chksum = 0;
|
||||
iecho->id = PING_ID;
|
||||
iecho->seqno = lwip_htons(seq);
|
||||
|
||||
for(i = 0; i < data_len; i++) {
|
||||
((char*)iecho)[sizeof(struct icmp_echo_hdr) + i] = (char)i;
|
||||
}
|
||||
|
||||
iecho->chksum = inet_chksum(iecho, len);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 检查目标IP是否可达
|
||||
* @param target_ip 目标IP地址字符串
|
||||
* @return 如果目标IP可达,返回1;否则返回0
|
||||
* @note 该函数通过发送ICMP Echo请求包到目标IP,并等待响应来检查目标IP是否可达
|
||||
*/
|
||||
static int ping_check(const char *target_ip)
|
||||
{
|
||||
int s;
|
||||
struct timeval timeout;
|
||||
struct sockaddr_in to;
|
||||
struct icmp_echo_hdr *iecho;
|
||||
size_t ping_size = sizeof(struct icmp_echo_hdr) + PING_DATA_SIZE;
|
||||
int ret = 0;
|
||||
int seq_num = 0;
|
||||
|
||||
/* Create raw socket */
|
||||
s = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
|
||||
if (s < 0) {
|
||||
osal_log_e("Ping: Failed to create socket");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Set receive timeout */
|
||||
timeout.tv_sec = PING_RCV_TIMEO / 1000;
|
||||
timeout.tv_usec = (PING_RCV_TIMEO % 1000) * 1000;
|
||||
setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
|
||||
|
||||
/* Prepare destination address */
|
||||
memset(&to, 0, sizeof(to));
|
||||
to.sin_len = sizeof(to);
|
||||
to.sin_family = AF_INET;
|
||||
to.sin_addr.s_addr = inet_addr(target_ip);
|
||||
|
||||
/* Allocate memory for packet */
|
||||
iecho = (struct icmp_echo_hdr *)osal_malloc(ping_size);
|
||||
if (!iecho) {
|
||||
osal_log_e("Ping: Failed to allocate memory");
|
||||
closesocket(s);
|
||||
return 0;
|
||||
}
|
||||
|
||||
osal_log_i("Ping: Pinging %s...", target_ip);
|
||||
|
||||
/* Try to ping a few times */
|
||||
for (int i = 0; i < 3; i++) {
|
||||
ping_prepare_echo(iecho, (u16_t)ping_size, ++seq_num);
|
||||
|
||||
if (sendto(s, iecho, ping_size, 0, (struct sockaddr*)&to, sizeof(to)) <= 0) {
|
||||
osal_log_e("Ping: Send failed");
|
||||
continue;
|
||||
}
|
||||
|
||||
char buf[64];
|
||||
struct sockaddr_in from;
|
||||
socklen_t fromlen = sizeof(from);
|
||||
int len;
|
||||
|
||||
while ((len = recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr*)&from, &fromlen)) > 0) {
|
||||
if (len >= (int)(sizeof(struct ip_hdr) + sizeof(struct icmp_echo_hdr))) {
|
||||
struct ip_hdr *iphdr = (struct ip_hdr *)buf;
|
||||
struct icmp_echo_hdr *iecho_reply = (struct icmp_echo_hdr *)(buf + (IPH_HL(iphdr) * 4));
|
||||
|
||||
if (ICMPH_TYPE(iecho_reply) != ICMP_ER) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (iecho_reply->id == PING_ID && iecho_reply->seqno == lwip_htons(seq_num)) {
|
||||
osal_log_i("Ping: Reply from %s", inet_ntoa(from.sin_addr));
|
||||
ret = 1;
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
osal_log_w("Ping: Timeout or no reply");
|
||||
|
||||
exit:
|
||||
osal_free(iecho);
|
||||
closesocket(s);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* Network Monitor Thread (DHCP & Fallback) */
|
||||
static void network_monitor_entry(void *parameter)
|
||||
{
|
||||
ip4_addr_t ipaddr, netmask, gw;
|
||||
int dhcp_timeout = 100; /* 100 * 100ms = 10 seconds */
|
||||
int dhcp_timeout = 300; /* 300 * 100ms = 30 seconds */
|
||||
int link_wait_timeout = 100; /* 10 seconds to wait for link up */
|
||||
|
||||
osal_log_i("Starting DHCP...");
|
||||
|
||||
/* Wait for link up before starting DHCP */
|
||||
while (link_wait_timeout > 0)
|
||||
{
|
||||
ethernet_link_check_state(&gnetif);
|
||||
if (netif_is_link_up(&gnetif))
|
||||
{
|
||||
osal_log_i("Network link is up, starting DHCP...");
|
||||
break;
|
||||
}
|
||||
osal_thread_mdelay(100);
|
||||
link_wait_timeout--;
|
||||
|
||||
/* Print a dot every second */
|
||||
if (link_wait_timeout % 10 == 0) osal_kprintf(".");
|
||||
}
|
||||
osal_kprintf("\n");
|
||||
|
||||
if (!netif_is_link_up(&gnetif))
|
||||
{
|
||||
osal_log_w("Network link down, starting DHCP anyway...");
|
||||
}
|
||||
|
||||
dhcp_start(&gnetif);
|
||||
|
||||
while (dhcp_timeout > 0)
|
||||
{
|
||||
/* Check link status periodically */
|
||||
if (dhcp_timeout % 10 == 0) /* Every second */
|
||||
{
|
||||
ethernet_link_check_state(&gnetif);
|
||||
}
|
||||
|
||||
if (gnetif.ip_addr.addr != 0)
|
||||
{
|
||||
osal_log_i("DHCP Success!");
|
||||
@ -328,18 +246,33 @@ int main(void)
|
||||
{
|
||||
osal_thread_t tid;
|
||||
|
||||
/* 1. 系统初始化 */
|
||||
osal_log_d("System initialization started...\n");
|
||||
|
||||
/* OSAL Initialization */
|
||||
osal_init();
|
||||
osal_log_d("OSAL initialized\n");
|
||||
|
||||
rt_kprintf("Main: OSAL Log Level = %d\n", OSAL_LOG_LEVEL);
|
||||
/* Hardware Abstraction Layer Initialization */
|
||||
if (hal_init() != HAL_STATUS_OK) {
|
||||
osal_log_e("Failed to initialize hardware abstraction layer\n");
|
||||
return -1;
|
||||
}
|
||||
osal_log_d("Hardware abstraction layer initialized\n");
|
||||
|
||||
osal_log_i("Main: OSAL Log Level = %d\n", OSAL_LOG_LEVEL);
|
||||
osal_log_i("Test osal_log_i from main");
|
||||
|
||||
/* 2. 应用组件初始化 */
|
||||
osal_log_d("Initializing application components...\n");
|
||||
|
||||
/* Initialize event queue */
|
||||
if (event_queue_init() != 0)
|
||||
{
|
||||
osal_log_e("Failed to initialize event queue");
|
||||
return -1;
|
||||
}
|
||||
osal_log_d("Event queue initialized\n");
|
||||
|
||||
/* Initialize event handler */
|
||||
if (event_handler_init() != 0)
|
||||
@ -347,6 +280,7 @@ int main(void)
|
||||
osal_log_e("Failed to initialize event handler");
|
||||
return -1;
|
||||
}
|
||||
osal_log_d("Event handler initialized\n");
|
||||
|
||||
/* Initialize transaction management */
|
||||
if (transaction_init() != 0)
|
||||
@ -354,6 +288,7 @@ int main(void)
|
||||
osal_log_e("Failed to initialize transaction management");
|
||||
return -1;
|
||||
}
|
||||
osal_log_d("Transaction management initialized\n");
|
||||
|
||||
/* Initialize state manager */
|
||||
if (state_manager_init() != 0)
|
||||
@ -361,6 +296,7 @@ int main(void)
|
||||
osal_log_e("Failed to initialize state manager");
|
||||
return -1;
|
||||
}
|
||||
osal_log_d("State manager initialized\n");
|
||||
|
||||
/* Initialize error handler */
|
||||
if (error_handler_init() != 0)
|
||||
@ -368,6 +304,7 @@ int main(void)
|
||||
osal_log_e("Failed to initialize error handler");
|
||||
return -1;
|
||||
}
|
||||
osal_log_d("Error handler initialized\n");
|
||||
|
||||
/* Register event handlers */
|
||||
event_handler_register(EVENT_TYPE_SENSOR_DATA, sensor_data_handler, NULL);
|
||||
@ -377,23 +314,15 @@ int main(void)
|
||||
event_handler_register(EVENT_TYPE_TCP_CLIENT_DISCONNECTED, tcp_client_disconnected_handler, NULL);
|
||||
event_handler_register(EVENT_TYPE_TIMER, timer_handler, NULL);
|
||||
event_handler_register(EVENT_TYPE_ERROR, error_handler, NULL);
|
||||
osal_log_d("Event handlers registered\n");
|
||||
|
||||
/* Create event dispatch thread */
|
||||
tid = osal_thread_create("event_dispatch", event_dispatch_thread, NULL, 1024, 8);
|
||||
if (tid != NULL)
|
||||
{
|
||||
osal_thread_start(tid);
|
||||
rt_kprintf("Thread 'event_dispatch' created.\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("Failed to create thread 'event_dispatch'\n");
|
||||
}
|
||||
/* 3. 网络初始化 */
|
||||
osal_log_d("Initializing network components...\n");
|
||||
|
||||
/* Initialize TCP/IP stack */
|
||||
rt_kprintf("Initializing TCP/IP stack...\n");
|
||||
osal_log_d("Initializing TCP/IP stack...\n");
|
||||
tcpip_init(NULL, NULL);
|
||||
rt_kprintf("TCP/IP stack initialized.\n");
|
||||
osal_log_d("TCP/IP stack initialized.\n");
|
||||
|
||||
/* Initialize Network Interface */
|
||||
ip4_addr_t ipaddr, netmask, gw;
|
||||
@ -406,33 +335,12 @@ int main(void)
|
||||
netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, ðernetif_init, &tcpip_input);
|
||||
netif_set_default(&gnetif);
|
||||
netif_set_up(&gnetif);
|
||||
osal_log_d("Network interface added\n");
|
||||
|
||||
sem_ip_ready = osal_sem_create("sem_ip", 0);
|
||||
|
||||
/* Create Network Monitor Thread (DHCP) */
|
||||
tid = osal_thread_create("net_mon", network_monitor_entry, NULL, 1024, 12);
|
||||
if (tid != NULL)
|
||||
{
|
||||
osal_thread_start(tid);
|
||||
rt_kprintf("Thread 'net_mon' created.\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("Failed to create thread 'net_mon'\n");
|
||||
}
|
||||
|
||||
/* Create Ethernet Input Thread */
|
||||
/* Increased priority to 6 (Higher than TCPIP thread which is 10) for better responsiveness */
|
||||
tid = osal_thread_create("eth_input", ethernet_input_entry, NULL, 1536, 6);
|
||||
if (tid != NULL)
|
||||
{
|
||||
osal_thread_start(tid);
|
||||
rt_kprintf("Thread 'eth_input' created.\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("Failed to create thread 'eth_input'\n");
|
||||
}
|
||||
/* 4. 传感器初始化 */
|
||||
osal_log_d("Initializing sensors...\n");
|
||||
|
||||
/* Initialize SHT40 sensor */
|
||||
if (sht40_init() != 0)
|
||||
@ -460,16 +368,56 @@ int main(void)
|
||||
}
|
||||
}
|
||||
|
||||
/* 5. 线程创建 */
|
||||
osal_log_d("Creating threads...\n");
|
||||
|
||||
/* Create event dispatch thread */
|
||||
tid = osal_thread_create("event_dispatch", event_dispatch_thread, NULL, 1024, 8);
|
||||
if (tid != NULL)
|
||||
{
|
||||
osal_thread_start(tid);
|
||||
osal_log_d("Thread 'event_dispatch' created.\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
osal_log_e("Failed to create thread 'event_dispatch'\n");
|
||||
}
|
||||
|
||||
/* Create Network Monitor Thread (DHCP) */
|
||||
tid = osal_thread_create("net_mon", network_monitor_entry, NULL, 1024, 12);
|
||||
if (tid != NULL)
|
||||
{
|
||||
osal_thread_start(tid);
|
||||
osal_log_d("Thread 'net_mon' created.\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
osal_log_e("Failed to create thread 'net_mon'\n");
|
||||
}
|
||||
|
||||
/* Create Ethernet Input Thread */
|
||||
/* Increased priority to 6 (Higher than TCPIP thread which is 10) for better responsiveness */
|
||||
tid = osal_thread_create("eth_input", ethernet_input_entry, NULL, 1536, 6);
|
||||
if (tid != NULL)
|
||||
{
|
||||
osal_thread_start(tid);
|
||||
osal_log_d("Thread 'eth_input' created.\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
osal_log_e("Failed to create thread 'eth_input'\n");
|
||||
}
|
||||
|
||||
/* Create TCP Server Thread */
|
||||
tid = osal_thread_create("tcp_server", tcp_server_entry, NULL, 2048, 15);
|
||||
if (tid != NULL)
|
||||
{
|
||||
osal_thread_start(tid);
|
||||
rt_kprintf("Thread 'tcp_server' created.\n");
|
||||
osal_log_d("Thread 'tcp_server' created.\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("Failed to create thread 'tcp_server'\n");
|
||||
osal_log_e("Failed to create thread 'tcp_server'\n");
|
||||
}
|
||||
|
||||
/* Create Blink/Status Thread */
|
||||
@ -477,18 +425,21 @@ int main(void)
|
||||
if (tid != NULL)
|
||||
{
|
||||
osal_thread_start(tid);
|
||||
rt_kprintf("Thread 'blink' created.\n");
|
||||
osal_log_d("Thread 'blink' created.\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("Failed to create thread 'blink'\n");
|
||||
osal_log_e("Failed to create thread 'blink'\n");
|
||||
}
|
||||
|
||||
/* 6. 系统信息 */
|
||||
/* 内存信息暂时使用rt-thread API,osal库可能没有提供对应的函数 */
|
||||
rt_size_t total, used, max_used;
|
||||
rt_memory_info(&total, &used, &max_used);
|
||||
rt_kprintf("Memory Info: Total=%d, Used=%d, MaxUsed=%d\n", total, used, max_used);
|
||||
osal_log_d("Memory Info: Total=%d, Used=%d, MaxUsed=%d\n", total, used, max_used);
|
||||
|
||||
/* OSAL Start */
|
||||
/* 7. 启动系统 */
|
||||
osal_log_d("System initialization completed. Starting OSAL...\n");
|
||||
osal_start();
|
||||
|
||||
return 0;
|
||||
|
||||
31
app/sht40.c
31
app/sht40.c
@ -8,9 +8,10 @@
|
||||
#include "sht40.h"
|
||||
#include "board.h"
|
||||
#include "osal.h"
|
||||
#include "hal.h"
|
||||
|
||||
/* 外部I2C句柄 */
|
||||
extern I2C_HandleTypeDef hi2c1;
|
||||
/* 硬件抽象层操作结构体 */
|
||||
const hal_ops_t *hal_ops = NULL;
|
||||
|
||||
/* CRC校验函数 */
|
||||
static uint8_t sht40_crc8(const uint8_t *data, uint8_t len)
|
||||
@ -42,6 +43,14 @@ static uint8_t sht40_crc8(const uint8_t *data, uint8_t len)
|
||||
*/
|
||||
int sht40_init(void)
|
||||
{
|
||||
/* 初始化硬件抽象层操作结构体 */
|
||||
hal_ops = hal_get_ops();
|
||||
if (hal_ops == NULL)
|
||||
{
|
||||
osal_log_e("SHT40: Failed to get HAL ops");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return sht40_reset();
|
||||
}
|
||||
|
||||
@ -53,7 +62,7 @@ int sht40_reset(void)
|
||||
{
|
||||
uint8_t cmd = SHT40_CMD_RESET;
|
||||
osal_log_i("SHT40: Sending reset command 0x%02X", cmd);
|
||||
if (HAL_I2C_Master_Transmit(&hi2c1, SHT40_I2C_ADDR << 1, &cmd, 1, 1000) != HAL_OK)
|
||||
if (hal_ops->i2c->master_transmit(SHT40_I2C_ADDR << 1, &cmd, 1, 1000) != HAL_STATUS_OK)
|
||||
{
|
||||
osal_log_e("SHT40 reset failed");
|
||||
return -1;
|
||||
@ -74,7 +83,7 @@ int sht40_read_serial(uint32_t *serial)
|
||||
uint8_t data[6];
|
||||
|
||||
//osal_log_i("SHT40: Sending read serial command 0x%02X", cmd);
|
||||
if (HAL_I2C_Master_Transmit(&hi2c1, SHT40_I2C_ADDR << 1, &cmd, 1, 1000) != HAL_OK)
|
||||
if (hal_ops->i2c->master_transmit(SHT40_I2C_ADDR << 1, &cmd, 1, 1000) != HAL_STATUS_OK)
|
||||
{
|
||||
osal_log_e("SHT40 send read serial command failed");
|
||||
return -1;
|
||||
@ -82,7 +91,7 @@ int sht40_read_serial(uint32_t *serial)
|
||||
|
||||
osal_thread_mdelay(1);
|
||||
|
||||
if (HAL_I2C_Master_Receive(&hi2c1, SHT40_I2C_ADDR << 1, data, 6, 1000) != HAL_OK)
|
||||
if (hal_ops->i2c->master_receive(SHT40_I2C_ADDR << 1, data, 6, 1000) != HAL_STATUS_OK)
|
||||
{
|
||||
osal_log_e("SHT40 read serial data failed");
|
||||
return -1;
|
||||
@ -133,7 +142,7 @@ int sht40_heater_enable(uint8_t power_level)
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (HAL_I2C_Master_Transmit(&hi2c1, SHT40_I2C_ADDR << 1, &cmd, 1, 1000) != HAL_OK)
|
||||
if (hal_ops->i2c->master_transmit(SHT40_I2C_ADDR << 1, &cmd, 1, 1000) != HAL_STATUS_OK)
|
||||
{
|
||||
osal_log_e("SHT40 heater enable failed");
|
||||
return -1;
|
||||
@ -180,10 +189,9 @@ int sht40_read_temperature_humidity_with_precision(float *temperature, float *hu
|
||||
|
||||
//osal_log_i("SHT40: Sending measure command 0x%02X to address 0x%02X", cmd, SHT40_I2C_ADDR);
|
||||
/* 发送测量命令 */
|
||||
HAL_StatusTypeDef status = HAL_I2C_Master_Transmit(&hi2c1, SHT40_I2C_ADDR << 1, &cmd, 1, 1000);
|
||||
if (status != HAL_OK)
|
||||
if (hal_ops->i2c->master_transmit(SHT40_I2C_ADDR << 1, &cmd, 1, 1000) != HAL_STATUS_OK)
|
||||
{
|
||||
osal_log_e("SHT40 send measure command failed, status: %d", status);
|
||||
osal_log_e("SHT40 send measure command failed");
|
||||
return -1;
|
||||
}
|
||||
//osal_log_i("SHT40: Measure command sent successfully");
|
||||
@ -193,10 +201,9 @@ int sht40_read_temperature_humidity_with_precision(float *temperature, float *hu
|
||||
|
||||
/* 读取测量结果 */
|
||||
//osal_log_i("SHT40: Reading 6 bytes of data");
|
||||
status = HAL_I2C_Master_Receive(&hi2c1, SHT40_I2C_ADDR << 1, data, 6, 1000);
|
||||
if (status != HAL_OK)
|
||||
if (hal_ops->i2c->master_receive(SHT40_I2C_ADDR << 1, data, 6, 1000) != HAL_STATUS_OK)
|
||||
{
|
||||
osal_log_e("SHT40 read data failed, status: %d", status);
|
||||
osal_log_e("SHT40 read data failed");
|
||||
return -1;
|
||||
}
|
||||
//osal_log_i("SHT40: Data read successfully: 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X",
|
||||
|
||||
Reference in New Issue
Block a user