Arquiteturas de sistemas embarcados
Prof.MSc.Rodrigo Maximiano Antunes de Almeida
Universidade Federal de Itajub´a
Interrup¸c˜
ao e Watchdog
Revis˜
ao
Interrup¸c˜
ao
Programa
Principal
Interrupção
Rotina de
Programa
Principal
Latência da Interrupção Acontece a
Interrupção da ISRInício Continua o fluxodo programa
Tempo
Latência da Interrupção Fim da
Interrup¸c˜
ao
Quando uma interrup¸c˜
ao ´
e gerada, o programa ´
e paralizado e uma
fun¸c˜
ao de interrup¸c˜
ao ´
e executada
Conversor AD: quando o resultado da convers˜
ao estiver dispon´ıvel para
leitura.
Porta B: quando algum dos bits configurados como entrada altera seu
valor.
Timer 0: quando acontece overflow em seu contador
Interrup¸c˜
ao
Aten¸
c˜
ao
A fun¸c˜
ao que ir´
a tratar da interrup¸
c˜
ao n˜
ao retorna nem recebe nenhum
valor
Interrup¸c˜
ao
1 v o i d I n t e r r u p c a o (v o i d) i n t e r r u p t 1 {
2 i f ( BitTst ( PIR1 , 0 ) ) { /* c o d i g o */ } // o v e r f l o w do TIMER1 3 i f ( BitTst ( PIR1 , 1 ) ) { /* c o d i g o */ } // c o m p a r a ¸c˜a o TIMER2 = PR2 4 i f ( BitTst ( PIR1 , 2 ) ) { /* c o d i g o */ } // c o m p a r a ¸c˜a o do CCP1 5 i f ( BitTst ( PIR1 , 3 ) ) { /* c o d i g o */ } // f i m e n v i o p o r t . p a r a l e l a 6 i f ( BitTst ( PIR1 , 4 ) ) { /* c o d i g o */ } // f i m t r a n s m i s s ˜a o da S e r i a l 7 i f ( BitTst ( PIR1 , 5 ) ) { /* c o d i g o */ } // r e c e p ¸c ˜a o da S e r i a l 8 i f ( BitTst ( PIR1 , 6 ) ) { /* c o d i g o */ } // f i m de c o n v e r s ˜a o do AD 9 i f ( BitTst ( PIR1 , 7 ) ) { /* c o d i g o */ } // l e i t u r a / e s c r i t a p a r a l e l a 10 i f ( BitTst ( PIR2 , 0 ) ) { /* c o d i g o */ } // c o m p a r a ¸c˜a o do CCP2 11 i f ( BitTst ( PIR2 , 1 ) ) { /* c o d i g o */ } // o v e r f l o w do TIMER3 12 i f ( BitTst ( PIR2 , 2 ) ) { /* c o d i g o */ } // p r o b l e m a na t e n s a o (V) 13 i f ( BitTst ( PIR2 , 3 ) ) { /* c o d i g o */ } // c o l i s ˜a o no b a r r a m e n t o 14 i f ( BitTst ( PIR2 , 4 ) ) { /* c o d i g o */ } // f i m e s c r i t a na f l a s h 15 i f ( BitTst ( PIR2 , 5 ) ) { /* c o d i g o */ } // i n t e r r u p ¸c ˜a o da USB 16 i f ( BitTst ( PIR2 , 6 ) ) { /* c o d i g o */ } // mudan¸ca na c o m p a r a ¸c˜a o 17 i f ( BitTst ( PIR2 , 7 ) ) { /* c o d i g o */ } // f a l h a no o s c i l a d o r 18 i f ( BitTst ( INTCON , 0 ) ) { /* c o d i g o */ } // mudan¸ca na PORTA B 19 i f ( BitTst ( INTCON , 1 ) ) { /* c o d i g o */ } // i n t e r r u p ¸c ˜a o e x t e r n a INT0 20 i f ( BitTst ( INTCON , 2 ) ) { /* c o d i g o */ } // o v e r f l o w no TIMER0 21 i f ( BitTst ( INTCON3 , 0 ) ) { /* c o d i g o */ } // i n t e r r u p ¸c ˜a o e x t e r n a INT1 22 i f ( BitTst ( INTCON3 , 1 ) ) { /* c o d i g o */ } // i n t e r r u p ¸c ˜a o e x t e r n a INT2 23 }
Interrup¸c˜
ao
1 BitClr ( RCON , 7 ) ; // d e s a b i l i t a IPEN ( modo de c o m p a t i b i l i d a d e ) 2 BitSet ( PIE1 , 6 ) ; // l i g a a i n t e r r u p ¸c ˜a o p a r a o AD 3 BitSet ( PIE1 , 5 ) ; // l i g a a i n t e r r u p ¸c ˜a o p a r a a r e c e p ¸c ˜a o na s e r i a l 4 BitSet ( INTCON , 5 ) ; // l i g a a i n t e r r u p ¸c ˜a o p a r a o t i m e r 0 5 BitSet ( INTCON , 3 ) ; // l i g a a i n t e r r u p ¸c ˜a o p a r a a p o r t a B 6 BitSet ( INTCON , 7 ) ; // h a b i l i t a t o d a s a s i n t e r r u p ¸c ˜o e s g l o b a i s 7 BitSet ( INTCON , 6 ) ; // h a b i l i t a t o d a s a s i n t e r r u p ¸c ˜o e s de p e r i f ´e r i c o s
Interrup¸c˜
ao
O watchdog visa aumentar a seguran¸ca do projeto.
Funciona como um temporizador
Precisa ter seu contador constantemente reiniciado.
Caso n˜
ao seja reiniciado no tempo exigido, o watchdog reinicia o
microcontrolador
Interrup¸c˜
ao
1 // Watchdog c o n t r o l a d o p o r s o f t w a r e 2 code c h a r at 0 x 3 0 0 0 0 3 C O N F I G 2 H = 0 x00 ; 3 4 5 #define CLRWTD ( ) _ a s m C L R W D T _ e n d a s m 6 7 v o i d m a i n (v o i d) 8 { 9 u n s i g n e d i n t i ; 10 u n s i g n e d c h a r t e m p ; 11 T R I S D=0x00 ; 12 P O R T D=0x00 ; 13 B i t S e t ( WDTCON , 0 ) ; // l i g a o s i s t e m a de w a t c h d o g 14 f o r( ; ; ) 15 { 16 P O R T D++; 17 f o r( i = 0 ; i < 1 0 0 0 0 ; i++) 18 { 19 C L R W T D ( ) ; 20 } 21 } 22 }Arquiteturas de sistemas embarcados
Arquiteturas de sistemas embarcados
Arquiteturas de sistemas embarcados
´
E sempre interessante definir uma arquitetura para o software
Dependendo dos requisitos do sistema pode-se usar desde uma
programa¸c˜
ao voltada apenas para a aplica¸
c˜
ao at´
e um sistema
operacional
Dentre as t´
ecnicas mais simples ser˜
ao apresentadas trˆ
es alternativas
One single loop
Interrupt control system
Cooperative multitasking
One single loop
One single loop
´
E a estrat´
egia utilizada at´
e agora na disciplina
Consiste em um loop infinito dentro do qual todas as tarefas s˜
ao
executadas
Apenas as rotinas de inicializa¸
c˜
ao acontecem antes dele
Programa¸
c˜
ao ´
e mais simples
Dificuldade de garantir requisi¸
c˜
oes temporais
Escalabilidade, reuso e manuten¸
c˜
ao prejudicados
One single loop
1 v o i d m a i n (v o i d) 2 { 3 // d e c l a r a ¸c ˜a o d a s v a r i ´a v e i s 4 i n t ia , ib , ic ; 5 f l o a t fa , fb , fc ; 6 // i n i c i a l i z a ¸c ˜a o d o s p e r i f ´e r i c o s 7 I n i c i a l i z a T e c l a d o ( ) ; 8 I n i c i a l i z a L C D ( ) ; 9 // ´u n i c o l o o p i n f i n i t o 10 f o r( ; ; ) 11 { 12 // chamada d a s t a r e f a s 13 ia = L e r T e c l a s ( ) ; 14 M u d a D i g i t o ( ia , 0 ) ;15 // tem que s e r e x e c u t a d o p e l o menos a c a d a 1 0 ( ms ) 16 A t u a l i z a D i s p l a y ( ) ;
17 D e b o u n c e T e c l a s ( ) ; 18 }
One single loop
1 v o i d m a i n (v o i d) 2 { 3 // d e c l a r a ¸c ˜a o d a s v a r i ´a v e i s 4 i n t ia , ib , ic ; 5 f l o a t fa , fb , fc ; 6 // i n i c i a l i z a ¸c ˜a o d o s p e r i f ´e r i c o s 7 I n i c i a l i z a T e c l a d o ( ) ; 8 I n i c i a l i z a L C D ( ) ; 9 // ´u n i c o l o o p i n f i n i t o 10 f o r( ; ; ) 11 { 12 // chamada d a s t a r e f a s 13 ia = L e r T e c l a s ( ) ; 14 E n v i a D a d o s ( ia ) ; 15 M u d a D i g i t o ( ia , 0 ) ;16 // tem que s e r e x e c u t a d o p e l o menos a c a d a 1 0 ( ms ) 17 A t u a l i z a D i s p l a y ( ) ; 18 D e b o u n c e T e c l a s ( ) ; 19 ic = R e c e b e S e r i a l ( ) ; 20 // n˜ao cumpre r e q u i s i ¸c ˜a o t e m p o r a l p o r c a u s a da 21 22 fa = 2 . 0 * ic / 3 . 1 4 ; 23 E n v i a S e r i a l ( fa & 0 x 0 0 F F ) ; 24 E n v i a S e r i a l ( fa >> 8 ) ; 25 } 26 }
Interrupt control system
Interrupt control system
Visa resolver o problema das restri¸
c˜
oes temporais
Apenas alguns sub-sistemas podem ser operados via interrup¸
c˜
ao
Os demais sistemas operam no loop principal
Facilita a expans˜
ao do sistema, j´
a que as interrup¸
c˜
oes n˜
ao dependem
do loop principal
´
Otima alternativa para sistemas que tem como requisito o baixo
consumo de energia
Interrupt control system
1 // r o t i n a de tempo v i a i n t e r r u p ¸c ˜a o 2 v o i d I n t e r r u p c a o (v o i d) i n t e r r u p t 1 {
3 i f ( BitTst ( INTCON , 2 ) ) { //TIMER0 : O v e r f l o w 4 B i t C l r ( INTCON , 2 ) ; // l i m p a a f l a g 5 R e s e t a T i m e r ( 5 0 0 0 ) ; // 5ms
6 A t u a l i z a D i s p l a y ( ) ; // tem que s e r e x e c u t a d o p e l o menos a ←-c a d a 8 . 3 ( ms ) 7 } 8 } 9 v o i d m a i n (v o i d) { 10 i n t ia ; 11 I n i c i a l i z a T e c l a d o ( ) ; 12 I n i c i a l i z a D i s p l a y ( ) ; 13 I n i c i a l i z a T i m e r ( ) ; 14 // i n i c i a l i z a ¸c ˜a o da i n t e r r u p ¸c ˜a o
15 B i t C l r ( RCON , 7 ) ; // d e s a b i l i t a IPEN ( modo de c o m p a t i b i l i d a d e ) 16 B i t S e t ( INTCON , 5 ) ; // l i g a a i n t e r r u p ¸c ˜a o p a r a o t i m e r 0 17 B i t S e t ( INTCON , 7 ) ; // h a b i l i t a t o d a s a s i n t e r r u p ¸c ˜o e s g l o b a i s 18 B i t S e t ( INTCON , 6 ) ; // h a b i l i t a t o d a s a s i n t e r r u p ¸c ˜o e s de ←-p e r i f ´e r i c o s 19 f o r( ; ; ) { 20 ia = L e r T e c l a s ( ) ; 21 M u d a D i g i t o ( ia , 0 ) ; 22 } 23 }
Interrupt control system
Aten¸
c˜
ao
Interrupt control system
Aten¸
c˜
ao
Usar as interrup¸
c˜
oes apenas para rotinas simples e R´
APIDAS.
Interrupt control system
Aten¸
c˜
ao
Usar as interrup¸
c˜
oes apenas para rotinas simples, r´
apidas e
Interrupt control system
Interrupt control system
Pode ser utilizado como controle do loop principal
Auxilia em quesitos de temporiza¸
c˜
ao
Permite cria¸
c˜
ao de ”timers”artificiais
Interrupt control system
1 // v a r i ´a v e i s g l o b a l de c o n t r o l e 2 s t a t i c u n s i g n e d i n t t i c k ; 3 s t a t i c u n s i g n e d c h a r l o o p C o n t r o l ; 4 5 // r o t i n a de tempo v i a i n t e r r u p ¸c ˜a o 6 v o i d I n t e r r u p c a o (v o i d) i n t e r r u p t 1 {7 i f ( BitTst ( INTCON , 2 ) ) { //TIMER0 : O v e r f l o w 8 B i t C l r ( INTCON , 2 ) ; // l i m p a a f l a g 9 R e s e t a T i m e r ( 5 0 0 0 ) ; // 5ms
10 A t u a l i z a D i s p l a y ( ) ; // tem que s e r e x e c u t a d o p e l o menos a ←-c a d a 8 . 3 ( ms )
11 t i c k++;
12 l o o p C o n t r o l = 1 ; 13 }
Interrupt control system
1 v o i d m a i n (v o i d) { 2 i n t ia ; 3 I n i c i a l i z a T e c l a d o ( ) ; 4 I n i c i a l i z a D i s p l a y ( ) ; 5 I n i c i a l i z a T i m e r ( ) ; 6 // i n i c i a l i z a ¸c ˜a o da i n t e r r u p ¸c ˜a o7 B i t C l r ( RCON , 7 ) ; // d e s a b i l i t a IPEN ( modo de c o m p a t i b i l i d a d e ) 8 B i t S e t ( INTCON , 5 ) ; // l i g a a i n t e r r u p ¸c ˜a o p a r a o t i m e r 0 9 B i t S e t ( INTCON , 7 ) ; // h a b i l i t a t o d a s a s i n t e r r u p ¸c ˜o e s g l o b a i s 10 B i t S e t ( INTCON , 6 ) ; // h a b i l i t a t o d a s a s i n t e r r u p ¸c ˜o e s de ←-p e r i f ´e r i c o s 11 f o r( ; ; ) { 12 l o o p C o n t r o l = 0 ; 13 i f( ( tick % 2 ) == 0 ) { 14 ia = L e r T e c l a s ( ) ; 15 M u d a D i g i t o ( ia , 0 ) ; 16 } 17 i f( ( tick % 3 ) == 0 ) { 18 D e b o u n c e T e c l a s ( ) ; 19 } 20 w h i l e( l o o p C o n t r o l == 0 ) ; // t i m e d l o o p 21 }
Programando multitasking de modo eficiente
Multitasking
Multitasking ´
e a capacidade de se executar mais de uma tarefa
simultaneamente.
A maioria dos sistemas embarcados (por serem de baixo custo) n˜
ao
possuem esta caracter´ıstica
A alternativa ´
e discretizar o tempo e permitir que as tarefas sejam
executadas em janelas de tempo, de modo sequencial.
Se a alternˆ
ancia entre as tarefas for r´
apida o suficiente, o sistema
apresentar´
a o comportamento de multitasking
Uma das maneiras de projetar este tipo de c´
odigo ´
e atrav´
es de
m´
aquinas de estado
Programando multitasking de modo eficiente
Exemplo de m´
aquina de estados
Inicio
Ler
Teclado
Atualiza
Display
Escreve
Serial
Atualiza
Display
Ler Serial
Atualiza
Display
Programando multitasking de modo eficiente
Cada ciclo representa um estado do sistema, a tarefa atualmente em
execu¸c˜
ao
Apos a fase de inicializa¸c˜
ao, o sistema entra num ciclo
A transposi¸c˜
ao da m´
aquina de estados para linguagem C pode ser
feita de modo simples atrav´
es da estrutura Switch-Case
1 v o i d m a i n (v o i d) { 2 c h a r s l o t ; 3 // I n i c i a l i z a . . . 4 f o r( ; ; ) { // i n i c i o do l o o p i n f i n i t o 5 //******************** top−s l o t ********************** 6 //*********** i n´ıc i o da m´aquina de e s t a d o ************ 7 s w i t c h( slot ) { 8 c a s e 0 : 9 L e T e c l a d o ( ) ; s l o t = 1 ; b r e a k; 10 c a s e 1 : 11 A t u a l i z a D i s p l a y ( ) ; s l o t = 2 ; b r e a k; 12 c a s e 2 : 13 R e c e b e S e r i a l ( ) ; s l o t = 3 ; b r e a k; 14 c a s e 3 : 15 A t u a l i z a D i s p l a y ( ) ; s l o t = 4 ; b r e a k; 16 c a s e 4 : 17 E n v i a S e r i a l ( ) ; s l o t = 5 ; b r e a k; 18 c a s e 5 : 19 A t u a l i z a D i s p l a y ( ) ; s l o t = 0 ; b r e a k; 20 d e f a u l t: 21 s l o t = 0 ; b r e a k; 22 } 23 //************ fim da m´aquina de e s t a d o ************** 24 //****************** bottom−s l o t ********************* 25 } // f i m l o o p i n f i n i t o ( ! ? )
Programando multitasking de modo eficiente
Deve-se notar que foram criadas trˆ
es ´
areas dentro de cada ciclo
A m´
aquina de estado
O top-slot
O bottom-slot
O aumento de tarefas se torna mais simples, basta adicionar outro
slot com a fun¸
c˜
ao desejada
O bottom e top slots podem ser usados para tarefas que deveriam ser
realizadas em todos os ciclos como a AtualizaDisplay() por exemplo
1 v o i d m a i n (v o i d) i n t e r r u p t 0{ 2 c h a r s l o t ; 3 // I n i c i a l i z a . . . 4 f o r( ; ; ) { // i n i c i o do l o o p i n f i n i t o 5 //******************** top−s l o t ********************** 6 A t u a l i z a D i s p l a y ( ) ; 7 //*********** i n´ıc i o da m´aquina de e s t a d o ************ 8 s w i t c h( slot ) { 9 c a s e 0 : 10 L e T e c l a d o ( ) ; s l o t = 1 ; b r e a k; 11 c a s e 1 : 12 R e c e b e S e r i a l ( ) ; s l o t = 2 ; b r e a k; 13 c a s e 2 : 14 E n v i a S e r i a l ( ) ; s l o t = 0 ; b r e a k; 15 d e f a u l t: 16 s l o t = 0 ; b r e a k; 17 } 18 //************ fim da m´aquina de e s t a d o ************** 19 //****************** bottom−s l o t ********************* 20 } // f i m l o o p i n f i n i t o ( ! ? ) 21 }
Programando multitasking de modo eficiente
Uma boa pr´
atica ´
e temporizar os slots
Deste modo cada ciclo tem o mesmo tempo para executar
Isto gera previsibilidade no funcionamento do sistema
1 v o i d m a i n (v o i d) { 2 c h a r s l o t ; 3 // I n i c i a l i z a . . . 4 f o r( ; ; ) { // i n i c i o do l o o p i n f i n i t o 5 //******************** top−s l o t ********************** 6 R e s e t a T i m e r ( 5 0 0 0 ) ; // 5 ms p a r a c a d a s l o t 7 A t u a l i z a D i s p l a y ( ) ; 8 //*********** i n´ıc i o da m´aquina de e s t a d o ************ 9 s w i t c h( slot ) { 10 c a s e 0 : 11 L e T e c l a d o ( ) ; s l o t = 1 ; b r e a k; 12 c a s e 1 : 13 R e c e b e S e r i a l ( ) ; s l o t = 2 ; b r e a k; 14 c a s e 2 : 15 E n v i a S e r i a l ( ) ; s l o t = 0 ; b r e a k; 16 d e f a u l t: 17 s l o t = 0 ; b r e a k; 18 } 19 //************ fim da m´aquina de e s t a d o ************** 20 //****************** bottom−s l o t ********************* 21 A g u a r d a T i m e r ( ) ; 22 } // f i m l o o p i n f i n i t o ( ! ? ) 23 }
Interrupt control system
1 // v a r i ´a v e i s g l o b a l de c o n t r o l e 2 s t a t i c u n s i g n e d c h a r l o o p C o n t r o l ; 3 4 // r o t i n a de tempo v i a i n t e r r u p ¸c ˜a o 5 v o i d I n t e r r u p c a o (v o i d) i n t e r r u p t 1 {6 i f ( BitTst ( INTCON , 2 ) ) { //TIMER0 : O v e r f l o w 7 B i t C l r ( INTCON , 2 ) ; // l i m p a a f l a g 8 R e s e t a T i m e r ( 5 0 0 0 ) ; // 5ms
9 l o o p C o n t r o l = 1 ; 10 }
1 v o i d m a i n (v o i d) { 2 c h a r s l o t ; 3 // I n i c i a l i z a . . . 4 f o r( ; ; ) { // i n i c i o do l o o p i n f i n i t o 5 //******************** top−s l o t ********************** 6 l o o p C o n t r o l == 0 ; 7 A t u a l i z a D i s p l a y ( ) ; 8 //*********** i n´ıc i o da m´aquina de e s t a d o ************ 9 s w i t c h( slot ) { 10 c a s e 0 : 11 L e T e c l a d o ( ) ; s l o t = 1 ; b r e a k; 12 c a s e 1 : 13 R e c e b e S e r i a l ( ) ; s l o t = 2 ; b r e a k; 14 c a s e 2 : 15 E n v i a S e r i a l ( ) ; s l o t = 0 ; b r e a k; 16 d e f a u l t: 17 s l o t = 0 ; b r e a k; 18 } 19 //************ fim da m´aquina de e s t a d o ************** 20 //****************** bottom−s l o t ********************* 21 w h i l e( l o o p C o n t r o l == 0 ) ; // t i m e d l o o p 22 } // f i m l o o p i n f i n i t o ( ! ? ) 23 }
Programando multitasking de modo eficiente
Exemplo da mudan¸
ca de slots no tempo
Top S.1 S.2 S.3 Bottom "vago" 0 5 10 15 20 25 30