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
Agenda
Bard, Gerstlauer, Cuevas, Holt, Telang, Tiwari, Valvano, Yerraballi
7-2
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
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
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
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
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
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
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
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
FSM Summary
7-11
Phase-Lock-Loop
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
SysTick Timer
Bard, Erez, Gerstlauer, Valvano, Holt, Yerraballi, Telang, Cuevas, Tiwari
RELOAD = n
CURRENT
n=0xFFFFFF
7-13
SysTick Timer
Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Cuevas, Tiwari
7-14
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
Floating point on TM4C123 EE319H
Bard, Erez, Gerstlauer, Valvano, Holt, Yerraballi, Telang, Cuevas, Tiwari
Reset_Handler
; MOVW R0, #0xED88
; MOVT R0, #0xE000
; LDR R1, [R0]
; ORR R1, #0x00F00000
; STR R1, [R0]
7-16
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
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
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