What is the correct approach for setting up a linker script when implementing a multithreading program on a microcontroller?
Do you define the task stack pointer in its own ram section and then declare multiple sections for each task? Or is there a better way?
There is no "correct" approach.
One common method is to declare the stack for each task (and the control blocks for semaphores etc) statically in C, so they go in the .data
or .bss
section.
Another approach more suited to larger microcontrollers is to use heap allocation. This might seem error prone, but it is widely used. As long as all tasks and other resources are created at the start of execution then their placement will be close to deterministic and there is little probability of allocation failure as long as the system has sufficient memory in total.
It would be a lot of work and quite unusual to specify the details of each task in the linker script. It might be marginally more efficient (eg: no unnecessary zeroing of stack space just because it is in .bss
) but it will be a lot harder to maintain.
If your system has multiple RAM banks with different speeds you might want to put some things in the faster and some in the slower RAM, but I wouldn't recommend more than that.