Embedded systems 2/7 J.-M Friedt Introduction
Embedded systems 2/7 J.-M Friedt FEMTO-ST/d´epartement temps-fr´equence
[email protected] slides at jmfriedt.free.fr
October 14, 2018
1 / 22
Embedded systems 2/7 J.-M Friedt Introduction
Operating system: the need for drivers
• Hardware abstraction: hide low level functions so that the developer can focus on the functionalities provided by the peripheral → a single entry point providing system calls (open, read, write, close) hiding access to hardware • Homogeneous interface to all peripherals (“Everything is a file”) • Only the kernel can access hardware resources (DMA, interrupts) • Share resources and make sure only one process can access a given hardware function • Add functionalities to the Linux kernel: modules
2 / 22
Embedded systems 2/7 J.-M Friedt Introduction
Virtual memory/hardware memory Hardware memory addressing • hardware memory: a value on the address bus identifies which peripheral is active • each peripheral decodes the address bus to detect whether it is the target of a message • only one peripheral must match a given physical address (otherwise, conflict) Virtual memory addressing • each process has its own address space • memory organization independent of physical constraints • dynamic loading binaries and associated libraries • MMU: translates between hardware and virtual memory addresses Virtual organization (process) 4096
Page 1
Physical organization (CPU) 4096
Page 2 page & 0xff..f000
Page 1 Page 2
page & 0xff..f000 Page 3 page & 0x00..0fff
Page 3 page & 0x00..0fff
Page N
Page N 3 / 22
Embedded systems 2/7 J.-M Friedt Introduction
Hardware access from userspace Through /dev/mem • advantage: not going through kernel abstraction layers (fast) • drawback: not going through kernel abstraction layers (no handling of concurrent memory access) #i n c l u d e < f c n t l . h> #i n c l u d e #d e f i n e MAP SIZE 4096UL #d e f i n e MAP MASK ( MAP SIZE−1)
// MMU page s i z e // mask
i n t main ( i n t a r g c , c h a r ∗∗ a r g v ) { i n t f d ; v o i d ∗ map base , ∗ v i r t a d d r ; unsigned long read result , w r i t e v a l ; o f f t t a r g e t =0x 1 2 34 5 6 78 ; // p h y s i c a l @ f d = open ( " / dev / mem " , O RDWR | O SYNC ) ; // MMU a c c e s s map base=mmap( 0 , MAP SIZE , PROT READ | PROT WRITE , \ MAP SHARED, f d , t a r g e t & ˜MAP MASK) ; v i r t a d d r=map base +( t a r g e t & MAP MASK) ; // v i r t . @ r e a d r e s u l t =∗(( u n s i g n e d l o n g ∗ ) v i r t a d d r ) ; // r e a d mem p r i n t f ( " 0 x % X (% p ) : 0 x % X \ n " , t a r g e t , v i r t a d d r , r e a d r e s u l t ) ; // ∗ ( ( u n s i g n e d l o n g ∗ ) v i r t a d d r ) = w r i t e v a l ; // w r i t e munmap ( map base , MAP SIZE ) ; c l o s e ( f d ) ; r e t u r n 0 ; } 4 / 22
Embedded systems 2/7 J.-M Friedt
The devmem tool
Introduction
• Fast prototyping when accessing processor registers • Available in busybox1 • Use: Read/write from physical address ADDRESS Address to act upon WIDTH Width (8/16/...) VALUE Data to be written
1 $BUILDROOT/output/build/busybox-1.27.1/miscutils/devmem.c 5 / 22
Embedded systems 2/7 J.-M Friedt
Hardware access from the kernel
Introduction
• Userspace: mmap on the /dev/mem pseudo-file • Kernel space: ioremap function after requesting the address range used by the peripheral #i n c l u d e < l i n u x / i o . h> // i o r e m a p #d e f i n e IO BASE 0 x e 0 0 0 a 0 0 0 // UG585 p . 1 3 4 7
and in the initialization function i f ( r e q u e s t m e m r e g i o n ( IO BASE , 0 x2e4 , " GPIO test " )==NULL) p r i n t k (KERN ALERT " mem request failed " ) ; j m f g p i o =(u32 ) i o r e m a p ( IO BASE , 0 x 2 e 4 ) ; // UG585 p . 1 3 4 9 w r i t e l (1