1 of 19

EE 319K�Introduction to Embedded Systems

Lecture 7: Finite state machines, SysTick

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

Read Chapter 5

https://www.youtube.com/playlist?list=PLyg2vmIzGxXEle4_R2VA_J5uwTWktdWTu

Read Chapter 5 book, ebook Chapter 10

7-1

2 of 19

Agenda

  • Recap
    • Stepper motor FSM
    • Use struct to create data structures
  • Outline
    • Solve lab 3 with FSM
    • Phase Lock Loop (PLL)
    • SysTick
    • Profiling sort functions (EE319H)

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

7-2

3 of 19

Call by Value vs Reference

Call-by-Value

Caller:

uint16_t var;

var = 22;

Add5(var);

//Q: What is var here?

//A: 22

Callee:

void Add5(uint16_t in){

in = in+5;

// Q: What is in here??

// A: 27

}

Call-by-value sends value to the subroutine, the subroutine can use the value but any changes it makes is to its copy of the value

Call-by-Reference

Caller:

uint16_t var;

var = 22;

Add5_pt(&var);

//Q: What is var here?

//A: 27

Callee:

void Add5_pt(uint16_t *in){

*in = *in+5;

// Q: What is *in here??

// A: 27

}

Call-by-reference sends the address (pointer) of the variable to the subroutine, when the subroutine modify that which the pointer points to, it changes the actual variable

7-3

4 of 19

Lab 3 with FSM

PE1 is input

PE2 is output

Touch and release cycles

10% is 50,450ms

30% is 150,350ms

50% is 250,250ms

70% is 350,150ms

90% is 450,50ms

Start with 50% duty cycle

Bard, Gerstlauer, Cuevas, Holt, Valvano, Yerraballi

High

1

250ms

Low

0

250ms

0

0

7-4

5 of 19

Lab 3 with FSM

High

1

50ms

Low

0

450ms

0

0

High

1

150ms

Low

0

350ms

0

0

High

1

250ms

Low

0

250ms

0

0

High

1

350ms

Low

0

150ms

0

0

High

1

450ms

Low

0

50ms

0

0

Thought question: how does the FSM remember information?

What does Lab 3 do if you are not pushing the button?

7-5

6 of 19

Lab 3 with FSM

High

1

50ms

Low

0

450ms

0

0

High

1

150ms

Low

0

350ms

0

0

High

1

250ms

Low

0

250ms

0

0

High

1

350ms

Low

0

150ms

0

0

High

1

450ms

Low

0

50ms

0

0

Change0

0

50ms

1

1

1

0

What does Lab 3 do if you do push the button?

7-6

7 of 19

Lab 3 with FSM

High

1

50ms

Low

0

50ms4

0

0

High

1

150ms

Low

0

350ms

0

0

High

1

250ms

Low

0

250ms

0

0

High

1

350ms

Low

0

150ms

0

0

High

1

450ms

Low

0

50ms

0

0

Change0

0

50ms

1

1

1

0

Change1

0

50ms

1

1

1

0

7-7

8 of 19

Lab 3 with FSM

High

1

50ms

Low

0

450ms

0

0

High

1

150ms

Low

0

350ms

0

0

High

1

250ms

Low

0

250ms

0

0

High

1

350ms

Low

0

150ms

0

0

High

1

450ms

Low

0

50ms

0

0

Change0

0

50ms

1

1

1

0

Change1

0

50ms

1

1

1

0

Change2

0

50ms

1

1

1

0

Change3

0

50ms

1

1

1

Change4

0

50ms

1

1

1

0

Init

7-8

9 of 19

FSM Engine in C (Index)

void main(void) {

uint32_t S; // state index

uint32_t Input;

// initialize ports and timer

S = High30; // start state

while(1) {

GPIO_PORTE_DATA_R = fsm[S].Out; // set LED

SysTick_Wait1ms(fsm[S].Delay);

Input = {GPIO_PORTE_DATA_R&0x02)>>1; // PE1

S = fsm[S].Next[Input];

}

}

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

Exact same controller as before

7-9

10 of 19

FSM Engine in C (Pointers) EE319H

State_t *Pt; // state pointer

uint32_t Input;

void main(void) {

// initialize ports and timer

Pt = High30; // start state

while(1) {

GPIO_PORTE_DATA_R = Pt->Out; // set LED PE2

SysTick_Wait1ms(Pt->Time);

Input = {GPIO_PORTE_DATA_R&0x02)>>1; // PE1

Pt = Pt->Next[Input];

}

}

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

Exact same controller as before

7-10

11 of 19

FSM Summary

  • Abstraction separates
    • What it does (STG)
    • How it works (C code)
  • Finite State Machines
    • States define what you know/believe
    • 1-1 mapping STG ⬄ STT ⬄ C structure
    • No conditional statements
  • Run Lab3_FSM (in EE319K\LabSolutionsKeil5\Lab3_FSM)
  • Run Lab 5 solution

7-11

12 of 19

Phase-Lock-Loop

  • Internal oscillator requires minimal power but is imprecise
  • External crystal provides stable bus clock
  • TM4C123 is equipped with 16.000 MHz crystal and bus clock can be set to a maximum of 80 MHz

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

What? Software sets bus frequency

Why? Accuracy, speed/power tradeoff

How? Grad school

Tolerance 50*10-6

7-12

13 of 19

SysTick Timer

  • Timer/Counter operation
    • 24-bit counter decrements at bus clock
      • With 80 MHz bus clock, decrements every 12.5 ns
    • Counting is from n → 0
      • Assume RELOAD= n
      • CURRENT is a modulo n+1 counter
        • next_value = (current_value-1) mod (n+1)
        • Sequence: n,n-1,n-2,n-3… 2,1,0,n,n-1…

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

RELOAD = n

CURRENT

n=0xFFFFFF

7-13

14 of 19

SysTick Timer

  • Initialization (4 steps)
    • Step1: Clear ENABLE to stop counter
    • Step2: Specify the RELOAD value
    • Step3: Clear counter by writing NVIC_ST_CURRENT_R
    • Step4: Set NVIC_ST_CTRL_R
      • CLK_SRC = 1 (bus clock is the only option)
      • INTEN = 0 for no interrupts
      • ENABLE = 1 to enable

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

7-14

15 of 19

SysTick Timer in C

Bard, Gerstlauer, Valvano, Yerraballi

#define NVIC_ST_CTRL_R (*((volatile uint32_t *)0xE000E010))

#define NVIC_ST_RELOAD_R (*((volatile uint32_t *)0xE000E014))

#define NVIC_ST_CURRENT_R (*((volatile uint32_t *)0xE000E018))

void SysTick_Init(void){

NVIC_ST_CTRL_R = 0; // 1) disable SysTick during setup

NVIC_ST_RELOAD_R = 0x00FFFFFF; // 2) maximum reload value

NVIC_ST_CURRENT_R = 0; // 3) any write to CURRENT clears it

NVIC_ST_CTRL_R = 0x00000005; // 4) enable SysTick with core clock

}

// The delay parameter is in units of the 80 MHz core clock(12.5 ns)

void SysTick_Wait(uint32_t delay){

NVIC_ST_RELOAD_R = delay-1; // number of counts

NVIC_ST_CURRENT_R = 0; // any value written to CURRENT clears

while((NVIC_ST_CTRL_R&0x00010000)==0){ // wait for COUNT flag

}

}

// Call this routine to wait for delay*10ms

void SysTick_Wait10ms(uint32_t delay){

for(uint32_t i=0; i<delay; i++){

SysTick_Wait(800000); // wait 10ms

}

7-15

16 of 19

Floating point on TM4C123 EE319H

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

  • Keil project settings
    • Use single precision

  • Edit startup.s
    • Turn on floating point

Reset_Handler

; MOVW R0, #0xED88

; MOVT R0, #0xE000

; LDR R1, [R0]

; ORR R1, #0x00F00000

; STR R1, [R0]

7-16

17 of 19

Floating point on TM4C123 EE319H

Bard, Erez, Gerstlauer, Valvano, Y Holt, erraballi, Telang, Cuevas, Tiwari

uint32_t FtoC1(uint32_t tempF){

return ((tempF-32)*5)/9;

}

float FtoC2(float tempF){

return (tempF-32.0F)*(5.0F/9.0F);

}

double FtoC3(double tempF){

return (tempF-32.0)*(5.0/9.0);

}

7-17

18 of 19

Use SysTick to profile EE319H

uint32_t C1;

float C2;

double C3;

//*************Profile*****************

uint32_t start,time1,time2,time3;

start = NVIC_ST_CURRENT_R;

C1 = FtoC1(212);

time1 = (start-NVIC_ST_CURRENT_R)&0x00FFFFFF;

start = NVIC_ST_CURRENT_R;

C2 = FtoC2(212.0F);

time2 = (start-NVIC_ST_CURRENT_R)&0x00FFFFFF;

start = NVIC_ST_CURRENT_R;

C3 = FtoC3(212.0);

time3 = (start-NVIC_ST_CURRENT_R)&0x00FFFFFF;

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

7-18

19 of 19

EE312H Profiling execution time

Seed random number generator srand(NVIC_ST_CURRENT_R);

Profile three sort functions

Start1 = NVIC_ST_CURRENT_R;

insertionSort(a, len);

Stop1 = NVIC_ST_CURRENT_R;

ExecutionTime1 = (Start1-Stop1)&0x0FFFFFF-Offset;

7-19