What is the purpose of the xTimerCreate() function in FreeRTOS?
The xTimerCreate() function creates a software timer that can be used to execute a callback function after a specified time period or repeatedly at fixed intervals.
Click to reveal answer
beginner
What are the main parameters of xTimerCreate()?
The main parameters are:
pcTimerName: a name for the timer (for debugging)
xTimerPeriodInTicks: the timer period in ticks
uxAutoReload: whether the timer reloads automatically (pdTRUE) or is one-shot (pdFALSE)
pvTimerID: an ID to associate with the timer
pxCallbackFunction: the function to call when the timer expires
Click to reveal answer
beginner
What does the uxAutoReload parameter control in xTimerCreate()?
It controls if the timer is periodic or one-shot. If uxAutoReload is set to pdTRUE, the timer restarts automatically after expiring. If pdFALSE, the timer runs once and stops.
Click to reveal answer
beginner
What type of value does xTimerCreate() return?
It returns a handle to the created timer of type TimerHandle_t. If the timer creation fails, it returns NULL.
Click to reveal answer
beginner
Why is the timer name parameter useful in xTimerCreate()?
The timer name helps during debugging and tracing by giving a human-readable identifier to the timer, making it easier to track timer behavior in logs or debuggers.
Click to reveal answer
What does the xTimerCreate() function return if it fails to create a timer?
A-1
B0
CNULL
DA boolean false
✗ Incorrect
If timer creation fails, xTimerCreate() returns NULL to indicate no valid timer handle was created.
Which parameter of xTimerCreate() decides if the timer repeats automatically?
ApcTimerName
BuxAutoReload
CpvTimerID
DxTimerPeriodInTicks
✗ Incorrect
The uxAutoReload parameter controls whether the timer restarts automatically after expiring.
What type of function must be passed as the callback in xTimerCreate()?
AA function that takes a TimerHandle_t parameter and returns void
BA function with no parameters and void return
CA function that returns an int
DAny function pointer
✗ Incorrect
The callback function must accept a TimerHandle_t parameter and return void, so it can receive the timer handle when called.
What unit is used for the timer period in xTimerCreate()?
ATicks
BMilliseconds
CSeconds
DMicroseconds
✗ Incorrect
The timer period is specified in ticks, which depend on the FreeRTOS tick rate configuration.
Which of these is NOT a parameter of xTimerCreate()?
ACallback function
BTimer name
CTimer period in ticks
DTimer priority
✗ Incorrect
Timer priority is not a parameter of xTimerCreate(); timers do not have priorities.
Explain how to create a periodic software timer using xTimerCreate() in FreeRTOS.
Think about the parameters needed to make the timer repeat automatically.
You got /5 concepts.
Describe the role of the callback function in a FreeRTOS timer created with xTimerCreate().
What happens when the timer reaches its period?
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the xTimerCreate() function in FreeRTOS?
easy
A. To create a software timer that can run a function after a delay or repeatedly
B. To start the scheduler for task management
C. To create a hardware interrupt
D. To allocate memory for tasks
Solution
Step 1: Understand the role of xTimerCreate()
The function xTimerCreate() is used to create software timers in FreeRTOS, which can execute a callback function after a delay or periodically.
Step 2: Compare with other options
Starting the scheduler, creating hardware interrupts, or allocating memory for tasks are not the purpose of xTimerCreate().
Final Answer:
To create a software timer that can run a function after a delay or repeatedly -> Option A
Quick Check:
Timer creation = software timer setup [OK]
Hint: xTimerCreate() makes timers to run code later [OK]
Common Mistakes:
Confusing timer creation with starting the scheduler
Thinking it creates hardware interrupts
Assuming it allocates memory for tasks
2. Which of the following is the correct way to call xTimerCreate() to create a one-shot timer named "MyTimer" with a 1000 tick delay and a callback function TimerCallback?
easy
A. xTimerCreate("MyTimer", 1000, pdTRUE, NULL, TimerCallback);
B. xTimerCreate("MyTimer", 1000, pdFALSE, NULL, TimerCallback);
C. xTimerCreate("MyTimer", 1000, pdFALSE, TimerCallback, NULL);
D. xTimerCreate("MyTimer", 1000, pdFALSE, NULL, &TimerCallback);
Solution
Step 1: Understand xTimerCreate() parameters
The parameters are: name (string), period (ticks), auto-reload (pdTRUE or pdFALSE), timer ID (can be NULL), and callback function pointer.
Step 2: Check each option
xTimerCreate("MyTimer", 1000, pdFALSE, NULL, TimerCallback); correctly uses pdFALSE for one-shot, NULL for timer ID, and TimerCallback as the function pointer. xTimerCreate("MyTimer", 1000, pdTRUE, NULL, TimerCallback); uses pdTRUE (auto-reload), which is wrong for one-shot. xTimerCreate("MyTimer", 1000, pdFALSE, TimerCallback, NULL); swaps callback and timer ID. xTimerCreate("MyTimer", 1000, pdFALSE, NULL, &TimerCallback); uses &TimerCallback which is incorrect; function name alone is used.
Final Answer:
xTimerCreate("MyTimer", 1000, pdFALSE, NULL, TimerCallback); -> Option B
Quick Check:
One-shot timer = pdFALSE, callback last parameter [OK]
Hint: Use pdFALSE for one-shot, callback is last parameter [OK]
A. TimerCallback function is defined after it is used, causing a compile error
B. pdFALSE should be pdTRUE for one-shot timers
C. xTimerStart is called before checking if timer is NULL
D. Timer name cannot be a string literal
Solution
Step 1: Check timer creation and start sequence
The timer is created and immediately started without checking if the returned handle is NULL, which can cause runtime errors if creation failed.
Step 2: Verify other parts
Defining TimerCallback after usage is allowed in C if a prototype exists or compiler supports it. pdFALSE is correct for one-shot. Timer name as string literal is valid.
Final Answer:
xTimerStart is called before checking if timer is NULL -> Option C
Quick Check:
Always check timer handle before starting [OK]
Hint: Check timer handle != NULL before starting [OK]
Common Mistakes:
Skipping NULL check on timer handle
Confusing pdFALSE and pdTRUE for timer type
Thinking string literals are invalid timer names
5. You want to create a timer that runs a callback every 200 ticks but only after an initial delay of 1000 ticks. Which approach using xTimerCreate() and FreeRTOS timers is correct?
hard
A. Create a one-shot timer with 200 ticks period and restart it manually every 1000 ticks
B. Create a one-shot timer with 1200 ticks period and use it as auto-reload
C. Create an auto-reload timer with 1200 ticks period and start it immediately
D. Create an auto-reload timer with 200 ticks period and start it after a one-shot timer of 1000 ticks triggers the first callback to start the auto-reload timer
Solution
Step 1: Understand the requirement
The timer should wait 1000 ticks before first callback, then run every 200 ticks repeatedly.
Step 2: Analyze options
Create an auto-reload timer with 200 ticks period and start it after a one-shot timer of 1000 ticks triggers the first callback to start the auto-reload timer uses a one-shot timer for initial delay, which on expiry starts the auto-reload timer for repeated 200 ticks callbacks. This matches the requirement. Create a one-shot timer with 1200 ticks period and use it as auto-reload incorrectly combines periods and auto-reload. Create an auto-reload timer with 1200 ticks period and start it immediately starts immediately without initial delay. Create a one-shot timer with 200 ticks period and restart it manually every 1000 ticks misuses one-shot timer and manual restart timing.
Final Answer:
Create an auto-reload timer with 200 ticks period and start it after a one-shot timer of 1000 ticks triggers the first callback to start the auto-reload timer -> Option D