embarqu´es 15/16 J.-M Friedt
Syst` emes embarqu´ es 15/16
J.-M Friedt
FEMTO-ST/d´ epartement temps-fr´ equence jmfriedt@femto-st.fr
transparents ` a jmfriedt.free.fr
22 mars 2018
1 / 17
embarqu´es 15/16
J.-M Friedt
Communication par /sys
Cr´eation du point de communciation/sys/bus/platform/devices/gpio-simple.0 s t a t i c s t r u c t p l a t f o r m d e v i c e ∗p d e v ;
s t a t i c s t r u c t p l a t f o r m d r i v e r g p i o s i m p l e d r i v e r = { . p r o b e = g p i o s i m p l e p r o b e ,
. r e m o v e = g p i o s i m p l e r e m o v e , . d r i v e r = {. name = " gpio - s i m p l e ", }, };
s t a t i c i n t i n i t g p i o s i m p l e i n i t (v o i d)
{p l a t f o r m d r i v e r r e g i s t e r (& g p i o s i m p l e d r i v e r ) ;
p d e v = p l a t f o r m d e v i c e r e g i s t e r s i m p l e (" gpio - s i m p l e ", 0 , → ,→NULL , 0 ) ;
i f ( IS ERR ( p d e v ) )
{p l a t f o r m d r i v e r u n r e g i s t e r (& g p i o s i m p l e d r i v e r ) ; r e t u r n PTR ERR ( p d e v ) ;
} r e t u r n 0 ; }
s t a t i c v o i d e x i t g p i o s i m p l e e x i t (v o i d) {p l a t f o r m d e v i c e u n r e g i s t e r ( p d e v ) ;
p l a t f o r m d r i v e r u n r e g i s t e r (& g p i o s i m p l e d r i v e r ) ;
} 2 / 17
embarqu´es 15/16
J.-M Friedt
Points de communication
Ajout d’un syst`eme de fichier1/sys/bus/platform/devices/gpio-simple.0/value s t a t i c DEVICE ATTR ( v a l u e , 0 4 4 4 , g p i o s h o w , NULL ) ;
s t a t i c s s i z e t g p i o s h o w (s t r u c t d e v i c e ∗dev , s t r u c t → ,→d e v i c e a t t r i b u t e ∗a t t r , c h a r ∗b u f )
{r e t u r n s p r i n t f ( b u f , " % d \ n ", g p i o g e t v a l u e ( g p i o ) ) ;}
s t a t i c i n t g p i o p r o b e (s t r u c t p l a t f o r m d e v i c e ∗p d e v ) {e r r = d e v i c e c r e a t e f i l e (& pdev−>dev , & d e v a t t r v a l u e ) ;
r e t u r n 0 ; }
s t a t i c i n t g p i o r e m o v e (s t r u c t p l a t f o r m d e v i c e ∗p d e v ) {d e v i c e r e m o v e f i l e (& pdev−>dev , & d e v a t t r v a l u e ) ;}
s t a t i c s t r u c t p l a t f o r m d r i v e r g p i o s i m p l e d r i v e r = { . p r o b e = g p i o p r o b e ,
. r e m o v e = g p i o r e m o v e ,
. d r i v e r = {. name = " gpio - s i m p l e ", }, };
Attribut de la plateforme : une fonction de callback pour la lecture
1. https://www.kernel.org/doc/htmldocs/device-drivers/
API-platform-device-register-simple.html 3 / 17
embarqu´es 15/16
J.-M Friedt
Communication
Fonction de callback
2lorsqu’une requˆ ete est faite vers le point de communication (selon les attributs du fichier) :
s t a t i c DEVICE ATTR ( v a l u e o u t , S IWUSR , NULL , e c r i t u r e ) ; s t a t i c i n t v a l e u r = 0 ;
s t a t i c s s i z e t e c r i t u r e (s t r u c t d e v i c e ∗dev , s t r u c t → ,→d e v i c e a t t r i b u t e ∗a t t r ,
c o n s t c h a r ∗b u f , s i z e t c o u n t ) {i f ( ! c o u n t ) r e t u r n −EINVAL ;
s s c a n f ( b u f , " % d \ n ", &v a l e u r ) ; r e t u r n c o u n t ;
}
Cas de l’´ ecriture
2. K.C. Accardi,How to Write a Device Driver, `aftp://ftp.polsl.pl/pub/
linux/kernel/people/kristen/presentations/FISL9.0/device_driver.pdf
4 / 17
embarqu´es 15/16
J.-M Friedt
Communication
// methode show
s t a t i c DEVICE ATTR ( v a l u e 1 , 0 4 4 0 , g p i o s i m p l e s h o w 1 , NULL ) ; s t a t i c DEVICE ATTR ( v a l u e 2 , 0 4 4 0 , g p i o s i m p l e s h o w 2 , NULL ) ; // methode s t o r e
s t a t i c DEVICE ATTR ( v a l u e 3 , 0 2 2 0 , NULL , g p i o s i m p l e r e a d 1 ) ;
se traduit par
# ls /sys/bus/platform/drivers/gpio-simple/gpio-simple.0/
driver driver_override modalias power subsystem uevent value1 value2 value3
dont les actions sont par exemple
i n t g p i o s i m p l e s h o w 1 (s t r u c t d e v i c e ∗dev , s t r u c t → ,→d e v i c e a t t r i b u t e ∗a t t r , c h a r ∗b u f )
{ r e t u r n s p r i n t f ( b u f , " H e l l o W o r l d 1\ n ") ; }
5 / 17
embarqu´es 15/16
J.-M Friedt
Plateforme v.s pilote
• Un module se charge par init et se d´ echarge par exit
• un seul module peut ˆ etre charg´ e ` a un instant donn´ e
insmod: ERROR: could not insert module xxx.ko: File exists
• un module ne peut pas recevoir de configuration au moment du chargement par le noyau
• un module peut charger une plateforme :
s t a t i c s t r u c t p l a t f o r m d e v i c e ∗pd1 ,∗pd2 ; s t a t i c i n t i n i t g p i o s i m p l e i n i t (v o i d)
{pd1= p l a t f o r m d e v i c e r e g i s t e r s i m p l e (" jmf ", 0 , NULL , 0 ) ; pd2= p l a t f o r m d e v i c e r e g i s t e r s i m p l e (" jmf ", 1 , NULL , 0 ) ; r e t u r n( 0 ) ;
}
• la plateforme d´ ecrit le mat´ eriel
6 / 17
embarqu´es 15/16
J.-M Friedt
Plateforme v.s pilote
• les plateformes se voient passer un argument
s t a t i c s t r u c t p l a t f o r m d r i v e r j m f d r i v e r = { . p r o b e = j m f p r o b e ,
. r e m o v e = j m f r m ,
. d r i v e r = {. name = " jmf ",}, };
s t a t i c i n t j m f r m (s t r u c t p l a t f o r m d e v i c e ∗p d e v ) {p r i n t k ( KERN ALERT " dr bye % d \ n ", pdev−>i d ) ;r e t u r n 0 ;}
s t a t i c i n t j m f p r o b e (s t r u c t p l a t f o r m d e v i c e ∗p d e v ) {p r i n t k ( KERN ALERT " dr lo % d \ n ", pdev−>i d ) ;r e t u r n 0 ;}
// $LINUX/ D o c u m e n t a t i o n / d r i v e r−m o d e l / p l a t f o r m . t x t // ∗ p l a t f o r m d e v i c e . i d . . . t h e d e v i c e i n s t a n c e // number , o r e l s e ”−1” t o i n d i c a t e t h e r e ’ s o n l y o n e .
s t a t i c i n t i n i t j m f i n i t (v o i d)
{p l a t f o r m d r i v e r r e g i s t e r (& j m f d r i v e r ) ;r e t u r n 0 ;}
•
[ 1238.117164] platform init # insmod pl [ 1240.112258] driver init # insmod dr [ 1240.112283] jmf hello 0[ 1240.112298] jmf hello 1
[ 1262.529881] jmf bye 0 # rrmod pl [ 1262.529914] jmf bye 1
[ 1262.529922] platform exit # rmmod dr 7 / 17
embarqu´es 15/16
J.-M Friedt
S´ eparation module/pilote
• un module charge plusieurs instances d’une plateforme (driver)
s t a t i c s t r u c t p l a t f o r m d e v i c e ∗p1 , ∗p2 ; s t a t i c i n t i n i t g p i o s i m p l e i n i t (v o i d)
{p1= p l a t f o r m d e v i c e r e g i s t e r s i m p l e (" jmf ", 0 , NULL , 0 ) ; p2= p l a t f o r m d e v i c e r e g i s t e r s i m p l e (" jmf ", 1 , NULL , 0 ) ; . . .
• le driver ne contient que la d´ efinition de la plateforme : les m´ ethodes init et exit ne contiennent que platform driver register(); et platform driver unregister();
• ⇒ d´ efinition implicite des m´ ethodes init et exit du module par module platform driver(gpio simple driver); avec
s t a t i c s t r u c t p l a t f o r m d r i v e r g p i o s i m p l e d r i v e r = { . p r o b e = g p i o s i m p l e p r o b e ,
. r e m o v e = g p i o s i m p l e r e m o v e , . d r i v e r = {. name = " jmf ",},
};
m o d u l e p l a t f o r m d r i v e r ( g p i o s i m p l e d r i v e r ) ;
8 / 17
embarqu´es 15/16
J.-M Friedt
Application
• Partant de l’exemple de module avec timer en /dev, impl´ ementer une m´ ethode read qui se d´ ebloque ` a intervalles de temps r´ eguliers (utiliser un mutex)
• Proposer le programme C correspondant qui affiche un message ` a intervalle de temps d´ etermin´ e par le timer
• Remplacer /dev/ par une plateforme dans /sys
Pour chaque ´ etape, effectuer la d´ emonstration sur PC puis sur carte Redpitaya.
9 / 17