I have to reduce the processor load, in a way like unwanted code should not run unnecessarily.
So that I am making my system partially event based, and I am giving priority to each event so that every part of the code (event handler) has a priority.
could you please suggest me something on this to improve my code.
The first question is why do you even HAVE “unwanted code” that is running “unnecessarily”?
In a real-time system, code should be designed to only run when it is needed, and should be blocking when it isn’t.
Using an “Event Based” system is a way to make a single task do different things based on what is happening, but such a system requires that YOU the programmer, design the system to meet your requirements.
one crucial thing to determine is which of your tasks are cpu bound (ie spend the majority oth their time making CPU intensive computations) or I/O bound (spending most of their time suspended waiting for I/O), respectively. An RTOS works best for a mix of the two where you can “squeeze the cpu bound computations” into the time periods where the I/O bound compuations are waiting.
If most your tasks positively need lots of CPU time, the only gain from a preemptive RTOS is that all tasks make some progress over time, but the overall throughput will not change (single core MCU assumed, SMP is of course another level).
Another thing to consider is task interdepencency; a lot of people tend to be overcautious and back door serialize tasks or (on the other end) split up fully serialzed code paths unnecessarily into muliple tasks which makes them co routines.
As Richard pointed out, it is mostly an issue of system design.
Hi. Probably you wish to save a battery? If so,
enable Idle hook by setting configUSE_IDLE_HOOK to 1 in OS configuration. Then provide the functon: vApplicationIdleHook()
And in that function put STOP or SLEEP command for the CPU. How to exit from STOP or SLEEP mode is anoter story which is depending on the system you running the OS on. Also while exiting the STOP or SLEEP, the system timer should be restored to keep the time correctly if was stopped during sleeping time.
Actually, which is better depends a lot on use case. tickless idle is a good and easy to implement system to give modest gains when you don’t know well what the system will be doing. With the IDLE HOOK, you can go into much lower power modes if you know that you really don’t have something to do for a while.
For me, tickless idle is the best in all cases, even on ports that don’t include a default tickless implementation. The Cortex M ports include a default implementation, which includes hooks to allow you to customize. To Richard’s point, if you don’t customize it or replace it, you’ll get only modest gains (basic sleep).
Tickless idle is designed specifically to allow you as the developer to select the depth of low-power mode. Whatever sleep code you would write with the idle-hook approach, you should write for tickless idle instead. With tickless idle, the architecture for entering/exiting low-power mode is already in place; FreeRTOS tells you the expected idle time and helps you manage the race conditions associated with going to sleep.
You can go either way (idle hook or tickless idle) and accomplish all the same things, but the tickless idle API is tailored for low power.