Presentation is loading. Please wait.

Presentation is loading. Please wait.

32-bitni mikrokontroleri i primena - MS1BMP 2. deo

Similar presentations


Presentation on theme: "32-bitni mikrokontroleri i primena - MS1BMP 2. deo"— Presentation transcript:

1 32-bitni mikrokontroleri i primena - MS1BMP 2. deo
2014 Nenad Jovičić

2 Šta je arhitektura?

3 Cortex-M3 – procesor baziran na ARMv7-M arhitekturi
RISC procesor Harward arhitektura

4 Cortex-M3 core periferije
NVIC (Nested Vector Interrupt Controller) – integrisani prekidni kontroler koji omogućava procesiranje prekida sa malim kašnjenjem WIC (Wakeup Interrupt Controller) – opcioni kontroler zahteva za buđenje iz režima smanjene potrošnje. SYSTICK – sistemski tajmer tj. 24bitni brojač na dole namenjen za podršku generisanju sistemskog prekida za operativne sisteme. MPU (Memory Protection Unit) – opciona jedinica za kontrolu pristupa pojedinim regionima u memoriji ETM (Embedded Trace Macrocell) – modul koji obezbeđuje logovanje izvršavanja instrukcija DWT (Data Watchpoint and Trace Unit) – modul koji implementira data watchpoint FPB (Flash Patch and Breakpoint Unit) – modul koji omogućava preusmeravanje izvršavanja programa iz CODE memorije u neki drugi deo memorije.

5 Magistrale za pristup instrukcijama i podacima
ICode memorijska magistrala bazirana na AHB-Lite standardu – koristi se za prenos instrukcija iz Code adresnog prostora (0x do 0x1FFFFFFF). Dcode memorijska magistrala bazirana na AHB-Lite standardu – koristi se za prenos podataka iz Code adresnog prostora (0x do 0x1FFFFFFF). System memorijska magistrala bazirana na AHB-Lite standardu – koristi se za prenos i instrukcija i podataka iz System adresnog prostora (0x do 0xDFFFFFFF i 0xE do 0xFFFFFFFF). PPB magistrala – pristup internim i eksternim privatnim periferijama.

6 AMBA – advanced microcontroller bus architecture
High Performance ARM processor APB UART High Bandwidth External Memory Interface Timer APB Bridge AHB Keypad High-bandwidth on-chip RAM DMA Bus Master PIO AMBA systems are based around two buses, a high performance system bus and a lower performance peripheral bus. The high performance bus (AHB) should connect all of the high performance, high bandwidth modules, such as the ARM Processor, any DMA engines, perhaps some fast, 32 bit wide local RAM (or wider to suit the ARM Bus Master being used), an external memory interface, and the interface to the lower performance bus. The number of modules connected here should be kept to a minimum to reduce the bus loading on this high performance bus and allow it to run much faster. The bulk of the design modules are placed on the lower performance Peripheral Bus (APB). Modules placed here are not accessed as frequently as AHB modules, and as a result of the APB timing, need not consume power (in their APB interfaces) when there is no APB activity. The address and data bus widths on the APB only need to be as wide as required, compared to the AHB where the maximum address and data widths are specified to maximize system performance in this critical area. AHB – advanced high-performance bus APB – advanced peripheral bus Visoke performanse Protočni prenos Burst prenosi Višestruki masteri Niska potrošnja Mala brzina Jednostavan interfejs Veza sa Slave periferijama

7 Tipična arhitektura Cortex-M3 mikrokontrolera

8 Registri procesora Registri opšte namene:
13 registara opšte namene, r0-r12 Pokazivač steka SP (r13) Link registra LR (r14) Programski brojač PC (r15) Registri r0-r7 (niži registri) su dostupni preko svih instrukcija Registri r8-r12 (viši registri) su dostupni preko svih 32-bitnih instrukcija. Specijalni registri: Programski statusni registar xPSR Registri za kontrolu prekida PRIMASK, FAULTMASK, BASEPRI Kontrlni registra CONTROL

9 Stanja i režimi rada procesora
Dva stanja – thumb (radno stanje) i debug (zaustavljen rad) Dva režima rada procesora – thread i handler režim. Dva nivoa privilegija – privilegovani nivo (OS, prekidi) i neprivilegovani (korisnički softver).

10 Modovi “rada” softvera
Privilegovani mod: U njega se automatski prelazi ulaskom u prekid. Potpuno su dostupni svi sistemski resursi. U ovom modu je moguće menjati nivo privilegije. Neprivilegovani mod: Uobičajeni režim običnog programa. Nisu dostupni svi sistemski resursi. Promena nivoa privilegija moguća samo nakon prekida. Za razliku od moda rada procesora, nivo privilegija se menja programski.

11 Privilegovani/neprivilegovani rad
Promena nivoa privilegije je moguća samo u privilegovanom modu rada. U prekidu je moguće uticati na nivo privilegije prilikom povratka u thread režim procesora. OS aplikacije Jednostavne aplikacije

12 Modovi rada procesora kod najnaprednijih ARM arhitektura (ARMv7-A)
U različitim modovima rada dozvoljeni su pristupi različitim resursima procesora.

13 SP – Stack pointer Registar koji se fizički realizuje kao dva 32-bitna registra. Main Stack Pointer (MSP) – ovaj registar je podrazumevani registar posle reseta i mogu da ga koriste i korisnički program i prekidne rutine. U slučaju korišćenja operativnog sistema, ovaj registar koriste Kernel i prekidi, kao i neki delovi korisničkih Thread-ova sa posebnim privilegijama. Process Stack Pointer (PSP) – ovaj registar je namenjen za korišćenje od strane neprivilegovanih delova softvera. Na primer, to su korisnički Thread-ovi u Operativnom sistemu. Može da se koristi samo u Thread modu. U zavisnosti od odgovarajućeg konfiguracionog bita u CONTROL registru pristupa se jednom ili drugom od ova dva registra. Jednostavne aplikacije bey OS-a uglavnom korsite samo MSP. Kod Cortex-M3 familije stek je realizovan kao Full Descending Stack, što znači da stek raste prema nižim adresama i pokazuje na poslednju zauzetu adresu. Sve PUSH i POP operacije su 32-bitne.

14 LR – Link Register LR se koristi za čuvanje adrese povratka iz potprograma - na primer kada je skok izvršen BL (Branch with Link) instrukcijom:

15 Statusni rigistri Application Program Status Register, APSR
Sadrži flegove koje koristi korisnički neprivilegovani aplikativni softver Interrupt Program Status Register, IPSR Sadrži broj izuzetka/prekida koji se trenutno izvršava Execution Program Status Register, EPSR Sadrži specijalne flegove koji kontrolišu rad procesora. Na primer biti ICI/IT se koriste za kontrolu prekidanja uslovnih blokova instrukcija i instrukcija koje rade višestruke Load/store operacije (LDM/STM)

16 Registri za kontrolu prekida
Setovanje PM bita u PRIMASK registru maskira sve prekide osim NMI i HardFault prekida. Setovanje FM bita u FAULTMASK registru maskira sve prekide osim NMI prekida. BASEPRI registra definiše nivo prioriteta prekida kojima je dozvoljeno generisanje.

17 Pristup registrima kontrole prekida
Iz C-a: Iz Asembler-a:

18 CONTROL registar Control registar poseduje samo dva bita.
Njime se kontroliše korišćenje steka i nivo privilegije softvera. Control registar može da se menja samo iz privilegovanog softvera.

19 3-stepena protočna obrada
Registraska banka 2 porta za čitanje, 1 za upis. Dodatni portovi za čitanje i upis PC-a. Barell pomerač ALU Adresni registar sa podrškom za inkrementiranje Dekoder instrukcija i kontrolna jedinica

20 Pipeline Pipeline u tri nivoa – tako zvana trostepena protočna obrada.
Pretetch jedinica može da unapred učita do tri 32-bitne instrukcije. Branch forwarding & speculation 1st Stage - Fetch 2nd Stage - Decode 3rd Stage - Execute Execute stage branch (ALU branch & Load Store Branch) Fetch (Prefetch) AGU Instruction Decode & Register Read Branch Address Phase & Write Back Data Phase Load/Store & Branch Multiply & Divide Shift ALU & Branch Write A prefetch unit fetches instructions in advance and can buffer up to three 32-bit instructions or six 16-bit instructions, or a combination. For direct branches (address or label vs. register), It does what’s called Branch Forwarding where in decode, it can fetch both possible instructions following a conditional branch (the fall through instruction and the branch target instruction) (point to first dotted). Then in execute, the branch is resolved and if the branch is not to be taken, the next instruction is already available. If the branch is to be taken, the branch instruction is made available at the same time as the decision is made, resulting in a single idle cycle. In an ARM7, branches always flush the pipe and usually cost 3 cycles. CM3 runs at bus speed – you must fetch instructions at CPU clock speed, mostly without wait states. These two points may limit how fast you can push the CM3 in any particular process.

21 Pipeline Fetch – učitavanje instrukcije iz memorije i smeštanje u prefetch buffer Decode – dekodiranje instrukcije i pripremanje kontrolnih signala koij regulišu protok podataka Execute – Očitavanje registara, pomeranje operanda, generisanje rezultata operacije (ALU) i upisivanje rezultata odredišni registar.

22 Optimalni pipeline Sve instrukcije su nad registrima
Efektivno dobijamo instrukciju po ciklusu  All operations here are on registers (single cycle execution)  In this example it takes 6 clock cycles to execute 6 instructions  Clock cycles per Instruction (CPI) = 1

23 Optimalni pipeline – obrada podataka

24 Pipeline sa memorijskim prenosom
 In this example it takes 7 clock cycles to execute 6 instructions  Clock cycles per Instruction (CPI) = 1.2  The read cycle must complete on the bus before the LDR instruction can complete since there is only one writeback port in the register file

25 Pipeline sa memorijskim prenosom
Computes a memory address similar to a data processing instruction. ‰Load instruction follows a similar pattern except that the data from memory only gets as far as the ‘data in’register on the 2ndcycle and a 3rd cycle is needed to transfer the data from there to the destination register

26 Pipeline sa uzastopnim Load/Store instrukcijama
 Store buffer allows STR instruction to finish before store cycle completes on the bus  Back-to-back STR instructions pipeline on the AHB-Lite bus - Also works for LDR-STR-LDR-STR

27 Pipeline sa instrukcijom skoka
3 ciklusa su potrebna da se završi skok It takes 3 cycles to complete the branch Worst case scenario – indirect branch taken

28 Pipeline sa instrukcijom skoka
The third cycle, which is required tocomplete thepipeline refilling, is also used to mark the small correction to the value stored in the linkregister in order that is points directly at the instruction which follows the branch

29 Arhitektura instrukcijskog seta ISA Instruction set architecture
“Instruction set architecture (ISA) is the structure of a computer that a machine language programmer (or a compiler) must understand to write a correct (timing independent) program for that machine” IBM introducing 360 in 1964

30 Razvoj instrukcijskog seta
32-bitni instrukcijski set Pomešan 16-bitni i 32-bitni instrukcijski set Počev od ARM7TDMI procesora uvedena su dva instrukcijska seta- ARM i Thumb. ARM je bit 32-bitni, a Thumb 16-bitni. Ideja je bila da procesor može da se prebacuje programski iz jednog moda u drugi. ARM instrukcijski set je trebalo da podrži kompleksne instrukcije kakve imaju aplikativni procesori, dok je Thumb trebalo da pruži jednostavnije, ali kratke i vremenski efikasne instrukcije potrebne u mikrokontrolerskim aplikacijama. Počev od v7-M arhitekture uveden je novi Thumb-2 superset koji pokriva i ne 32-bitne instrukcije, podržava stari Thumb instrukcijski set i dodaje i neke nove i 16-bitne instrkucije. v7-R i v7-A arhitektura podržavaju i dalje ARM instrukcijski set. Glavna karakteristika kod Thumb-2 instrukcijskog seta je što podržava i 32-bitne i 16-bitne instrukcije...Dakle nam switching-a nego sve ide protočno... 16-bitni instrukcijski set

31 Glavni elementi arhitekture instrukcijskog seta
32-bits mov r0, #1 ld r1, [r0,#5] mem((r0)+5) bne loop subs r2, #1

32 ARM/Thumb instrukcijski set
Tradicionalni ARM set: fiksna dužina od 32 bita Dva ili tri operanda Radi se sa podacima u registrima Memorijske transferi se obavljaju samo kroz Load/Store instrukcije Uslovno izvršavanje instrukcija Originalni Thumb instrukcijski set: Implemenaticija određenih instrukcija iz ARM seta, ali sa 16 bita Većina Thumb instrukcija ima dva operanda, tj. Jedan od izvorišnih je i odredišni operand Nisu dostupni registri r8 do r15 Nema uslovnog izvršavanja ARM coding

33 ARM/Thumb instrukcijski set
Primer implementacije instrukcije sabiranja na ARM i Thumb formatu: -nema uslovnog izvršavanja -nema rotacije neposrednog operanda -statusni flagovi se uvek postavljaju (nema S)

34 Prelaz sa ARM na Thumb i obrnuto
Kod ranijih ARM arhitektura prebacivanje sa jednog na drugi instrukcijski set je posedovalo značajan overhead. With the introduction of the Thumb-2 instruction set, it is now possible to handle all processing requirements in one operation state. There is no need to switch between the two. In fact, the Cortex-M3 does not support the ARM code. Even interrupts are now handled with the Thumb state. (Previously, the ARM core entered interrupt handlers in the ARM state.) Zanimljive instrukcije: • UFBX, BFI, and BFC: Bit field extract, insert, and clear instructions • UDIV and SDIV: Unsigned and signed divide instructions • WFE, WFI, and SEV: Wait-For-Event, Wait-For-Interrupts, and Send-Event; these allow the processor to enter sleep mode and to handle task synchronization on multiprocessor systems • MSR and MRS: Move to special register from general-purpose register and move special register to general-purpose register; for access to the special registers ALI, Cortex-M3 ne podržava sve instrukcije iz Thumb-2 seta. Na primer instrukcije za koprocesor nisu podržane, kao i instrukcije za Single Instruction–Multiple Data (SIMD) instrukcije. Sa druge strane neke instrukcije iz starog Thumb seta nisu pdoržane. Na primer instrukcije za izmenu stanja procesora (Koje su ranije korišćene da se iz Thumb seta pređe u ARM set).

35 Thumb-2 Thumb-2 obuhvata i 16-bitne i 32-bitne instrukcije.
Napravljen je sa osnovnom idejom da se izbegne potreba za izmenom režima rada procesora. Thumb-2 je zadržao skoro sve insturkcije starijeg Thumb seta i implementirao većinu instrukcija iz ARM seta. Veći deo instrukcija može da se prevede i u 16-bitni i u 32-bitni format u zavisnosti od parametara konkretne instrukcije. Odluku o implementaciji donosi kompajler osim u slučaju kada se implicitno traži jedna ili druga implementacija.

36 16-bitne Thumb-2 instrukcije

37 32-bitne Thumb-2 instrukcije
Ako učitana reč (16 bita) sadrži najviših 5 bitova sledećeg sadržaja: 0b11101 0b11110 0b11111 instrukcija je 32-bitna, tj. Dekodira se i sledeća reč.

38 16 ili 32 bita? Primer instrukcije ADD (sabiranje) kada se radi nad registrima. U zavisnosti od upotrbeljenih registara ili opcionih polja u instrukciji moguće su različite vrste kodiranja instrukcije. To se u krajnjoj liniji reflektuje na gustinu koda.

39 Značaj prefetch buffer-a

40 Poređenje performansi instrukcijskih setova
Instrukcijski set promenljive dužine ARM instrukcije su bile isključivo 32-bitne Thumb instrukcije su bile isključivo 16-bitne Thumb-2 instrukcije mogu da budu i 16-bitne i 32-bitne, Thumb-2 daje za oko 26% veću gustinu koda od ARM Thumb-2 daje za oko 25% veće performanse u odnosu na Thumb Relative Dhrystone performance and code size for ARM, Thumb and Thumb-2 Dhrzstone test je kreiran 1984 godine i namenjen je za testiranje tj. Upoređivanje performansi u celobrojnoj airtmetici. Nasuprot njemu Whetstone je namenjen za testiranje floating-point aritmetike. Variable-length instructions ARM instructions are a fixed length of 32 bits Thumb instructions are a fixed length of 16 bits Thumb-2 instructions can be either 16-bit or 32-bit Thumb-2 gives approximately 26% improvement in code density over ARM Thumb-2 gives approximately 25% improvement in performance over Thumb

41 Tipovi podataka ARM podržava tri tipa podataka:
Byte – 8-bitni podatak Halfword – 16-bitni podatak Word – 32-bitni podatak Pristup memoriji kod Cortex-M3 omogućava takozvani neporavnat (unaligned) pristup. Ali to ne važi kod nekih instrukcija. Na primer PUSH i POP rade samo aligned acces. Takođe i specijalne instrukcije za prenos blokova podataka.

42 Modovi adresiranja Neposredno adresiranje – sastavni deo instrukcije je podatak, tj. konstanta. Registarsko adresiranje – podatak je u nekom od registara opšte namene. Indirektno registarsko adresiranje – podatak je u memoriji a adresa se nalazi ili proračunava iz odgovarajućeg registra opšte namene. Immediate: The offset is an unsigned number that can be added to or subtracted from the base register value. Immediate offset addressing is useful for accessing data elements that are a fixed distance from the start of the data object, such as structure fields, stack offsets and input/output registers. Register: The offset is a value from a general-purpose register. This register cannot be the PC. The value can be added to, or subtracted from, the base register value. Register offsets are useful for accessing arrays or blocks of data. Scaled register: The offset is a general-purpose register, other than the PC, shifted by an immediate value, then added to or subtracted from the base register. This means an array index can be scaled by the size of each array element. The offset and base register can be used in three different ways to formthe memory address. The addressing modes are described as follows: Offset The offset is added to or subtracted from the base register to form the memory address. Pre-indexed The offset is added to or subtracted from the base register to form the memory address. The base register isthen updated with this new address, to permit automatic indexing through an array or memory block. Post-indexed The value of the base register alone is used as the memory address. The offset is then added to or subtracted from the base register. and this value is stored back in the base register, to permit automatic indexing through an array or memory block.

43 Load /store arhitektura
Cortex-M3 je Load/Store arhitektura, što znači da se memoriji pristupa samo preko Load/Store instrukcija, a sve druge operacije se obavljaju samo nad registrima. Nema neposrednog adresiranja memorijske lokacije.

44 LDRx/STRx instrukcije
Pristup memoriji je moguć praktično jedino preko ovih instrukcija. Sve kasnije instrukcije za obradu podataka rade sa podacima koji su sastavni deo instrukcije (konstante) i podacima koji su već u registrima. Primeri:

45 LDRx/STRx instrukcije
Adresa podatka u memoriji se dobija kao kombinacija sadržaja baznog registra i odgovarajućeg offset-a Vrednost offset-a se određuje na sledeće načine: Neposredno: Offset je zapisan neposredno u samoj instrukciji. Rigaistarsko: Offset se nalazi u offset registru Skalirano registarsko: Offset se dobija tako što se sadržaj offset registra šiftuje za vrednost konstante zapisane u insturkciji Izračunati offset i bazni registar mogu na sledeće načine da formiraju konačnu adresu: Offset (klasični offset): Prosto dodavanje sadržaja offsetnog na bazni registar Pre-indexno: Adresa se dobija sabiranjem sadržaja baznog registra i offset-a, ali se nakon izvršene operacije sadržaj baznog registra ažurira na novu vrednost Post-indexno: Adresa se dobija samo na osnovu baznog registra, ali se nakon instrukcije na sadržaj baznog registra dodaje sadržaj izračunatog offset-a.

46 Kako upisati 32-bitnu konstantu u registar?

47 Još bolji metod! Još bolji način je aseblerska sintaksa:
LDR r0,=0x koja generiše: LDR r0,[pc, offsetto lit pool] A negde u code memoriji se nalazi oblast lit pool u kojoj se nalazi flashovana konstanta: DCD 0x


Download ppt "32-bitni mikrokontroleri i primena - MS1BMP 2. deo"

Similar presentations


Ads by Google