My first Raspberry Pi tutorial in C. Christophe Delord

Size: px
Start display at page:

Download "My first Raspberry Pi tutorial in C. Christophe Delord"

Transcription

1 My first Raspberry Pi tutorial in C Christophe Delord Sunday 22 October 2017

2 2

3 Contents 1 Availability COPYRIGHT This book is now freely available About this book 9 3 Introduction Licenses Source code wiringpi Hardware Software Generic definitions wiringpi library State machines Clock state machine interface Clock state machine implementation Clock example main loop Compilation on a PC Execution on a PC Compilation on a Raspberry Pi Execution on a Raspberry Pi What s next

4 4 CONTENTS 4 Blinking LED Hardware Software Blinking LED state machine interface Blinking LED state machine implementation Blinking LED example main loop Compilation on a PC Execution on a PC Compilation on a Raspberry Pi Execution on a Raspberry Pi What s next PWM output (music generator) Hardware Software Music state machine interface Music state machine implementation Music generator example main loop Compilation on a PC Execution on a PC Compilation on a Raspberry Pi Execution on a Raspberry Pi What s next Light sensor input Hardware Software Light sensor state machine interface Light sensor state machine interface Music state machine Main loop Compilation on a PC

5 CONTENTS Execution on a PC Compilation on a Raspberry Pi Execution on a Raspberry Pi What s next A Appendices 63 A.1 Generic definitions A.1.1 demo.h A.1.2 demo A.2 wiringpi stub A.2.1 wiringpi.h A.2.2 wiringpi.c A.3 STATUS state machine A.3.1 status.h A.3.2 status.c A.4 Clock state machine A.4.1 clock.h A.4.2 clock.c A.5 Blinking LED state machine A.5.1 blink.h A.5.2 blink.c A.6 Music generator state machine A.6.1 music.h A.6.2 music.c A.7 Light sensor state machine A.7.1 light.h A.7.2 light.c A.8 ex1-clock.c: basic state machine demo A.9 ex2-blinkingled.c: blinking LED demo A.10 ex3-pwmoutput.c: music generator demo A.11 ex4-lightsensor.c: light sensor demo

6 6 CONTENTS

7 Chapter 1 Availability 1.1 COPYRIGHT This book is copyrighted by Christophe Delord (CDSoft.fr/essays). available at lulu.com: It is PDF version (ISBN ): My first Raspberry Pi tutorial in C Printed version (ISBN ): My first Raspberry Pi tutorial in C 1.2 This book is now freely available This book has been initially published on lulu.com. It is now fully and freely available on HTML version: PDF version: You can download and read this tutorial for free. If you like it, please consider making some donation here ( or buying it on lulu.com to support my work. 7

8 8 CHAPTER 1. AVAILABILITY

9 Chapter 2 About this book This book has been written using some free tools and formats: PP: a general text preprocessor written in Haskell and designed with Pandoc and Markdown in mind. Pandoc: a universal document converter (to produce HTML and PDF versions of the book). Inkscape GIMP GNU Make gcc Debian Raspbian If you are interested by the source code of the book, please contact me at The source code of this book is also a nice example of literate programming with PP: the same source files are used to produce the documentation (in PDF and HTML formats) as well as the source code of the examples that can be compiled for both the Linux simulated platform and the actual Raspberry Pi device. 9

10 10 CHAPTER 2. ABOUT THIS BOOK

11 Chapter 3 Introduction The aim of this tutorial is simply to learn how to program Raspberry Pi 3 GPIOs in C as well as to show how to write clean and efficient C code for the Raspberry Pi. The circuit we are going to realize is quite simple. It provides three functions: 1. The unavoidable blinking LED (aka Raspberry Pi Hello World! project). The purpose of the LED is to indicate that the demo is running. It uses a single output GPIO. 2. A speaker playing some horrible random music. The speaker is driven by a PWM output GPIO. A second LED (called music LED ) is turned on when the frequency is rising and off when the frequency is decreasing. 3. A light sensor is used to switch on and off the music. The music stops when the light level is low to let people sleep! 11

12 12 CHAPTER 3. INTRODUCTION Figure 3.1: Final project The software is based on state machines and written in C. The goal is to provide micro controller low level like software instead of using higher level languages such as Python. C gives better performances and real time characteristics. The reader is assumed to have some basic knowledge on GNU/Linux, bash, gcc Licenses Source code The source code available in this tutorial is copyrighted under the GNU General Public License. Copyright (C) 2016 Christophe Delord This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published

13 3.2. HARDWARE 13 by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PUR- POSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see wiringpi This tutorial uses the wiringpi library. The source code of wiringpi can be found here: /* * wiringpi.h: * Arduino like Wiring library for the Raspberry Pi. * Copyright (c) Gordon Henderson *********************************************************************** * This file is part of wiringpi: * * * wiringpi is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * wiringpi is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with wiringpi. If not, see < *********************************************************************** */ 3.2 Hardware This demo runs on a Raspberry Pi 3 but I guess it can run on a Raspberry Pi 2 as well. We will also simulate the hardware to debug and run the software on a regular GNU/Linux PC before running it on a real Raspberry Pi.

14 14 CHAPTER 3. INTRODUCTION 3.3 Software This dummy example shows the structure of a classical state machine for embedded softwares. This implementation has a taste of object oriented programmation but with static allocation only (no dynamic object allocation, no polymorphism or inheritance à la C++). All (hard) real time applications must have a clean and deterministic time model. In this tutorial we will define a simple model based on cooperative multitasking. Contrary to preemptive multitasking, cooperative tasks run for some period of time and shall give control back to the sequencer deterministically. Tasks are not arbitrarily interrupted by the sequencer. This model makes it easier to distribute time to tasks and control shared global variables. Each task is a state machine. The state machines are made of a state (a set of values that represent the current state) and a set of functions (or methods) that transform the state. During each time slice, the sequencer calls every state machines once.

15 3.3. SOFTWARE 15 Figure 3.2: Generic software architecture - cooperative multitasking

16 16 CHAPTER 3. INTRODUCTION Generic definitions First we need some generic definitions and tools demo.h /* file: demo.h */ /* */ /* Some global definitions for theses tutorials. */ #ifndef DEMO #define DEMO #include <stdio.h> #include <stdlib.h> /* wiringpi library used to access Raspberry Pi GPIOs */ #include "wiringpi.h" /* Time units (all times are internally in µs) */ #define US * 1 #define MS * 1000 US #define S * 1000 MS /* The base cycle is the period of the sequencer. */ #define CYCLE (10 MS) /* Routines to be defined by the application. */ /* init is called once to initialise the state machines */ extern void init(); /* isalive is called periodically to check if the state machines */ /* are still alive */ extern int isalive(); /* run is called periodically to run one step of every state machines */ extern void run(); /* cleanup is called once when the system is not alive */ extern void cleanup(); /* interrupted is called when an interrupt signal is received. */

17 3.3. SOFTWARE 17 /* interrupt signals are SIGINT, SIGQUIT, SIGABRT and SIGTERM. */ extern void interrupted(); #endif demo.c /* file: demo.h */ /* */ /* Sequencer for the state machines of theses tutorials. */ #include <signal.h> #include <string.h> #include <sys/time.h> #include <unistd.h> #include "demo.h" /* signal_handler is called when a signal is received. It calls */ /* the `interrupted` function defined by the application (which */ /* in turns calls the "interrupted" functions of the state */ /* machines. */ static void signal_handler(int sig) printf("\nsignal received: %s\n", strsignal(sig)); interrupted(); /* timer_handler is called periodically. The period is CYCLE. */ /* The main loop is driven by a timer which is more accurate than */ /* a simple wait. */ /* timer_handler calls: */ /* - `run` to execute one step of every state machines */ /* - `isalive` to check that the system is still alive */ /* - `cleanup` and exits when the system is not alive anymore */ static void timer_handler(int signum) /* run one step */ run(); /* check if the program is still alive */ if (!isalive())

18 18 CHAPTER 3. INTRODUCTION /* stop the program when it is not alive anymore */ cleanup(); exit(exit_success); /* wait for the next cycle */ /* The main function initializes the state machines, */ /* starts a periodic timer to run the state machines */ /* and undefinitely waits for the system to exit. */ int main(void) /* Initialisation of the wiringpi library to access GPIOs */ wiringpisetup(); /* Interruption signal handler */ signal(sigint, signal_handler); signal(sigquit, signal_handler); signal(sigabrt, signal_handler); signal(sigterm, signal_handler); /* Some information about the hardware */ int model, rev, mem, maker, overvolted; piboardid(&model, &rev, &mem, &maker, &overvolted); printf("model = %s\n", pimodelnames[model]); printf("rev = %s\n", pirevisionnames[rev]); printf("mem = %d Mb\n", mem); printf("maker = %s\n", pimakernames[maker]); printf("over volted = %s\n", overvolted? "yes" : "no"); printf("\n"); /* state machine initializations */ init(); /* Install timer_handler as the signal handler for SIGALRM. */ struct sigaction sa; memset (&sa, 0, sizeof (sa)); sa.sa_handler = &timer_handler; sigaction(sigalrm, &sa, NULL); /* Configure the timer to expire after CYCLE µs... */ struct itimerval itimer; itimer.it_value.tv_sec = 0; itimer.it_value.tv_usec = CYCLE; /*... and every CYCLE µs after that. */

19 3.3. SOFTWARE 19 itimer.it_interval.tv_sec = 0; itimer.it_interval.tv_usec = CYCLE; /* Start the timer. */ setitimer (ITIMER_REAL, &itimer, NULL); /* Main loop */ while (1) /* This loop could be used as a background loop */ sleep(1000); return EXIT_SUCCESS; wiringpi library WiringPi is a C library that brings the simplicity of Arduino to the powerful world of Raspberry Pi. To simulate the Raspberry Pi on a GNU/Linux PC, we need to stub this library. I/O functions on a PC will be simulated. Of course on a Raspberry Pi, the real library will be used. The source code of the wiringpi stub is in the appendix wiringpi stub State machines Instead of abusing Linux facilities, we will design software with state machines. This is a clean and deterministic way to describe the behaviour of several tasks (some kind of processes). They are simple and predictable, which is not always the case with general OS. We don t need here the richness of GNU/Linux. We are going to use a cooperative multitasking model instead of a preemptive model. A state machine is a set of functions (methods) that initialize and manage the state of the state machine by interacting with other machines or with the environment. A state is described by a structured type. Each instance of a state machine has its own state in a global variable.

20 20 CHAPTER 3. INTRODUCTION Clock state machine interface This example defines a clock that just counts ticks. Each clock can have its own period. /* file: clock.h */ /* */ /* Clock state machine. Clocks can count execution cycles */ /* and generate rising edges. */ /* A clock is defined by a period and an offset (date of the */ /* first rising edge) in µs. */ #ifndef CLOCK_STATE_MACHINE #define CLOCK_STATE_MACHINE #include "demo.h" /* Clock state */ typedef struct int period_us; /* period of the clock in µs */ int offset_us; /* offset of the clock in µs */ int t; /* time in µs since the start of the period */ int time_us; /* current time in µs */ int ticks; /* number of cycles since the clock started */ CLOCK_t; /* Clock initialisation method */ void CLOCK_init(CLOCK_t *c, int period_us, int offset_us); /* Clock main loop method */ void CLOCK_run(CLOCK_t *c); double CLOCK_time(CLOCK_t *c); /* current time in seconds */ int CLOCK_time_ms(CLOCK_t *c); /* current time in ms */ int CLOCK_time_us(CLOCK_t *c); /* current time in µs */ int CLOCK_edge(CLOCK_t *c); /* is the current cycle a rising edge */ int CLOCK_ticks(CLOCK_t *c); /* number of cycles since the beginning */ double CLOCK_ratio(CLOCK_t *c); /* position within the current cycle */ #endif

21 3.3. SOFTWARE Clock state machine implementation /* file: clock.c */ /* */ /* Clock state machine: implementation (see clock.h) */ #include "clock.h" void CLOCK_init(CLOCK_t *c, int period_us, int offset_us) c->period_us = period_us; c->offset_us = offset_us; c->t = offset_us > 0? period_us - offset_us : 0; c->time_us = 0; c->ticks = 0; void CLOCK_run(CLOCK_t *c) c->ticks++; c->time_us += CYCLE; c->t += CYCLE; if (c->t >= c->period_us) c->t = 0; double CLOCK_time(CLOCK_t *c) return c->time_us/1e6; int CLOCK_time_ms(CLOCK_t *c) return c->time_us/1000; int CLOCK_time_us(CLOCK_t *c) return c->time_us; int CLOCK_edge(CLOCK_t *c) return c->t == 0; int CLOCK_ticks(CLOCK_t *c) return c->ticks; double CLOCK_ratio(CLOCK_t *c) return ((double)c->t) / c->period_us; Clock example main loop The program is made of two main functions: init initializes all the state machines. run runs all the state machines in one cycle

22 22 CHAPTER 3. INTRODUCTION The main function calls init and starts a timer to periodically call run. The function isalive shall return FALSE to stop the execution. In this example, we instantiate two clocks. The first one has a period of 500 ms and the first edge is shifted by 250 ms. The second one has a period of 1 s and no offset. Figure 3.3: Clock example timing /* file: ex1-clock.c */ /* */ /* Clock usage example. */ #include "demo.h" #include "clock.h" /* Clocks are statically allocated "objects" */ CLOCK_t c1, c2; void init() /* Initialisation of the state machines */ CLOCK_init(&c1, 500 MS, 250 MS); CLOCK_init(&c2, 1 S, 0 MS); int isalive() /* Let's run for 5 seconds... */ return CLOCK_time(&c1) < 5.0;

23 3.4. COMPILATION ON A PC 23 void run() /* Some outputs to see that the clocks are running */ int e1 = CLOCK_edge(&c1); int e2 = CLOCK_edge(&c2); if (e1 e2) /* print "CLOCK" in upper case on rising edges */ printf("[%s 1: %7.3f / %9d] [%s 2: %7.3f / %9d]\n", e1? "CLOCK" : "clock", CLOCK_time(&c1), CLOCK_ticks(&c1), e2? "CLOCK" : "clock", CLOCK_time(&c2), CLOCK_ticks(&c2)); /* Execute one step */ CLOCK_run(&c1); CLOCK_run(&c2); void interrupted() void cleanup() 3.4 Compilation on a PC First we are going to test this dummy example on a regular PC. The program can be compiled on GNU/Linux (maybe on Cygwin as well) with gcc using the stubbed wiringpi library: gcc -Wall -Werror \ -o ex1-clock \ ex1-clock.c demo.c clock.c wiringpi.c Some quick explanations about this command line: -Wall reports all warning messages. This is a good practice to fix all warnings. Warnings are sometimes real bugs. Fixing them as early as possible is the best way not to lose time tracking some tricky bugs when testing a software.

24 24 CHAPTER 3. INTRODUCTION -Werror turns warning messages into errors. Warnings will make the compilation fail. Just to be sure not to forget some warnings! -o ex1-clock is the name of the executable. ex1-clock.c is the source of the example. clock.c is the source of the clock state machine. wiringpi.c is the source of the stubbed wiringpi library. demo.c is the source of the sequencer. It also contains the main function. 3.5 Execution on a PC The executable can be run from the command line. ex1-clock will run for 5 seconds, printing a line when a rising edge is detected on one of the clocks (c1 or c2). $./ex1-clock model = Stubbed wiringpi rev = n/a mem = 0 Mb maker = CDSoft.fr over volted = no [clock 1: / 0] [CLOCK 2: / 0] [CLOCK 1: / 25] [clock 2: / 25] [CLOCK 1: / 75] [clock 2: / 75] [clock 1: / 100] [CLOCK 2: / 100] [CLOCK 1: / 125] [clock 2: / 125] [CLOCK 1: / 175] [clock 2: / 175] [clock 1: / 200] [CLOCK 2: / 200] [CLOCK 1: / 225] [clock 2: / 225] [CLOCK 1: / 275] [clock 2: / 275] [clock 1: / 300] [CLOCK 2: / 300] [CLOCK 1: / 325] [clock 2: / 325] [CLOCK 1: / 375] [clock 2: / 375] [clock 1: / 400] [CLOCK 2: / 400] [CLOCK 1: / 425] [clock 2: / 425] [CLOCK 1: / 475] [clock 2: / 475] 3.6 Compilation on a Raspberry Pi A similar command line can be used on a Raspberry Pi (I m using Raspbian). gcc -Wall -Werror \

25 3.7. EXECUTION ON A RASPBERRY PI 25 -o ex1-clock \ ex1-clock.c demo.c clock.c \ -lwiringpi The main difference is that we use the real wiringpi library (-lwiringpi) instead of the stub. 3.7 Execution on a Raspberry Pi This example is not specific to the Raspberry Pi as it doesn t uses any Raspberry Pi input/output. Except from the hardware detection, the program should print the same clock timings. The program uses the real wiringpi library so it requires root privileges. $ sudo./ex1-clock model = Model 2 rev = 1.1 mem = 1024 Mb maker = Sony over volted = yes [clock 1: / 0] [CLOCK 2: / 0] [CLOCK 1: / 25] [clock 2: / 25] [CLOCK 1: / 75] [clock 2: / 75] [clock 1: / 100] [CLOCK 2: / 100] [CLOCK 1: / 125] [clock 2: / 125] [CLOCK 1: / 175] [clock 2: / 175] [clock 1: / 200] [CLOCK 2: / 200] [CLOCK 1: / 225] [clock 2: / 225] [CLOCK 1: / 275] [clock 2: / 275] [clock 1: / 300] [CLOCK 2: / 300] [CLOCK 1: / 325] [clock 2: / 325] [CLOCK 1: / 375] [clock 2: / 375] [clock 1: / 400] [CLOCK 2: / 400] [CLOCK 1: / 425] [clock 2: / 425] [CLOCK 1: / 475] [clock 2: / 475] 3.8 What s next The following chapters will use the same kind of software architecture. Of course we will also see how to use the Inputs/Outputs of the Raspberry Pi to interact with the real world.

26 26 CHAPTER 3. INTRODUCTION Printing messages on a terminal may be some kind of fun... But blinking a LED is way more amusing!

27 Chapter 4 Blinking LED This chapter shows: how to connect a LED to the Raspberry Pi how to write a software in C to turn the LED on and off 4.1 Hardware The electrical wiring for this project is quite simple. Here is what you need: a Raspberry Pi (I have used a Raspberry Pi 3 model) with its power supply a LED a 330 Ohm resistor a breadboard and some wires The LED and the resistor are connected to an output GPIO. 27

28 28 CHAPTER 4. BLINKING LED Figure 4.1: Blinking LED

29 4.1. HARDWARE 29 Figure 4.2: Detailed wiring (blinking LED)

30 30 CHAPTER 4. BLINKING LED 4.2 Software Blinking LED state machine interface For this example we need a state machine that implements a blinking LED. The state machine has two states (the LED can be switched on or off). A clock is embedded to the LED state machine to periodically switch the LED state. To debug this state machine on a regular PC, we use the STATUS state machine that prints some information. This state machine will be used in all the examples in this book. Its source code is in the appendix STATUS state machine Figure 4.3: Blinking LED timing /* file: blink.h */ /* */ /* Blinking LED state machine. The LED is periodically switched */ /* on and off. */ #ifndef BLINK_STATE_MACHINE #define BLINK_STATE_MACHINE #include "demo.h" #include "clock.h" /* Blinking LED state */ typedef struct int pin; /* pin on which the LED is connected */ CLOCK_t clock; /* internal clock of the blinking LED */ enum BLINK_LED_OFF, BLINK_LED_ON, state; /* current LED state */

31 4.2. SOFTWARE 31 int killed; /* kill flag to stop the led */ BLINK_t; /* Blinking LED initialisation method */ void BLINK_init(BLINK_t *q, int pin, int period_in_us); /* The blinking period can be modified */ void BLINK_reschedule(BLINK_t *q, int period_in_us); /* Blinking LED main loop method */ void BLINK_run(BLINK_t *q); /* This method is called to kill the state machine. */ void BLINK_kill(BLINK_t *q); /* The state machine is alive until it is killed. */ int BLINK_isalive(BLINK_t *q); /* Cleanup function (to leave the LED switched off at the end) */ void BLINK_cleanup(BLINK_t *q); #endif Blinking LED state machine implementation /* file: blink.c */ /* */ /* Blinking LED state machine: implementation (see blink.h) */ #include "blink.h" #include "status.h" /* Blinking LED initialisation method */ void BLINK_init(BLINK_t *q, int pin, int period_in_us) q->killed = FALSE; q->pin = pin; CLOCK_init(&q->clock, period_in_us, 0); q->state = BLINK_LED_OFF; pinmode(q->pin, OUTPUT);

32 32 CHAPTER 4. BLINKING LED /* The blinking period can be modified */ void BLINK_reschedule(BLINK_t *q, int period_in_us) q->clock.period_us = period_in_us; /* Blinking LED main loop method */ void BLINK_run(BLINK_t *q) switch (q->state) /* if the LED is off during the first half of the clock */ /* period it must be switched on. */ case BLINK_LED_OFF: if (CLOCK_ratio(&q->clock) < 0.5) q->state = BLINK_LED_ON; digitalwrite(q->pin, HIGH); STATUS_act(HIGH); break; /* if the LED is on during the second half of the clock */ /* period it must be switched off. */ case BLINK_LED_ON: if (CLOCK_ratio(&q->clock) >= 0.5) q->state = BLINK_LED_OFF; digitalwrite(q->pin, LOW); STATUS_act(LOW); break; /* the clock shall also run. */ CLOCK_run(&q->clock); /* This method is called to kill the state machine. */ void BLINK_kill(BLINK_t *q) q->killed = TRUE; /* The state machine is alive until it is killed. */ int BLINK_isalive(BLINK_t *q)

33 4.2. SOFTWARE 33 return!q->killed; /* Cleanup function (to leave the LED switched off at the end) */ void BLINK_cleanup(BLINK_t *q) digitalwrite(q->pin, LOW); Blinking LED example main loop The main program is very similar to the previous example ex2-blinkingled.c /* file: ex2-blinkingled.c */ /* */ /* The famous Raspberry Pi "hello world": Blinking LED example. */ #include "demo.h" #include "status.h" #include "blink.h" #define ACTIVITY_LED 7 /* BCM GPIO 4 */ #define ACTIVITY_PERIOD (1 S) /* The activity LED that eternally blinks to show that the */ /* software is running. */ BLINK_t activity; void init() /* Initialisation of the state machines */ BLINK_init(&activity, ACTIVITY_LED, ACTIVITY_PERIOD); STATUS_init(); int isalive()

34 34 CHAPTER 4. BLINKING LED /* Continue while no interruption signal is received. */ return BLINK_isalive(&activity); void run() BLINK_run(&activity); STATUS_run(); void cleanup() BLINK_cleanup(&activity); STATUS_cleanup(); void interrupted() /* Interruption signal received => stop the demo */ BLINK_kill(&activity); 4.3 Compilation on a PC First we are going to test this demo on a regular PC. The program can be compiled on GNU/Linux with gcc using the stubbed wiringpi library: gcc -Wall -Werror \ -o ex2-blinkingled \ ex2-blinkingled.c demo.c clock.c blink.c wiringpi.c Some quick explanations about this command line: -Wall, -Werror Always the same story. The sooner the errors and warnings are fixed, the better. -o ex2-blinkingled is the name of the executable. ex2-blinkingled.c is the source of the example. clock.c is the source of the clock state machine. It is required by blink.c blink.c is the source of the blinking LED state machine. wiringpi.c is the source of the stubbed wiringpi library. demo.c is the source of the sequencer. It also contains the main function.

35 4.4. EXECUTION ON A PC Execution on a PC The executable can be run from the command line. ex2-blinkingled will run until you kill it (with Ctrl-C for instance). $./ex2-blinkingled model = Stubbed wiringpi rev = n/a mem = 0 Mb maker = CDSoft.fr over volted = no [ACT: HIGH] Signal received: Interrupt 4.5 Compilation on a Raspberry Pi A similar command line can be used on a Raspberry Pi (I m using Raspbian). gcc -Wall -Werror \ -o ex2-blinkingled \ ex2-blinkingled.c demo.c clock.c blink.c \ -lwiringpi The main difference is that we use the real wiringpi library (-lwiringpi) instead of the stub. 4.6 Execution on a Raspberry Pi This example uses a GPIO connected to a LED. While running it you should see the LED blinking once per second. The program uses the real wiringpi library so it requires root privileges. $ sudo./ex2-blinkingled model = Model 2 rev = 1.1 mem = 1024 Mb maker = Sony over volted = yes [ACT: HIGH] Signal received: Interrupt

36 36 CHAPTER 4. BLINKING LED Figure 4.4: Blinking LED 4.7 What s next Now we have a blinking LED showing that the system is running... But it still actually does nothing amazing. The next chapter will add a state machine that composes some nice music (for some definition of nice music). The blinking LED of this chapter will still continue blinking in parallel.

37 Chapter 5 PWM output (music generator) This chapter shows: how to connect a PWM (Pulse Width Modulation) output to a speaker to generate some music how to write a software in C to generate random music 5.1 Hardware The electrical wiring for this project is quite simple. It reuses the blinking LED example. Here is what you need to add to the previous example: an additional LED an additional 330 Ohm resistor a potentiometer a speaker (e.g. PCB Mount Mini Speaker #1898 from Adafruit) The LEDs and resistors are connected to output GPIOs. The speaker is connected to an output PWM GPIO. The potentiometer is used to regulate the volume. The additional LED will be used to kind of visualize the sounds that are being produced. It s turned on when the PWM output frequency is rising. 37

38 38 CHAPTER 5. PWM OUTPUT (MUSIC GENERATOR) Figure 5.1: Blinking LED + PWM output

39 5.1. HARDWARE 39 Figure 5.2: Detailed wiring (blinking LED)

40 40 CHAPTER 5. PWM OUTPUT (MUSIC GENERATOR) 5.2 Software Music state machine interface For this example we need a state machine that generates music. The state machine has several states (play, stop, fade in/out transitions,... ). The music is generated by constantly increasing or decreasing the current frequency (f) until a target frequency (f2) is reached. Then a new frequency is randomly chosen between 110 Hz and 880 Hz. Figure 5.3: Music generator state machine The source code contains some code that will be used in the next chapter to start and stop the music according to a light sensor. This code in not used in this chapter.

41 5.2. SOFTWARE 41 The symbol LIGHTSENSOR must not be defined for the C preprocessor right now. /* file: music.h */ /* */ /* Music state machine. The music is generated on a PWM output. */ #ifndef MUSIC_STATE_MACHINE #define MUSIC_STATE_MACHINE #include "demo.h" #ifdef LIGHTSENSOR #include "light.h" #endif /* Music state */ /* Music generation mode */ typedef enum STOPPED, PLAY, FADEOUT, NIGHT, FADEIN musicmode_t; /* Frequency range used by the random generator */ #define FMIN 110 /* Hz */ #define FMAX 880 /* Hz */ /* Full state machine state */ typedef struct int ticks; /* number of clock ticks */ int fade_slowdown; /* slowdown in fade out/in mode */ int f; /* current frequency */ int f2; /* target frequency */ musicmode_t mode; /* music generation mode */ int led; /* music LED pin number */ int pwm; /* PWM GPIO pin number */ int killed; /* kill flag to stop the music */ #ifdef LIGHTSENSOR LightSensor_t *sensor; /* light sensor state machine */ #endif MUSIC_t; /* Initialisation method */ #ifdef LIGHTSENSOR void MUSIC_init(MUSIC_t *q, int led, int pwm, int fade_slowdown, LightSensor_t *sensor); #else void MUSIC_init(MUSIC_t *q, int led, int pwm, int fade_slowdown);

42 42 CHAPTER 5. PWM OUTPUT (MUSIC GENERATOR) #endif /* The music generator is alive until it is killed. */ int MUSIC_isalive(MUSIC_t *q); /* This metod changes the current music generation mode. */ void MUSIC_mode(MUSIC_t *q, musicmode_t mode); /* This methods kills the music state machine. */ void MUSIC_kill(MUSIC_t *q); /* Computes and eventually plays a different tone at each cycle. */ void MUSIC_run(MUSIC_t *q); /* Stops the music. */ void MUSIC_cleanup(MUSIC_t *q); #endif Music state machine implementation /* file: music.c */ /* */ /* Music state machine: implementation (see music.h) */ #include "music.h" #include "status.h" /* Initialisation method */ #ifdef LIGHTSENSOR void MUSIC_init(MUSIC_t *q, int led, int pwm, int fade_slowdown, LightSensor_t *sensor) #else void MUSIC_init(MUSIC_t *q, int led, int pwm, int fade_slowdown) #endif q->ticks = 0; q->fade_slowdown = fade_slowdown; q->f = 0; q->f2 = 0; q->mode = PLAY; q->led = led;

43 5.2. SOFTWARE 43 q->pwm = pwm; q->killed = FALSE; pinmode(q->led, OUTPUT); pinmode(q->pwm, PWM_OUTPUT); pwmsetmode(pwm_mode_ms); #ifdef LIGHTSENSOR q->sensor = sensor; #endif /* The music generator is alive until it is killed. */ int MUSIC_isalive(MUSIC_t *q) return q->mode!= STOPPED; /* This metod changes the current music generation mode. */ void MUSIC_mode(MUSIC_t *q, musicmode_t mode) q->mode = mode; /* This methods kills the music state machine. */ void MUSIC_kill(MUSIC_t *q) MUSIC_mode(q, FADEOUT); q->killed = TRUE; digitalwrite(q->led, LOW); /* Computes and eventually plays a different tone at each cycle. */ void MUSIC_run(MUSIC_t *q) switch (q->mode) /* Play mode: constantly updates and plays f */ /* until f == f2. */ /* Then choose a new target frequency. */ /* The music may be stopped (Fadeout mode) */ /* when the light is switched off. */ case PLAY: if (q->f!= q->f2) /* continue playing towards the same frequency */ if (q->f < q->f2) q->f++;

44 44 CHAPTER 5. PWM OUTPUT (MUSIC GENERATOR) if (q->f > q->f2) q->f--; else /* find a new tone */ q->f2 = FMIN + random()%(fmax-fmin); digitalwrite(q->led, q->f2 > q->f? HIGH : LOW); #ifdef LIGHTSENSOR if (!LIGHTSENSOR_value(q->sensor)) q->mode = FADEOUT; #endif break; /* Fadeout mode: constantly decreases and plays f */ /* until f == 0. */ /* Then stops the music generation. */ /* The music may be restarted in Fadein */ /* mode if the light is switched on. */ case FADEOUT: if (q->f > 0 && q->ticks%q->fade_slowdown == 0) q->f--; if (q->f <= 0) q->mode = q->killed? STOPPED : NIGHT; digitalwrite(q->led, LOW); #ifdef LIGHTSENSOR if (!q->killed && LIGHTSENSOR_value(q->sensor)) q->mode = FADEIN; #endif break; /* Fadein mode: constantly increases and plays f */ /* while f < f2. */ /* Then starts normal Play mode. */ /* The music may be stopped in Fadeout */ /* mode if the light is switched off. */ case FADEIN: if (q->f < q->f2 && q->ticks%q->fade_slowdown == 0) q->f++; if (q->f >= q->f2) q->mode = PLAY; digitalwrite(q->led, HIGH); #ifdef LIGHTSENSOR if (!LIGHTSENSOR_value(q->sensor)) q->mode = FADEOUT; #endif break; /* Night mode: plays nothing */ /* The music may be restarted in Fadein */ /* mode if the light is switched on. */ case NIGHT: #ifdef LIGHTSENSOR

45 5.2. SOFTWARE 45 if (!q->killed && LIGHTSENSOR_value(q->sensor)) q->mode = FADEIN; #endif break; /* Stop mode: plays nothing */ /* and waits for the sequencer to exit. */ case STOPPED: q->f = 0; break; /* Play the current tone */ pwmtonewrite(q->pwm, q->f); STATUS_music(q->mode, q->f, q->f2); q->ticks++; /* Stops the music. */ void MUSIC_cleanup(MUSIC_t *q) pwmtonewrite(q->pwm, 0); digitalwrite(q->led, LOW); Music generator example main loop The main program is very similar to the previous examples ex3-pwmoutput.c /* file: ex3-pwmoutput.c */ /* */ /* Unpleasant random music generator. */ #include "demo.h" #include "status.h" #include "blink.h"

46 46 CHAPTER 5. PWM OUTPUT (MUSIC GENERATOR) #include "music.h" #define ACTIVITY_LED 7 /* BCM GPIO 4 */ #define MUSIC_LED 0 /* BCM GPIO 0 */ #define MUSIC_PWM 1 /* BCM GPIO 18 */ #define ACTIVITY_PERIOD (1 S) #define FADE_SLOWDOWN 4 /* The activity LED that eternally blinks to show that the */ /* software is running. */ BLINK_t activity; /* The music state machine that generates random music according */ /* to its generation mode. */ MUSIC_t music; void init() /* Initialisation of the state machines */ BLINK_init(&activity, ACTIVITY_LED, ACTIVITY_PERIOD); MUSIC_init(&music, MUSIC_LED, MUSIC_PWM, FADE_SLOWDOWN); STATUS_init(); int isalive() return BLINK_isalive(&activity) && MUSIC_isalive(&music); void run() BLINK_run(&activity); MUSIC_run(&music); STATUS_run(); void cleanup() BLINK_cleanup(&activity); MUSIC_cleanup(&music); STATUS_cleanup(); void interrupted()

47 5.3. COMPILATION ON A PC 47 /* Makes the LED blink faster while the program is stopping. */ BLINK_reschedule(&activity, ACTIVITY_PERIOD/4); /* Also tells the music state machine to stop the music. */ MUSIC_kill(&music); 5.3 Compilation on a PC First we are going to test this demo on a regular PC. The program can be compiled on GNU/Linux with gcc using the stubbed wiringpi library: gcc -Wall -Werror \ -o ex3-pwmoutput \ ex3-pwmoutput.c demo.c clock.c blink.c music.c wiringpi.c Some quick explanations about this command line: -Wall, -Werror Always the same story. The sooner the errors and warnings are fixed, the better. -o ex3-pwmoutput is the name of the executable. ex3-pwmoutput.c is the source of the example. clock.c is the source of the clock state machine. It is required by blink.c blink.c is the source of the blinking LED state machine. music.c is the source of the music generator state machine. wiringpi.c is the source of the stubbed wiringpi library. demo.c is the source of the sequencer. It also contains the main function. 5.4 Execution on a PC The executable can be run from the command line. ex3-pwmoutput will run until you kill it (with Ctrl-C for instance). $./ex3-pwmoutput model = Stubbed wiringpi rev = n/a mem = 0 Mb maker = CDSoft.fr over volted = no [ACT: HIGH] [MUSIC: PLAY / 565 / 839] Of course no sound is played on Linux using the wiringpi stub.

48 48 CHAPTER 5. PWM OUTPUT (MUSIC GENERATOR) 5.5 Compilation on a Raspberry Pi A similar command line can be used on a Raspberry Pi (I m using Raspbian). gcc -Wall -Werror \ -o ex3-pwmoutput \ ex3-pwmoutput.c demo.c clock.c blink.c music.c \ -lwiringpi The main difference is that we use the real wiringpi library (-lwiringpi) instead of the stub. 5.6 Execution on a Raspberry Pi This example uses a GPIO connected to a LED to show if the frequency is rising or falling and another GPIO connected to a speaker. While running it you should see the activity LED blinking once per second, the music LED blinking according to the variation of the frequency and of course hear some unpleasant sound coming from the speaker. The program uses the real wiringpi library so it requires root privileges. $ sudo./ex3-pwmoutput model = Model 2 rev = 1.1 mem = 1024 Mb maker = Sony over volted = yes [ACT: HIGH] [MUSIC: PLAY / 565 / 839]

49 5.7. WHAT S NEXT 49 Figure 5.4: Blinking LED + Music 5.7 What s next Now we produce some sound... But we have no control. The next chapter will add a state machine that detects light and stops the music when the light is low.

50 50 CHAPTER 5. PWM OUTPUT (MUSIC GENERATOR)

51 Chapter 6 Light sensor input This chapter shows: how to connect a light sensor to a discrete input how to write a software in C to stop the music when the light is switched off 6.1 Hardware The electrical wiring for this project is quite simple. It reuses the music generator example. Here is what you need to add to the previous example: a light detector (e.g. ALSPT19 Light Sensor from Adafruit) The LEDs and resistors are connected to output GPIOs. The speaker is connected to an output PWM GPIO. The potentiometer is used to regulate the volume. The additional LED will be used to kind of visualize the sounds that are being produced. It s turned on when the PWM output frequency is rising. The light sensor is connected to an input GPIO. For this demo, the threshold can not be modified. 51

52 52 CHAPTER 6. LIGHT SENSOR INPUT Figure 6.1: Blinking LED + PWM output + Light sensor

53 6.1. HARDWARE 53 Figure 6.2: Detailed wiring (blinking LED)

54 54 CHAPTER 6. LIGHT SENSOR INPUT 6.2 Software Light sensor state machine interface For this example we need a state machine that reads and filters the light sensor values. It s important to filter the light sensor values since they can vary a lot when the light level is near to the sensor threshold. The light level must be stable before being used by the software. A value must be stable for a few cycles before being taken into account. Figure 6.3: Light sensor filtering timing /* file: light.h */ /* */ /* Light sensor state machine. It reads and filters the light */ /* sensor values. */ #ifndef LIGHT_STATE_MACHINE #define LIGHT_STATE_MACHINE #include "demo.h" /* Light state */ typedef struct int pin; /* GPIO pin number of the sensor */ int confirm; /* number of cycles the input is stable */ int value; /* current value acquired on the GPIO */ int confirmedvalue; /* value previously confirmed */ int confirmationperiod; /* number of confirmation cycles */ LightSensor_t; /* Light sensor initialisation */

55 6.2. SOFTWARE 55 void LIGHTSENSOR_init(LightSensor_t *q, int pin, int confirmation_in_us); /* Currently confirmed value */ int LIGHTSENSOR_value(LightSensor_t *q); /* Reads and filters the light sensor value. */ void LIGHTSENSOR_run(LightSensor_t *q); /* The light sensor is considered always alive. */ int LIGHTSENSOR_isalive(LightSensor_t *q); /* Stops the light sensor acquisition. */ void LIGHTSENSOR_cleanup(LightSensor_t *q); #endif Light sensor state machine interface /* file: light.c */ /* */ /* Light sensor state machine: implementation (see light.h) */ #include "light.h" #include "status.h" void LIGHTSENSOR_init(LightSensor_t *q, int pin, int confirmation_in_us) q->pin = pin; q->confirm = 0; q->value = 0; q->confirmationperiod = confirmation_in_us / CYCLE; pinmode(q->pin, INPUT); q->confirmedvalue = q->value = digitalread(q->pin); int LIGHTSENSOR_value(LightSensor_t *q) return q->confirmedvalue; void LIGHTSENSOR_run(LightSensor_t *q)

56 56 CHAPTER 6. LIGHT SENSOR INPUT int value = digitalread(q->pin); if (value == q->value) /* same value than the previous cycle */ q->confirm++; if (q->confirm >= q->confirmationperiod) /* same value for `q->confirmationperiod` cycles */ q->confirmedvalue = value; else /* value not stable => start a new confirmation phase */ q->value = value; q->confirm = 0; STATUS_light(q->value, q->confirmedvalue); int LIGHTSENSOR_isalive(LightSensor_t *q) return TRUE; void LIGHTSENSOR_cleanup(LightSensor_t *q) Music state machine The music state machine described in the previous example is compiled with the LIGHTSENSOR feature. This adds states and transitions to stop and restart the music when the light gets low or high.

57 6.2. SOFTWARE 57 Figure 6.4: Music generator state machine with light sensor Main loop The main program is very similar to the previous examples ex4-lightsensor.c /* file: ex4-lightsensor.c */ /* */ /* Unpleasant random music generator */ /* moderated with a light sensor */ #include "demo.h" #include "status.h"

58 58 CHAPTER 6. LIGHT SENSOR INPUT #include "blink.h" #include "music.h" #include "light.h" #define ACTIVITY_LED 7 /* BCM GPIO 4 */ #define MUSIC_LED 0 /* BCM GPIO 0 */ #define MUSIC_PWM 1 /* BCM GPIO 18 */ #define LIGHT_INPUT 2 /* BCM GPIO 27 */ #define ACTIVITY_PERIOD (1 S) #define LIGHT_CONFIRMATION (100 MS) #define FADE_SLOWDOWN 4 /* The activity LED that eternally blinks to show that */ /* the software is running. */ BLINK_t activity; /* The music state machine that generates random music according */ /* to its generation mode. */ MUSIC_t music; /* The light sensor confirmation state machines. */ LightSensor_t light; void init() /* Initialisation of the state machines */ BLINK_init(&activity, ACTIVITY_LED, ACTIVITY_PERIOD); LIGHTSENSOR_init(&light, LIGHT_INPUT, LIGHT_CONFIRMATION); MUSIC_init(&music, MUSIC_LED, MUSIC_PWM, FADE_SLOWDOWN, &light); STATUS_init(); int isalive() return BLINK_isalive(&activity) && MUSIC_isalive(&music) && LIGHTSENSOR_isalive(&light); void run() BLINK_run(&activity); LIGHTSENSOR_run(&light); MUSIC_run(&music); STATUS_run();

59 6.3. COMPILATION ON A PC 59 void cleanup() BLINK_cleanup(&activity); MUSIC_cleanup(&music); LIGHTSENSOR_cleanup(&light); STATUS_cleanup(); void interrupted() /* Makes the LED blink faster while the program is stopping. */ BLINK_reschedule(&activity, ACTIVITY_PERIOD/4); /* Also tells the music state machine to stop the music. */ MUSIC_kill(&music); 6.3 Compilation on a PC First we are going to test this demo on a regular PC. The program can be compiled on GNU/Linux with gcc using the stubbed wiringpi library: gcc -Wall -Werror \ -DLIGHTSENSOR \ -o ex4-lightsensor \ ex4-lightsensor.c demo.c clock.c blink.c music.c light.c wiringpi.c Some quick explanations about this command line: -Wall, -Werror Always the same story. The sooner the errors and warnings are fixed, the better. -DLIGHTSENSOR activates the light sensor usage in the music generator state machine. -o ex4-lightsensor is the name of the executable. ex4-lightsensor.c is the source of the example. clock.c is the source of the clock state machine. It is required by blink.c blink.c is the source of the blinking LED state machine. music.c is the source of the music generator state machine. light.c is the source of the light sensor state machine. wiringpi.c is the source of the stubbed wiringpi library. demo.c is the source of the sequencer. It also contains the main function.

60 60 CHAPTER 6. LIGHT SENSOR INPUT 6.4 Execution on a PC The executable can be run from the command line. ex4-lightsensor will run until you kill it (with Ctrl-C for instance). $./ex3-pwmoutput model = Stubbed wiringpi rev = n/a mem = 0 Mb maker = CDSoft.fr over volted = no [ACT: HIGH] [LIGHT: 1 / DAY ] [MUSIC: PLAY / 565 / 839] Of course no sound is played on Linux using the wiringpi stub. 6.5 Compilation on a Raspberry Pi A similar command line can be used on a Raspberry Pi (I m using Raspbian). gcc -Wall -Werror \ -DLIGHTSENSOR \ -o ex4-lightsensor \ ex4-lightsensor.c demo.c clock.c blink.c music.c light.c \ -lwiringpi The main difference is that we use the real wiringpi library (-lwiringpi) instead of the stub. 6.6 Execution on a Raspberry Pi This example uses a GPIO connected to a LED to show if the frequency is rising or falling and another GPIO connected to a speaker. The light sensor should detect the light level and the software will stop when the light is switched off. While running it you should see the activity LED blinking once per second, the music LED blinking according to the variation of the frequency and of course hear some unpleasant sound coming from the speaker. Switch the light of the room to stop the music! The program uses the real wiringpi library so it requires root privileges.

61 6.7. WHAT S NEXT 61 $ sudo./ex3-pwmoutput model = Model 2 rev = 1.1 mem = 1024 Mb maker = Sony over volted = yes [ACT: HIGH] [LIGHT: 1 / DAY ] [MUSIC: PLAY / 565 / 839] Figure 6.5: Blinking LED + Music + Light sensor 6.7 What s next Now we produce some sound with some limited control over it... The next step is to use an analog input sensor connected to an I2C input to be able to adjust the detection level. Be patient and wait for the next edition of this tutorial.

62 62 CHAPTER 6. LIGHT SENSOR INPUT

63 Appendix A Appendices A.1 Generic definitions A.1.1 demo.h /* file: demo.h */ /* */ /* Some global definitions for theses tutorials. */ #ifndef DEMO #define DEMO #include <stdio.h> #include <stdlib.h> /* wiringpi library used to access Raspberry Pi GPIOs */ #include "wiringpi.h" /* Time units (all times are internally in µs) */ #define US * 1 #define MS * 1000 US #define S * 1000 MS /* The base cycle is the period of the sequencer. */ #define CYCLE (10 MS) /* Routines to be defined by the application. */ 63

64 64 APPENDIX A. APPENDICES /* init is called once to initialise the state machines */ extern void init(); /* isalive is called periodically to check if the state machines */ /* are still alive */ extern int isalive(); /* run is called periodically to run one step of every state machines */ extern void run(); /* cleanup is called once when the system is not alive */ extern void cleanup(); /* interrupted is called when an interrupt signal is received. */ /* interrupt signals are SIGINT, SIGQUIT, SIGABRT and SIGTERM. */ extern void interrupted(); #endif A.1.2 demo /* file: demo.h */ /* */ /* Sequencer for the state machines of theses tutorials. */ #include <signal.h> #include <string.h> #include <sys/time.h> #include <unistd.h> #include "demo.h" /* signal_handler is called when a signal is received. It calls */ /* the `interrupted` function defined by the application (which */ /* in turns calls the "interrupted" functions of the state */ /* machines. */ static void signal_handler(int sig) printf("\nsignal received: %s\n", strsignal(sig)); interrupted();

65 A.1. GENERIC DEFINITIONS 65 /* timer_handler is called periodically. The period is CYCLE. */ /* The main loop is driven by a timer which is more accurate than */ /* a simple wait. */ /* timer_handler calls: */ /* - `run` to execute one step of every state machines */ /* - `isalive` to check that the system is still alive */ /* - `cleanup` and exits when the system is not alive anymore */ static void timer_handler(int signum) /* run one step */ run(); /* check if the program is still alive */ if (!isalive()) /* stop the program when it is not alive anymore */ cleanup(); exit(exit_success); /* wait for the next cycle */ /* The main function initializes the state machines, */ /* starts a periodic timer to run the state machines */ /* and undefinitely waits for the system to exit. */ int main(void) /* Initialisation of the wiringpi library to access GPIOs */ wiringpisetup(); /* Interruption signal handler */ signal(sigint, signal_handler); signal(sigquit, signal_handler); signal(sigabrt, signal_handler); signal(sigterm, signal_handler); /* Some information about the hardware */ int model, rev, mem, maker, overvolted; piboardid(&model, &rev, &mem, &maker, &overvolted); printf("model = %s\n", pimodelnames[model]); printf("rev = %s\n", pirevisionnames[rev]); printf("mem = %d Mb\n", mem); printf("maker = %s\n", pimakernames[maker]); printf("over volted = %s\n", overvolted? "yes" : "no"); printf("\n");

66 66 APPENDIX A. APPENDICES /* state machine initializations */ init(); /* Install timer_handler as the signal handler for SIGALRM. */ struct sigaction sa; memset (&sa, 0, sizeof (sa)); sa.sa_handler = &timer_handler; sigaction(sigalrm, &sa, NULL); /* Configure the timer to expire after CYCLE µs... */ struct itimerval itimer; itimer.it_value.tv_sec = 0; itimer.it_value.tv_usec = CYCLE; /*... and every CYCLE µs after that. */ itimer.it_interval.tv_sec = 0; itimer.it_interval.tv_usec = CYCLE; /* Start the timer. */ setitimer (ITIMER_REAL, &itimer, NULL); /* Main loop */ while (1) /* This loop could be used as a background loop */ sleep(1000); return EXIT_SUCCESS; A.2 wiringpi stub The wiringpi stub is simply the original wiringpi.h file along with a fake wiringpi.c implementation that simulates some I/O. A.2.1 wiringpi.h /* * wiringpi.h: * Arduino like Wiring library for the Raspberry Pi. * Copyright (c) Gordon Henderson ***********************************************************************

6 GPIO 84. Date: 29/09/2016 Name: ID: This laboratory session discusses about writing program to interact with GPIO of Reapberry Pi.

6 GPIO 84. Date: 29/09/2016 Name: ID: This laboratory session discusses about writing program to interact with GPIO of Reapberry Pi. 6 GPIO 84 Date: 29/09/2016 Name: ID: Name: ID: 6 GPIO This laboratory session discusses about writing program to interact with GPIO of Reapberry Pi. GPIO programming with Assembly Code:block installation

More information

F28HS Hardware-Software Interface: Systems Programming

F28HS Hardware-Software Interface: Systems Programming : Systems Programming Hans-Wolfgang Loidl School of Mathematical and Computer Sciences, Heriot-Watt University, Edinburgh Semester 2 2017/18 0 No proprietary software has been used in producing these slides

More information

Functions (API) Setup Functions

Functions (API) Setup Functions Functions (API) Some of the functions in the WiringPi library are designed to mimic those in the Arduino Wiring system. There are relatively easy to use and should present no problems for anyone used to

More information

tm1640-rpi Documentation

tm1640-rpi Documentation tm1640-rpi Documentation Release 0.1 Michael Farrell October 20, 2015 Contents 1 Introduction 3 1.1 Resources................................................. 3 2 Building the software 5 3 Wiring the

More information

CSE 421: Introduction to Operating Systems

CSE 421: Introduction to Operating Systems Recitation 5: UNIX Signals University at Buffalo, the State University of New York October 2, 2013 About Me 4th year PhD student But TA first time From South Korea Today, We will study... What UNIX signals

More information

Microcontrollers for Ham Radio

Microcontrollers for Ham Radio Microcontrollers for Ham Radio MARTIN BUEHRING - KB4MG MAT T PESCH KK4NLK TOM PERRY KN4LSE What is a Microcontroller? A micro-controller is a small computer on a single integrated circuit containing a

More information

Gooligum Electronics 2015

Gooligum Electronics 2015 The Wombat Prototyping Board for Raspberry Pi Operation and Software Guide This prototyping board is intended to make it easy to experiment and try out ideas for building electronic devices that connect

More information

SPDM Level 2 Smart Electronics Unit, Level 2

SPDM Level 2 Smart Electronics Unit, Level 2 SPDM Level 2 Smart Electronics Unit, Level 2 Evidence Folder John Johns Form 3b RSA Tipton 1.1 describe the purpose of circuit components and symbols. The candidate can describe the purpose of a range

More information

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

CSci 4061 Introduction to Operating Systems. Programs in C/Unix CSci 4061 Introduction to Operating Systems Programs in C/Unix Today Basic C programming Follow on to recitation Structure of a C program A C program consists of a collection of C functions, structs, arrays,

More information

Embedded Systems Programming

Embedded Systems Programming Embedded Systems Programming Signaling (Module 24) Yann-Hang Lee Arizona State University yhlee@asuedu (480) 727-7507 Summer 2014 Signals A signal is an event generated by OS in response to some condition

More information

Lecture 8. Interrupt Handling

Lecture 8. Interrupt Handling F28HS Hardware-Software Interface: Systems Programming Hans-Wolfgang Loidl School of Mathematical and Computer Sciences, Heriot-Watt University, Edinburgh Semester 2 2016/17 0 No proprietary software has

More information

Basic Electronics and Raspberry Pi IO Programming

Basic Electronics and Raspberry Pi IO Programming Basic Electronics and Raspberry Pi IO Programming Guoping Wang Indiana University Purdue University Fort Wayne IEEE Fort Wayne Section wang@ipfw.edu February 18, 2016 Table of Contents 1 Safety Guideline

More information

Princeton University. Computer Science 217: Introduction to Programming Systems. Signals

Princeton University. Computer Science 217: Introduction to Programming Systems. Signals Princeton University Computer Science 217: Introduction to Programming Systems Signals 1 Goals of this Lecture Help you learn about: Sending signals Handling signals and thereby How the OS exposes the

More information

F28HS Hardware-Software Interface: Systems Programming

F28HS Hardware-Software Interface: Systems Programming F28HS Hardware-Software Interface: Systems Programming Hans-Wolfgang Loidl School of Mathematical and Computer Sciences, Heriot-Watt University, Edinburgh Semester 2 2016/17 0 No proprietary software has

More information

Introduction to Internet of Things Prof. Sudip Misra Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Introduction to Internet of Things Prof. Sudip Misra Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur Introduction to Internet of Things Prof. Sudip Misra Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur Lecture - 30 Implementation of IoT with Raspberry Pi- I In the

More information

CS240: Programming in C

CS240: Programming in C CS240: Programming in C Lecture 17: Processes, Pipes, and Signals Cristina Nita-Rotaru Lecture 17/ Fall 2013 1 Processes in UNIX UNIX identifies processes via a unique Process ID Each process also knows

More information

Internet of Things: Using MRAA to Abstract Platform I/O Capabilities

Internet of Things: Using MRAA to Abstract Platform I/O Capabilities Internet of Things: Using MRAA to Abstract Platform I/O Capabilities Integrated Computer Solutions, Inc. Contents 1. Abstract... 3 2. MRAA Overview... 3 2.1. Obtaining MRAA APIs and API Documentation...

More information

Dumping gcov data at runtime - a simple example

Dumping gcov data at runtime - a simple example Dumping gcov data at runtime - a simple example Der Herr Hofrat OpenTech EDV Research GmbH, Austria < der.herr@hofr.at > Revision History Revision 0.1 2011-07-27 First release Table of Contents 1. Introduction

More information

INTEGRATED INFORMATION AND COMMUNICATION LEARNING MODEL FOR RASPBERRY Pi ENVIRONMENT

INTEGRATED INFORMATION AND COMMUNICATION LEARNING MODEL FOR RASPBERRY Pi ENVIRONMENT INTEGRATED INFORMATION AND COMMUNICATION LEARNING MODEL FOR RASPBERRY Pi ENVIRONMENT Y. J. Lee Department of Technology Education, Korea National University of Education, South Korea E-Mail: lyj@knue.ac.kr

More information

leos little embedded Operating System User's guide Written by Leonardo Miliani

leos little embedded Operating System User's guide Written by Leonardo Miliani leos little embedded Operating System User's guide Written by Leonardo Miliani www.leonardomiliani.com (Ɔ) Copyleft 2012-2013 Index: 1. What's leos? 2. How to use leos methods 3. 32-/64-bit math 4. How

More information

CSci 4061 Introduction to Operating Systems. (Advanced Control Signals)

CSci 4061 Introduction to Operating Systems. (Advanced Control Signals) CSci 4061 Introduction to Operating Systems (Advanced Control Signals) What is a Signal? Signals are a form of asynchronous IPC Earlier: Non-blocking I/O and check if it has happened => polling Problem

More information

BCS Raspberry Pi Launch Events Getting started with Raspberry Pi

BCS Raspberry Pi Launch Events Getting started with Raspberry Pi BCS Raspberry Pi Launch Events Getting started with Raspberry Pi Department of Computer Science 16 th & 17 th April 2013 Who are you? How many of you.. are teachers in STEM subjects in non STEM subjects

More information

How to Become an IoT Developer (and Have Fun!) Justin Mclean Class Software.

How to Become an IoT Developer (and Have Fun!) Justin Mclean Class Software. How to Become an IoT Developer (and Have Fun!) Justin Mclean Class Software Email: justin@classsoftware.com Twitter: @justinmclean Who am I? Freelance Developer - programming for 25 years Incubator PMC

More information

A Slice of Raspberry Pi

A Slice of Raspberry Pi A Slice of Raspberry Pi Roadmap Introduction to the Raspberry Pi device What can you use a Raspberry Pi for? Talking to the Hardware A Raspberry Pi Arcade table Q & A Raspberry Pi Introduction What is

More information

Fortran Signal Handling

Fortran Signal Handling Fortran Signal Handling Ge Baolai SHARCNET The University of Western Ontario June 19, 2008 1 Signal Handling in User Applications A running programme may be interrupted or terminated by the operating system.

More information

The DTMF generator comprises 3 main components.

The DTMF generator comprises 3 main components. Make a DTMF generator with an Arduino board This article is for absolute beginners, and describes the design and construction of a DTMF generator. DTMF generators are often used to signal with equipment

More information

Arduino Prof. Dr. Magdy M. Abdelhameed

Arduino Prof. Dr. Magdy M. Abdelhameed Course Code: MDP 454, Course Name:, Second Semester 2014 Arduino What is Arduino? Microcontroller Platform Okay but what s a Microcontroller? Tiny, self-contained computers in an IC Often contain peripherals

More information

Signals. POSIX defines a variety of signal types, each for a particular

Signals. POSIX defines a variety of signal types, each for a particular Signals A signal is a software interrupt delivered to a process. The operating system uses signals to report exceptional situations to an executing program. Some signals report errors such as references

More information

keyestudio Keyestudio MEGA 2560 R3 Board

keyestudio Keyestudio MEGA 2560 R3 Board Keyestudio MEGA 2560 R3 Board Introduction: Keyestudio Mega 2560 R3 is a microcontroller board based on the ATMEGA2560-16AU, fully compatible with ARDUINO MEGA 2560 REV3. It has 54 digital input/output

More information

CSCI-243 Exam 2 Review February 22, 2015 Presented by the RIT Computer Science Community

CSCI-243 Exam 2 Review February 22, 2015 Presented by the RIT Computer Science Community CSCI-43 Exam Review February, 01 Presented by the RIT Computer Science Community http://csc.cs.rit.edu C Preprocessor 1. Consider the following program: 1 # include 3 # ifdef WINDOWS 4 # include

More information

Operating Systems 2010/2011

Operating Systems 2010/2011 Operating Systems 2010/2011 Signals Johan Lukkien 1 Signals A signal is a software generated event notification of state change encapsulation of physical event usually: sent from a process to a process

More information

System Programming. Signals I

System Programming. Signals I Content : by Dr. B. Boufama School of Computer Science University of Windsor Instructor: Dr. A. Habed adlane@cs.uwindsor.ca http://cs.uwindsor.ca/ adlane/60-256 Content Content 1 Introduction 2 3 Signals

More information

Processes. CS439: Principles of Computer Systems January 24, 2018

Processes. CS439: Principles of Computer Systems January 24, 2018 Processes CS439: Principles of Computer Systems January 24, 2018 Last Time History Lesson Hardware expensive, humans cheap Hardware cheap, humans expensive Hardware very cheap, humans very expensive Dual-mode

More information

Processes. CS439: Principles of Computer Systems January 30, 2019

Processes. CS439: Principles of Computer Systems January 30, 2019 Processes CS439: Principles of Computer Systems January 30, 2019 What We Know Operating system complexity increased over time in response to economic and technological changes The three roles did not show

More information

ECGR 4101/5101, Fall 2016: Lab 1 First Embedded Systems Project Learning Objectives:

ECGR 4101/5101, Fall 2016: Lab 1 First Embedded Systems Project Learning Objectives: ECGR 4101/5101, Fall 2016: Lab 1 First Embedded Systems Project Learning Objectives: This lab will introduce basic embedded systems programming concepts by familiarizing the user with an embedded programming

More information

Kurt Schmidt. October 30, 2018

Kurt Schmidt. October 30, 2018 to Structs Dept. of Computer Science, Drexel University October 30, 2018 Array Objectives to Structs Intended audience: Student who has working knowledge of Python To gain some experience with a statically-typed

More information

Signals. Goals of this Lecture. Help you learn about: Sending signals Handling signals

Signals. Goals of this Lecture. Help you learn about: Sending signals Handling signals Signals 1 Goals of this Lecture Help you learn about: Sending signals Handling signals and thereby How the OS exposes the occurrence of some exceptions to application processes How application processes

More information

REVISION HISTORY NUMBER DATE DESCRIPTION NAME

REVISION HISTORY NUMBER DATE DESCRIPTION NAME i ii REVISION HISTORY NUMBER DATE DESCRIPTION NAME iii Contents 1 Walking through the Assignment 2 1 1.1 Preparing the base code from Assignment 1..................................... 1 1.2 Dummy port

More information

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 18

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 18 CS24: INTRODUCTION TO COMPUTING SYSTEMS Spring 2018 Lecture 18 LAST TIME: OVERVIEW Expanded on our process abstraction A special control process manages all other processes Uses the same process abstraction

More information

GDB Tutorial. A Walkthrough with Examples. CMSC Spring Last modified March 22, GDB Tutorial

GDB Tutorial. A Walkthrough with Examples. CMSC Spring Last modified March 22, GDB Tutorial A Walkthrough with Examples CMSC 212 - Spring 2009 Last modified March 22, 2009 What is gdb? GNU Debugger A debugger for several languages, including C and C++ It allows you to inspect what the program

More information

Digital Design and Computer Architecture

Digital Design and Computer Architecture Digital Design and Computer Architecture Lab 6: C Programming Introduction In this lab, you will learn to program an ARM processor on the Raspberry Pi in C by following a tutorial and then writing several

More information

RPi General Purpose IO (GPIO) Pins

RPi General Purpose IO (GPIO) Pins GPIO RPi Setup for Today Because the cobbler connector has a notch, you can only put the cable in the right way But, it is possible to put the cable in upside down on the Raspberry Pi The colored wire

More information

82V391x / 8V893xx WAN PLL Device Families Device Driver User s Guide

82V391x / 8V893xx WAN PLL Device Families Device Driver User s Guide 82V391x / 8V893xx WAN PLL Device Families Device Driver Version 1.2 April 29, 2014 Table of Contents 1. Introduction... 1 2. Software Architecture... 2 2.1. Overview... 2 2.2. Hardware Abstraction Layer

More information

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community http://csc.cs.rit.edu History and Evolution of Programming Languages 1. Explain the relationship between machine

More information

Goals of this Lecture

Goals of this Lecture Signals 1 Goals of this Lecture Help you learn about: Sending signals Handling signals and thereby How the OS exposes the occurrence of some exceptions to application processes How application processes

More information

Arduino 101 AN INTRODUCTION TO ARDUINO BY WOMEN IN ENGINEERING FT T I NA A ND AW E S O ME ME NTO R S

Arduino 101 AN INTRODUCTION TO ARDUINO BY WOMEN IN ENGINEERING FT T I NA A ND AW E S O ME ME NTO R S Arduino 101 AN INTRODUCTION TO ARDUINO BY WOMEN IN ENGINEERING FT T I NA A ND AW E S O ME ME NTO R S Overview Motivation Circuit Design and Arduino Architecture Projects Blink the LED Switch Night Lamp

More information

C Tutorial: Part 1. Dr. Charalampos C. Tsimenidis. Newcastle University School of Electrical and Electronic Engineering.

C Tutorial: Part 1. Dr. Charalampos C. Tsimenidis. Newcastle University School of Electrical and Electronic Engineering. C Tutorial: Part 1 Dr. Charalampos C. Tsimenidis Newcastle University School of Electrical and Electronic Engineering September 2013 Why C? Small (32 keywords) Stable Existing code base Fast Low-level

More information

CamJam EduKit Sensors Worksheet Five. Equipment Required. The Parts. The Passive Infrared Sensor

CamJam EduKit Sensors Worksheet Five. Equipment Required. The Parts. The Passive Infrared Sensor CamJam EduKit Sensors Worksheet Five Project Description Passive Infrared Sensor In this project, you will learn how to wire and program a passive infrared sensor that detects movement near it. Equipment

More information

Lab 4: Interrupts and Realtime

Lab 4: Interrupts and Realtime Lab 4: Interrupts and Realtime Overview At this point, we have learned the basics of how to write kernel driver module, and we wrote a driver kernel module for the LCD+shift register. Writing kernel driver

More information

Prototyping & Engineering Electronics Kits Basic Kit Guide

Prototyping & Engineering Electronics Kits Basic Kit Guide Prototyping & Engineering Electronics Kits Basic Kit Guide odysseyboard.com Please refer to www.odysseyboard.com for a PDF updated version of this guide. Guide version 1.0, February, 2018. Copyright Odyssey

More information

Index. Jeff Cicolani 2018 J. Cicolani, Beginning Robotics with Raspberry Pi and Arduino,

Index. Jeff Cicolani 2018 J. Cicolani, Beginning Robotics with Raspberry Pi and Arduino, A Accessor methods, 92 Adafruit, 9 Adafruit DC & Stepper Motor HAT assembling board adjustment, 199 circuit board, 199 kit, 197 pins, 197 preparation, 197 Raspberry Pi, 198, 204 removal, 201 rotation,

More information

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20 CS24: INTRODUCTION TO COMPUTING SYSTEMS Spring 2018 Lecture 20 LAST TIME: UNIX PROCESS MODEL Began covering the UNIX process model and API Information associated with each process: A PID (process ID) to

More information

Report 4. Implementation of FIR filter

Report 4. Implementation of FIR filter 1 Embedded Systems WS 2014/15 Report 4: Implementation of FIR filter Stefan Feilmeier Facultatea de Inginerie Hermann Oberth Master-Program Embedded Systems Advanced Digital Signal Processing Methods Winter

More information

Design and development of embedded systems for the Internet of Things (IoT) Fabio Angeletti Fabrizio Gattuso

Design and development of embedded systems for the Internet of Things (IoT) Fabio Angeletti Fabrizio Gattuso Design and development of embedded systems for the Internet of Things (IoT) Fabio Angeletti Fabrizio Gattuso Why C? Test on 21 Android Devices with 32-bits and 64-bits processors and different versions

More information

SF Innovations Ltd. User Instructions (5th January 2016) Contents. Introduction

SF Innovations Ltd. User Instructions (5th January 2016) Contents. Introduction SF Innovations Ltd Custard Pi 1 - Breakout Board with protection for the Raspberry Pi GPIO Custard Pi 1A - Breakout Board for the Raspberry Pi GPIO User Instructions (5th January 2016) Contents Introduction

More information

Introduction to Arduino Diagrams & Code Brown County Library

Introduction to Arduino Diagrams & Code Brown County Library Introduction to Arduino Diagrams & Code Project 01: Blinking LED Components needed: Arduino Uno board LED Put long lead into pin 13 // Project 01: Blinking LED int LED = 13; // LED connected to digital

More information

Introduction To Arduino

Introduction To Arduino Introduction To Arduino What is Arduino? Hardware Boards / microcontrollers Shields Software Arduino IDE Simplified C Community Tutorials Forums Sample projects Arduino Uno Power: 5v (7-12v input) Digital

More information

Deep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems

Deep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems Deep C Multifile projects Getting it running Data types Typecasting Memory management Pointers Fabián E. Bustamante, Fall 2004 Multifile Projects Give your project a structure Modularized design Reuse

More information

System Calls & Signals. CS449 Spring 2016

System Calls & Signals. CS449 Spring 2016 System Calls & Signals CS449 Spring 2016 Operating system OS a layer of software interposed between the application program and the hardware Application programs Operating system Processor Main memory

More information

CSE410 Operating Systems Spring 2018 Project 1: Introduction to Unix/Linux Signals

CSE410 Operating Systems Spring 2018 Project 1: Introduction to Unix/Linux Signals CSE410 Operating Systems Spring 2018 Project 1: Introduction to Unix/Linux Signals 1 Overview and Background In this exercise you will gain first hand experience with Unix/Linux signals. You will develop

More information

1 Introduction. 2 Managing contexts. Green, green threads of home. Johan Montelius HT2018

1 Introduction. 2 Managing contexts. Green, green threads of home. Johan Montelius HT2018 1 Introduction Green, green threads of home Johan Montelius HT2018 This is an assignment where you will implement your own thread library. Instead of using the operating systems threads you will create

More information

RaRa Academy: Raspberry Pi. Karl Heinz Kremer - K5KHK

RaRa Academy: Raspberry Pi. Karl Heinz Kremer - K5KHK RaRa Academy: Raspberry Pi Karl Heinz Kremer - K5KHK Why Are We Here? I cannot convert you into a Raspberry Pi (or Linux) expert in two hours I cannot teach you everything there is to know about using

More information

Signals. Joseph Cordina

Signals. Joseph Cordina 1 Signals Signals are software interrupts that give us a way to handle asynchronous events. Signals can be received by or sent to any existing process. It provides a flexible way to stop execution of a

More information

My malloc: mylloc and mhysa. Johan Montelius HT2016

My malloc: mylloc and mhysa. Johan Montelius HT2016 1 Introduction My malloc: mylloc and mhysa Johan Montelius HT2016 So this is an experiment where we will implement our own malloc. We will not implement the world s fastest allocator, but it will work

More information

ECE 598 Advanced Operating Systems Lecture 4

ECE 598 Advanced Operating Systems Lecture 4 ECE 598 Advanced Operating Systems Lecture 4 Vince Weaver http://www.eece.maine.edu/~vweaver vincent.weaver@maine.edu 28 January 2016 Announcements HW#1 was due HW#2 was posted, will be tricky Let me know

More information

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

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto Ricardo Rocha Department of Computer Science Faculty of Sciences University of Porto Adapted from the slides Revisões sobre Programação em C, Sérgio Crisóstomo Compilation #include int main()

More information

Class Information ANNOUCEMENTS

Class Information ANNOUCEMENTS Class Information ANNOUCEMENTS Third homework due TODAY at 11:59pm. Extension? First project has been posted, due Monday October 23, 11:59pm. Midterm exam: Friday, October 27, in class. Don t forget to

More information

Lesson6_7segments digital LED score board

Lesson6_7segments digital LED score board Lesson6_7segments digital LED score board 7 segments digital LED score board is a digital LED display, and made by 7 LED lights, there are share a common ground pin, so we can control each pin to show

More information

Capacitive Fingerprint Reader User Manual

Capacitive Fingerprint Reader User Manual OVERVIEW Capacitive Fingerprint Reader User Manual The Capacitive Fingerprint Reader is a standard fingerprinting module designed for secondary development, allows fast and stable fingerprint verification.

More information

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information Laboratory 2: Programming Basics and Variables Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information 3. Comment: a. name your program with extension.c b. use o option to specify

More information

Java Programming on the Raspberry Pi with Pi4J. Rob Ratcliff

Java Programming on the Raspberry Pi with Pi4J. Rob Ratcliff Java Programming on the Raspberry Pi with Pi4J Rob Ratcliff What is a Raspberry Pi? Single Board Computer Pi 3 Model B+ Full size HDMI ARMv8 64 bit with 1.4 GHz 1 GB RAM 4 USB ports 40 I/O Pins for GPIO,

More information

1 A Brief Introduction To GDB

1 A Brief Introduction To GDB 1 A Brief Introduction To GDB GDB, the GNU Project debugger, allows you to see what is going on inside another program while it executes or what another program was doing at the moment it crashed. GDB

More information

RedBoard Hookup Guide

RedBoard Hookup Guide Page 1 of 11 RedBoard Hookup Guide CONTRIBUTORS: JIMB0 Introduction The Redboard is an Arduino-compatible development platform that enables quick-and-easy project prototyping. It can interact with real-world

More information

Signals! Goals of this Lecture! Help you learn about:" Sending signals" Handling signals"

Signals! Goals of this Lecture! Help you learn about: Sending signals Handling signals Signals! 1 Goals of this Lecture! Help you learn about: Sending signals Handling signals and thereby How the OS exposes the occurrence of some exceptions to application processes How application processes

More information

Real Time Operating Systems and Middleware

Real Time Operating Systems and Middleware Real Time Operating Systems and Middleware Real-Time Programming Interfaces Luca Abeni abeni@dit.unitn.it Real Time Operating Systems and Middleware p. 1 Needs for a Real-Time Interface Real-Time applications

More information

Basic Input/Output Operations

Basic Input/Output Operations Basic Input/Output Operations Posted on May 9, 2008, by Ibrahim KAMAL, in Micro-controllers, tagged In this third part of the 89s52 tutorial, we are going to study the basic structure and configuration

More information

Operating Systemss and Multicore Programming (1DT089)

Operating Systemss and Multicore Programming (1DT089) Operating Systemss and Multicore Programming (1DT089) Problem Set 1 - Tutorial January 2013 Uppsala University karl.marklund@it.uu.se pointers.c Programming with pointers The init() functions is similar

More information

ENGR 40M Project 3c: Switch debouncing

ENGR 40M Project 3c: Switch debouncing ENGR 40M Project 3c: Switch debouncing For due dates, see the overview handout 1 Introduction This week, you will build on the previous two labs and program the Arduino to respond to an input from the

More information

Interrupts, timers and counters

Interrupts, timers and counters Interrupts, timers and counters Posted on May 10, 2008, by Ibrahim KAMAL, in Micro-controllers, tagged Most microcontrollers come with a set of ADD-ONs called peripherals, to enhance the functioning of

More information

ECEN 449 Microprocessor System Design. Hardware-Software Communication. Texas A&M University

ECEN 449 Microprocessor System Design. Hardware-Software Communication. Texas A&M University ECEN 449 Microprocessor System Design Hardware-Software Communication 1 Objectives of this Lecture Unit Learn basics of Hardware-Software communication Memory Mapped I/O Polling/Interrupts 2 Motivation

More information

signals Communicating with the OS System call (last lecture) Signal (this lecture) User Process Operating System

signals Communicating with the OS System call (last lecture) Signal (this lecture) User Process Operating System Signals 1 Communicating with the OS signals User Process Operating System systems calls System call (last lecture) o Request to the operating system to perform a task o that the process does not have permission

More information

Signal Example 1. Signal Example 2

Signal Example 1. Signal Example 2 Signal Example 1 #include #include void ctrl_c_handler(int tmp) { printf("you typed CTL-C, but I don't want to die!\n"); int main(int argc, char* argv[]) { long i; signal(sigint, ctrl_c_handler);

More information

CS 326: Operating Systems. Lecture 1

CS 326: Operating Systems. Lecture 1 CS 326: Operating Systems Lecture 1 Welcome to CS 326! Glad to have you all in class! Lecture Information: Time: T, Th 9:55 11:40am Lab: M 4:45 6:20pm Room: LS G12 Course website: http://www.cs.usfca.edu/~mmalensek/cs326

More information

Raspberry Pi GPIO Zero Reaction Timer

Raspberry Pi GPIO Zero Reaction Timer Raspberry Pi GPIO Zero Reaction Timer Tutorial by Andrew Oakley Public Domain 1 Feb 2016 www.cotswoldjam.org Introduction This Python programming tutorial, shows you how simple it is to use an LED light

More information

ARDUINO YÚN Code: A000008

ARDUINO YÚN Code: A000008 ARDUINO YÚN Code: A000008 Arduino YÚN is the perfect board to use when designing connected devices and, more in general, Internet of Things projects. It combines the power of Linux with the ease of use

More information

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2 CS 220: Introduction to Parallel Computing Beginning C Lecture 2 Today s Schedule More C Background Differences: C vs Java/Python The C Compiler HW0 8/25/17 CS 220: Parallel Computing 2 Today s Schedule

More information

USB. Bluetooth. Display. IO connectors. Sound. Main CPU Atmel ARM7 JTAG. IO Processor Atmel AVR JTAG. Introduction to the Lego NXT

USB. Bluetooth. Display. IO connectors. Sound. Main CPU Atmel ARM7 JTAG. IO Processor Atmel AVR JTAG. Introduction to the Lego NXT Introduction to the Lego NXT What is Lego Mindstorm? Andreas Sandberg A kit containing: A Lego NXT computer 3 motors Touch sensor Light sensor Sound sensor Ultrasonic range

More information

Keyboards. The PS/2 Protocol

Keyboards. The PS/2 Protocol Keyboards The PS/2 Protocol Debugging Always start from a known working state; stop in a working state. If it breaks, what changed? Take a simple small step, check it carefully, then take another small

More information

EL2310 Scientific Programming

EL2310 Scientific Programming (yaseminb@kth.se) Overview Overview Roots of C Getting started with C Closer look at Hello World Programming Environment Discussion Basic Datatypes and printf Schedule Introduction to C - main part of

More information

acknowledgments...xiii foreword...xiv

acknowledgments...xiii foreword...xiv Contents in Detail acknowledgments...xiii foreword...xiv Introduction... xv Why Build and Learn About Robots?...xvi Why the Raspberry Pi?... xvii What Is in This Book?... xvii Who is This Book For?...xix

More information

Topic 6: A Quick Intro To C

Topic 6: A Quick Intro To C Topic 6: A Quick Intro To C Assumption: All of you know Java. Much of C syntax is the same. Also: Many of you have used C or C++. Goal for this topic: you can write & run a simple C program basic functions

More information

1/Build a Mintronics: MintDuino

1/Build a Mintronics: MintDuino 1/Build a Mintronics: The is perfect for anyone interested in learning (or teaching) the fundamentals of how micro controllers work. It will have you building your own micro controller from scratch on

More information

Lecture 4 Threads. (chapter 4)

Lecture 4 Threads. (chapter 4) Bilkent University Department of Computer Engineering CS342 Operating Systems Lecture 4 Threads (chapter 4) Dr. İbrahim Körpeoğlu http://www.cs.bilkent.edu.tr/~korpe 1 References The slides here are adapted/modified

More information

Signals. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Signals. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University Signals Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu Multitasking (1) Programmer s model of multitasking fork() spawns new process Called once,

More information

SE350: Operating Systems. Lecture 3: Concurrency

SE350: Operating Systems. Lecture 3: Concurrency SE350: Operating Systems Lecture 3: Concurrency Main Points Thread abstraction What are threads and what is the thread abstraction? Thread life cycle What states does a thread go through? Thread Implementation

More information

Signals, Synchronization. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han

Signals, Synchronization. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han , Synchronization CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han Announcements Program Assignment #1 due Tuesday Feb. 15 at 11:55 pm TA will explain parts b-d in recitation Read chapters 7 and

More information

Computer Labs: Debugging

Computer Labs: Debugging Computer Labs: Debugging 2 o MIEIC Pedro F. Souto (pfs@fe.up.pt) October 29, 2012 Bugs and Debugging Problem To err is human This is specially true when the human is a programmer :( Solution There is none.

More information

PRINCIPLES OF OPERATING SYSTEMS

PRINCIPLES OF OPERATING SYSTEMS PRINCIPLES OF OPERATING SYSTEMS Tutorial-1&2: C Review CPSC 457, Spring 2015 May 20-21, 2015 Department of Computer Science, University of Calgary Connecting to your VM Open a terminal (in your linux machine)

More information

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

Topic 6: A Quick Intro To C. Reading. goto Considered Harmful History Topic 6: A Quick Intro To C Reading Assumption: All of you know basic Java. Much of C syntax is the same. Also: Some of you have used C or C++. Goal for this topic: you can write & run a simple C program

More information

Signals! Goals of this Lecture! Help you learn about:" Sending signals" Handling signals"

Signals! Goals of this Lecture! Help you learn about: Sending signals Handling signals Signals! 1 Goals of this Lecture! Help you learn about: Sending signals Handling signals and thereby How the OS exposes the occurrence of some exceptions to application processes How application processes

More information