ATMega2560 reset when exiting task

Hi, i have 3 tasks running and a shutdown condition that causes every task to exit its main loop. if i after the main loop have each task enter an infinite loop, everything is fine; if *any* one of them exit out of the task function, the device reboots.. this also happens when only running 1 task and at any stack size.. watch dog is turned off… since the kernel, according to the documentation, never exits vTaskStartScheduler unless vTaskEndScheduler is called, what happens to tasks that return? do they just end as one would expect with the scheduler continuing? maybe it’s when the idle task tries to do post-task cleanup that the device crashes/reboots? im using heap_1.c by the way…

ATMega2560 reset when exiting task

From the WEB "Task functions should never return so are typically implemented as a continuous loop". If you want to leave a task then you have to write something like this – void TaskFunction(void *pv) {     while(1)     {         // task code that contains a break to jump out of the loop.     }     // Must not run off end of function so delete ourselves.     vTaskDelete(NULL); }

ATMega2560 reset when exiting task

Further to davedoors’ comment: The tasks are simply functions when you create a task at start-up all you do is put a pointer to the functions entry point in the TCB list of the scheduler. When the function then terminates, unless you explicitely call vTaskDelete(NULL) the function exit will cause a stack underflow, that is items that hadn’t been pushed on the stack get popped and the program counter made to point somewhere in oblivion. Resulting in the reset that you observed.

ATMega2560 reset when exiting task

great advice… since vTaskDelete can be omitted from the build to save space, i probably will put a "for ( ;; );" at the end of the tasks in question instead.. thanks..

ATMega2560 reset when exiting task

If you never want the task to run again and don’t want to free the memory it is using you can instead use vTaskSuspend(NULL); This will prevent the task using CPU time, unlike the for(;;) solution.

ATMega2560 reset when exiting task

it’s not that important if the tasks use cpu or not when entering shutdown.. i tried the vTaskSuspend ( NULL) option anyway (since i have it included in the build already), but this trigs a spontaneous device reboot in about 50% of cases also, which i find very strange…