0% found this document useful (0 votes)
861 views11 pages

FreeRTOS Multitasking Application Guide

The document is an assignment submitted by Keerti R Bhadankar for the subject Embedded System Design. It contains 3 questions asking to create multitasking programs using FreeRTOS on the Keil MCB 1800 evaluation board. Question 1 asks to create tasks to blink two LEDs concurrently and change the GLCD display. Question 2 asks to create tasks to read temperature from a sensor and display it on the GLCD by communicating between tasks using a message queue. Question 3 asks to create tasks to simulate a stepper motor driver using 4 LEDs and synchronize task execution using event flags, while concurrently updating the GLCD display. The questions provide the necessary BSP headers to interface with the hardware and ask to explain

Uploaded by

SNOWFLAKE STUDIO
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
861 views11 pages

FreeRTOS Multitasking Application Guide

The document is an assignment submitted by Keerti R Bhadankar for the subject Embedded System Design. It contains 3 questions asking to create multitasking programs using FreeRTOS on the Keil MCB 1800 evaluation board. Question 1 asks to create tasks to blink two LEDs concurrently and change the GLCD display. Question 2 asks to create tasks to read temperature from a sensor and display it on the GLCD by communicating between tasks using a message queue. Question 3 asks to create tasks to simulate a stepper motor driver using 4 LEDs and synchronize task execution using event flags, while concurrently updating the GLCD display. The questions provide the necessary BSP headers to interface with the hardware and ask to explain

Uploaded by

SNOWFLAKE STUDIO
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
  • Task 1: Multitasking Application for LED Blink: Develops a program to demonstrate creation of concurrently executing tasks for blinking two LEDs using freeRTOS.
  • Task 2: Multitasking with Mailbox on Keil MCB 1800: Demonstrates inter-process communication using mailbox on Keil MCB 1800 evaluation board.
  • Task 3: Event Flags and Stepper Motor Control: Synchronizes task execution using event flags to interface with a stepper motor drive.
  • Task 4: ADC and Temperature Sensor Application: Implements an application to read ADC and display temperature sensor values on GLCD using freeRTOS APIs.

R.V.

COLLEGE OF ENGINEERING, Bengaluru – 560059


(Autonomous Institution Affiliated to VTU, Belagavi)

ASSIGNMENT

Submitted by,

KEERTI R BHADANKAR
1RV19EC405
EMBEDDED SYSTEM DESIGN

Bachelor of Engineering
IN
ELECTRONICS & COMMUNICATION ENGINEERING
2020-2021

Department of Electronics & Communication Engineering, 2016-17


1. Create a multitasking application program to demonstrate creation of concurrently
executing tasks. Task1 is expected to control the blinking of two LEDs and Task2 is to
change font and colour of the textual display on GLCD concurrently. Use APIs of
freeRTOS real time kernel. The target for the development is Keil MCB 1800 evaluation
board and supported with following BSPs to interface LEDs. The APIs of GLCD shown
in question 2 can be used. Explain the APIs used to create tasks in freeRTOs.
BSP of LED: LED.h & corresponding LED_LPC18XX.c
SOITION:

[10:43 AM, 1/14/2021] Grishma : #define SIGNAL_BUTTON_PRESS

1 /* USER CODE BEGIN PD

[10:44 AM, 1/14/2021] Grishma : void

StartTask1(void const * argument)

for(;;)

osSignalWait

SIGNAL_BUTTON_PRESS,osWaitForever ););/* USER CODE BEGIN 5

HAL_GPIO_TogglePin

LED_RED_GPIO_Port , LED_RED_Pin

RED LED will be toggled on each button press.

configUSE_TASK_NOTIFICATIONS

ALERNATIVE CODE:

#include "FreeRTOS/include/FreeRTOS.h"

#include "FreeRTOS/include/task.h"

Department of Electronics & Communication Engineering, 2016-17


#include <LPC1850.h> /* LPC1850 FOR MCB 1800 */

#include "GLCD.h"

#include "driver/gpio.h"

void blink_task(void *pvParameter);

void glcd_task(void *pvParameter);

int main (void) {

xTaskCreate(&blink_task,"blink_task",1024,NULL,5,NULL);

printf("blink task started\n");

xTaskCreate(&glcd_task,"glcd_task",128,NULL,2,NULL);

void glcd_task(void *pvParameter)

GLCD_SetFont(0x11);

GLCD_SetBackgroundColor(0x00);

void blink_task(void *pvParameter)

//GPIO_NUM_16 is G16 on board

gpio_set_direction(GPIO_NUM_16,GPIO_MODE_OUTPUT);

printf("Blinking LED on GPIO 16\n");

int cnt=0;

Department of Electronics &amp; Communication Engineering, 2016-17


while(1) {

gpio_set_level(GPIO_NUM_16,cnt%2);

cnt++;

vTaskDelay(1000/portTICK_PERIOD_MS);

2. Create a multitasking program on Keil MCB 1800 evaluation board to demonstrate


IPC using mailbox. Create a task to read temperature value and send to another task
executing concurrently through queues. Display temperature value on GLCD.
Synchronize the execution of tasks. Use APIs of freeRTOS real time kernel. The
following BSPs of sensor & GLCD can be used. Explain the APIs of freeRTOS
used to manage message queues.
BSP of temperature sensor: TH_LM75.h and corresponding TH_LM75.c
SOLUTION: #include <FreeRTOS/include/FreeRTOS.h>
#include <FreeRTOS/include/task.h>
#include <LPC1850.h> /* LPC1850 FOR MCB 1800 */
#include <GLCD.h>
#include <driver/gpio.h>
#include <stdio.h>
osThreadId t_temp; /* assigned task id of task: phase_a */
osThreadId t_lcd; /* assigned task id of task: lcd */
OS_MBX mailbox;
osMailQDe(mailbox,2); // Macro declare mailbox
int16_t tempVal;

__task void TEMPread (void) {


char StringBufSend[20];
for (;;) {
tempVal = TH_GetTemp();
strcpy(StringBufSend," ");
sprintf(StringBufSend, "Digital Value:%d", tempVal);
osMailPut(mailbox, (void *)StringBufSend, 100);
osDelay(200);
}
}

__task void lcd (void) {


void *StringBufRecv;;
for (;;) {
osMailGet (mailbox,&StringBufRecv,0xFFFF);
GLCD_SetBackColor(Blue); /* Set the Text Color */

Department of Electronics &amp; Communication Engineering, 2016-17


GLCD_SetTextColor(White); /* Set the Text Color */
GLCD_DisplayString (4, 1, 1, (unsigned char *)StringBufRecv);
osDelay(400);
}
}
__task void init (void) {
t_temp =osThreadCreate(TEMPread, 0); /* start task phaseA */
t_lcd = osThreadCreate(lcd, 0); /* start task lcd */
osThreadTerminate(osThreadGetId());
}

int main (void) {


TH_Init();
GLCD_Init(); /* Initialize the GLCD */
GLCD_Clear(White); /* Clear the GLCD */
osMailCreate(&mailbox, 100);
osMailCreate(init);
}
ALTERNATE CODE: #include <stdio.h>
#include <io.h>
#include <signal.h>

/* Scheduler includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"

/* Project includes */
#include "clock.h"
#include "leds.h"
#include "uart0.h"
#include "ds1722.h"

/* Function Prototypes */
static void prvSetupHardware( void );
static void vPrintTask(void* pvParameters);
static void vTempTask(void* pvParameters);

int putchar(int c)
{
return uart0_putchar(c);
}

/* Global Variables */
xQueueHandle xQueue;

Department of Electronics &amp; Communication Engineering, 2016-17


/**
* The main function.
*/
int main( void )
{
/* Setup the hardware. */
prvSetupHardware();

/* Create the Queue for communication between the tasks */


xQueue = xQueueCreate( 5, sizeof(uint8_t) );

/* Add the two tasks to the scheduler */


xTaskCreate(vPrintTask, "Print", configMINIMAL_STACK_SIZE, NULL, 1,
NULL );
xTaskCreate(vTempTask, "Temperature", configMINIMAL_STACK_SIZE,
NULL, 1, NULL );

/* Start the scheduler. */


vTaskStartScheduler();

/* As the scheduler has been started we should never get here! */


return 0;
}

/**
* Initialize the main hardware parameters.
*/
static void prvSetupHardware( void )
{
/* Stop the watchdog timer. */
WDTCTL = WDTPW + WDTHOLD;

/* Setup MCLK 8MHz and SMCLK 1MHz */


set_mcu_speed_xt2_mclk_8MHz_smclk_1MHz();

/* Enable Interrupts */
eint();
}

/**
* The Print task function.
* It waits until a sensor value has been put in the queue,
* and prints its value on the uart port.
* \param pvParameter NULL is passed as parameter.
*/

Department of Electronics &amp; Communication Engineering, 2016-17


static void vPrintTask(void* pvParameters)
{
uint8_t temp_meas;
uint16_t samplecount = 0;

/* Initialize the uart port */


uart0_init(UART0_CONFIG_1MHZ_115200);

/* Infinite loop */
while (1)
{
/* Wait until an element is received from the queue */
if (xQueueReceive(xQueue, &temp_meas, portMAX_DELAY))
{
samplecount++;
/* Print the result on the uart port */
printf("Sample #%u: temperature = %u C\r\n", samplecount, temp_meas);
}
}
}

/**
* The temperature measurement task function.
* It reads the temperature from the sensor and puts it on the queue
* \param pvParameters NULL is passed, unused here.
*/
static void vTempTask(void* pvParameters)
{
uint8_t msb;
uint16_t xLastWakeTime = xTaskGetTickCount();

/* The sample period is 1000 ticks, about 1s */


const uint16_t xWakePeriod = 1000;

/* Initialize the temperature sensor */


ds1722_init();
ds1722_set_res(8);
ds1722_sample_cont();

/* Infinite loop */
while(1)
{
/* Read the sensor */
msb = ds1722_read_MSB();

/* Put the read value on the queue */

Department of Electronics &amp; Communication Engineering, 2016-17


xQueueSendToBack(xQueue, &msb, 0);

/* Block until xWakePeriod (=1000) ticks since previous call */


vTaskDelayUntil(&xLastWakeTime, xWakePeriod);
}
}

3. Create a multitasking program to demonstrate event flags to synchronize task execution.


Create four tasks to simulate the operation of stepper motor driver. Use four LEDs blinking
to simulate the activation of the four output driver stages. Create another concurrently
executing task to display text on GLCD. The stepper motor driver tasks are expected to run
sequentially. As the target is KEIL MCB 1800 evaluation board, BSP of LEDs and GLCD
shown in previous questions can be used. Use APIs of freeRTOS to manage signals events
and explain the APIs.
SOLUTION: [11:08 AM, 1/14/2021] Grishma : void check_key (void) __task
{
while(1)
{
if(READ_XP==0)
{
//os_tsk_prio(tsk3, 2);
os_evt_set (0x0003, tsk3);//if key pressed is X+ve or SM1 +ve
key pressed
} //run this tsk

else
os_evt_clr (0x0003, tsk3); //else stop running this tsk

if(READ_XN==0)
os_evt_set (0x0004, tsk4);//if key pressed is X+ve or SM1 +ve
key pressed
else
os_evt_clr (0x0004, tsk4); //else stop running this tsk

if(READ_YP==0)
os_evt_set (0x0005, tsk5);//if key pressed is X+ve or SM1 +ve
key pressed
else
os_evt_clr (0x0005, tsk5);//else stop running this tsk

if(READ_YN==0)
os_evt_set (0x0006, tsk6);//if key pressed is X+ve or SM1 +ve
key pressed
else
os_evt_clr (0x0006, tsk6);//else stop running this tsk

Department of Electronics &amp; Communication Engineering, 2016-17


if(READ_ZP==0)
os_evt_set (0x0007, tsk7);//if key pressed is X+ve or SM1 +ve
key pressed
else
os_evt_clr (0x0007, tsk7);//else stop running this tsk

if(READ_ZN==0)
os_evt_set (0x0009, tsk8);//if key pressed is X+ve or SM1 +ve
key pressed
else
os_evt_clr (0x0009, tsk8);//else stop running this tsk
}

}
/***************************
start tsk if key pressed is x+ve
****************************/
void check_XP (void) __task
{
while (1)
{
//os_evt_wait_and (0x0003, 5000);
DIR_X_SET //set X +ve direction
run_xclock(); //run clk for the SM
[11:09 AM, 1/14/2021] Grishma : void check_XN (void) __task
{
while (1)
{
DIR_X_CLEAR //set X -ve direction
//lcd_print_string(2,2,"XN");
run_xclock(); //run clk for the SM
}
[11:10 AM, 1/14/2021] Grishma : void init_SM_ports()
{
port_pin_dir(1,18,INPUT); //X +VE made as input
port_pin_dir(0,21,INPUT); //X -VE made as input
[11:11 AM, 1/14/2021] Grishma : int main (void)
{
init_SM_ports();
lcd_init();
lcd_print_string(1,0,"test");
os_sys_init (main_tsk);
//return 0;
}

Department of Electronics &amp; Communication Engineering, 2016-17


4. Write an application code to create two concurrently executing tasks. Task 1 function is to
read ADC value and display on GLCD. Task 2 function is used to read value of temperature
sensor and display on GLCD. Use semaphore mechanism in freeRTOS to synchronize the
shared resource. BSP of temperature sensor shown in question 2 can be used. The ADC
interface in supported with following APIs in BSP. Explain the APIs of freeRTOS used to
manage semaphores.
SOLUTION : xQueueHandle queue_led;

void TaskTemp ( void *pvParameters )


{
while(1)
{
queue_led = xQueueCreate( 2, sizeof( uint16_t ) );

uint16_t temp_val;

(void)AD1_Measure(TRUE);
(void)AD1_GetValue16(&temp_val);

xQueueSendToFront(queue_led, &temp_val, 100);


vTaskDelay(100);
}

}
void TaskLed ( void *pvParameters )
{
while(1)
{

uint16_t temp_val;

xQueueReceive(queue_led, &temp_val, 100);

if (temp_val<59000)
{
LED_1_SetVal();
}
else if (temp_val>59000)
{
LED_1_ClrVal();
}

if (temp_val<56000)
{

Department of Electronics &amp; Communication Engineering, 2016-17


LED_2_SetVal();
}
else if (temp_val>56000)
{
LED_2_ClrVal();
}

}
vTaskDelete ( NULL );

Code in main():

xTaskCreate(TaskLed, (signed char *)"tl", 150, NULL, 1, NULL);


xTaskCreate(TaskTemp, (signed char *)"tt", 150, NULL, 1, NULL);
vTaskStartScheduler();
return(0);

Department of Electronics &amp; Communication Engineering, 2016-17

You might also like