<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Add FreeRTOS to do a lot of caculation and BLE data data transmission in MutilRole device</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/82049/add-freertos-to-do-a-lot-of-caculation-and-ble-data-data-transmission-in-mutilrole-device</link><description>Hi, 
 I got the example github.com/.../nrf52-ble-multi-link-multi-role and I have added FreeRTOS to the example refer to ble_app_hrs_freertos in SDK v15.2.0 with S140 v6.1.0. 
 We want to do a game project. One 52840 works in mutilrole(central_and_peripheral</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 08 Dec 2021 02:44:19 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/82049/add-freertos-to-do-a-lot-of-caculation-and-ble-data-data-transmission-in-mutilrole-device" /><item><title>RE: Add FreeRTOS to do a lot of caculation and BLE data data transmission in MutilRole device</title><link>https://devzone.nordicsemi.com/thread/342447?ContentTypeID=1</link><pubDate>Wed, 08 Dec 2021 02:44:19 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5f3026bb-1a11-4286-9535-fd1d5976fec2</guid><dc:creator>Bruse</dc:creator><description>&lt;p&gt;Hi&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I have solved the issue causing by flash erasing. I use the queue&amp;nbsp;mechanism like this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;		while(1)
		{				
			if(xQueueReceive(ble_task_handle,&amp;amp;ble_task_msg,portMAX_DELAY)==pdTRUE)							
			{					
				switch(ble_task_msg.task_id)
				{
					case APP_COMMAND_QUEUE_ID:				
							 process_ble_commands();	
					break;
					
					case WRITE_FLASH_QUEUE_ID:
							 flash_operate(fs_event_num);		
					break;
											
				}
				ble_task_msg.task_id = 0;
			}			
		}&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Add FreeRTOS to do a lot of caculation and BLE data data transmission in MutilRole device</title><link>https://devzone.nordicsemi.com/thread/341176?ContentTypeID=1</link><pubDate>Tue, 30 Nov 2021 06:19:18 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:37d643b1-682b-46f1-9654-b64e085abf19</guid><dc:creator>Susheel Nuguru</dc:creator><description>[quote user="Bruse"]However, if I&amp;nbsp;commen out&amp;nbsp;if (pdPASS != xTaskCreate(app_task_demo, &amp;quot;TaskDemo&amp;quot;, 256, NULL, 1, &amp;amp;m_demo_thread)), that is to say, the task&amp;nbsp;&lt;span&gt;app_task_demo has not been created, the flash erasing and writing is all OK.&lt;/span&gt;[/quote]
&lt;p&gt;It does not sound to me that the taskCreate function was successful and it looks like your application is not handling that fault correctly. I would first advise you to find out the error code for that failure in xtaskCreate function and if it is due to insufficient heap memory, then you need to increase the heap size in&amp;nbsp;configTOTAL_HEAP_SIZE in your FreeRTOSConfig.h file.&amp;nbsp;&lt;/p&gt;
[quote user="Bruse"]But I meet other issue. I need to write some data to flash which is receive from UART. When it &lt;span&gt;runs&lt;/span&gt; nrf_fstorage_erase() function, the code is dead and it couldn&amp;#39;t run any more.&amp;nbsp;[/quote]
&lt;p&gt;There is no such thing as the code is dead, the PC of the CPU still needs to execute something so you need to figure out the context in which your program counter (PC) is still executing. Start your application in debugger after compiling it without any debug optimizations and when the error occurs, then halt the debugger to check the context in which your code is being executed. It could be a loop, app_error_check handler or an hardfault. Once you figured out why your code seems dead to you (most likely because it might be looping somewhere) then you can try to understand the nature of your problem.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Add FreeRTOS to do a lot of caculation and BLE data data transmission in MutilRole device</title><link>https://devzone.nordicsemi.com/thread/341167?ContentTypeID=1</link><pubDate>Tue, 30 Nov 2021 01:16:35 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:61a94472-e612-413d-a4c4-a066a66bb4d5</guid><dc:creator>Bruse</dc:creator><description>&lt;p&gt;I also try to lock when flash is erasing like this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;			taskENTER_CRITICAL();
    		flash_clear();				
			taskEXIT_CRITICAL();&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;However,the code still would die and it couldn&amp;#39;t run any more when it runs to&amp;nbsp;flash_clear unless I reset or power up again.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Is there any solutions for this issue? Thanks.&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Add FreeRTOS to do a lot of caculation and BLE data data transmission in MutilRole device</title><link>https://devzone.nordicsemi.com/thread/341104?ContentTypeID=1</link><pubDate>Mon, 29 Nov 2021 14:06:44 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:89a73264-c95b-45a2-a78b-c17728bb26b8</guid><dc:creator>Bruse</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;I just add a delay&amp;nbsp;vTaskDelay(xDelay) to the&amp;nbsp;while(1) in the&amp;nbsp;BLEAppTask task, then the two task could run, also I have add a while(1) to the&amp;nbsp;app_task_demo task.&lt;/p&gt;
&lt;p&gt;But I meet other issue. I need to write some data to flash which is receive from UART. When it &lt;span&gt;runs&lt;/span&gt; nrf_fstorage_erase() function, the code is dead and it couldn&amp;#39;t run any more.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;static void BLEAppTask(void * pvParameter)
{	
	uint8_t task_run_count = 0;
	const portTickType xDelay = pdMS_TO_TICKS(50); 

		if (pdPASS != xTimerStart(m_battery_timer, OSTIMER_WAIT_FOR_QUEUE))
		{
				APP_ERROR_HANDLER(NRF_ERROR_NO_MEM);
		}		
		while(1)
		{	
			  process_ble_commands();				
				flash_operate(fs_event_num);
				taskYIELD();
				vTaskDelay(xDelay);	
		}
}

static void ble_task(void *arg)
{		
		advertising_start(); 	
		scan_start();                          // Start scanning for peripherals and initiate connection to devices which  advertise.	
}	
static void app_task_demo(void *arg)
{		
		
		if (pdPASS != xTimerStart(m_test_timer, OSTIMER_WAIT_FOR_QUEUE))
		{
				APP_ERROR_HANDLER(NRF_ERROR_NO_MEM);
		}	
		uart_printf(&amp;quot;app_task_demo start.\n&amp;quot;);
		
		while(1)
		{

		}
}	
static void run_task(void)
{
	nrf_sdh_freertos_init(ble_task, NULL);	                                 // Create a FreeRTOS task for the BLE stack.

    if (pdPASS != xTaskCreate(BLEAppTask, &amp;quot;BLEAppTask&amp;quot;, 256, NULL, 2, &amp;amp;m_app_thread))
    {
        uart_printf(&amp;quot;Failed to create BLEAppTask.\n&amp;quot;);
    }

    if (pdPASS != xTaskCreate(app_task_demo, &amp;quot;TaskDemo&amp;quot;, 256, NULL, 1, &amp;amp;m_demo_thread))
    {
        uart_printf(&amp;quot;Failed to create TaskDemo task.\n&amp;quot;);
    }
}
int main(void)
{
    ....
    ....
	run_task();
		
	uart_printf(&amp;quot;MutilRole application start %s\n&amp;quot;,FIRM_WARE_REVERSION);	
    vTaskStartScheduler();		

    for (;;)
    {	
		APP_ERROR_HANDLER(NRF_ERROR_FORBIDDEN);
    }
		
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;I refer to&amp;nbsp;flash_fstorage example to do flash writing and the main code as follow:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;nrf_fstorage_api_t * p_fs_api;

//uint32_t data_arry[5]={0x110A0B0C,0x220A0B0C,0x330A0B0C,0x440A0B0C,0x550A0B0C};

void fstorage_evt_handler(nrf_fstorage_evt_t * p_evt)
{
    if (p_evt-&amp;gt;result != NRF_SUCCESS)
    {
        uart_printf(&amp;quot;--&amp;gt; Event received: ERROR while executing an fstorage operation.\n&amp;quot;);
        return;
    }

    switch (p_evt-&amp;gt;id)
    {
        case NRF_FSTORAGE_EVT_WRITE_RESULT:
        {
            uart_printf(&amp;quot;--&amp;gt; Event received: wrote %d bytes at address 0x%x.\n&amp;quot;,
                         p_evt-&amp;gt;len, p_evt-&amp;gt;addr);
							
        } break;

        case NRF_FSTORAGE_EVT_ERASE_RESULT:
        {
            uart_printf(&amp;quot;--&amp;gt; Event received: erased %d page from address 0x%x.\n&amp;quot;,
                         p_evt-&amp;gt;len, p_evt-&amp;gt;addr);
        } break;
				
        default:
            break;
    }
}
NRF_FSTORAGE_DEF(nrf_fstorage_t fstorage) =
{
    /* Set a handler for fstorage events. */
    .evt_handler = fstorage_evt_handler,

    /* These below are the boundaries of the flash space assigned to this instance of fstorage.
     * You must set these manually, even at runtime, before nrf_fstorage_init() is called.
     * The function nrf5_flash_end_addr_get() can be used to retrieve the last address on the
     * last page of flash available to write data. */
    .start_addr = 0x3e000,
    .end_addr   = 0x3ffff,
};
static void print_flash_info(nrf_fstorage_t * p_fstorage)
{
    uart_printf(&amp;quot;========| flash info |========\n&amp;quot;);
    uart_printf(&amp;quot;erase unit: \t%d bytes\n&amp;quot;,      p_fstorage-&amp;gt;p_flash_info-&amp;gt;erase_unit);
    uart_printf(&amp;quot;program unit: \t%d bytes\n&amp;quot;,    p_fstorage-&amp;gt;p_flash_info-&amp;gt;program_unit);
		uart_printf(&amp;quot;start_addr: \t0x%x\n&amp;quot;,    p_fstorage-&amp;gt;start_addr);
		uart_printf(&amp;quot;end_addr: \t0x%x\n&amp;quot;,    p_fstorage-&amp;gt;end_addr);
    uart_printf(&amp;quot;==============================\n&amp;quot;);
}
void wait_for_flash_ready(nrf_fstorage_t const * p_fstorage)
{
    /* While fstorage is busy, sleep and wait for an event. */
    while (nrf_fstorage_is_busy(p_fstorage))
    {
       ;
    }
}

void flash_clear(void)
{
    ret_code_t rc = nrf_fstorage_erase(&amp;amp;fstorage, fstorage.start_addr, 1, NULL);
    if (rc != NRF_SUCCESS)
    {
        uart_printf(&amp;quot;nrf_fstorage_erase() returned: 0x%x\r\n&amp;quot;, rc);
    } 	
}
void flash_store(uint32_t *dat,uint16_t len)
{
		ret_code_t rc;
	  rc = nrf_fstorage_write(&amp;amp;fstorage, fstorage.start_addr, dat, len, NULL); //Write data to memory address 0x100000. Check it with command: nrfjprog --memrd 0x100000 --n 16
    APP_ERROR_CHECK(rc);
    wait_for_flash_ready(&amp;amp;fstorage);
    uart_printf(&amp;quot;Done.\n&amp;quot;);
}

void flash_load(uint32_t *dat,uint16_t len)
{
		ret_code_t rc;
	  rc = nrf_fstorage_read(&amp;amp;fstorage, fstorage.start_addr, dat, len); //Write data to memory address 0x100000. Check it with command: nrfjprog --memrd 0xFF000 --n 16
    APP_ERROR_CHECK(rc);	
}	


/**@brief   Helper function to obtain the last address on the last page of the on-chip flash that
 *          can be used to write user data.
 */
static uint32_t nrf5_flash_end_addr_get()
{
    uint32_t const bootloader_addr = NRF_UICR-&amp;gt;NRFFW[0];
    uint32_t const page_sz         = NRF_FICR-&amp;gt;CODEPAGESIZE;
    uint32_t const code_sz         = NRF_FICR-&amp;gt;CODESIZE;

		uart_printf(&amp;quot;bootloader_addr=%x page_sz=%x code_sz=%x\n&amp;quot;,    bootloader_addr,page_sz,code_sz);
    return (bootloader_addr != 0xFFFFFFFF ?
            bootloader_addr : (code_sz * page_sz));
}		
void fstorage_init(void)
{		
    ret_code_t rc;	
		p_fs_api = &amp;amp;nrf_fstorage_sd;
		fstorage.start_addr = nrf5_flash_end_addr_get()- 0x1000;	
		fstorage.end_addr = fstorage.start_addr + 0x1000;
    rc = nrf_fstorage_init(&amp;amp;fstorage, p_fs_api, NULL);
    APP_ERROR_CHECK(rc);
		print_flash_info(&amp;amp;fstorage);

}
void flash_operate(fs_evt event)
{
		switch(event)
		{
			case FS_STORE_EVT:
				uart_printf(&amp;quot;Store......\r\n&amp;quot;);	
				break;
			
			case FS_ERASE_EVT:
					uart_printf(&amp;quot;Erase......\r\n&amp;quot;);
			//		flash_load((uint32_t *)&amp;amp;at_com_data,sizeof(at_com_data_t));	
    			flash_clear();
					flash_store((uint32_t *)&amp;amp;at_com_data,sizeof(at_com_data_t));			
					fs_event_num = FS_IDLE;
				break;			
		}
}
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;However, if I&amp;nbsp;commen out&amp;nbsp;if (pdPASS != xTaskCreate(app_task_demo, &amp;quot;TaskDemo&amp;quot;, 256, NULL, 1, &amp;amp;m_demo_thread)), that is to say, the task&amp;nbsp;&lt;span&gt;app_task_demo has not been created, the flash erasing and writing is all OK.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Best regards,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Bruse&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Add FreeRTOS to do a lot of caculation and BLE data data transmission in MutilRole device</title><link>https://devzone.nordicsemi.com/thread/340862?ContentTypeID=1</link><pubDate>Fri, 26 Nov 2021 09:52:30 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:93e9e24a-df70-4d63-bda0-feddddf40c3d</guid><dc:creator>run_ar</dc:creator><description>&lt;p&gt;Hi,&lt;br /&gt;Please try to add the workaround for the issue to your code to see if that helps. Since there is a known issue, it&amp;#39;s probably better to test that first, before starting to benchmark other functions.&lt;/p&gt;
&lt;p&gt;One way to do the benchmarking is to add gpio toggling to parts of your code that you want to check run time for, then observe that using a logic analyzer.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Add FreeRTOS to do a lot of caculation and BLE data data transmission in MutilRole device</title><link>https://devzone.nordicsemi.com/thread/340849?ContentTypeID=1</link><pubDate>Fri, 26 Nov 2021 09:11:38 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f60f085b-cccf-45ea-a6fe-e7166e9d5906</guid><dc:creator>Bruse</dc:creator><description>&lt;p&gt;Hi&lt;/p&gt;
&lt;p&gt;How to do&amp;nbsp;bench marking of the various tasks?&lt;/p&gt;
&lt;p&gt;Now I just have two task. One task(&lt;span&gt;BLEAppTask&lt;/span&gt;) mainly is used&amp;nbsp;for&amp;nbsp;&lt;span&gt;&amp;nbsp;BLE data&amp;nbsp;transmission. Another task(app_task_demo) is just used to start a timer to toggle a LED.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Should I&amp;nbsp;&lt;span&gt;merge&lt;/span&gt; &lt;span&gt;BLEAppTask into the task which&amp;#39;s created by&amp;nbsp;nrf_sdh_freertos_init(). I have tried to do it as follow, but it&amp;nbsp;&amp;nbsp;couldn&amp;#39;t funciton properly.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="text"&gt;static void ble_task(void *arg)
{		
		advertising_start(); 	
		scan_start();                          // Start scanning for peripherals and initiate connection to devices which  advertise.	
	
		while(1)
		{
			  process_ble_commands();				
			flash_operate(fs_event_num);
			taskYIELD();
		}
}

nrf_sdh_freertos_init(ble_task, NULL);&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;I see some issues about nRF52 SDK on &lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/34155/what-are-sdk-15-x-0-known-issues.Would"&gt;https://devzone.nordicsemi.com/f/nordic-q-a/34155/what-are-sdk-15-x-0-known-issues&lt;/a&gt;. Would these issues cause my issue？&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Best regards,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Bruse&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Add FreeRTOS to do a lot of caculation and BLE data data transmission in MutilRole device</title><link>https://devzone.nordicsemi.com/thread/340529?ContentTypeID=1</link><pubDate>Wed, 24 Nov 2021 12:03:47 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:143d08ee-b05b-4109-b1a7-71edcdf6e921</guid><dc:creator>run_ar</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Have you done any bench marking of the various tasks you have? Sounds as you are running out of CPU time and would need to optimize some function or adapt you app to handle that in some way.&lt;/p&gt;
&lt;p&gt;As for raising app priorities to a higher level, this could disturb the SD behavior and is not recommended. If you use the app high level for some of&amp;nbsp; your tasks you need to make sure the sd has some time to run tasks in SD low priority as well, or it will not able to funciton properly.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>