Overview. The C programming model. The C programming model. The C programming model. The C programming model 1/23/2009. Real-time Systems D0003E

Similar documents
The naked computer. The naked computer. Separate bus architecture. Real-time Systems SMD138

Embedded Systems Programming - PA8001

Administration. Literature. Lectures. Lab assignments. Text book coverage. Realtime systems D0003E. Also need: some additional book for C

C Language Programming

Computer Organization & Systems Exam I Example Questions

Embedded programming, AVR intro

Hardware: Logical View

Beginning C Programming for Engineers

Under the Hood: Data Representations, Memory and Bit Operations. Computer Science 104 Lecture 3

Low-Level Programming

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

Integer Representation

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

C-string format with scanf/printf

Lecture 03 Bits, Bytes and Data Types

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

COMP3221: Microprocessors and. and Embedded Systems. Instruction Set Architecture (ISA) What makes an ISA? #1: Memory Models. What makes an ISA?

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

CS C Primer. Tyler Szepesi. January 16, 2013

Memory, Data, & Addressing II CSE 351 Spring

EL2310 Scientific Programming

Page 1. Where Have We Been? Chapter 2 Representing and Manipulating Information. Why Don t Computers Use Base 10?

CS 24: INTRODUCTION TO. Spring 2015 Lecture 2 COMPUTING SYSTEMS

Variables and literals

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

HW1 due Monday by 9:30am Assignment online, submission details to come

Topic Notes: Bits and Bytes and Numbers

COMP2121: Microprocessors and Interfacing. Instruction Set Architecture (ISA)

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

CS 241 Data Organization Binary

Topic Notes: Bits and Bytes and Numbers

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

CS Programming In C

Embedded Systems - FS 2018

Arithmetic and Bitwise Operations on Binary Data

CS 11 C track: lecture 8

Embedded Systems - FS 2018

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: Bits and Bytes and Numbers

ME 461 C review Session Fall 2009 S. Keres

The C Programming Language Guide for the Robot Course work Module

Course Outline Introduction to C-Programming

Integers II. CSE 351 Autumn Instructor: Justin Hsia

ICS Instructor: Aleksandar Kuzmanovic TA: Ionut Trestian Recitation 2

printf( Please enter another number: ); scanf( %d, &num2);

EE 308 Spring A software delay. To enter a software delay, put in a nested loop, just like in assembly.

Data III & Integers I

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Programming Project 2

CSC 2400: Computer Systems. Bit- Level vs. Logical Opera<ons. Bit- Level Operators. Common to C and Java &,, ~, ^, <<, >>

CS31 Discussion 1E Spring 17 : week 08

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

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

Fundamental of Programming (C)

Continued from previous lecture

Embedded Systems. Introduction. The C Language. Introduction. Why C instead ASM. Introduction to C Embedded Programming language

The Design of C: A Rational Reconstruction: Part 2

Programming refresher and intro to C programming

Bits and Bytes. Why bits? Representing information as bits Binary/Hexadecimal Byte representations» numbers» characters and strings» Instructions

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

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

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

EL2310 Scientific Programming

Basic I/O. COSC Software Tools. Streams. Standard I/O. Standard I/O. Formatted Output

C-string format with scanf/printf

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

Final CSE 131B Spring 2004

C - Basics, Bitwise Operator. Zhaoguo Wang

CS 107 Lecture 2: Bits and Bytes (continued)

Data III & Integers I

Lecture 02 C FUNDAMENTALS

Topics Introduction to Microprocessors

Memory, Data, & Addressing II

Binghamton University. CS-211 Fall Pointers

211: Computer Architecture Summer 2016

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Computers Programming Course 5. Iulian Năstac

CSC C69: OPERATING SYSTEMS

CENG 447/547 Embedded and Real-Time Systems. Review of C coding and Soft Eng Concepts

Data Types. Data Types. Integer Types. Signed Integers

Introduction to the MC9S12 Hardware Subsystems

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

Chapter 7. Basic Types

Data III & Integers I

Fundamentals of Programming

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

A few examples are below. Checking your results in decimal is the easiest way to verify you ve got it correct.

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

QUIZ. What is wrong with this code that uses default arguments?

CS61C : Machine Structures

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

Bits, Bytes and Integers Part 1

Bitwise Data Manipulation. Bitwise operations More on integers

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Computers Programming Course 6. Iulian Năstac

Special Topics for Embedded Programming

Pointers cause EVERYBODY problems at some time or another. char x[10] or char y[8][10] or char z[9][9][9] etc.

Integers II. CSE 351 Autumn Instructor: Justin Hsia

Carnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

Transcription:

Overview Real-time Systems D0003E Lecture 2: Bit manipulation and hardware interfacing Burn/Wellings ch. 15 Leftovers from lecture 1 the C execution model bit-manipulation in C memory mapped I/O port-mapped I/O some examples x = w = 0 y = 55 1

y = 55 y = 55 Locals of c z = 23 int z = 23; y = 2 Locals of c z = 23 int z = 23; w = 77; y = 2 Locals of c z = 23 int z = 23; w = 77; return z+1; w = 77 w = 77 y = 24 b () { int y; int z = 23; w = 77; return z+1; int z; w = 77; return z+1; w = 77 w = 77 2

Standard I/O To write to the standard output stream: printf("result is %d\n", r); putchar( q ); To read from the standard input stream: char c = getchar(); gets(buf); To access a non-standard stream: FILE *f = fopen("myfile.txt", "r"); char c = getc(f); fprintf(f, "Result is %d\n", r); To write to the standard error stream: fprintf(stderr, "Result is %d\n", r); More on printf() The first argument to printf() is a string where certain characters have special meaning: %d format the int found as extra argument %x as %d, but use hexadecimal format %f format a float %c insert a char found as extra argument %s insert a string found as extra argument %% insert a single % Many more options, see library manual Several format chars consume extra arguments L-R Note that printf takes a variable number of arguments (a gross hack even by C standards!) However As for the rest All I/O operations in the C standard libraries are built upon the assumption of an underlying operating system (Unix-like) Our target platform has no OS, so we ll have to replace printf and friends with something else Still, the standard I/O library is very handy when experimenting with C on the development system Note also that these standard libraries are completely different from those in Java Expressions, loops, switches very similar to Java In older C standards (we use a new one): Can't declare the loop variable inside a for() [No type boolean in C] For fuller coverage, read the tutorials referenced on the course homepage Best of all, study examples, and try modifying them bitby-bit Or ask your lab instructor! Accessing hardware in C The naked computer Whenever a user types Whenever the CPU finds a read instruction? read CPU write read RAM Whenever the CPU finds a write instruction write? 70 times a second Two methods for accessing hardware memory mapped I/O - hardware looks like memory separate I/O-bus Approaches look different from software point of view read CPU write read RAM write For now, let s concentrate on how to read and write to I/O ports We ll return to the question of I/O synchronization in a while 3

Separate bus architecture Memory-mapped architecture data bus I/O data bus data bus RAM CPU address bus I/O address bus CPU RAM I/O ports accessed by special I/O instructions I/O-port maps to actual hardware registers when writing a port, we actually write to the hardware Example: Intel x86 family address bus I/O devices appear as plain memory cells We write to memory on special addresses Example: the AVR architecture Memory-mapped I/O in C If ptr contains the address of the port: Reading: value = *ptr; Writing: *ptr = new_val; Just as reading a regular memory cell The type of ptr must match the width of the port: 8 bits: char *ptr; 16 bits (on the AVR): int *ptr; Setting the address (assign value to pointer): char *ptr = (char *)0x1ff; To avoid confusion caused by signed values: unsigned char *ptr = (unsigned char *)0x1ff; To stop the compiler from removing *ptr = x; x = *ptr; : volatile char *ptr = (volatile char *)0x1ff; volatile tells compiler not to optimize that Memory-mapped I/O It is very common to use the preprocessor to simplify port accesses: #define SOME_REG *((char *)0x1ff) SOME_REG = 0; x = SOME_REG & 0x01; /* mask out lsb */ Explanation: 0x1ff is memory address of hardware cast to char pointer: (char*) 0x1ff dereference pointer (we get what it is pointing to): *((char *)0x1ff) Such definitions are usually found in platformspecific.h files (see avr/iom169.h for example) On the AVR it s even simpler than this! Separate bus I/O Bit operations Requires inline assembler instructions (goes beyond C) special instruction to write to I/O-port Usually presented as preprocessor macros For example, the QNX real-time OS provides a set of macros called in8, out8, in16, out16, etc Reading a byte from port 0x30d unsigned char val = in8(0x30d); Writing a 32-bit word to port 0xf4 out32(0xf4, expr); Bitwise AND (a & b) Bitwise OR (a b) Bitwise XOR (a ^ b) Bitwise NOT (~ a) Shift bits left (a << b) shift a, b bits right Shift bits right (a >> b) arithmetic shift if type is signed! on some machines :-) otherwise normal shift arithmetic shift shift in 1's instead of 0's if value negative this will keep the value negative shift divides by two use unsigned types to avoid errors 4

Bitwise AND Bitwise OR 193 & 234 = 192 193 234 = 235 193 = 0xc1 = 1 1 0 0 0 0 0 1 193 = 0xc1 = 1 1 0 0 0 0 0 1 234 = 0xea = 1 1 1 0 1 0 1 0 234 = 0xea = 1 1 1 0 1 0 1 0 1 1 0 0 0 0 0 0 = 0xc0 = 192 1 1 1 0 1 0 1 1 = 0xeb = 235 C.f. logical AND, OR Big vs. little endian Don t confuse the bitwise operators & and with the logical operators && and that work on integer expressions: 0 && 17 = 0 31-28 = 1 0x40 && 0x02 = 1 These operators only distinguish between the values zero and non-zero zero means false non-zero means true first byte of a word? to the right or to the left? low memory address or high memory address? Example: long a = 0x12345678; //assign value char *p = (char *)&a; //pointer to "first" byte printf( %02X\n, *p); //print "first" byte printout depends on architecture: little-endian machine (Intel): 0x78 big-endian machine (most others): 0x12 treating words as a sequence of bytes (and vice versa)! Use caution! Big vs. little endian Examples Example: long a = 0x12345678; //assign value big endian: high address low address 0x78 0x56 0x34 0x12 little endian: 0x12 0x34 0x56 0x78 Testing a certain bit (memory-mapped I/O) #define OK_BIT 5 #define OK_MASK (1 << OK_BIT) //create mask with 1 in pos. 5 volatile char *status_reg = (volatile char *)0x34c; //0x34 is address of status reg. if (*status_reg & OK_MASK) // read register and mask out bit // bit 5 was set in status register else // it was not 5

Examples Examples Setting a certain bit (memory-mapped I/O) #define OK_BIT 5 #define OK_MASK (1 << OK_BIT) volatile char *reg = (volatile char *) 0x34c; *reg = *reg OK_MASK; // read reg. and set bit, write back register Note that single bits can t be written all 8 bits of a byte must be written at once Testing a certain bit (separate I/O bus) #define OK_BIT 5 #define OK_MASK (1 << OK_BIT) #define STATUS_REG 0x34c if (in8(status_reg) & OK_MASK) // bit 5 was set in status register else // it was not Examples Note: Setting a certain bit (separate I/O bus) #define OK_BIT 5 #define OK_MASK (1 << OK_BIT) #define RW_REG 0x34c out8(rw_reg, in8(rw_reg) OK_MASK)); There s nothing that guarantees that reading and writing at the same address will refer to the same register! completely up to the hardware can do whatever it wants with value For example, some devices map a status register (for reading) and a command register (for writing) to the same address: #define IS_READY (1 << 5) #define CONVERT (1 << 5) #define STATUS_REG *((char *)0x34c) #define CMD_REG *((char *)0x34c) if (STATUS_REG & IS_READY) CMD_REG = CONVERT; Shadowing Single write The correct way to update such overlayed registers is to use a shadow variable: #define CONVERT (1 << 5) #define CMD_REG *((char *)0x34c) char cmd_shadow; cmd_shadow = cmd_shadow CONVERT; CMD_REG = cmd_shadow; It s important to let all changes to CMD_REG be reflected in cmd_shadow as well [Fortunately, all ports in the AVR are read/write] Tip: C provides a convenient assignment sytax var op= expr; means var = var op expr; In many cases, output ports are written as single values, so reading of the same port is not required: #define CTRL (1 << 3) #define SIZE1 (1 << 4) #define SIZE2 (2 << 4) #define SIZE3 (3 << 4) #define EXTRAFLAG (1 << 6) CMD_REG = EXTRAFLAG SIZE2 CTRL ; 6

Reading multi-bit values Masking out a multi-bit value, and then shifting: get two-bit value in bits 4-6: #define SIZEMASK (3 << 4) // two 1's shifted 4 bits #define DATA_REG *((char *)0x34c) //switch on value of the two bits switch ((DATA_REG & SIZEMASK) >> 4) { case 1: case 2: case 3: A slightly bigger example In the port pointed to by ptr, set n bits starting at bit number p to the value of x void setbits(int *ptr, int n, int p, int x) { int mask = ~(~0 << n) << p; // the mask int data = (x << p) & mask; // data to be set *ptr = *ptr & ~mask; // clear current bits *ptr = *ptr data; // OR in the data [Notice the assumption here that ptr is read/write] A slightly bigger example Back to I/O synchronization assume p=2 and n=3 0 ~0 ~0 << n ~(~0 << n) ~(~0 << n) << p ~(~0 << n) << p 76543210 00000000 11111111 11111000 00000111 00011100 assume x=5 (x << p) & mask void setbits(int *ptr, int n, int p, int x) { int mask = ~(~0 << n) << p; // the mask int data = (x << p) & mask; // data to be set *ptr = *ptr & ~mask; // clear current bits *ptr = *ptr data; // OR in the data x x << p x << p & mask 76543210 00000101 00010100 00010100 I/O register allows current key status to be read at any time Keyboard electronics signals all key changes to the port read write CPU RAM read What if the software is to count keypresses? How does the software become informed of changes in key status? write Poor man s solution Busy-waiting Read the key status register over and over again, until two subsequent values are not equal int old = KEY_STATUS_REG; int val = old; while (old == val) val = KEY_STATUS_REG; // status has changed This technique is called busy-waiting, because it causes the program to wait although the CPU is still busy Note that the program has no control over when to exit the loop this is in the hands of the external world, which has its own means of affecting KEY_STATUS_REG [Replace KEY_STATUS_REG with an ordinary variable, and the program is stuck forever!] Busy-waiting as a synchronization technique has a lot of drawbacks, we will discuss them in depth in subsequent lectures However, busy-waiting has one striking benefit: it allows the programmer to think of I/O in the familiar terms of reading and writing program variables int getchar() { while (KEY_STATUS_REG & PRESSED) ; while (!(KEY_STATUS_REG & PRESSED)) ; return KEY_VALUE_REG; We will explore busy-waiting extensively in lab 1, so that we may safely drop it in later work! 7

The ATmega169 microcontroller Some important ports #include <avr/io.h> LCDCCR, LCDCRB, LCDCRA, LCDFRR, LCDDRn TCCR1B, TCNT1 DDRB, PORTB, PINB CLKPR See online manuals ATmega169.pdf and AVR065.pdf for detailed information Atmel AVR Much simpler than in general case no need for explicit pointers and volatile - most of the time Writiong to a port: <portname>=<value> Reading from a port: <varianble>=<portname> Example: LCDCCR=controlBits; Next lecture concurrency and mutual exclusion 8