FreeRTOS Daemon On C89
August was very busy for me so it was no publications here… Need to fix it then, let’s make a daemon for the FreeRTOS and understand how it works! :)
First things first, let’s understand core concepts of the FreeRTOS:
- Standard language for FreeRTOS which is used by most developers is C89.
- In the daemon we need to have a blocking calls to allow other tasks to run.
- The daemon is based on an infinite loop that will make our task running forever.
First we will include all necessary headers for our daemon:
Then we define a handle for our daemon task and the queue for inter-task communication if necessary:
Next, create a task, which will run on our daemon. In will be a function with an infinite loop inside:
As you can see, this task doesn’t have any useful payload. It will just print string of the text. But let’s talk about the code anyway.
Here we determine delay for our ticks.
This is all the logic of our function, it will not do nothing else but print the string.
This is an interesting part of our task function. I already said about the blocking call inside the loop above and it’s few ways how we can do it:
- A fixed delay (our case): vTaskDelay()
- Waiting in a semaphore: xSemaphoreTake()
- Waiting for a message in a queue: xQueueReceive() In this example we just using a simplest way with predetermined fixed delay between ticks.
Normally this part of the code NEVER shouldn’t be reached if everything works well. If something went wrong and our infinite loop ends, the task should be deleted.
So now we have complete function with our simple task. Just need to handle it correctly for make our daemon works.
In the xTaskCreate() we have next parameters:
- Pointer to our task function.
- Name (just for description) of our task
- Size of a stack for our task.
- Pointer for the parameters of the task, does not apply in our example.
- Priority of the task.
- Handle to the created task.
This will create a queue for our task with size of 10 to receive messages.
Starting the scheduler.
The program should never reach this place. Have to handle it correctly in production.
And complete code of our daemon will look like this:
That’s all, we made our own daemon. C is a simple and beautiful language, I really like it. Even in the modern world it works awesome and gives you unlimited opportunities to create whatever you want. Even with the old C89 standard.