Embedded C Programming, End Term
Consider the following FreeRTOS code snippet:
#include "FreeRTOS.h"#include "task.h"#include "queue.h"
QueueHandle_t xQueue;
void SenderTask(void *pvParameters) { int data = 100; while (1) { if (xQueueSend(xQueue, &data, pdMS_TO_TICKS(1000)) == pdPASS) { printf("Data sent: %d\n", data); } vTaskDelay(pdMS_TO_TICKS(500)); }}
void ReceiverTask(void *pvParameters) { int receivedData; while (1) { if (xQueueReceive(xQueue, &receivedData, pdMS_TO_TICKS(2000)) == pdTRUE) { printf("Data received: %d\n", receivedData); } }}
int main() { xQueue = xQueueCreate(5, sizeof(int));
xTaskCreate(SenderTask, "Sender", 1000, NULL, 1, NULL); xTaskCreate(ReceiverTask, "Receiver", 1000, NULL, 1, NULL);
vTaskStartScheduler(); while (1);}What will happen if the receiver task is delayed for more than 3 seconds?
Consider the following FreeRTOS code snippet: #include "FreeRTOS.h" #include "task.h" #include "queue.h" QueueHandle_t xQueue; void SenderTask(void *pvParameters) { int data = 100; while (1) { if (xQueueSend(xQueue, &data, pdMS_TO_TICKS(1000)) == pdPASS) { printf("Data sent: %d\n", data); } vTaskDelay(pdMS_TO_TICKS(500)); } } void ReceiverTask(void *pvParameters) { int receivedData; while (1) { if (xQueueReceive(xQueue, &receivedData, pdMS_TO_TICKS(2000)) == pdTRUE) { printf("Data received: %d\n", receivedData); } } } int main() { xQueue = xQueueCreate(5, sizeof(int)); xTaskCreate(SenderTask, "Sender", 1000, NULL, 1, NULL); xTaskCreate(ReceiverTask, "Receiver", 1000, NULL, 1, NULL); vTaskStartScheduler(); while (1); } What will happen if the receiver task is delayed for more than 3 seconds? Consider a finite state machine (FSM) controlling a **motor speed system** with a single button. The states and events are defined as follows: **States:** 1. **STATE_STOPPED** – Motor is off. 2. **STATE_SLOW** – Motor runs at slow speed. 3. **STATE_FAST** – Motor runs at fast speed. 4. **STATE_OVERSPEED** – Motor runs at an unsafe speed. **Events:** 1. **EVENT_BUTTON_PRESS** – Button is pressed. 2. **EVENT_SENSOR_ALERT** – A sensor detects overspeed. Consider the following code. void handleEvent(Event_t event) { switch (currentState) { case STATE_STOPPED: if (event == EVENT_BUTTON_PRESS) { currentState = STATE_SLOW; } break; case STATE_SLOW: if (event == EVENT_BUTTON_PRESS) { currentState = STATE_FAST; } break; case STATE_FAST: if (event == EVENT_BUTTON_PRESS) { currentState = STATE_STOPPED; } else if (event == EVENT_SENSOR_ALERT) { currentState = STATE_OVERSPEED; } break; case STATE_OVERSPEED: if (event == EVENT_BUTTON_PRESS) { currentState = STATE_STOPPED; } break; default: currentState = STATE_STOPPED; break; } } If the initial state is **STATE_SLOW**, what will be the next state after one **EVENT_BUTTON_PRESS** and one **EVENT_SENSOR_ALERT** event? What is the main issue caused by priority inversion in an RTOS?