缩小变量作用域:消除18处variableScope,声明移入实际使用块
This commit is contained in:
+2
-2
@@ -81,9 +81,7 @@ static uint8_t rtc_calc_week(uint16_t y, uint8_t m, uint8_t d)
|
|||||||
*/
|
*/
|
||||||
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)
|
||||||
{
|
{
|
||||||
uint32_t y, m, d;
|
|
||||||
uint8_t h, mi, s;
|
uint8_t h, mi, s;
|
||||||
int is_leap;
|
|
||||||
|
|
||||||
/* 时分秒 */
|
/* 时分秒 */
|
||||||
{
|
{
|
||||||
@@ -97,6 +95,8 @@ static void unix_to_sd2506(uint32_t unix_secs, sd2506_time_t *p_sd_time)
|
|||||||
|
|
||||||
/* 年月日 */
|
/* 年月日 */
|
||||||
{
|
{
|
||||||
|
uint32_t y, m, d;
|
||||||
|
int is_leap;
|
||||||
uint64_t day_count = ((uint64_t)unix_secs / 86400uL);
|
uint64_t day_count = ((uint64_t)unix_secs / 86400uL);
|
||||||
|
|
||||||
y = 1970u;
|
y = 1970u;
|
||||||
|
|||||||
+1
-2
@@ -36,10 +36,9 @@
|
|||||||
static int time_sync_open_listener(void)
|
static int time_sync_open_listener(void)
|
||||||
{
|
{
|
||||||
int retry;
|
int retry;
|
||||||
int s;
|
|
||||||
|
|
||||||
for (retry = 0; retry < 5; retry++) {
|
for (retry = 0; retry < 5; retry++) {
|
||||||
s = net_socket(NET_AF_INET, NET_SOCK_STREAM);
|
int s = net_socket(NET_AF_INET, NET_SOCK_STREAM);
|
||||||
if (s < 0) {
|
if (s < 0) {
|
||||||
DBG_ERROR("time_sync: net_socket FAIL");
|
DBG_ERROR("time_sync: net_socket FAIL");
|
||||||
vTaskDelay(pdMS_TO_TICKS(500));
|
vTaskDelay(pdMS_TO_TICKS(500));
|
||||||
|
|||||||
+1
-2
@@ -91,13 +91,12 @@ size_t ringbuf_peek(const ringbuf_t *rb, uint8_t *data, size_t len)
|
|||||||
{
|
{
|
||||||
size_t n = 0;
|
size_t n = 0;
|
||||||
size_t i = 0;
|
size_t i = 0;
|
||||||
size_t idx;
|
|
||||||
|
|
||||||
n = ringbuf_avail(rb);
|
n = ringbuf_avail(rb);
|
||||||
n = (n < len) ? n : len;
|
n = (n < len) ? n : len;
|
||||||
|
|
||||||
for (i = 0; i < n; i++) {
|
for (i = 0; i < n; i++) {
|
||||||
idx = (rb->tail + i) % rb->size;
|
size_t idx = (rb->tail + i) % rb->size;
|
||||||
data[i] = rb->buf[idx];
|
data[i] = rb->buf[idx];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -604,13 +604,12 @@ uint8_t ch395f_get_cmd_status(void) {
|
|||||||
* 函数说明:每20ms查询一次,直到命令完成或超时
|
* 函数说明:每20ms查询一次,直到命令完成或超时
|
||||||
*/
|
*/
|
||||||
uint8_t ch395f_poll_cmd_status(uint32_t timeout_ms) {
|
uint8_t ch395f_poll_cmd_status(uint32_t timeout_ms) {
|
||||||
uint8_t status;
|
|
||||||
uint32_t retry_count = 0U;
|
uint32_t retry_count = 0U;
|
||||||
uint32_t max_retries = (timeout_ms + 20U - 1U) / 20U;
|
uint32_t max_retries = (timeout_ms + 20U - 1U) / 20U;
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
HAL_Delay(20U);
|
HAL_Delay(20U);
|
||||||
status = ch395f_get_cmd_status();
|
uint8_t status = ch395f_get_cmd_status();
|
||||||
|
|
||||||
if (status != CH395F_ERR_BUSY) {
|
if (status != CH395F_ERR_BUSY) {
|
||||||
return status;
|
return status;
|
||||||
|
|||||||
@@ -585,13 +585,13 @@ int gd5f2gq5ue_read_id(uint8_t *p_mid, uint8_t *p_did) {
|
|||||||
* 函数说明:自动处理跨页读取;offset+size 越界返回错误
|
* 函数说明:自动处理跨页读取;offset+size 越界返回错误
|
||||||
*/
|
*/
|
||||||
int gd5f2gq5ue_read(long offset, uint8_t *p_buf, size_t size) {
|
int gd5f2gq5ue_read(long offset, uint8_t *p_buf, size_t size) {
|
||||||
int ret;
|
|
||||||
|
|
||||||
if (offset < 0 || (uint32_t)offset + (uint32_t)size > GD5F_TOTAL_SIZE) {
|
if (offset < 0 || (uint32_t)offset + (uint32_t)size > GD5F_TOTAL_SIZE) {
|
||||||
return GD5F_ERROR;
|
return GD5F_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (size > 0) {
|
while (size > 0) {
|
||||||
|
int ret;
|
||||||
uint32_t page_addr = offset / GD5F_PAGE_SIZE;
|
uint32_t page_addr = offset / GD5F_PAGE_SIZE;
|
||||||
uint16_t column = offset % GD5F_PAGE_SIZE;
|
uint16_t column = offset % GD5F_PAGE_SIZE;
|
||||||
size_t bytes = GD5F_PAGE_SIZE - column;
|
size_t bytes = GD5F_PAGE_SIZE - column;
|
||||||
@@ -628,13 +628,13 @@ int gd5f2gq5ue_read(long offset, uint8_t *p_buf, size_t size) {
|
|||||||
* 函数说明:自动处理跨页写入;offset+size 越界返回错误
|
* 函数说明:自动处理跨页写入;offset+size 越界返回错误
|
||||||
*/
|
*/
|
||||||
int gd5f2gq5ue_write(long offset, const uint8_t *p_buf, size_t size) {
|
int gd5f2gq5ue_write(long offset, const uint8_t *p_buf, size_t size) {
|
||||||
int ret;
|
|
||||||
|
|
||||||
if (offset < 0 || (uint32_t)offset + (uint32_t)size > GD5F_TOTAL_SIZE) {
|
if (offset < 0 || (uint32_t)offset + (uint32_t)size > GD5F_TOTAL_SIZE) {
|
||||||
return GD5F_ERROR;
|
return GD5F_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (size > 0) {
|
while (size > 0) {
|
||||||
|
int ret;
|
||||||
uint32_t page_addr = offset / GD5F_PAGE_SIZE;
|
uint32_t page_addr = offset / GD5F_PAGE_SIZE;
|
||||||
uint16_t column = offset % GD5F_PAGE_SIZE;
|
uint16_t column = offset % GD5F_PAGE_SIZE;
|
||||||
size_t bytes = GD5F_PAGE_SIZE - column;
|
size_t bytes = GD5F_PAGE_SIZE - column;
|
||||||
@@ -665,7 +665,6 @@ int gd5f2gq5ue_write(long offset, const uint8_t *p_buf, size_t size) {
|
|||||||
* 函数说明:循环擦除 [offset, offset+size) 覆盖的每个块,偏移/长度不对齐返回错误
|
* 函数说明:循环擦除 [offset, offset+size) 覆盖的每个块,偏移/长度不对齐返回错误
|
||||||
*/
|
*/
|
||||||
int gd5f2gq5ue_erase(long offset, size_t size) {
|
int gd5f2gq5ue_erase(long offset, size_t size) {
|
||||||
int ret;
|
|
||||||
|
|
||||||
if (offset % GD5F_BLOCK_SIZE != 0) {
|
if (offset % GD5F_BLOCK_SIZE != 0) {
|
||||||
return GD5F_ERROR;
|
return GD5F_ERROR;
|
||||||
@@ -675,6 +674,7 @@ int gd5f2gq5ue_erase(long offset, size_t size) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
while (size > 0) {
|
while (size > 0) {
|
||||||
|
int ret;
|
||||||
uint32_t block_addr = offset / GD5F_BLOCK_SIZE;
|
uint32_t block_addr = offset / GD5F_BLOCK_SIZE;
|
||||||
|
|
||||||
ret = gd5f_block_erase(block_addr);
|
ret = gd5f_block_erase(block_addr);
|
||||||
|
|||||||
@@ -633,11 +633,11 @@ int lftpd_start(const char *directory, int port, lftpd_t *lftpd)
|
|||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
char ip_str[16];
|
|
||||||
struct net_sockaddr_in addr;
|
struct net_sockaddr_in addr;
|
||||||
int addrlen = sizeof(addr);
|
int addrlen = sizeof(addr);
|
||||||
if (net_getsockname(client_socket,
|
if (net_getsockname(client_socket,
|
||||||
(struct net_sockaddr *)&addr, &addrlen) == 0) {
|
(struct net_sockaddr *)&addr, &addrlen) == 0) {
|
||||||
|
char ip_str[16];
|
||||||
net_inet_ntoa(addr.sin_addr.s_addr, ip_str);
|
net_inet_ntoa(addr.sin_addr.s_addr, ip_str);
|
||||||
int remote_port = lftpd_inet_get_socket_port(client_socket);
|
int remote_port = lftpd_inet_get_socket_port(client_socket);
|
||||||
lftpd_log_info("connection received from %s:%d...",
|
lftpd_log_info("connection received from %s:%d...",
|
||||||
|
|||||||
@@ -8,10 +8,9 @@
|
|||||||
int lftpd_inet_listen(int port)
|
int lftpd_inet_listen(int port)
|
||||||
{
|
{
|
||||||
int retry;
|
int retry;
|
||||||
int s;
|
|
||||||
|
|
||||||
for (retry = 0; retry < 5; retry++) {
|
for (retry = 0; retry < 5; retry++) {
|
||||||
s = net_socket(NET_AF_INET, NET_SOCK_STREAM);
|
int s = net_socket(NET_AF_INET, NET_SOCK_STREAM);
|
||||||
if (s < 0) {
|
if (s < 0) {
|
||||||
if (retry == 0) DBG_ERROR("create socket");
|
if (retry == 0) DBG_ERROR("create socket");
|
||||||
vTaskDelay(pdMS_TO_TICKS(500));
|
vTaskDelay(pdMS_TO_TICKS(500));
|
||||||
|
|||||||
@@ -201,10 +201,9 @@ void *lftpd_io_opendir(const char *path)
|
|||||||
int lftpd_io_readdir(void *dp, char *name, int name_max, uint32_t *p_size, int *p_is_dir)
|
int lftpd_io_readdir(void *dp, char *name, int name_max, uint32_t *p_size, int *p_is_dir)
|
||||||
{
|
{
|
||||||
FILINFO fno;
|
FILINFO fno;
|
||||||
FRESULT fr;
|
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
fr = f_readdir((DIR *)dp, &fno);
|
FRESULT fr = f_readdir((DIR *)dp, &fno);
|
||||||
if (fr != FR_OK || fno.fname[0] == 0) {
|
if (fr != FR_OK || fno.fname[0] == 0) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -194,7 +194,6 @@ int net_poll(void) {
|
|||||||
/* 本次轮询累计发生的事件数(连接/断开/接收/发送/超时各计 1) */
|
/* 本次轮询累计发生的事件数(连接/断开/接收/发送/超时各计 1) */
|
||||||
int event_count = 0;
|
int event_count = 0;
|
||||||
/* 全局中断状态寄存器值(CH395F GET_GLOB_INT_STATUS_ALL 为 16 位) */
|
/* 全局中断状态寄存器值(CH395F GET_GLOB_INT_STATUS_ALL 为 16 位) */
|
||||||
uint16_t int_status;
|
|
||||||
|
|
||||||
/* 防御性检查:尚未初始化则不处理任何中断,直接返回 */
|
/* 防御性检查:尚未初始化则不处理任何中断,直接返回 */
|
||||||
if (!s_net_initialized) {
|
if (!s_net_initialized) {
|
||||||
@@ -209,6 +208,8 @@ int net_poll(void) {
|
|||||||
*/
|
*/
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
|
uint16_t int_status;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* GPIO 轮询 INT# 引脚:低电平有效(有中断待处理)。
|
* GPIO 轮询 INT# 引脚:低电平有效(有中断待处理)。
|
||||||
* 采用 GPIO 轮询而非 EXTI 边沿中断,仅当 INT# 为低电平时才读取状态,减少不必要 SPI 事务。
|
* 采用 GPIO 轮询而非 EXTI 边沿中断,仅当 INT# 为低电平时才读取状态,减少不必要 SPI 事务。
|
||||||
@@ -924,7 +925,6 @@ static int net_send_locked(net_sock_t *p_sock, const void *buf, int len) {
|
|||||||
* 3. CLOSED 状态尝试排空接收缓冲区残余数据(Trap 13:FIN 可能先于数据到达)
|
* 3. CLOSED 状态尝试排空接收缓冲区残余数据(Trap 13:FIN 可能先于数据到达)
|
||||||
*/
|
*/
|
||||||
static int net_recv_locked(net_sock_t *p_sock, void *buf, int len, int flags) {
|
static int net_recv_locked(net_sock_t *p_sock, void *buf, int len, int flags) {
|
||||||
int recv_len;
|
|
||||||
uint32_t tick_start;
|
uint32_t tick_start;
|
||||||
int sockfd = (int)(p_sock - s_net_socks);
|
int sockfd = (int)(p_sock - s_net_socks);
|
||||||
|
|
||||||
@@ -938,7 +938,7 @@ static int net_recv_locked(net_sock_t *p_sock, void *buf, int len, int flags) {
|
|||||||
while (1) {
|
while (1) {
|
||||||
net_poll();
|
net_poll();
|
||||||
|
|
||||||
recv_len = ch395f_get_recv_len(sockfd);
|
int recv_len = ch395f_get_recv_len(sockfd);
|
||||||
if (recv_len > 0) {
|
if (recv_len > 0) {
|
||||||
int read_len = (recv_len > len) ? len : recv_len;
|
int read_len = (recv_len > len) ? len : recv_len;
|
||||||
DBG_INFO("recv sock%d len=%d", sockfd, recv_len);
|
DBG_INFO("recv sock%d len=%d", sockfd, recv_len);
|
||||||
@@ -1342,7 +1342,6 @@ int net_sendto(int sockfd, const void *buf, int len, int flags,
|
|||||||
int net_recvfrom(int sockfd, void *buf, int len, int flags,
|
int net_recvfrom(int sockfd, void *buf, int len, int flags,
|
||||||
struct net_sockaddr *src_addr, int *addrlen) {
|
struct net_sockaddr *src_addr, int *addrlen) {
|
||||||
net_sock_t *p_sock;
|
net_sock_t *p_sock;
|
||||||
int recv_len;
|
|
||||||
uint32_t tick_start;
|
uint32_t tick_start;
|
||||||
|
|
||||||
/* 参数检查 */
|
/* 参数检查 */
|
||||||
@@ -1367,7 +1366,7 @@ int net_recvfrom(int sockfd, void *buf, int len, int flags,
|
|||||||
while (1) {
|
while (1) {
|
||||||
net_poll();
|
net_poll();
|
||||||
|
|
||||||
recv_len = ch395f_get_recv_len(sockfd);
|
int recv_len = ch395f_get_recv_len(sockfd);
|
||||||
if (recv_len > 0) {
|
if (recv_len > 0) {
|
||||||
/* UDP Server 模式:前 8 字节为固定格式(IP + 端口 + 长度)*/
|
/* UDP Server 模式:前 8 字节为固定格式(IP + 端口 + 长度)*/
|
||||||
if (p_sock->type == NET_SOCK_DGRAM && recv_len >= 8) {
|
if (p_sock->type == NET_SOCK_DGRAM && recv_len >= 8) {
|
||||||
@@ -1520,7 +1519,6 @@ int net_get_errno(void) {
|
|||||||
*/
|
*/
|
||||||
uint32_t net_inet_addr(const char *cp) {
|
uint32_t net_inet_addr(const char *cp) {
|
||||||
uint32_t addr = 0;
|
uint32_t addr = 0;
|
||||||
uint32_t val;
|
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
if (NULL == cp) {
|
if (NULL == cp) {
|
||||||
@@ -1528,7 +1526,7 @@ uint32_t net_inet_addr(const char *cp) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < 4; i++) {
|
for (i = 0; i < 4; i++) {
|
||||||
val = 0;
|
uint32_t val = 0;
|
||||||
while (*cp >= '0' && *cp <= '9') {
|
while (*cp >= '0' && *cp <= '9') {
|
||||||
val = val * 10 + (*cp - '0');
|
val = val * 10 + (*cp - '0');
|
||||||
cp++;
|
cp++;
|
||||||
@@ -1795,7 +1793,6 @@ static void fire_event(net_sock_t *p_sock, net_event_t event) {
|
|||||||
* 2. 参数非法(NULL)时直接返回,不做解析
|
* 2. 参数非法(NULL)时直接返回,不做解析
|
||||||
*/
|
*/
|
||||||
static void parse_ip_string(const char *str, uint8_t *ip_arr) {
|
static void parse_ip_string(const char *str, uint8_t *ip_arr) {
|
||||||
uint32_t val;
|
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
if (NULL == str ||NULL == ip_arr) {
|
if (NULL == str ||NULL == ip_arr) {
|
||||||
@@ -1803,7 +1800,7 @@ static void parse_ip_string(const char *str, uint8_t *ip_arr) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < 4; i++) {
|
for (i = 0; i < 4; i++) {
|
||||||
val = 0;
|
uint32_t val = 0;
|
||||||
while (*str >= '0' && *str <= '9') {
|
while (*str >= '0' && *str <= '9') {
|
||||||
val = val * 10 + (*str - '0');
|
val = val * 10 + (*str - '0');
|
||||||
str++;
|
str++;
|
||||||
|
|||||||
Reference in New Issue
Block a user