Title: EE 319K Introduction to Embedded Systems
1EE 319KIntroduction to Embedded Systems
- Lecture 4Data structures, Finite state machines
2Agenda
- Outline
- More on SysTick Timer
- Bit Specific Addressing
- Data structures
- Finite State Machines, Indexed Implementation.
- Interrupts
3SysTick Timer in C
define NVIC_ST_CTRL_R(((volatile uint32_t
)0xE000E010)) define NVIC_ST_RELOAD_R(((volati
le 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_R0x000100
00)0) // wait for flag // Call this
routine to wait for delay10ms void
SysTick_Wait10ms(uint32_t delay) unsigned long
i for(i0 iltdelay i)
SysTick_Wait(800000) // wait 10ms
4I/O Port Bit-Specific
- I/O Port bit-specific addressing is used to
access port data register - Define address offset as 42b, where b is the
selected bit position - 256 possible bit combinations (0-8)
- Add offsets for each bit selected to base address
for the port - Example PF4 and PF0
Port F 0x4005.D000 0x4005.D0000x00040x0040
0x4005.D044 Provides friendly and atomic
access to port pins
5Recap Array access
- Calculate address from Base and index
- Byte Baseindex
- Halfword Base2index
- Word Base4index
- Size_N BaseNindex
- Access sequentially using pointers
- Byte pt pt1
- Halfword pt pt2
- Word pt pt4
- Size_N pt ptN
6Abstraction
- Software abstraction
- Define a problem with a minimal set of basic,
abstract principles / concepts - Separation of concerns via interface/policy
mechanisms - Straightforward, mechanical path to
implementation - Three advantages of abstraction are
- it can be faster to develop
- it is easier to debug (prove correct) and
- it is easier to change
7Finite State Machine (FSM)
- Finite State Machines (FSMs)
- Set of inputs, outputs, states and transitions
- State graph defines input/output relationship
- What is a state?
- Description of current conditions
- What is a state graph?
- Graphical interconnection between states
- What is a controller?
- Software that inputs, outputs, changes state
- Accesses the state graph
8Finite State Machine (FSM)
- What is a finite state machine?
- Inputs (sensors)
- Outputs (actuators)
- States
- State Transition Graph
- Output Determination
9Finite State Machine (FSM)
- Moore FSM
- output value depends only on the current state,
- inputs affect the state transitions
- significance is being in a state
- Input when to change state
- Output definition of being in that state
10Finite State Machine (FSM)
- Moore FSM Execution Sequence
- Perform output corresponding to the current state
- Wait a prescribed amount of time (optional)
- Read inputs
- Change state, which depends on the input and the
current state - Go back to 1. and repeat
-
11Finite State Machine (FSM)
- Mealy FSM
- output value depends on input and current state
- inputs affect the state transitions.
- significance is the state transition
- Input when to change state
- Output how to change state
Inputs Control
Outputs Brake, Gas
12Finite State Machine (FSM)
- Mealy FSM Execution Sequence
- Wait a prescribed amount of time (optional)
- Read inputs
- Perform output, which depends on the input and
the current state - Change state, which depends on the input and the
current state - Go back to 1. and repeat
13Finite State Machine (FSM)
14FSM Implementation
- Data Structure embodies the FSM
- multiple identically-structured nodes
- statically-allocated fixed-size linked structures
- one-to-one mapping FSM state graph and linked
structure - one structure for each state
- Linked Structure
- pointer (or link) to other nodes (define next
states) - Table structure
- indices to other nodes (define next states)
15Traffic Light Control
PE10, PE00 means no cars exist on either
road PE10, PE01 means there are cars on the
East road PE11, PE00 means there are cars on
the North road PE11, PE01 means there are cars
on both roads
goN, PB5-0 100001 makes it green on North
and red on East waitN, PB5-0 100010 makes it
yellow on North and red on East goE, PB5-0
001100 makes it red on North and green on
East waitE, PB5-0 010100 makes it red on
North and yellow on East
16Traffic Light Control
17Linked Data Structure in C
- const struct State
- uint32_t Out
- uint32_t Time // 10 ms units
- uint32_t Next4 // list of next states
-
- typedef const struct State STyp
- define goN 0
- define waitN 1
- define goE 2
- define waitE 3
- STyp FSM4
- 0x21,3000,goN,waitN,goN,waitN,
- 0x22, 500,goE,goE,goE,goE,
- 0x0C,3000,goE,goE,waitE,waitE,
- 0x14, 500,goN,goN,goN,goN
18FSM Engine in C (Moore)
- void main(void)
- uint32 CS // index of current state
- uint32_t Input
-
- // initialize ports and timer
-
- CS goN // start state
- while(1)
- LIGHT FSMCS.Out // set lights
- SysTick_Wait10ms(FSMCS.Time)
- Input SENSOR // read sensors
- CS FSMCS.NextInput
-
-