From 991258c084018f14a2ad602b5a1ef179c7f019d3 Mon Sep 17 00:00:00 2001 From: kyz Date: Tue, 2 Apr 2024 16:29:40 +0800 Subject: [PATCH] =?utf8?q?=E6=94=AF=E6=8C=81=E4=BB=A5=E5=A4=AA=E7=BD=91TCP?= =?utf8?q?=E8=BF=9E=E6=8E=A5=E4=B8=8A=E6=8A=A5=E5=B9=B3=E5=8F=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 以太网tcp连接上报功能支持 比例阀超温保护功能启用 --- Core/Src/main.c | 3 +- Drivers/BSP/Components/lan8742/lan8742.c | 61 ++-- LWIP/Target/lwipopts.h | 14 +- MDK-ARM/welding_controller_v2.uvprojx | 5 + user/at_parser.c | 10 +- user/bootloader.c | 5 + user/user.h | 6 +- user/user_adc.c | 10 +- user/user_app.c | 14 +- user/user_cdc_rx_2.c | 4 +- user/user_eth.c | 345 +++++++++++++++++++---- user/user_report.c | 47 +++ user/user_ui.c | 45 ++- user/user_valve_ctrl.c | 17 +- user/user_wifi.c | 72 ++--- welding_controller_v2.ioc | 8 +- 16 files changed, 482 insertions(+), 184 deletions(-) create mode 100644 user/user_report.c diff --git a/Core/Src/main.c b/Core/Src/main.c index 8802609..02a1313 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -69,7 +69,6 @@ void MX_FREERTOS_Init(void); /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ - /* USER CODE END 0 */ /** @@ -84,6 +83,8 @@ int main(void) //程序0x00001000不放0地址,否则许多内存读写函数会判为NULL? //在这里手动修改VTOR SCB->VTOR = 0x1000; + TIM7->DIER &= ~(TIM_IT_UPDATE); //防止bootloader有pending中断在TIM7初始化前被触发,引起异常。 + TIM7->SR &= ~(TIM_IT_UPDATE); /* USER CODE END 1 */ /* MPU Configuration--------------------------------------------------------*/ diff --git a/Drivers/BSP/Components/lan8742/lan8742.c b/Drivers/BSP/Components/lan8742/lan8742.c index a268393..aacddbc 100644 --- a/Drivers/BSP/Components/lan8742/lan8742.c +++ b/Drivers/BSP/Components/lan8742/lan8742.c @@ -87,7 +87,7 @@ int32_t LAN8742_RegisterBusIO(lan8742_Object_t *pObj, lan8742_IOCtx_t *ioctx) */ int32_t LAN8742_Init(lan8742_Object_t *pObj) { - uint32_t tickstart = 0, regvalue = 0, addr = 0; + uint32_t tickstart = 0, regvalue = 0; int32_t status = LAN8742_STATUS_OK; if(pObj->Is_Initialized == 0) @@ -99,32 +99,8 @@ int32_t LAN8742_RegisterBusIO(lan8742_Object_t *pObj, lan8742_IOCtx_t *ioctx) } /* for later check */ - pObj->DevAddr = LAN8742_MAX_DEV_ADDR + 1; + pObj->DevAddr = 3; - /* Get the device address from special mode register */ - for(addr = 0; addr <= LAN8742_MAX_DEV_ADDR; addr ++) - { - if(pObj->IO.ReadReg(addr, LAN8742_SMR, ®value) < 0) - { - status = LAN8742_STATUS_READ_ERROR; - /* Can't read from this device address - continue with next address */ - continue; - } - - if((regvalue & LAN8742_SMR_PHY_ADDR) == addr) - { - pObj->DevAddr = addr; - status = LAN8742_STATUS_OK; - break; - } - } - - if(pObj->DevAddr > LAN8742_MAX_DEV_ADDR) - { - status = LAN8742_STATUS_ADDRESS_ERROR; - } - /* if device address is matched */ if(status == LAN8742_STATUS_OK) { @@ -168,6 +144,7 @@ int32_t LAN8742_RegisterBusIO(lan8742_Object_t *pObj, lan8742_IOCtx_t *ioctx) if(status == LAN8742_STATUS_OK) { + pObj->IO.WriteReg(pObj->DevAddr, LAN8742_BCR, LAN8742_BCR_AUTONEGO_EN); tickstart = pObj->IO.GetTick(); /* Wait for 2s to perform initialization */ @@ -354,34 +331,28 @@ int32_t LAN8742_GetLinkState(lan8742_Object_t *pObj) } } else /* Auto Nego enabled */ - { - if(pObj->IO.ReadReg(pObj->DevAddr, LAN8742_PHYSCSR, &readval) < 0) + { + if(pObj->IO.ReadReg(pObj->DevAddr, 0x11, &readval) < 0) { return LAN8742_STATUS_READ_ERROR; } /* Check if auto nego not done */ - if((readval & LAN8742_PHYSCSR_AUTONEGO_DONE) == 0) + if((readval & (1<<11)) == 0) { return LAN8742_STATUS_AUTONEGO_NOTDONE; } - - if((readval & LAN8742_PHYSCSR_HCDSPEEDMASK) == LAN8742_PHYSCSR_100BTX_FD) - { - return LAN8742_STATUS_100MBITS_FULLDUPLEX; - } - else if ((readval & LAN8742_PHYSCSR_HCDSPEEDMASK) == LAN8742_PHYSCSR_100BTX_HD) - { - return LAN8742_STATUS_100MBITS_HALFDUPLEX; - } - else if ((readval & LAN8742_PHYSCSR_HCDSPEEDMASK) == LAN8742_PHYSCSR_10BT_FD) - { - return LAN8742_STATUS_10MBITS_FULLDUPLEX; + + if((readval >>13) == 3 ){//b'01 1' + return LAN8742_STATUS_100MBITS_FULLDUPLEX; + }else if((readval >>13) == 2 ){//b'01 0' + return LAN8742_STATUS_100MBITS_HALFDUPLEX; + }else if((readval >>13) == 2 ){//b'00 1' + return LAN8742_STATUS_10MBITS_HALFDUPLEX; + }else{ + return LAN8742_STATUS_10MBITS_HALFDUPLEX; } - else - { - return LAN8742_STATUS_10MBITS_HALFDUPLEX; - } + } } diff --git a/LWIP/Target/lwipopts.h b/LWIP/Target/lwipopts.h index 8ecd77b..346dc36 100644 --- a/LWIP/Target/lwipopts.h +++ b/LWIP/Target/lwipopts.h @@ -113,9 +113,21 @@ #define CHECKSUM_CHECK_TCP 0 /*----- Value in opt.h for CHECKSUM_CHECK_ICMP6: 1 -----*/ #define CHECKSUM_CHECK_ICMP6 0 +/*----- Default Value for NETIF_DEBUG: LWIP_DBG_OFF ---*/ +#define NETIF_DEBUG LWIP_DBG_ON +/*----- Default Value for PBUF_DEBUG: LWIP_DBG_OFF ---*/ +//#define PBUF_DEBUG LWIP_DBG_ON +/*----- Default Value for MEM_DEBUG: LWIP_DBG_OFF ---*/ +#define MEM_DEBUG LWIP_DBG_ON +/*----- Default Value for MEMP_DEBUG: LWIP_DBG_OFF ---*/ +#define MEMP_DEBUG LWIP_DBG_ON +/*----- Default Value for TCP_DEBUG: LWIP_DBG_OFF ---*/ +//#define TCP_DEBUG LWIP_DBG_ON +/*----- Default Value for TCP_INPUT_DEBUG: LWIP_DBG_OFF ---*/ +//#define TCP_INPUT_DEBUG LWIP_DBG_ON /*-----------------------------------------------------------------------------*/ /* USER CODE BEGIN 1 */ - +#define LWIP_DEBUG /* USER CODE END 1 */ #ifdef __cplusplus diff --git a/MDK-ARM/welding_controller_v2.uvprojx b/MDK-ARM/welding_controller_v2.uvprojx index dd61466..50a54ba 100644 --- a/MDK-ARM/welding_controller_v2.uvprojx +++ b/MDK-ARM/welding_controller_v2.uvprojx @@ -1118,6 +1118,11 @@ 1 ..\user\user_eth.c + + user_report.c + 1 + ..\user\user_report.c + diff --git a/user/at_parser.c b/user/at_parser.c index 85db462..2a2a385 100644 --- a/user/at_parser.c +++ b/user/at_parser.c @@ -221,8 +221,8 @@ int at_parser_init(void) char *send_delimiter = AT_SEND_DELIMITER; int timeout = AT_UART_TIMEOUT_MS; - void *task; - int stack_used; +// void *task; +// int stack_used; printf( "%s\n", __func__ ); @@ -297,7 +297,7 @@ static int at_recvfrom_lower(at_uart_dev_t *uart, void *data, uint32_t expect_si { extern int32_t wifi_read_byte( uint8_t *rd_byte ); - int ret = -1; +// int ret = -1; uint8_t rd_byte; uint32_t rd_size = 0; @@ -836,7 +836,7 @@ static void at_worker(void *arg) offset = memcpy_size; } } - vTaskDelete(NULL); - return; + //vTaskDelete(NULL); + //return; } diff --git a/user/bootloader.c b/user/bootloader.c index df357ac..31dceab 100644 --- a/user/bootloader.c +++ b/user/bootloader.c @@ -71,6 +71,8 @@ void launch_app( uint32_t addr ) SysTick->CTRL = 0; HAL_TIM_Base_DeInit(&htim7); + __HAL_TIM_DISABLE_IT(&htim7, TIM_IT_UPDATE ); + __HAL_TIM_CLEAR_FLAG(&htim7, TIM_IT_UPDATE ); /* Initialize user application's Stack Pointer & Jump to user application */ JumpToApplication = (pFunction) (*(__IO uint32_t*) ( addr + 4)); @@ -341,3 +343,6 @@ uint16_t user_crc16( uint8_t *data, uint32_t length ) { return mbm_CRC16Calc( data, length ); } + +ETH_HandleTypeDef heth; +void HAL_ETH_IRQHandler( ETH_HandleTypeDef * unused){ return;} \ No newline at end of file diff --git a/user/user.h b/user/user.h index 7024745..ae00504 100644 --- a/user/user.h +++ b/user/user.h @@ -109,7 +109,7 @@ typedef struct{ #define PRESSURE_SENSOR_ERR ( 1<<2 ) //压力计故障 #define TEMP_ERR ( 1<<3 ) //超温故障 #define SFLASH_ERR ( 1<<4 ) //spi flash 故障 -#define VALVE_CTRL_ERROR ( 1<<5 ) //比例阀电流控制 AD5245 故障 +#define VALVE_CTRL_ERR ( 1<<5 ) //比例阀电流控制 AD5245 故障 #define WIFI_ERR ( 1<<6 ) @@ -148,7 +148,9 @@ typedef struct { float debug_welding_curr; //模拟焊接电流 A float debug_valve_curr; //设置比例阀输出电流 A - uint32_t connected; //0:disconnect, 1:wifi connected, 2:tcp connected + uint32_t wifi_connected; //0:disconnect, 1:wifi connected, 2:tcp connected + uint32_t eth_connected; //0: eth disconnect, 1:eth connected, 2::tcp connected + uint32_t remote_locked; //0: unlock, 1:locked }dev_info_t; extern TIM_HandleTypeDef htim2; //htim2 用于触发DMA ADC采集 diff --git a/user/user_adc.c b/user/user_adc.c index 3f508f8..ba51773 100644 --- a/user/user_adc.c +++ b/user/user_adc.c @@ -166,7 +166,7 @@ void user_adc_task( void* unused ) float voltage; uint32_t pressure_err = 0; uint32_t curr_err = 0; - uint32_t temp_err = 0; +// uint32_t temp_err = 0; uint32_t pressure_sum = 0; uint32_t flowrate_sum = 0; @@ -301,13 +301,14 @@ void user_adc_task( void* unused ) ----------------------------------------------------------------------------------- */ - HAL_ADC_Start_DMA( &hadc3, (uint32_t*)&adc3_raw[0], ARRAY_SIZE(adc3_raw) ); + HAL_ADC_Start_DMA( &hadc3, (uint32_t*)&adc3_raw[0], ADC3_RAW_NRS ); - raw = adc_average( adc3_raw_copy, ARRAY_SIZE(adc3_raw_copy) ); + raw = adc_average( adc3_raw_copy, ADC3_RAW_NRS ); voltage = raw*VREF/0x0fff; //12bit adc3 g_dev.valve_temperature = voltage; - if( voltage < 0.55f ){ +#if 0 //超温提示代码移动到比例阀控制任务中处理 + if( voltage < 0.50f ){ if( temp_err > 10 ){ //0.5秒*10=5秒后报告传感器故障 g_dev.state |= TEMP_ERR; @@ -320,6 +321,7 @@ void user_adc_task( void* unused ) g_dev.state &= ~TEMP_ERR; } } +#endif } } } diff --git a/user/user_app.c b/user/user_app.c index 32588f3..8ec1934 100644 --- a/user/user_app.c +++ b/user/user_app.c @@ -59,8 +59,6 @@ void StartDefaultTask(void *argument) { /* init code for USB_DEVICE */ MX_USB_DEVICE_Init(); - /* init code for LWIP */ - MX_LWIP_Init(); #if 0 while(0){ @@ -238,7 +236,7 @@ void user_beep_task( void* unused ) } osDelay(1000); } - if( g_dev.state & VALVE_CTRL_ERROR ){ + if( g_dev.state & VALVE_CTRL_ERR ){ for( i=0; i<4; i++ ){ osDelay(100); HAL_GPIO_WritePin(PF8_PWM_SPK_TIM13_CH1_GPIO_Port, PF8_PWM_SPK_TIM13_CH1_Pin, GPIO_PIN_SET); @@ -247,6 +245,16 @@ void user_beep_task( void* unused ) } osDelay(1000); } + if( g_dev.state & TEMP_ERR ){ + for( i=0; i<5; i++ ){ + osDelay(100); + HAL_GPIO_WritePin(PF8_PWM_SPK_TIM13_CH1_GPIO_Port, PF8_PWM_SPK_TIM13_CH1_Pin, GPIO_PIN_SET); + osDelay(100); + HAL_GPIO_WritePin(PF8_PWM_SPK_TIM13_CH1_GPIO_Port, PF8_PWM_SPK_TIM13_CH1_Pin, GPIO_PIN_RESET); + } + osDelay(1000); + } + osDelay(5000); } } diff --git a/user/user_cdc_rx_2.c b/user/user_cdc_rx_2.c index e11264d..e99b870 100644 --- a/user/user_cdc_rx_2.c +++ b/user/user_cdc_rx_2.c @@ -441,7 +441,7 @@ int32_t get_app_fw_info( uint32_t * fw_size, uint16_t fw_crc ) uint32_t flag; uint8_t *sw_ver_addr; uint32_t *region_base; - uint32_t *region_limit; +// uint32_t *region_limit; uint32_t vectors_addr; uint32_t image_size; uint32_t image_crc; @@ -471,7 +471,7 @@ int32_t get_app_fw_info( uint32_t * fw_size, uint16_t fw_crc ) sw_ver_addr = (uint8_t *)p[1]; region_base = (uint32_t *)p[2]; - region_limit = (uint32_t *)p[3]; +// region_limit = (uint32_t *)p[3]; vectors_addr = p[4]; image_size = region_base[4] - vectors_addr; diff --git a/user/user_eth.c b/user/user_eth.c index 30fcd4a..98884ab 100644 --- a/user/user_eth.c +++ b/user/user_eth.c @@ -1,85 +1,306 @@ #include "user.h" #include "at_parser.h" +#include "lwip.h" +#include "lwip/api.h" +#include "lwip/netif.h" +//#include "stm32h7xx_hal.h" +//#include "cmsis_os.h" +#include "socket.h" +#include "dns.h" +#include "tcp.h" + + +typedef struct{ +// char* host; +// uint16_t port; + + ip_addr_t addr; + struct tcp_pcb *pcb; + int32_t connected; //0 wait connect, 1: connected, -1:connect fail + int32_t connection_close; //1: to close tcp connection + struct pbuf *p_tx; + struct netif *p_netif; + + //uint32_t lock; + //uint32_t sem; + uint32_t state; + osMutexId_t mutex; +}tcp_cli_t; + +extern struct netif gnetif; + +static const osMutexAttr_t clientMutex_attributes = { + .name = "devinfoMutex" +}; + +static tcp_cli_t g_client; + +extern char * devinfo_report_str( void ); + +int32_t eth_devinfo_report( tcp_cli_t *client ) +{ + char * rpt_data; + err_t err; + + if( !g_client.pcb ){ + printf( "---dev info report fail , pcb is NULL\n" ); + return -1; + } + + rpt_data = devinfo_report_str(); + + if( strlen( rpt_data) > tcp_sndbuf(g_client.pcb) ){ + printf( "tx len[%d] > sendbuf[%d]\n", strlen(rpt_data), tcp_sndbuf(g_client.pcb) ); + return -1; + } + + err = tcp_write( g_client.pcb, rpt_data, strlen(rpt_data), 1 ); + if( err == ERR_OK ){ + err = tcp_output( g_client.pcb ); + //printf( "--tcp output ret:%d-%s, pcb:%p, state:%d-%s\n", err, lwip_strerr(err), g_client.pcb, g_client.pcb->state, tcp_debug_state_str( g_client.pcb->state) ); + }else{ + printf( "--tcp write ret:%d-%s, pcb:%p, state:%d-%s\n", err, lwip_strerr(err), g_client.pcb, g_client.pcb->state, tcp_debug_state_str( g_client.pcb->state) ); + } + return err; +} -static char mac_str[20]; //"00:00:00:00:00:00"; -static const char *fmt = "{\n\ - \"cmd\":\"rt\",\n\ - \"cid\":%d,\n\ - \"mac\":\"%s\",\n\ - \"val\":\n\ - {\n\ - \"c\":\"%0.1f\",\n\ - \"s\":\"%d\",\n\ - \"g\":\"%0.1f\",\n\ - }\n\ -}"; - -void eth_mac_str_init( void ) +void DNS_found_cb(const char *name, const ip_addr_t *ipaddr, void *callback_arg) { - uint32_t uid0; - uint32_t uid1; - uint32_t mac[6]; - - uid0 = HAL_GetUIDw0(); - uid1 = HAL_GetUIDw1(); - mac[0] = (uid0>>24)&0xff; - mac[1] = (uid0>>16)&0xff; - mac[2] = (uid0>>8)&0xff; - mac[3] = (uid0>>0)&0xff; - mac[4] = (uid1>>24)&0xff; - mac[5] = (uid1>>16)&0xff; - - sprintf( mac_str, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5] ); + //uint32_t* ip = (uint32_t*)ipaddr; + //printf("DNS found: IP %d:%d:%d:%d\n",((*ip)&0xff),(((*ip)>>8)&0xff),(((*ip)>>16)&0xff),(((*ip)>>24)&0xff)); } -int32_t eth_devinfo_report( void ) + +int32_t tcp_eth_connection_close( tcp_cli_t *cli ) { - static uint32_t cid = 0; - static uint8_t tx_buf[200]; - float c,g; - uint32_t s; + struct tcp_pcb *tpcb = cli->pcb; + err_t err; - c = g_dev.welding_current; - g = g_dev.flowmeter.flow_speed; - s = g_dev.welding_start; - sprintf( (char*)tx_buf, fmt, cid++, mac_str, c, s, g ); + printf( "tcp_eth_connection_close pcb:%p, state:%d-%s\n", tpcb, tpcb->state, tcp_debug_state_str( tpcb->state) ); + if( tpcb ){ + /* remove callbacks */ + tcp_recv(tpcb, NULL); + tcp_sent(tpcb, NULL); + tcp_poll(tpcb, NULL,0); + /* close tcp connection */ + printf( "tcp abort tpcb:%p, state:%d-%s\n", tpcb, tpcb->state, tcp_debug_state_str( tpcb->state) ); + //err = tcp_close(tpcb); + tcp_abort( tpcb ); + cli->pcb = NULL; + printf( "tcp abort return:%d, tpcb:%p, state:%d-%s\n", err, tpcb, tpcb->state, tcp_debug_state_str( tpcb->state) ); + } + cli->connected = 0; + return ERR_OK; +} - return 0; +static err_t tcp_eth_connected(void *arg, struct tcp_pcb *tpcb, err_t err) +{ + if( err == ERR_OK ){ + g_client.connected = 1; + printf( "cb -- tcp connect ok!\n" ); + }else{ + g_client.connected = -1; + printf( "cb -- tcp connect fail, err:%s!\n", lwip_strerr( err ) ); + } + return ERR_OK; } -#include "lwip/netif.h" -extern struct netif gnetif; -void user_eth_task( void* unused ) + +static err_t tcp_eth_poll(void *arg, struct tcp_pcb *tpcb) { - TickType_t last_ticks; - g_dev.connected = 0; - - //1. init - while( 1 ){ - //eth init fail - printf( "netif ip_addr:%d.%d.%d.%d\n", - (gnetif.ip_addr.addr)&0xff, - (gnetif.ip_addr.addr>>8)&0xff, - (gnetif.ip_addr.addr>>16)&0xff, - (gnetif.ip_addr.addr>>24)&0xff ); - osDelay( 10*1000 ); + tcp_cli_t *client = (tcp_cli_t*)arg; + err_t ret_err = ERR_OK; + + if( !client || client->pcb != tpcb ){ + printf( "--- tcp abort pcb:%p - state:%s, cli.pcb:%p\n", tpcb, tcp_debug_state_str( tpcb->state), client->pcb ); + tcp_abort( tpcb ); + }else{ + if( client->connection_close ){ + client->connection_close = 0; + tcp_eth_connection_close( client ); + } + //printf( "tcp poll:%p - state:%s, cli.pcb:%p\n", tpcb, tcp_debug_state_str( tpcb->state), client->pcb ); } + return ret_err; +} - //if( tcp_server_connect( g_dev.cfg.net.tcp_server, g_dev.cfg.net.tcp_port ) == 0 ){ +static err_t tcp_eth_sent(void *arg, struct tcp_pcb *tpcb, u16_t len) +{ + //printf( "tcp sent %d Bytes\n", len ); + return ERR_OK; +} - last_ticks = xTaskGetTickCount(); +static err_t tcp_eth_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) +{ + tcp_cli_t *client = (tcp_cli_t*)arg; + err_t ret_err; + if (p == NULL){ + /* if we receive an empty tcp frame from server => close connection */ + printf( "remote close connection!\n" ); + tcp_eth_connection_close( client ); + ret_err = ERR_OK; + }else if(err != ERR_OK){ + /* else : a non empty frame was received from server but for some reason err != ERR_OK */ + /* free received pbuf*/ + printf( "tcp recv err:%d-%s, pbuf:%p!\n", err, lwip_strerr(err), p ); + if (p != NULL){ + pbuf_free(p); + } + ret_err = err; + }else if(client->connected > 0 ){ + /* Acknowledge data reception */ + tcp_recved(tpcb, p->tot_len); + + //TODO: handle rx data + printf( "tcp recv %d Bytes!\n", p->tot_len ); + pbuf_free(p); + ret_err = ERR_OK; + }else{ + /* data received when connection already closed */ + /* Acknowledge data reception */ + tcp_recved(tpcb, p->tot_len); + printf( "tcp data received when connection already closed\n" ); - while( g_dev.connected == 2 ){ + /* free pbuf and do nothing */ + pbuf_free(p); + ret_err = ERR_OK; + } + return ret_err; +} + +int32_t tcp_eth_connect( tcp_cli_t *client, ip_addr_t *addr, uint16_t port ) +{ + TickType_t tick; + struct tcp_pcb *tpcb; + + tpcb = tcp_new(); + + err_t err; + + if (!tpcb){ + return ERR_MEM; + } + + client->connected = 0; + client->pcb = NULL; + + tcp_arg( tpcb, client ); + /* connect to destination address/port */ + err = tcp_connect(tpcb, addr, port, tcp_eth_connected); + printf( "tcp_connect ret:%s\n", lwip_strerr( err ) ); + + tick = xTaskGetTickCount(); + while( client->connected==0 && xTaskGetTickCount() - tick < 5*1000 ){ + } + if( client->connected > 0 ){ + client->pcb = tpcb; + tcp_arg(tpcb, client); + tcp_sent( tpcb, tcp_eth_sent ); + tcp_recv( tpcb, tcp_eth_recv ); + tcp_poll( tpcb, tcp_eth_poll, 2 ); + printf( "tcp connect success, tpcb:%p, state:%d-%s\n", tpcb, tpcb->state, tcp_debug_state_str(tpcb->state) ); + return ERR_OK; + }else{ + tcp_close( tpcb ); + printf( "tcp connect fail, close tpcb:%p\n", tpcb ); + return ERR_CONN; + } +} + +void user_client_lock( tcp_cli_t *client ) +{ + osMutexAcquire( client->mutex,osWaitForever ); +} + +void user_client_unlock( tcp_cli_t *client ) +{ + osMutexRelease( client->mutex ); +} + +void user_eth_task( void* unused ) +{ + TickType_t last_ticks; + + ip_addr_t addr; + uint16_t port; + err_t err; + + memset( &g_client, 0, sizeof( g_client ) ); + g_client.p_netif = &gnetif; + g_client.mutex = osMutexNew(&clientMutex_attributes); + /* init code for LWIP */ + MX_LWIP_Init(); + osDelay(2000); + + uint32_t tcp_err_cnt = 0; + + while(1){ + + //1. wait linkup + if( !netif_is_link_up(g_client.p_netif) || !g_client.p_netif->ip_addr.addr){ + g_dev.eth_connected = 0; + osDelay( 1000 ); + continue; + } + g_dev.eth_connected = 1; + //2. get host and port + port = atoi(g_dev.cfg.net.tcp_port); + if( !strlen(g_dev.cfg.net.tcp_server) || port ==0 || port > 0xffff ){ + //无效服务器信息 + osDelay( 1000 ); + continue; + } + + //3. dns + addr.addr = 0; + if( dns_gethostbyname( g_dev.cfg.net.tcp_server, &addr, DNS_found_cb, NULL) || !addr.addr ){ + osDelay( 1000 ); + printf( "dns %s -- fail!\n", g_dev.cfg.net.tcp_server ); + continue; + } + printf( "-- dns %s -- %d.%d.%d.%d\n", g_dev.cfg.net.tcp_server, addr.addr&0xff, (addr.addr>>8)&0xff, (addr.addr>>16)&0xff, (addr.addr>>24)&0xff ); + + //4.连接 + if( tcp_eth_connect( &g_client, &addr, port ) != ERR_OK ){ + osDelay( 5000 ); + continue; + } + + g_dev.eth_connected = 2; + //5.定时上报 + last_ticks = xTaskGetTickCount(); + while( g_client.connected > 0 ){ + //连接状态,定时上报数据 vTaskDelayUntil(&last_ticks, 1000 ); - osDelay(1000); //定时上报 - if( eth_devinfo_report() < 0 ){ - //reconnect -// connected = 0; -// g_dev.connected = 1; + if( !netif_is_link_up(g_client.p_netif)){ + //链路中断 + //tcp_eth_connection_close( &g_client ); + //tcp_abort( g_client.pcb ); + //g_client.pcb = NULL; + g_client.connection_close = 1; + break; + } + err = eth_devinfo_report( &g_client); + if( err == ERR_OK ){ + tcp_err_cnt = 0; + }else if( err == ERR_MEM ){ + if( tcp_err_cnt++ > 3 ){ + tcp_err_cnt = 0; + //tcp_eth_connection_close( &g_client ); + printf( "tcp err , close ...\n" ); + g_client.connection_close = 1; + break; + } + }else{ + tcp_err_cnt = 0; +// tcp_eth_connection_close( &g_client ); + g_client.connection_close = 1; break; } } - //} + g_dev.eth_connected = 0; + osDelay(1000); + } } //static osThreadId_t ethTaskHandle; diff --git a/user/user_report.c b/user/user_report.c new file mode 100644 index 0000000..c9e21d2 --- /dev/null +++ b/user/user_report.c @@ -0,0 +1,47 @@ +#include "user.h" + +static char mac_str_buf[20]; //"00:00:00:00:00:00"; +static char *g_mac_str; + +static const char *fmt = "{\n\ + \"cmd\":\"rt\",\n\ + \"cid\":%d,\n\ + \"softVersion\":\"V%d.%d\",\n\ + \"lock\":\"%d\",\n\ + \"mac\":\"%s\",\n\ + \"val\":\n\ + {\n\ + \"c\":\"%0.1f\",\n\ + \"s\":\"%d\",\n\ + \"g\":\"%0.1f\"\n\ + }\n\ +}"; + +static void mac_str_init( void ) +{ + uint8_t mac[6]; + + device_get_mac(mac); + sprintf( mac_str_buf, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5] ); + g_mac_str = mac_str_buf; +} + +char * devinfo_report_str( void ) +{ + static uint32_t cid = 0; + static char tx_buf[200]; + + float c,g; + uint32_t s; + + if( !g_mac_str ){ + mac_str_init(); + } + + c = g_dev.welding_current; + g = g_dev.flowmeter.flow_speed; + s = g_dev.welding_start; + sprintf( (char*)tx_buf, fmt, cid++, APP_VER_MAJOR, APP_VER_MINOR, g_dev.remote_locked, g_mac_str, c, s, g ); + + return tx_buf; +} diff --git a/user/user_ui.c b/user/user_ui.c index a85facd..8bfcfd9 100644 --- a/user/user_ui.c +++ b/user/user_ui.c @@ -47,7 +47,8 @@ static uint8_t lcd_uart_rx_count = 0; static lcd_cmd_resp_t lcd_resp; static int32_t lcd_read( uint16_t addr, uint8_t reg_nr ); -static void lcd_write_u16( uint16_t addr, uint16_t value ); +//static void lcd_write_u16( uint16_t addr, uint16_t value ); +static void lcd_write_s16( uint16_t addr, int16_t value ); void lcd_uart_rx_start( void ) { @@ -119,15 +120,53 @@ void ui_handle_lcd_pkt( void ) net_cfg.tcp_port[i] = *p; p++; } +#if 0 if( strlen( net_cfg.wifi_ssid)&& strlen( net_cfg.wifi_passwd ) && strlen( net_cfg.tcp_server ) && strlen( net_cfg.tcp_port ) ){ if( memcmp( &g_dev.cfg.net, &net_cfg, sizeof( net_cfg ) ) ){ memcpy( &g_dev.cfg.net, &net_cfg, sizeof( net_cfg ) ); dev_cfg_store( &g_dev.cfg ); - g_dev.connected = 0; + g_dev.wifi_connected = 0; } } +#else + if( !strlen( net_cfg.tcp_server)|| !strlen( net_cfg.tcp_port ) || atoi( net_cfg.tcp_port ) < 0 || atoi( net_cfg.tcp_port )>0xffff){ + //无效配置,忽略掉 + strcpy( net_cfg.tcp_server, g_dev.cfg.net.tcp_server ); + strcpy( net_cfg.tcp_port, g_dev.cfg.net.tcp_port ); + } + + if( strcmp( g_dev.cfg.net.tcp_server, net_cfg.tcp_server ) + || strcmp( g_dev.cfg.net.tcp_port, net_cfg.tcp_port ) ){ + //平台改变 + printf( "remote srv cfg [%s:%s -> %s:%s], connected -> 0\n", + g_dev.cfg.net.tcp_server, g_dev.cfg.net.tcp_port, net_cfg.tcp_server, net_cfg.tcp_port ); + strcpy( g_dev.cfg.net.tcp_server, net_cfg.tcp_server ); + strcpy( g_dev.cfg.net.tcp_port, net_cfg.tcp_port ); + if( strcmp( g_dev.cfg.net.wifi_ssid, net_cfg.wifi_ssid ) + || strcmp( g_dev.cfg.net.wifi_passwd, net_cfg.wifi_passwd ) ){ + printf( "+++wifi cfg [%s:%s -> %s:%s]\n", + g_dev.cfg.net.wifi_ssid, g_dev.cfg.net.wifi_passwd, net_cfg.wifi_ssid, net_cfg.wifi_passwd ); + strcpy( g_dev.cfg.net.wifi_ssid, net_cfg.wifi_ssid ); + strcpy( g_dev.cfg.net.wifi_passwd, net_cfg.wifi_passwd ); + } + + dev_cfg_store( &g_dev.cfg ); + g_dev.wifi_connected = 0; + g_dev.eth_connected = 0; + }else if( strcmp( g_dev.cfg.net.wifi_ssid, net_cfg.wifi_ssid ) + || strcmp( g_dev.cfg.net.wifi_passwd, net_cfg.wifi_passwd ) ){ + //wifi配置改变 + printf( "wifi cfg [%s:%s -> %s:%s]\n", + g_dev.cfg.net.wifi_ssid, g_dev.cfg.net.wifi_passwd, net_cfg.wifi_ssid, net_cfg.wifi_passwd ); + strcpy( g_dev.cfg.net.wifi_ssid, net_cfg.wifi_ssid ); + strcpy( g_dev.cfg.net.wifi_passwd, net_cfg.wifi_passwd ); + dev_cfg_store( &g_dev.cfg ); + g_dev.wifi_connected = 0; + } + +#endif break; } @@ -366,7 +405,7 @@ void user_ui_task( void* unused ) reg_values[2] = g_dev.pressure; reg_values[3] = g_dev.flowmeter.flow_total; lcd_write( lcd_welding_current_addr, reg_values, 4 ); - reg_values[0] = g_dev.connected; + reg_values[0] = g_dev.wifi_connected; lcd_write( lcd_net_connected_addr, reg_values, 1 ); //reg_values[4] = g_dev.cfg.welding_method+1; //reg_values[5] = g_dev.cfg.save_gas_factor; diff --git a/user/user_valve_ctrl.c b/user/user_valve_ctrl.c index 4a3fc59..dd9a7b0 100644 --- a/user/user_valve_ctrl.c +++ b/user/user_valve_ctrl.c @@ -4,7 +4,7 @@ #include "weld_alg.h" //温度检测功能开关 -#define VALVE_TEMP_CHECK 0 +#define VALVE_TEMP_CHECK 1 //------------------------------------------------ @@ -400,7 +400,7 @@ void user_valve_ctrl_task( void* unused ) // float target_flowspeed = 0; uint32_t tem_state_last = 0; -// uint32_t now; + uint32_t now; HAL_DAC_SetValue( &hdac1, DAC_CHANNEL_2, DAC_ALIGN_12B_R, 0 ); HAL_DAC_Start(&hdac1, DAC_CHANNEL_2); @@ -448,17 +448,17 @@ void user_valve_ctrl_task( void* unused ) while(1){ //200毫秒扫描一次, 考虑pid的流量计反馈,让扫描周期大于流量计的采集周期, vTaskDelayUntil(&last_tick, 100 ); -// now = xTaskGetTickCount(); welding_current = g_dev.welding_current; if( welding_current < 0 ) welding_current = -welding_current; #if VALVE_TEMP_CHECK + now = xTaskGetTickCount(); //电磁阀温度检查 if( tem_err_state == 0 ){ //正常状态, 检查温度 - if( g_dev.valve_temperature < 0.55f ){ + if( g_dev.valve_temperature < 0.50f ){ if( tem_alarm_start_tick == 0 ) tem_alarm_start_tick = now; else if( now-tem_alarm_start_tick > 1000 ){ @@ -471,11 +471,11 @@ void user_valve_ctrl_task( void* unused ) tem_alarm_start_tick = 0; } }else if( tem_err_state == 1 ){ - if( g_dev.valve_temperature > 0.55f ){ + if( g_dev.valve_temperature > 0.50f ){ //温度恢复正常,等待5min恢复 if( tem_restore_tick==0 ) tem_restore_tick = now; - else if( now - tem_restore_tick > 5*60*1000/30 ){ + else if( now - tem_restore_tick > 5*60*1000 ){ tem_err_state = 0; tem_restore_tick = 0; tem_alarm_start_tick = 0; @@ -488,6 +488,11 @@ void user_valve_ctrl_task( void* unused ) printf( "[%d] valve tem state:%d -> %d\n", now, tem_state_last, tem_err_state); tem_state_last = tem_err_state; } + if( tem_err_state ){ + g_dev.state |= TEMP_ERR; + }else{ + g_dev.state &= ~(TEMP_ERR); + } #endif if( !bypass && (g_dev.ui_bypass || tem_err_state ) ){ //屏上点击旁路或温度故障,开启旁路 diff --git a/user/user_wifi.c b/user/user_wifi.c index 893cab7..fa1097b 100644 --- a/user/user_wifi.c +++ b/user/user_wifi.c @@ -185,42 +185,14 @@ int32_t tcp_server_connect( const char * tcp_server, const char * port ) return 0; } -static char mac_str[20]; //"00:00:00:00:00:00"; -static const char *fmt = "{\n\ - \"cmd\":\"rt\",\n\ - \"cid\":%d,\n\ - \"mac\":\"%s\",\n\ - \"val\":\n\ - {\n\ - \"c\":\"%0.1f\",\n\ - \"s\":\"%d\",\n\ - \"g\":\"%0.1f\",\n\ - }\n\ -}"; - -void mac_str_init( void ) +extern char * devinfo_report_str( void ); +int32_t wifi_devinfo_report( void ) { - uint32_t uid0; - uint32_t uid1; + char * rpt_data; - uint8_t mac[6]; - device_get_mac(mac); - - sprintf( mac_str, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5] ); -} + rpt_data = devinfo_report_str(); + wifi_tcp_tx( (uint8_t *)rpt_data, strlen((char *)rpt_data) ); -int32_t devinfo_report( void ) -{ - static uint32_t cid = 0; - static uint8_t tx_buf[200]; - float c,g; - uint32_t s; - - c = g_dev.welding_current; - g = g_dev.flowmeter.flow_speed; - s = g_dev.welding_start; - sprintf( (char*)tx_buf, fmt, cid++, mac_str, c, s, g ); - wifi_tcp_tx( tx_buf, strlen((char *)tx_buf) ); return 0; } @@ -254,8 +226,8 @@ int32_t esp_read_imsi( void ) static uint8_t payload[TCP_MAX_PAYLOAD_SIZE] = {0}; static void link_down( void *arg, char *rspinfo, int rsplen ) { - if( g_dev.connected == 2 ) - g_dev.connected = 1; + if( g_dev.wifi_connected == 2 ) + g_dev.wifi_connected = 1; } static void tcp_rx_data_handle(void *arg, char *rspinfo, int rsplen) @@ -331,17 +303,15 @@ void user_wifi_task( void* unused ) char *ssid; char *passwd; -// int32_t connected; + TickType_t last_ticks; -// connected = 0; - g_dev.connected = 0; + g_dev.wifi_connected = 0; MX_UART5_Init(); wifi_uart_rx_start(); at_parser_init(); - mac_str_init(); //1. init while( wifi_init() ){ //wifi init fail @@ -368,12 +338,12 @@ void user_wifi_task( void* unused ) osDelay( 1000 ); continue; } - g_dev.connected = 1; - - while( g_dev.connected == 1 ){ + g_dev.wifi_connected = 1; + + while( g_dev.wifi_connected == 1 ){ //tcp connect if( tcp_server_connect( g_dev.cfg.net.tcp_server, g_dev.cfg.net.tcp_port ) == 0 ){ - g_dev.connected = 2; + g_dev.wifi_connected = 2; }else{ osDelay( 1000 ); continue; @@ -382,14 +352,18 @@ void user_wifi_task( void* unused ) last_ticks = xTaskGetTickCount(); - while( g_dev.connected == 2 ){ - vTaskDelayUntil(&last_ticks, 1000 ); - osDelay(1000); + while( g_dev.wifi_connected == 2 ){ + vTaskDelayUntil(&last_ticks, 1000 ); + + if( g_dev.eth_connected == 2 ){ + //如果有线已经连接,wifi不上传数据 + continue; + } + //定时上报 - if( devinfo_report() < 0 ){ + if( wifi_devinfo_report() < 0 ){ //reconnect -// connected = 0; - g_dev.connected = 1; + g_dev.wifi_connected = 1; break; } } diff --git a/welding_controller_v2.ioc b/welding_controller_v2.ioc index 0f74f53..0450fc1 100644 --- a/welding_controller_v2.ioc +++ b/welding_controller_v2.ioc @@ -148,10 +148,16 @@ I2C2.Speed=100 I2C2.Timing=0x2040C4DF KeepUserPlacement=false LWIP.BSP.number=1 -LWIP.IPParameters=LWIP_DNS,MEM_ALIGNMENT,LWIP_RAM_HEAP_POINTER +LWIP.IPParameters=LWIP_DNS,MEM_ALIGNMENT,LWIP_RAM_HEAP_POINTER,TCP_DEBUG,NETIF_DEBUG,PBUF_DEBUG,MEM_DEBUG,MEMP_DEBUG,TCP_INPUT_DEBUG LWIP.LWIP_DNS=1 LWIP.LWIP_RAM_HEAP_POINTER=0x30004000 +LWIP.MEMP_DEBUG=LWIP_DBG_ON LWIP.MEM_ALIGNMENT=4 +LWIP.MEM_DEBUG=LWIP_DBG_ON +LWIP.NETIF_DEBUG=LWIP_DBG_ON +LWIP.PBUF_DEBUG=LWIP_DBG_ON +LWIP.TCP_DEBUG=LWIP_DBG_ON +LWIP.TCP_INPUT_DEBUG=LWIP_DBG_ON LWIP.Version=v2.1.2_Cube LWIP0.BSP.STBoard=false LWIP0.BSP.api=BSP_COMPONENT_DRIVER -- 2.34.1