From 02c10fa5eff65cbec5644bb6cbf0723bf4425fc8 Mon Sep 17 00:00:00 2001 From: Wanderingss <1624155937@qq.com> Date: Tue, 1 Sep 2026 16:57:34 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DYoda=E6=9D=A1=E4=BB=B6?= =?UTF-8?q?=EF=BC=88=E5=B8=B8=E9=87=8F=E5=8F=B3=E7=BD=AE=EF=BC=89=E4=B8=8E?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E5=AE=9A=E4=B9=89=E6=8B=AC=E5=8F=B7=E5=90=8C?= =?UTF-8?q?=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - net_select.c/net_socket.c 共 62 处 Yoda 条件改为常量在右(规范 6.3.1) - 11 个文件 58 处函数定义左大括号由换行改为与右括号同行(规范 4.4.2) --- App/app_main.c | 6 +-- App/sys_clock.c | 21 ++++------ App/task/adc_task.c | 6 +-- App/task/net_task.c | 9 ++--- App/task/rs485_task.c | 6 +-- App/task/ui_task.c | 6 +-- App/time_sync.c | 9 ++--- App/util/crc.c | 6 +-- App/util/ringbuf.c | 27 +++++-------- Drivers/BSP/NET/net_select.c | 42 ++++++++++---------- Drivers/BSP/NET/net_socket.c | 64 +++++++++++++++---------------- Drivers/BSP/RS485/rs485.c | 24 ++++-------- Drivers/BSP/TPAFE5160/tpafe5160.c | 54 +++++++++----------------- 13 files changed, 111 insertions(+), 169 deletions(-) diff --git a/App/app_main.c b/App/app_main.c index d300dadf..a1dc763b 100644 --- a/App/app_main.c +++ b/App/app_main.c @@ -40,8 +40,7 @@ * 5) SD2506 诊断信息 * 6) 触发首次 ADC 转换 */ -int app_main_init(void) -{ +int app_main_init(void) { /* * 1) 时间子系统初始化(尽早完成,后续所有日志带时间戳) * sd2506_init() 从 RTC 读取初始时间 @@ -113,8 +112,7 @@ int app_main_init(void) * 限定条件:FreeRTOS 调度器启动后不再返回 * 函数说明:空闲任务可调用的应用层处理入口 */ -void app_main_run(void) -{ +void app_main_run(void) { for (;;) { /* 应用层空闲处理 */ } diff --git a/App/sys_clock.c b/App/sys_clock.c index e758b61c..9be10d49 100644 --- a/App/sys_clock.c +++ b/App/sys_clock.c @@ -54,8 +54,7 @@ static volatile uint32_t s_base_unix_secs = UINT32_MAX; * 函数说明:1. 采用 Zeller 公式(变种),结果 0 表示周日 * 2. 与 unix_to_sd2506() 内星期算法保持一致 */ -static uint8_t rtc_calc_week(uint16_t y, uint8_t m, uint8_t d) -{ +static uint8_t rtc_calc_week(uint16_t y, uint8_t m, uint8_t d) { uint32_t wy = y; uint32_t wm = m; @@ -79,8 +78,7 @@ static uint8_t rtc_calc_week(uint16_t y, uint8_t m, uint8_t d) * 2. 星期由 Zeller 公式(与 rtc_calc_week 同算法)得出 * 3. SD2506 年份字段为相对值(0~99,+2000),故回写时减 2000 */ -static void unix_to_sd2506(uint32_t unix_secs, sd2506_time_t *p_sd_time) -{ +static void unix_to_sd2506(uint32_t unix_secs, sd2506_time_t *p_sd_time) { uint8_t h, mi, s; /* 时分秒 */ @@ -163,8 +161,7 @@ static void unix_to_sd2506(uint32_t unix_secs, sd2506_time_t *p_sd_time) * 4. 注意:sd2506_get_time() 返回的 year 已是完整年份(驱动内 +2000), * 换算时不得再加 2000,否则年份虚增导致 Unix 秒数超 uint32 回绕 */ -int sys_clock_init(void) -{ +int sys_clock_init(void) { sd2506_time_t sd_time; uint32_t year, month, day, hour, minute, second; int64_t unix_secs = 0; @@ -288,8 +285,7 @@ int sys_clock_init(void) * 的整除差推算流逝秒数(避免每毫秒累加,减少漂移与溢出风险) * 2. 若推算结果为负(基准未建立),返回 UINT32_MAX 表示无效 */ -uint32_t sys_clock_get(void) -{ +uint32_t sys_clock_get(void) { uint32_t now_tick = HAL_GetTick(); int64_t unix_secs = (int64_t)s_base_unix_secs + (int64_t)((uint32_t)(now_tick - s_base_tick) / 1000uL); @@ -311,8 +307,7 @@ uint32_t sys_clock_get(void) * 2. 通过 unix_to_sd2506() 转换为 SD2506 结构后格式化 * 3. 使用 snprintf 防止缓冲区溢出 */ -char *sys_clock_get_str(char *buf, size_t buf_size) -{ +char *sys_clock_get_str(char *buf, size_t buf_size) { uint32_t unix_secs = sys_clock_get(); if (unix_secs == UINT32_MAX) { snprintf(buf, buf_size, "----/--/-- --:--:--"); @@ -338,8 +333,7 @@ char *sys_clock_get_str(char *buf, size_t buf_size) * 函数说明:1. 重置基准 Tick 与基准 Unix 秒数,使后续 sys_clock_get() 以此为准 * 2. 若 sd_time 非 NULL,将其写回 SD2506 RTC,实现断电保持 */ -int sys_clock_set(uint32_t unix_secs, const sd2506_time_t *sd_time) -{ +int sys_clock_set(uint32_t unix_secs, const sd2506_time_t *sd_time) { /* Step 1: 更新墙钟基准 */ s_base_tick = HAL_GetTick(); s_base_unix_secs = unix_secs; @@ -366,8 +360,7 @@ int sys_clock_set(uint32_t unix_secs, const sd2506_time_t *sd_time) * 函数说明:将 Unix 秒数转换为 SD2506 时间结构,再调用 sys_clock_set() * 更新运行期基准并写回 RTC,实现掉电保持 */ -int sys_clock_set_unix(uint32_t unix_secs) -{ +int sys_clock_set_unix(uint32_t unix_secs) { sd2506_time_t sd; /* 内部静态函数:将 Unix 秒数拆分为 SD2506 时间结构 */ diff --git a/App/task/adc_task.c b/App/task/adc_task.c index ae880dda..917f1ced 100644 --- a/App/task/adc_task.c +++ b/App/task/adc_task.c @@ -22,8 +22,7 @@ * 限定条件:TPAFE5160 硬件已上电 * 函数说明:配置采样参数,准备数据缓冲区 */ -int adc_task_init(void) -{ +int adc_task_init(void) { DBG_INFO("adc_task_init"); return 0; } @@ -35,8 +34,7 @@ int adc_task_init(void) * 限定条件:adc_task_init() 已成功调用 * 函数说明:定时触发 TPAFE5160 同步采样并处理数据 */ -void adc_task_func(void const *arg) -{ +void adc_task_func(void const *arg) { (void)arg; for (;;) { diff --git a/App/task/net_task.c b/App/task/net_task.c index aa39ba4f..81fdd53d 100644 --- a/App/task/net_task.c +++ b/App/task/net_task.c @@ -31,8 +31,7 @@ * NET 层模式由 netTask 调用;硬件层模式(阶段 1~5)netTask 不创建, * 由 testTask 调用后独占 CH395F。 */ -int net_stack_bring_up(void) -{ +int net_stack_bring_up(void) { DBG_INFO("=== Network Init ==="); if (net_init("192.168.1.100", "255.255.255.0", "192.168.1.1") != 0) { @@ -87,8 +86,7 @@ int net_stack_bring_up(void) * 限定条件:CH395F 协议栈已初始化 * 函数说明:创建 TCP Server 监听 Socket,注册回调等 */ -int net_task_init(void) -{ +int net_task_init(void) { DBG_INFO("net_task_init"); return 0; } @@ -102,8 +100,7 @@ int net_task_init(void) * 仅 NET 层测试模式(阶段 6~10)会创建本任务; * 硬件层模式下 netTask 不创建,CH395F 由 testTask 独占。 */ -void net_task_func(void const *arg) -{ +void net_task_func(void const *arg) { (void)arg; osDelay(2000); diff --git a/App/task/rs485_task.c b/App/task/rs485_task.c index f9ac4696..b2867b0c 100644 --- a/App/task/rs485_task.c +++ b/App/task/rs485_task.c @@ -22,8 +22,7 @@ * 限定条件:UART 和 GPIO 已由 CubeMX 初始化 * 函数说明:配置 RS-485 通信参数,注册接收回调 */ -int rs485_task_init(void) -{ +int rs485_task_init(void) { DBG_INFO("rs485_task_init"); return 0; } @@ -35,8 +34,7 @@ int rs485_task_init(void) * 限定条件:rs485_task_init() 已成功调用 * 函数说明:轮询接收缓冲区并解析协议帧 */ -void rs485_task_func(void const *arg) -{ +void rs485_task_func(void const *arg) { (void)arg; for (;;) { diff --git a/App/task/ui_task.c b/App/task/ui_task.c index 672e8665..c3606ae0 100644 --- a/App/task/ui_task.c +++ b/App/task/ui_task.c @@ -25,8 +25,7 @@ * 限定条件:sys_evtHandle 为 NULL 时直接返回 * 函数说明:无限期等待 SYS_EVT_NET_READY 标志 */ -static void ui_task_wait_net_ready(void) -{ +static void ui_task_wait_net_ready(void) { if (sys_evtHandle != NULL) { osEventFlagsWait(sys_evtHandle, SYS_EVT_NET_READY, osFlagsWaitAny, osWaitForever); @@ -41,8 +40,7 @@ static void ui_task_wait_net_ready(void) * 函数说明:1. 等待 SYS_EVT_NET_READY 后依次初始化 ui_port/ui_menu/ui_net_sink * 2. 主循环周期调用 ui_port_step 与 ui_net_sink_poll 刷新界面 */ -void ui_task_func(void *argument) -{ +void ui_task_func(void *argument) { (void)argument; DBG_INFO("UI task: started, wait net ready"); diff --git a/App/time_sync.c b/App/time_sync.c index 03bccd17..21ebe21d 100644 --- a/App/time_sync.c +++ b/App/time_sync.c @@ -33,8 +33,7 @@ * 单连接模式下监听 Socket 在收到连接后会转为数据通道,连接关闭后即被释放, * 因此每次调用都会新建监听 Socket(与 lftpd 同模式),失败自动重试。 */ -static int time_sync_open_listener(void) -{ +static int time_sync_open_listener(void) { int retry; for (retry = 0; retry < 5; retry++) { @@ -78,8 +77,7 @@ static int time_sync_open_listener(void) * 返回值:无 * 函数说明:阻塞读取 PC 发来的时间包("TIME"+uint32 大端 本地时间),解析后写回 RTC,然后关闭连接 */ -static void time_sync_handle_conn(int conn_sock) -{ +static void time_sync_handle_conn(int conn_sock) { uint8_t buf[16]; int len; @@ -106,8 +104,7 @@ static void time_sync_handle_conn(int conn_sock) * 函数说明:等待网络栈就绪后,循环:建监听 -> 等连接 -> 处理 -> 关闭 -> 重建监听。 * 全部 socket 操作经消息队列由 netTask 串行执行,与本任务/其它网络任务并发安全。 */ -void timeSyncTask(void *argument) -{ +void timeSyncTask(void *argument) { (void)argument; /* 等网络栈真正就绪(取代拍脑袋的 8s 魔法延时),确保 netTask 已开始处理消息 */ diff --git a/App/util/crc.c b/App/util/crc.c index 82412da0..0df237c6 100644 --- a/App/util/crc.c +++ b/App/util/crc.c @@ -89,8 +89,7 @@ static const uint8_t s_crc8_table[256] = { * 限定条件:data 为非空指针 * 函数说明:多项式 0x8005,初始值 0xFFFF,结果异或 0x0000 */ -uint16_t crc16_modbus(const uint8_t *data, size_t len) -{ +uint16_t crc16_modbus(const uint8_t *data, size_t len) { uint16_t crc = 0xFFFF; size_t i; @@ -109,8 +108,7 @@ uint16_t crc16_modbus(const uint8_t *data, size_t len) * 限定条件:data 为非空指针 * 函数说明:多项式 0x07,初始值 0x00 */ -uint8_t crc8(const uint8_t *data, size_t len) -{ +uint8_t crc8(const uint8_t *data, size_t len) { uint8_t crc = 0; size_t i; diff --git a/App/util/ringbuf.c b/App/util/ringbuf.c index 6edae494..02d65ec2 100644 --- a/App/util/ringbuf.c +++ b/App/util/ringbuf.c @@ -19,8 +19,7 @@ * 限定条件:rb 和 pool 均为非空指针 * 函数说明:size 保留一个空位用于区分空/满状态 */ -void ringbuf_init(ringbuf_t *rb, uint8_t *pool, size_t size) -{ +void ringbuf_init(ringbuf_t *rb, uint8_t *pool, size_t size) { rb->buf = pool; rb->size = size; rb->head = 0; @@ -36,8 +35,7 @@ void ringbuf_init(ringbuf_t *rb, uint8_t *pool, size_t size) * 限定条件:rb 和 data 均为非空指针 * 函数说明:空间不足时写入能容纳的最大字节数 */ -size_t ringbuf_put(ringbuf_t *rb, const uint8_t *data, size_t len) -{ +size_t ringbuf_put(ringbuf_t *rb, const uint8_t *data, size_t len) { size_t cap = 0; size_t n = 0; size_t i = 0; @@ -62,8 +60,7 @@ size_t ringbuf_put(ringbuf_t *rb, const uint8_t *data, size_t len) * 限定条件:rb 和 data 均为非空指针 * 函数说明:读取后数据从缓冲区移除 */ -size_t ringbuf_get(ringbuf_t *rb, uint8_t *data, size_t len) -{ +size_t ringbuf_get(ringbuf_t *rb, uint8_t *data, size_t len) { size_t n = 0; size_t i = 0; @@ -87,8 +84,7 @@ size_t ringbuf_get(ringbuf_t *rb, uint8_t *data, size_t len) * 限定条件:rb 和 data 均为非空指针 * 函数说明:与 ringbuf_get 的区别是不移动 tail 指针 */ -size_t ringbuf_peek(const ringbuf_t *rb, uint8_t *data, size_t len) -{ +size_t ringbuf_peek(const ringbuf_t *rb, uint8_t *data, size_t len) { size_t n = 0; size_t i = 0; @@ -109,8 +105,7 @@ size_t ringbuf_peek(const ringbuf_t *rb, uint8_t *data, size_t len) * 返 回 值:有效数据字节数 * 限定条件:rb 为非空指针 */ -size_t ringbuf_avail(const ringbuf_t *rb) -{ +size_t ringbuf_avail(const ringbuf_t *rb) { if (rb->head >= rb->tail) { return rb->head - rb->tail; } @@ -123,8 +118,7 @@ size_t ringbuf_avail(const ringbuf_t *rb) * 返 回 值:剩余空间字节数(保留一个空位) * 限定条件:rb 为非空指针 */ -size_t ringbuf_space(const ringbuf_t *rb) -{ +size_t ringbuf_space(const ringbuf_t *rb) { return rb->size - 1 - ringbuf_avail(rb); } @@ -134,8 +128,7 @@ size_t ringbuf_space(const ringbuf_t *rb) * 返 回 值:1 - 空,0 - 非空 * 限定条件:rb 为非空指针 */ -int ringbuf_is_empty(const ringbuf_t *rb) -{ +int ringbuf_is_empty(const ringbuf_t *rb) { return (rb->head == rb->tail) ? 1 : 0; } @@ -145,8 +138,7 @@ int ringbuf_is_empty(const ringbuf_t *rb) * 返 回 值:1 - 满,0 - 非满 * 限定条件:rb 为非空指针 */ -int ringbuf_is_full(const ringbuf_t *rb) -{ +int ringbuf_is_full(const ringbuf_t *rb) { return (ringbuf_avail(rb) == rb->size - 1) ? 1 : 0; } @@ -156,8 +148,7 @@ int ringbuf_is_full(const ringbuf_t *rb) * 返 回 值:无 * 限定条件:rb 为非空指针 */ -void ringbuf_reset(ringbuf_t *rb) -{ +void ringbuf_reset(ringbuf_t *rb) { rb->head = 0; rb->tail = 0; } diff --git a/Drivers/BSP/NET/net_select.c b/Drivers/BSP/NET/net_select.c index 666bad0d..b5605df2 100644 --- a/Drivers/BSP/NET/net_select.c +++ b/Drivers/BSP/NET/net_select.c @@ -60,12 +60,12 @@ int net_select(int nfds, net_fd_set_t *readfds, net_fd_set_t *writefds, int i; /* 检查输入参数合法性 */ - if ((0 >= nfds) || (NET_MAX_SOCKETS < nfds)) { + if ((nfds <= 0) || (nfds > NET_MAX_SOCKETS)) { return -1; } /* 计算超时时间(毫秒) */ - if (NULL != timeout) { + if (timeout != NULL) { timeout_ms = (uint32_t)(timeout->tv_sec * 1000) + (uint32_t)(timeout->tv_usec / 1000); } @@ -80,12 +80,12 @@ int net_select(int nfds, net_fd_set_t *readfds, net_fd_set_t *writefds, for (i = 0; i < nfds && i < NET_MAX_SOCKETS; i++) { const net_sock_t *p_sock = net_get_sock(i); - if (NULL == p_sock) { + if (p_sock == NULL) { continue; } /* 检查可读事件 */ - if ((NULL != readfds) && NET_FD_ISSET(i, readfds)) { + if ((readfds != NULL) && NET_FD_ISSET(i, readfds)) { if (check_socket_readable(i)) { NET_FD_SET(i, &result_read); ready_count++; @@ -93,7 +93,7 @@ int net_select(int nfds, net_fd_set_t *readfds, net_fd_set_t *writefds, } /* 检查可写事件 */ - if ((NULL != writefds) && NET_FD_ISSET(i, writefds)) { + if ((writefds != NULL) && NET_FD_ISSET(i, writefds)) { if (check_socket_writable(i)) { NET_FD_SET(i, &result_write); ready_count++; @@ -101,7 +101,7 @@ int net_select(int nfds, net_fd_set_t *readfds, net_fd_set_t *writefds, } /* 检查异常事件 */ - if ((NULL != exceptfds) && NET_FD_ISSET(i, exceptfds)) { + if ((exceptfds != NULL) && NET_FD_ISSET(i, exceptfds)) { if (check_socket_error(i)) { NET_FD_SET(i, &result_except); ready_count++; @@ -110,13 +110,13 @@ int net_select(int nfds, net_fd_set_t *readfds, net_fd_set_t *writefds, } /* 有事件就绪,返回 */ - if (0 < ready_count) { + if (ready_count > 0) { break; } /* 超时检测 */ - if (NULL != timeout) { - if ((0 < timeout_ms) && + if (timeout != NULL) { + if ((timeout_ms > 0) && ((HAL_GetTick() - start_tick) >= timeout_ms)) { break; /* 超时返回 0 */ } @@ -127,13 +127,13 @@ int net_select(int nfds, net_fd_set_t *readfds, net_fd_set_t *writefds, } /* 复制结果集合 */ - if (NULL != readfds) { + if (readfds != NULL) { readfds->fd_bits = result_read.fd_bits; } - if (NULL != writefds) { + if (writefds != NULL) { writefds->fd_bits = result_write.fd_bits; } - if (NULL != exceptfds) { + if (exceptfds != NULL) { exceptfds->fd_bits = result_except.fd_bits; } @@ -158,7 +158,7 @@ int net_poll_events(net_pollfd_t *fds, int nfds, int timeout) { int ready = 0; int i; - if ((NULL == fds) || (0 >= nfds)) { + if ((fds == NULL) || (nfds <= 0)) { return -1; } @@ -172,7 +172,7 @@ int net_poll_events(net_pollfd_t *fds, int nfds, int timeout) { fds[i].revents = 0; /* 检查 fd 有效性 */ - if ((0 > fds[i].fd) || (NET_MAX_SOCKETS <= fds[i].fd)) { + if ((fds[i].fd < 0) || (fds[i].fd >= NET_MAX_SOCKETS)) { fds[i].revents |= NET_POLLNVAL; ready++; continue; @@ -180,7 +180,7 @@ int net_poll_events(net_pollfd_t *fds, int nfds, int timeout) { const net_sock_t *p_sock = net_get_sock(fds[i].fd); - if (NULL == p_sock) { + if (p_sock == NULL) { fds[i].revents |= NET_POLLNVAL; ready++; continue; @@ -215,12 +215,12 @@ int net_poll_events(net_pollfd_t *fds, int nfds, int timeout) { } } - if (0 < ready) { + if (ready > 0) { break; } /* 超时检测 */ - if (0 <= timeout) { + if (timeout >= 0) { if ((HAL_GetTick() - start_tick) >= (uint32_t)timeout) { break; } @@ -250,7 +250,7 @@ static int check_socket_readable(int sockfd) { const net_sock_t *p_sock; p_sock = net_get_sock(sockfd); - if (NULL == p_sock) { + if (p_sock == NULL) { return 0; } @@ -280,7 +280,7 @@ static int check_socket_writable(int sockfd) { const net_sock_t *p_sock; p_sock = net_get_sock(sockfd); - if (NULL == p_sock) { + if (p_sock == NULL) { return 0; } @@ -307,12 +307,12 @@ static int check_socket_error(int sockfd) { const net_sock_t *p_sock; /* 无效 fd 视为错误 */ - if ((0 > sockfd) || (NET_MAX_SOCKETS <= sockfd)) { + if ((sockfd < 0) || (sockfd >= NET_MAX_SOCKETS)) { return 1; } p_sock = net_get_sock(sockfd); - if (NULL == p_sock) { + if (p_sock == NULL) { return 1; /* 未使用的 Socket 视为错误 */ } diff --git a/Drivers/BSP/NET/net_socket.c b/Drivers/BSP/NET/net_socket.c index 0c53077e..823cdc25 100644 --- a/Drivers/BSP/NET/net_socket.c +++ b/Drivers/BSP/NET/net_socket.c @@ -129,7 +129,7 @@ int net_init(const char *ip, const char *mask, const char *gateway) { ch395f_set_keepalive_cnt(3); /* 最多探 3 次*/ /* 设置网络参数(必须在 INIT_CH395 之前调用 */ - if (NULL != ip) { + if (ip != NULL) { uint8_t ip_arr[4]; uint8_t mask_arr[4]; uint8_t gw_arr[4]; @@ -300,7 +300,7 @@ int net_poll(void) { void net_process_messages(void) { net_msg_t msg; - if (NULL == netMsgQueueHandle) { + if (netMsgQueueHandle == NULL) { return; } @@ -308,7 +308,7 @@ void net_process_messages(void) { switch (msg.type) { case NET_MSG_SEND: { net_sock_t *p_sock = net_get_sock(msg.sockfd); - if (NULL == p_sock || p_sock->state != NET_SOCK_STATE_ESTABLISHED) { + if (p_sock == NULL || p_sock->state != NET_SOCK_STATE_ESTABLISHED) { msg.result = -1; } else { msg.result = net_send_locked(p_sock, msg.buf, msg.len); @@ -317,7 +317,7 @@ void net_process_messages(void) { } case NET_MSG_RECV: { net_sock_t *p_sock = net_get_sock(msg.sockfd); - if (NULL == p_sock) { + if (p_sock == NULL) { msg.result = -1; } else { msg.result = net_recv_locked(p_sock, msg.buf, msg.len, msg.flags); @@ -326,7 +326,7 @@ void net_process_messages(void) { } case NET_MSG_CLOSE: { net_sock_t *p_sock = net_get_sock(msg.sockfd); - if (NULL == p_sock) { + if (p_sock == NULL) { msg.result = -1; } else { msg.result = net_close_locked(p_sock); @@ -346,7 +346,7 @@ void net_process_messages(void) { case NET_MSG_ACCEPT: { struct net_sockaddr *p_addr = (struct net_sockaddr *)msg.buf; int addrlen = msg.len; - int *p_addrlen = (NULL != p_addr && addrlen > 0) ? &addrlen : NULL; + int *p_addrlen = (p_addr != NULL && addrlen > 0) ? &addrlen : NULL; msg.result = net_accept_locked(msg.sockfd, p_addr, p_addrlen); break; } @@ -423,7 +423,7 @@ int net_bind(int sockfd, const struct net_sockaddr *addr, int addrlen) { return -1; } - if (NULL == addr || addrlen < (int)sizeof(struct net_sockaddr_in)) { + if (addr == NULL || addrlen < (int)sizeof(struct net_sockaddr_in)) { s_net_errno = NET_ERR_INVAL; return -1; } @@ -567,7 +567,7 @@ int net_listen(int sockfd) { return -1; } - if (NULL == netMsgQueueHandle) { + if (netMsgQueueHandle == NULL) { s_net_errno = NET_ERR; return -1; } @@ -627,7 +627,7 @@ static int net_accept_locked(int sockfd, struct net_sockaddr *addr, int *addrlen } /* 填充对端地址(若调用方需要) */ - if (NULL != addr && NULL != addrlen) { + if (addr != NULL && addrlen != NULL) { struct net_sockaddr_in *p_addr_in = (struct net_sockaddr_in *)addr; p_addr_in->sin_family = NET_AF_INET; p_addr_in->sin_port = net_htons(p_listen_sock->remote_port); @@ -656,7 +656,7 @@ int net_accept(int sockfd, struct net_sockaddr *addr, int *addrlen) { return -1; } - if (NULL == netMsgQueueHandle) { + if (netMsgQueueHandle == NULL) { s_net_errno = NET_ERR; return -1; } @@ -664,7 +664,7 @@ int net_accept(int sockfd, struct net_sockaddr *addr, int *addrlen) { msg.type = NET_MSG_ACCEPT; msg.sockfd = sockfd; msg.buf = (void *)addr; - msg.len = (NULL != addrlen) ? *addrlen : 0; + msg.len = (addrlen != NULL) ? *addrlen : 0; msg.caller = xTaskGetCurrentTaskHandle(); msg.result = -1; @@ -679,7 +679,7 @@ int net_accept(int sockfd, struct net_sockaddr *addr, int *addrlen) { msg.result = (int)s_notified; } - if (NULL != addrlen &&NULL != addr) { + if (addrlen != NULL &&addr != NULL) { *addrlen = sizeof(struct net_sockaddr_in); } @@ -709,12 +709,12 @@ int net_connect(int sockfd, const struct net_sockaddr *addr, int addrlen) { return -1; } - if (NULL == netMsgQueueHandle) { + if (netMsgQueueHandle == NULL) { s_net_errno = NET_ERR; return -1; } - if (NULL == addr || addrlen < (int)sizeof(struct net_sockaddr_in)) { + if (addr == NULL || addrlen < (int)sizeof(struct net_sockaddr_in)) { s_net_errno = NET_ERR_INVAL; return -1; } @@ -767,7 +767,7 @@ static int net_connect_locked(int sockfd, const struct net_sockaddr *addr, int a return -1; } - if (NULL == addr || addrlen < (int)sizeof(struct net_sockaddr_in)) { + if (addr == NULL || addrlen < (int)sizeof(struct net_sockaddr_in)) { s_net_errno = NET_ERR_INVAL; return -1; } @@ -865,7 +865,7 @@ static int net_send_locked(net_sock_t *p_sock, const void *buf, int len) { uint32_t tick_start; int sockfd = (int)(p_sock - s_net_socks); - if (NULL == buf || len <= 0) { + if (buf == NULL || len <= 0) { s_net_errno = NET_ERR_INVAL; return -1; } @@ -928,7 +928,7 @@ static int net_recv_locked(net_sock_t *p_sock, void *buf, int len, int flags) { uint32_t tick_start; int sockfd = (int)(p_sock - s_net_socks); - if (NULL == buf || len <= 0) { + if (buf == NULL || len <= 0) { s_net_errno = NET_ERR_INVAL; return -1; } @@ -1117,7 +1117,7 @@ int net_send(int sockfd, const void *buf, int len, int flags) { } /* ---- 2. 运行环境检查:消息队列须已创建(网络层已初始化) ---- */ - if (NULL == netMsgQueueHandle) { + if (netMsgQueueHandle == NULL) { s_net_errno = NET_ERR; /* 网络层未初始化 */ return -1; } @@ -1172,7 +1172,7 @@ int net_recv(int sockfd, void *buf, int len, int flags) { return -1; } - if (NULL == netMsgQueueHandle) { + if (netMsgQueueHandle == NULL) { s_net_errno = NET_ERR; return -1; } @@ -1219,7 +1219,7 @@ int net_close(int sockfd) { return -1; } - if (NULL == netMsgQueueHandle) { + if (netMsgQueueHandle == NULL) { s_net_errno = NET_ERR; return -1; } @@ -1277,13 +1277,13 @@ int net_sendto(int sockfd, const void *buf, int len, int flags, return -1; } - if (NULL == buf || len <= 0) { + if (buf == NULL || len <= 0) { s_net_errno = NET_ERR_INVAL; return -1; } /* UDP 模式:设置目标地址 */ - if (NULL != dest_addr && addrlen >= (int)sizeof(struct net_sockaddr_in)) { + if (dest_addr != NULL && addrlen >= (int)sizeof(struct net_sockaddr_in)) { p_addr_in = (const struct net_sockaddr_in *)dest_addr; /* 转换 IP 为大端数组(同 TCP 客户端,set_des_ip 期望大端) */ @@ -1365,7 +1365,7 @@ int net_recvfrom(int sockfd, void *buf, int len, int flags, return -1; } - if (NULL == buf || len <= 0) { + if (buf == NULL || len <= 0) { s_net_errno = NET_ERR_INVAL; return -1; } @@ -1384,7 +1384,7 @@ int net_recvfrom(int sockfd, void *buf, int len, int flags, recv_len -= 8; /* 填充来源地址 */ - if (NULL != src_addr &&NULL != addrlen && + if (src_addr != NULL &&addrlen != NULL && *addrlen >= (int)sizeof(struct net_sockaddr_in)) { struct net_sockaddr_in *p_addr_in = (struct net_sockaddr_in *)src_addr; p_addr_in->sin_family = NET_AF_INET; @@ -1436,7 +1436,7 @@ int net_recvfrom(int sockfd, void *buf, int len, int flags, * 函数说明:直接调用 net_send_locked 完成发送 */ int net_send_sock(net_sock_t *p_sock, const void *buf, int len) { - if (NULL == p_sock) { + if (p_sock == NULL) { s_net_errno = NET_ERR_BADF; return -1; } @@ -1455,7 +1455,7 @@ int net_send_sock(net_sock_t *p_sock, const void *buf, int len) { * 函数说明:直接操作 Socket 控制块,不经过消息队列。避免 netTask 向自身发送消息造成死锁 */ int net_recv_sock(net_sock_t *p_sock, void *buf, int len, int flags) { - if (NULL == p_sock) { + if (p_sock == NULL) { s_net_errno = NET_ERR_BADF; return -1; } @@ -1470,7 +1470,7 @@ int net_recv_sock(net_sock_t *p_sock, void *buf, int len, int flags) { * 函数说明:直接操作 Socket 控制块,不经过消息队列。TCP Socket 先断开连接再释放控制块 */ int net_close_sock(net_sock_t *p_sock) { - if (NULL == p_sock) { + if (p_sock == NULL) { s_net_errno = NET_ERR_BADF; return -1; } @@ -1530,7 +1530,7 @@ uint32_t net_inet_addr(const char *cp) { uint32_t addr = 0; int i; - if (NULL == cp) { + if (cp == NULL) { return 0; } @@ -1561,7 +1561,7 @@ uint32_t net_inet_addr(const char *cp) { char *net_inet_ntoa(uint32_t addr, char *buf) { uint32_t naddr = NET_NTOHL(addr); - if (NULL == buf) { + if (buf == NULL) { return NULL; } @@ -1787,7 +1787,7 @@ static void handle_timeout_event(net_sock_t *p_sock) { * 2. 回调参数为 ch395_sock、event、event_arg */ static void fire_event(net_sock_t *p_sock, net_event_t event) { - if (NULL != p_sock->event_cb) { + if (p_sock->event_cb != NULL) { p_sock->event_cb(p_sock->ch395_sock, event, p_sock->event_arg); } } @@ -1805,7 +1805,7 @@ static void fire_event(net_sock_t *p_sock, net_event_t event) { static void parse_ip_string(const char *str, uint8_t *ip_arr) { int i; - if (NULL == str ||NULL == ip_arr) { + if (str == NULL ||ip_arr == NULL) { return; } @@ -1864,7 +1864,7 @@ int net_getsockname(int sockfd, struct net_sockaddr *addr, int *addrlen) { return -1; } - if (NULL == addr ||NULL == addrlen || *addrlen < (int)sizeof(struct net_sockaddr_in)) { + if (addr == NULL ||addrlen == NULL || *addrlen < (int)sizeof(struct net_sockaddr_in)) { s_net_errno = NET_ERR_INVAL; return -1; } diff --git a/Drivers/BSP/RS485/rs485.c b/Drivers/BSP/RS485/rs485.c index 317fdb58..304ea8f3 100644 --- a/Drivers/BSP/RS485/rs485.c +++ b/Drivers/BSP/RS485/rs485.c @@ -40,8 +40,7 @@ rs485_handle_t g_rs485; void rs485_init(rs485_handle_t *p_handle, UART_HandleTypeDef *p_huart, GPIO_TypeDef *p_dir_port, - uint16_t dir_pin) -{ + uint16_t dir_pin) { p_handle->huart = p_huart; p_handle->dir_port = p_dir_port; p_handle->dir_pin = dir_pin; @@ -71,8 +70,7 @@ void rs485_init(rs485_handle_t *p_handle, HAL_StatusTypeDef rs485_transmit(rs485_handle_t *p_handle, const uint8_t *p_data, uint16_t len, - uint32_t timeout) -{ + uint32_t timeout) { HAL_StatusTypeDef status = HAL_OK; if (timeout == 0U) { @@ -106,8 +104,7 @@ HAL_StatusTypeDef rs485_transmit(rs485_handle_t *p_handle, * HAL 自动调用 HAL_UARTEx_RxEventCallback 并返回已接收字节数。 * 用户须在回调中重新调用本函数重新开启接收。 */ -HAL_StatusTypeDef rs485_receive_start(rs485_handle_t *p_handle) -{ +HAL_StatusTypeDef rs485_receive_start(rs485_handle_t *p_handle) { p_handle->rx_size = 0; return HAL_UARTEx_ReceiveToIdle_IT(p_handle->huart, p_handle->rx_buf, RS485_RX_BUF_SIZE); } @@ -120,8 +117,7 @@ HAL_StatusTypeDef rs485_receive_start(rs485_handle_t *p_handle) * 限定条件:须在 HAL_UARTEx_RxEventCallback 中调用 * 函数说明:将 HAL 回调中接收到的数据大小回写到句柄的 rx_size 字段 */ -void rs485_rx_set_size(rs485_handle_t *p_handle, uint16_t size) -{ +void rs485_rx_set_size(rs485_handle_t *p_handle, uint16_t size) { p_handle->rx_size = size; } @@ -132,8 +128,7 @@ void rs485_rx_set_size(rs485_handle_t *p_handle, uint16_t size) * 限定条件:须在 HAL_UARTEx_RxEventCallback 触发后调用 * 函数说明:获取 rx_size 字段中缓存的接收字节数 */ -uint16_t rs485_rx_get_size(const rs485_handle_t *p_handle) -{ +uint16_t rs485_rx_get_size(const rs485_handle_t *p_handle) { return p_handle->rx_size; } @@ -143,8 +138,7 @@ uint16_t rs485_rx_get_size(const rs485_handle_t *p_handle) * 应用层接收钩子默认实现(弱符号,可被应用重定义)。 * 在中断上下文调用,仅做占位,业务解析由应用提供。 */ -__weak void rs485_on_rx(uint8_t *buf, uint16_t size) -{ +__weak void rs485_on_rx(uint8_t *buf, uint16_t size) { (void)buf; (void)size; } @@ -154,8 +148,7 @@ __weak void rs485_on_rx(uint8_t *buf, uint16_t size) * 当 RS-485 总线空闲超过 1 个字符时间后触发,由 HAL 自动调用。 * 注意:回调按实例分发,仅处理本驱动绑定的 UART,不写死 UART5。 */ -void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size) -{ +void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size) { if (huart == g_rs485.huart) { rs485_rx_set_size(&g_rs485, Size); @@ -171,8 +164,7 @@ void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size) * HAL UART 错误回调(ORE/NE/FE/PE 等)。 * 读 SR + DR 清除错误标志后重新开启接收,避免中断锁死。 */ -void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart) -{ +void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart) { if (huart == g_rs485.huart) { /* STM32F4 通过读 SR + DR 清除 ORE/NE/FE/PE 错误标志 */ (void)huart->Instance->SR; diff --git a/Drivers/BSP/TPAFE5160/tpafe5160.c b/Drivers/BSP/TPAFE5160/tpafe5160.c index 8564a691..f15422c9 100644 --- a/Drivers/BSP/TPAFE5160/tpafe5160.c +++ b/Drivers/BSP/TPAFE5160/tpafe5160.c @@ -65,8 +65,7 @@ static void tpafe5160_read_channels(uint8_t count, int16_t *buf); * 2. 确保 RD 为高、CONVST 为低 * 3. 等待 BUSY 释放(转换空闲) */ -int tpafe5160_init(void) -{ +int tpafe5160_init(void) { /* 设置默认过采样:无过采样 (OS[2:0] = 000) */ tpafe5160_set_os(TPAFE5160_OS_NONE); @@ -91,8 +90,7 @@ int tpafe5160_init(void) * 限定条件:GPIO 已初始化 * 函数说明:通过 OS[2:0] 引脚设置过采样率,在下一次 BUSY 下降沿锁存生效 */ -void tpafe5160_set_os(tpafe5160_os_t os) -{ +void tpafe5160_set_os(tpafe5160_os_t os) { TPAFE5160_OS0(os & 0x01); /* OS0 = bit0 */ TPAFE5160_OS1((os >> 1) & 0x01); /* OS1 = bit1 */ TPAFE5160_OS2((os >> 2) & 0x01); /* OS2 = bit2 */ @@ -105,8 +103,7 @@ void tpafe5160_set_os(tpafe5160_os_t os) * 限定条件:GPIO 已初始化 * 函数说明:产生 CONVST 脉冲上升沿,启动全部8通道同步采样与转换 */ -void tpafe5160_start_conv(void) -{ +void tpafe5160_start_conv(void) { TPAFE5160_CONVST_PULSE(); } @@ -117,8 +114,7 @@ void tpafe5160_start_conv(void) * 限定条件:已调用 tpafe5160_start_conv() * 函数说明:轮询 BUSY 引脚等待下降沿 */ -int tpafe5160_wait_busy(uint32_t timeout_ms) -{ +int tpafe5160_wait_busy(uint32_t timeout_ms) { uint32_t tick_start = HAL_GetTick(); /* 等待 BUSY 释放(低电平表示空闲) */ @@ -138,8 +134,7 @@ int tpafe5160_wait_busy(uint32_t timeout_ms) * 限定条件:GPIO 已初始化 * 函数说明:读取 BUSY 引脚电平 */ -uint8_t tpafe5160_is_busy(void) -{ +uint8_t tpafe5160_is_busy(void) { return (TPAFE5160_BUSY_READ() == GPIO_PIN_SET) ? 1 : 0; } @@ -152,8 +147,7 @@ uint8_t tpafe5160_is_busy(void) * 2. 连续8次拉低RD读取各通道数据 * 3. 通过 FRSTDATA 验证第一通道 */ -int tpafe5160_read_all(int16_t *buf) -{ +int tpafe5160_read_all(int16_t *buf) { int ret = 0; /* 启动转换 */ @@ -180,8 +174,7 @@ int tpafe5160_read_all(int16_t *buf) * 限定条件:GPIO 已初始化 * 函数说明:启动转换并等待完成后,连续读取至指定通道 */ -int tpafe5160_read_channel(uint8_t channel, int16_t *value) -{ +int tpafe5160_read_channel(uint8_t channel, int16_t *value) { int ret = 0; int16_t buf[TPAFE5160_CH_NUM] = {0}; @@ -207,8 +200,7 @@ int tpafe5160_read_channel(uint8_t channel, int16_t *value) * ±10V量程时 LSB = 20V / 65536 ≈ 305.18μV * 硬件 RANGE 引脚接 GND,默认 ±5V 量程 */ -float tpafe5160_to_voltage(int16_t raw) -{ +float tpafe5160_to_voltage(int16_t raw) { /* RANGE 接 GND → ±5V 量程,满量程 10V */ return (float)raw * (10.0f / 65536.0f); } @@ -220,8 +212,7 @@ float tpafe5160_to_voltage(int16_t raw) * 限定条件:RD 为低或 CS 与 RD 已拉低 * 函数说明:读取 GPIOG->IDR 低16位,对应 DB[15:0] */ -uint16_t tpafe5160_read_bus(void) -{ +uint16_t tpafe5160_read_bus(void) { return TPAFE5160_READ_BUS(); } @@ -241,8 +232,7 @@ uint16_t tpafe5160_read_bus(void) * t14 数据建立 ≤ 21ns → 数据在 RD 下降沿后 21ns 内有效 * 5 个 NOP (~30ns) 覆盖建立时间 */ -static void tpafe5160_read_channels(uint8_t count, int16_t *buf) -{ +static void tpafe5160_read_channels(uint8_t count, int16_t *buf) { uint8_t i; for (i = 0; i < count; i++) { @@ -272,8 +262,7 @@ static volatile uint8_t s_buf_sel = 0; /* 0 = 写 B / 读 A, 1 = 写 A / 读 B * 限定条件:CubeMX 已完成 GPIO 和 NVIC 配置 * 函数说明:正常启动流程无需调用,仅在 irq_disable() 后需要重新使能时使用 */ -void tpafe5160_irq_enable(void) -{ +void tpafe5160_irq_enable(void) { HAL_NVIC_EnableIRQ(EXTI9_5_IRQn); } @@ -284,8 +273,7 @@ void tpafe5160_irq_enable(void) * 限定条件:已调用 tpafe5160_irq_enable() * 函数说明:仅禁用 NVIC 中断,GPIO 配置保持 CubeMX 设定 */ -void tpafe5160_irq_disable(void) -{ +void tpafe5160_irq_disable(void) { HAL_NVIC_DisableIRQ(EXTI9_5_IRQn); } @@ -296,8 +284,7 @@ void tpafe5160_irq_disable(void) * 限定条件:已调用 tpafe5160_irq_enable() * 函数说明:产生 CONVST 脉冲,转换完成后由 EXTI 中断自动读取 8 通道数据 */ -void tpafe5160_start_conv_irq(void) -{ +void tpafe5160_start_conv_irq(void) { s_ready = 0; TPAFE5160_CONVST_PULSE(); } @@ -309,8 +296,7 @@ void tpafe5160_start_conv_irq(void) * 限定条件:中断模式已启用 * 函数说明:在 EXTI 回调中置位,主循环读取后需调用 tpafe5160_clear_ready() 清除 */ -uint8_t tpafe5160_data_ready(void) -{ +uint8_t tpafe5160_data_ready(void) { return s_ready; } @@ -321,8 +307,7 @@ uint8_t tpafe5160_data_ready(void) * 限定条件:中断模式已启用 * 函数说明:主循环处理完数据后调用 */ -void tpafe5160_clear_ready(void) -{ +void tpafe5160_clear_ready(void) { s_ready = 0; } @@ -333,8 +318,7 @@ void tpafe5160_clear_ready(void) * 限定条件:tpafe5160_data_ready() 返回 1 时调用 * 函数说明:双缓冲切换,ISR 写另一个缓冲区,主循环安全读取当前缓冲区 */ -const int16_t* tpafe5160_get_buf(void) -{ +const int16_t* tpafe5160_get_buf(void) { return (s_buf_sel == 0) ? s_buf_a : s_buf_b; } @@ -345,13 +329,11 @@ const int16_t* tpafe5160_get_buf(void) * 限定条件:BUSY EXTI 已使能 * 函数说明:转换完成后自动读取 8 通道数据到缓冲区,耗时约 0.5µs (168MHz) */ -void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) -{ +void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { tpafe5160_exti_handler(GPIO_Pin); } -void tpafe5160_exti_handler(uint16_t GPIO_Pin) -{ +void tpafe5160_exti_handler(uint16_t GPIO_Pin) { if (GPIO_Pin != TP_BUSY_Pin) { return; }