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

Similar documents
Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

Introduction to C. Zhiyuan Teo. Cornell CS 4411, August 26, Geared toward programmers

Introduction to C. Sean Ogden. Cornell CS 4411, August 30, Geared toward programmers

Introduction to C. Ayush Dubey. Cornell CS 4411, August 31, Geared toward programmers

Structures and Pointers

PRINCIPLES OF OPERATING SYSTEMS

Lectures 5-6: Introduction to C

A Fast Review of C Essentials Part I

CS349/SE382 A1 C Programming Tutorial

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

Lectures 5-6: Introduction to C

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

Class Information ANNOUCEMENTS

Why learn C (after Java)? C for Java programmers. What does this C program do? Simple Example. Goals of this tutorial

A brief introduction to C programming for Java programmers

Kurt Schmidt. October 30, 2018

Chapter IV Introduction to C for Java programmers

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

CS 61C: Great Ideas in Computer Architecture Introduction to C

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

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Dynamic Memory Allocation and Command-line Arguments

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

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

Complex data structures. Cedric Saule

C - Basics, Bitwise Operator. Zhaoguo Wang

Topic 6: A Quick Intro To C

Motivation was to facilitate development of systems software, especially OS development.

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

In Java we have the keyword null, which is the value of an uninitialized reference type

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

CS61, Fall 2012 Section 2 Notes

Princeton University COS 333: Advanced Programming Techniques A Subset of C90

A flow chart is a graphical or symbolic representation of a process.

BLM2031 Structured Programming. Zeyneb KURT

Contents. Preface. Introduction. Introduction to C Programming

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

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

Motivation was to facilitate development of systems software, especially OS development.

Dynamic memory allocation (malloc)

Lecture 2: C Programm

Contents. A Review of C language. Visual C Visual C++ 6.0

C Introduction. Comparison w/ Java, Memory Model, and Pointers

COMP26120: Pointers in C (2018/19) Lucas Cordeiro

CSCI 171 Chapter Outlines

Processes. Johan Montelius KTH

C Language Summary. Chris J Michael 28 August CSC 4103 Operating Systems Fall 2008 Lecture 2 C Summary

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Introduction to C. Winter 2019

Room 3P16 Telephone: extension ~irjohnson/uqc146s1.html

A process. the stack

CS3157: Advanced Programming. Outline

Programming refresher and intro to C programming

C for Java programmers. Carlo U. Nicola, FHNW SG I Loosely based on notes of I. Gupta Cornell University

ELEC 377 C Programming Tutorial. ELEC Operating Systems

Pointers (continued), arrays and strings

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Arrays and Pointers in C. Alan L. Cox


CS Programming In C

EMBEDDED SYSTEMS PROGRAMMING Language Basics

o Code, executable, and process o Main memory vs. virtual memory

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

Lecture07: Strings, Variable Scope, Memory Model 4/8/2013

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

High Performance Computing

EL6483: Brief Overview of C Programming Language

EL2310 Scientific Programming

Pointers (continued), arrays and strings

Programming in C - Part 2

just a ((somewhat) safer) dialect.

CSC 1600 Memory Layout for Unix Processes"

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

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

Physics 2660: Fundamentals of Scientific Computing. Lecture 3 Instructor: Prof. Chris Neu

EC 413 Computer Organization

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

Work relative to other classes

Pointers as Arguments

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

Programing in C. Pierre-Alain FOUQUE

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

Fundamental of C programming. - Ompal Singh

CE221 Programming in C++ Part 1 Introduction

Fall 2017 CISC124 9/16/2017

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

211: Computer Architecture Summer 2016

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

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

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

ECE551 Midterm Version 2

Programming. Pointers, Multi-dimensional Arrays and Memory Management

CS 11 C track: lecture 5

CSE 303: Concepts and Tools for Software Development

Variables and literals

CS C Primer. Tyler Szepesi. January 16, 2013

Transcription:

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 of Android. 2

C in a nutshell C is not Python or Java. C is hard to learn and sometimes can be hard to handle. 3

C is everywhere C is fast. C is a small language. C is designed to be compiled by simple compilers. C map efficiently machine instructions. C is portable and works on every platforms. C is used by the 99% of the Operating Systems. Every developer must know at least the basic notions. 4

The reality Something is wrong! 5

C is powerful C offers some special features ideally for system programming: Explicit memory management Explicit error detection Low-level features like bit operations No complex pre-built data-structures This means more control but more possible mistakes. 6

The first example /* Hello World program */ #include <stdio.h> int main() { printf("hello World.\n ); return 0; } Compile phase: gcc -o hello hello.c Execute phase:./hello Anyone doesn t know this stuff? 7

Primitive types The basic and the most powerful one: void The none type: NULL The integers: char, short, int, long, long long The floating points: float, double, long double Only by the standard C99 is define the boolean type but usually it is not used to back compatibility. Use instead 1 or 0. Every type can be signed or not signed. The size of every type depends on the platforms. chart -> 1 bytes short -> 2 bytes int -> 4 bytes long -> 8 bytes float -> 4 bytes double -> 8 bytes long double -> 16 bytes 8

Integer with different bases Usually we represent integer with base 10 but sometimes is not the best choice for the embedded systems. C allows to handle some special representation adding the following prefixes: Hexadecimal (base 16): 0x (%x to use with printf) Octal (base 8): 0 (%o to use with printf) There isn t a standard support for the binary representation but you can write your own functions to translate a base 10 int to a base 2 int. I hope you will do for the next lesson! 9

Bitwise operations in C Symbol Operator Example & Bitwise AND 101 & 001 = 001 Bitwise Inclusive OR 110 101 = 111 ^ XOR 110 ^ 100 = 011 << Left shift 00110 << 2 = 1100 >> Right shift 00110 >> 2 = 0011 ~ Bitwise Not (1 complement) ~110 = 001 10

Not so complex types There isn t a predefined type for a string or for other structures like map/ dictionary without using external libraries (not always suggested). What is a string in C? A sequence of chars terminated by a \0. char[] course = IoT17\0 ; There are some utilities function to manipulate strings (string.h): strncpy, strncmp, strncat 11

Not so complex types (2) What is an array in C? Same story. A sequence of a predefined type but without \0 char. int a[10]; for (int i = 0; i < 10; i++) { a[i] = 0; } There are some utilities function to manipulate data: memcpy, memcmp 12

Enumerations An enum is a user-defined data type that consists of constants. enum months { JANUARY, FEBRUARY, MARCH }; enum months { JANUARY = 1, FEBRUARY = 3, MARCH }; enum week {sunday, monday, tuesday, wednesday, thursday, friday, saturday}; int main() { enum week today; today = wednesday; printf("day %d, today + 1); return 0; } Each element of enum gets an integer value and used like an integer. 13

Structures A struct is used to aggregate different data with the same meaning. struct birthday { char* name; enum months month; int day; int year; }; struct my_birthday; my_birthday.name = Fabrizio\0 my_birthday.month = JUNE; Struct can refer also to other structures. 14

SEEMS EASY, DOESN T IT? 15

Pointers A pointer is a special variable that contains variable address. int* pointer = &var; &var is the address of the variable *var is the pointer to the variable If you assign a value to the pointer you modify also the original variable: *pointer = 2; 16

Pointers (2) A pointer variable must be always initialized with a valid address or with the NULL value. A pointer point to a address but you have to specify a type to understand what is written inside the address. char short int 1 2 3 4 5 6 long 7 8 pointer 17

Enter the void Void means nothing, emptiness. A function returning void actually does not return anything. The size of void is undefined for this reason. void* pointer; is used to store addresses of unspecified types You can cast to a specific type to do specific tasks. void* pointer; (int) pointer; NEVER READ OR WRITE A NOT ALLOWED MEMORY AREA 18

Pointer arithmetic char* pointer; 1 2 3 4 5 6 7 8 pointer pointer += 1 char my_array[5]; my_array[3] = 0; &my_array += 3 or &my_array += 3 * sizeof(type) 19

Memory management Global Variables Declared outside the body function Space allocated statically before program execution Cannot deallocate space until program finishes Name has to be unique for the whole program Local Variables Declared in the body of the function Space allocated when entering the function Space automatically deallocated when functions returns NEVER REFER TO A LOCAL VARIABLE AFTER THE FUNCTION RETURN 20

Memory management (2) Dynamic Variables Memory has to be explicitly allocated Memory has to be explicitly deallocated (only one time) Dynamic variables are allocated in the the heap (a special area of memory) Allocate memory: void* malloc(int) Deallocate memory: void* free(void*) Reallocate memory: void* realloc(void *ptr, size_t size) 21

Functions In C you can pass the values to the functions by: Value Reference /* arguments passed by value */ int sum(int a, int b) { return (a + b); /* return by value */ } /* arguments passed by reference */ int psum(int* pa, int* pb) { return ((*pa) + (*pb)); } int psum(int *p) { *p = a + b; /* return by reference */ } 22

Functions (2) In C is also defined a pointer to a function. Usually used to force another programmer to implement their own version of common functionalities. The OS needs specific tasks such as sendpacket() and receivepacket(). We can implement our version and pass to a function by reference. Another example is to implement specific comparator or iterator such as is common in Java Programming. void myproc(int d) {} void mycaller(void (*f)(int), int param) { f(param); /* call function f with param */ } void main(void) { mycaller(myproc, 10); /* call myproc using mycaller */ } 23

Compiling phase.o file.c files Preprocessor Compiler Linker.h files libs 24

Preprocessor commands We can use special functions of preprocessors to help us Import modules /* include standard library declaration */ #include <stdio.h> /* include custom declarations */ #include myheader.h Symbol definition #define DEBUG 0 #define MAX_LIST_LENGTH 100 if (DEBUG) printf("max length of list is %d.\n", MAX_LIST_LENGTH); 25

Preprocessor commands (2) Conditional compilation #ifdef DEBUG printf("debug: line " _LINE_ " has been reached.\n"); #endif #ifndef HEADER_H #include header.h #endif Macro definition #define min(x, Y) ((X) < (Y)? (X) : (Y)) int res = min(1, 2); 26

You have always to remember To initialize variables before using, especially pointers. The life of pointer should be short and equal to the life of the pointed object. Do not return local variables by reference Do not dereference (*p) pointer before initialization or deallocation C has no error handler except for assert system. You always should do error handling by your hand. C require practices, focus and you should always follow standards, convention and what suggested by the operating system or by the platform you are working on. Check Google and StackOverflow. On desktop you can use valgrind to check your program or gdb to debug. 27

28