// Send a character from serial port of your laptop to the development board,
// configure the appropriate UART interrupt such that when:
// ‘Y’ is sent, LED should be turned ON
// ‘N’ is sent, LED should be turned OFF
// Make use of Centralized Deferred Interrupt Processing.
#include <Arduino.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
// LED Pin
#define LED_PIN 2
#define QUEUE_LENGTH 10
// Buffer to store received characters
#define UART_BUFFER_SIZE 20 // Reduced the buffer size for this example
char uart_buffer[UART_BUFFER_SIZE];
volatile int uart_buffer_index = 0;
// Task handle for UART processing task
TaskHandle_t processUartTaskHandle;
TaskHandle_t serialInputTaskHandle;
QueueHandle_t stringQueue;
void processUartTask(void* parameter) {
char received_buffer[UART_BUFFER_SIZE];
while (1) {
// Wait to receive data from the queue
if (xQueueReceive(stringQueue, &received_buffer, portMAX_DELAY)) {
// Process the received data
for (int i = 0; i < UART_BUFFER_SIZE && received_buffer[i] != '\0'; i++) {
char received_char = received_buffer[i];
if (received_char == 'Y') {
digitalWrite(LED_PIN, HIGH);
} else if (received_char == 'N') {
digitalWrite(LED_PIN, LOW);
}
}
}
}
}
void serialInputTask(void* parameter) {
while (1) {
if (Serial.available()) {
uart_buffer_index = 0;
// Read all available characters
while (Serial.available() && uart_buffer_index < UART_BUFFER_SIZE - 1) {
uart_buffer[uart_buffer_index++] = Serial.read();
}
uart_buffer[uart_buffer_index] = '\0'; // Null-terminate the string
// Send the buffer to the queue
xQueueSend(stringQueue, uart_buffer, pdMS_TO_TICKS(10));
}
vTaskDelay(pdMS_TO_TICKS(10)); // Add a small delay to allow other tasks to run
}
}
void setup() {
// Initialize LED pin
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
// Initialize Serial communication
Serial.begin(115200);
stringQueue = xQueueCreate(QUEUE_LENGTH, sizeof(uart_buffer));
// Create task for processing UART data
xTaskCreate(processUartTask, "Process UART Task", 1024, NULL, 1, &processUartTaskHandle);
xTaskCreate(serialInputTask, "Serial Input Task", 1024, NULL, 1, &serialInputTaskHandle);
}
void loop() {
// Nothing to do in the loop as everything is handled by tasks
}