Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Lab 2 Notes: Computer Engineering – Software Perspective, Lecture notes of Software Engineering

These lab notes provide a brief survey of background information relevant to understanding the purpose and context for the lab. The field of computer engineering is at the interface between hardware and software and seeks to balance the tension between application requirements and technology constraints. In this lab, you will explore the field of computer engineering from a software perspective by incrementally programming a microcontroller in C++ to implement an IoT “smart light” system.

Typology: Lecture notes

2021/2022

Uploaded on 05/11/2023

weldon
weldon 🇺🇸

4.5

(8)

5 documents

1 / 12

Toggle sidebar

Related documents


Partial preview of the text

Download Lab 2 Notes: Computer Engineering – Software Perspective and more Lecture notes Software Engineering in PDF only on Docsity! CURIE Academy, Summer 2021 Lab 2 Notes: Computer Engineering – Software Perspective Prof. Christopher Batten School of Electrical and Computer Engineering Cornell University The field of computer engineering is at the interface between hardware and software and seeks to balance the tension between application requirements and technology constraints. In Lab 1, you explored the field of computer engineering from a hardware perspective by assembling basic logic gates to implement a simple “calculator” for adding small binary numbers. In this lab, you will ex- plore the field of computer engineering from a software perspective by incrementally programming a microcontroller in C++ to implement an IoT “smart light” system. These lab notes provide a brief survey of background information relevant to understanding the purpose and context for the lab. As illustrated in Figure 1, computer systems can be viewed as a stack of abstraction and implementa- tion layers from applications at the highest layer to technology at the lowest layer. In these lab notes we will briefly discuss the application, algorithm, programming language, operating system, com- piler, and instruction set layers as they relate to our “smart light” system. In the actual lab session, you will have an opportunity to put what you have learned into practice. We will focus on some layers more than others, but by the end of this lab you should have a good understanding of how computer engineers can leverage these layers to design software for future computing systems. 1. Application: Smart Light Figure 2 illustrates an example “smart light” system from a company called Brilliant. An internet- connected light switch enables turning on lights anywhere in the home, but this same light switch can Application Algorithm Programming Language Instruction Set Architecture Microarchitecture Register-Transfer Level Gate Level Circuits Devices Technology C om pu te r E ng in ee ri ng Smart Light Flowchart C++ ARM Machine Instructions Operating System C U RI E La b 2 Ripple Carry Adder NOT, AND, OR, XOR Resistors, LEDs, Transistors C U RI E La b 1 Inverter Compiler Particle OS Particle Development Environment Figure 1: Computer Systems Stack 1 CURIE Academy, Summer 2021 Lab 2 Notes: Computer Engineering – Software Perspective Figure 2: Example Commercial “Smart Light” System from Brilliant IoT Output Device IoT Input Device IoT Cloud LED Output Module Button Input Module Figure 3: Diagram of Simple “Smart Light” System WiFi Atenna Analog Port A0 Digital Port D4 Digital Port D2 I2C Port 1I2C Port 2 UART Port Analog Port A4 Analog Port A2 USB Port Blue LED on Pin D7 Power Connector for LiPo Battery Reset Button Status LED Mode Button Figure 4: Particle Argon 2 CURIE Academy, Summer 2021 Lab 2 Notes: Computer Engineering – Software Perspective 1 int button_state; 2 button_state = read_button_state( button_pin ); 3 4 if ( button_state == 1 ) { 5 // send "on" msg 6 } 7 else { 8 // send "off" msg 9 } 10 11 // wait 1 second (a) Sketch of IoT Input Device Program 1 void receive_msg( msg ) 2 { 3 if ( msg == "on" ) { 4 // turn light on 5 } 6 else { 7 // turn light off 8 } 9 } (b) Sketch of IoT Output Device Program Figure 7: Sketch of C++ Programs for Smart Light In this case, we store the value 2 into the variable a (i.e., we put the value 2 into the box named a). The statement on line 3 is a variable initialization statement which simply does a variable declaration and assignment in a single step. Once the computer has finished executing lines 1–3, the value 2 is stored in the variable a and the value 3 is stored in the variable b. Note that comments start with two forward slashes (i.e., //) and are ignored by the computer. You can feel free to exclude them although comments are important part of effective software engineering. Line 5 is another variable declaration statement. Line 6 is a variable assignment statement where the right-hand side is more complicated than just a single number. Here the computer must first determine the value stored in the variable a, the value stored in the variable b, add these two values together, and store the result (i.g., the value 5) in the variable c. Figure 6(b) illustrates how to define a function. A function is a parameterized sequence of statements. Line 2 declares the function’s interface including the function’s name (add), the functions parameters (a,b), the type of each parameter (int), and the function’s return type (int). Lines 3–7 are called the function body. In this example, the function first adds the values stored in the two parameters and stores the result in a variable named sum. The sum is then returned using a return statement on Line 6. Figure 6(c) illustrates how to call a function. We use the function’s name and include a list of variables which will be passed in as the parameters to the function. So the example in Figure 6(c) will first declare a variable named c, then call the function add, which adds the values 2 and 3 before returning the result 5, which is finally stored in the variable c. Figure 7 is a sketch of an initial C++ program for both the IoT input device and the IoT output device which corresponds to the algorithms shown in Figure 5. Lines 4–9 are a conditional statement. First, the conditional expression within the parenthesis on line 4 is evaluated (i.e., does the variable button_state contain the value 1?). If this conditional expression is true, then the statement on line 5 is executed. If this conditional expression is false, then the statement on line 8 is executed. A conditional statement can be used to implement decision steps in our flowchart. However, the C++ program in Figure 7 is not complete; it is just a sketch, since how our program reads the button state, sends messages, receives messages, and turns on/off LEDs depends on the operating system which is discussed in the next section. If you would like to learn more about basic C++ programming consider reviewing this gentle intro- duction which is part of an intermediate programming course taught at Cornell University: • https://cornell-ece2400.github.io/ece2400-docs/ece2400-T01-intro-c 5 CURIE Academy, Summer 2021 Lab 2 Notes: Computer Engineering – Software Perspective 4. Operating System: Particle OS Figure 7 was an incomplete sketch of the programs needed to implement our flowchart algorithms. To complete the sketch, we will need to call various functions that are part of the operating system. The operating system is in charge of managing the interaction between the application program and the underlying low-level hardware. The Particle operating system (OS) provides functions for setting up the Particle Argon, reading/writing pins, and sending/receiving messages (also called events). We will start by considering a simple example that adds two numbers, before revisiting the complete C++ programs to implement our flowchart algorithms. Figure 8 shows a C++ program which adds two numbers together and displays the result using an LED. The Particle Argon devices we are targeting do not have any kind of screen, so we have to think creatively about how to display the status of the program. Here we are blinking an LED z times where z is the sum of the variables x and y. Our Particle Argon programs can be divided into four sections. The first section (lines 1–7) is where we create global names for pin assignments and define global variables (i.e., variables that can be accessed in any function throughout the program). The second section (lines 9–16) is where we define any helper functions (e.g., an add helper function to add two integers together). The third section (lines 18–24) is the setup function. The statements in the setup function execute only once at the very beginning of the program. The fourth and final section (lines 26–43) is the loop function. The statements in the loop function execute repeatedly over and over again. Throughout this lab, we will provide you the necessary code for the first three sections; you will be responsible for focusing on the loop function and possibly adding global variables where necessary. The Particle OS takes care of calling the setup and loop functions appropriately. The statement on line 23 is a call to the pinMode function which is provided by the Particle OS. The pinMode function configures the given pin number to be either an input pin or an output pin. Since we assigned the value D7 to the variable led_pin, we are effectively configuring digital pin D7 to be an output. Digital pin D7 corresponds to the small blue LED by the USB connector on the Particle Argon (see Figure 4). The iteration statement on line 34 is a for loop which is used to execute the loop body multiple times. This specific code will execute the loop body on lines 35–38 z times. The loop body contains two calls to the digitalWrite function and two calls to the delay function. Both of these functions are provided by the Particle OS. The digitalWrite function will write the corresponding output pin with either a HIGH or LOW voltage, while the delay function tells the Particle Argon to wait for the given number of milliseconds before continuing to the next statement. Finally, on line 42 we wait an additional four seconds. Figure 9 shows the complete C++ programs that implement the algorithms shown in Figure 5. Notice how the digital pin D4 is setup to be an input pin in the IoT input device program, but it is setup to be an output pin in the IoT output device program. The Particle OS digitalRead function is used to read the button state in the IoT input device program, and the Particle OS digitalWrite function is used to write the LED in the IoT output device program. The IoT input device program in Figure 9(a) uses the Particle.publish function to send messages (also called events) to the IoT cloud (see line 13 and 16). The IoT output device program Figure 9(b) uses the Particle.subscribe function to indicate what other function should be called to receive messages from the IoT cloud. Line 16 specifies that the receive_msg function should be called which is defined on lines 3–11. The Particle OS will call this function with the name of the event and the message as C strings. C strings can be quite tricky to work with, so care is required. On line 5, we use the strcmp function to compare the string variable msg to string literal "on". The strcmp function returns 0 if the two strings match. 6 CURIE Academy, Summer 2021 Lab 2 Notes: Computer Engineering – Software Perspective 1 // Global constants for pin assignments and global variables 2 3 int led_pin = D7; 4 5 int x = 2; 6 int y = 3; 7 int z = 0; 8 9 // Helper functions 10 11 int add( int a, int b ) 12 { 13 int sum; 14 sum = a + b; 15 return sum; 16 } 17 18 // The setup routine runs once when you press reset 19 20 void setup() 21 { 22 // Configure led_pin as digital output 23 pinMode( led_pin, OUTPUT ); 24 } 25 26 // The loop routine runs over and over again 27 28 void loop() 29 { 30 // Do the addition 31 z = add( x, y ); 32 33 // Blink LED z times 34 for ( int i = 0; i < z; i++ ) { 35 digitalWrite( led_pin, HIGH ); // Turn on the LED 36 delay(500); // Wait 0.5 seconds 37 digitalWrite( led_pin, LOW ); // Turn off the LED 38 delay(500); // Wait 0.5 seconds 39 } 40 41 // Wait four seconds 42 delay(4000); 43 } Figure 8: Complete Example C++ Program 7 CURIE Academy, Summer 2021 Lab 2 Notes: Computer Engineering – Software Perspective Figure 11: Particle Console Device Page 10 CURIE Academy, Summer 2021 Lab 2 Notes: Computer Engineering – Software Perspective Figure 12: Particle Console Event Page 11 CURIE Academy, Summer 2021 Lab 2 Notes: Computer Engineering – Software Perspective 6. Instruction Set Architecture: ARM Machine Instructions The compiler translates the high-level statements used in the programming language into very sim- ple machine instructions which are part of an instruction set architecture. Figure 13 illustrates a very simple architecture with a processor to compute on data and two kinds of memory to store data: main memory is slow but can store a large amount of data, while registers are fast but can only store a small amount of data. This simple architecture supports three kinds of machine instructions: load instructions move values from memory into registers; store instructions move values from registers into memory; and arithmetic instructions perform simple arithmetic on values stored in registers. As mentioned in the previous section, the compiler transforms the sequence of high-level statements in Figure 6 into a sequence of machine instructions the processor can understand. Figure 14 shows the actual machine instructions generated by the compiler for line 5 in Figure 6(b). There are four ma- chine instructions: the first two machine instructions load values from main memory into registers, the third machine instruction adds these two values together, and the final machine instruction stores the sum back out to main memory. Note that the processor can use a ripple-carry adder similar to the one you developed in Lab 1 to implement the add machine instruction which means our tour of the computer systems stack is complete! In Lab 1, we explored technology to machine instructions, and in lab 2 we explored from applications to machine instructions. We meet in the middle of the stack at the instruction set architecture, where hardware meets software and software meets hardware! Main Memory Registers Load/Store Instructions Arithmetic Instructions Figure 13: Simple Architecture 1 # load two values from main memory into two registers 2 ldr r2, [r7, #4] 3 ldr r3, [r7] 4 5 # do the actual addition 6 add r3, r3, r2 7 8 # store the sum from a register back into main memory 9 str r3, [r7, #12] Figure 14: Machine Instructions for Line 5 in Figure 6(b) 12
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved