更新 CH395 端口与保持寄存器同步

This commit is contained in:
吴俊潮 2025-04-02 13:52:20 +08:00
parent ea0dac3a2e
commit 74d91adc76
2 changed files with 217 additions and 212 deletions

View File

@ -4,36 +4,36 @@
struct ch395q_t g_ch395q_sta;
/**
* @brief ch395_gpio初始化
* @param
* @retval
* @brief ch395_gpio初始化
* @param
* @retval
*/
void ch395_gpio_init( void )
{
GPIO_InitTypeDef gpio_init_struct;
CH395_SCS_GPIO_CLK_ENABLE(); /* 使能SCS时钟 */
CH395_INT_GPIO_CLK_ENABLE(); /* 使能INT时钟 */
CH395_RST_GPIO_CLK_ENABLE(); /* 使能RST时钟 */
CH395_SCS_GPIO_CLK_ENABLE(); /* 使能SCS时钟 */
CH395_INT_GPIO_CLK_ENABLE(); /* 使能INT时钟 */
CH395_RST_GPIO_CLK_ENABLE(); /* 使能RST时钟 */
/* SCS */
gpio_init_struct.Pin = CH395_SCS_GPIO_PIN;
gpio_init_struct.Speed = GPIO_SPEED_FREQ_MEDIUM;
gpio_init_struct.Mode = GPIO_MODE_OUTPUT_PP; /* 推拉输出 */
gpio_init_struct.Mode = GPIO_MODE_OUTPUT_PP; /* 推拉输出 */
HAL_GPIO_Init( CH395_SCS_GPIO_PORT, &gpio_init_struct );
ch395_scs_low;
/* 初始化中断引脚 */
/* 初始化中断引脚 */
gpio_init_struct.Pin = CH395_INT_GPIO_PIN;
gpio_init_struct.Mode = GPIO_MODE_INPUT; /* 输入 */
gpio_init_struct.Pull = GPIO_PULLUP; /* 上拉 */
gpio_init_struct.Speed = GPIO_SPEED_FREQ_HIGH; /* 高速 */
gpio_init_struct.Mode = GPIO_MODE_INPUT; /* 输入 */
gpio_init_struct.Pull = GPIO_PULLUP; /* 上拉 */
gpio_init_struct.Speed = GPIO_SPEED_FREQ_HIGH; /* 高速 */
HAL_GPIO_Init( CH395_INT_GPIO_PORT, &gpio_init_struct );
gpio_init_struct.Pin = CH395_RST_GPIO_PIN;
gpio_init_struct.Mode = GPIO_MODE_OUTPUT_PP; /* 输出 */
gpio_init_struct.Pull = GPIO_PULLUP; /* 上拉 */
gpio_init_struct.Speed = GPIO_SPEED_FREQ_HIGH; /* 高速 */
gpio_init_struct.Mode = GPIO_MODE_OUTPUT_PP; /* 输出 */
gpio_init_struct.Pull = GPIO_PULLUP; /* 上拉 */
gpio_init_struct.Speed = GPIO_SPEED_FREQ_HIGH; /* 高速 */
HAL_GPIO_Init( CH395_RST_GPIO_PORT, &gpio_init_struct );
HAL_GPIO_WritePin(CH395_RST_GPIO_PORT, CH395_RST_GPIO_PIN, GPIO_PIN_SET);
@ -42,9 +42,9 @@ void ch395_gpio_init( void )
}
/**
* @brief ch395关闭spi
* @param
* @retval
* @brief ch395关闭spi
* @param
* @retval
*/
void ch395_spi_off( void )
{
@ -55,76 +55,76 @@ void ch395_spi_off( void )
/* SCK\SDI\SDO */
gpio_init_struct.Pin = GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12;
gpio_init_struct.Speed = GPIO_SPEED_FREQ_MEDIUM;
gpio_init_struct.Mode = GPIO_MODE_OUTPUT_OD; /* 开漏输出 */
gpio_init_struct.Mode = GPIO_MODE_OUTPUT_OD; /* 开漏输出 */
HAL_GPIO_Init( GPIOC, &gpio_init_struct );
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12, GPIO_PIN_SET);
}
/**
* @brief SPI1读写一个字节数据
* @param txdata : (1)
* @retval (1)
* @brief SPI1读写一个字节数据
* @param txdata : (1)
* @retval (1)
*/
uint8_t spi2_read_write_byte(uint8_t txdata)
{
uint8_t rxdata;
HAL_SPI_TransmitReceive(&hspi2, &txdata, &rxdata, 1, 1000);
return rxdata; /* 返回收到的数据 */
return rxdata; /* 返回收到的数据 */
}
/**
* @brief SPI输出且输入8个位数据
* @param d:ch395的数据
* @retval
* @brief SPI输出且输入8个位数据
* @param d:ch395的数据
* @retval
*/
uint8_t ch395_read_write_byte( uint8_t data )
{
uint8_t rxdata;
rxdata = spi2_read_write_byte(data); /* SPI写入一个CH395Q数据并返回一个数据 */
return rxdata; /* 返回收到的数据 */
rxdata = spi2_read_write_byte(data); /* SPI写入一个CH395Q数据并返回一个数据 */
return rxdata; /* 返回收到的数据 */
}
/**
* @brief ch395写命令
* @param ch395的命令码
* @retval
* @brief ch395写命令
* @param ch395的命令码
* @retval
*/
void ch395_write_cmd( uint8_t mcmd )
{
ch395_scs_hign; /* 防止CS原来为低先将CS置高 */
ch395_scs_low; /* 命令开始CS拉低 */
ch395_scs_hign; /* 防止CS原来为低先将CS置高 */
ch395_scs_low; /* 命令开始CS拉低 */
delay_us(2);
ch395_read_write_byte(mcmd); /* SPI发送命令码 */
delay_us(2); /* 必要延时,延时1.5uS确保读写周期不小于1.5uS */
ch395_read_write_byte(mcmd); /* SPI发送命令码 */
delay_us(2); /* 必要延时,延时1.5uS确保读写周期不小于1.5uS */
}
/**
* @brief ch395写数据
* @param ch395的数据
* @retval
* @brief ch395写数据
* @param ch395的数据
* @retval
*/
void ch395_write_data( uint8_t mdata )
{
ch395_read_write_byte(mdata); /* SPI发送数据 */
ch395_read_write_byte(mdata); /* SPI发送数据 */
}
/**
* @brief ch395读数据
* @param
* @retval
* @brief ch395读数据
* @param
* @retval
*/
uint8_t ch395_read_data( void )
{
uint8_t i;
i = ch395_read_write_byte(0xff); /* SPI读数据 */
i = ch395_read_write_byte(0xff); /* SPI读数据 */
return i;
}
/**
* @brief ch395_keeplive_set
* @param
* @retval
* @brief ch395_keeplive_set
* @param
* @retval
*/
void ch395_keeplive_set(void)
{
@ -134,9 +134,9 @@ void ch395_keeplive_set(void)
}
/**
* @brief ch395 socket配置
* @param ch395_sokectSocket配置信息
* @retval
* @brief ch395 socket配置
* @param ch395_sokectSocket配置信息
* @retval
*/
uint8_t ch395q_socket_config(ch395_socket * ch395_sokect)
{
@ -145,7 +145,7 @@ uint8_t ch395q_socket_config(ch395_socket * ch395_sokect)
return 0;
}
// if (g_ch395q_sta.dhcp_status == DHCP_UP) /* DHCP获取成功状态 */
// if (g_ch395q_sta.dhcp_status == DHCP_UP) /* DHCP获取成功状态 */
// {
// ch395_sokect->net_info.ip[0] = g_ch395q_sta.ipinf_buf[0];
// ch395_sokect->net_info.ip[1] = g_ch395q_sta.ipinf_buf[1];
@ -172,12 +172,12 @@ uint8_t ch395q_socket_config(ch395_socket * ch395_sokect)
// ch395_sokect->net_info.dns2[2] = g_ch395q_sta.ipinf_buf[18];
// ch395_sokect->net_info.dns2[3] = g_ch395q_sta.ipinf_buf[19];
// }
// else /* DHCP获取失败状态设置静态IP地址信息 */
// else /* DHCP获取失败状态设置静态IP地址信息 */
// {
// ch395_cmd_set_ipaddr(ch395_sokect->net_config.ipaddr); /* 设置CH395的IP地址 */
// ch395_cmd_set_gw_ipaddr(ch395_sokect->net_config.gwipaddr); /* 设置网关地址 */
// ch395_cmd_set_maskaddr(ch395_sokect->net_config.maskaddr); /* 设置子网掩码默认为255.255.255.0*/
// printf("静态IP信息.....................................\r\n");
// ch395_cmd_set_ipaddr(ch395_sokect->net_config.ipaddr); /* 设置CH395的IP地址 */
// ch395_cmd_set_gw_ipaddr(ch395_sokect->net_config.gwipaddr); /* 设置网关地址 */
// ch395_cmd_set_maskaddr(ch395_sokect->net_config.maskaddr); /* 设置子网掩码默认为255.255.255.0*/
// printf("静态IP信息.....................................\r\n");
// printf("IP:%02d.%02d.%02d.%02d\r\n", (uint16_t)ch395_sokect->net_config.ipaddr[0], (uint16_t)ch395_sokect->net_config.ipaddr[1], (uint16_t)ch395_sokect->net_config.ipaddr[2], (uint16_t)ch395_sokect->net_config.ipaddr[3]);
// printf("GWIP:%02d.%02d.%02d.%02d\r\n", (uint16_t)ch395_sokect->net_config.gwipaddr[0], (uint16_t)ch395_sokect->net_config.gwipaddr[1], (uint16_t)ch395_sokect->net_config.gwipaddr[2], (uint16_t)ch395_sokect->net_config.gwipaddr[3]);
// printf("Mask:%02d.%02d.%02d.%02d\r\n", (uint16_t)ch395_sokect->net_config.maskaddr[0], (uint16_t)ch395_sokect->net_config.maskaddr[1], (uint16_t)ch395_sokect->net_config.maskaddr[2], (uint16_t)ch395_sokect->net_config.maskaddr[3]);
@ -185,52 +185,52 @@ uint8_t ch395q_socket_config(ch395_socket * ch395_sokect)
// HAL_Delay(10);
// }
ch395_cmd_set_ipaddr(ch395_sokect->net_config.ipaddr); /* 设置CH395的IP地址 */
ch395_cmd_set_gw_ipaddr(ch395_sokect->net_config.gwipaddr); /* 设置网关地址 */
ch395_cmd_set_maskaddr(ch395_sokect->net_config.maskaddr); /* 设置子网掩码默认为255.255.255.0*/
printf("静态IP信息.....................................\r\n");
ch395_cmd_set_ipaddr(ch395_sokect->net_config.ipaddr); /* 设置CH395的IP地址 */
ch395_cmd_set_gw_ipaddr(ch395_sokect->net_config.gwipaddr); /* 设置网关地址 */
ch395_cmd_set_maskaddr(ch395_sokect->net_config.maskaddr); /* 设置子网掩码默认为255.255.255.0*/
printf("静态IP信息.....................................\r\n");
printf("IP:%02d.%02d.%02d.%02d\r\n", (uint16_t)ch395_sokect->net_config.ipaddr[0], (uint16_t)ch395_sokect->net_config.ipaddr[1], (uint16_t)ch395_sokect->net_config.ipaddr[2], (uint16_t)ch395_sokect->net_config.ipaddr[3]);
printf("GWIP:%02d.%02d.%02d.%02d\r\n", (uint16_t)ch395_sokect->net_config.gwipaddr[0], (uint16_t)ch395_sokect->net_config.gwipaddr[1], (uint16_t)ch395_sokect->net_config.gwipaddr[2], (uint16_t)ch395_sokect->net_config.gwipaddr[3]);
printf("Mask:%02d.%02d.%02d.%02d\r\n", (uint16_t)ch395_sokect->net_config.maskaddr[0], (uint16_t)ch395_sokect->net_config.maskaddr[1], (uint16_t)ch395_sokect->net_config.maskaddr[2], (uint16_t)ch395_sokect->net_config.maskaddr[3]);
ch395_cmd_init();
//ch395_cmd_set_macaddr(ch395_sokect->net_config.macaddr); /* 设置MAC地址 */
//ch395_cmd_set_macaddr(ch395_sokect->net_config.macaddr); /* 设置MAC地址 */
memcpy(&g_ch395q_sta.socket[ch395_sokect->socket_index].config, ch395_sokect, sizeof(ch395_socket));
switch(ch395_sokect->proto)
{
case CH395Q_SOCKET_UDP:
/* socket 为UDP模式 */
ch395_set_socket_desip(ch395_sokect->socket_index, ch395_sokect->des_ip); /* 设置socket 0目标IP地址 */
ch395_set_socket_prot_type(ch395_sokect->socket_index, PROTO_TYPE_UDP); /* 设置socket 0协议类型 */
ch395_set_socket_desport(ch395_sokect->socket_index, ch395_sokect->des_port); /* 设置socket 0目的端口 */
ch395_set_socket_sourport(ch395_sokect->socket_index, ch395_sokect->sour_port); /* 设置socket 0源端口 */
g_ch395q_sta.ch395_error(ch395_open_socket(ch395_sokect->socket_index)); /* 检查是否成功 */
/* socket 为UDP模式 */
ch395_set_socket_desip(ch395_sokect->socket_index, ch395_sokect->des_ip); /* 设置socket 0目标IP地址 */
ch395_set_socket_prot_type(ch395_sokect->socket_index, PROTO_TYPE_UDP); /* 设置socket 0协议类型 */
ch395_set_socket_desport(ch395_sokect->socket_index, ch395_sokect->des_port); /* 设置socket 0目的端口 */
ch395_set_socket_sourport(ch395_sokect->socket_index, ch395_sokect->sour_port); /* 设置socket 0源端口 */
g_ch395q_sta.ch395_error(ch395_open_socket(ch395_sokect->socket_index)); /* 检查是否成功 */
break;
case CH395Q_SOCKET_TCP_CLIENT:
/* socket 为TCPClient模式 */
ch395_keeplive_set(); /* 保活设置 */
ch395_set_socket_desip(ch395_sokect->socket_index, ch395_sokect->des_ip); /* 设置socket 0目标IP地址 */
ch395_set_socket_prot_type(ch395_sokect->socket_index, PROTO_TYPE_TCP); /* 设置socket 0协议类型 */
ch395_set_socket_desport(ch395_sokect->socket_index, ch395_sokect->des_port); /* 设置socket 0目的端口 */
ch395_set_socket_sourport(ch395_sokect->socket_index, ch395_sokect->sour_port); /* 设置socket 0源端口 */
g_ch395q_sta.ch395_error(ch395_open_socket(ch395_sokect->socket_index)); /* 检查sokect是否打开成功 */
g_ch395q_sta.ch395_error(ch395_tcp_connect(ch395_sokect->socket_index)); /* 检查tcp连接是否成功 */
/* socket 为TCPClient模式 */
ch395_keeplive_set(); /* 保活设置 */
ch395_set_socket_desip(ch395_sokect->socket_index, ch395_sokect->des_ip); /* 设置socket 0目标IP地址 */
ch395_set_socket_prot_type(ch395_sokect->socket_index, PROTO_TYPE_TCP); /* 设置socket 0协议类型 */
ch395_set_socket_desport(ch395_sokect->socket_index, ch395_sokect->des_port); /* 设置socket 0目的端口 */
ch395_set_socket_sourport(ch395_sokect->socket_index, ch395_sokect->sour_port); /* 设置socket 0源端口 */
g_ch395q_sta.ch395_error(ch395_open_socket(ch395_sokect->socket_index)); /* 检查sokect是否打开成功 */
g_ch395q_sta.ch395_error(ch395_tcp_connect(ch395_sokect->socket_index)); /* 检查tcp连接是否成功 */
break;
case CH395Q_SOCKET_TCP_SERVER:
/* socket 为TCPServer模式 */
//ch395_set_socket_desip(ch395_sokect->socket_index, ch395_sokect->des_ip); /* 设置socket 0目标IP地址 */
ch395_set_socket_prot_type(ch395_sokect->socket_index, PROTO_TYPE_TCP); /* 设置socket 0协议类型 */
ch395_set_socket_sourport(ch395_sokect->socket_index, ch395_sokect->sour_port); /* 设置socket 0源端口 */
/* socket 为TCPServer模式 */
//ch395_set_socket_desip(ch395_sokect->socket_index, ch395_sokect->des_ip); /* 设置socket 0目标IP地址 */
ch395_set_socket_prot_type(ch395_sokect->socket_index, PROTO_TYPE_TCP); /* 设置socket 0协议类型 */
ch395_set_socket_sourport(ch395_sokect->socket_index, ch395_sokect->sour_port); /* 设置socket 0源端口 */
HAL_Delay(3000);
g_ch395q_sta.ch395_error(ch395_open_socket(ch395_sokect->socket_index)); /* 检查sokect是否打开成功 */
g_ch395q_sta.ch395_error(ch395_tcp_listen(ch395_sokect->socket_index)); /* 监听tcp连接 */
g_ch395q_sta.ch395_error(ch395_open_socket(ch395_sokect->socket_index)); /* 检查sokect是否打开成功 */
g_ch395q_sta.ch395_error(ch395_tcp_listen(ch395_sokect->socket_index)); /* 监听tcp连接 */
break;
case CH395Q_SOCKET_MAC_RAW:
ch395_set_socket_prot_type(ch395_sokect->socket_index, PROTO_TYPE_MAC_RAW); /* 设置socket 0协议类型 */
g_ch395q_sta.ch395_error(ch395_open_socket(ch395_sokect->socket_index)); /* 检查sokect是否打开成功 */
ch395_set_socket_prot_type(ch395_sokect->socket_index, PROTO_TYPE_MAC_RAW); /* 设置socket 0协议类型 */
g_ch395q_sta.ch395_error(ch395_open_socket(ch395_sokect->socket_index)); /* 检查sokect是否打开成功 */
break;
}
@ -238,18 +238,18 @@ uint8_t ch395q_socket_config(ch395_socket * ch395_sokect)
}
/**
* @brief 使
* @param ierror
* @retval
* @brief 使
* @param ierror
* @retval
*/
void ch395_error(uint8_t ierror)
{
if (ierror == CMD_ERR_SUCCESS)
{
return; /* 操作成功 */
return; /* 操作成功 */
}
printf("Error: %02X\r\n", (uint16_t)ierror); /* 显示错误 */
printf("Error: %02X\r\n", (uint16_t)ierror); /* 显示错误 */
// while ( 1 )
// {
@ -259,9 +259,9 @@ void ch395_error(uint8_t ierror)
}
/**
* @brief CH395 PHY状态
* @param phy_statusPHY状态值
* @retval
* @brief CH395 PHY状态
* @param phy_statusPHY状态值
* @retval
*/
void ch395_phy_status(uint8_t phy_status)
{
@ -290,13 +290,13 @@ void ch395_phy_status(uint8_t phy_status)
}
/**
* @brief socket接口的接收与发送缓冲区
* @param
* @retval
* @brief socket接口的接收与发送缓冲区
* @param
* @retval
*/
void ch395_socket_r_s_buf_modify(void)
{
ch395_set_socket_recv_buf(0,0,4); /* Socket 0 接收缓冲区4*512 = 2K发送缓冲区2*512 = 1K*/
ch395_set_socket_recv_buf(0,0,4); /* Socket 0 接收缓冲区4*512 = 2K发送缓冲区2*512 = 1K*/
ch395_set_socket_send_buf(0,4,2);
ch395_set_socket_recv_buf(1,6,4); /* Socket 1 */
@ -323,9 +323,9 @@ void ch395_socket_r_s_buf_modify(void)
int re_cnt = 0;
/**
* @brief ch395_tcp初始化
* @param
* @retval
* @brief ch395_tcp初始化
* @param
* @retval
*/
void ch395_hardware_init(void)
{
@ -338,25 +338,25 @@ void ch395_hardware_init(void)
g_ch395q_sta.ch395_reconnection = ch395_reconnection;
g_ch395q_sta.dhcp_status = DHCP_STA;
i = ch395_cmd_check_exist(0x65); /* 测试命令,用于测试硬件以及接口通讯 */
i = ch395_cmd_check_exist(0x65); /* 测试命令,用于测试硬件以及接口通讯 */
if (i != 0x9a)
{
g_ch395q_sta.ch395_error(i); /* ch395q检测错误 */
g_ch395q_sta.ch395_error(i); /* ch395q检测错误 */
}
ch395_cmd_reset(); /* 对ch395q复位 */
HAL_Delay(100); /* 这里必须等待100以上延时 */
ch395_cmd_reset(); /* 对ch395q复位 */
HAL_Delay(100); /* 这里必须等待100以上延时 */
g_ch395q_sta.ch395_error(ch395_cmd_init()); /* 初始化ch395q命令 */
g_ch395q_sta.ch395_error(ch395_cmd_init()); /* 初始化ch395q命令 */
ch395_socket_r_s_buf_modify();
// ch395_set_tcpmss(536);
// ch395_set_start_para(FUN_PARA_FLAG_TCP_SERVER | SOCK_CTRL_FLAG_SOCKET_CLOSE);
do
{
g_ch395q_sta.phy_status = ch395_cmd_get_phy_status(); /* 获取PHY状态 */
g_ch395q_sta.ch395_phy_cb(g_ch395q_sta.phy_status); /* 判断双工和网速模式 */
g_ch395q_sta.phy_status = ch395_cmd_get_phy_status(); /* 获取PHY状态 */
g_ch395q_sta.ch395_phy_cb(g_ch395q_sta.phy_status); /* 判断双工和网速模式 */
re_cnt++;
if(re_cnt > 50)
@ -365,58 +365,58 @@ void ch395_hardware_init(void)
}
while(g_ch395q_sta.phy_status == PHY_DISCONN);
g_ch395q_sta.version = ch395_cmd_get_ver(); /* 获取版本 */
g_ch395q_sta.version = ch395_cmd_get_ver(); /* 获取版本 */
printf("CH395VER : %2x\r\n", (uint16_t)g_ch395q_sta.version);
// i = ch395_dhcp_enable(1); /* 开启DHCP */
// g_ch395q_sta.ch395_error(i); /* ch395q检测错误 */
// i = ch395_dhcp_enable(1); /* 开启DHCP */
// g_ch395q_sta.ch395_error(i); /* ch395q检测错误 */
HAL_Delay(1000); /* ch395q初始化延时 */
HAL_Delay(1000); /* ch395q初始化延时 */
}
/**
* @brief CH395 socket ,
* @param sockindex 0~7
* @retval
* @brief CH395 socket ,
* @param sockindex 0~7
* @retval
*/
void ch395_socket_interrupt(uint8_t sockindex)
{
uint8_t sock_int_socket;
uint16_t rx_len = 0;
sock_int_socket = ch395_get_socket_int(sockindex); /* 获取socket 的中断状态 */
sock_int_socket = ch395_get_socket_int(sockindex); /* 获取socket 的中断状态 */
if (sock_int_socket & SINT_STAT_SENBUF_FREE) /* 发送缓冲区空闲,可以继续写入要发送的数据 */
if (sock_int_socket & SINT_STAT_SENBUF_FREE) /* 发送缓冲区空闲,可以继续写入要发送的数据 */
{
}
if (sock_int_socket & SINT_STAT_SEND_OK) /* 发送完成中断 */
if (sock_int_socket & SINT_STAT_SEND_OK) /* 发送完成中断 */
{
}
if (sock_int_socket & SINT_STAT_RECV) /* 接收中断 */
if (sock_int_socket & SINT_STAT_RECV) /* 接收中断 */
{
g_ch395q_sta.socket[sockindex].config.recv.size = ch395_get_recv_length(sockindex); /* 获取当前缓冲区内数据长度 */
g_ch395q_sta.socket[sockindex].config.recv.size = ch395_get_recv_length(sockindex); /* 获取当前缓冲区内数据长度 */
rx_len = g_ch395q_sta.socket[sockindex].config.recv.size;
ch395_get_recv_data(sockindex, rx_len, g_ch395q_sta.socket[sockindex].config.recv.buf); /* 读取数据 */
ch395_get_recv_data(sockindex, rx_len, g_ch395q_sta.socket[sockindex].config.recv.buf); /* 读取数据 */
g_ch395q_sta.socket[sockindex].config.recv.buf[rx_len] = '\0';
modbus_process_tcp();
//printf("%s", g_ch395q_sta.socket[sockindex].config.recv.buf);
g_ch395q_sta.socket[sockindex].config.recv.recv_flag |= 0x04;
}
if (sock_int_socket & SINT_STAT_CONNECT) /* 连接中断仅在TCP模式下有效 */
if (sock_int_socket & SINT_STAT_CONNECT) /* 连接中断仅在TCP模式下有效 */
{
if (g_ch395q_sta.socket[sockindex].config.proto == CH395Q_SOCKET_TCP_CLIENT)
{
ch395_set_keeplive(sockindex,1); /* 打开KEEPALIVE保活定时器 */
ch395_setttl_num(sockindex,60); /* 设置TTL */
ch395_set_keeplive(sockindex,1); /* 打开KEEPALIVE保活定时器 */
ch395_setttl_num(sockindex,60); /* 设置TTL */
}
}
if (sock_int_socket & SINT_STAT_DISCONNECT) /* 断开中断仅在TCP模式下有效 */
if (sock_int_socket & SINT_STAT_DISCONNECT) /* 断开中断仅在TCP模式下有效 */
{
g_ch395q_sta.ch395_error(ch395_open_socket(g_ch395q_sta.socket[sockindex].config.socket_index));
@ -431,14 +431,14 @@ void ch395_socket_interrupt(uint8_t sockindex)
default:
break;
}
HAL_Delay(200); /* 延时200MS后再次重试没有必要过于频繁连接 */
HAL_Delay(200); /* 延时200MS后再次重试没有必要过于频繁连接 */
}
if (sock_int_socket & SINT_STAT_TIM_OUT) /* 超时中断仅在TCP模式下有效 */
if (sock_int_socket & SINT_STAT_TIM_OUT) /* 超时中断仅在TCP模式下有效 */
{
if (g_ch395q_sta.socket[sockindex].config.proto == CH395Q_SOCKET_TCP_CLIENT)
{
HAL_Delay(200); /* 延时200MS后再次重试没有必要过于频繁连接 */
HAL_Delay(200); /* 延时200MS后再次重试没有必要过于频繁连接 */
g_ch395q_sta.ch395_error(ch395_open_socket(g_ch395q_sta.socket[sockindex].config.socket_index));
g_ch395q_sta.ch395_error(ch395_tcp_connect(g_ch395q_sta.socket[sockindex].config.socket_index));
}
@ -446,9 +446,9 @@ void ch395_socket_interrupt(uint8_t sockindex)
}
/**
* @brief CH395全局中断函数
* @param
* @retval
* @brief CH395全局中断函数
* @param
* @retval
*/
void ch395_interrupt_handler(void)
{
@ -457,22 +457,22 @@ void ch395_interrupt_handler(void)
init_status = ch395_cmd_get_glob_int_status_all();
if (init_status & GINT_STAT_UNREACH) /* 不可达中断,读取不可达信息 */
if (init_status & GINT_STAT_UNREACH) /* 不可达中断,读取不可达信息 */
{
ch395_cmd_get_unreachippt(g_ch395q_sta.ipinf_buf);
}
if (init_status & GINT_STAT_IP_CONFLI) /* 产生IP冲突中断建议重新修改CH395的 IP并初始化CH395 */
if (init_status & GINT_STAT_IP_CONFLI) /* 产生IP冲突中断建议重新修改CH395的 IP并初始化CH395 */
{
}
if (init_status & GINT_STAT_PHY_CHANGE) /* 产生PHY改变中断 */
if (init_status & GINT_STAT_PHY_CHANGE) /* 产生PHY改变中断 */
{
g_ch395q_sta.phy_status = ch395_cmd_get_phy_status(); /* 获取PHY状态 */
g_ch395q_sta.phy_status = ch395_cmd_get_phy_status(); /* 获取PHY状态 */
}
if (init_status & GINT_STAT_DHCP) /* 处理DHCP中断 */
if (init_status & GINT_STAT_DHCP) /* 处理DHCP中断 */
{
g_ch395q_sta.dhcp_status = DHCP_DOWN;
return;
@ -492,72 +492,72 @@ void ch395_interrupt_handler(void)
// break;
// default:
// g_ch395q_sta.dhcp_status = DHCP_DOWN;
// /* 设置默认IP地址信息 */
// printf("静态IP信息.....................................\r\n");
// /* 设置默认IP地址信息 */
// printf("静态IP信息.....................................\r\n");
// break;
// }
}
if (init_status & GINT_STAT_SOCK0)
{
ch395_socket_interrupt(CH395Q_SOCKET_0); /* 处理socket 0中断 */
ch395_socket_interrupt(CH395Q_SOCKET_0); /* 处理socket 0中断 */
}
if (init_status & GINT_STAT_SOCK1)
{
ch395_socket_interrupt(CH395Q_SOCKET_1); /* 处理socket 1中断 */
ch395_socket_interrupt(CH395Q_SOCKET_1); /* 处理socket 1中断 */
}
if (init_status & GINT_STAT_SOCK2)
{
ch395_socket_interrupt(CH395Q_SOCKET_2); /* 处理socket 2中断 */
ch395_socket_interrupt(CH395Q_SOCKET_2); /* 处理socket 2中断 */
}
if (init_status & GINT_STAT_SOCK3)
{
ch395_socket_interrupt(CH395Q_SOCKET_3); /* 处理socket 3中断 */
ch395_socket_interrupt(CH395Q_SOCKET_3); /* 处理socket 3中断 */
}
if (init_status & GINT_STAT_SOCK4)
{
ch395_socket_interrupt(CH395Q_SOCKET_4); /* 处理socket 4中断 */
ch395_socket_interrupt(CH395Q_SOCKET_4); /* 处理socket 4中断 */
}
if (init_status & GINT_STAT_SOCK5)
{
ch395_socket_interrupt(CH395Q_SOCKET_5); /* 处理socket 5中断 */
ch395_socket_interrupt(CH395Q_SOCKET_5); /* 处理socket 5中断 */
}
if (init_status & GINT_STAT_SOCK6)
{
ch395_socket_interrupt(CH395Q_SOCKET_6); /* 处理socket 6中断 */
ch395_socket_interrupt(CH395Q_SOCKET_6); /* 处理socket 6中断 */
}
if (init_status & GINT_STAT_SOCK7)
{
ch395_socket_interrupt(CH395Q_SOCKET_7); /* 处理socket 7中断 */
ch395_socket_interrupt(CH395Q_SOCKET_7); /* 处理socket 7中断 */
}
}
/**
* @brief CH395全局管理函数
* @param
* @retval
* @brief CH395全局管理函数
* @param
* @retval
*/
void ch395q_handler(void)
{
if (ch395_int_pin_wire == 0)
{
ch395_interrupt_handler(); /* 中断处理函数 */
ch395_interrupt_handler(); /* 中断处理函数 */
}
g_ch395q_sta.ch395_reconnection(); /* 检测PHY状态并重新连接 */
g_ch395q_sta.ch395_reconnection(); /* 检测PHY状态并重新连接 */
}
/**
* @brief PHY状态
* @param
* @retval
* @brief PHY状态
* @param
* @retval
*/
void ch395_reconnection(void)
{
@ -568,7 +568,7 @@ void ch395_reconnection(void)
if (g_ch395q_sta.socket[socket_index].config.socket_enable == CH395Q_ENABLE)
{
ch395_close_socket(g_ch395q_sta.socket[socket_index].config.socket_index);
g_ch395q_sta.ch395_error(ch395_dhcp_enable(0)); /* ch395q检测错误 */
g_ch395q_sta.ch395_error(ch395_dhcp_enable(0)); /* ch395q检测错误 */
g_ch395q_sta.socket[socket_index].config.socket_enable = CH395Q_DISABLE;
g_ch395q_sta.dhcp_status = DHCP_STA;
}
@ -579,61 +579,61 @@ void ch395_reconnection(void)
{
if (g_ch395q_sta.dhcp_status == DHCP_STA)
{
ch395_cmd_reset(); /* 对ch395q复位 */
HAL_Delay(100); /* 这里必须等待100以上延时 */
ch395_cmd_reset(); /* 对ch395q复位 */
HAL_Delay(100); /* 这里必须等待100以上延时 */
ch395_cmd_init();
HAL_Delay(100); /* 这里必须等待100以上延时 */
HAL_Delay(100); /* 这里必须等待100以上延时 */
ch395_socket_r_s_buf_modify();
// ch395_set_tcpmss(536);
// ch395_set_start_para(FUN_PARA_FLAG_TCP_SERVER | SOCK_CTRL_FLAG_SOCKET_CLOSE);
// g_ch395q_sta.ch395_error(ch395_dhcp_enable(1)); /* 开启DHCP */
// g_ch395q_sta.ch395_error(ch395_dhcp_enable(1)); /* 开启DHCP */
}
// do
// {
// if (ch395_int_pin_wire == 0)
// {
// ch395_interrupt_handler(); /* 中断处理函数 */
// ch395_interrupt_handler(); /* 中断处理函数 */
// }
// }
// while (g_ch395q_sta.dhcp_status == DHCP_STA); /* 获取DHCP */
// while (g_ch395q_sta.dhcp_status == DHCP_STA); /* 获取DHCP */
switch(g_ch395q_sta.socket[socket_index].config.proto)
{
case CH395Q_SOCKET_UDP:
/* socket 为UDP模式 */
ch395_set_socket_desip(socket_index, g_ch395q_sta.socket[socket_index].config.des_ip); /* 设置socket 0目标IP地址 */
ch395_set_socket_prot_type(socket_index, PROTO_TYPE_UDP); /* 设置socket 0协议类型 */
ch395_set_socket_desport(socket_index, g_ch395q_sta.socket[socket_index].config.des_port); /* 设置socket 0目的端口 */
ch395_set_socket_sourport(socket_index, g_ch395q_sta.socket[socket_index].config.sour_port); /* 设置socket 0源端口 */
g_ch395q_sta.ch395_error(ch395_open_socket(socket_index)); /* 检查是否成功 */
/* socket 为UDP模式 */
ch395_set_socket_desip(socket_index, g_ch395q_sta.socket[socket_index].config.des_ip); /* 设置socket 0目标IP地址 */
ch395_set_socket_prot_type(socket_index, PROTO_TYPE_UDP); /* 设置socket 0协议类型 */
ch395_set_socket_desport(socket_index, g_ch395q_sta.socket[socket_index].config.des_port); /* 设置socket 0目的端口 */
ch395_set_socket_sourport(socket_index, g_ch395q_sta.socket[socket_index].config.sour_port); /* 设置socket 0源端口 */
g_ch395q_sta.ch395_error(ch395_open_socket(socket_index)); /* 检查是否成功 */
break;
case CH395Q_SOCKET_TCP_CLIENT:
/* socket 为TCPClient模式 */
ch395_keeplive_set(); /* 保活设置 */
ch395_set_socket_desip(socket_index, g_ch395q_sta.socket[socket_index].config.des_ip); /* 设置socket 0目标IP地址 */
ch395_set_socket_prot_type(socket_index, PROTO_TYPE_TCP); /* 设置socket 0协议类型 */
ch395_set_socket_desport(socket_index,g_ch395q_sta.socket[socket_index].config.des_port); /* 设置socket 0目的端口 */
ch395_set_socket_sourport(socket_index,g_ch395q_sta.socket[socket_index].config.sour_port); /* 设置socket 0源端口 */
g_ch395q_sta.ch395_error(ch395_open_socket(socket_index)); /* 检查sokect是否打开成功 */
g_ch395q_sta.ch395_error(ch395_tcp_connect(socket_index)); /* 检查tcp连接是否成功 */
/* socket 为TCPClient模式 */
ch395_keeplive_set(); /* 保活设置 */
ch395_set_socket_desip(socket_index, g_ch395q_sta.socket[socket_index].config.des_ip); /* 设置socket 0目标IP地址 */
ch395_set_socket_prot_type(socket_index, PROTO_TYPE_TCP); /* 设置socket 0协议类型 */
ch395_set_socket_desport(socket_index,g_ch395q_sta.socket[socket_index].config.des_port); /* 设置socket 0目的端口 */
ch395_set_socket_sourport(socket_index,g_ch395q_sta.socket[socket_index].config.sour_port); /* 设置socket 0源端口 */
g_ch395q_sta.ch395_error(ch395_open_socket(socket_index)); /* 检查sokect是否打开成功 */
g_ch395q_sta.ch395_error(ch395_tcp_connect(socket_index)); /* 检查tcp连接是否成功 */
break;
case CH395Q_SOCKET_TCP_SERVER:
/* socket 为TCPServer模式 */
//ch395_set_socket_desip(socket_index, g_ch395q_sta.socket[socket_index].config.des_ip); /* 设置socket 0目标IP地址 */
ch395_set_socket_prot_type(socket_index, PROTO_TYPE_TCP); /* 设置socket 0协议类型 */
ch395_set_socket_sourport(socket_index, g_ch395q_sta.socket[socket_index].config.sour_port); /* 设置socket 0源端口 */
/* socket 为TCPServer模式 */
//ch395_set_socket_desip(socket_index, g_ch395q_sta.socket[socket_index].config.des_ip); /* 设置socket 0目标IP地址 */
ch395_set_socket_prot_type(socket_index, PROTO_TYPE_TCP); /* 设置socket 0协议类型 */
ch395_set_socket_sourport(socket_index, g_ch395q_sta.socket[socket_index].config.sour_port); /* 设置socket 0源端口 */
HAL_Delay(3000);
g_ch395q_sta.ch395_error(ch395_open_socket(socket_index)); /* 检查sokect是否打开成功 */
g_ch395q_sta.ch395_error(ch395_tcp_listen(socket_index)); /* 监听tcp连接 */
g_ch395q_sta.ch395_error(ch395_open_socket(socket_index)); /* 检查sokect是否打开成功 */
g_ch395q_sta.ch395_error(ch395_tcp_listen(socket_index)); /* 监听tcp连接 */
break;
case CH395Q_SOCKET_MAC_RAW:
ch395_set_socket_prot_type(socket_index, PROTO_TYPE_MAC_RAW); /* 设置socket 0协议类型 */
g_ch395q_sta.ch395_error(ch395_open_socket(socket_index)); /* 检查sokect是否打开成功 */
ch395_set_socket_prot_type(socket_index, PROTO_TYPE_MAC_RAW); /* 设置socket 0协议类型 */
g_ch395q_sta.ch395_error(ch395_open_socket(socket_index)); /* 检查sokect是否打开成功 */
break;
default:
ch395_set_socket_prot_type(socket_index, PROTO_TYPE_TCP);
ch395_set_socket_sourport(socket_index, g_ch395q_sta.socket[socket_index].config.sour_port); /* 设置socket 1~7源端口 */
ch395_set_socket_sourport(socket_index, g_ch395q_sta.socket[socket_index].config.sour_port); /* 设置socket 1~7源端口 */
break;
}
g_ch395q_sta.socket[socket_index].config.socket_enable = CH395Q_ENABLE;
@ -643,22 +643,22 @@ void ch395_reconnection(void)
}
/* 本地网络信息IP地址、网关地址、子网掩码和MAC地址 */
/* 本地网络信息IP地址、网关地址、子网掩码和MAC地址 */
uint8_t ch395_ipaddr[4] = {192,168,114,194};
uint8_t ch395_gw_ipaddr[4] = {192,168,114,110};
uint8_t ch395_ipmask[4] = {255,255,255,0};
uint16_t ch395_port = 8080;
//uint8_t ch395_macaddr[6] = {0x5C,0x53,0x10,0x6C,0x18,0x49};
/* 远程IP地址设置 */
/* 远程IP地址设置 */
//uint8_t ch395_des_ipaddr[4] = {192,168,1,111};
static uint8_t socket0_send_buf[] = {"This is from CH395Q\r\n"};
static uint8_t socket0_recv_buf[1024];
ch395_socket cha95_sockct_sta[8];
/**
* @brief
* @param
* @retval
* @brief
* @param
* @retval
*/
void ch395_init(void)
{
@ -666,7 +666,7 @@ void ch395_init(void)
// {
// ch395q_handler();
// }
// while (g_ch395q_sta.dhcp_status == DHCP_STA); /* 获取DHCP */
// while (g_ch395q_sta.dhcp_status == DHCP_STA); /* 获取DHCP */
for(int n = 0;n < 4;n++)
{
@ -676,20 +676,20 @@ void ch395_init(void)
}
ch395_port = HoldReg[52];
cha95_sockct_sta[0].socket_enable = CH395Q_ENABLE; /* 使能socket对 */
cha95_sockct_sta[0].socket_index = CH395Q_SOCKET_0; /* 设置socket对 */
//memcpy(cha95_sockct_sta[0].des_ip, ch395_des_ipaddr, sizeof(cha95_sockct_sta[0].des_ip)); /* 设置目标IP地址 */
memcpy(cha95_sockct_sta[0].net_config.ipaddr, ch395_ipaddr, sizeof(cha95_sockct_sta[0].net_config.ipaddr)); /* 设置静态本地IP地址 */
memcpy(cha95_sockct_sta[0].net_config.gwipaddr, ch395_gw_ipaddr, sizeof(cha95_sockct_sta[0].net_config.gwipaddr)); /* 设置静态网关IP地址 */
memcpy(cha95_sockct_sta[0].net_config.maskaddr, ch395_ipmask, sizeof(cha95_sockct_sta[0].net_config.maskaddr)); /* 设置静态子网掩码地址 */
//memcpy(cha95_sockct_sta[0].net_config.macaddr, ch395_macaddr, sizeof(cha95_sockct_sta[0].net_config.macaddr)); /* 设置静态MAC地址 */
cha95_sockct_sta[0].sour_port = 8080; /* 源端口 */
cha95_sockct_sta[0].proto = CH395Q_SOCKET_TCP_SERVER; /* 设置协议 */
cha95_sockct_sta[0].send.buf = socket0_send_buf; /* 发送数据 */
cha95_sockct_sta[0].send.size = sizeof(socket0_send_buf); /* 发送数据大小 */
cha95_sockct_sta[0].recv.buf = socket0_recv_buf; /* 接收数据缓冲区 */
cha95_sockct_sta[0].recv.size = sizeof(socket0_recv_buf); /* 接收数据大小 */
ch395q_socket_config(&cha95_sockct_sta[0]); /* 配置socket参数 */
cha95_sockct_sta[0].socket_enable = CH395Q_ENABLE; /* 使能socket对 */
cha95_sockct_sta[0].socket_index = CH395Q_SOCKET_0; /* 设置socket对 */
//memcpy(cha95_sockct_sta[0].des_ip, ch395_des_ipaddr, sizeof(cha95_sockct_sta[0].des_ip)); /* 设置目标IP地址 */
memcpy(cha95_sockct_sta[0].net_config.ipaddr, ch395_ipaddr, sizeof(cha95_sockct_sta[0].net_config.ipaddr)); /* 设置静态本地IP地址 */
memcpy(cha95_sockct_sta[0].net_config.gwipaddr, ch395_gw_ipaddr, sizeof(cha95_sockct_sta[0].net_config.gwipaddr)); /* 设置静态网关IP地址 */
memcpy(cha95_sockct_sta[0].net_config.maskaddr, ch395_ipmask, sizeof(cha95_sockct_sta[0].net_config.maskaddr)); /* 设置静态子网掩码地址 */
//memcpy(cha95_sockct_sta[0].net_config.macaddr, ch395_macaddr, sizeof(cha95_sockct_sta[0].net_config.macaddr)); /* 设置静态MAC地址 */
cha95_sockct_sta[0].sour_port = ch395_port; /* 源端口 */
cha95_sockct_sta[0].proto = CH395Q_SOCKET_TCP_SERVER; /* 设置协议 */
cha95_sockct_sta[0].send.buf = socket0_send_buf; /* 发送数据 */
cha95_sockct_sta[0].send.size = sizeof(socket0_send_buf); /* 发送数据大小 */
cha95_sockct_sta[0].recv.buf = socket0_recv_buf; /* 接收数据缓冲区 */
cha95_sockct_sta[0].recv.size = sizeof(socket0_recv_buf); /* 接收数据大小 */
ch395q_socket_config(&cha95_sockct_sta[0]); /* 配置socket参数 */
}
@ -704,22 +704,22 @@ void set_ipv4(void)
ch395_port = HoldReg[52];
ch395_close_socket(cha95_sockct_sta[0].socket_index);
g_ch395q_sta.ch395_error(ch395_dhcp_enable(0)); /* ch395q检测错误 */
g_ch395q_sta.ch395_error(ch395_dhcp_enable(0)); /* ch395q检测错误 */
cha95_sockct_sta[0].socket_enable = CH395Q_DISABLE;
g_ch395q_sta.dhcp_status = DHCP_STA;
HAL_Delay(500);
ch395_cmd_reset(); /* 对ch395q复位 */
HAL_Delay(500); /* 这里必须等待100以上延时 */
ch395_cmd_reset(); /* 对ch395q复位 */
HAL_Delay(500); /* 这里必须等待100以上延时 */
g_ch395q_sta.ch395_error(ch395_cmd_init()); /* 初始化ch395q命令 */
g_ch395q_sta.ch395_error(ch395_cmd_init()); /* 初始化ch395q命令 */
ch395_socket_r_s_buf_modify();
do
{
g_ch395q_sta.phy_status = ch395_cmd_get_phy_status(); /* 获取PHY状态 */
g_ch395q_sta.ch395_phy_cb(g_ch395q_sta.phy_status); /* 判断双工和网速模式 */
g_ch395q_sta.phy_status = ch395_cmd_get_phy_status(); /* 获取PHY状态 */
g_ch395q_sta.ch395_phy_cb(g_ch395q_sta.phy_status); /* 判断双工和网速模式 */
re_cnt++;
if(re_cnt > 50)
@ -729,13 +729,13 @@ void set_ipv4(void)
HAL_Delay(1000);
cha95_sockct_sta[0].socket_enable = CH395Q_ENABLE; /* 使能socket对 */
cha95_sockct_sta[0].socket_index = CH395Q_SOCKET_0; /* 设置socket对 */
memcpy(cha95_sockct_sta[0].net_config.ipaddr, ch395_ipaddr, sizeof(cha95_sockct_sta[0].net_config.ipaddr)); /* 设置静态本地IP地址 */
memcpy(cha95_sockct_sta[0].net_config.gwipaddr, ch395_gw_ipaddr, sizeof(cha95_sockct_sta[0].net_config.gwipaddr)); /* 设置静态网关IP地址 */
memcpy(cha95_sockct_sta[0].net_config.maskaddr, ch395_ipmask, sizeof(cha95_sockct_sta[0].net_config.maskaddr)); /* 设置静态子网掩码地址 */
cha95_sockct_sta[0].sour_port = 8080; /* 源端口 */
ch395q_socket_config(&cha95_sockct_sta[0]); /* 配置socket参数 */
cha95_sockct_sta[0].socket_enable = CH395Q_ENABLE; /* 使能socket对 */
cha95_sockct_sta[0].socket_index = CH395Q_SOCKET_0; /* 设置socket对 */
memcpy(cha95_sockct_sta[0].net_config.ipaddr, ch395_ipaddr, sizeof(cha95_sockct_sta[0].net_config.ipaddr)); /* 设置静态本地IP地址 */
memcpy(cha95_sockct_sta[0].net_config.gwipaddr, ch395_gw_ipaddr, sizeof(cha95_sockct_sta[0].net_config.gwipaddr)); /* 设置静态网关IP地址 */
memcpy(cha95_sockct_sta[0].net_config.maskaddr, ch395_ipmask, sizeof(cha95_sockct_sta[0].net_config.maskaddr)); /* 设置静态子网掩码地址 */
cha95_sockct_sta[0].sour_port = ch395_port; /* 源端口 */
ch395q_socket_config(&cha95_sockct_sta[0]); /* 配置socket参数 */
}

View File

@ -205,6 +205,11 @@
<WinNumber>1</WinNumber>
<ItemText>astep_s</ItemText>
</Ww>
<Ww>
<count>10</count>
<WinNumber>1</WinNumber>
<ItemText>tx_start</ItemText>
</Ww>
</WatchWindow1>
<MemoryWindow1>
<Mm>