0% found this document useful (0 votes)
12 views42 pages

Lect 10

The document is a lecture on Real-time Operating Systems presented by Dr. Mateusz Cholewiński, focusing on dynamic memory allocation, task scheduling, and communication mechanisms in FreeRTOS. It discusses various heap allocation strategies, task priorities, scheduling algorithms, and the use of queues for inter-task communication. The lecture emphasizes the importance of efficient memory management and task scheduling in embedded systems.

Uploaded by

Moji Bake
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)
12 views42 pages

Lect 10

The document is a lecture on Real-time Operating Systems presented by Dr. Mateusz Cholewiński, focusing on dynamic memory allocation, task scheduling, and communication mechanisms in FreeRTOS. It discusses various heap allocation strategies, task priorities, scheduling algorithms, and the use of queues for inter-task communication. The lecture emphasizes the importance of efficient memory management and task scheduling in embedded systems.

Uploaded by

Moji Bake
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
You are on page 1/ 42

Last time

Communication

Real-time Operating Systems


Lecture 10

dr inż. Mateusz Cholewiński

Chair of Cybernetics and Robotics


Faculty of Electronics, Photonics and Microsystems
Wrocław University of Science and Technology

12.06.2025

rys/CyRobekow

dr inż. Mateusz Cholewiński Real-time Operating Systems Lecture 10


Last time
Communication

Scope of presentation

1 Last time

2 Communication

rys/CyRobekow

dr inż. Mateusz Cholewiński Real-time Operating Systems Lecture 10


Dynamic memory allocation
Last time Tasks
Communication Scheduling
Queues

Dynamic memory allocation

FreeRTOS kernel objects are tasks, queues, semaphores and event


groups. These kernel objects are not statically allocated at
compile-time, but dynamically allocated at run-time; FreeRTOS
allocates RAM each time a kernel object is created, and frees RAM
each time a kernel object is deleted.
This policy reduces design and planning effort, simplifies the API,
and minimizes the RAM footprint.

rys/CyRobekow

dr inż. Mateusz Cholewiński Real-time Operating Systems Lecture 10


Dynamic memory allocation
Last time Tasks
Communication Scheduling
Queues

Dynamic memory allocation

Small dedicated embedded systems usually create tasks and other


kernel objects before the scheduler has been started. From this
perspective memory is dynamically allocated before the
applications starts to perform any real-time functionality and stays
allocated through its lifetime.
This gives possibility not to consider any of the more complex
allocation issues, e.g. determinism and fragmentation and allows
to focus on different aspects. Similar to pooling.

rys/CyRobekow

dr inż. Mateusz Cholewiński Real-time Operating Systems Lecture 10


Dynamic memory allocation
Last time Tasks
Communication Scheduling
Queues

Heap

Allocation strategies
heap 1 – subdivide the memory array into smaller blocks,
does not allow to free memory, all tasks have Task Control
Block and stack inside, configTOTAL HEAP SIZE; always
deterministic, cannot fragment memory, very important for
critical and safety systems,
heap 2 – used for backward compatibility, subdivide the
memory array into smaller blocks, allows to free memory,
pvPortMalloc() uses the free block of memory closest in
size to requested size, configTOTAL HEAP SIZE; possible
fragmentation, not deterministic,
rys/CyRobekow

dr inż. Mateusz Cholewiński Real-time Operating Systems Lecture 10


Dynamic memory allocation
Last time Tasks
Communication Scheduling
Queues

Heap

Allocation strategies
heap 3 – uses the standard malloc() and free() functions,
size of the stack is defined by the linker configuration,
suspending scheduler when executed, thread-safe,
heap 4 – subdivide the memory array into smaller blocks,
adjacent free blocks of memory into a single larger block,
configTOTAL HEAP SIZE, not deterministic,
heap 5 – similar to Heap 4, but is not limited to allocating
memory from a single statically declared array – it can
allocate memory from multiple and separated memory spaces;
very useful when RAM provided bu the system is not a single
contiguous block in system’s memory map.
rys/CyRobekow

dr inż. Mateusz Cholewiński Real-time Operating Systems Lecture 10


Dynamic memory allocation
Last time Tasks
Communication Scheduling
Queues

Heap Related Utility Functions

Allocation strategies
xPortGetFreeHeapSize() – returns number of free bytes in
the heap, at the time when is called,
xPortGetMinimumEverFreeHeapSize() – returns the
minimum number of unallocated bytes that have ever existed
in the heap since the FreeRTOS application started executing,
void vApplicationMallocFailedHook( void ) –
pvPortMalloc() when not able to allocate memory returns
NULL; this hook can be used as a fall-back mechanism.

rys/CyRobekow

dr inż. Mateusz Cholewiński Real-time Operating Systems Lecture 10


Dynamic memory allocation
Last time Tasks
Communication Scheduling
Queues

Task priorities

when creating the task uxPriority sets the priority of the


task,
priority can be changed in the runtime,
lowest priority – 0, highest priority configMAX PRIORITIES–1

rys/CyRobekow

dr inż. Mateusz Cholewiński Real-time Operating Systems Lecture 10


Dynamic memory allocation
Last time Tasks
Communication Scheduling
Queues

Priorities

Setting new priority value: The vTaskPrioritySet() API


function can be used to change the priority of any task after
the scheduler has been started. It will be enabled when
INCLUDE vTaskPrioritySet is set to 1.
void vTaskPrioritySet( TaskHandle t pxTask,
UBaseType t uxNewPriority ),
getting priority value:
UBaseType t uxTaskPriorityGet( TaskHandle t
pxTask)

rys/CyRobekow

dr inż. Mateusz Cholewiński Real-time Operating Systems Lecture 10


Dynamic memory allocation
Last time Tasks
Communication Scheduling
Queues

Task scheduling based on priority

generic method – FreeRTOS does not limit the maximum


value to which configMAX PRIORITIES can be set. It is good
to limit it due the amount of RAM to be used and the
increase of worst case execution time; setting
configUSE PORT OPTIMISED TASK SELECTION
architecture optimized method – faster than generic based on
some assembler code, configMAX PRIORITIES does not
effect the worst case execution time, but the value should not
be greater than 32.

state, in turn.
rys/CyRobekow

dr inż. Mateusz Cholewiński Real-time Operating Systems Lecture 10


Dynamic memory allocation
Last time Tasks
Communication Scheduling
Queues

Task scheduling based on priority

The FreeRTOS scheduler will always ensure that the highest


priority task that is able to run is the task selected to enter the
Running state. Where more than one task of the same priority is
able to run, the scheduler will transition each task into and out of
the Running state, in turn.
To be able to execute next task scheduler itself must execute at
the end of each time slice. Hence, it is using a periodic interrupt
called ’tick interrupt’ exactly for this purpose. Time slice length is
configurable using configTICK RATE HZ

rys/CyRobekow

dr inż. Mateusz Cholewiński Real-time Operating Systems Lecture 10


Dynamic memory allocation
Last time Tasks
Communication Scheduling
Queues

Task scheduling based on priority

Figure 1: Mastering the FreeRTOS Real Time Kernel A Hands On


Tutorial Guide rys/CyRobekow

dr inż. Mateusz Cholewiński Real-time Operating Systems Lecture 10


Dynamic memory allocation
Last time Tasks
Communication Scheduling
Queues

Task state machine

Figure 2: Mastering the FreeRTOS— Real Time Kernel rys/CyRobekow

dr inż. Mateusz Cholewiński Real-time Operating Systems Lecture 10


Dynamic memory allocation
Last time Tasks
Communication Scheduling
Queues

Task state machine

blocked state
temporal events – waiting for some time to pass (delay period
or an ansolute time being reached),
synchronization events – event originate form another task or
interrupt (waiting data to arrive on a queue).
suspended state – not available task for scheduler, API:
vTaskSuspend(), vTaskResume(),
xTaskResumeFromISR(),
ready state,

rys/CyRobekow

dr inż. Mateusz Cholewiński Real-time Operating Systems Lecture 10


Dynamic memory allocation
Last time Tasks
Communication Scheduling
Queues

Delays

polling – null loop, where task is polling an incrementing loop


counter until it reaches a fixed number; not effective due to
usage of processor and fixed to some number of iterations,
which indirectly indicate some time,
vTaskDelay() – can be used when INCLUDE vTaskDelay is
set, places task in the Blocked state for a fixed number of tick
interrupts. Task does not use any processing time then.

void vTaskDelay( TickType t xTicksToDelay )

rys/CyRobekow

dr inż. Mateusz Cholewiński Real-time Operating Systems Lecture 10


Dynamic memory allocation
Last time Tasks
Communication Scheduling
Queues

Delays

Figure 3: Mastering the FreeRTOS— Real Time Kernel rys/CyRobekow

dr inż. Mateusz Cholewiński Real-time Operating Systems Lecture 10


Dynamic memory allocation
Last time Tasks
Communication Scheduling
Queues

Task deletion

task can delete itself or other tasks; INCLUDE vTaskDelete


must be set to 1
void vTaskDelete( TaskHandle t pxTaskToDelete)

rys/CyRobekow

dr inż. Mateusz Cholewiński Real-time Operating Systems Lecture 10


Dynamic memory allocation
Last time Tasks
Communication Scheduling
Queues

Scheduling algorithm

”The scheduling algorithm is the software routine that decides


which Ready state task to transition into the Running state.”
configUSE PREEMPTION,
configUSE TIME SLICING,
configUSE TICKLESS IDLE.

rys/CyRobekow

dr inż. Mateusz Cholewiński Real-time Operating Systems Lecture 10


Dynamic memory allocation
Last time Tasks
Communication Scheduling
Queues

Low power mode

For reducing the consumed power idle task hook is used to place
microcontroller in a low power mode. Power saving is limited using
this feature to periodically exit and re-enter low power state.
When the frequency of tick interrupt, which triggers it, is to high
the cost of exiting and re-entering is higher than the savings.
”Tickless idle mode stops periodic tick interrupt during idle periods
(periods when there are no application tasks that are able to
execute), , then makes a correcting adjustment to the RTOS tick
count value when the tick interrupt is restarted.”

rys/CyRobekow

dr inż. Mateusz Cholewiński Real-time Operating Systems Lecture 10


Dynamic memory allocation
Last time Tasks
Communication Scheduling
Queues

Prioritized Pre-emptive Scheduling with Time Slicing

Prioritized Pre-emptive Scheduling with Time Slicing


algorithm used by most small RTOS apps,
does not change priorities of task, do not prevent tasks
themselves to change their or other tasks’ priorities,
provide preemption, involuntarily (without explicitly yielding
or blocking) moving from Running state to Ready state,
shares processing time between tasks of equal priority.

rys/CyRobekow

dr inż. Mateusz Cholewiński Real-time Operating Systems Lecture 10


Dynamic memory allocation
Last time Tasks
Communication Scheduling
Queues

Prioritized Pre-emptive Scheduling with Time Slicing

Figure 4: Mastering the FreeRTOS— Real Time Kernel


rys/CyRobekow

dr inż. Mateusz Cholewiński Real-time Operating Systems Lecture 10


Dynamic memory allocation
Last time Tasks
Communication Scheduling
Queues

Prioritized Pre-emptive Scheduling without Time Slicing

Prioritized Pre-emptive Scheduling without Time Slicing


algorithm used by most small RTOS apps,
does not change priorities of task, do not prevent tasks
themselves to change their or other tasks’ priorities,
provide preemption, involuntarily (without explicitly yielding
or blocking) moving from Running state to Ready state,
does not share processing time between tasks of equal priority.

rys/CyRobekow

dr inż. Mateusz Cholewiński Real-time Operating Systems Lecture 10


Dynamic memory allocation
Last time Tasks
Communication Scheduling
Queues

Prioritized Pre-emptive Scheduling without Time Slicing

Figure 5: Mastering the FreeRTOS— Real Time Kernel


rys/CyRobekow

dr inż. Mateusz Cholewiński Real-time Operating Systems Lecture 10


Dynamic memory allocation
Last time Tasks
Communication Scheduling
Queues

Co-operative Scheduling

configUSE PREEMPTION -- 0,
configUSE TIME SLICING -- Any value,

Co-operative Scheduling
context switch will only occur when the Running state task
enters the Blocked state or,
running state task explicitly yields (manually request a
re-schedule) by calling taskYIELD(),
there is no pre-emption so time slicing can not be used.

rys/CyRobekow

dr inż. Mateusz Cholewiński Real-time Operating Systems Lecture 10


Dynamic memory allocation
Last time Tasks
Communication Scheduling
Queues

Co-operative Scheduling

Figure 6: Mastering the FreeRTOS— Real Time Kernel


rys/CyRobekow

dr inż. Mateusz Cholewiński Real-time Operating Systems Lecture 10


Dynamic memory allocation
Last time Tasks
Communication Scheduling
Queues

Queues

Queues provide
task-to-task,
task-to-interrupt,
interrupt-to-task
communication mechanism.
Queue has a finite number of fixed size data items and maximum
numbers of items a queue can hold is called its length. Length and
size of data item are set when creating queue.
Typically used as FIFO buffers, but it is possible to add write
elements in the beginning or overwrite other elements already
added to queue.
rys/CyRobekow

dr inż. Mateusz Cholewiński Real-time Operating Systems Lecture 10


Dynamic memory allocation
Last time Tasks
Communication Scheduling
Queues

Queues – more
There are 2 methods to add data to queue:
queue by copy - data sent to queue are being copied byte for
byte,
queue by reference - queue holds only a pointer to data sent
to queue.
FreeRTOS uses the first option.

Some aspect:
queues can be accessed by different tasks, ISRs,
when attempting to read from queue, task can enter to
Blocked state,
when attempting to write to queue, task can enter to Blocked
state,
rys/CyRobekow
queues can be organised in the form of sets.
dr inż. Mateusz Cholewiński Real-time Operating Systems Lecture 10
Dynamic memory allocation
Last time Tasks
Communication Scheduling
Queues

Using queues
QueueHandle t xQueueCreate( UBaseType t
uxQueueLength, UBaseType t uxItemSize),
BaseType t xQueueSendToFront( QueueHandle t
xQueue, const void * pvItemToQueue, TickType t
xTicksToWait ),
BaseType t xQueueSendToBack( QueueHandle t xQueue,
const void * pvItemToQueue, TickType t
xTicksToWait ),
pdPASS, errQUEUE FULL,
BaseType t xQueueReceive( QueueHandle t xQueue,
void * const pvBuffer, TickType t xTicksToWait ),
UBaseType t uxQueueMessagesWaiting( QueueHandle t
rys/CyRobekow
xQueue ).
dr inż. Mateusz Cholewiński Real-time Operating Systems Lecture 10
General idea
Last time Communication in embedded systems
Communication Inter System Protocol
Intra System Protocols

General idea

from Latin communicare, meaning ”to share”

rys/CyRobekow

dr inż. Mateusz Cholewiński Real-time Operating Systems Lecture 10


General idea
Last time Communication in embedded systems
Communication Inter System Protocol
Intra System Protocols

Steps
1 The formation of communicative motivation or reason.
2 Message composition (further internal or technical elaboration
on what exactly to express).
3 Message encoding (for example, into digital data, written
text, speech, pictures, gestures and so on).
4 Transmission of the encoded message as a sequence of signals
using a specific channel or medium.
5 Noise sources such as natural forces and in some cases human
activity (both intentional and accidental) begin influencing the
quality of signals propagating from the sender to one or more
receivers.
6 Reception of signals and reassembling of the encoded message
from a sequence of received signals.
7 Decoding of the reassembled encoded message.
rys/CyRobekow
8 Interpretation and making sense of the presumed original
message. dr inż. Mateusz Cholewiński Real-time Operating Systems Lecture 10
General idea
Last time Communication in embedded systems
Communication Inter System Protocol
Intra System Protocols

Communication protocols

Communication protocols are associated with physical layer


describing the signals incorporated, signal strength, hand shaking
mechanism, bus arbitration, device addressing, wired or wireless,
data lines etc.
Types of communication protocols for embedded systems:
Inter System Protocol,
Intra System Protocol.

rys/CyRobekow

dr inż. Mateusz Cholewiński Real-time Operating Systems Lecture 10


General idea
Last time Communication in embedded systems
Communication Inter System Protocol
Intra System Protocols

Inter System Protocol

Communication between two communicating devices, e.g. PC and


µC boards. Uses inter bus system.
Inter System Protocol can be categorized as:
USB Communication protocols,
UART Communication protocols,
USART Communication protocols.

rys/CyRobekow

dr inż. Mateusz Cholewiński Real-time Operating Systems Lecture 10


General idea
Last time Communication in embedded systems
Communication Inter System Protocol
Intra System Protocols

USB

Universal Serial Bus (USB) is a two-wired serial communication


protocol. It allows 127 devices to be connected at any given time.
USB supports plug & play functionality.
USB protocol sends and receives the data serially between host and
external peripheral devices through data signal lines D+ and D-.
Apart from two data lines, USB has VCC and Ground signals to
power up the device. The USB pin out is shown in Figure 4 below.

rys/CyRobekow

dr inż. Mateusz Cholewiński Real-time Operating Systems Lecture 10


General idea
Last time Communication in embedded systems
Communication Inter System Protocol
Intra System Protocols

USB

Advantages:
Fast and simple.
It is of low cost.
Plug and Play hardware.
Disadvantages:
Needs powerful master device.
Specific drivers are required.

rys/CyRobekow

dr inż. Mateusz Cholewiński Real-time Operating Systems Lecture 10


General idea
Last time Communication in embedded systems
Communication Inter System Protocol
Intra System Protocols

UART

Universal Asynchronous Receiver/Transmitter (UART) – a physical


piece of hardware which converts parallel data into serial data. Its
main purpose is to transmit and receive data serially. Consists of
two lines:
RX - receiver,
TX - transmitter.
Data are sent asynchronously, there is no clock signal associated
with transmitting and receiving data. UART uses start and stop
bits in order to indicate a whole packet.
Receiver starts reading data whenever detects start bit. UART
uses half duplex communication. baudrate
rys/CyRobekow

dr inż. Mateusz Cholewiński Real-time Operating Systems Lecture 10


General idea
Last time Communication in embedded systems
Communication Inter System Protocol
Intra System Protocols

USART

Universal Synchronous Asynchronous Receiver/Transmitter


(USART) – identical to UART. Its main purpose is to transmit and
receive data serially. Consists of two lines:
RX - receiver,
TX - transmitter.
Data are sent synchronously, there is clock signal associated with
transmitting and receiving data. UART uses duplex
communication.

rys/CyRobekow

dr inż. Mateusz Cholewiński Real-time Operating Systems Lecture 10


General idea
Last time Communication in embedded systems
Communication Inter System Protocol
Intra System Protocols

UART and USART

Advantages of UART/ USART Communication Protocol:


Clock signal is not required,
Cost effective,
Uses parity bit for error detection,
Requires only 2 wires for data communication.
Disadvantages of UART/ USART Communication Protocol
Doesn’t support multiple master slave functionality,
Baud rate of communicating UART should be within 10
percent of each other.

rys/CyRobekow

dr inż. Mateusz Cholewiński Real-time Operating Systems Lecture 10


General idea
Last time Communication in embedded systems
Communication Inter System Protocol
Intra System Protocols

Intra System Protocols

Group of protocols responsible for communication between


components in PCBs. It allows to connect more devices to µC
system.
More devices equal more power needed in system.
Intra System Protocols provide possibility to securely access data
from the peripherals.

rys/CyRobekow

dr inż. Mateusz Cholewiński Real-time Operating Systems Lecture 10


General idea
Last time Communication in embedded systems
Communication Inter System Protocol
Intra System Protocols

Intra System Protocols

I2C, I2 C,
I3C,
CAN.

rys/CyRobekow

dr inż. Mateusz Cholewiński Real-time Operating Systems Lecture 10


General idea
Last time Communication in embedded systems
Communication Inter System Protocol
Intra System Protocols

Inter Integrated Circuit


Communication Protocol developed by Phillips. It allows to easily
connect different types of external chips to µC. Chips are
connected as memory mapped devices.
Uses SDA (Serial Data Line) and SCL (Serial Clock Line) to carry
data between devices.
I2C is a master – slave protocol. Each slave has defined its unique
address¿ in order to start communication with given slave, master
sends target slave address along with R/W flag. After receiving
slave device will enter the active mode, while others will enter off
state.
Once the slave device is ready, communication starts between
master and slave devices. One bit acknowledgement is replied by
the receiver if transmitter transmits 1 byte (8 bits) of data. A stop
condition is issued at the end of communication between devices.rys/CyRobekow
dr inż. Mateusz Cholewiński Real-time Operating Systems Lecture 10
General idea
Last time Communication in embedded systems
Communication Inter System Protocol
Intra System Protocols

Inter Integrated Circuit

Advantages:
Provides good communication between onboard devices which
are accessed infrequently
Addressing mechanism eases master slave communication
Cost and circuit complexity does not end up on number of
devices
Disadvantage:
limited speed.

rys/CyRobekow

dr inż. Mateusz Cholewiński Real-time Operating Systems Lecture 10


General idea
Last time Communication in embedded systems
Communication Inter System Protocol
Intra System Protocols

Bibliography

1 https://xenomai.org/
2 https://www.freertos.org/index.html
3 https://www.freertos.org/Documentation/161204_
Mastering_the_FreeRTOS_Real_Time_Kernel-A_
Hands-On_Tutorial_Guide.pdf.

rys/CyRobekow

dr inż. Mateusz Cholewiński Real-time Operating Systems Lecture 10

You might also like