ELEC3730 Embedded Systems Lecture 1: Introduction and C Essentials



Similar documents
Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Sources: On the Web: Slides will be available on:

Keil C51 Cross Compiler

The programming language C. sws1 1

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

Informatica e Sistemi in Tempo Reale

Programming Microcontrollers in C

Pemrograman Dasar. Basic Elements Of Java

C / C++ and Unix Programming. Materials adapted from Dan Hood and Dianna Xu

C Programming. for Embedded Microcontrollers. Warwick A. Smith. Postbus 11. Elektor International Media BV. 6114ZG Susteren The Netherlands

The C Programming Language course syllabus associate level

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

How To Write Portable Programs In C

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

About The Tutorial. Audience. Prerequisites. Copyright & Disclaimer

C++ Language Tutorial

Computer Programming Tutorial

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

ELEG3924 Microprocessor Ch.7 Programming In C

Programing the Microprocessor in C Microprocessor System Design and Interfacing ECE 362

C++ Programming Language

C++ Essentials. Sharam Hekmat PragSoft Corporation

1 Abstract Data Types Information Hiding

PROGRAMMING IN C PROGRAMMING IN C CONTENT AT A GLANCE

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

Object Oriented Software Design

Embedded C Programming, Linux, and Vxworks. Synopsis

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

COS 217: Introduction to Programming Systems

Objective-C Tutorial

Why using ATmega16? University of Wollongong Australia. 7.1 Overview of ATmega16. Overview of ATmega16

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Example of a Java program

CS 106 Introduction to Computer Science I

Chapter 2 Introduction to Java programming

Lecture 22: C Programming 4 Embedded Systems

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

C PROGRAMMING FOR MATHEMATICAL COMPUTING

Stacks. Linear data structures

Fundamentals of Programming

C++ INTERVIEW QUESTIONS

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Object Oriented Software Design

SECTION C [short essay] [Not to exceed 120 words, Answer any SIX questions. Each question carries FOUR marks] 6 x 4=24 marks

DNA Data and Program Representation. Alexandre David

Introduction to Java

Chapter 1 Java Program Design and Development

C++FA 5.1 PRACTICE MID-TERM EXAM

MISRA-C:2012 Standards Model Summary for C / C++

El Dorado Union High School District Educational Services

Top 10 Bug-Killing Coding Standard Rules

MPLAB TM C30 Managed PSV Pointers. Beta support included with MPLAB C30 V3.00

Moving from CS 61A Scheme to CS 61B Java

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Comp151. Definitions & Declarations

CSC230 Getting Starting in C. Tyler Bletsch

Embedded C Programming

J a v a Quiz (Unit 3, Test 0 Practice)

VB.NET Programming Fundamentals

5 Arrays and Pointers

Variables, Constants, and Data Types

Computers. Hardware. The Central Processing Unit (CPU) CMPT 125: Lecture 1: Understanding the Computer

Fast Arithmetic Coding (FastAC) Implementations

2 ASCII TABLE (DOS) 3 ASCII TABLE (Window)

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Statements and Control Flow

Chapter One Introduction to Programming

Smart Cards a(s) Safety Critical Systems

An Incomplete C++ Primer. University of Wyoming MA 5310

CHAPTER 4 MARIE: An Introduction to a Simple Computer

Install Java Development Kit (JDK) 1.8

C Programming Tutorial

GCSE. Mark Scheme for January Computing (Pilot) General Certificate of Secondary Education Unit A451: Computer systems and programming

Memory Systems. Static Random Access Memory (SRAM) Cell

An Overview of Java. overview-1

Object Oriented Software Design II

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Java Interview Questions and Answers

A C Test: The 0x10 Best Questions for Would-be Embedded Programmers

Java (12 Weeks) Introduction to Java Programming Language

Chapter 2 Basic Structure of Computers. Jin-Fu Li Department of Electrical Engineering National Central University Jungli, Taiwan

Lecture 5: Java Fundamentals III

CHAPTER 7: The CPU and Memory

6.S096 Lecture 1 Introduction to C

Tutorial on C Language Programming

ADVANCED PROCESSOR ARCHITECTURES AND MEMORY ORGANISATION Lesson-12: ARM

Microcontroller Systems. ELET 3232 Topic 8: Slot Machine Example

The Cool Reference Manual

arrays C Programming Language - Arrays

Programming languages C

Coding Rules. Encoding the type of a function into the name (so-called Hungarian notation) is forbidden - it only confuses the programmer.

Freescale Semiconductor, I

Transcription:

ELEC3730 Embedded Systems Lecture 1: Introduction and C Essentials Overview of Embedded Systems Essentials of C programming - Lecture 1 1 Embedded System Definition and Examples Embedded System Definition: A device incorporating a microprocessor Aerospace Navigation systems, landing control; Automotive Cruise Control, anti-lock braking systems; Communications Satellites, network routers, mobile basestation; Industrial Elevators, security/environment control; Personal Mobile handset, MP3 player, GPS, PDA; Home Dishwasher, home theatre components, air conditioning control; Instrumentation Oscilloscope, signal generators, spectrum analyser, data loggers 2 DLINK Router: 16 bit ARM Processor 4 Microprocessor 6 RAM 7 Flash RAM 15 Ethernet Bridge 3

Miele Dishwahser - 8bit Motorolla 68HC05 4 NASA Mars Sojourner: 8 bit Intel 80C85 5 Sony Aibo Dog: 64 bit MIPS Reduced Instruction Set up 6

Embedded Systems - Key Aspects, Goals and Tools Goals Reliability Failure may be Life-Threatening; Required service times: 24/7/365; Can t reboot Low Cost High Volume Small unit savings=large total savings Low Development Time Portability Minimize RAM, clock speed, processor bus width longer battery life Tools Multitasking and Scheduling Multiple simultaneous operations; Assembly Language Optimized I/O, efficient implementations; High Level Language Fast development cycle C is language of choice for embedded designs 7 Widespread Adoption of C 8 Demand for programmins skills (July 2006) 9

Syntatically - Java and C very similar Operators same as Java: Arithmetic i = i+1; i++; i--; i *= 2; +, -, *, /, %, Relational and Logical <, >, <=, >=, ==,!= &&,, &,,! Syntax same as in Java: if ( ) { } else { } while ( ) { } do { } while ( ); for(i=1; i <= 100; i++) { } switch ( ) {case 1: } continue; break; 10 C language - Essential points Very simple structure - only 32 keywords High Acceptance; Compilers can be developed quickly for new platforms; Low-level nature programmer has close control Aggressive optimisation possible Often, only hand-tuned assembly language code runs faster, Low-level access to computer memory via pointers possible Negatives Need libraries to implement interesing functions No error checking inherint to language bugs! Eg - arrays may go beyond bounds (buffer overflow) 11 Essentials of C for Embedded Programming Topics Covered: Variables and statements Control and loops Functions Bitwise Operations Arrays and strings Structures Pointers The standard library Memory Management Extensions Mixing C and Assembly Accommodating Multiple Processes (Threads) Employing Real Time Operating System 12

C Program Development Editor Program is created in the editor and stored on disk 1 Edit Preprocessor Preprocessor program processes the code 2 Preprocess Compiler Compiler creates object code and stores it on disk 3 Compile 4 Link 5 Load 6 Execute Linker Loader CPU Primary Memory Primary Memory Linker links the object code with the libraries, creates aout and stores it on disk Loader puts program in memory CPU takes each instruction and executes it, possibly storing new data values as the program executes 13 C, the language, is just 32 keywords auto break case char const continue default do do else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while 14 A first C program #include <stdioh> void main(void) { printf( Hello World\n ); /* print out a message */ return; } C:\elec3730>Hello World Essential Points: #include <stdioh> = include header file stdioh No semicolon at end Small letters only C is case-sensitive void main(void){ } is the only code executed /* = start of comment block, */ = end of comment block \n = newline, \t = tab, \a = alarm (beep), \p=backspace \f = formfeed, \r = carriage return, \\= backslash, \v=vertical tab 15

Contents of stdioh - A first look at header files void perror(const char *); int printf(const char * restrict, ); int putc(int, FILE *); int putchar(int); int puts(const char *); int remove(const char *); int rename (const char *, const char *); void rewind(file *); int scanf(const char * restrict, ) ; void setbuf(file * restrict, char * restrict); int setvbuf(file * restrict, char * restrict, int, size_t); int sprintf(char * restrict, const char * restrict, ); int sscanf(const char * restrict, const char * restrict, ); 16 4 Basic Data Types Type Definition Byte Size char Character 1 int Integer 2 or 4 float Real-single precision 4 double Real-double precision 8 17 Data Ranges and Qualifiers char unsigned char signed char int unsigned int signed int short int unsigned short int signed short int long int unsigned long int signed long int float double long double ASCII Characters 0 to 255-128 to 127-32,768 to 32,767 0 to 65535 As int As int As unsigned int As int -2,147,483,648 to 2,147,483,647 0 to 4294967295 Same as long int Six digits of precision approximately Twelve digits of precision approximately Twenty four digits of precision approximately 18

Declarations and Statements Example of data declarations (Memory is reserved): float x; double d = 5; int *p, i, a[100]; char s[21]; Syntax: type variable_name, [= value]; Rules: declarations must precede executable statements int type may be modified by: long, short, unsigned char and int type may be modified by unsigned C program is execution of one statement after another An expression becomes a statement when terminated with a semicolon ; Braces { } around a group of statements form a compound statement Syntatically equivalent to a single statement 19 Changing Variable Values Example: int x, y, z; x = 2; x = x + 1; Getting Fancy y = z = 4 + 5; x += 1; ++x; x++; y = x--; Note: assignment statements return value, which may be ignored; same goes for increment statements 20 Pre- and Post Increment/Decrement Operators Operate on the value assigned to a variable either before or after all other processing The difference is the location ivar = 3 and inum = 5 prior to each of the following: ivalue = ivar++ + inum++; ivalue = 8, ivar = 4, and inum = 6 at the end; ivalue = ++ivar + inum++; ivalue = 9, ivar = 4, and inum = 6 at the end; ivalue = ++ivar + ++inum; ivalue = 10, ivar = 4, and inum = 6 at the end 21

Special Assignment Operators Can combine assignment and arithmetic operators Provides shorthand notation Does not compile more efficiently Harder to read and understand Because of above, always used (?) x-=2 x = x - 2 (where 2 can be any value) x+=2 x = x + 2 x*=2 x = x * 2 x/=2 x = x / 2 x%=2 x = x % 2 22 Obfuscated C contest winner 2003 (time > speaker) #include<unistdh> #include<timeh> #define k ("C9B7351A@D-/E+F?')G>H%J#=I"[(d[(i/13)*2]*91+d[(i/13)*2+1]-3220)&(\ 4096>(i%13))?l+1:l]-59) #define g(n)e(n<13?n:n<20?n+1:n>20?11+n/10:13,0);e(n>12&&n<20?26:n>20&&n%10?n%\ 10:-1,2); #define x(n)g(localtime(&a)->tm_##n) unsigned char *d="kzs2ittwhwzyvec@jbyxojf9-tzrgdb/el7#q(`sz# _netwq\\mqj5cvgte\ K-ReK-(Mq8+D'6Ui0tG88vXJ-Tu{VI=d%cR]h7CumwBq\\-#{thj8fw$OEfEvLHP13_##wOD[7Cw2\ ]<T{ [F}L;L:*+A#PwLnp{9'M3Mr w_ unm'}$*(5]_$?o9zo{{wz4p6vp8ipu}$bqospf=-isnyl'\ g53o^c`ov-p`-x+ ZAd<e?<'b9P LkZOf{B-8`K([srqv&gy1,:}$ s7d{yn6m#cqykpc *_ #xa#\ 'YfQ}$k$kr7dM#dcnWDg PHdA#^j&q}$x@(a;k2JB]50ZKp{xRwbkTmv\\a3fJ@V[`J#fN`s szeh\ t$zmm#vtfq zrklea#p&fpowj#pf_$?/2#@ $w}$?flxa#^-}$cm^aa#&g#wiv?^ yy#s)lwt'k#on\ $",p[6789]; int o[]={145,1145,1745,2545,3045,4045,4345,5145,5745,6369},i=0,j=0,l=0; void e(n,h){ for(j=0;n>0;n-=!(p[j++]^9)); for(;!n&&j[p]^9;j++)write(1,p+p[j][o],o[p[j]+1]-p[j][o]); for(i=0;i<h[o];i++)write(1,d+7,1);} int main(){ time_t a=time(&a); for(;i<8476;j[p]=k>=0?j<*o?k-2:(p[j+1]=k<<4):0,j+=k>=0?1+(j>=*o):0, l=-k*(k<0),i++);e(21,4);x(hour)e(22,1);x(min)e(23,1);e(25,0);x(sec)e(24,0); return;} 23 Obfuscated C winner - square root of 2n digit integer #include <stdioh> int l;int main(int o,char **O, int I){char c,*d=o[1];if(o>0){ for(l=0;d[l ];D[l ++]-=10){D [l++]-=120;d[l]-= 110;while (!main(0,o,l))d[l] += 20; putchar((d[l]+1032) /20 ) ;}putchar(10);}else{ c=o+ (D[I]+82)%10-(I>l/2)* (D[I-l+I]+72)/10-9;D[I]+=I<0?0 :!(o=main(c/10,o,i-1))*((c+999 )%10-(D[I]+92)%10);}return o;} 24