This resource describes how to program the myrio in C to perform timer interrupts.

Similar documents
CSE 333 SECTION 9. Threads

CSCI4430 Data Communication and Computer Networks. Pthread Programming. ZHANG, Mi Jan. 26, 2017

CS 153 Lab4 and 5. Kishore Kumar Pusukuri. Kishore Kumar Pusukuri CS 153 Lab4 and 5

Q1: /8 Q2: /30 Q3: /30 Q4: /32. Total: /100

CS 3305 Intro to Threads. Lecture 6

Introduction to pthreads

0x0d2C May your signals all trap May your references be bounded All memory aligned Floats to ints round. remember...

POSIX PTHREADS PROGRAMMING

CS 220: Introduction to Parallel Computing. Condition Variables. Lecture 24

Threads. studykorner.org

Threads. Threads (continued)

pthreads CS449 Fall 2017

Lab Exercise 08 DC motor PID position control

Pthread (9A) Pthread

Section 4: Threads and Context Switching

Lab-exercise. Industrialization & Training in Microelectronics. Cluster: Cluster9 Module: Module1 Threads. Target group: Teacher and student

Operating Systems. Threads and Signals. Amir Ghavam Winter Winter Amir Ghavam

Programming Assignment #4

ENCM 501 Winter 2019 Assignment 9

CS240: Programming in C. Lecture 18: PThreads

Introduction to PThreads and Basic Synchronization

Software API Library. User s Manual V1.3

Section 4: Threads CS162. September 15, Warmup Hello World Vocabulary 2

CSE 333 Section 9 - pthreads

HPCSE - I. «Introduction to multithreading» Panos Hadjidoukas

Concurrency. Johan Montelius KTH

POSIX threads CS 241. February 17, Copyright University of Illinois CS 241 Staff

What is concurrency? Concurrency. What is parallelism? concurrency vs parallelism. Concurrency: (the illusion of) happening at the same time.

Operating systems fundamentals - B06

EPL372 Lab Exercise 2: Threads and pthreads. Εργαστήριο 2. Πέτρος Παναγή

Creating Threads. Programming Details. COMP750 Distributed Systems

CS510 Operating System Foundations. Jonathan Walpole

So far, system calls have had easy syntax. Integer, character string, and structure arguments.

CS170 Operating Systems. Discussion Section Week 4 Project 2 - Thread

Threads. lightweight processes

Thread and Synchronization

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Lab Exercise 07 DC motor PI velocity control

CS333 Intro to Operating Systems. Jonathan Walpole

Chapter 09. Programming in Assembly

Outline. CS4254 Computer Network Architecture and Programming. Introduction 2/4. Introduction 1/4. Dr. Ayman A. Abdel-Hamid.

ECE 448 Lecture 9. Bare Metal System Software Development

SpiNNaker Application Programming Interface (API)

CS 345 Operating Systems. Tutorial 2: Treasure Room Simulation Threads, Shared Memory, Synchronization

Structures, Unions Alignment, Padding, Bit Fields Access, Initialization Compound Literals Opaque Structures Summary. Structures

TCSS 422: OPERATING SYSTEMS

pthreads Announcement Reminder: SMP1 due today Reminder: Please keep up with the reading assignments (see class webpage)

CMPSC 311- Introduction to Systems Programming Module: Concurrency

QNX Software Development Platform 6.6. Input Events Library Reference

Chapter 6 PROGRAMMING THE TIMERS

ANSI/IEEE POSIX Standard Thread management

CMPSC 311- Introduction to Systems Programming Module: Concurrency

Multicore and Multiprocessor Systems: Part I

CS-345 Operating Systems. Tutorial 2: Grocer-Client Threads, Shared Memory, Synchronization

POSIX Threads. HUJI Spring 2011

Data Structures. Home

CS510 Operating System Foundations. Jonathan Walpole

CMPSC 311- Introduction to Systems Programming Module: Concurrency

USB MAX3421 Host Controller User Guide

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Generalised User Interface for Embedded Applications using an LCD screen and keypad.

Embedded Systems. Input/Output Programming

PThreads in a Nutshell

Midterm Exam #1 September 28, 2016 CS162 Operating Systems

LSN 13 Linux Concurrency Mechanisms

IPv4 and ipv6 INTEROPERABILITY

Roadmap. Concurrent Programming. Concurrent Programming. Motivation. Why Threads? Tevfik Ko!ar. CSC Systems Programming Fall 2010

Agenda. Process vs Thread. ! POSIX Threads Programming. Picture source:

CSE 539S, Spring 2015 Concepts in Mul9core Compu9ng Lecture 1: Introduc9on

Threaded Programming. Lecture 9: Alternatives to OpenMP

Operating systems. Lecture 10

Pthreads. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CS 326: Operating Systems. Process Execution. Lecture 5

Writing TMS320C8x PP Code Under the Multitasking Executive

CS 261 Fall Mike Lam, Professor. Threads

Advanced Pointer & Data Storage

A Thread is an independent stream of instructions that can be schedule to run as such by the OS. Think of a thread as a procedure that runs

Roadmap. Concurrent Programming. Concurrent Programming. Communication Between Tasks. Motivation. Tevfik Ko!ar

IMPLEMENTATION OF SIGNAL HANDLING. CS124 Operating Systems Fall , Lecture 15

CSC Systems Programming Fall Lecture - XVIII Concurrent Programming. Tevfik Ko!ar. Louisiana State University. November 11 th, 2008

User Space Multithreading. Computer Science, University of Warwick

EL6483: Brief Overview of C Programming Language

1. Overview Ethernet FIT Module Outline of the API API Information... 5

Computer Science 162, Fall 2014 David Culler University of California, Berkeley Midterm 1 September 29, 2014

Lecture 4. Threads vs. Processes. fork() Threads. Pthreads. Threads in C. Thread Programming January 21, 2005

Threads (light weight processes) Chester Rebeiro IIT Madras

PS3 Programming. Week 2. PPE and SPE The SPE runtime management library (libspe)

LatticeMico32 GPIO. Version. Features

VueMetrix Firmware Uploader

CSC 1600: Chapter 6. Synchronizing Threads. Semaphores " Review: Multi-Threaded Processes"

Exercise (could be a quiz) Solution. Concurrent Programming. Roadmap. Tevfik Koşar. CSE 421/521 - Operating Systems Fall Lecture - IV Threads

Unix. Threads & Concurrency. Erick Fredj Computer Science The Jerusalem College of Technology

MPLAB Harmony Help - Test Harness Library

DM6420 Driver for Linux User s Manual Version 2.01.xx

MPLAB Harmony Test Libraries Help

Interrupt handling. Purpose. Interrupts. Computer Organization

CS444 1/28/05. Lab 03

MPLAB Harmony Help - Sample Library

NOTE: Debug and DebugSingle are the only MPI library configurations that will produce trace output.

5.Coding for 64-Bit Programs

Transcription:

Resource 07 Timer interrupts This resource describes how to program the myrio in C to perform timer interrupts. C.07.1 Main thread: background Initializing the timer interrupt is similar to initializing the digital input interrupt. We will use a separate thread to produce interrupts at periodic intervals. Within main, we will configure the timer interrupt and create a new thread to respond when the interrupt occurs. The two threads communicate through a globally defined thread resource structure: typedef struct { NiFpga_IrqContext irqcontext; // IRQ context reserved NiFpga_Bool irqthreadrdy; // IRQ thread ready flag } ThreadResource; National Instruments provides C functions to set up the timer interrupt request (IRQ). C.07.1.1 Register the Timer IRQ The first of these functions reserves the interrupt from the FPGA and configures the timer and IRQ. Its prototype is: int32_t Irq_RegisterTimerIrq( MyRio_IrqTimer* irqchannel, NiFpga_IrqContext* irqcontext, uint32_t timeout where the three input arguments are: 1. irqchannel: A pointer to a structure containing the registers and settings for the IRQ I/O to modify; defined in TimerIRQ.h as: typedef struct { uint32_t timerwrite; // Timer IRQ interval register uint32_t timerset; // Timer IRQ setting register Irq_Channel timerchannel; // Timer IRQ supported I/O } MyRio_IrqTimer; updated 29 April 2018, 19:10:29 C.07 1

2. irqcontext: a pointer to a context variable identifying the interrupt to be reserved. It is the first component of the thread resources structure. 3. timeout: the timeout interval in µs. The returned value is 0 for success. C.07.1.2 Create the interrupt thread A new thread must be configured to service the timer interrupt. In main we will use pthread_create to set up that thread. Its prototype is: int pthread_create( pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg where the four input arguments are: 1. thread: a pointer to a thread identifier. 2. attr: a pointer to thread attributes. In our case, use NULL to apply the default attributes. 3. start_routine: the name of the starting function in the new thread. 4. arg: the sole argument to be passed to the new thread. In our case, it will be a pointer to the thread resource structure defined above and used in the second argument of Irq_RegisterDiIrq. This function also returns 0 for success. C.07.2 Main thread: our case We can combine these ideas into a portion of the main code needed to initialize the Timer IRQ. 2 For interrupts triggered by the timer in the FPGA, we have: 2 The IRQ settings symbols associated with the timer interrupt, are defined in the header file: TimerIRQ.h. updated 29 April 2018, 19:10:29 C.07 2

int32_t status; MyRio_IrqTimer irqtimer0; ThreadResource irqthread0; pthread_t thread; // Registers corresponding to the IRQ channel irqtimer0.timerwrite = IRQTIMERWRITE; irqtimer0.timerset = IRQTIMERSETTIME; timeoutvalue = 5; status = Irq_RegisterTimerIrq( &irqtimer0, &irqthread0.irqcontext, timeoutvalue // Set the indicator to allow the new thread. irqthread0.irqthreadrdy = NiFpga_True; // Create new thread to catch the IRQ. status = pthread_create( &thread, NULL, Timer_Irq_Thread, &irqthread0 Other main tasks go here. After the tasks of main are completed, it should signal the new thread to terminate by setting the irqthreadrdy flag in the ThreadResource structure. Then it should wait for the thread to terminate. For example, irqthread0.irqthreadrdy = NiFpga_False; status = pthread_join(thread, NULL Finally, the timer interrupt must be unregistered: status = Irq_UnregisterTimerIrq( &irqtimer0, irqthread0.irqcontext using the same arguments from above. updated 29 April 2018, 19:10:29 C.07 3

C.07.3 The interrupt thread This is the separate thread that was named and started by the pthread_create function. Its overall task is to perform any necessary function in response to the interrupt. This thread will run until signaled to stop by main. The new thread is the starting routine specified in the pthread_create function called in main. In our case: void *Timer_Irq_Thread(void* resource). The first step in Timer_Irq_Thread is to cast its input argument (passed as void *) into appropriate form. In our case, we cast the resource argument back to a ThreadResource structure. For example, declare first step ThreadResource* threadresource = (ThreadResource*) resource; The second step is to enter a while loop. Two functions are performed each time through the loop, as described in Algorithm 4. second step Algorithm 4 ISR thread loop pseudocode while the main thread has not signaled this thread to stop do wait for the occurrence (or timeout) of the IRQ schedule the next interrupt if the Timer IRQ has been asserted then perform operations to service the interrupt acknowledge the interrupt end if end while The while loop should continue until the irqthreadrdy flag (set in main) indicates that the thread should end. For example, 1. Use the Irq_Wait function to pause the loop while waiting for the interrupt. For our case the call might be, with TIMERIRQNO a constant defining the Timer IRQ s IRQ number, defined in TimerIRQ.h: uint32_t irqassert = 0; Irq_Wait( threadresource->irqcontext, TIMERIRQNO, &irqassert, updated 29 April 2018, 19:10:29 C.07 4

(NiFpga_Bool*) &(threadresource->irqthreadrdy) Notice that it receives the ThreadResource context and Timer IRQ number information, and returns the irqthreadrdy flag set in the main thread. Schedule the next interrupt by writing the time interval into the IRQTIMERWRITE register, and setting the IRQTIMERSETTIME flag. That is, NiFpga_WriteU32( myrio_session, IRQTIMERWRITE, timeoutvalue NiFpga_WriteBool( myrio_session, IRQTIMERSETTIME, NiFpga_True The timeoutvalue is the number of µs (uint32_t) until the next interrupt. The myrio_session used in these functions should be declared within this timer thread. That is, extern NiFpga_Session myrio_session; This variable was defined when you called MyRio_Open in the main thread. 2. Because the Irq_Wait times out after 100 ms, we must check the irqassert flag to see if the Timer IRQ has been asserted. In addition, after the interrupt is serviced, it must be acknowledged to the scheduler. For example, if(irqassert & (1 << TIMERIRQNO)) { // Bit mask // Your interrupt service code here Irq_Acknowledge(irqAssert } third step In the third step (after the end of the loop) we terminate the new thread, and return from the function: updated 29 April 2018, 19:10:29 C.07 5

pthread_exit(null return NULL; updated 29 April 2018, 19:10:29 C.07 6