F28HS2 Hardware-Software Interface Lecture 6 - Programming in C 6.

Slides:



Advertisements
Similar presentations
Dr. Rabie A. Ramadan Al-Azhar University Lecture 3
Advertisements

OUTPUT INTERFACE – Microprocessor Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Programming Microcontrollers B. Furman 19MAR2011.
F28PL1 Programming Languages Lecture 8 Programming in C - 3.
Programming the ATmega16
1 Integral Data Types in C. 2 First lecture response sent out Lots of good questions Some questions will be punted to reading when appropriate 3 statements.
Introduction to Programming with Java, for Beginners
24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05.
Butterfly I/O Ports CS-212 Dick Steflik. I/O for our labs To get data into and out of our Butterfly its a little trickier than using printf and scanf.
AVR Programming CS-212 Dick Steflik. ATmega328P I/O for our labs To get data into and out of our Arduino its a little trickier than using printf and.
Getting the O in I/O to work on a typical microcontroller Activating a FLASH memory “output line” Part 1 Main part of Laboratory 1 Also needed for “voice.
Bit Operations C is well suited to system programming because it contains operators that can manipulate data at the bit level –Example: The Internet requires.
A bit can have one of two values: 0 or 1. The C language provides four operators that can be used to perform bitwise operations on the individual bits.
Professor Jennifer Rexford COS 217
1 Integral Data Types in C Professor Jennifer Rexford
L.
©Brooks/Cole, 2003 Chapter 2 Data Representation.
1 The Design of C: A Rational Reconstruction Jennifer Rexford.
ECE291 Computer Engineering II Lecture 9 Josh Potts University of Illinois at Urbana- Champaign.
1 ARM University Program Copyright © ARM Ltd 2013 General Purpose I/O.
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
1 ARM University Program Copyright © ARM Ltd 2013 General Purpose I/O.
L. Akshay Masare Piyush Awasthi IMAGE PROCESSING AND OPENCV.
Chapter 18 – Miscellaneous Topics. Multiple File Programs u Makes possible to accommodate many programmers working on same project u More efficient to.
Lecture12. Outline Binary representation of integer numbers Operations on bits –The Bitwise AND Operator –The Bitwise Inclusive-OR Operator –The Bitwise.
Computer Organization and Assembly Language Bitwise Operators.
Bitwise Operators Fall 2008 Dr. David A. Gaitros
Bit Operations Horton pp Why we need to work with bits Sometimes one bit is enough to store your data: say the gender of the student (e.g. 0.
Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #6 Structures,
CS1372: HELPING TO PUT THE COMPUTING IN ECE CS1372 Some Basics.
CSE 351 Final Exam Review 1. The final exam will be comprehensive, but more heavily weighted towards material after the midterm We will do a few problems.
Memory and the Character Display Chapter 10. The Bitwise Operators The bitwise operators are used to access the computer hardware. Use of bitwise operators.
Bit Manipulation in 'C' 'C' bit operators
Lecture 11 Text mode video
Project Assignment Snake Game/Car Acceleration Meter Min 10 Pages 10 min Presentation Max 5 group members Submitting Date: lab 2:Dec 27, 2014 Lab 3: Dec.
Embedded Systems Lecture 4 January 20, 2016.
F28HS Hardware-Software Interface Lecture 11: ARM Assembly Language 5.
ARM Embedded Programming Lecture 4 Accessing Memory Mapped Registers.
Using Linux with ARM Tutorial #3.
1 Manipulating Information (1). 2 Outline Bit-level operations Suggested reading –2.1.7~
 Backlight 에서 나온 백색광이 액정 셀을 통과하면 서 투과율이 조절되고 red, green, blue 의 color filter 를 투과해 나오는 빛의 혼합을 통해 색이 구 성됨  Color filter 는 셀사이의 빛을 차단하는 black matrix,
Programming PIC 16F84A in Assembly. PIC16F84 pin-out and required external components.
CompSci From bits to bytes to ints  At some level everything is stored as either a zero or a one  A bit is a binary digit a byte is a binary.
From bits to bytes to ints
ECE Application Programming
Programming in Machine Language
ENEL 111 Digital Electronics
Digital Electronics Jess 2008.
TMF1414 Introduction to Programming
Session #5 File I/O Bit Masks, Fields & Bit Manipulations
Bit Operations Horton pp
Dr. Michael Nasief Lecture 2
What to bring: iCard, pens/pencils (They provide the scratch paper)
Introduction to Computer Programming
Binary – Octal - Hexadecimal
Introduction to Programming and the C Language
Bits and Bytes Topics Representing information as bits
Bits and Bytes Topics Representing information as bits
Bitwise Operations C includes operators that permit working with the bit-level representation of a value. You can: - shift the bits of a value to the left.
Embedded Programming in C
Bits and Bytes Topics Representing information as bits
Chapter 2 Data Representation.
Your questions from last session
Module 10 Operations on Bits
Chapter 6 Programming the basic computer
Bit Manipulations CS212.
ENEL 111 Digital Electronics
Bit Operations Horton pp
ECE 120 Midterm 1 HKN Review Session.
Presentation transcript:

F28HS2 Hardware-Software Interface Lecture 6 - Programming in C 6

Low level programming in C want to control hardware devices devices mapped into memory accessed at specific memory addresses set memory locations to bit sequences to: – configure devices – send/receive data

Bits, bytes and hexadecimal binaryhexdecimalbinaryhexdecimal a b c d e f15

Bits, bytes and hexadecimal think about hexadecimal as specifying bit settings not numbers byte is 2 * 4 bits 0xh 1 h 0 – h 1 is bits 7-4 – h 0 is bits 3-0 e.g. – 0xff == == all bits 1 – 0x00 == == all bits 0 – 0x65 == == bits 6, 2 and 0 set == ‘e’

Viewing bytes od file - octal dump each line – byte number + byte representations show as hex – od -x file show as ASCII – od -c file show as hex & ASCII – od -x -c file

Low level programming in C can access individual bytes of a 32 bit integer by: – casting to: array of 4 chars – or: struct of 4 chars – addressing each char individually

Casting ints 0x670x450x230x01 x 32 bits == 4 * 8 bit bytes int x; x = 0x ;

Casting int to char char * c1; c1 = (char *)&x; - c now points at x’s space printf(“%x %x %x %x\n”, c1[0],c1[1],c1[2],c1[3]); 

Casting int to to char &x returns address of 1 st byte of int (char *) coerces address of 1 st byte of int to address of 1 st byte of array of char – c1[0] is 1 st byte of x == 0x67 – c1[1] is 2 nd byte of x == 0x45 etc 0x670x450x230x01 x c1[0] c1[1] c1[2]c1[3] c1

Casting int to struct struct chars {char a;char b;char c;char d;}; c2 = (struct char *) &x; printf(“%x %x %x %x\n”, c2->a,c2->b,c3->c,c2->d); 

Casting int to to struct &x returns address of 1 st byte of int (struct chars *) coerces address of 1 st byte of int to address of 1 st byte of struct of 4* char – c2->a is 1 st byte of x == 0x67 – c2->b is 2 nd byte of x == 0x45 etc 0x670x450x230x01 x a b cd c2

Low level programming in C in CPU (ARM, Intel) registers are 32 bit so addressing individual chars in an int involves manipulating a 32 bit register C exposes details of low level bit manipulation use logical & shift operations with hexadecimal representations of bit patterns in bytes

Logical operations XYX & Y XYX | Y XYX ^ Y X & Y == AND == 1 if both 1 use 0 in Y to set to 0 X | Y == OR == 1 if either 1 use 1 in Y to set to 1 X ^ Y == XOR - exclusive OR == 1 if both different use 1 in Y to flip X from 0 to 1 or 1 to 0

Masks mask – bit pattern that affects specific bits e.g. to select bytes in 32 bit register with & #define byte0 0x000f #define byte1 0x00f #define byte2 0x0f #define byte3 0xf

Masks int word; word = 0x ; word == word & byte0 == word & byte1 == word & byte2 == word & byte3 ==

Masking bytes - high to low #define BYTE0 0x000000ff #define BYTE1 0x0000ff00 #define BYTE2 0x00ff0000 #define BYTE3 0xff main() { int word; word = 0x ; /* a b c d */ printf("%8x\n",word&BYTE0); printf("%8x\n",word&BYTE1); printf("%8x\n",word&BYTE2); printf("%8x\n",word&BYTE3); } ==>

Shifting how to isolate individual bytes? shifting – move bits in sequence a specified number of places left/right – pad to right/left with 0s X << Y == shift X left Y places and add Y 0s to right X >> Y == shift X right Y places and add Y 0s to left

Selecting bytes: low to high word == == 0x word & byte0 == == 0x64 word >> 8 == == 0x (word >> 8) & byte0 == == 0x63 (word >> 16) == == 0x (word >> 16) & byte0 == == 0x62 etc

Selecting bytes: low to high #define BYTE0 0x000000ff main() { int word; int i; word = 0x ; /* a b c d */ for(i=0;i<4;i++) { printf("%8x\n",word&BYTE0); word = word >> 8; } ==>

Selecting bytes: high to low mask leftmost byte & shift right 3 bytes then shift one byte left #define BYTE3 0xff main() {... for(i=0;i<4;i++) { printf("%8x\n",(word&BYTE3)>>24); word = word << 8; } ==>

Selecting bits: low to high #define BIT0 0x main() { int n,i; printf("enter value> "); scanf("%d",&n); for(i=0;i<32;i++) printf("%3d",i); putchar('\n'); for(i=0;i<32;i++) { if(n&BIT1)printf(" 1"); else printf(" 0"); n = n>>1; } putchar(‘\n’); } enter value>

Selecting bits: high to low #define BIT31 0x main() { int n; int i;... for(i=31;i>0;i--) printf("%3d",i); putchar('\n'); for(i=0;i<31;i++) { if(n&BIT31)printf(" 1"); else printf(" 0"); n = n<<1; } putchar('\n'); } enter number>

Manipulating images raw images are usually manipulated as bitmaps each pixel represented by red, green & blue values – big == maximum – 0 == minimum

Manipulating images e.g == black i.e. no colour == white i.e. max colour == red i.e.no green or blue struct RGB { int r,g,b; }; image is rows of columns of pixels struct RGB ** image;

Manipulating images

Colour masks black white red

Combining images - AND AND image2 into image1 starting at x,y andRGB(struct RGB ** i1,int x,int y,struct RGB ** i2, int i2rows,int i2cols) { int i,j; for(i=0;i<i2rows;i++) for(j=0;j<i2cols;j++) { i1->p[i+x][j+y].r = i1->p[i+x][j+y].r & i2->p[i][j].r; i1->p[i+x][j+y].g = i1->p[i+x][j+y].g & i2->p[i][j].g; i1->p[i+x][j+y].b = i1->p[i+x][j+y].b & i2->p[i][j].b; }

Colour mask AND black white red

Combining images - OR OR image2 into image1 starting at x,y andRGB(struct RGB ** i1,int x,int y,struct RGB ** i2, int i2rows,int i2cols) { int i,j; for(i=0;i<i2rows;i++) for(j=0;j<i2cols;j++) { i1->p[i+x][j+y].r = i1->p[i+x][j+y].r | i2->p[i][j].r; i1->p[i+x][j+y].g = i1->p[i+x][j+y].g | i2->p[i][j].g; i1->p[i+x][j+y].b = i1->p[i+x][j+y].b | i2->p[i][j].b; }

Colour mask OR black white red

Combining images - XOR XOR image2 into image1 starting at x,y andRGB(struct RGB ** i1,int x,int y,struct RGB ** i2, int i2rows,int i2cols) { int i,j; for(i=0;i<i2rows;i++) for(j=0;j<i2cols;j++) { i1->p[i+x][j+y].r = i1->p[i+x][j+y].r ^ i2->p[i][j].r; i1->p[i+x][j+y].g = i1->p[i+x][j+y].g ^ i2->p[i][j].g; i1->p[i+x][j+y].b = i1->p[i+x][j+y].b ^ i2->p[i][j].b; }

Colour mask XOR black white red

Combining images + AND ORXOR

Raspberry Pi GPIO General Purpose I/O chip – BCM2853 connects RPi to external devices has 54 general purpose I/O lines – GPIO 0 to GPIO 53 NB many I/O lines have dedicated use in RPi

Raspberry Pi GPIO controller has 41 * 32-bit registers – registers mapped to memory – addressed by offset from address of first register first 6 registers control pin functions numbernamepinsoffset 0GPSEL00-90x00 1GPSEL x04 2GPSEL x08 3GPSEL x0C 4GPSEL x10 5GPSEL x14

Raspberry Pi GPIO in each register, each pin has 3 bits for control e.g. pin 13 ==GPSEL1 bits 9-11 e.g. pin 27 == GPSEL GPSEL -> bitspin bitspin b 29 b 28 b b 14 b 13 b b 26 b 25 b b 11 b 10 b b 23 b 22 b b 08 b 07 b b 20 b 19 b b 05 b 4 b b 17 b 16 b b 02 b 01 b

Raspberry Pi GPIO to set pin to input or output set bits in corresponding GSEL register – input == 000 – output == 001 e.g. to set pin 25 to output set GPSEL2 to 001 << 15 i.e. bits 0-2 in 001 now bits 15-17

Raspberry Pi GPIO to set or clear pins set corresponding bit in set and clear registers GPSET0 & GPCLR0 affect pins 0-31 GPSET1 & GPCLR1 affect pins numbernamepinsoffset 7GPSET00-310x1C 8GPSET x20 10GPCLR00-310x28 11GPCLR x2C

Raspberry Pi GPIO definitions #define GPIO_GPFSEL0 0 #define GPIO_GPFSEL1 1 #define GPIO_GPFSEL2 2 #define GPIO_GPFSEL3 3 #define GPIO_GPFSEL4 4 #define GPIO_GPFSEL5 5 #define GPIO_GPSET0 7 #define GPIO_GPSET1 8 #define GPIO_GPCLR0 10 #define GPIO_GPCLR1 11

Raspberry Pi GPIO suppose start of GPIO registers in memory is: volatile unsigned int * gpio; i.e pointer to int == groups of 4 bytes so GPIO register i is gpio[i]

Raspberry Pi GPIO e.g. green light on the RPi board is connected to GPIO pin 47 == GPSEL4 bits to set as output: gpio[GPIO_GPSEL4] = 1 << 21;

Raspberry Pi GPIO pin 47 controlled by bit 15 in GPSET1 & GPCLR1 to turn on & off: while(1) { gpio[GPIO_GPCLR1] = 1 << 15; /* on */ delay(); gpio[GPIO_GPSET1] = 1 << 15; /* off */ delay(); }

Raspberry Pi GPIO need to set gpio to address of first GPIO register in memory RPi runs Linux each program runs in own virtual address space can’t directly access physical memory

Raspberry Pi GPIO Linux sees physical memory as a character device file /dev/mem can open this for access: #include int fd;... if ((fd = open ("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0) { printf("can't open /dev/mem\n"); exit(0); } NB use of | to combine flags for read/write, synchronise & close on exit

Raspberry Pi GPIO RPi 2 registers start at 0x3f on map opened /dev/mem onto this physical location: #include #define BLOCK_SIZE (4*1024) #define GPIO_BASE 0x3f gpio = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_BASE) ; if((int)gpio==-1) { printf("can't mmap\n"); exit(0); }

Raspberry Pi GPIO #include #define BLOCK_SIZE (4*1024) #define GPIO_BASE 0x3F200000UL #define LED_GPFSEL GPIO_GPFSEL4 #define LED_GPFBIT 21 #define LED_GPSET GPIO_GPSET1 #define LED_GPCLR GPIO_GPCLR1

Raspberry Pi GPIO #define LED_GPIO_BIT 15 #define GPIO_GPFSEL4 4 #define GPIO_GPSET1 8 #define GPIO_GPCLR1 11 volatile unsigned int* gpio; volatile unsigned int tim;

Raspberry Pi GPIO int main(void) { int fd; if ((fd = open ("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0) { printf("can't open /dev/mem\n"); exit(0); } gpio = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_BASE) ; if((int)gpio==-1) { printf("can't mmap\n"); exit(0); }

Raspberry Pi GPIO gpio[LED_GPFSEL] |= (1 << LED_GPFBIT); while(1) { for(tim = 0; tim < ; tim++); gpio[LED_GPCLR] = (1 << LED_GPIO_BIT); for(tim = 0; tim < ; tim++); gpio[LED_GPSET] = (1 << LED_GPIO_BIT); } with thanks to: for basic program: bare-metal-programming-in-cpt1/ bare-metal-programming-in-cpt1/ for mmap advice, Gordon Henderson: pi/gpio-examples/tux-crossing/gpio-examples-1-a-single-led/ pi/gpio-examples/tux-crossing/gpio-examples-1-a-single-led/

Raspberry Pi GPIO