Download presentation
Presentation is loading. Please wait.
Published byJoshua Moody Modified over 9 years ago
1
CSCI1600: Embedded and Real Time Software Lecture 16: Advanced Programming with I/O Steven Reiss, Fall 2015
2
Problem to Consider Keypad for entering alarm code Task to read the keypad Determine when a button has been pushed Want to check a sequence of button presses against known code(s) Task that does the checking How do these communicate?
3
Making Communication Simple
4
Handshake Communication The previous depended on timing (or sequential tasks) What should we do when tasks need to synchronize Task A sends a request to Task B Task A needs to know when Task B is done In order to send another request Ensure no new button is pressed until current one is processed Can use 2 variables rather than one Task A owns variable REQ Task B owns variable ACK
5
Handshake Communication Code A: set Data, set REQ, wait for ACK, clear REQ, wait for ~ACK B: wait for REQ, read Data, set ACK, wait for ~REQ, clear ACK Sequence A: set Data, set REQ B: wait for REQ, read Data, set ACK A: wait for ACK, clear REQ, wait for ~ACK B: wait for ~REQ, clear ACK
6
Communicating Arduinos Suppose you want to have two Arduinos talk to each other Just simple message back and forth (sequence of bits) How would you do this?
7
Communication: Queues More complex communication involves multiple requests Model railroad: multiple switches can be triggered But only one can be activated at a time This is generally implemented as a queue Allows the two tasks to be asynchronous
8
Queue-based Tasks
9
Queue Implementation Queues in embedded programs Are generally fixed in size Often sized so they never overflow Or data is discarded if they would How to program a queue?
10
Simple Queue byte queue[10]; int qp = 0; void push(byte x) [ TASK A ] if qp < 10, then queue[qp++] = x else exception byte pop() [ TASK B ] if (qp == 0) exception rslt = queue[0]; for (i = 1; i < qp; ++i) queue[i-1] = queue[i] qp = qp-1 return rslt What’s wrong with this?
11
Circular Buffer Queue Use a single array Maintain a start and end pointer Treat the array as a circular buffer Element beyond the last is the first, etc. Maintain two pointers Head (read): pointer to the first element to extract Tail (write): pointer to the next place to insert
12
Circular Buffer Pop: if read == write then exception result = buf[read]; read = (read + 1) % size
13
Circular Buffer Push(data): If read == write then exception buf[write] = data write = (write + 1)%size
14
Circular Buffer Advantages Little data movement Pointers are only changed by one task Queue cells are safely read/written (why?) Code is simpler Extensions Can read/write multiple things at once Data stream or communications channel
15
Circular Buffers and Handshakes What happens with a circular buffer of size one?
16
Multiple Inputs on a Port How to multiplex a port with different inputs Generally unsafe to connect multiple sensors at once If sensor is at ground, another sensor at high might ground thru the first, not through the input port Then the input would always be low To get around this Switches: only provide power/ground to enabled ones Other logic circuits/devices: Open collector
17
Open Collector Circuit Add a transistor to circuit Acts as a switch Effectively unconnected if off External pull up makes it high Grounded if on Result is low Can connect multiple Effectively an OR gate Many ICs have this built in Hence negated outputs
18
Serial Communication Send data from one machine to another Using only 2 wires Using clocked serial input 10 bits to send 8 Plus parity?
19
UART: Hardware for Serial I/O Accepts bytes in parallel from the CPU Sends these serially at fixed rate Reads bytes from the serial line Tells CPU when a byte is ready to read Includes lines to ensure synchronization Ready to transmit, …
20
UART Internals
21
Interrupts Interrupts allow occasional inputs that aren’t polled Limited set of pins support interrupts Varies based on arduino model, for example attachInterrupt(pin,routine,mode) routine – pointer to function (function name in C) mode: LOW, CHANGE, RISING, FALLING, HIGH High not always supported on Arduino detachInterrupt(pin)
22
Other Interrupts Analog interrupts Configure based on threshold Timer interrupts Based on clock counter overflowing Direct interrupt lines into CPU
23
Interrupt Synchronization Interrupts are asynchronous Need to synchronize for them noInterrupts() – disables interrupts interrupts() – enables interrupts Can have multiple levels of interrupts
24
Tic Tac Toe How could you use interrupts for tic-tac-toe? Lights? Switches?
25
Interrupt Coding Interrupt routines should do a minimum amount of work Time added to time of executing task Asynchronous -> race conditions can occur Typically these routines just set a flag Possibly read/write value and set flag Possibly read/write from/to circular buffer
26
Interrupt Problems static int iTemperatures[2]; void interrupt vReadTemperatures (void) { iTemperatures[0] = read Dev 1 iTemperatures[1] = read Dev 2 } void main(void) { int iTemp0, iTemp1; // Setup code iTemperatures[0] = 0; iTemperatures[1] = 0; while(TRUE) { iTemp0 = iTemperatures[0]; iTemp1 = iTemperatures[1]; if ( iTemp0 != iTemp1 ) { // Set off alarm! }
27
Homework Read Chapters 10, 11
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.