This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Openthread with FreeRTOS

Hi!
I've successfully set op an OpenThread MQTT broker using a Rasperry Pi and nrf52 dongle and I am able to publish and subscribe to and from my computer by flashing the mqttsn_client_subscriber and mqttsn_client_publisher examples to my nrf52840 DK.

I want to incorporate thread communication into a larger project on the nrf52840 DK using FreeRTOS. My first step has been trying to modify the mqttsn_client_subscriber example to incorporate FreeRTOS. However I have not had much success.

Does the SDK provide examples of OpenThread with FreeRTOS or are there other sources I can consult?






Parents
  • Hello,

    There is one example that uses the openthread stack and FreeRTOS:

    SDK_for_Thread_and_Zigbee_4.1.0\examples\thread\freertos_coap_server

    Perhaps you can use this example for reference.

    Best regards,

    Edvin

  • Hi!
    I've tried this unsuccessfully.

    I modified the mqttsn_client_subscriber example some, making it connect to the broker and subscribe to topic automatically, not using the buttons.
    This solution works well. 



    However when i try to implement the same solution in FreeRTOS, using the freertos_coap_server as a reference, it doesn't work. The program does not connect to the gateway etc. 


    There seems to be an issue with running the mqttsn_evt_handler, perhaps this is related to using the app_scheduler in the program? I am not sure, but the events are not triggered. 

    Here is my code: https://github.com/hunshamar/freertos_openthread

    Do you have any suggestions for making this work? I am not too experienced with FreeRTOS or the nordic SDK, so any help would be appreciated. 

    -Asgeir


  • Hello,

    I experimented a bit more. It seems like you receive a hardfault, but I am not yet sure why. Please check the attached project:

    5460.freertos_coap_server_2.zip

    Put it in the same location as your current project. 

    Also, open hardfault_implementation.c and modify the function HardFault_process() to look like this:

    /*lint -save -e14 */
    __WEAK void HardFault_process(HardFault_stack_t * p_stack)
    {
        // Restart the system by default
        //NVIC_SystemReset();
        while (true)
        {
            NRF_LOG_PROCESS();
        }
    }
    /*lint -restore */

    And monitor the RTT log.

    I have to ask a colleague of mine, but he is not in today. I'll check with him, hopefully in the beginning of next week.

    BR,

    Edvin

  • Ok! I see.
    Keep me posted if your college or you find a solution. Hopefully we can make it work. 

    -Asgeir. 

  • Hi!
    Any updates? 

    One thing i noticed, when trying to run the working project without FreeRTOS in the project directory of the FreeRTOS program (modified coap server), and with the FreeRTOS makefile, the program would not run with the FreeRTOS makefile, despite not using any FreeRTOS functionality.

    I did not get a hardfault error, but other than that the program responded as the FreeRTOS program, not receiving any MQTT-events. However when i tried removing the FreeRTOS-files from the makefile, just from the SRC_FILES as shown below the program works.


    The right file is the working makefile.



    The program flow. First running the makefile on the left, then running the makefile on the right. 



    It seems that just including the FreeRTOS source files, without including or using them in the main-file,in the project causes the MQTT-events to stop responding. 



  • Hello,

    We believe the issue is that your function calls are using a lot of timers, which are not supposed to be called until you have kickstarted the FreeRTOS itself using vTaskStartScheduler().

    Basically, you can't use any of this:

        thread_instance_init();
        mqttsn_init();
        
        /* Connect to gateway */ 
        uint32_t err_code = mqttsn_client_search_gateway(&m_client, SEARCH_GATEWAY_TIMEOUT);
        if (err_code != NRF_SUCCESS)
        {
            NRF_LOG_INFO("SEARCH GATEWAY message could not be sent. Error: 0x%x\r\n", err_code);
        }else{
            NRF_LOG_INFO("Search gateway message sendt");
        }

    before you call vTaskStartScheduler(). Please note that the vTaskStartScheduler does not return, so you need to add these parts e.g. within your openthread task. 

    Without being able to pin point exactly what caused the Hardfault it was definitely a timing related issue. The reason it worked in SES was probably due to optimization. When I compiled the DEBUG variant of the project, it hardfaulted, just like your armgcc project. 

    I don't know exactly what it should look like, but look at the unmodified freertos_coap_server example, and do only one change at the time. If it stops working, then look into why it doesn't work.

    Are you sure you really need FreeRTOS anyway?

    BR,

    Edvin

  • static void thread_stack_task(void * arg)
    {
        UNUSED_PARAMETER(arg);
        mqttsn_init();
        uint32_t err_code = mqttsn_client_search_gateway(&m_client, SEARCH_GATEWAY_TIMEOUT);
        if (err_code != NRF_SUCCESS)
        {
            NRF_LOG_INFO("SEARCH GATEWAY message could not be sent. Error: 0x%x\r\n", err_code);
        }else{
            NRF_LOG_INFO("Search gateway message sendt");
        }
    
        while (1)
        {
            NRF_LOG_INFO("in loop");
            
            thread_process();
            app_sched_execute();
            if (NRF_LOG_PROCESS() == false)
            {
                thread_sleep();
            }
    
            UNUSED_VARIABLE(ulTaskNotifyTake(pdTRUE, portMAX_DELAY));
        }
    }
    

    Thank you for the reply! 
    Moving the mqttsn_init() function and code for connecting the gateway into the task, i.e. running it after the vTaskStartScheduler(), fixed the problem and the program works! I found moving the thread_instance_init() function to the task caused the program to crash. 

    So now everything seems to work! 

Reply
  • static void thread_stack_task(void * arg)
    {
        UNUSED_PARAMETER(arg);
        mqttsn_init();
        uint32_t err_code = mqttsn_client_search_gateway(&m_client, SEARCH_GATEWAY_TIMEOUT);
        if (err_code != NRF_SUCCESS)
        {
            NRF_LOG_INFO("SEARCH GATEWAY message could not be sent. Error: 0x%x\r\n", err_code);
        }else{
            NRF_LOG_INFO("Search gateway message sendt");
        }
    
        while (1)
        {
            NRF_LOG_INFO("in loop");
            
            thread_process();
            app_sched_execute();
            if (NRF_LOG_PROCESS() == false)
            {
                thread_sleep();
            }
    
            UNUSED_VARIABLE(ulTaskNotifyTake(pdTRUE, portMAX_DELAY));
        }
    }
    

    Thank you for the reply! 
    Moving the mqttsn_init() function and code for connecting the gateway into the task, i.e. running it after the vTaskStartScheduler(), fixed the problem and the program works! I found moving the thread_instance_init() function to the task caused the program to crash. 

    So now everything seems to work! 

Children
No Data
Related