2020-09-25 08:03 AM
Hello,
Today and for the past days I am struggling trying to setup a TCP/IP server on the STM32F767Zi-Nucleo board..
The IP address is set to :
192.168.0.10
The mast address is set to :
255.255.255.0
The gateway address is set to :
192.168.0.1
My local machine show an IP address : 192.168.1.95
From my PC I am able to ping the board (w/ delay ~.025ms)
The main function is defined as :
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
osKernelInitialize();
defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);
osKernelStart();
while (1)
{
}
}
The task is defined as :
void StartDefaultTask(void *argument)
{
MX_LWIP_Init();
printf("Entered task\n");
tcp_echoserver_init();
for(;;)
{
}
}
And the server initialization :
void tcp_echoserver_init(void) {
local = netconn_new(NETCONN_TCP);
if (local == NULL) {
printf("Setup local connection failed\n");
return;
}
printf("Local connection initialized\n");
err_t err;
err = netconn_bind(local, IP_ADDR_ANY, 7); // open port 7 || WILL BE CHANGED
struct netbuf *buf = netbuf_new();
void *data;
uint16_t len;
if (err == ERR_OK) {
printf("Connection binded to port 7\n");
err = netconn_listen(local); // set listening mode
if (err != ERR_OK) printf("listen issue\n");
printf("Port 7 is in listening mode\n");
printf("Socket : %d\n", local->socket); // -1 is a problem
while (1) {
printf("Accepting..\n");
err = netconn_accept(local, &remote);
if (err == ERR_OK) {
printf("Accepted!\n");
while ((err = netconn_recv(remote, &buf)) == ERR_OK) {
do {
netbuf_data(buf, &data, &len);
netconn_write(remote, data, len, NETCONN_COPY);
} while (netbuf_next(buf) >= 0);
netbuf_delete(buf);
}
netconn_close(remote);
netconn_delete(remote);
}
}
} else {
printf("Bind issue\n");
netconn_delete(remote);
}
}
In my ioc file the clock is configured :
I didn’t showed the rest since only this part is manually configurable.
LWIP Config :
Version 2.12 and DHCP disabled
The rest is at default configuration
I also turned on RTOS :
API : CMSIS v2
FreeRTOS version 10.2.1
CMSIS-RTOS 2.0
The ETH port is enabled in RMII with global interrupt (w/o wake-up interrupt EXTI line 19)
RCC is Crystal/Ceramic Resonator HSE, rest of configuration unchanged
And SYS :
Debug to Serial Wire with timebase source TIM1 (as RTOS wont use Systick)
When launching debug session I got the following output in ITM console I got the following output :
Entered task
Local connection initialized
Connection binded to port 7
Port 7 is in listening mode
Socket : -1
Accepting..
For output to work. change write function from
__attribute__((weak))
int _write(int file, char *ptr, int len)
{
int DataIdx;
for (DataIdx = 0; DataIdx < len; DataIdx++)
{
__io_putchar(*ptr++);
}
return len;
}
to
//__attribute__((weak))
int _write(int file, char *ptr, int len)
{
/* Implement your write code here, this is used by puts and printf for example */
for(int i = 0 ; i < len ; i++) ITM_SendChar((*ptr++));
return len;
// int DataIdx;
//
// for (DataIdx = 0; DataIdx < len; DataIdx++)
// {
// __io_putchar(*ptr++);
// }
// return len;
}
From there I simply though : “Hey let’s use netcat or whatever and just connect.�?
Or setting a C project that going to communicate with the board (as it is my final goal)
client.c
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#define MAX 80
#define PORT 8080
#define SA struct sockaddr
void func(int sockfd)
{
char buff[MAX];
int n;
for (;;) {
bzero(buff, sizeof(buff));
printf("Enter the string : ");
n = 0;
while ((buff[n++] = getchar()) != '\n')
;
write(sockfd, buff, sizeof(buff));
bzero(buff, sizeof(buff));
read(sockfd, buff, sizeof(buff));
printf("From Server : %s", buff);
if ((strncmp(buff, "exit", 4)) == 0) {
printf("Client Exit...\n");
break;
}
}
}
int main()
{
int sockfd, connfd;
struct sockaddr_in servaddr, cli;
// socket create and varification
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
printf("socket creation failed...\n");
exit(0);
}
else
printf("Socket successfully created..\n");
bzero(&servaddr, sizeof(servaddr));
// assign IP, PORT
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
servaddr.sin_port = htons(PORT);
// connect the client socket to server socket
if (connect(sockfd, (SA*)&servaddr, sizeof(servaddr)) != 0) {
printf("connection with the server failed...\n");
exit(0);
}
else
printf("connected to the server..\n");
// function for chat
func(sockfd);
// close the socket
close(sockfd);
}
server.c
#include <stdio.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#define MAX 80
#define PORT 8080
#define SA struct sockaddr
// Function designed for chat between client and server.
void func(int sockfd)
{
char buff[MAX];
int n;
// infinite loop for chat
for (;;) {
bzero(buff, MAX);
// read the message from client and copy it in buffer
read(sockfd, buff, sizeof(buff));
// print buffer which contains the client contents
printf("From client: %s\t To client : ", buff);
bzero(buff, MAX);
n = 0;
// copy server message in the buffer
while ((buff[n++] = getchar()) != '\n')
;
// and send that buffer to client
write(sockfd, buff, sizeof(buff));
// if msg contains "Exit" then server exit and chat ended.
if (strncmp("exit", buff, 4) == 0) {
printf("Server Exit...\n");
break;
}
}
}
// Driver function
int main()
{
int sockfd, connfd, len;
struct sockaddr_in servaddr, cli;
// socket create and verification
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
printf("socket creation failed...\n");
exit(0);
}
else
printf("Socket successfully created..\n");
bzero(&servaddr, sizeof(servaddr));
// assign IP, PORT
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(PORT);
// Binding newly created socket to given IP and verification
if ((bind(sockfd, (SA*)&servaddr, sizeof(servaddr))) != 0) {
printf("socket bind failed...\n");
exit(0);
}
else
printf("Socket successfully binded..\n");
// Now server is ready to listen and verification
if ((listen(sockfd, 5)) != 0) {
printf("Listen failed...\n");
exit(0);
}
else
printf("Server listening..\n");
len = sizeof(cli);
// Accept the data packet from client and verification
connfd = accept(sockfd, (SA*)&cli, &len);
if (connfd < 0) {
printf("server acccept failed...\n");
exit(0);
}
else
printf("server acccept the client...\n");
// Function for chatting between client and server
func(connfd);
// After chatting close the socket
close(sockfd);
}
gcc server.c -o server && gcc client.c -o client
When changing client server address fomr 127.0.01 to 192.168.0.10 and port from 8080 to 7
So… PC-wise it seems to work. But the connection is never accepted on the board.
I also tried to use the project from the git :
LwIP_HTTP_Server_Netconn_RTOS under STM32CubeF7-master/Projects/STM32F767ZI-Nucleo/Applications/LwIP/LwIP_HTTP_Server_Netconn_RTOS
removed the line #define USE_LCD and #define USE_DHCP
changing the IP by the one I set on my board. Still have no result. Trying to access the webpage at 192.168.0.10:80 shows “Unable to connect�? from e.g. Firefox
I also tried to use MBED /w their IDE, still get no results.
I've only been working with STM board since like... 3-4 month on only 2projects, the one only involved USART & i2C communication (way easier). That's for my background w/ MCUs :)
Thanks a lot !
Nicolas
2020-09-26 03:40 PM
Is this a new from scratch project created in Cube, or adaptation of some example provided by ST in the package for this Nucleo board?
If the former - please get a ready example working on your board first.
-- pa
2020-09-28 12:42 AM
Hello Pavel A.
Yes, this is a project created using CubeMX IDE and I extracted the functions used in the example to try it.
As the example only use functions from the LwIP library, I reused the same code. My understanding of it is that, I missed something during the configuration of the project.
On the other hand, only using the project within the archive ( STM32CubeF7-master ) and launching it as is, doesn't work either. (as specified in the end of my post, I remove the USE_DHCP and USE_LCD define from this project. Since I do not use them.
Also trying a simple program using Mbed Studio (on the same board)
#include "mbed.h"
#include "EthernetInterface.h"
// Network interface
EthernetInterface net;
// Socket demo
int main()
{
// Bring up the ethernet interface
printf("Ethernet socket example\n");
net.connect();
// Show the network address
SocketAddress a;
net.get_ip_address(&a);
printf("IP address: %s\n", a.get_ip_address() ? a.get_ip_address() : "None");
// Open a socket on the network interface, and create a TCP connection to mbed.org
TCPSocket socket;
socket.open(&net);
net.gethostbyname("ifconfig.io", &a);
a.set_port(80);
socket.connect(a);
// Send a simple http request
char sbuffer[] = "GET / HTTP/1.1\r\nHost: ifconfig.io\r\n\r\n";
int scount = socket.send(sbuffer, sizeof sbuffer);
printf("sent %d [%.*s]\n", scount, strstr(sbuffer, "\r\n") - sbuffer, sbuffer);
// Recieve a simple http response and print out the response line
char rbuffer[64];
int rcount = socket.recv(rbuffer, sizeof rbuffer);
printf("recv %d [%.*s]\n", rcount, strstr(rbuffer, "\r\n") - rbuffer, rbuffer);
// Close the socket to return its memory and bring down the network interface
socket.close();
// Bring down the ethernet interface
net.disconnect();
printf("Done\n");
}
Return the following result.
IP address: None
sent -3004 [GET / HTTP/1.1]
-3304 == NSAPI_ERROR_NO_CONNECTION
Meaning I'm not connected to a network.
Edit : The thing is I cannot manage to have an example working.
2021-01-07 01:12 PM
Dear all,
I have the same problem, I am not able to manage getting the "LwIP_HTTP_Server_Netconn_RTOS" example run.
I am using STM32CubeIDE (version 1.5.1), imported the example, but when I try to build the application, errors occur with files missing (e.g. httpd_opts.h which should be located in directory "lwip/apps"). There problem is, that the missing files are not part of the imported project (they are not there on disk).
Is there anybody who got this example running on the STM32F767Zi-Nucleo board using the STM32CubeIDE?
Kind regards.