Presentation is loading. Please wait.

Presentation is loading. Please wait.

Module 2.E Buffered I/O Tim Rogers 2017.

Similar presentations


Presentation on theme: "Module 2.E Buffered I/O Tim Rogers 2017."— Presentation transcript:

1 Module 2.E Buffered I/O Tim Rogers 2017

2 Learning Outcome #2 Bus Timing Analysis
“An ability to interface a microcontroller to various devices” Bus Timing Analysis 9S12C Multiplexed Bus Expansion General-Purpose I/O Ports Interrupt Handling Buffered I/O Buffered, Interrupt-Driven Printer Design Example How?

3 Objective Why? “Buffered I/O”
Devices are much slower than CPUs (yes – even the 9S12) Problems: Data production/consumption rates are different. CPU slowed by device. EX: CPU has a character to print - printer is busy, so CPU must wait. Data may be coming in to CPU before the CPU is ready to deal with it.

4 These simple relationship are called producer/consumer.
Solution: A buffer For output devices - want the CPU to put data in a buffer as soon as it’s ready (don’t wait for the device to no longer be busy). Buffer Produce Data Consume Data These simple relationship are called producer/consumer.

5 These simple relationship are called producer/consumer.
Solution: A Buffer For input devices - want the device to place data in a buffer as it’s produced. Then the CPU can grab it when the CPU is ready. Buffer Consume Data Produce Data These simple relationship are called producer/consumer.

6 Circular Buffers Space is finite and we do not have dynamic memory management (i.e. malloc/free or new/delete). Allocate a fixed size in memory, then “wrap around” Managed with a First in First Out (FIFO) policy.

7 Circular buffers 2 pointers manage buffer allocation and deallocation
IN: points to next available location. Producer puts data here. OUT: points to next data item in FIFO order. Consumer takes data from here. We are going to manage the buffer with no other meta-data. IN OUT

8 Circular Buffers OUT IN Example: BufferSize = 8 Empty condition:
IN == OUT

9 Circular Buffers A B C D OUT IN Example: BufferSize = 8
Content in buffer A B C D IN

10 (IN+1) mod BufferSize == OUT
Can the buffer be full when OUT points to index 3 (A) Yes (B) No (C) I really can’t tell Can the buffer be full when OUT points to index 3 (A) Yes  (B) No (C) I really can’t tell Circular Buffers Example: BufferSize = 8 OUT Buffer Full: (IN+1) mod BufferSize == OUT A B Why? IN C G D E F

11 Circular Buffer Algorithms
Spin-wait when buffer is full Producer process 1. Check state of buffer 2. If buffer is FULL, wait for space 3. Else, place character in buffer at location pointed to by IN 4. Increment IN pointer modulo BUFSIZE 5. Exit

12 Circular Buffer Algorithms
Spin-wait when buffer is empty Consumer Process 1. Check state of buffer 2. If buffer is EMPTY, wait for data 3. Else, get data from location pointed to by OUT pointer 4. Increment OUT pointer modulo BUFSIZE 5. Exit


Download ppt "Module 2.E Buffered I/O Tim Rogers 2017."

Similar presentations


Ads by Google