1 of 20

EE 319K�Introduction to Embedded Systems

Lecture 13c: �Lab 10,

Edge trigger, Physics, Trig, Movies

Bard, Tiwari, Cuevas, Holt, Gerstlauer, Valvano, Erez, Telang, Yerraballi

13-1

2 of 20

Agenda

  • Recap
    • Bitmaps
    • Sprites
    • Game engine
    • Drawing
  • Agenda
    • Edge-triggered interrupts
    • Physics with fixed point
    • Trig
    • Movie
    • Backgrounds
    • Ray tracing

Bard, Tiwari, Cuevas, Holt, Gerstlauer, Valvano, Erez, Telang, Yerraballi

https://www.youtube.com/watch?v=KgrlOPFZtlo

13-2

3 of 20

TM4C Interrupt Vectors

77 interrupt sources on TM4C123 with 8 priority levels

Vector address

Num

IRQ

ISR

NVIC

Priority bits

0x0000003C

15

-1

SysTick Handler

NVIC_SYS_PRI3_R

31 – 29

0x00000040

16

0

GPIO Port A

NVIC_PRI0_R

7 – 5

0x00000044

17

1

GPIO Port B

NVIC_PRI0_R

15 – 13

0x00000048

18

2

GPIO Port C

NVIC_PRI0_R

23 – 21

0x0000004C

19

3

GPIO Port D

NVIC_PRI0_R

31 – 29

0x00000050

20

4

GPIO Port E

NVIC_PRI1_R

7 – 5

0x00000054

21

5

UART0 Rx/Tx

NVIC_PRI1_R

15 – 13

0x00000058

22

6

UART1 Rx/Tx

NVIC_PRI1_R

23 – 21

0x0000008C

35

19

Timer 0A

NVIC_PRI4_R

31 – 29

0x00000090

36

20

Timer 0B

NVIC_PRI5_R

7 – 5

0x00000094

37

21

Timer 1A

NVIC_PRI5_R

15 – 13

0x00000098

38

22

Timer 1B

NVIC_PRI5_R

23 – 21

0x0000009C

39

23

Timer 2A

NVIC_PRI5_R

31 – 29

0x000000A0

40

24

Timer 2B

NVIC_PRI6_R

7 – 5

0x000000B8

46

30

GPIO Port F

NVIC_PRI7_R

23 – 21

EE 445L – Bard, McDermott, Valvano

13-3

4 of 20

When a Port F interrupt is triggered…

  • The current instruction is finished
  • Eight registers are pushed on the stack with R0 on top
    • pushed onto whichever stack is active: either the MSP or PSP
  • The vector address is loaded into the PC
  • The IPSR register is set to 46 (Port F)
  • The top 24 bits of LR are set to 0xFFFFFF, and the bottom 8 bits:
    • 0xF1 Return to Handler mode MSP (if interrupt of an ISR)
    • 0xF9 Return to Thread mode MSP
    • 0xFD Return to Thread mode PSP

EE 445L – Bard, McDermott, Valvano

4

We will use this

0xE9 means floating point

13-4

5 of 20

When a Port F interrupt is triggered…

EE 445L – Bard, McDermott, Valvano

Vector address

ISR_Num

IRQ

ISR

NVIC

Priority bits

0x000000B8

46

30

GPIO Port F

NVIC_PRI7_R

23 – 21

13-5

6 of 20

Both Edge Interrupt Initialization

EE 445L – Bard, McDermott, Valvano

6

void Switch_Init(void){

SYSCTL_RCGCGPIO_R |= 0x00000020; // activate clock for port F

while((SYSCTL_PRGPIO_R & 0x00000020) == 0){};

GPIO_PORTF_DIR_R &= ~0x10; // make PF4 in (built-in button)

GPIO_PORTF_DEN_R |= 0x10; // enable digital I/O on PF4

GPIO_PORTF_PUR_R |= 0x10; // enable weak pull-up on PF4

GPIO_PORTF_IS_R &= ~0x10; // PF4 is edge-sensitive

GPIO_PORTF_IBE_R |= 0x10; // PF4 is both edges

GPIO_PORTF_ICR_R = 0x10; // clear flag4

GPIO_PORTF_IM_R |= 0x10; // arm interrupt on PF4

NVIC_PRI7_R = (NVIC_PRI7_R

& 0xFF00FFFF)

| 0x00A00000; // Set Priority Level to 5

NVIC_EN0_R = 0x40000000; // enable interrupt 30 in NVIC

}

13-6

7 of 20

Rising Edge Counter Interrupt Service Routine

EE 445L – Bard, McDermott, Valvano

7

void GPIOPortF_Handler(void){

GPIO_PORTF_ICR_R = 0x10; // ack PF4

//GPIO_PORTF_RIS_R is trigger flag

if(GPIO_PORTF_DATA_R&0x10){ // 0x10 means released

// execute release task

}

else{

// execute touch task

}

}

0.22uF

10k

10k*0.22uF = 2.2ms

100

100*0.22uF = 22us

Ceramic

Switch is open, Q=CV charge on capacitor

Switch first closes, Q flows through 100 to ground

13-7

8 of 20

Trig

Cosine

LDR R1,=CosTab

LDRSH R0,[R1,R0, LSL #1]

BX LR

; 256 16-bit signed values implementing x=cos(theta)

; indices are the integer parts of a fixed-point value theta with resolution 2pi/256

; so as index varies from 0 to 255, theta varies from 0 to 255*2pi/256

; table values are integer parts of a fixed-point value x with resolution 1/32767

; so as table varies from -32767 to +32767 x=cos(theta) varies from -1 to +1

CosTab DCW 32767, 32757, 32728, 32678, 32609, 32521, 32412, 32285, 32137, 31971, 31785, 31580, 31356, 31113, 30852, 30571

DCW 30273, 29956, 29621, 29268, 28898, 28510, 28105, 27683, 27245, 26790, 26319, 25832, 25329, 24811, 24279, 23731

DCW 23170, 22594, 22005, 21403, 20787, 20159, 19519, 18868, 18204, 17530, 16846, 16151, 15446, 14732, 14010, 13279

DCW 12539, 11793, 11039, 10278, 9512, 8739, 7962, 7179, 6393, 5602, 4808, 4011, 3212, 2410, 1608, 804, 0, -804, -1608

DCW -2410, -3212, -4011, -4808, -5602, -6393, -7179, -7962, -8739, -9512, -10278, -11039, -11793, -12539, -13279, -14010

DCW -14732, -15446, -16151, -16846, -17530, -18204, -18868, -19519, -20159, -20787, -21403, -22005, -22594, -23170

DCW -23731, -24279, -24811, -25329, -25832, -26319, -26790, -27245, -27683, -28105, -28510, -28898, -29268, -29621

DCW -29956, -30273, -30571, -30852, -31113, -31356, -31580, -31785, -31971, -32137, -32285, -32412, -32521, -32609

DCW -32678, -32728, -32757, -32767, -32757, -32728, -32678, -32609, -32521, -32412, -32285, -32137, -31971, -31785

DCW -31580, -31356, -31113, -30852, -30571, -30273, -29956, -29621, -29268, -28898, -28510, -28105, -27683, -27245

DCW -26790, -26319, -25832, -25329, -24811, -24279, -23731, -23170, -22594, -22005, -21403, -20787, -20159, -19519

DCW -18868, -18204, -17530, -16846, -16151, -15446, -14732, -14010, -13279, -12539, -11793, -11039, -10278, -9512

DCW -8739, -7962, -7179, -6393, -5602, -4808, -4011, -3212, -2410, -1608, -804, 0, 804, 1608, 2410, 3212, 4011, 4808

DCW 5602, 6393, 7179, 7962, 8739, 9512, 10278, 11039, 11793, 12539, 13279, 14010, 14732, 15446, 16151, 16846, 17530

DCW 18204, 18868, 19519, 20159, 20787, 21403, 22005, 22594, 23170, 23731, 24279, 24811, 25329, 25832, 26319, 26790

DCW 27245, 27683, 28105, 28510, 28898, 29268, 29621, 29956, 30273, 30571, 30852, 31113, 31356, 31580, 31785, 31971

DCW 32137, 32285, 32412, 32521, 32609, 32678, 32728, 32757

13-8

9 of 20

Arbitrary movement

  • Old Physics

  • Solution: fixed point
    • Define position, velocities as integer/FIX

Bard, Tiwari, Cuevas, Holt, Gerstlauer, Valvano, Erez, Telang, Yerraballi

void Move(void){

oldx = x; oldy=y;

y = y+vy; // integration

x = x+vx; // integration

}

void Draw(void){

if(life == alive){

ST7735_DrawBitmap(oldx,oldy,bimage,xsize,ysize);

ST7735_DrawBitmap(x,y,image,xsize,ysize);

}

}

#define FIX 16

void Move(void){

oldx = x; oldy=y;

y = y+vy; // integration

x = x+vx; // integration

}

void Draw(void){

if(life == alive){

ST7735_DrawBitmap(oldx/FIX,oldy/FIX,

bimage,xsize,ysize);

ST7735_DrawBitmap(x/FIX,y/FIX,image,xsize,ysize);

}

}

13-9

10 of 20

Animation

  • Old Sprite

  • How to define animation?
    • What is a movie?
    • What parameters are needed?

Bard, Tiwari, Cuevas, Holt, Gerstlauer, Valvano, Erez, Telang, Yerraballi

class Sprite{

private:

int32_t x,y,oldx,oldy; // coordinates (in fixed point)

int32_t vx,vy; // velocity(in fixed point)

uint32_t xsize,ysize;

const uint16_t *image; // ptr->image

const uint16_t *bimage; // ptr->image

status_t life; // dead/alive

13-10

11 of 20

Animation

  • New fields

  • Add frame method

  • Create bill, executed dynamically

Bard, Tiwari, Cuevas, Holt, Gerstlauer, Valvano, Erez, Telang, Yerraballi

const uint16_t *movie[10]; // ptr->image

uint32_t t; // 0, 1, 2, ...nframes-1

uint32_t nframes; // number of frames

int AddMovieFrame(const uint16_t *p){

if(nframes >= 10) return false;

movie[nframes] = p;

nframes++;

return true;

}

bill.AddMovieFrame(blender2);

bill.AddMovieFrame(blender3);

bill.AddMovieFrame(blender4);

bill.AddMovieFrame(blender3);

bill.AddMovieFrame(blender2);

13-11

12 of 20

Animation drawing

  • Old draw

  • New draw method

Bard, Tiwari, Cuevas, Holt, Gerstlauer, Valvano, Erez, Telang, Yerraballi

void DrawOld(void){

if(life == alive){

ST7735_DrawBitmap(oldx/FIX,oldy/FIX,bimage,xsize,ysize);

ST7735_DrawBitmap(x/FIX,y/FIX,image,xsize,ysize);

}

}

void Draw(void){

if(life == alive){

ST7735_DrawBitmap(oldx/FIX,oldy/FIX,bimage,xsize,ysize);

ST7735_DrawBitmap(x/FIX,y/FIX,movie[t],xsize,ysize);

t++;

if(t>=nframes) t =0;

}

}

13-12

13 of 20

Background Images

  • One LCD image does not fit in RAM
  • Draw sprite with mask (green screen)

Black means transparent

Bard, Tiwari, Cuevas, Holt, Gerstlauer, Valvano, Erez, Telang, Yerraballi

16 by 16

128 by 160

13-13

14 of 20

Background Images

  • RAM buffer size of sprite
  • Fill buffer with background at sprite location

Bard, Tiwari, Cuevas, Holt, Gerstlauer, Valvano, Erez, Telang, Yerraballi

Background where sprite will be

16 by 16

128 by 160

13-14

15 of 20

Background Images

  • Merge two images
  • Draw merged image at sprite location

Bard, Tiwari, Cuevas, Holt, Gerstlauer, Valvano, Erez, Telang, Yerraballi

Merged image

13-15

16 of 20

Rotating sprites

  • Draw many images

  • Choose image based on angle

Bard, Tiwari, Cuevas, Holt, Gerstlauer, Valvano, Erez, Telang, Yerraballi

ST7735_DrawBitmap(Player.x/FIX,

Player.y/FIX,

Player.image[(32*Player.theta)/FIX],

18,18);

13-16

17 of 20

Ray Tracing

13-17

18 of 20

Ray Tracing

13-18

19 of 20

Perspective

Field is a list of points

(xi,yi,0)

connected by lines

Point of view (z constant)

(x,y,z,θ)

Distance

d=sqrt((xi-x)2 +(yi-y)2)

Pixel height

h = z*(d-a)/d

x

y

θ

Point of view

Point of view

LCD

z

h

d

a

d-a

d

(xi,yi,0)

(xi,yi,0)

(x,y,z,θ)

(x,y,z,θ)

13-19

20 of 20

Perspective

x

y

θ

Point of view

LCD

w

a

Φ

(xi,yi,0)

(x,y,z,θ)

vx = cos(θ)

vy = sin(θ)

Φ = arctan((yi-y)/(xi-x))

φ = Φ-θ

w = sin(φ)

φ

void ST7735_Line(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color);

13-20