Special Topics for Embedded Programming

Similar documents
CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

C Language Programming

CS Programming In C

ME 461 C review Session Fall 2009 S. Keres

5.Coding for 64-Bit Programs

UNIT- 3 Introduction to C++

Fixed-Point Math and Other Optimizations

Basic Types, Variables, Literals, Constants

A Fast Review of C Essentials Part I

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

Tokens, Expressions and Control Structures

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

The CPU and Memory. How does a computer work? How does a computer interact with data? How are instructions performed? Recall schematic diagram:

Lecture 03 Bits, Bytes and Data Types


Introduction to C. Why C? Difference between Python and C C compiler stages Basic syntax in C

LAB A Translating Data to Binary

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

Zheng-Liang Lu Java Programming 45 / 79

Number Systems for Computers. Outline of Introduction. Binary, Octal and Hexadecimal numbers. Issues for Binary Representation of Numbers

Work relative to other classes

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

The x86 Microprocessors. Introduction. The 80x86 Microprocessors. 1.1 Assembly Language

Final Labs and Tutors

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Computer System and programming in C

Chapter 1. Microprocessor architecture ECE Dr. Mohamed Mahmoud.

More Programming Constructs -- Introduction

Preview from Notesale.co.uk Page 6 of 52

Arduino Uno. Power & Interface. Arduino Part 1. Introductory Medical Device Prototyping. Digital I/O Pins. Reset Button. USB Interface.

Pre-Lab due: at 3 p.m. Post-Lab due: at 5 p.m. Name:... Forename:... Initials:...

EECS 461 Winter 2009 Lab 1: Familiarization and Digital I/O

Decimal & Binary Representation Systems. Decimal & Binary Representation Systems

CS401 - Computer Architecture and Assembly Language Programming Glossary By

AGH University of Science and Technology Cracow Department of Electronics

EL6483: Brief Overview of C Programming Language

Ch. 3: The C in C++ - Continued -

Binary Representations and Arithmetic

C Programming Language (Chapter 2 of K&R) Variables and Constants

Declaration and Memory

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

Java enum, casts, and others (Select portions of Chapters 4 & 5)

Embedded Systems - FS 2018

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Course Outline Introduction to C-Programming

Embedded programming, AVR intro

Declaration Syntax. Declarations. Declarators. Declaration Specifiers. Declaration Examples. Declaration Examples. Declarators include:

These are reserved words of the C language. For example int, float, if, else, for, while etc.

INTRODUCTION 1 AND REVIEW

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

Definition: Data Type A data type is a collection of values and the definition of one or more operations on those values.

CSE 351: The Hardware/Software Interface. Section 2 Integer representations, two s complement, and bitwise operators

Program SoC using C Language

EECS 388 C Introduction. Gary J. Minden August 29, 2016

Presented By : Gaurav Juneja

C LANGUAGE AND ITS DIFFERENT TYPES OF FUNCTIONS

Computers Programming Course 5. Iulian Năstac

Some Basic Concepts EL6483. Spring EL6483 Some Basic Concepts Spring / 22

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

ECE 250 / CS 250 Computer Architecture. C to Binary: Memory & Data Representations. Benjamin Lee

CPE300: Digital System Architecture and Design

Embedded Systems - FS 2018

The New C Standard (Excerpted material)

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

SIGNED AND UNSIGNED SYSTEMS

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

BLM2031 Structured Programming. Zeyneb KURT

Octal and Hexadecimal Integers

Computer Architecture and System Software Lecture 02: Overview of Computer Systems & Start of Chapter 2

First of all, it is a variable, just like other variables you studied

Programming (1.0hour)

Data Storage. August 9, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 August 9, / 19

CS 261 Fall Mike Lam, Professor Integer Encodings

data within a computer system are stored in one of 2 physical states (hence the use of binary digits)

CS61B Lecture #14: Integers. Last modified: Wed Sep 27 15:44: CS61B: Lecture #14 1

Chapter 3 Basic Data Types. Lecture 3 1

Using peripherals on the MSP430 (if time)

Fundamentals of Programming

XC Specification. 1 Lexical Conventions. 1.1 Tokens. The specification given in this document describes version 1.0 of XC.

Chapter 7. Basic Types

Rapid Software Development on 32-Bit Automotive MPC56xx Family of MCUs

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

CS107 Handout 13 Spring 2008 April 18, 2008 Computer Architecture: Take II

Hacking in C. The C programming language. Radboud University, Nijmegen, The Netherlands. Spring 2018

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

Procedures, Parameters, Values and Variables. Steven R. Bagley

Programming in C++ 6. Floating point data types

Lab 5: EBI and ADC: Digital Voltmeter

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

ECE2049 E17 Lecture 4 MSP430 Architecture & Intro to Digital I/O

UNIT 3 OPERATORS. [Marks- 12]

Basic Types and Formatted I/O

ECE2049: Embedded Computing in Engineering Design A Term Fall Lecture #9: Exam Review w/ Solutions

History of the Microprocessor. ECE/CS 5780/6780: Embedded System Design. Microcontrollers. First Microprocessors. MC9S12C32 Block Diagram

Number Systems. Decimal numbers. Binary numbers. Chapter 1 <1> 8's column. 1000's column. 2's column. 4's column

Transcription:

1 Special Topics for Embedded Programming ETH Zurich Fall 2018 Reference: The C Programming Language by Kernighan & Ritchie 1

2 Overview of Topics Microprocessor architecture Peripherals Registers Memory mapped I/O C programming for embedded systems Lab 1: General Purpose I/O Read data from input pins and write to output pins on the MPC5553 GPIO example code 2

3 Freescale MPC55xx Architecture 132 MHz 32-bit PowerPC, Temperature range: -40 to 125ºC 1.5 MB of embedded Flash 64 KB on-chip static RAM 8 KB of cache 210 selectable-priority interrupt sources 3 DSPI (serial peripheral interface) 2 esci (serial communications) GPIO 2 x 40-ch. ADC 24-ch. emios 2 CAN 32-ch. etpu Direct Memory Access (DMA)

4 Registers and Addresses Microprocessor memory has location (an address, specified as a hexadecimal number) and contents (the data stored at a specific address in memory) 1.5 MB on-chip non-volatile flash memory for both instructions and data 64 KB static RAM (SRAM) Registers are memory locations used for calculations, to initialize the processor or check its status, or to access peripheral devices. General purpose registers Special purpose registers Memory-mapped I/O refers to communication between the CPU and its peripheral devices using the same instructions, and same bus, as between CPU and memory Peripheral devices are accessed by writing to and reading from special purpose registers Address maps in the user manual define the address, name and size of each special purpose register

5 General Purpose Registers Generally used for storage and manipulation of data required in the program execution Faster access than main memory MPC5553 has thirty-two 64-bit general purpose registers C compilers automatically use these registers as resources to load/store data and perform calculations Unless you re writing assembly code, you won t have to read from or write to any GPR We ll discuss assembly code, but we won t write any 5

Special Purpose Registers: Memory Mapped I/O 6 Access peripherals by writing to and reading from memory Each peripheral has a fixed range of memory assigned to it - Registers are identified with a name, an offset from the base address, and size - Each address refers to an 8-bit byte of memory Lots of registers associated with each peripheral - Peripheral configuration registers - Peripheral status registers - inputs from the hardware - outputs to the hardware System Interface Unit (SIU) is used to manage the hardware pins that are used for input and output (you ll do this in lab #1)

Note that Addresses are Hexadecimal 7 Binary (base 2) There are 10 types of people in the world: Those who know binary and those who don t Binary number 1011 converted to decimal is: (1 2 0 ) + (1 2 1 ) + (0 2 2 ) + (1 2 3 ) = 11 Hexadecimal (base 16) 0 9 represent values zero to nine A, B, C, D, E, F represent values ten to fifteen Hexadecimal number 2AF3 converted to decimal is: (3 16 0 ) + (15 16 1 ) + (10 16 2 ) + (2 16 3 ) = 10,995 Compiler recognizes 0x2af3 or 0x2AF3 as a hexadecimal number Hex to binary conversion (and back) is easy: Each hex digit is a 4-bit nibble (2 nibbles are an 8-bit byte) 2AF3 converted to binary is: 0010 1010 1111 0011 7

8 C Programming for Embedded Systems 8

Primitive Data Types, Data Declaration Integer data types Have both size and sign char (8-bit) short (16-bit) int (32-bit) long (32-bit) signed (positive and negative) unsigned (positive only) Floating-point types Only have size Can always be positive or negative float (32-bit) double (64-bit) 12.34: constant of type double 12.34f or 12.34F: constant of type float Data declarations should be at the top of a code block { /* Top of Code Block */ signed char A; char input; unsigned short var; int output; unsigned long var2; float realnum; double realnum2; } 9 9

10 Freescale Defined Types See freescale/typedefs.h typedef signed char int8_t; typedef unsigned char uint8_t; typedef volatile signed char vint8_t; typedef volatile unsigned char vuint8_t; typedef signed short int16_t; typedef unsigned short uint16_t;

11 Functions and Function Prototypes Function Prototype declares name, parameters and return type prior to the functions actual declaration Information for the compiler; does not include the actual function code You will be given function prototypes to access peripherals Pro forma: RetType FcnName (ArgType ArgName, ); int Sum (int a, int b); /* Function Prototype */ void main() { } c = Sum ( 2, 5 ); /* Function call */ int Sum (int a, int b) /* Function Definition */ { return ( a + b ); }

12 C vs. C++ C++ language features cannot be used No new, delete, class Comment with /* */, not // Variables must be declared at top of code blocks { int j; double x; /* code */ int q; /* only in C++, not in C */ /* code */ } 12

13 Useful Features for Embedded Code Type Qualifiers Volatile Static Pointers Structures Unions Bit Operations Integer Conversions 13

14 Volatile Type Qualifier Variables that are reused repeatedly in different parts of the code are often identified by compilers for optimization. These variables are often stored in an internal register that is read from or written to whenever the variable is accessed in the code. This optimizes performance and can be a useful feature Problem for embedded code: Some memory values may change without software action! Example: Consider a memory-mapped register representing a DIP-switch input Register is read and saved into a general-purpose register Program will keep reading the same value, even if hardware has changed Use volatile qualifier: Value is loaded from or stored to memory every time it is referenced Example: volatile unsigned char var_name; Freescale type: vuint8_t var_name; 14

15 Static Type Qualifier Two uses: in a file and in a code block (e.g., function) 1) Declaring a variable static in a file limits the scope of that variable to the file in which it is declared. The variable will not conflict with other variables of the same name defined in other files. Similarly, declaring a function static in a file limits the scope of that function to the file. 2) Declaring a variable static in a function allocates it a persistent memory location and it retains its value between successive function calls. In embedded code we often want a variable to retain its value between function calls Consider a function that senses a change in the crankshaft angle: The function needs to know the previous angle in order to compute the difference Example: static int x = 2 ; 15

Global Variables 16 variables that are visible to routines in all files, not just the file in which they are defined such variables are said to have program scope Use of global variables can make a program difficult to understand and maintain To make a variable local, declare it with the static keyword: int j; /* global */ static int k; /* local */ main () { } 16

17 Pointers Every variable has an address in memory and a value A pointer is a variable that stores an address The value of a pointer is the location of another variable The size of a pointer variable is the size of an address 4 bytes (32 bits) for the MPC5553 Two operators used with pointers & operator returns the address of a variable * is used to de-reference a pointer (i.e., to access the value at the address stored by a pointer) 17

18 Pointer Example Address Value Variable 0x100 5 x 0x104 5 y 0x108 0x100 ptr int x, y; /* x is located at address 0x100 */ int *ptr; /* This is how to declare a pointer */ x = 5; ptr = &x; /* The value of ptr is now 0x100 */ y = *ptr; /* y now has the value 5 */ *ptr = 6; /* The value at address 0x100 is 6 */ 18

More Pointer Examples 19 0 1 1 Declare a pointer, volatile int *p; Assign the address of the I/O memory location to the pointer, p = (volatile int*) 0x30610000; /* 4-byte long, address of some memorymapped register */ Output a 32-bit value by setting the value of bytes at 0x30610000, *p = 0x7FFFFFFF; /* Turn on all but the leftmost bit */ 19

20 Structures A struct holds multiple variables Divides range of memory into pieces that can be referenced individually Example: The date consists of several parts struct date { int day; int month; }; int year; Recall: We can treat a peripheral s memory map as a range of memory. Result: We can use a structure and its member variables to access peripheral registers. Structure date date.day 10 09 date.month 2018 date.year 20

21 Structures Access structure member variables using. and ->. is used with structures -> is used with pointers-tostructures Example current_time is a variable of type time pcurrent_time is a pointer to a structure (pointers to structures have special properties which will be useful in accessing arrays of structures) Assign hour, minute and second Increment minute /* Definition */ struct time { second; int hour, minute, }; Void main() { struct time current_time; struct time *pcurrent_time; current_time.hour = 12; current_time.minute = 0; current_time.second = 0; pcurrent_time = &current_time; pcurrent_time -> minute++; }; /* same as (*pcurrent_time).minute++ */

22 Structure bit-fields Number of bits per data member in structures is specified with :n struct RGB_color{ }; unsigned char r:2; /* 2-bits */ unsigned char g:2; unsigned char b:2; /* padding */ unsigned char:2; /*don t need to explicitly do this. */ r g b (padding) /* reset g */ struct RGB_color clr; clr.g = 0 ; 8 bits

23 Bit-Fields Diagram What if sometimes we want to access individual bit fields, and other times we want the whole register? r g b space 0x0000 0x0100 0x0200

24 Unions Multiple ways to view memory locations Unions contain member variables Member variables share same memory All variables are overlaid on each other Changing one member results in the other members being changed Union is as long as longest member Two ways To read the Same memory location Memory Location 0x0716 rgb: 6 padding r : 2 g : 2 b : 2 padding 24

25 Unions rgb clr: r g b space 0x00 0x02 0x04 0x06 union RGB_color{ struct { unsigned char r:2,g:2,b:2; unsigned char:2; }IND; struct { unsigned char rgb:6; unsigned char:2; }ALL; }; union RGB_color clr;

26 Pros & Cons of Using Structures Pros Readable code Simple way to set or clear individual bits Cons Relies on the compiler implementation Assembly code for a simple bit-write is much longer than if registers were written to directly More information on unions from Kernighan and Ritchie (see handout on website) 26

27 Constants and Bit Operations Tools for Manipulating Values 27

28 Integer Constants Binary constant: 0bnumber Example: 0b10110000 Octal constant: 0number (prefix is a zero) Example: 0775 Hexadecimal constant: 0xnumber Example: 0xffff or 0xFFFF 28

29 Bit Operations Bit-shifts Right shift: Left shift: num >> shift 0b1001 >> 2 0b0010 num << shift 0b0011 << 2 0b1100 Masking Bit-or: num mask 0b0001 0b1000 0b1001 Bit-and: num & mask 0b1001 & 0b1000 0b1000 Complement Not: ~ num ~ 0b0101 0b1010 Set bits set the 5th bit x = x (1 << 4); set the 5th bit x = (1 << 4); Clear bits clear 5th and 6th bit x = x & ~(0b11 << 4); clear 5th and 6th bit x &= ~(0b11 << 4); 29

30 Type Conversions Explicit Casts For specifying the new data type Syntax: (type-name)expression Example: (int)largevar Integral Promotion Before basic operation ( + - * / ), both operands converted to same type The smaller type is promoted (increased in size) to the larger type Value of promoted type is preserved Implicit Casts Assigning a value into a different type Widening conversion preserve value of expression short x = 10; long y = x; Narrowing conversions do NOT preserve value unsigned long x = 257; unsigned char y = x; 30

31 Type Conversions More information on integral promotion from K&R on website 31

32 Integer Division When dividing two numbers, may receive unexpected results If both operands are integers, then result is integer Expected result of division is truncated to fit into an integer If one operand is floating-point, then result is floating-point Integer operand is promoted to floating-point Receive expected result /* floating-point results */ (5.0 / 2.0) 2.5 /* no promotion, result is float */ (5.0 / 2) 2.5 /* operand 2 promoted to float */ (5 / 2.0) 2.5 /* operand 5 promoted to float */ /* integer-valued results */ (5 / 2) 2 /* no promotion, result is integer */ 32