This post was last edited by supermiao123 on 2019-3-1 16:37 Today I bring you the evaluation of serial port and network port data interaction under H7's LWIP. Since H7 has a significant performance improvement compared to previous series, the architecture has become more complex. STM32cube has greatly improved the speed of programming, and today's evaluation is no exception. As shown in the figure below, the relevant settings for ETH are generated. It should be noted that the gray font below means that ETH can only work when the RAM is 0x24000000.
So I looked at the manual and saw that 0x24000000 is AXI SRAM, and the default 0x20000000 is DTCM.
Looking at the figure below, the architectural positions of the two RAMs are different.
Another thing to note is that the ETH IO speed is LOW, so it is best to change it to VERY HIGH.
Then configure LWIP. Fixed IPs often collide with each other, or when changing switches, you may encounter 192.168.0.X or 192.168.1.x. Simply use the automatic IP acquisition function, and configure TFT and the serial port to output the IP address when obtaining the IP.
Below is the serial port configuration.
Then just generate it. Then pay attention to the previous prompts, the compiler has changed two places.
Next is the code to implement the interaction.
- USART3->CR1 |= USART_CR1_TE |USART_CR1_RE |USART_CR1_RXNEIE; USART3->CR1 |= USART_CR1_UE; HAL_GPIO_WritePin(GPIOC, TFT_BL_Pin, GPIO_PIN_SET); SPI1->CR1 |=SPI_CR1_SPE ; SPI1->CR1 |= SPI_CR1_CSTART; lcd_initial(); dsp_single_colour(WHITE); tcp_server_init();
复制代码The above is the initialization of the serial port and TFT. The serial port enables the receive interrupt, enables the SPI that controls the TFT, and initializes the TFT. Finally, TCP_SERVER is initialized.
- void tcp_server_init(void) { err_t err; tcp_server_pcb = tcp_new(); if(tcp_server_pcb !=NULL) { err = tcp_bind(tcp_server_pcb,IP_ADDR_ANY,7); if(err == ERR_OK) { tcppcbconn = tcp_listen(tcp_server_pcb); tcp_accept(tcppcbconn,tcp_server_accept); } else { memp_free(MEMP_TCP_PCB, tcp_server_pcb); } } }
复制代码After TCP receives it, the serial port sends it out.
- static err_t tcp_server_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p,err_t err) { HAL_UART_Transmit(&huart3,p->payload,p->len,100); pbuf_free(p); return ERR_OK; }
复制代码The serial port receives data and detects 0xA carriage return and then sends it
- void USART3_IRQHandler(void) { /* USER CODE BEGIN USART3_IRQn 0 */ uart3_data[uart3_rx_cnt]=USART3->RDR; if(uart3_data[uart3_rx_cnt]==0xA) { uart3_data_valid=1; //uart3_rx_cnt=0; } else uart3_rx_cnt++; /* USER CODE END USART3_IRQn 0 */ HAL_UART_IRQHandler(&huart3); /* USER CODE BEGIN USART3_IRQn 1 */ /* USER CODE END USART3_IRQn 1 */ }
复制代码Add the operation of displaying the IP to the TFT and sending it to the serial port in the LWIP handler. And send the code to TCP after receiving the valid command from the serial port.
- void MX_LWIP_Process(void) { /* USER CODE BEGIN 4_1 */ unsigned char ip[4]={0}; char ipstr[16]={0}; /* USER CODE END 4_1 */ ethernetif_input(&gnetif); /* USER CODE BEGIN 4_2 */ /* USER CODE END 4_2 */ /* Handle timeouts */ sys_check_timeouts(); /* USER CODE BEGIN 4_3 */ if(gnetif.ip_addr.addr!=0&&!ipget) { ip[3]=(uint8_t)(gnetif.ip_addr.addr>>24); ip[2]=(uint8_t)(gnetif.ip_addr.addr>>16); ip[1]=(uint8_t)(gnetif.ip_addr.addr>>8); ip[0]=(uint8_t)(gnetif.ip_addr.addr); printf("ip_addr : %d.%d.%d.%d \r\n",ip[0],ip[1],ip[2],ip[3]); TFTStrDisplay_lite(0,0,"IP GET",6); sprintf(ipstr,"%d.%d.%d.%d",ip[0],ip[1],ip[2],ip[3]); TFTStrDisplay_lite(1,0,ipstr, 15); ipget=1; } uart_send_tcp_recv(uart3_data_valid,uart3_data,uart3_rx_cnt); if(uart3_data_valid) { memset(uart3_data,0,100); uart3_data_valid=0; uart3_rx_cnt=0; } /* USER CODE END 4_3 */ } void uart_send_tcp_recv(unsigned char flag,unsigned char *data,unsigned char cnt) { if(flag ) { tcp_write(tcp_server_pcb,data,cnt,1); tcp_output(tcp_server_pcb); } }
复制代码After running, TFT displays the IP address.
The serial port also sees the IP address. According to the obtained IP, the running effect is as shown below. This simply completes the network port
My level is limited, welcome to exchange and correct, thank you for your browsing!
This content is provided by EEWORLD forum user [ size=3]supermiao123Original, if you want to reprint or use it for commercial purposes, you must obtain the author's consent and indicate the source