cancel
Showing results for 
Search instead for 
Did you mean: 

Azure RTOS: function "tx_event_flags_get" does not react if it waits too long

Moe
Associate II

 

Hello everyone,

on my Evalboard (STM32H750B-DK) I use Azure RTOS, NetX Duo and the MQTT stack. I run the RTOS with two threads. The first thread (low priority) is the heartbeat thread. In this thread, only one LED is blinking. In the second thread (high priority) a MQTT communication is running. Sending and receiving works also very well.

About the problem: I have the problem that the function "tx_event_flags_get()" does not always react to the event. It almost never reacts to the event if the event occurs after a certain time ( approx. longer than 20 seconds). The event that is waited for is an MQTT message.

If the µC reaches the above mentioned function and then the event occurs immediately after 1-2 seconds, then everything works very well.

How can it be that when the µC spends a longer time in the "tx_event_flags_get" function, it stops responding to the event even when the event actually arrives? 

The relevant code looks like the following. I have added the full code as a text file to the attachment. If you need any additional information beyond that, please let me know:

 

 

/* Includes ------------------------------------------------------------------*/
#include "app_netxduo.h"
#include "nxd_mqtt_client.h"
#include "main.h"
#include "string.h"
#include "stdio.h"

/* Private define ------------------------------------------------------------*/

#define LOCAL_SERVER_ADDRESS   (IP_ADDRESS(192,168,0,14))
#define MQTT_CLIENT_STACK_SIZE 4096
#define CLIENT_ID_STRING       "mytestclient"
#define STRLEN(p)              (sizeof(p) - 1)
#define SUB_TOPIC_NAME         "Temp/Sensor/Value/f"

#define MQTT_THREAD_PRIORTY    2
#define MQTT_KEEP_ALIVE_TIMER  300
#define QOS0                   0
#define QOS1                   1

#define DEMO_MESSAGE_EVENT     1
#define DEMO_ALL_EVENTS        3


/* Private variables ---------------------------------------------------------*/
TX_THREAD             NxAppThread;
TX_THREAD             NxHbThread;
NX_PACKET_POOL        NxAppPool;
NX_IP                 NetXDuoEthIpInstance;
NXD_MQTT_CLIENT       mqtt_client;
TX_EVENT_FLAGS_GROUP  mqtt_app_flag;
static ULONG          mqtt_client_stack [MQTT_CLIENT_STACK_SIZE / sizeof(ULONG)];
static UCHAR          message_buffer    [NXD_MQTT_MAX_MESSAGE_LENGTH];
static UCHAR          topic_buffer      [NXD_MQTT_MAX_TOPIC_NAME_LENGTH];
char*				  MESSAGE_STRING     = "";

/* Private function prototypes -----------------------------------------------*/
static VOID nx_app_thread_entry (ULONG thread_input);
static VOID nx_app_hb_entry (ULONG thread_input);

static VOID my_notify_func(NXD_MQTT_CLIENT* client_ptr, UINT number_of_messages)
{

    tx_event_flags_set(&mqtt_app_flag, DEMO_MESSAGE_EVENT, TX_OR);
    return;
}

static VOID nx_app_thread_entry (ULONG thread_input)
{
 
   UINT ret =   NX_SUCCESS;
   NXD_ADDRESS  server_ip;
   ULONG        events;
   UINT         topic_length
   UINT       	 message_length;
   UINT         i = 0;

    /* Create MQTT client instance. */
   ret = nxd_mqtt_client_create(&mqtt_client, "my_client", CLIENT_ID_STRING, STRLEN(CLIENT_ID_STRING), &NetXDuoEthIpInstance, &NxAppPool,(VOID*)mqtt_client_stack, sizeof(mqtt_client_stack MQTT_THREAD_PRIORTY, NX_NULL, 0);

    /* Create an event flag for this demo. */
   tx_event_flags_create(&mqtt_app_flag, "my app event");

    /*Select the IP protocol version and the IP address of the broker*/
   server_ip.nxd_ip_version    = 4;
   server_ip.nxd_ip_address.v4 = LOCAL_SERVER_ADDRESS;

    /* Start the connection to the server. */
   ret = nxd_mqtt_client_connect(&mqtt_client, &server_ip, NXD_MQTT_PORT, MQTT_KEEP_ALIVE_TIMER, 0, NX_WAIT_FOREVER);

   /* Subscribe to the topic with QoS level 0. */
   ret = nxd_mqtt_client_subscribe(&mqtt_client, SUB_TOPIC_NAME, STRLEN(SUB_TOPIC_NAME), QOS0);

   /* Set the receive notify function. */
   ret = nxd_mqtt_client_receive_notify_set(&mqtt_client, my_notify_func);

   while(1)
   {
	   /*Wait until MQTT message has arrived*/
	   tx_event_flags_get(&mqtt_app_flag, DEMO_ALL_EVENTS, TX_OR_CLEAR, &events, TX_WAIT_FOREVER);


	   /* Let the LED blink 10 times to signal the receipt of a new MQTT message */
	   do
	   {
		   HAL_GPIO_TogglePin(GPIOJ, GPIO_PIN_2);
		   tx_thread_sleep(10);
		   i++;

	   } while(i<10);

	   i = 0;
		
	   /* Get the receiving MQTT message */
	   if(events & DEMO_MESSAGE_EVENT)
	   {
		   ret = nxd_mqtt_client_message_get(&mqtt_client, topic_buffer, sizeof(topic_buffer), &topic_length, message_buffer,sizeof(message_buffer), &message_length);
	   }
	   
	   if(message_buffer[0] == "X")
	   {
		   break;
	   }
   }

   /* Now unsubscribe the topic. */
   nxd_mqtt_client_unsubscribe(&mqtt_client, SUB_TOPIC_NAME, STRLEN(SUB_TOPIC_NAME));

   /* Disconnect from the broker. */
   nxd_mqtt_client_disconnect(&mqtt_client);

   /* Delete the client instance, release all the resources. */
   nxd_mqtt_client_delete(&mqtt_client);

  /* USER CODE END Nx_App_Thread_Entry 0 */

}

static VOID nx_app_hb_entry (ULONG thread_input)
{
	while(1)
	{
		HAL_GPIO_TogglePin(GPIOI, GPIO_PIN_13);
		tx_thread_sleep(30);
	}
}
/* USER CODE BEGIN 1 */

/* USER CODE END 1 */

 

 

1 REPLY 1
STM32_ZA
ST Employee

Hi,

Can you check if the event arrives correctly to your callback function :

my_notify_func

  May be there is no message delivered by MQTT addons to the application