| 
									
										
										
										
											2019-11-22 14:28:15 -03:00
										 |  |  | Call function with external stack
 | 
					
						
							|  |  |  | =================================
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Overview
 | 
					
						
							|  |  |  | --------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | A given function can be executed with a user allocated stack space
 | 
					
						
							|  |  |  | which is independent of current task stack, this mechanism can be
 | 
					
						
							|  |  |  | used to save stack space wasted by tasks which call a common function
 | 
					
						
							|  |  |  | with intensive stack usage such as `printf`. The given function can
 | 
					
						
							| 
									
										
										
										
											2020-03-12 02:59:53 -03:00
										 |  |  | be called inside the shared stack space which is a callback function
 | 
					
						
							|  |  |  | deferred by calling :cpp:func:`esp_execute_shared_stack_function`, 
 | 
					
						
							|  |  |  | passing that function as parameter
 | 
					
						
							| 
									
										
										
										
											2019-11-22 14:28:15 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | Usage
 | 
					
						
							|  |  |  | -----
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-12 02:59:53 -03:00
										 |  |  | :cpp:func:`esp_execute_shared_stack_function` takes four arguments, 
 | 
					
						
							| 
									
										
										
										
											2019-11-22 14:28:15 -03:00
										 |  |  | a mutex object allocated by the caller, which is used to protect if 
 | 
					
						
							|  |  |  | the same function shares its allocated stack, a pointer to the top 
 | 
					
						
							| 
									
										
										
										
											2020-03-12 02:59:53 -03:00
										 |  |  | of stack used to that fuction, the size in bytes of stack and, a pointer
 | 
					
						
							|  |  |  | to a user function where the shared stack space will reside, after calling
 | 
					
						
							|  |  |  | the function, the user defined function will be deferred as a callback
 | 
					
						
							|  |  |  | where functions can be called using the user allocated space without
 | 
					
						
							|  |  |  | taking space from current task stack. 
 | 
					
						
							| 
									
										
										
										
											2019-11-22 14:28:15 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | The usage may looks like the code below:
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. code-block:: c
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-12 02:59:53 -03:00
										 |  |  |     void external_stack_function(void)
 | 
					
						
							|  |  |  |     {
 | 
					
						
							|  |  |  |         printf("Executing this printf from external stack! \n");
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-22 14:28:15 -03:00
										 |  |  |     //Let's suppose we wanting to call printf using a separated stack space
 | 
					
						
							|  |  |  |     //allowing app to reduce its stack size.
 | 
					
						
							|  |  |  |     void app_main()
 | 
					
						
							|  |  |  |     {
 | 
					
						
							|  |  |  |         //Allocate a stack buffer, from heap or as a static form:
 | 
					
						
							|  |  |  |         portSTACK_TYPE *shared_stack = malloc(8192 * sizeof(portSTACK_TYPE));
 | 
					
						
							| 
									
										
										
										
											2019-12-20 13:30:30 -03:00
										 |  |  |         assert(shared_stack != NULL);
 | 
					
						
							| 
									
										
										
										
											2019-11-22 14:28:15 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  |         //Allocate a mutex to protect its usage:
 | 
					
						
							|  |  |  |         SemaphoreHandle_t printf_lock = xSemaphoreCreateMutex();
 | 
					
						
							| 
									
										
										
										
											2019-12-20 13:30:30 -03:00
										 |  |  |         assert(printf_lock != NULL);
 | 
					
						
							| 
									
										
										
										
											2019-11-22 14:28:15 -03:00
										 |  |  |      
 | 
					
						
							|  |  |  |         //Call the desired function using the macro helper:
 | 
					
						
							| 
									
										
										
										
											2020-03-12 02:59:53 -03:00
										 |  |  |         esp_execute_shared_stack_function(printf_lock, 
 | 
					
						
							|  |  |  |                                         shared_stack,
 | 
					
						
							|  |  |  |                                         8192,
 | 
					
						
							|  |  |  |                                         external_stack_function);
 | 
					
						
							|  |  |  |         
 | 
					
						
							| 
									
										
										
										
											2019-12-20 13:23:47 -03:00
										 |  |  |         vSemaphoreDelete(printf_lock);    
 | 
					
						
							|  |  |  |         free(shared_stack); 
 | 
					
						
							| 
									
										
										
										
											2019-11-22 14:28:15 -03:00
										 |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. _esp-call-with-stack-basic_usage:
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | API Reference
 | 
					
						
							|  |  |  | -------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-13 11:46:16 +08:00
										 |  |  | .. include-build-file:: inc/esp_expression_with_stack.inc
 | 
					
						
							| 
									
										
										
										
											2019-11-22 14:28:15 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 |