Networking for Embedded Systems

Networking for Embedded Systems

CS4101 Introduction to Embedded Systems Lab 9: Task Scheduling Prof. Chung-Ta King Department of Computer Science National Tsing Hua University, Taiwan National Tsing Hua University Introduction In this lab, we will learn - Basic concepts of MQX tasks - Scheduling and management of tasks National Tsing Hua University 2

MQX Tasks Multiple tasks, created from same or different task template, can coexist - MQX maintains each instance by saving its context: program counter, registers, and stack. - Each task has an application-unique 32-bit task ID, which MQX and other tasks use to identify the task. A task is defined by its task descriptor: - Task ID Context: program counter, stack, registers Priority Resources and task-specific parameters

National Tsing Hua University 3 Task Environment National Tsing Hua University 4 Priorities Priorities run from 0 to N - Priority 0: interrupts disabled, 1 highest priority N is set by the highest priority in MQX_Template_List - Idle task runs at N+1

MQX creates one ready queue for each priority up to lowest priority priorities are consecutive Can change priority during runtime _task_set_priority() Any tasks at priority below 6 can mask certain levels of interrupts user tasks should start at 7 or above National Tsing Hua University 5 Hello World 2 on MQX (1/2) #include #include #include /* Task IDs */ #define HELLO_TASK 5

#define WORLD_TASK 6 extern void hello_task(uint_32); extern void world_task(uint_32); const TASK_TEMPLATE_STRUCT MQX_template_list[] = { /* Task Index, Function, Stack, Priority, Name, Attributes, Parameters, Time Slice */ {WORLD_TASK, world_task, 1000, 9, "world", MQX_AUTO_START_TASK, 0, 0}, {HELLO_TASK, hello_task, 1000, 8, "hello", 0,0,0}, { 0 } }; National Tsing Hua University 6 Hello World 2 on MQX (2/2)

/* world_task:create hello_task & print " World " */ void world_task(uint_32 initial_data) { _task_id hello_task_id; hello_task_id = _task_create(0, HELLO_TASK, 0); if (hello_task_id == MQX_NULL_TASK_ID) { printf ("\n Could not create hello_task\n"); } else { printf(" World \n"); } _mqx_exit(0); } void hello_task(uint_32 initial_data) { printf("\n Hello\n"); _task_block(); } National Tsing Hua University

7 Hello World 2 Explained When MQX starts, it creates world_task. The world_task creates hello_task by calling _task_create() with hello_task as a parameter. If _task_create() is successful, it returns the task ID of the new child task; otherwise, it returns MQX_NULL_TASK_ID. The new hello_task task has a higher priority than world_task, it becomes active and prints Hello. The world_task is then scheduled and prints World. National Tsing Hua University

8 _task_create() _task_id _task_create( _processor_number processor_number, _mqx_uint template_index, uint_32 create_parameter) - processor_number: # of the processor where the task is to be created or 0 if created on local - template_index: index of the task template in the processors task template list to use for the child task or 0 to use the template that create_parameter defines - create_parameter: if template_index is not 0, then pointer to the parameters that MQX passes to the child task; otherwise pointer to the task template National Tsing Hua University

9 Task States A task is in one of these logical states: - Blocked: the task is blocked and is not ready, waiting for a condition to be true - Active: the task is ready and is running because it is the highest-priority ready task - Ready: the task is ready, but not running because it is not the highest-priority ready task National Tsing Hua University 10 Task States Task Finishes

Explicit Termination Terminated Active Higher-priority Task becomes Ready Time Slice Expires Interrupt comes in Blocking Call Blocked Context Switch

Task Starts National Tsing Hua University Ready Object Available Timeout Expires 11 Transitions between Task States Tasks can be automatically created when MQX starts; also, any task can create another task by calling

_task_create() or _task_create_blocked() - _task_create() puts the new task in the ready state and the scheduler runs the highest priority task - If _task_create_blocked is used, the task is not ready until _task_ready() is called National Tsing Hua University 12 Common Calls for Task Management _task_abort _task_create _task_create_blocked

_task_destroy _task_errno _task_get_id _task_get_processor _task_restart National Tsing Hua University Terminates the task after running its task exit handler and releasing its resources. Allocates & starts (makes ready) a new task. Allocates a new task in the blocked state. Terminates task after freeing its resources. Gets the task error code for the active task. Gets the task ID. Gets processor # on which a task resides. Restarts a task at top of its function; keeps

same task descriptor, task ID, task stack 13 Task Creation Example {INIT_TASK, init_task, 1500, 11, "init", MQX_AUTO_START_TAS K, 0, 0}, {TASK_A, Task_A, 1500, 10, Task A", 0, 0, 0}, void init_task(void) { _task_create(0,TASK_A,0); ...

... init_task is created when MQX starts _task_ready(Task_B); } void Task_A(void) { ... _task_create_blocked(0,TASK_B,0); ... _task_abort(TASK_A); }

{TASK_B, Task_B, 1500, 9, Task B", 0, 0, 0}, void Task_B(void) { ... _task_abort(TASK_B); CPU Time } National Tsing Hua University 14

Task Scheduling National Tsing Hua University 15 MQX Scheduling Policies FIFO: (default policy) - Active task is the highest-priority task that has been ready for the longest Round Robin: - Active task is the highest-priority task that has been ready for the longest without consuming its time slice - The scheduler is explicitly called after a specified period of time, a time slice. Allows other tasks at same priority level to be active

Tasks in an application may have combinations of FIFO and round-robin with different time slice values. Explicit: using task queues National Tsing Hua University 16 Priority-Based FIFO Scheduling priority high low FIFO list of

ready tasks Ready CPU Scheduler active National Tsing Hua University processor time 17 Priority-Based FIFO Scheduling priority high

low FIFO list of ready tasks Ready CPU Scheduler active National Tsing Hua University processor time 18

Priority-Based FIFO Scheduling priority high low FIFO list of ready tasks Ready CPU Scheduler active

National Tsing Hua University processor time 19 Round-Robin Scheduling Task 1 75ms Same Priority Task 2 50ms Time Slice = 50ms

Task 3 60ms time Ready Task1 T0 Task2 50ms National Tsing Hua University Task3 100ms Task1 Task3

150ms 200ms time 20 Context Switching A task A will stop running and call scheduler if: - It calls a blocking function - Its time slice expires (Round Robin) - A higher priority task is made ready An interrupt occurs Then: - Context of task A is stored

- Context of highest priority task in ready queue is restored Task A is put at the end of ready or wait queue, - If called a blocking function, is put in wait queue - Else is put back in ready queue National Tsing Hua University 21 Preemption Preemption occurs when a higher-priority task becomes ready, and thus becomes active - The previously active task is still ready, but is no longer the active task Occurs when:

- An interrupt handler causes a higher-priority task to become ready - Active task makes a higher-priority task ready National Tsing Hua University 22 Common Calls for Task Scheduling _sched_yield _task_block _task_get_priority _task_ready _task_set_priority _task_start_preemption _sched_set_policy _sched_set_rr_interval

_sched_set_rr_interval_ticks National Tsing Hua University Moves active task to end of its ready queue, which yields the processor to the next ready task of equal priority. Blocks the task. Gets a task's priority. Makes a task ready. Sets a task's priority. Re-enables preemption for the task. Sets the scheduling policy. Sets the time slice in milliseconds. Sets the time slice in tick time. 23

Board Support Package (BSP) National Tsing Hua University 24 What is a Board Support Package? Startup code for the processor Initialize memory, clock, interrupt controller All the drivers needed with the proper settings National Tsing Hua University

25 MQX BSP Initializes microprocessor and board - Clocks, memory interface, core registers Defines board specific parameters - Clocks, memory parameters, interrupt usage, driver parameters/enabling, MQX limits, IO pin definitions, ... Presents board-specific API to IO drivers and application - Timer ISR functions used by MQX scheduler, IO pin initializations Installs and initializes device drivers (selected by user_config.h)

National Tsing Hua University 26 Use GPIO National Tsing Hua University 27 Initialize LED #if defined BSP_LED1 #define PIN_LED1 GPIO_PIN_STRUCT pin_led1[] = { BSP_LED1, GPIO_LIST_END };

#endif PSP, BSPtwrk60d100m User Config user_config.h#define BSPCFG_ENABLE_GPIODEV define BSPCFG_ENABLE_GPIODEV 1 National Tsing Hua University 28 Turn on the LED /* open device for output */ port_file_led1 = fopen("gpio:write", (char *) NULL); /* add pins/signals to the existing file */ ioctl(port_file_led1, GPIO_IOCTL_ADD_PINS, &pin_led1 ); /* write logical 0 to all signals in the file (fast) */ ioctl(port_file_led1, GPIO_IOCTL_WRITE_LOG0, NULL );

National Tsing Hua University 29 Initialize Button #if defined BSP_BUTTON1 #define PIN_BTN1 GPIO_PIN_STRUCT pin_btn1[] = { BSP_BUTTON1, GPIO_LIST_END }; #endif National Tsing Hua University 30

Check Button Status /* open device for input */ port_file_btn1 = fopen("gpio:read", (char *) &pin_btn1 ); /* read status from the input device ioctl(port_file_btn1, GPIO_IOCTL_READ, (char *) &pin_btn1); National Tsing Hua University 31 Create a New MQX Project National Tsing Hua University 32

National Tsing Hua University 33 National Tsing Hua University 34 National Tsing Hua University 35 Basic Lab Create four tasks: main, print, LED1, and LED2, where LED1 and LED2 use the same task template. - Main task initialized IO, creates print task, and yields CPU.

- Print task prints Hello, World!, delays 500, yields CPU. - Main task checks whether button1 or button2 are pressed. If button1 (button2) is pressed, main task creates LED1 (LED2) task to blink red (green) LED until button1 (button2) is released. Hint: - You may like to use functions such as _sched_yield(), _task_block(), and _task_ready() - Set the priority of the tasks carefully National Tsing Hua University Bonus Lab Modify Basic Lab to use the round robin scheduling and perform the same operations. Use the main task to monitor whether button1 and button2 are pressed and released. Then create or

destroy LED1 and LED2 tasks accordingly. - In other words, LED1 and LED2 only need to blink the LED periodically and do not need to check button status. - Hint: set their priority and time slices properly National Tsing Hua University 37

Recently Viewed Presentations

  • Title of Presentation - OASIS

    Title of Presentation - OASIS

    Example CAP Library. Here is an example CAP Library from Google. This CAP Library is written in Java and supports creating and parsing feeds in the Common Alerting Protocol (CAP) format. The Library also supports validation and provides a sample...
  • Chalkboard Challenge - Gwinnett County Public Schools

    Chalkboard Challenge - Gwinnett County Public Schools

    Animals for 100 Amphibian 200 What is a frog Animals for 200 Warm-blooded 300 What are animals that keep the same body temperature when the temperature around them changes? Animals for 300 Examples of reptiles 400 What are snakes, lizards,...

    Unit 7: 1.1 Finding Fair Shares. CCSS: 3.NF.1. SFO: I will find equal parts of a whole and name them with fractions. I will divide an area into equal parts. I will name fraction p
  • Public Facilities Application Basics 12/6/2017 CDBG Field Services

    Public Facilities Application Basics 12/6/2017 CDBG Field Services

    Identify location of photos (address, map) News Reports. Mental Health Directors. AAA Directors. Building Inspectors. Reports and/or Consent orders from EPD. NOTE: Make sure the documentation is relevant to the project!
  • IAB


    required to maintain a cash book or use day books. Cash sales will be processed through a main cash account and sales account in the ledger. Sales and purchases on credit will be processed through sales/purchases account in the ledger...
  • Pixels and Bitmaps

    Pixels and Bitmaps

    However, it doesn't take into account the curvature of the surface. Equal-sized texture patches must be stretched to fit over the surface patch. Linear Texture Mapping Two-Part Mapping 1. Map the texture to a simple three-dimensional intermediate surface sphere, cylindar,...
  • Seizures - Morning Report

    Seizures - Morning Report

    Epilepsy is a condition in which a person has recurrent seizures due to a chronic, underlying process. ... Psychic symptoms (eg, déjà vu, jamais vu, hallucinations, illusions that objects are growing smaller or larger) ... secretions pool in the oropharynx,...
  • Classification of Living Things Scientists estimate that there

    Classification of Living Things Scientists estimate that there

    Classification of Living Things Scientists estimate that there are between 3 million and 100 million species of organisms on Earth. Taxonomists--biologists who specialize in identifying and classifying life on our planet--have named approximately 1.7 million species so far.