EDA482/487: Machine- Oriented Programming Lecture 7: Timing & - - PowerPoint PPT Presentation

eda482 487 machine oriented programming
SMART_READER_LITE
LIVE PREVIEW

EDA482/487: Machine- Oriented Programming Lecture 7: Timing & - - PowerPoint PPT Presentation

EDA482/487: Machine- Oriented Programming Lecture 7: Timing & Synchronization Pedro Moura Trancoso ppedro@chalmers.se Original slides by Ulf Assarsson Objectives Topics: Access Time Synchronous/Asynchronous Interface


slide-1
SLIDE 1

Lecture 7: Timing & Synchronization Pedro Moura Trancoso

ppedro@chalmers.se

EDA482/487: Machine- Oriented Programming

Original slides by Ulf Assarsson

slide-2
SLIDE 2

4/17/18 Chalmers 2

Objectives

  • Topics:
  • Access Time
  • Synchronous/Asynchronous Interface
  • Time diagram
  • System Clock "SysTick"
  • Examples
  • Reading:
  • “Arbetsbok” chapter 5
  • Datasheet "ASCII-display"
slide-3
SLIDE 3

4/17/18 Chalmers 3

  • Pointers
  • Address/Content
  • Absolute Address - Ports
  • Pointers as function parameters
  • Arrays
  • Multi-dimentional Arrays

Previous Lecture

int arrayOfArrays[3][4]; … arrayOfArrays[i][j] = 10; // OR arrayOfArrays+i*4+j = 10; …

slide-4
SLIDE 4

4/17/18 Chalmers 4

Strings (again…)

#include <stdio.h> int main() { char str[] = "hello"; char *x = "hello"; printf("Before str=%s\n", str); str[1] = 'o'; printf("After str=%s\n", str); printf("Before x=%s\n", x); x[1] = ’o'; printf("After x=%s\n", x); return 0; } str has memory space allocated automatically and initialized to string ”hello” x points to read-only ”hello” string.

Bus Error!

slide-5
SLIDE 5

4/17/18 Chalmers 5

Void Pointer (1)

#include <stdio.h> int main() { int a=10; float b=35.75; void *ptr; // Declaring a void pointer ptr=&a; // Assigning address of integer to void pointer. printf("The value of integer variable is= %d",*( (int*) ptr) ptr=&b; // Assigning address of float to void pointer. printf("The value of float variable is= %f",*( (float*) ptr) ); } Examples from http://www.circuitstoday.com/void-pointers-in-c

slide-6
SLIDE 6

4/17/18 Chalmers 6

Void Pointer (2)

void funct(void *a, int z) { if(z==1) printf("%d",*(int*)a); // If user inputs 1, then he means the data is an integer else if(z==2) printf("%c",*(char*)a); // Typecasting for character pointer. else if(z==3) printf("%f",*(float*)a); // Typecasting for float pointer } IMPORTANT: void *ptr; int a; ptr=&a; ptr++; // This statement is invalid and will result in an error because 'ptr' is a void pointer variable. Examples from http://www.circuitstoday.com/void-pointers-in-c

slide-7
SLIDE 7

4/17/18 Chalmers 7

Access Time for Different Units

Highly varying performance results in major differences in access time

1980 1990 2000 2010 1 10 100 1 000 10 000 100 000 CPU DRAM

Performance gap CPU/DRAM

ROM EPROM DRAM SRAM

Intel i7-6700:

  • L1 D$ 32KB + I$ 32KB (4 cycles)
  • L2 256KB (12 cycles)
  • L3 8MB (4 core) (42 cycles)
  • RAM (93 cycles)
slide-8
SLIDE 8

4/17/18 Chalmers 8

Unconditional Transfer

Requires synchronization

  • A special signal, "Enable", has to be used to specify

exactly when data exchange is to take place.

The rising/falling edges define the synchronization. The signal is often called "clock signal"

Enable

Raising edge Falling edge Raising edge

slide-9
SLIDE 9

4/17/18 Chalmers 9

Conditional Transfer

= Asynchronous Transfer

Mottagaren Redo? Sänd Data till mottagaren N J

Polling

Mottagaren Redo? Sänd Data till mottagaren J N

Busy Wait

The peripheral unit has an interface (register) with a status bit indicating whether or not data is available.

Does not require synchronization but requires special "handshake signals". Therefore, such an interface we call "asynchronous".

Receiver Ready? Receiver Ready? Send Data to receiver Send Data to receiver

slide-10
SLIDE 10

4/17/18 Chalmers 10

Synchronous and Asynchronous Interfaces

Synchronous: A clock signal from the central unit determines when data exchange

  • ccurs

Asynchronous: Handshake signals determine when data exchange can occur

Enable

slide-11
SLIDE 11

4/17/18 Chalmers 11

Time Diagram – Basis for Synchronization

Write cycle: data is transferred from the CPU to peripheral device Read cycle: data is transferred from peripheral device to CPU

min

tc Cycle time min tw Clock pulse ("Enable") duration (high and low) min tsu1 control signal setup time, before positive edge min tsu2 setup time for data writing, before negative edge min tD Setup time for data, reading, before negative edge max th Hold time, duration (after negative edge) min

slide-12
SLIDE 12

4/17/18 Chalmers 12

Time Diagram – Basis for Synchronization

Write cycle: data is transferred from the CPU to peripheral device Read cycle: data is transferred from peripheral device to CPU

min

tc Cycle time min tw Clock pulse ("Enable") duration (high and low) min tsu1 control signal setup time, before positive edge min tsu2 setup time for data writing, before negative edge min tD Setup time for data, reading, before negative edge max th Hold time, duration (after negative edge) min

Write cycle: 1.Set the R/W-signal to 0 (a bit in a specific port) 2.Wait at least tsu1 ns. 3.Set the enable-bit and wait at least tw ns. 4.At least tsu2 ns before reseting the enable-bit, write the data to the data bus (a port) 5.Reset the enable-bit. 6.Wait at least th ns until starting again (and set R/W). Wait for at least a total of tc ns (cycle time). Read cycle: 1.Set the R/W-bit to 1. 2.Wait at least tsu ns. 3.Set the enable-bit to 1 4.Wait at least tD ns. 5.Read from the data bus. 6.Wait at least tw have passed. 7.Set enable-bit to 0. 8.Wait at least th ns. 9.Modify R/W if you want, but wait for a complete cycle time of tc.

slide-13
SLIDE 13

4/17/18 Chalmers 13

Examples: Interface/Algorithms

synchronization signal Read or Write Command or ASCII-data Byte to be transfered

Transfer 8 bits to the ascii controller: Control-register(RW)=0; Wait tsu1; Control-register(E)=1; Data-register = (8 bits); Wait tsu2; (until at least tw passes) E = 0; Wait until a total tc passes;

Write Cycle Control-register Data-register

slide-14
SLIDE 14

4/17/18 Chalmers 14

Examples: Interface/Algorithms

Write Cycle

  • 1. For example setup port GPIO_E for the ascii-display.
  • 2. The Ascii-display expects:

Data-register (bit 8 - 15) => ODR_high-byte Control-register (bit 0 - 7) => ODR_low-byte

Synkronisera nde signal Läs eller skriv

GPIO_E ODR_High ODR_low

Transfer 8 bits to the ascii controller: Control-register(RW)=0; Wait tsu1; Control-register(E)=1; Data-register = (8 bits); Wait tsu2; (at least until tw passes) E = 0; Wait until a total of tc passes;

slide-15
SLIDE 15

4/17/18 Chalmers 15

Examples: Interface/Algorithms

Write Cycle

  • 1. For example setup port GPIO_E for the ascii-display.
  • 2. The Ascii-display expects:

Data-register (bit 8 - 15) => ODR_high-byte Control-register (bit 0 - 7) => ODR_low-byte

Synkronisera nde signal Läs eller skriv

GPIO_E ODR_High ODR_low

Transfer 8 bits to the ascii controller: Control-register(RW)=0; Wait tsu1; Control-register(E)=1; Data-register = (8 bits); Wait tsu2; (at least until tw passes) E = 0; Wait until a total of tc passes;

#define GPIO_E 0x40021000 /* MD407 port E */ #define portOdrLow ((volatile unsigned char *)(GPIO_E+0x14)) #define portOdrHigh ((volatile unsigned char *)(GPIO_E+0x14+1)) #define B_E 0x40 #define B_SELECT 0x04 #define B_RW 0x02 #define B_RS 0x01 // RS is set to 0 for command or to 1 for data read/write *portOdrLow |= B_SELECT; // set the select-bit *portOdrLow &= ~B_RW; // turn off RW delay(t_su1= ~40ns); *portOdrLow |= B_E; // set enable *portOdrHigh = our 8 bits of data; delay(max(t_su2=80ns, t_w=240ns); *portOdrLow &= ~B_E; // reset enable delay( (t_c=500ns) – 240ns);

slide-16
SLIDE 16

4/17/18 Chalmers 16

Counting Circuit - "SysTick"

STK_LOAD Reload t STK_VAL Reload Reload

Downcounter 24 bits

slide-17
SLIDE 17

4/17/18 Chalmers 17

Counting Circuit - Register

Algoritm: STK_CTRL = 0 Reset SysTick STK_LOAD = CountValue STK_VAL = 0 Reset the counter register STK_CTRL = 5 Restart the counter Wait until COUNTFLAG=1 STK_CTRL = 0 Reset SysTick Delay():

slide-18
SLIDE 18

4/17/18 Chalmers 18

Counting Circuit - Register

Algoritm: STK_CTRL = 0 Reset SysTick STK_LOAD = CountValue STK_VAL = 0 Reset the counter register STK_CTRL = 5 Restart the counter Wait until COUNTFLAG=1 STK_CTRL = 0 Reset SysTick Delay():

1: Räknare aktiverad

Bit0 = 1 (enable) Bit1 = 0 (no interrupt) Bit2 = 1 (system clock)

slide-19
SLIDE 19

4/17/18 Chalmers 19

Example 1: 250 ns delay with “SysTick”

1. Create a function delay_250ns(void) which blocks (delays) the calling function by least 250 ns. 2. Also show how this can be used to create a delay routine delay_milli( unsigned int millisec) which delays the application execution by a variable number of milliseconds.

How many cycles? 250ns x 168MHz = 42 cycles #define STK_CTRL ((volatile unsigned int *)(0xE000E010)) #define STK_LOAD ((volatile unsigned int *)(0xE000E014)) #define STK_VAL ((volatile unsigned int *)(0xE000E018)) void delay_250ns( void ) { /* SystemCoreClock = 168000000 */ *STK_CTRL = 0; *STK_LOAD = ( (168/4) -1 ); *STK_VAL = 0; *STK_CTRL = 5; while( (*STK_CTRL & 0x10000 )== 0 ) {} *STK_CTRL = 0; } void delay_micro(unsigned int us) { while(us--) { delay_250ns(); delay_250ns(); delay_250ns(); delay_250ns(); } } // delay_milli(): 1 ms = 1000 us void delay_milli( unsigned int ms ) { while( ms-- ) delay_micro(1000); }

slide-20
SLIDE 20

4/17/18 Chalmers 20

Example 2: Periodic blincking led

We have a led connected to port D (0-7). Present an application that causes the led to flash once per second. Initialization function app_init(), which initializes the port is given, as well as the following port definition : #define GPIO_ODR_LOW ((volatile unsigned char *)(0x40020C14)) Programs have completely different time characteristics when executed in the simulator or the hardware. Use conditional compilation to customize the delay in the simulator example. Equivalent to uppg.21 Arbetsboken.

#define PORT_BARGRAPH_BASE 0x40020C00 /* MD407 port D */ #define portBargraphModer ((volatile unsigned int *) (PORT_BARGRAPH_BASE)) #define portBargraphOtyper ((volatile unsigned short *) (PORT_BARGRAPH_BASE+0x4)) #define portBargraphOspeedr ((volatile unsigned int *) (PORT_BARGRAPH_BASE+0x8)) #define portBargraphOdrLow ((volatile unsigned char *) (PORT_BARGRAPH_BASE+0x14)) void main(void) { init_app(); while(1) { *portBargraphOdrLow = 0; delay_milli(500); *portBargraphOdrLow = 0xFF; delay_milli(500); } }

slide-21
SLIDE 21

4/17/18 Chalmers 21

Programming the ASCII-display

The circuit translates ASCII characters into corresponding bit patterns via a simple interface : control register - 4 bits used data register - 8 bits used

Synchronization signal Read or Write Command or ASCII-data Byte to be transfered

slide-22
SLIDE 22

4/17/18 Chalmers 22

Program Structure

ascii_read_controller() ascii_read_status() ascii_write_controller(cmd/data) ascii_write_cmd( cmd ) ascii_write_data( data ) RS=1 RW=0 RS=1 RW=1 RS=0 RW=1 RS=0 RW=0 ascii_read_data()

slide-23
SLIDE 23

4/17/18 Chalmers 23

Write Cycle - Characteristics

The Datasheet time diagram illustrates how the control signal and data are activated for a write cycle. Algoritm:

min

tc Cycle time 500 ns tw Clock pulse ("Enable") duration (high and low) 230 ns tsu1 Control signal setup-time, before positive edge 40 ns tsu2 setup-time for data, write, before negative edge 80 ns th hold-time, duration (after negativ edge) 10 ns

ascii_write_controller(cmd/data)

slide-24
SLIDE 24

4/17/18 Chalmers 24

Read Cycle - Characteristics

The Datasheet time diagram illustrates how the control signal should be set and how the display module answers with data.

min max

tc Cycle time 1000 ns tw Clock pulse ("Enable") duration (high and low) 450 ns tsu Control signal setup-time, before positive edge 60 ns tD setup-time for data, reading, before negative edge 360 ns th hold-time, duration (after negative edge) 10 ns

Algoritm:

ascii_read_controller()

slide-25
SLIDE 25

4/17/18 Chalmers 25

Example 3: Programming of the ASCII-Display

Implement functions: ascii_write_controller(cmd/data) ascii_read_controller()

ascii_read_data()

ascii_init() ascii_write_char () ascii_gotoxy ()

slide-26
SLIDE 26

4/17/18 Chalmers 26

Example 3:

#define B_E 0x40 #define B_SELECT 4 #define B_RW 2 #define B_RS 1 void ascii_ctrl_bit_set( unsigned char x ) { GPIO_E.odrLow |= ( B_SELECT | x ); } void ascii_ctrl_bit_clear( unsigned char x ) { GPIO_E.odrLow &= ( B_SELECT | ~x); } void ascii_write_controller( unsigned char c) { ascii_ctrl_bit_set(B_E); GPIO_E.odrHigh = c; delay_250ns(); ascii_ctrl_bit_clear(B_E); } unsigned char ascii_read_controller( void ) { ascii_ctrl_bit_set(B_E); delay_250ns(); delay_250ns(); char c = GPIO_E.idrHigh; ascii_ctrl_bit_clear(B_E); return c; }

slide-27
SLIDE 27

Introduktion C-programmering 27

GPIO

from QuickGuide

Resurser - quickguide-mop.pdf

slide-28
SLIDE 28

4/17/18 Chalmers 28

Arbetsboken, page 85: ascii_gotoxy(row, column) address = row-1

  • m column = 2

address = address + 0x40 ascii_write_cmd(0x80 | address) instruction-specific delay (i.e 43 μs)

Remarks

Add this row for safety.

void ascii_gotoxy( unsigned char x, unsigned char y) { unsigned char address; if(y!=1) address=0x40 | (x-1); else address=x-1; ascii_write_cmd( 0x80 | address); delay_micro( 45 ); }

slide-29
SLIDE 29

4/17/18 Chalmers 29

  • Structs, pointers to structs, array of structs,
  • Port addressing with structs
  • Function pointers,
  • structs with function pointers (object-oriented style)

Next Lecture