Corso di Elettronica dei Sistemi Programmabili

Similar documents
Corso di Elettronica dei Sistemi Programmabili

Context Switching & Task Scheduling

FreeRTOS. A Brief Overview. Christopher Kenna. October 1, Avionics. FreeRTOS 1 / 34

FreeRTOS. A Brief Overview. Christopher Kenna. October 1, Avionics. FreeRTOS 1 / 34

AVR Subroutine Basics

COMP 7860 Embedded Real- Time Systems: Threads

Free-RTOS Implementation

What we will learn. Threaded Systems: Threads. Threads. Threads. Bag of functions & procedures ST OS. AUCs. Threads. AUCs. Processes MT OS MP OS

Embedded Systems and Software

real-time kernel documentation

An Analysis and Description of the Inner Workings of the FreeRTOS Kernel

Module 8: Atmega32 Stack & Subroutine. Stack Pointer Subroutine Call function

Grundlagen Microcontroller Interrupts. Günther Gridling Bettina Weiss

ATmega Interrupts. Reading. The AVR Microcontroller and Embedded Systems using Assembly and C) by Muhammad Ali Mazidi, Sarmad Naimi, and Sepehr Naimi

12. Interrupts and Programmable Multilevel Interrupt Controller

Lecture 4: Mechanism of process execution. Mythili Vutukuru IIT Bombay

Today s Menu. >Use the Internal Register(s) >Use the Program Memory Space >Use the Stack >Use global memory

COMP2121: Microprocessors and Interfacing

EE 308: Microcontrollers

AGH University of Science and Technology Cracow Department of Electronics

Microprocessors & Interfacing

Interrupts (I) Lecturer: Sri Notes by Annie Guo. Week8 1

Implementation of the ART Real Time Micro Kernel

UNIVERSITY OF MANITOBA Final Exam

EE458 - Embedded Systems Exceptions and Interrupts

Microprocessors & Interfacing

Interrupts (II) Lecturer: Sri Parameswaran Notes by: Annie Guo

CODE TIME TECHNOLOGIES. Abassi RTOS. Porting Document. ATmega128 IAR

AN HONORS UNIVERSITY IN MARYLAND UMBC. AvrX. Yousef Ebrahimi Professor Ryan Robucci

CODE TIME TECHNOLOGIES. Abassi RTOS. Porting Document. ATmega IAR

Newbie s Guide to AVR Interrupts

COMP2121: Microprocessors and Interfacing

Embedded Systems. 5. Operating Systems. Lothar Thiele. Computer Engineering and Networks Laboratory

ADAPTING MODE SWITCHES INTO THE HIERARCHICAL SCHEDULING DANIEL SANCHEZ VILLALBA

Interrupts & Interrupt Service Routines (ISRs)

Wireless Sensor Networks (WSN)

AGH University of Science and Technology Cracow Department of Electronics

Concurrent programming: Introduction I

CprE 288 Translating C Control Statements and Function Calls, Loops, Interrupt Processing. Instructors: Dr. Phillip Jones Dr.

Introduction to Real-Time Systems and Multitasking. Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff

Embedding OS in AVR microcontrollers. Prof. Prabhat Ranjan DA-IICT, Gandhinagar

Micrium µc/os II RTOS Introduction EE J. E. Lumpp

Today s Topics. u Thread implementation. l Non-preemptive versus preemptive threads. l Kernel vs. user threads


Engineer To Engineer Note

e-pg Pathshala Subject : Computer Science Paper: Embedded System Module: Interrupt Handling Module No: CS/ES/13 Quadrant 1 e-text

AVR Microcontrollers Architecture

Interrupts and timers

FreeRTOS - Common Task Design Patterns in Multi-tasking Applications

Chapters 2, 3: bits and pieces. Chapters 2 & 3. Chapters 2, 3: bits and pieces. Chapters 2, 3: bits and pieces. Using C. A last word about hardware

CODE TIME TECHNOLOGIES. Abassi RTOS. Porting Document. ATmega128 GCC

Why use an Operating System? Operating System Definition

Processes Prof. James L. Frankel Harvard University. Version of 6:16 PM 10-Feb-2017 Copyright 2017, 2015 James L. Frankel. All rights reserved.

AVR ISA & AVR Programming (I) Lecturer: Sri Parameswaran Notes by: Annie Guo

AVR ISA & AVR Programming (I) Lecturer: Sri Parameswaran Notes by: Annie Guo

Process Context & Interrupts. New process can mess up information in old process. (i.e. what if they both use the same register?)

Computer System Overview. Chapter 1

ARM Cortex-M and RTOSs Are Meant for Each Other

HT8 UL60730 Safety Library -- Program Counter Register Test

8051 Microcontroller Interrupts

Cooperative Multitasking

Today s Topics. u Thread implementation. l Non-preemptive versus preemptive threads. l Kernel vs. user threads

AVR ISA & AVR Programming (I)

Fundamental concept in computation Interrupt execution of a program to handle an event

8-bit Microcontroller with 4K Bytes In-System Programmable Flash. ATtiny40. Preliminary

Chapter 7 Subroutines. Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C

VCC PB2 (SCK/ADC1/T0/PCINT2) PB1 (MISO/AIN1/OC0B/INT0/PCINT1) PB0 (MOSI/AIN0/OC0A/PCINT0)

Mechatronics and Microcomputers. Stipendium Hungaricum 2018/2019 Autumn Semester Szilárd Aradi, PhD

EKT222 Miroprocessor Systems Lab 5

Lesson FreeRTOS + LPC17xx. FreeRTOS & Tasks LPC17xx Memory Map Lab Assignment: FreeRTOS Tasks

Interrupts and Time. Real-Time Systems, Lecture 5. Martina Maggio 28 January Lund University, Department of Automatic Control

Precept 2: Non-preemptive Scheduler. COS 318: Fall 2018

Interrupts and Time. Interrupts. Content. Real-Time Systems, Lecture 5. External Communication. Interrupts. Interrupts

Processes and Operating Systems

Computer System Overview

Copyright 2015 by Joseph E. Moster. All Rights Reserved

Institutionen för systemteknik

Example of a Real-Time Operating System: FreeRTOS

EE 308: Microcontrollers

UNIVERSITY OF CONNECTICUT. ECE 3411 Microprocessor Application Lab: Fall Lab Test III

RT extensions/applications of general-purpose OSs

Common Computer-System and OS Structures

FreeRTOS and LPC Microcontrollers. Richard Barry Design West, San Jose, 2013

hardware interrupts software interrupts

2 Principal Architect EDU,QuEST Global, Thiruvananthapuram

8-bit Microcontroller with 8K Bytes Programmable Flash AT90C8534. Preliminary

CHAPTER 11 INTERRUPTS PROGRAMMING

Real-Time Programming

CIS233J Java Programming II. Threads

Processes and Non-Preemptive Scheduling. Otto J. Anshus

Multitasking on Cortex-M(0) class MCU A deepdive into the Chromium-EC scheduler

Using SRAM in AVR assembler language

Objectives. Explain non-preemptive scheduling Explain step-by-step how a context switch works in TOS

Introduction to the ThreadX Debugger Plugin for the IAR Embedded Workbench C-SPYDebugger

Scheduling - Overview

Lecture Topics. Announcements. Today: Uniprocessor Scheduling (Stallings, chapter ) Next: Advanced Scheduling (Stallings, chapter

Threads Chapter 5 1 Chapter 5

Process Management Rab Nawaz Jadoon

INTERRUPT, TIMER/COUNTER. KONKUK UNIV. VLSI Design Lab. LSI Design Lab

Plan. Regression testing: Demo of how to use the regress.sh script.

Transcription:

Corso di Elettronica dei Sistemi Programmabili Sistemi Operativi Real Time freertos implementation Aprile 2014 Stefano Salvatori 1/24

Sommario RTOS tick Execution context Context switch example 2/24

RTOS Tick When sleeping, a task will specify a time after which it requires 'waking'. When blocking, a task can specify a maximum time it wishes to wait. The FreeRTOS real time kernel measures time using a tick count variable. A timer interrupt (the RTOS tick interrupt) increments the tick count with strict temporal accuracy - allowing the real time kernel to measure time to a resolution of the chosen timer interrupt frequency. 3/24

RTOS Tick At (1) the RTOS idle task is executing. At (2) the RTOS tick occurs, and control transfers to the tick ISR (3). The RTOS tick ISR makes vcontroltask ready to run, and as vcontroltask has a higher priority than the RTOS idle task, switches the context to that of vcontroltask. As the execution context is now that of vcontroltask, exiting the ISR (4) returns control to vcontroltask, which starts executing (5). 4/24

preempitive A context switch occurring in this way is said to be Preemptive, as the interrupted task is preempted without suspending itself voluntarily. The uc port of FreeRTOS uses a compare match event on timer 1 to generate the RTOS tick. In the following, the RTOS tick ISR is described in detail. 5/24

FreeRTOS Tick Code /*--------------------------------------------------*/ /* Interrupt service routine for the RTOS tick. */ void SIG_OUTPUT_COMPARE1A( void ) { /* Call the tick function. */ vportyieldfromtick(); /* Return from the interrupt. If a context switch has occurred this will return to a different task. */ asm volatile ( "reti" ); } /*--------------------------------------------------*/ 6/24

FreeRTOS Tick Code void vportyieldfromtick( void ) { /* This is a naked function so the context is saved. */ portsave_context(); /* Increment the tick count and check to see if the new tick value has caused a delay period to expire. This function call can cause a task to become ready to run. */ vtaskincrementtick(); /* See if a context switch is required. Switch to the context of a task made ready to run by vtaskincrementtick() if it has a priority higher than the interrupted task. */ vtaskswitchcontext(); /* Restore the context. If a context switch has occurred this will restore the context of the task being resumed. */ portrestore_context(); /* Return from this naked function. */ asm volatile ( "ret" ); } /*--------------------------------------------------*/ 7/24

Execution Context 8/24

Saving the Context #define portsave_context() asm volatile ( \ "push r0 \n\t" \ "in \n\t" \ r0, SREG \n\t" \ "cli \n\t" \ "push r0 \n\t" \ "push r1 \n\t" \ "clr r1 \n\t" \ "push r2 \n\t" \ "push r3 \n\t" \ "push r4 \n\t" \ "push r5 \n\t" \... "push r30 \n\t" \ "push r31 \n\t" \ "lds r26, pxcurrenttcb \n\t" \ "lds r26, pxcurrenttcb + 1 \n\t" \ "in r0, SP_L \n\t" \ "st x+, r0 \n\t" \ "in r0, SP_H \n\t" \ "st x+, r0 \n\t" \ ); 9/24

Saving the Context #define portrestore_context() \ asm volatile ( \ "lds r26, pxcurrenttcb \n\t" \ "lds r27, pxcurrenttcb + 1 \n\t" \ "ld r28, x+ \n\t" \ "out SP_L, r28 \n\t" \ "ld r29, x+ \n\t" \ "out SP_H, r29 \n\t" \ "pop r31 \n\t" \ "pop r30 \n\t" \... "pop r1 \n\t" \ "pop r0 \n\t" \ "out SREG, r0 \n\t" \ "pop r0 \n\t" \ ); 10/24

Context Switch Detailed Example 11/24

Detailed example (1) Prior to the RTOS tick interrupt This example starts with TaskA executing. TaskB has previously been suspended so its context has already been stored on the TaskB stack. 12/24

Detailed example (2) The RTOS tick interrupt occurs The RTOS tick occurs just as TaskA is about to execute an LDI instruction. When the interrupt occurs the uc automatically places the current program counter (PC) onto the stack before jumping to the start of the RTOS tick ISR. 13/24

Detailed example (3) The RTOS tick interrupt executes /* Interrupt service routine for the RTOS tick. */ void SIG_OUTPUT_COMPARE1A( void ) { vportyieldfromtick(); asm volatile ( "reti" ); } void vportyieldfromtick( void ) { portsave_context(); vtaskincrementtick(); vtaskswitchcontext(); portrestore_context(); asm volatile ( "ret" ); } 14/24

Detailed example (4) void vportyieldfromtick( void ) { } portsave_context(); vtaskincrementtick(); vtaskswitchcontext(); portrestore_context(); asm volatile ( "ret" ); 2 1 15/24

Detailed example (5) Incrementing the Tick Count vtaskincrementtick() executes after the TaskA context has been saved. For the purposes of this example assume that incrementing the tick count has caused TaskB to become ready to run. void vportyieldfromtick( void ) { portsave_context(); vtaskincrementtick(); vtaskswitchcontext(); portrestore_context(); asm volatile ( "ret" ); } 16/24

Detailed example (6) Incrementing the Tick Count TaskB has a higher priority than TaskA so vtaskswitchcontext() selects TaskB as the task to be given processing time when the ISR completes. void vportyieldfromtick( void ) { portsave_context(); vtaskincrementtick(); vtaskswitchcontext(); portrestore_context(); asm volatile ( "ret" ); } 17/24

Detailed example (7) The TaskB stack pointer is retrieved void vportyieldfromtick( void ) { 1 } portsave_context(); vtaskincrementtick(); vtaskswitchcontext(); portrestore_context(); asm volatile ( "ret" ); 18/24

Detailed example (8) Restore the TaskB context portrestore_context() completes by restoring the TaskB context from its stack into the appropriate processor registers Only the program counter remains on the stack. void vportyieldfromtick( void ) { portsave_context(); vtaskincrementtick(); vtaskswitchcontext(); portrestore_context(); asm volatile ( "ret" ); } 19/24

Detailed example (9) The RTOS tick exits vportyieldfromtick() returns to SIG_OUTPUT_COMPARE1A()... void vportyieldfromtick( void ) { portsave_context(); vtaskincrementtick(); vtaskswitchcontext(); portrestore_context(); asm volatile ( "ret" ); } 20/24

Detailed example (10) The RTOS tick exits vportyieldfromtick() returns to SIG_OUTPUT_COMPARE1A() where the final instruction is a return from interrupt (RETI). A RETI instruction assumes the next value on the stack is a return address placed onto the stack when the interrupt occurred. void SIG_OUTPUT_COMPARE1A ( void ) { vportyieldfromtick(); asm volatile ( "reti" ); } 21/24

Detailed example (11) The RTOS tick exits When the RTOS tick interrupt started the AVR automatically placed the TaskA return address onto the stack - the address of the next instruction to execute in TaskA. The ISR altered the stack pointer so it now points to the TaskB stack. Therefore the return address POP'ed from the stack by the RETI instruction is actually the address of the instruction TaskB was going to execute immediately before it was suspended. The RTOS tick interrupt interrupted TaskA, but is returning to TaskB the context switch is complete! 22/24

Detailed example (12) The RTOS tick interrupt interrupted TaskA, but is returning to TaskB the context switch is complete! 2 1 23/24

Riferimenti Richard Barry, FreeRTOS, at www.freertos.org, chapter 16, pp. 13, 14, 17-27 24/24