cancel
Showing results for 
Search instead for 
Did you mean: 

Nucleo STM32F207ZG - Problem with LWIP integration via STM23CubeIDE

Aplou.1
Associate III

Hello everyone!

I want to use a TCP/IP stack with my Nucleo STM32F207ZG over FreeRTOS, so I went ahead and created an stm32 project with STM32CubeIDE and initialized the project with cubeMX. (see linked file).

So far so good! My problem begins when I try to read incoming packets, I use the function lwip_read() (alose tried read()) to read from the input buffer sequentially. It works a couple of times then the thread crashes. see code snipet below :

Code

So I have a single thread, that I pasted below. In it I use an MQTT library. All the function I use from this library only write to CHAR buffers and do not interact with the TCP/IP Stack.

The code here is simple and it's purpose is to demonstrate read and write capabilities to the network. The code crashes at line 70. I don't know why. (I know that it's weird to have a couple of read like that, but it is only to better show the symptoms of the problem).

void StartDefaultTask(void *argument)
{
  /* init code for LWIP */
  MX_LWIP_Init();
  /* USER CODE BEGIN StartDefaultTask */
 
	// Init variables and constants
	MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
	char buf[200];
	int buflen = sizeof(buf);
	MQTTString topicString = MQTTString_initializer;
	char* payload = "76";
	int payloadlen = strlen(payload);
	int len = 0;
	int port = 1883;
	int sock = 0;
	struct sockaddr_in serv_addr;
	char *ip = "192.168.0.139";
	uint8_t toSend[50] = {0};
	int sendStatus = 0;
	int receiveStatus = 0;
	int errorbuffer = 0;
	int psi = 0;
 
	/* Infinite loop */
	for(;;)
	{
		// Create socket
		if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
		{
			printf("\n Socket creation error \n");
		}
 
		// Convert IPv4 and IPv6 addresses from text to binary form
		serv_addr.sin_family = AF_INET;
		serv_addr.sin_port = htons(port);
		if(inet_pton(AF_INET, ip, &serv_addr.sin_addr)<=0)
		{
			printf("\nInvalid address/ Address not supported \n");
		}
 
		// Connect to server
		if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
		{
			printf("\nConnection Failed \n");
		}
 
        // Create MQTT Message
		data.clientID.cstring = "1";
		data.keepAliveInterval = 10000;
		data.cleansession = 1;
		data.username.cstring = "testuser";
		data.password.cstring = "testpassword";
		data.MQTTVersion = 4;
		len = MQTTSerialize_connect((unsigned char *)buf, buflen, &data);
 
        // Send mqtt message
		sendStatus = send(sock, (char*)buf, len, 0);
		if( sendStatus < 0){
			errorbuffer = errno;
			snprintf((char *)toSend,sizeof(toSend),"\r\nReturn status of send : %d, %d\r\n", sendStatus, errorbuffer);
			HAL_UART_Transmit(&huart3,toSend,strlen((char *)toSend),10);
		}
 
        // Receive CONNACK
		receiveStatus = lwip_read(sock,(char*)buf, 1);
		receiveStatus = lwip_read(sock,(char*)buf + 1, 1);
		receiveStatus = lwip_read(sock,(char*)buf + 2, 1);
		receiveStatus = lwip_read(sock,(char*)buf + 3, 1);
        //<!><!><!> CRASHES HERE <!><!><!>
		receiveStatus = lwip_read(sock,(char*)buf + 4, 1);
		receiveStatus = lwip_read(sock,(char*)buf + 5, 1);
		receiveStatus = lwip_read(sock,(char*)buf + 6, 1);
		receiveStatus = lwip_read(sock,(char*)buf + 7, 1);
 
 
		if (MQTTPacket_read((unsigned char*)buf, buflen, lwip_read) == CONNACK)
			{
				unsigned char sessionPresent, connack_rc;
				if (MQTTDeserialize_connack(&sessionPresent, &connack_rc, (unsigned char *) buf, buflen) != 1 || connack_rc != 0)
				{
					printf("Unable to connect, return code %d\n", connack_rc);
				}
				// print connection result
				snprintf((char *)toSend,sizeof(toSend),"\r\nConnexion Success with code: %s, %s\r\n", sessionPresent, connack_rc);
				HAL_UART_Transmit(&huart3,toSend,strlen((char *)toSend),10);
			}
 
		close(sock);
 
		osDelay(1000);
	}

What am I doing wrong ? Any help is welcome !

Alexandre,

Thank you and have a great day!

0 REPLIES 0