Upload
lydung
View
212
Download
0
Embed Size (px)
Citation preview
LearningOutcome#2
How?
A. BusTimingAnalysisB. 9S12CMultiplexedBusExpansionC. General-PurposeI/OPortsD. InterruptHandlingE. BufferedI/OF. Buffered,Interrupt-DrivenPrinterDesignExample
“Anabilitytointerfaceamicrocontrollertovariousdevices”
[2.C]-2
Objective“BufferedI/O” Why?
DevicesaremuchslowerthanCPUs(yes– eventhe9S12)
[2.C]-3
Problems:Dataproduction/consumptionratesaredifferent.
CPUslowedbydevice.EX:CPUhasacharactertoprint- printerisbusy,soCPUmustwait.DatamaybecomingintoCPUbeforetheCPUisreadytodealwithit.
Solution:Abuffer
[2.C]-4
Foroutputdevices- wanttheCPUtoputdatainabufferassoonasit’sready(don’twaitforthedevicetonolongerbebusy).
Thesesimplerelationshiparecalledproducer/consumer.
ProduceData
BufferConsumeData
Solution:ABuffer
[2.C]-5
Forinputdevices- wantthedevicetoplacedatainabufferasit’sproduced.ThentheCPUcangrabitwhentheCPUisready.
Thesesimplerelationshiparecalledproducer/consumer.
ProduceData
BufferConsumeData
CircularBuffers
• Spaceisfiniteandwedonothavedynamicmemorymanagement(i.e.malloc/freeornew/delete).
• Allocateafixedsizeinmemory,then“wraparound”• ManagedwithaFirstinFirstOut(FIFO)policy.
[2.C]-6
Circularbuffers• 2pointersmanagebufferallocationanddeallocation
• IN:pointstonextavailablelocation.Producerputsdatahere.• OUT: pointstonextdataiteminFIFOorder.Consumertakesdatafromhere.• Wearegoingtomanagethebufferwithnoothermeta-data.
[2.C]-7
IN OUT
CircularBuffers
[2.C]-10
IN
OUT
BufferFull:(IN+1)modBufferSize ==OUT
0 12
345
6
7
Example:BufferSize =8
A B
C
D
F
G
Why?
Can the buffer be full when OUT points to index 3(A) Yes(B) No(C) I really can’t tell
E
CircularBufferAlgorithms• Producerprocess
1.Checkstateofbuffer2.IfbufferisFULL,waitforspace3.Else,placecharacterinbufferatlocationpointedtobyIN4.IncrementINpointermoduloBUFSIZE5.Exit
11
Spin-waitwhenbufferisfull