PIC32 FreeRTOS Bus Error(IFETCH)

I’m using FreeRTOS 6.0.2, the PIC32 USB Starter board, MPLAB 8.43, and C32 1.10. Execution is fine up through the call to vTaskStartScheduler().  The tasks all run just fine. However, if all of the FreeRTOS tasks terminate, the system throws a Bus Error(IFETCH) exception.  Sadly, no exception address is given. vTaskStartScheduler() sits in an infinite loop if you never give it any tasks at all, so I presume this is something chasing a NULL (or misaligned) pointer after things get deleted from some internal queue.

PIC32 FreeRTOS Bus Error(IFETCH)

Can you explain what you are doing in more detail? Why would the tasks terminate? Why would you call vTaskStartScheduler() without creating at least one task first?

PIC32 FreeRTOS Bus Error(IFETCH)

Taking these in reverse order:
Why would you call vTaskStartScheduler() without creating at least one task first?
This was just to see what vTaskStartScheduler would do when faced with no tasks.  I was looking to see if the exception would fire even with no tasks.  It does not, so the data structures are consistent even with no tasks initially.  And vTaskStartScheduler() sits in an infinite loop.  I am a bit surprised by that-I would have expected it to simply return.
Why would the tasks terminate?
In my case, it was not intentional.  I had a bug in my tasks that was causing them to terminate.  The *PROBLEM* was that I couldn’t diagnose that my tasks were terminating because FreeRTOS caused that BUS ERROR(IFETCH) exception which smashed any useful diagnostic information. Presumably, something doesn’t clean up properly when all tasks terminate.  I suspect that there is an old stack lying around or a NULL pointer in some scheduling queue that gets followed. This gives me some concern because I am going to want to be able to use a bootloader to overwrite the flash on this at some point.  It would be nice to have a clean way of shutting down FreeRTOS, moving back to a small bootloader, updating the flash, and then reactivating FreeRTOS.

PIC32 FreeRTOS Bus Error(IFETCH)

This was just to see what vTaskStartScheduler would do when faced with no tasks. I was looking to see if the exception would fire even with no tasks. It does not, so the data structures are consistent even with no tasks initially. And vTaskStartScheduler() sits in an infinite loop. I am a bit surprised by that-I would have expected it to simply return.
The scheduler will create the idle task, then run the idle task.  I presume the infinite loop you mention is in fact the idle task.
In my case, it was not intentional. I had a bug in my tasks that was causing them to terminate. The *PROBLEM* was that I couldn’t diagnose that my tasks were terminating because FreeRTOS caused that BUS ERROR(IFETCH) exception which smashed any useful diagnostic information.
…and it was FreeRTOS that was causing that, right?  Not the bugs in your tasks.
Presumably, something doesn’t clean up properly when all tasks terminate. I suspect that there is an old stack lying around or a NULL pointer in some scheduling queue that gets followed.
Terminate how?  The only was to terminate a task is by calling vTaskDelete(), in which case things are cleaned up.  If you crash tasks and trash data structures in a system that is not using an MMU or MPU then there is no magic the kernel can do to carry on regardless. Regards.

PIC32 FreeRTOS Bus Error(IFETCH)

…and it was FreeRTOS that was causing that, right? Not the bugs in your tasks.
Yes.  Nothing in the tasks were throwing an exception.  They simply exited their infinite loop and returned.
Terminate how? The only was to terminate a task is by calling vTaskDelete(), in which case things are cleaned up. If you crash tasks and trash data structures in a system that is not using an MMU or MPU then there is no magic the kernel can do to carry on regardless.
The tasks didn’t “crash”.  They just exited.  Basically I had three tasks like this:
void task(void *pvParameters) {
    int flg;
    while (flg) {
        flg = FALSE; // Unintentionally
    }
}
Once all the tasks returned, the IFETCH got thrown. Continuing to run the idle task is acceptable behavior as I could have queried things about the tasks and discovered that they were not running.  Returning cleanly from vTaskStartScheduler() would also be acceptable as I would have wondered how execution got there and could have checked what was going on. Getting into a state where *FreeRTOS* throws an IFETCH and blows away all of my diagnostic information is not useful behavior.

PIC32 FreeRTOS Bus Error(IFETCH)

The only was to terminate a task is by calling vTaskDelete()
This is the important line in Richards post. Take note of it and your application will not crash, ignore it and it will. If you want tasks to simply exit their infinite loop then you must put a vTaskDelete(NULL) at the end of the function that implements a task.